From 8fa81bf9d2447828f75c38b8797a7f8e735df882 Mon Sep 17 00:00:00 2001 From: killagu Date: Tue, 25 Nov 2025 15:46:16 +0800 Subject: [PATCH 1/4] feat: use sql params --- src/connection.ts | 4 ++-- src/operator.ts | 7 ++----- src/transaction.ts | 4 ++-- src/types.ts | 2 +- test/client.test.ts | 16 ++++++++-------- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/connection.ts b/src/connection.ts index 2daaa39..09f2dcc 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -32,8 +32,8 @@ export class RDSConnection extends Operator { return this.conn.release(); } - async _query(sql: string) { - return await this.conn.query(sql); + async _query(sql: string, values?: object | any[]) { + return await this.conn.query(sql, values); } async beginTransaction() { diff --git a/src/operator.ts b/src/operator.ts index 4562cff..d991c79 100644 --- a/src/operator.ts +++ b/src/operator.ts @@ -75,9 +75,6 @@ export abstract class Operator { async query(sql: string, values?: object | any[]): Promise { // query(sql, values) - if (values) { - sql = this.format(sql, values); - } if (this.beforeQueryHandlers.length > 0) { for (const beforeQueryHandler of this.beforeQueryHandlers) { const newSql = beforeQueryHandler(sql); @@ -98,7 +95,7 @@ export abstract class Operator { connection: this.#connection, } as QueryStartMessage); try { - rows = await this._query(sql); + rows = await this._query(sql, values); if (Array.isArray(rows)) { debug('[connection#%s] query get %o rows', this.threadId, rows.length); } else { @@ -132,7 +129,7 @@ export abstract class Operator { } // eslint-disable-next-line @typescript-eslint/no-unused-vars - protected async _query(_sql: string): Promise { + protected async _query(_sql: string, _values?: object | any[]): Promise { throw new Error('SubClass must impl this'); } diff --git a/src/transaction.ts b/src/transaction.ts index c655b40..9fa03fc 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -36,9 +36,9 @@ export class RDSTransaction extends Operator { } } - protected async _query(sql: string) { + protected async _query(sql: string, values?: object | any[]) { this.#check(); - return await this.conn!._query(sql); + return await this.conn!._query(sql, values); } #check() { diff --git a/src/types.ts b/src/types.ts index 695ee20..7811c3f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -13,7 +13,7 @@ export interface RDSClientOptions extends PoolOptions { } export interface PoolConnectionPromisify extends Omit { - query(sql: string): Promise; + query(sql: string, values: any | any[] | { [param: string]: any }): Promise; beginTransaction(): Promise; commit(): Promise; rollback(): Promise; diff --git a/test/client.test.ts b/test/client.test.ts index b1da0a5..d690d9d 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -357,7 +357,7 @@ describe('test/client.test.ts', () => { [ table, prefix + 'm@fengmk2.com' ]); assert.deepEqual(mockLogs, [ 'show tables', - `select * from \`${table}\` where email = '${prefix + 'm@fengmk2.com'}' limit 1`, + 'select * from ?? where email = ? limit 1', ]); }); }); @@ -1478,7 +1478,7 @@ describe('test/client.test.ts', () => { counter2After++; }); await db.query('select * from ?? limit 10', [ table ]); - assert.equal(lastSql, 'select * from `myrds-test-user` limit 10'); + assert.equal(lastSql, 'select * from ?? limit 10'); assert.equal(lastArgs[0], lastSql); assert.equal(Array.isArray(lastArgs[1]), true); assert.equal(count, 1); @@ -1491,8 +1491,8 @@ describe('test/client.test.ts', () => { values(?, ?, now(), now())`, [ table, prefix + 'beginTransactionScope1', prefix + 'm@beginTransactionScope1.com' ]); }); - assert.equal(lastSql, 'insert into `myrds-test-user`(name, email, gmt_create, gmt_modified)\n' + - ` values('${prefix}beginTransactionScope1', '${prefix}m@beginTransactionScope1.com', now(), now())`); + assert.equal(lastSql, 'insert into ??(name, email, gmt_create, gmt_modified)\n' + + ' values(?, ?, now(), now())'); assert.equal(lastArgs[0], lastSql); assert.equal(lastArgs[1].affectedRows, 1); assert.equal(count, 2); @@ -1502,8 +1502,8 @@ describe('test/client.test.ts', () => { values(?, ?, now(), now())`, [ table, prefix + 'beginDoomedTransactionScope1', prefix + 'm@beginDoomedTransactionScope1.com' ]); }); - assert.equal(lastSql, 'insert into `myrds-test-user`(name, email, gmt_create, gmt_modified)\n' + - ` values('${prefix}beginDoomedTransactionScope1', '${prefix}m@beginDoomedTransactionScope1.com', now(), now())`); + assert.equal(lastSql, 'insert into ??(name, email, gmt_create, gmt_modified)\n' + + ' values(?, ?, now(), now())'); assert.equal(lastArgs[0], lastSql); assert.equal(lastArgs[1].affectedRows, 1); assert.equal(count, 3); @@ -1514,8 +1514,8 @@ describe('test/client.test.ts', () => { values(?, ?, now(), now())`, [ table, prefix + 'transaction1', prefix + 'm@transaction1.com' ]); await conn.commit(); - assert.equal(lastSql, 'insert into `myrds-test-user`(name, email, gmt_create, gmt_modified)\n' + - ` values('${prefix}transaction1', '${prefix}m@transaction1.com', now(), now())`); + assert.equal(lastSql, 'insert into ??(name, email, gmt_create, gmt_modified)\n' + + ' values(?, ?, now(), now())'); assert.equal(lastArgs[0], lastSql); assert.equal(lastArgs[1].affectedRows, 1); assert.equal(count, 4); From df0ba7121280f7a050d632d7135ffa01c96e9c34 Mon Sep 17 00:00:00 2001 From: killa Date: Tue, 25 Nov 2025 15:49:25 +0800 Subject: [PATCH 2/4] Update src/types.ts Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Signed-off-by: killa --- src/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index 7811c3f..635c859 100644 --- a/src/types.ts +++ b/src/types.ts @@ -13,7 +13,7 @@ export interface RDSClientOptions extends PoolOptions { } export interface PoolConnectionPromisify extends Omit { - query(sql: string, values: any | any[] | { [param: string]: any }): Promise; + query(sql: string, values?: any | any[] | { [param: string]: any }): Promise; beginTransaction(): Promise; commit(): Promise; rollback(): Promise; From 45ce6ac85b5492c7eb49a853662ac0afb2f73bee Mon Sep 17 00:00:00 2001 From: killagu Date: Tue, 25 Nov 2025 15:53:35 +0800 Subject: [PATCH 3/4] f --- src/channels.ts | 2 ++ src/operator.ts | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/channels.ts b/src/channels.ts index e867b39..fd2af2c 100644 --- a/src/channels.ts +++ b/src/channels.ts @@ -24,12 +24,14 @@ export interface ConnectionEnqueueMessage { export interface QueryStartMessage { connection: PoolConnectionPromisify; + values?: object | any[]; sql: string; } export interface QueryEndMessage { connection: PoolConnectionPromisify; sql: string; + values?: object | any[]; duration: number; error?: Error; } diff --git a/src/operator.ts b/src/operator.ts index d991c79..bb5575e 100644 --- a/src/operator.ts +++ b/src/operator.ts @@ -92,6 +92,7 @@ export abstract class Operator { let lastError: Error | undefined; channels.queryStart.publish({ sql, + values, connection: this.#connection, } as QueryStartMessage); try { @@ -112,6 +113,7 @@ export abstract class Operator { channels.queryEnd.publish({ sql, connection: this.#connection, + values, duration, error: lastError, } as QueryEndMessage); From e820e8aa680b57ad46c7264815142430423cf942 Mon Sep 17 00:00:00 2001 From: killagu Date: Tue, 25 Nov 2025 15:58:15 +0800 Subject: [PATCH 4/4] f --- src/operator.ts | 4 ++-- src/types.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/operator.ts b/src/operator.ts index bb5575e..7b9fbe3 100644 --- a/src/operator.ts +++ b/src/operator.ts @@ -77,7 +77,7 @@ export abstract class Operator { // query(sql, values) if (this.beforeQueryHandlers.length > 0) { for (const beforeQueryHandler of this.beforeQueryHandlers) { - const newSql = beforeQueryHandler(sql); + const newSql = beforeQueryHandler(sql, values); if (newSql) { sql = newSql; } @@ -119,7 +119,7 @@ export abstract class Operator { } as QueryEndMessage); if (this.afterQueryHandlers.length > 0) { for (const afterQueryHandler of this.afterQueryHandlers) { - afterQueryHandler(sql, rows, duration, lastError); + afterQueryHandler(sql, rows, duration, lastError, values); } } } diff --git a/src/types.ts b/src/types.ts index 635c859..b3f6956 100644 --- a/src/types.ts +++ b/src/types.ts @@ -63,8 +63,8 @@ export type LockTableOption = { tableAlias: string; }; -export type BeforeQueryHandler = (sql: string) => string | undefined | void; -export type AfterQueryHandler = (sql: string, result: any, execDuration: number, err?: Error) => void; +export type BeforeQueryHandler = (sql: string, values?: object | any[]) => string | undefined | void; +export type AfterQueryHandler = (sql: string, result: any, execDuration: number, err?: Error, values?: object | any[]) => void; export type TransactionContext = Record; export type TransactionScope = (transaction: RDSTransaction) => Promise;