Skip to content

Commit 11879df

Browse files
authored
Merge pull request #1493 from xWTF/master
fix(typing & documentation): prepared statements usage
2 parents bd95444 + ca76531 commit 11879df

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

documentation/Prepared-Statements.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ connection.execute('select 1 + ? + ? as result', [5, 6], (err, rows) => {
1313
// close cached statement for 'select 1 + ? + ? as result'. noop if not in cache
1414
connection.unprepare('select 1 + ? + ? as result');
1515
```
16+
Note that `connection.execute()` will cache the prepared statement for better performance, remove the cache with `connection.unprepare()` when you're done.
1617

1718
## Manual prepare / execute
1819

20+
Manually prepared statements doesn't comes with LRU cache and SHOULD be closed using `statement.close()` instead of `connection.unprepare()`.
21+
1922
```js
2023
connection.prepare('select ? + ? as tests', (err, statement) => {
2124
// statement.parameters - array of column definitions, length === number of params, here 2
@@ -27,6 +30,7 @@ connection.prepare('select ? + ? as tests', (err, statement) => {
2730
// -> [ { tests: 3 } ]
2831
});
2932

33+
// don't use connection.unprepare(), it won't work!
3034
// note that there is no callback here. There is no statement close ack at protocol level.
3135
statement.close();
3236
});

promise.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ export interface Connection extends EventEmitter {
6262
values: any | any[] | { [param: string]: any }
6363
): Promise<[T, FieldPacket[]]>;
6464

65-
unprepare(sql: string): void;
65+
prepare(options: string | QueryOptions): Promise<PreparedStatementInfo>;
66+
unprepare(sql: string | QueryOptions): void;
6667

6768
end(options?: any): Promise<void>;
6869

@@ -137,3 +138,8 @@ export function createConnection(
137138
config: ConnectionOptions
138139
): Promise<Connection>;
139140
export function createPool(config: PoolOptions): Pool;
141+
142+
export interface PreparedStatementInfo {
143+
close(): Promise<void>;
144+
execute(parameters: any[]): Promise<[RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader, FieldPacket[]]>;
145+
}

0 commit comments

Comments
 (0)