Skip to content

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

@efsant0s

Description

@efsant0s

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,
        }));
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions