Skip to content

Commit 5fc7ed2

Browse files
authored
Merge pull request #1565 from zordtk/master
Add type declarations for Prepare & PrepareStatementInfo
2 parents 2bc513c + 3a964f9 commit 5fc7ed2

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

index.d.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ export interface Connection extends mysql.Connection {
7272
): mysql.Query;
7373
ping(callback?: (err: mysql.QueryError | null) => any): void;
7474
promise(promiseImpl?: PromiseConstructor): PromiseConnection;
75+
unprepare(sql: string): mysql.PrepareStatementInfo;
76+
prepare(sql: string, callback?: (err: mysql.QueryError | null, statement: mysql.PrepareStatementInfo) => any): mysql.Prepare;
7577
}
7678

7779
export interface PoolConnection extends mysql.PoolConnection, Connection {
@@ -149,6 +151,8 @@ export interface Pool extends mysql.Connection {
149151
on(event: 'release', listener: (connection: PoolConnection) => any): this;
150152
on(event: 'enqueue', listener: () => any): this;
151153
promise(promiseImpl?: PromiseConstructor): PromisePool;
154+
unprepare(sql: string): mysql.PrepareStatementInfo;
155+
prepare(sql: string, callback?: (err: mysql.QueryError | null, statement: mysql.PrepareStatementInfo) => any): mysql.Prepare;
152156
}
153157

154158
type authPlugins = (pluginMetadata: {
@@ -183,11 +187,11 @@ export interface ConnectionOptions extends mysql.ConnectionOptions {
183187
}
184188

185189
export interface ConnectionConfig extends ConnectionOptions {
186-
static mergeFlags(defaultFlags: string[], userFlags: string[] | string): number;
187-
static getDefaultFlags(options?: ConnectionOptions): string[];
188-
static getCharsetNumber(charset: string): number;
189-
static getSSLProfile(name: string): { ca: string[] };
190-
static parseUrl(url: string): { host: string, port: number, database: string, user: string, password: string, [key: string]: any };
190+
mergeFlags(defaultFlags: string[], userFlags: string[] | string): number;
191+
getDefaultFlags(options?: ConnectionOptions): string[];
192+
getCharsetNumber(charset: string): number;
193+
getSSLProfile(name: string): { ca: string[] };
194+
parseUrl(url: string): { host: string, port: number, database: string, user: string, password: string, [key: string]: any };
191195
}
192196

193197
export interface PoolOptions extends mysql.PoolOptions, ConnectionOptions {}

typings/mysql/index.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {PoolOptions} from './lib/Pool';
77
import BasePoolCluster = require('./lib/PoolCluster');
88
import {PoolClusterOptions} from './lib/PoolCluster';
99
import BaseQuery = require('./lib/protocol/sequences/Query');
10+
import BasePrepare = require('./lib/protocol/sequences/Prepare');
1011
import {QueryOptions, StreamOptions, QueryError} from './lib/protocol/sequences/Query';
12+
import {PrepareStatementInfo} from 'mysql2/typings/mysql/lib/protocol/sequences/Prepare';
1113

1214
export function createConnection(connectionUri: string): Connection;
1315
export function createConnection(config: BaseConnection.ConnectionOptions): Connection;
@@ -28,7 +30,8 @@ export {
2830
PoolOptions,
2931
PoolClusterOptions,
3032
QueryOptions,
31-
QueryError
33+
QueryError,
34+
PrepareStatementInfo
3235
};
3336
export * from './lib/protocol/packets/index';
3437

@@ -38,3 +41,4 @@ export interface PoolConnection extends BasePoolConnection {}
3841
export interface Pool extends BasePool {}
3942
export interface PoolCluster extends BasePoolCluster {}
4043
export interface Query extends BaseQuery {}
44+
export interface Prepare extends BasePrepare {}

typings/mysql/lib/Connection.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Modifications copyright (c) 2021, Oracle and/or its affiliates.
55

66
import Query = require('./protocol/sequences/Query');
7+
import Prepare = require('./protocol/sequences/Prepare');
78
import {OkPacket, FieldPacket, RowDataPacket, ResultSetHeader} from './protocol/packets/index';
89
import {EventEmitter} from 'events';
910

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import Sequence = require('mysql2/typings/mysql/lib/protocol/sequences/Sequence');
2+
import Query = require('mysql2/typings/mysql/lib/protocol/sequences/Query');
3+
import {OkPacket, FieldPacket, RowDataPacket, ResultSetHeader} from 'mysql2/typings/mysql/lib/protocol/packets';
4+
import {Readable} from 'stream';
5+
6+
declare namespace Prepare {
7+
export class PrepareStatementInfo {
8+
close(): void;
9+
execute<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(
10+
paramaters: any | any[] | { [param: string]: any },
11+
callback?: (
12+
err: Query.QueryError | null,
13+
result: T,
14+
fields: FieldPacket[]
15+
) => any): Query;
16+
}
17+
}
18+
19+
declare class Prepare extends Sequence {
20+
/**
21+
* The SQL for a constructed query
22+
*/
23+
sql: string;
24+
25+
/**
26+
* Emits a query packet to start the query
27+
*/
28+
start(): void;
29+
30+
/**
31+
* Determines the packet class to use given the first byte of the packet.
32+
*
33+
* @param firstByte The first byte of the packet
34+
* @param parser The packet parser
35+
*/
36+
determinePacket(firstByte: number, parser: any): any;
37+
38+
/**
39+
* Creates a Readable stream with the given options
40+
*
41+
* @param options The options for the stream.
42+
*/
43+
stream(options?: Query.StreamOptions): Readable;
44+
45+
on(event: string, listener: Function): this;
46+
on(event: 'error', listener: (err: Query.QueryError) => any): this;
47+
on(event: 'fields', listener: (fields: FieldPacket, index: number) => any): this;
48+
on(event: 'result', listener: (result: RowDataPacket | OkPacket, index: number) => any): this;
49+
on(event: 'end', listener: () => any): this;
50+
}
51+
52+
export = Prepare;

0 commit comments

Comments
 (0)