oceanbase 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/License +19 -0
- package/README.md +250 -0
- package/index.d.ts +1 -0
- package/index.js +77 -0
- package/lib/auth_41.js +95 -0
- package/lib/auth_plugins/caching_sha2_password.js +108 -0
- package/lib/auth_plugins/caching_sha2_password.md +18 -0
- package/lib/auth_plugins/index.js +8 -0
- package/lib/auth_plugins/mysql_clear_password.js +17 -0
- package/lib/auth_plugins/mysql_native_password.js +34 -0
- package/lib/auth_plugins/sha256_password.js +68 -0
- package/lib/base/connection.js +978 -0
- package/lib/base/pool.js +237 -0
- package/lib/base/pool_connection.js +70 -0
- package/lib/commands/auth_switch.js +111 -0
- package/lib/commands/binlog_dump.js +109 -0
- package/lib/commands/change_user.js +68 -0
- package/lib/commands/client_handshake.js +241 -0
- package/lib/commands/close_statement.js +18 -0
- package/lib/commands/command.js +54 -0
- package/lib/commands/execute.js +112 -0
- package/lib/commands/index.js +27 -0
- package/lib/commands/ping.js +36 -0
- package/lib/commands/prepare.js +143 -0
- package/lib/commands/query.js +366 -0
- package/lib/commands/quit.js +29 -0
- package/lib/commands/register_slave.js +27 -0
- package/lib/commands/server_handshake.js +203 -0
- package/lib/compressed_protocol.js +127 -0
- package/lib/connection.js +12 -0
- package/lib/connection_config.js +326 -0
- package/lib/constants/charset_encodings.js +316 -0
- package/lib/constants/charsets.js +317 -0
- package/lib/constants/client.js +40 -0
- package/lib/constants/commands.js +36 -0
- package/lib/constants/cursor.js +8 -0
- package/lib/constants/encoding_charset.js +50 -0
- package/lib/constants/errors.js +3973 -0
- package/lib/constants/field_flags.js +20 -0
- package/lib/constants/server_status.js +44 -0
- package/lib/constants/session_track.js +11 -0
- package/lib/constants/ssl_profiles.js +11 -0
- package/lib/constants/types.js +64 -0
- package/lib/create_connection.js +10 -0
- package/lib/create_pool.js +10 -0
- package/lib/create_pool_cluster.js +9 -0
- package/lib/helpers.js +86 -0
- package/lib/packet_parser.js +195 -0
- package/lib/packets/auth_next_factor.js +35 -0
- package/lib/packets/auth_switch_request.js +38 -0
- package/lib/packets/auth_switch_request_more_data.js +33 -0
- package/lib/packets/auth_switch_response.js +30 -0
- package/lib/packets/binary_row.js +95 -0
- package/lib/packets/binlog_dump.js +33 -0
- package/lib/packets/binlog_query_statusvars.js +115 -0
- package/lib/packets/change_user.js +97 -0
- package/lib/packets/close_statement.js +21 -0
- package/lib/packets/column_definition.js +291 -0
- package/lib/packets/execute.js +214 -0
- package/lib/packets/handshake.js +112 -0
- package/lib/packets/handshake_response.js +144 -0
- package/lib/packets/index.js +152 -0
- package/lib/packets/packet.js +931 -0
- package/lib/packets/prepare_statement.js +27 -0
- package/lib/packets/prepared_statement_header.js +16 -0
- package/lib/packets/query.js +27 -0
- package/lib/packets/register_slave.js +46 -0
- package/lib/packets/resultset_header.js +124 -0
- package/lib/packets/ssl_request.js +25 -0
- package/lib/packets/text_row.js +47 -0
- package/lib/parsers/binary_parser.js +235 -0
- package/lib/parsers/parser_cache.js +68 -0
- package/lib/parsers/static_binary_parser.js +213 -0
- package/lib/parsers/static_text_parser.js +152 -0
- package/lib/parsers/string.js +50 -0
- package/lib/parsers/text_parser.js +214 -0
- package/lib/pool.js +12 -0
- package/lib/pool_cluster.js +369 -0
- package/lib/pool_config.js +30 -0
- package/lib/pool_connection.js +12 -0
- package/lib/promise/connection.js +222 -0
- package/lib/promise/inherit_events.js +27 -0
- package/lib/promise/make_done_cb.js +19 -0
- package/lib/promise/pool.js +112 -0
- package/lib/promise/pool_cluster.js +54 -0
- package/lib/promise/pool_connection.js +19 -0
- package/lib/promise/prepared_statement_info.js +32 -0
- package/lib/results_stream.js +38 -0
- package/lib/server.js +37 -0
- package/package.json +80 -0
- package/promise.d.ts +131 -0
- package/promise.js +202 -0
- package/typings/mysql/LICENSE.txt +15 -0
- package/typings/mysql/index.d.ts +95 -0
- package/typings/mysql/info.txt +1 -0
- package/typings/mysql/lib/Auth.d.ts +30 -0
- package/typings/mysql/lib/Connection.d.ts +453 -0
- package/typings/mysql/lib/Pool.d.ts +69 -0
- package/typings/mysql/lib/PoolCluster.d.ts +90 -0
- package/typings/mysql/lib/PoolConnection.d.ts +10 -0
- package/typings/mysql/lib/Server.d.ts +11 -0
- package/typings/mysql/lib/constants/CharsetToEncoding.d.ts +8 -0
- package/typings/mysql/lib/constants/Charsets.d.ts +326 -0
- package/typings/mysql/lib/constants/Types.d.ts +70 -0
- package/typings/mysql/lib/constants/index.d.ts +5 -0
- package/typings/mysql/lib/parsers/ParserCache.d.ts +4 -0
- package/typings/mysql/lib/parsers/index.d.ts +18 -0
- package/typings/mysql/lib/parsers/typeCast.d.ts +54 -0
- package/typings/mysql/lib/protocol/packets/Field.d.ts +10 -0
- package/typings/mysql/lib/protocol/packets/FieldPacket.d.ts +27 -0
- package/typings/mysql/lib/protocol/packets/OkPacket.d.ts +23 -0
- package/typings/mysql/lib/protocol/packets/ProcedurePacket.d.ts +13 -0
- package/typings/mysql/lib/protocol/packets/ResultSetHeader.d.ts +18 -0
- package/typings/mysql/lib/protocol/packets/RowDataPacket.d.ts +9 -0
- package/typings/mysql/lib/protocol/packets/index.d.ts +28 -0
- package/typings/mysql/lib/protocol/packets/params/ErrorPacketParams.d.ts +6 -0
- package/typings/mysql/lib/protocol/packets/params/OkPacketParams.d.ts +9 -0
- package/typings/mysql/lib/protocol/sequences/ExecutableBase.d.ts +40 -0
- package/typings/mysql/lib/protocol/sequences/Prepare.d.ts +65 -0
- package/typings/mysql/lib/protocol/sequences/Query.d.ts +170 -0
- package/typings/mysql/lib/protocol/sequences/QueryableBase.d.ts +40 -0
- package/typings/mysql/lib/protocol/sequences/Sequence.d.ts +5 -0
- package/typings/mysql/lib/protocol/sequences/promise/ExecutableBase.d.ts +21 -0
- package/typings/mysql/lib/protocol/sequences/promise/QueryableBase.d.ts +21 -0
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const process = require('process');
|
|
4
|
+
|
|
5
|
+
const Pool = require('./pool.js');
|
|
6
|
+
const PoolConfig = require('./pool_config.js');
|
|
7
|
+
const Connection = require('./connection.js');
|
|
8
|
+
const EventEmitter = require('events').EventEmitter;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Selector
|
|
12
|
+
*/
|
|
13
|
+
const makeSelector = {
|
|
14
|
+
RR() {
|
|
15
|
+
let index = 0;
|
|
16
|
+
return (clusterIds) => clusterIds[index++ % clusterIds.length];
|
|
17
|
+
},
|
|
18
|
+
RANDOM() {
|
|
19
|
+
return (clusterIds) =>
|
|
20
|
+
clusterIds[Math.floor(Math.random() * clusterIds.length)];
|
|
21
|
+
},
|
|
22
|
+
ORDER() {
|
|
23
|
+
return (clusterIds) => clusterIds[0];
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const getMonotonicMilliseconds = function () {
|
|
28
|
+
let ms;
|
|
29
|
+
|
|
30
|
+
if (typeof process.hrtime === 'function') {
|
|
31
|
+
ms = process.hrtime();
|
|
32
|
+
ms = ms[0] * 1e3 + ms[1] * 1e-6;
|
|
33
|
+
} else {
|
|
34
|
+
ms = process.uptime() * 1000;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return Math.floor(ms);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const patternRegExp = function (pattern) {
|
|
41
|
+
if (pattern instanceof RegExp) {
|
|
42
|
+
return pattern;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const source = pattern
|
|
46
|
+
.replace(/([.+?^=!:${}()|[\]/\\])/g, '\\$1')
|
|
47
|
+
.replace(/\*/g, '.*');
|
|
48
|
+
|
|
49
|
+
return new RegExp(`^${source}$`);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
class PoolNamespace {
|
|
53
|
+
constructor(cluster, pattern, selector) {
|
|
54
|
+
this._cluster = cluster;
|
|
55
|
+
this._pattern = pattern;
|
|
56
|
+
this._selector = makeSelector[selector]();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
getConnection(cb) {
|
|
60
|
+
const clusterNode = this._getClusterNode();
|
|
61
|
+
if (clusterNode === null) {
|
|
62
|
+
let err = new Error('Pool does Not exist.');
|
|
63
|
+
err.code = 'POOL_NOEXIST';
|
|
64
|
+
|
|
65
|
+
if (this._cluster._findNodeIds(this._pattern, true).length !== 0) {
|
|
66
|
+
err = new Error('Pool does Not have online node.');
|
|
67
|
+
err.code = 'POOL_NONEONLINE';
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return cb(err);
|
|
71
|
+
}
|
|
72
|
+
return this._cluster._getConnection(clusterNode, (err, connection) => {
|
|
73
|
+
if (err) {
|
|
74
|
+
if (
|
|
75
|
+
this._cluster._canRetry &&
|
|
76
|
+
this._cluster._findNodeIds(this._pattern).length !== 0
|
|
77
|
+
) {
|
|
78
|
+
this._cluster.emit('warn', err);
|
|
79
|
+
return this.getConnection(cb);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return cb(err);
|
|
83
|
+
}
|
|
84
|
+
return cb(null, connection);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* pool cluster query
|
|
90
|
+
* @param {*} sql
|
|
91
|
+
* @param {*} values
|
|
92
|
+
* @param {*} cb
|
|
93
|
+
* @returns query
|
|
94
|
+
*/
|
|
95
|
+
query(sql, values, cb) {
|
|
96
|
+
const query = Connection.createQuery(sql, values, cb, {});
|
|
97
|
+
this.getConnection((err, conn) => {
|
|
98
|
+
if (err) {
|
|
99
|
+
if (typeof query.onResult === 'function') {
|
|
100
|
+
query.onResult(err);
|
|
101
|
+
} else {
|
|
102
|
+
query.emit('error', err);
|
|
103
|
+
}
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
try {
|
|
107
|
+
conn.query(query).once('end', () => {
|
|
108
|
+
conn.release();
|
|
109
|
+
});
|
|
110
|
+
} catch (e) {
|
|
111
|
+
conn.release();
|
|
112
|
+
throw e;
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
return query;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* pool cluster execute
|
|
120
|
+
* @param {*} sql
|
|
121
|
+
* @param {*} values
|
|
122
|
+
* @param {*} cb
|
|
123
|
+
*/
|
|
124
|
+
execute(sql, values, cb) {
|
|
125
|
+
if (typeof values === 'function') {
|
|
126
|
+
cb = values;
|
|
127
|
+
values = [];
|
|
128
|
+
}
|
|
129
|
+
this.getConnection((err, conn) => {
|
|
130
|
+
if (err) {
|
|
131
|
+
return cb(err);
|
|
132
|
+
}
|
|
133
|
+
try {
|
|
134
|
+
conn.execute(sql, values, cb).once('end', () => {
|
|
135
|
+
conn.release();
|
|
136
|
+
});
|
|
137
|
+
} catch (e) {
|
|
138
|
+
conn.release();
|
|
139
|
+
throw e;
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
_getClusterNode() {
|
|
145
|
+
const foundNodeIds = this._cluster._findNodeIds(this._pattern);
|
|
146
|
+
if (foundNodeIds.length === 0) {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
const nodeId =
|
|
150
|
+
foundNodeIds.length === 1
|
|
151
|
+
? foundNodeIds[0]
|
|
152
|
+
: this._selector(foundNodeIds);
|
|
153
|
+
return this._cluster._getNode(nodeId);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
class PoolCluster extends EventEmitter {
|
|
158
|
+
constructor(config) {
|
|
159
|
+
super();
|
|
160
|
+
config = config || {};
|
|
161
|
+
this._canRetry =
|
|
162
|
+
typeof config.canRetry === 'undefined' ? true : config.canRetry;
|
|
163
|
+
this._removeNodeErrorCount = config.removeNodeErrorCount || 5;
|
|
164
|
+
this._restoreNodeTimeout = config.restoreNodeTimeout || 0;
|
|
165
|
+
this._defaultSelector = config.defaultSelector || 'RR';
|
|
166
|
+
this._closed = false;
|
|
167
|
+
this._lastId = 0;
|
|
168
|
+
this._nodes = {};
|
|
169
|
+
this._serviceableNodeIds = [];
|
|
170
|
+
this._namespaces = {};
|
|
171
|
+
this._findCaches = {};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
of(pattern, selector) {
|
|
175
|
+
pattern = pattern || '*';
|
|
176
|
+
selector = selector || this._defaultSelector;
|
|
177
|
+
selector = selector.toUpperCase();
|
|
178
|
+
if (!makeSelector[selector] === 'undefined') {
|
|
179
|
+
selector = this._defaultSelector;
|
|
180
|
+
}
|
|
181
|
+
const key = pattern + selector;
|
|
182
|
+
if (typeof this._namespaces[key] === 'undefined') {
|
|
183
|
+
this._namespaces[key] = new PoolNamespace(this, pattern, selector);
|
|
184
|
+
}
|
|
185
|
+
return this._namespaces[key];
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
add(id, config) {
|
|
189
|
+
if (typeof id === 'object') {
|
|
190
|
+
config = id;
|
|
191
|
+
id = `CLUSTER::${++this._lastId}`;
|
|
192
|
+
}
|
|
193
|
+
if (typeof this._nodes[id] === 'undefined') {
|
|
194
|
+
this._nodes[id] = {
|
|
195
|
+
id: id,
|
|
196
|
+
errorCount: 0,
|
|
197
|
+
pool: new Pool({ config: new PoolConfig(config) }),
|
|
198
|
+
_offlineUntil: 0,
|
|
199
|
+
};
|
|
200
|
+
this._serviceableNodeIds.push(id);
|
|
201
|
+
this._clearFindCaches();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
remove(pattern) {
|
|
206
|
+
const foundNodeIds = this._findNodeIds(pattern, true);
|
|
207
|
+
|
|
208
|
+
for (let i = 0; i < foundNodeIds.length; i++) {
|
|
209
|
+
const node = this._getNode(foundNodeIds[i]);
|
|
210
|
+
|
|
211
|
+
if (node) {
|
|
212
|
+
this._removeNode(node);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
getConnection(pattern, selector, cb) {
|
|
218
|
+
let namespace;
|
|
219
|
+
if (typeof pattern === 'function') {
|
|
220
|
+
cb = pattern;
|
|
221
|
+
namespace = this.of();
|
|
222
|
+
} else {
|
|
223
|
+
if (typeof selector === 'function') {
|
|
224
|
+
cb = selector;
|
|
225
|
+
selector = this._defaultSelector;
|
|
226
|
+
}
|
|
227
|
+
namespace = this.of(pattern, selector);
|
|
228
|
+
}
|
|
229
|
+
namespace.getConnection(cb);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
end(callback) {
|
|
233
|
+
const cb =
|
|
234
|
+
callback !== undefined
|
|
235
|
+
? callback
|
|
236
|
+
: (err) => {
|
|
237
|
+
if (err) {
|
|
238
|
+
throw err;
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
if (this._closed) {
|
|
242
|
+
process.nextTick(cb);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
this._closed = true;
|
|
247
|
+
|
|
248
|
+
let calledBack = false;
|
|
249
|
+
let waitingClose = 0;
|
|
250
|
+
const onEnd = (err) => {
|
|
251
|
+
if (!calledBack && (err || --waitingClose <= 0)) {
|
|
252
|
+
calledBack = true;
|
|
253
|
+
return cb(err);
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
for (const id in this._nodes) {
|
|
258
|
+
waitingClose++;
|
|
259
|
+
this._nodes[id].pool.end(onEnd);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (waitingClose === 0) {
|
|
263
|
+
process.nextTick(onEnd);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
_findNodeIds(pattern, includeOffline) {
|
|
268
|
+
let currentTime = 0;
|
|
269
|
+
let foundNodeIds = this._findCaches[pattern];
|
|
270
|
+
|
|
271
|
+
if (foundNodeIds === undefined) {
|
|
272
|
+
const expression = patternRegExp(pattern);
|
|
273
|
+
|
|
274
|
+
foundNodeIds = this._serviceableNodeIds.filter((id) =>
|
|
275
|
+
id.match(expression)
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
this._findCaches[pattern] = foundNodeIds;
|
|
280
|
+
|
|
281
|
+
if (includeOffline) {
|
|
282
|
+
return foundNodeIds;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
return foundNodeIds.filter((nodeId) => {
|
|
286
|
+
const node = this._getNode(nodeId);
|
|
287
|
+
|
|
288
|
+
if (!node._offlineUntil) {
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (!currentTime) {
|
|
293
|
+
currentTime = getMonotonicMilliseconds();
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return node._offlineUntil <= currentTime;
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
_getNode(id) {
|
|
301
|
+
return this._nodes[id] || null;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
_increaseErrorCount(node) {
|
|
305
|
+
const errorCount = ++node.errorCount;
|
|
306
|
+
|
|
307
|
+
if (this._removeNodeErrorCount > errorCount) {
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (this._restoreNodeTimeout > 0) {
|
|
312
|
+
node._offlineUntil =
|
|
313
|
+
getMonotonicMilliseconds() + this._restoreNodeTimeout;
|
|
314
|
+
this.emit('offline', node.id);
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
this._removeNode(node);
|
|
319
|
+
this.emit('remove', node.id);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
_decreaseErrorCount(node) {
|
|
323
|
+
let errorCount = node.errorCount;
|
|
324
|
+
|
|
325
|
+
if (errorCount > this._removeNodeErrorCount) {
|
|
326
|
+
errorCount = this._removeNodeErrorCount;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (errorCount < 1) {
|
|
330
|
+
errorCount = 1;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
node.errorCount = errorCount - 1;
|
|
334
|
+
|
|
335
|
+
if (node._offlineUntil) {
|
|
336
|
+
node._offlineUntil = 0;
|
|
337
|
+
this.emit('online', node.id);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
_getConnection(node, cb) {
|
|
342
|
+
node.pool.getConnection((err, connection) => {
|
|
343
|
+
if (err) {
|
|
344
|
+
this._increaseErrorCount(node);
|
|
345
|
+
return cb(err);
|
|
346
|
+
}
|
|
347
|
+
this._decreaseErrorCount(node);
|
|
348
|
+
|
|
349
|
+
connection._clusterId = node.id;
|
|
350
|
+
return cb(null, connection);
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
_removeNode(node) {
|
|
355
|
+
const index = this._serviceableNodeIds.indexOf(node.id);
|
|
356
|
+
if (index !== -1) {
|
|
357
|
+
this._serviceableNodeIds.splice(index, 1);
|
|
358
|
+
delete this._nodes[node.id];
|
|
359
|
+
this._clearFindCaches();
|
|
360
|
+
node.pool.end();
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
_clearFindCaches() {
|
|
365
|
+
this._findCaches = {};
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
module.exports = PoolCluster;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const ConnectionConfig = require('./connection_config.js');
|
|
4
|
+
|
|
5
|
+
class PoolConfig {
|
|
6
|
+
constructor(options) {
|
|
7
|
+
if (typeof options === 'string') {
|
|
8
|
+
options = ConnectionConfig.parseUrl(options);
|
|
9
|
+
}
|
|
10
|
+
this.connectionConfig = new ConnectionConfig(options);
|
|
11
|
+
this.waitForConnections =
|
|
12
|
+
options.waitForConnections === undefined
|
|
13
|
+
? true
|
|
14
|
+
: Boolean(options.waitForConnections);
|
|
15
|
+
this.connectionLimit = isNaN(options.connectionLimit)
|
|
16
|
+
? 10
|
|
17
|
+
: Number(options.connectionLimit);
|
|
18
|
+
this.maxIdle = isNaN(options.maxIdle)
|
|
19
|
+
? this.connectionLimit
|
|
20
|
+
: Number(options.maxIdle);
|
|
21
|
+
this.idleTimeout = isNaN(options.idleTimeout)
|
|
22
|
+
? 60000
|
|
23
|
+
: Number(options.idleTimeout);
|
|
24
|
+
this.queueLimit = isNaN(options.queueLimit)
|
|
25
|
+
? 0
|
|
26
|
+
: Number(options.queueLimit);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
module.exports = PoolConfig;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const BasePoolConnection = require('./base/pool_connection.js');
|
|
4
|
+
|
|
5
|
+
class PoolConnection extends BasePoolConnection {
|
|
6
|
+
promise(promiseImpl) {
|
|
7
|
+
const PromisePoolConnection = require('./promise/pool_connection.js');
|
|
8
|
+
return new PromisePoolConnection(this, promiseImpl);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
module.exports = PoolConnection;
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const EventEmitter = require('events').EventEmitter;
|
|
4
|
+
const PromisePreparedStatementInfo = require('./prepared_statement_info.js');
|
|
5
|
+
const makeDoneCb = require('./make_done_cb.js');
|
|
6
|
+
const inheritEvents = require('./inherit_events.js');
|
|
7
|
+
const BaseConnection = require('../base/connection.js');
|
|
8
|
+
|
|
9
|
+
class PromiseConnection extends EventEmitter {
|
|
10
|
+
constructor(connection, promiseImpl) {
|
|
11
|
+
super();
|
|
12
|
+
this.connection = connection;
|
|
13
|
+
this.Promise = promiseImpl || Promise;
|
|
14
|
+
inheritEvents(connection, this, [
|
|
15
|
+
'error',
|
|
16
|
+
'drain',
|
|
17
|
+
'connect',
|
|
18
|
+
'end',
|
|
19
|
+
'enqueue',
|
|
20
|
+
]);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
release() {
|
|
24
|
+
this.connection.release();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
query(query, params) {
|
|
28
|
+
const c = this.connection;
|
|
29
|
+
const localErr = new Error();
|
|
30
|
+
if (typeof params === 'function') {
|
|
31
|
+
throw new Error(
|
|
32
|
+
'Callback function is not available with promise clients.'
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
return new this.Promise((resolve, reject) => {
|
|
36
|
+
const done = makeDoneCb(resolve, reject, localErr);
|
|
37
|
+
if (params !== undefined) {
|
|
38
|
+
c.query(query, params, done);
|
|
39
|
+
} else {
|
|
40
|
+
c.query(query, done);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
execute(query, params) {
|
|
46
|
+
const c = this.connection;
|
|
47
|
+
const localErr = new Error();
|
|
48
|
+
if (typeof params === 'function') {
|
|
49
|
+
throw new Error(
|
|
50
|
+
'Callback function is not available with promise clients.'
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
return new this.Promise((resolve, reject) => {
|
|
54
|
+
const done = makeDoneCb(resolve, reject, localErr);
|
|
55
|
+
if (params !== undefined) {
|
|
56
|
+
c.execute(query, params, done);
|
|
57
|
+
} else {
|
|
58
|
+
c.execute(query, done);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
end() {
|
|
64
|
+
return new this.Promise((resolve) => {
|
|
65
|
+
this.connection.end(resolve);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
beginTransaction() {
|
|
70
|
+
const c = this.connection;
|
|
71
|
+
const localErr = new Error();
|
|
72
|
+
return new this.Promise((resolve, reject) => {
|
|
73
|
+
const done = makeDoneCb(resolve, reject, localErr);
|
|
74
|
+
c.beginTransaction(done);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
commit() {
|
|
79
|
+
const c = this.connection;
|
|
80
|
+
const localErr = new Error();
|
|
81
|
+
return new this.Promise((resolve, reject) => {
|
|
82
|
+
const done = makeDoneCb(resolve, reject, localErr);
|
|
83
|
+
c.commit(done);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
rollback() {
|
|
88
|
+
const c = this.connection;
|
|
89
|
+
const localErr = new Error();
|
|
90
|
+
return new this.Promise((resolve, reject) => {
|
|
91
|
+
const done = makeDoneCb(resolve, reject, localErr);
|
|
92
|
+
c.rollback(done);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
ping() {
|
|
97
|
+
const c = this.connection;
|
|
98
|
+
const localErr = new Error();
|
|
99
|
+
return new this.Promise((resolve, reject) => {
|
|
100
|
+
c.ping((err) => {
|
|
101
|
+
if (err) {
|
|
102
|
+
localErr.message = err.message;
|
|
103
|
+
localErr.code = err.code;
|
|
104
|
+
localErr.errno = err.errno;
|
|
105
|
+
localErr.sqlState = err.sqlState;
|
|
106
|
+
localErr.sqlMessage = err.sqlMessage;
|
|
107
|
+
reject(localErr);
|
|
108
|
+
} else {
|
|
109
|
+
resolve(true);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
connect() {
|
|
116
|
+
const c = this.connection;
|
|
117
|
+
const localErr = new Error();
|
|
118
|
+
return new this.Promise((resolve, reject) => {
|
|
119
|
+
c.connect((err, param) => {
|
|
120
|
+
if (err) {
|
|
121
|
+
localErr.message = err.message;
|
|
122
|
+
localErr.code = err.code;
|
|
123
|
+
localErr.errno = err.errno;
|
|
124
|
+
localErr.sqlState = err.sqlState;
|
|
125
|
+
localErr.sqlMessage = err.sqlMessage;
|
|
126
|
+
reject(localErr);
|
|
127
|
+
} else {
|
|
128
|
+
resolve(param);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
prepare(options) {
|
|
135
|
+
const c = this.connection;
|
|
136
|
+
const promiseImpl = this.Promise;
|
|
137
|
+
const localErr = new Error();
|
|
138
|
+
return new this.Promise((resolve, reject) => {
|
|
139
|
+
c.prepare(options, (err, statement) => {
|
|
140
|
+
if (err) {
|
|
141
|
+
localErr.message = err.message;
|
|
142
|
+
localErr.code = err.code;
|
|
143
|
+
localErr.errno = err.errno;
|
|
144
|
+
localErr.sqlState = err.sqlState;
|
|
145
|
+
localErr.sqlMessage = err.sqlMessage;
|
|
146
|
+
reject(localErr);
|
|
147
|
+
} else {
|
|
148
|
+
const wrappedStatement = new PromisePreparedStatementInfo(
|
|
149
|
+
statement,
|
|
150
|
+
promiseImpl
|
|
151
|
+
);
|
|
152
|
+
resolve(wrappedStatement);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
changeUser(options) {
|
|
159
|
+
const c = this.connection;
|
|
160
|
+
const localErr = new Error();
|
|
161
|
+
return new this.Promise((resolve, reject) => {
|
|
162
|
+
c.changeUser(options, (err) => {
|
|
163
|
+
if (err) {
|
|
164
|
+
localErr.message = err.message;
|
|
165
|
+
localErr.code = err.code;
|
|
166
|
+
localErr.errno = err.errno;
|
|
167
|
+
localErr.sqlState = err.sqlState;
|
|
168
|
+
localErr.sqlMessage = err.sqlMessage;
|
|
169
|
+
reject(localErr);
|
|
170
|
+
} else {
|
|
171
|
+
resolve();
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
get config() {
|
|
178
|
+
return this.connection.config;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
get threadId() {
|
|
182
|
+
return this.connection.threadId;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
// patching PromiseConnection
|
|
186
|
+
// create facade functions for prototype functions on "Connection" that are not yet
|
|
187
|
+
// implemented with PromiseConnection
|
|
188
|
+
|
|
189
|
+
// proxy synchronous functions only
|
|
190
|
+
(function (functionsToWrap) {
|
|
191
|
+
for (let i = 0; functionsToWrap && i < functionsToWrap.length; i++) {
|
|
192
|
+
const func = functionsToWrap[i];
|
|
193
|
+
|
|
194
|
+
if (
|
|
195
|
+
typeof BaseConnection.prototype[func] === 'function' &&
|
|
196
|
+
PromiseConnection.prototype[func] === undefined
|
|
197
|
+
) {
|
|
198
|
+
PromiseConnection.prototype[func] = (function factory(funcName) {
|
|
199
|
+
return function () {
|
|
200
|
+
return BaseConnection.prototype[funcName].apply(
|
|
201
|
+
this.connection,
|
|
202
|
+
arguments
|
|
203
|
+
);
|
|
204
|
+
};
|
|
205
|
+
})(func);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
})([
|
|
209
|
+
// synchronous functions
|
|
210
|
+
'close',
|
|
211
|
+
'createBinlogStream',
|
|
212
|
+
'destroy',
|
|
213
|
+
'escape',
|
|
214
|
+
'escapeId',
|
|
215
|
+
'format',
|
|
216
|
+
'pause',
|
|
217
|
+
'pipe',
|
|
218
|
+
'resume',
|
|
219
|
+
'unprepare',
|
|
220
|
+
]);
|
|
221
|
+
|
|
222
|
+
module.exports = PromiseConnection;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function inheritEvents(source, target, events) {
|
|
4
|
+
const listeners = {};
|
|
5
|
+
target
|
|
6
|
+
.on('newListener', (eventName) => {
|
|
7
|
+
if (events.indexOf(eventName) >= 0 && !target.listenerCount(eventName)) {
|
|
8
|
+
source.on(
|
|
9
|
+
eventName,
|
|
10
|
+
(listeners[eventName] = function () {
|
|
11
|
+
const args = [].slice.call(arguments);
|
|
12
|
+
args.unshift(eventName);
|
|
13
|
+
|
|
14
|
+
target.emit.apply(target, args);
|
|
15
|
+
})
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
})
|
|
19
|
+
.on('removeListener', (eventName) => {
|
|
20
|
+
if (events.indexOf(eventName) >= 0 && !target.listenerCount(eventName)) {
|
|
21
|
+
source.removeListener(eventName, listeners[eventName]);
|
|
22
|
+
delete listeners[eventName];
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
module.exports = inheritEvents;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function makeDoneCb(resolve, reject, localErr) {
|
|
4
|
+
return function (err, rows, fields) {
|
|
5
|
+
if (err) {
|
|
6
|
+
localErr.message = err.message;
|
|
7
|
+
localErr.code = err.code;
|
|
8
|
+
localErr.errno = err.errno;
|
|
9
|
+
localErr.sql = err.sql;
|
|
10
|
+
localErr.sqlState = err.sqlState;
|
|
11
|
+
localErr.sqlMessage = err.sqlMessage;
|
|
12
|
+
reject(localErr);
|
|
13
|
+
} else {
|
|
14
|
+
resolve([rows, fields]);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = makeDoneCb;
|