Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the current proper way to set up parameters for a query ? #40

Open
efsant0s opened this issue Nov 14, 2023 · 0 comments
Open

What is the current proper way to set up parameters for a query ? #40

efsant0s opened this issue Nov 14, 2023 · 0 comments

Comments

@efsant0s
Copy link

efsant0s commented Nov 14, 2023

Hello, I'm trying to utilize the latest version, but I'm currently struggling to pass parameters to my query.
Doesn't matter what I do, I receive an stop application exception without stack.

My existing database script:

CREATE TABLE IF NOT EXISTS unit_area (
    serial_area VARCHAR(50),
    created VARCHAR(50),
    updated VARCHAR(50),
    label VARCHAR(50),
    format VARCHAR(50),
    decimal VARCHAR(50),
    PRIMARY KEY (serial_area)
);

My main class:

const dynamicImport = async (packageName: string) => new Function(`return import('${packageName}')`)(); 
import { QueryImmudb } from './database.dto';
import { SqlExecProps } from '@codenotary/immudb-node/dist/immu-api/sql-exec';
import { HttpStatus, Injectable } from '@nestjs/common'; 
@Injectable()
export class ImmudbService extends MainService {

    public async insertData(queryBuilder: QueryImmudb): Promise<boolean> {
        const con = await this.getConnection();
        try {
            this.logDebug("[ImmudbService] [insertData] INIT-->", { sql: queryBuilder.sql, params: queryBuilder.params });
            const tst = queryBuilder.getParams();
            console.log(tst)
            console.log(queryBuilder.sql)
            const sqlExecReq: SqlExecProps = {
                sql: queryBuilder.sql,
                params: [
                    {name: ':label', type: 'VARCHAR', value: 'TESTE'},
                    {name: ':format', type: 'VARCHAR', value: 'TESTE'},
                    {name: ':decimal', type: 'VARCHAR', value: 'TESTE'},
                       ]
            }; 
            const sqlExecRes = await con.sqlExec(sqlExecReq);

            this.logInfo("[ImmudbService] [insertData] SUCCESS:", sqlExecRes);
            return true;

        } catch (ex: any) {
            console.error(ex.message)
            throw new ErrorManager("ImmudbService-->insertData", ex.message,
                null, null, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}

My QueryImmudb parameters:{

    const insertQuery = new QueryImmudb();
    insertQuery.sql = `
    INSERT INTO unit_date (
       serial_date,
       created ,
       updated,
       label,
       format,
       decimal
    )
    VALUES (
       ${'cast(random_uuid() as VARCHAR)'},
       cast( NOW() as VARCHAR) ,
       cast( NOW() as VARCHAR) ,
       :label,
       :format,
       :decimal
    )
 `;
 
 // Add the remaining parameters using addParams
 insertQuery.addParams('label', 'Date Label 1');
 insertQuery.addParams('format', 'YYYY-MM-DD');
 insertQuery.addParams('decimal', '2'); 
}

My QueryImmudb class:

export class QueryImmudb {

    sql: string;
    params: { name: string; type: string; value: any }[];

    constructor() {
        this.sql = '';
        this.params = [];
    }

    addParams(column: string, value: any, type: string = 'VARCHAR'): void {
        const param: { name: string; type: string; value: any } = {
            name: `:${column}`,
            type: type,
            value: value,   
        };
        this.params.push(param);
    }

    getParams(): any[] {
        return this.params.map(param => ({
            name: param.name,
            type: param.type, // Convert type to SqlType
            value: param.value,
        }));
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant