@@ -3,59 +3,78 @@ var EventEmitter = require('events').EventEmitter;
3
3
var util = require ( 'util' ) ;
4
4
5
5
function inheritEvents ( source , target , events ) {
6
- events
7
- . forEach ( function ( eventName ) {
8
- source . on ( eventName , function ( ) {
9
- var args = [ ] . slice . call ( arguments ) ;
10
- args . unshift ( eventName ) ;
11
-
12
- target . emit . apply ( target , args ) ;
13
- } ) ;
6
+ events . forEach ( function ( eventName ) {
7
+ source . on ( eventName , function ( ) {
8
+ var args = [ ] . slice . call ( arguments ) ;
9
+ args . unshift ( eventName ) ;
10
+
11
+ target . emit . apply ( target , args ) ;
14
12
} ) ;
13
+ } ) ;
15
14
}
16
15
17
- function createConnection ( opts ) {
18
- var coreConnection = core . createConnection ( opts ) ;
19
- var Promise = opts . Promise || global . Promise ;
16
+ function createConnection ( opts ) {
17
+ const coreConnection = core . createConnection ( opts ) ;
18
+ const createConnectionErr = new Error ( ) ;
19
+ const Promise = opts . Promise || global . Promise ;
20
20
if ( ! Promise ) {
21
- throw new Error ( 'no Promise implementation available.' +
22
- 'Use promise-enabled node version or pass userland Promise' +
23
- ' implementation as parameter, for example: { Promise: require(\'bluebird\') }' ) ;
21
+ throw new Error (
22
+ 'no Promise implementation available.' +
23
+ 'Use promise-enabled node version or pass userland Promise' +
24
+ " implementation as parameter, for example: { Promise: require('bluebird') }"
25
+ ) ;
24
26
}
25
- return new Promise ( function ( resolve , reject ) {
26
- coreConnection . once ( 'connect' , function ( connectParams ) {
27
+ return new Promise ( function ( resolve , reject ) {
28
+ coreConnection . once ( 'connect' , function ( connectParams ) {
27
29
resolve ( new PromiseConnection ( coreConnection , Promise ) ) ;
28
30
} ) ;
29
- coreConnection . once ( 'error' , reject ) ;
31
+ coreConnection . once ( 'error' , err => {
32
+ createConnectionErr . message = err . message ;
33
+ createConnectionErr . code = err . code ;
34
+ createConnectionErr . errno = err . errno ;
35
+ createConnectionErr . sqlState = err . sqlState ;
36
+ reject ( createConnectionErr ) ;
37
+ } ) ;
30
38
} ) ;
31
39
}
32
40
33
- function PromiseConnection ( connection , promiseImpl ) {
41
+ function PromiseConnection ( connection , promiseImpl ) {
34
42
this . connection = connection ;
35
43
this . Promise = promiseImpl ;
36
44
37
- inheritEvents ( connection , this , [ 'error' , 'drain' , 'connect' , 'end' , 'enqueue' ] ) ;
45
+ inheritEvents ( connection , this , [
46
+ 'error' ,
47
+ 'drain' ,
48
+ 'connect' ,
49
+ 'end' ,
50
+ 'enqueue'
51
+ ] ) ;
38
52
}
39
53
util . inherits ( PromiseConnection , EventEmitter ) ;
40
54
41
- PromiseConnection . prototype . release = function ( ) {
55
+ PromiseConnection . prototype . release = function ( ) {
42
56
this . connection . release ( ) ;
43
57
} ;
44
58
45
- function makeDoneCb ( resolve , reject ) {
46
- return function ( err , rows , fields ) {
59
+ function makeDoneCb ( resolve , reject , localErr ) {
60
+ return function ( err , rows , fields ) {
47
61
if ( err ) {
48
- reject ( err ) ;
62
+ localErr . message = err . message ;
63
+ localErr . code = err . code ;
64
+ localErr . errno = err . errno ;
65
+ localErr . sqlState = err . sqlState ;
66
+ reject ( localErr ) ;
49
67
} else {
50
68
resolve ( [ rows , fields ] ) ;
51
69
}
52
70
} ;
53
71
}
54
72
55
- PromiseConnection . prototype . query = function ( query , params ) {
56
- var c = this . connection ;
57
- return new this . Promise ( function ( resolve , reject ) {
58
- var done = makeDoneCb ( resolve , reject ) ;
73
+ PromiseConnection . prototype . query = function ( query , params ) {
74
+ const c = this . connection ;
75
+ const localErr = new Error ( ) ;
76
+ return new this . Promise ( function ( resolve , reject ) {
77
+ const done = makeDoneCb ( resolve , reject , localErr ) ;
59
78
if ( params ) {
60
79
c . query ( query , params , done ) ;
61
80
} else {
@@ -64,9 +83,9 @@ PromiseConnection.prototype.query = function (query, params) {
64
83
} ) ;
65
84
} ;
66
85
67
- PromiseConnection . prototype . execute = function ( query , params ) {
86
+ PromiseConnection . prototype . execute = function ( query , params ) {
68
87
var c = this . connection ;
69
- return new this . Promise ( function ( resolve , reject ) {
88
+ return new this . Promise ( function ( resolve , reject ) {
70
89
var done = makeDoneCb ( resolve , reject ) ;
71
90
if ( params ) {
72
91
c . execute ( query , params , done ) ;
@@ -76,50 +95,50 @@ PromiseConnection.prototype.execute = function (query, params) {
76
95
} ) ;
77
96
} ;
78
97
79
- PromiseConnection . prototype . end = function ( ) {
98
+ PromiseConnection . prototype . end = function ( ) {
80
99
var c = this . connection ;
81
- return new this . Promise ( function ( resolve , reject ) {
82
- c . end ( function ( ) {
100
+ return new this . Promise ( function ( resolve , reject ) {
101
+ c . end ( function ( ) {
83
102
resolve ( ) ;
84
103
} ) ;
85
104
} ) ;
86
105
} ;
87
106
88
- PromiseConnection . prototype . beginTransaction = function ( ) {
107
+ PromiseConnection . prototype . beginTransaction = function ( ) {
89
108
var c = this . connection ;
90
- return new this . Promise ( function ( resolve , reject ) {
109
+ return new this . Promise ( function ( resolve , reject ) {
91
110
var done = makeDoneCb ( resolve , reject ) ;
92
111
c . beginTransaction ( done ) ;
93
112
} ) ;
94
113
} ;
95
114
96
- PromiseConnection . prototype . commit = function ( ) {
115
+ PromiseConnection . prototype . commit = function ( ) {
97
116
var c = this . connection ;
98
- return new this . Promise ( function ( resolve , reject ) {
117
+ return new this . Promise ( function ( resolve , reject ) {
99
118
var done = makeDoneCb ( resolve , reject ) ;
100
119
c . commit ( done ) ;
101
120
} ) ;
102
121
} ;
103
122
104
- PromiseConnection . prototype . rollback = function ( ) {
123
+ PromiseConnection . prototype . rollback = function ( ) {
105
124
var c = this . connection ;
106
- return new this . Promise ( function ( resolve , reject ) {
125
+ return new this . Promise ( function ( resolve , reject ) {
107
126
var done = makeDoneCb ( resolve , reject ) ;
108
127
c . rollback ( done ) ;
109
128
} ) ;
110
129
} ;
111
130
112
- PromiseConnection . prototype . ping = function ( ) {
131
+ PromiseConnection . prototype . ping = function ( ) {
113
132
var c = this . connection ;
114
- return new this . Promise ( function ( resolve , reject ) {
133
+ return new this . Promise ( function ( resolve , reject ) {
115
134
c . ping ( resolve ) ;
116
135
} ) ;
117
136
} ;
118
137
119
- PromiseConnection . prototype . connect = function ( ) {
138
+ PromiseConnection . prototype . connect = function ( ) {
120
139
var c = this . connection ;
121
- return new this . Promise ( function ( resolve , reject ) {
122
- c . connect ( function ( error , param ) {
140
+ return new this . Promise ( function ( resolve , reject ) {
141
+ c . connect ( function ( error , param ) {
123
142
if ( error ) {
124
143
reject ( error ) ;
125
144
} else {
@@ -129,29 +148,32 @@ PromiseConnection.prototype.connect = function () {
129
148
} ) ;
130
149
} ;
131
150
132
- PromiseConnection . prototype . prepare = function ( options ) {
151
+ PromiseConnection . prototype . prepare = function ( options ) {
133
152
var c = this . connection ;
134
153
var promiseImpl = this . Promise ;
135
- return new this . Promise ( function ( resolve , reject ) {
136
- c . prepare ( options , function ( error , statement ) {
154
+ return new this . Promise ( function ( resolve , reject ) {
155
+ c . prepare ( options , function ( error , statement ) {
137
156
if ( error ) {
138
157
reject ( error ) ;
139
158
} else {
140
- var wrappedStatement = new PromisePreparedStatementInfo ( statement , promiseImpl ) ;
159
+ var wrappedStatement = new PromisePreparedStatementInfo (
160
+ statement ,
161
+ promiseImpl
162
+ ) ;
141
163
resolve ( wrappedStatement ) ;
142
164
}
143
165
} ) ;
144
166
} ) ;
145
167
} ;
146
168
147
- function PromisePreparedStatementInfo ( statement , promiseImpl ) {
169
+ function PromisePreparedStatementInfo ( statement , promiseImpl ) {
148
170
this . statement = statement ;
149
171
this . Promise = promiseImpl ;
150
172
}
151
173
152
- PromisePreparedStatementInfo . prototype . execute = function ( parameters ) {
174
+ PromisePreparedStatementInfo . prototype . execute = function ( parameters ) {
153
175
var s = this . statement ;
154
- return new this . Promise ( function ( resolve , reject ) {
176
+ return new this . Promise ( function ( resolve , reject ) {
155
177
var done = makeDoneCb ( resolve , reject ) ;
156
178
if ( parameters ) {
157
179
s . execute ( parameters , done ) ;
@@ -161,9 +183,9 @@ PromisePreparedStatementInfo.prototype.execute = function (parameters) {
161
183
} ) ;
162
184
} ;
163
185
164
- PromisePreparedStatementInfo . prototype . close = function ( ) {
186
+ PromisePreparedStatementInfo . prototype . close = function ( ) {
165
187
var s = this . statement ;
166
- return new this . Promise ( function ( resolve , reject ) {
188
+ return new this . Promise ( function ( resolve , reject ) {
167
189
s . close ( ) ;
168
190
resolve ( ) ;
169
191
} ) ;
@@ -177,26 +199,26 @@ PromisePreparedStatementInfo.prototype.close = function () {
177
199
// implemented with PromiseConnection
178
200
179
201
// proxy synchronous functions only
180
- ( function ( functionsToWrap ) {
181
-
202
+ ( function ( functionsToWrap ) {
182
203
for ( var i = 0 ; functionsToWrap && i < functionsToWrap . length ; i ++ ) {
183
204
var func = functionsToWrap [ i ] ;
184
205
185
206
if (
186
- typeof core . Connection . prototype [ func ] === 'function'
187
- && PromiseConnection . prototype [ func ] === undefined
207
+ typeof core . Connection . prototype [ func ] === 'function' &&
208
+ PromiseConnection . prototype [ func ] === undefined
188
209
) {
189
- PromiseConnection . prototype [ func ] = ( function factory ( funcName ) {
190
- return function ( ) {
191
- return core . Connection
192
- . prototype [ funcName ] . apply ( this . connection , arguments ) ;
210
+ PromiseConnection . prototype [ func ] = ( function factory ( funcName ) {
211
+ return function ( ) {
212
+ return core . Connection . prototype [ funcName ] . apply (
213
+ this . connection ,
214
+ arguments
215
+ ) ;
193
216
} ;
194
217
} ) ( func ) ;
195
218
}
196
219
}
197
-
198
220
} ) ( [
199
- // synchronous functions
221
+ // synchronous functions
200
222
'close' ,
201
223
'createBinlogStream' ,
202
224
'destroy' ,
@@ -212,16 +234,16 @@ PromisePreparedStatementInfo.prototype.close = function () {
212
234
function PromisePool ( pool , Promise ) {
213
235
this . pool = pool ;
214
236
this . Promise = Promise ;
215
-
237
+
216
238
inheritEvents ( pool , this , [ 'acquire' , 'connection' , 'enqueue' , 'release' ] ) ;
217
239
}
218
240
util . inherits ( PromisePool , EventEmitter ) ;
219
241
220
- PromisePool . prototype . getConnection = function ( ) {
242
+ PromisePool . prototype . getConnection = function ( ) {
221
243
var corePool = this . pool ;
222
244
223
- return new this . Promise ( function ( resolve , reject ) {
224
- corePool . getConnection ( function ( err , coreConnection ) {
245
+ return new this . Promise ( function ( resolve , reject ) {
246
+ corePool . getConnection ( function ( err , coreConnection ) {
225
247
if ( err ) {
226
248
reject ( err ) ;
227
249
} else {
@@ -231,10 +253,10 @@ PromisePool.prototype.getConnection = function () {
231
253
} ) ;
232
254
} ;
233
255
234
- PromisePool . prototype . query = function ( sql , args ) {
256
+ PromisePool . prototype . query = function ( sql , args ) {
235
257
var corePool = this . pool ;
236
258
237
- return new this . Promise ( function ( resolve , reject ) {
259
+ return new this . Promise ( function ( resolve , reject ) {
238
260
var done = makeDoneCb ( resolve , reject ) ;
239
261
if ( args ) {
240
262
corePool . query ( sql , args , done ) ;
@@ -244,19 +266,19 @@ PromisePool.prototype.query = function (sql, args) {
244
266
} ) ;
245
267
} ;
246
268
247
- PromisePool . prototype . execute = function ( sql , values ) {
269
+ PromisePool . prototype . execute = function ( sql , values ) {
248
270
var corePool = this . pool ;
249
271
250
- return new Promise ( function ( resolve , reject ) {
272
+ return new Promise ( function ( resolve , reject ) {
251
273
corePool . execute ( sql , values , makeDoneCb ( resolve , reject ) ) ;
252
274
} ) ;
253
275
} ;
254
276
255
- PromisePool . prototype . end = function ( ) {
277
+ PromisePool . prototype . end = function ( ) {
256
278
var corePool = this . pool ;
257
279
258
- return new Promise ( function ( resolve , reject ) {
259
- corePool . end ( function ( err ) {
280
+ return new Promise ( function ( resolve , reject ) {
281
+ corePool . end ( function ( err ) {
260
282
if ( err ) {
261
283
reject ( err ) ;
262
284
} else {
@@ -266,13 +288,15 @@ PromisePool.prototype.end = function () {
266
288
} ) ;
267
289
} ;
268
290
269
- function createPool ( opts ) {
291
+ function createPool ( opts ) {
270
292
var corePool = core . createPool ( opts ) ;
271
293
var Promise = opts . Promise || global . Promise ;
272
294
if ( ! Promise ) {
273
- throw new Error ( 'no Promise implementation available.' +
274
- 'Use promise-enabled node version or pass userland Promise' +
275
- ' implementation as parameter, for example: { Promise: require(\'bluebird\') }' ) ;
295
+ throw new Error (
296
+ 'no Promise implementation available.' +
297
+ 'Use promise-enabled node version or pass userland Promise' +
298
+ " implementation as parameter, for example: { Promise: require('bluebird') }"
299
+ ) ;
276
300
}
277
301
278
302
return new PromisePool ( corePool , Promise ) ;
0 commit comments