Skip to content

Commit 8fa81bf

Browse files
committed
feat: use sql params
1 parent a1a801d commit 8fa81bf

File tree

5 files changed

+15
-18
lines changed

5 files changed

+15
-18
lines changed

src/connection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export class RDSConnection extends Operator {
3232
return this.conn.release();
3333
}
3434

35-
async _query(sql: string) {
36-
return await this.conn.query(sql);
35+
async _query(sql: string, values?: object | any[]) {
36+
return await this.conn.query(sql, values);
3737
}
3838

3939
async beginTransaction() {

src/operator.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ export abstract class Operator {
7575

7676
async query<T = any>(sql: string, values?: object | any[]): Promise<T> {
7777
// query(sql, values)
78-
if (values) {
79-
sql = this.format(sql, values);
80-
}
8178
if (this.beforeQueryHandlers.length > 0) {
8279
for (const beforeQueryHandler of this.beforeQueryHandlers) {
8380
const newSql = beforeQueryHandler(sql);
@@ -98,7 +95,7 @@ export abstract class Operator {
9895
connection: this.#connection,
9996
} as QueryStartMessage);
10097
try {
101-
rows = await this._query(sql);
98+
rows = await this._query(sql, values);
10299
if (Array.isArray(rows)) {
103100
debug('[connection#%s] query get %o rows', this.threadId, rows.length);
104101
} else {
@@ -132,7 +129,7 @@ export abstract class Operator {
132129
}
133130

134131
// eslint-disable-next-line @typescript-eslint/no-unused-vars
135-
protected async _query(_sql: string): Promise<any> {
132+
protected async _query(_sql: string, _values?: object | any[]): Promise<any> {
136133
throw new Error('SubClass must impl this');
137134
}
138135

src/transaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ export class RDSTransaction extends Operator {
3636
}
3737
}
3838

39-
protected async _query(sql: string) {
39+
protected async _query(sql: string, values?: object | any[]) {
4040
this.#check();
41-
return await this.conn!._query(sql);
41+
return await this.conn!._query(sql, values);
4242
}
4343

4444
#check() {

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface RDSClientOptions extends PoolOptions {
1313
}
1414

1515
export interface PoolConnectionPromisify extends Omit<PoolConnection, 'query'> {
16-
query(sql: string): Promise<any>;
16+
query(sql: string, values: any | any[] | { [param: string]: any }): Promise<any>;
1717
beginTransaction(): Promise<void>;
1818
commit(): Promise<void>;
1919
rollback(): Promise<void>;

test/client.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ describe('test/client.test.ts', () => {
357357
[ table, prefix + 'm@fengmk2.com' ]);
358358
assert.deepEqual(mockLogs, [
359359
'show tables',
360-
`select * from \`${table}\` where email = '${prefix + 'm@fengmk2.com'}' limit 1`,
360+
'select * from ?? where email = ? limit 1',
361361
]);
362362
});
363363
});
@@ -1478,7 +1478,7 @@ describe('test/client.test.ts', () => {
14781478
counter2After++;
14791479
});
14801480
await db.query('select * from ?? limit 10', [ table ]);
1481-
assert.equal(lastSql, 'select * from `myrds-test-user` limit 10');
1481+
assert.equal(lastSql, 'select * from ?? limit 10');
14821482
assert.equal(lastArgs[0], lastSql);
14831483
assert.equal(Array.isArray(lastArgs[1]), true);
14841484
assert.equal(count, 1);
@@ -1491,8 +1491,8 @@ describe('test/client.test.ts', () => {
14911491
values(?, ?, now(), now())`,
14921492
[ table, prefix + 'beginTransactionScope1', prefix + 'm@beginTransactionScope1.com' ]);
14931493
});
1494-
assert.equal(lastSql, 'insert into `myrds-test-user`(name, email, gmt_create, gmt_modified)\n' +
1495-
` values('${prefix}beginTransactionScope1', '${prefix}m@beginTransactionScope1.com', now(), now())`);
1494+
assert.equal(lastSql, 'insert into ??(name, email, gmt_create, gmt_modified)\n' +
1495+
' values(?, ?, now(), now())');
14961496
assert.equal(lastArgs[0], lastSql);
14971497
assert.equal(lastArgs[1].affectedRows, 1);
14981498
assert.equal(count, 2);
@@ -1502,8 +1502,8 @@ describe('test/client.test.ts', () => {
15021502
values(?, ?, now(), now())`,
15031503
[ table, prefix + 'beginDoomedTransactionScope1', prefix + 'm@beginDoomedTransactionScope1.com' ]);
15041504
});
1505-
assert.equal(lastSql, 'insert into `myrds-test-user`(name, email, gmt_create, gmt_modified)\n' +
1506-
` values('${prefix}beginDoomedTransactionScope1', '${prefix}m@beginDoomedTransactionScope1.com', now(), now())`);
1505+
assert.equal(lastSql, 'insert into ??(name, email, gmt_create, gmt_modified)\n' +
1506+
' values(?, ?, now(), now())');
15071507
assert.equal(lastArgs[0], lastSql);
15081508
assert.equal(lastArgs[1].affectedRows, 1);
15091509
assert.equal(count, 3);
@@ -1514,8 +1514,8 @@ describe('test/client.test.ts', () => {
15141514
values(?, ?, now(), now())`,
15151515
[ table, prefix + 'transaction1', prefix + 'm@transaction1.com' ]);
15161516
await conn.commit();
1517-
assert.equal(lastSql, 'insert into `myrds-test-user`(name, email, gmt_create, gmt_modified)\n' +
1518-
` values('${prefix}transaction1', '${prefix}m@transaction1.com', now(), now())`);
1517+
assert.equal(lastSql, 'insert into ??(name, email, gmt_create, gmt_modified)\n' +
1518+
' values(?, ?, now(), now())');
15191519
assert.equal(lastArgs[0], lastSql);
15201520
assert.equal(lastArgs[1].affectedRows, 1);
15211521
assert.equal(count, 4);

0 commit comments

Comments
 (0)