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/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..7b9fbe3 100644 --- a/src/operator.ts +++ b/src/operator.ts @@ -75,12 +75,9 @@ 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); + const newSql = beforeQueryHandler(sql, values); if (newSql) { sql = newSql; } @@ -95,10 +92,11 @@ export abstract class Operator { let lastError: Error | undefined; channels.queryStart.publish({ sql, + values, 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 { @@ -115,12 +113,13 @@ export abstract class Operator { channels.queryEnd.publish({ sql, connection: this.#connection, + values, duration, error: lastError, } as QueryEndMessage); if (this.afterQueryHandlers.length > 0) { for (const afterQueryHandler of this.afterQueryHandlers) { - afterQueryHandler(sql, rows, duration, lastError); + afterQueryHandler(sql, rows, duration, lastError, values); } } } @@ -132,7 +131,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..b3f6956 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; @@ -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; 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);