crabatool 1.0.367 → 1.0.369
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/index.js +7 -4
- package/lib/cuid.js +130 -0
- package/lib/db/baseLib.js +480 -0
- package/lib/db/dbHelper.js +423 -0
- package/lib/db/mySqlLib.js +186 -0
- package/lib/handler.js +1 -2
- package/lib/pager.js +151 -0
- package/lib/server.js +7 -2
- package/lib/stringUtils.js +276 -0
- package/lib/utils.js +81 -4
- package/package.json +9 -3
package/index.js
CHANGED
|
@@ -20,6 +20,9 @@ exports.run = function(options) {
|
|
|
20
20
|
}
|
|
21
21
|
exports.utils = require('./lib/utils.js');
|
|
22
22
|
exports.server = require('./lib/server.js');
|
|
23
|
+
exports.stringBuilder = require('./lib/stringBuilder.js');
|
|
24
|
+
exports.stringUtils = require('./lib/stringUtils.js');
|
|
25
|
+
exports.pager = require('./lib/pager.js');
|
|
23
26
|
|
|
24
27
|
function checkFast(args) {
|
|
25
28
|
// 1. 服务端构建环境,自动化,根据命令行参数,直接调用脚手架功能
|
|
@@ -41,16 +44,16 @@ function checkFast(args) {
|
|
|
41
44
|
config.progress = 0; // 通过命令行启动,默认不显示进度条
|
|
42
45
|
|
|
43
46
|
if (args.includes('-checkjs')) {
|
|
44
|
-
start.bindConfigByArgv('-ignoreFiles','array');
|
|
45
|
-
start.bindConfigByArgv('-cssFiles','array');
|
|
47
|
+
start.bindConfigByArgv('-ignoreFiles', 'array');
|
|
48
|
+
start.bindConfigByArgv('-cssFiles', 'array');
|
|
46
49
|
|
|
47
50
|
start.checkAllJs();
|
|
48
51
|
return false;
|
|
49
52
|
}
|
|
50
53
|
|
|
51
54
|
if (args.includes('-checkiconfont')) {
|
|
52
|
-
start.bindConfigByArgv('-ignoreFiles','array');
|
|
53
|
-
start.bindConfigByArgv('-cssFiles','array');
|
|
55
|
+
start.bindConfigByArgv('-ignoreFiles', 'array');
|
|
56
|
+
start.bindConfigByArgv('-cssFiles', 'array');
|
|
54
57
|
|
|
55
58
|
start.checkIconfont();
|
|
56
59
|
return false;
|
package/lib/cuid.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* apracjs
|
|
3
|
+
* Copyright(c) 2017 by wssf
|
|
4
|
+
* 2017-04-26
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict'
|
|
8
|
+
var os = require('os');
|
|
9
|
+
var utils = require('./utils.js');
|
|
10
|
+
var hiGetter = null;
|
|
11
|
+
var inited = false;
|
|
12
|
+
var lastLo = null;
|
|
13
|
+
var loInc = 1;
|
|
14
|
+
var _hi = 0;
|
|
15
|
+
var _hi2 = null;
|
|
16
|
+
var TimestampBase = (new Date()).getTime();
|
|
17
|
+
var MinDateTime = (new Date("2017-06-26 00:00:00")).getTime();
|
|
18
|
+
var TickBase = TimestampBase - MinDateTime;
|
|
19
|
+
|
|
20
|
+
/* 返回数字的cuid */
|
|
21
|
+
exports.newCuid = function cuid$newCuid() {
|
|
22
|
+
var lo;
|
|
23
|
+
try {
|
|
24
|
+
if (!inited) {
|
|
25
|
+
init();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
lo = getLo();
|
|
29
|
+
if (lo == lastLo || lo < lastLo + loInc) {
|
|
30
|
+
lo = lastLo + loInc;
|
|
31
|
+
loInc++;
|
|
32
|
+
} else {
|
|
33
|
+
lastLo = lo;
|
|
34
|
+
loInc = 1;
|
|
35
|
+
}
|
|
36
|
+
} catch (ex) {
|
|
37
|
+
|
|
38
|
+
} finally {
|
|
39
|
+
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
var hi = _hi << 48; // 左移6个字节
|
|
43
|
+
return hi + lo;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/* 返回字符串的cuid */
|
|
47
|
+
exports.newCuidString = function cuid$newCuidString() {
|
|
48
|
+
var lo;
|
|
49
|
+
try {
|
|
50
|
+
if (!inited) {
|
|
51
|
+
init();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
lo = getLo();
|
|
55
|
+
if (lo == lastLo || lo < lastLo + loInc) {
|
|
56
|
+
lo = lastLo + loInc;
|
|
57
|
+
loInc++;
|
|
58
|
+
} else {
|
|
59
|
+
lastLo = lo;
|
|
60
|
+
loInc = 1;
|
|
61
|
+
}
|
|
62
|
+
} catch (ex) {
|
|
63
|
+
|
|
64
|
+
} finally {
|
|
65
|
+
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return _hi2 + utils.padLeft(lo.toString(), 12, "0");
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/* 注册cuid的自定义hi接口 */
|
|
72
|
+
exports.registerHiGetter = async function(getter) {
|
|
73
|
+
if (hiGetter || !getter || inited) return; // 只初始化一次
|
|
74
|
+
|
|
75
|
+
hiGetter = getter;
|
|
76
|
+
|
|
77
|
+
console.log('开始初始化cuid的hi');
|
|
78
|
+
if (typeof getter.getHi != 'function') {
|
|
79
|
+
console.log('注册的接口没有实现getHi方法');
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
var siteId = getSiteId();
|
|
84
|
+
var hi = await getter.getHi(siteId);
|
|
85
|
+
if (hi <= 0) {
|
|
86
|
+
console.log('cuid自定义接口生成的hi失败,使用内部生成hi');
|
|
87
|
+
return; // 标记为失败,使用内部自动生成hi,使用newCuidString的情况重复概率也不大;存储空间为2的12次方
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
console.log('cuid自定义接口生成的hi为:' + hi);
|
|
91
|
+
makeHi(hi);
|
|
92
|
+
inited = true;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function init() {
|
|
96
|
+
inited = true;
|
|
97
|
+
|
|
98
|
+
lastLo = getLo();
|
|
99
|
+
|
|
100
|
+
makeHashHi();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function getLo() {
|
|
104
|
+
var ticks = (new Date()).getTime() - TimestampBase;
|
|
105
|
+
return TickBase + ticks;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function makeHashHi() {
|
|
109
|
+
var siteId = getSiteId();
|
|
110
|
+
var hashId = parseInt(utils.getHashCode(siteId));
|
|
111
|
+
makeHi(hashId);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function makeHi(hc) {
|
|
115
|
+
_hi = hc & 32; // JavaScript最大整数精度2*53位,时间戳48位,hi剩余5位
|
|
116
|
+
_hi2 = utils.padLeft(hc.toString(), 12, "0");
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function getSiteId() {
|
|
120
|
+
var siteId = utils.format("{0}_{1}", os.hostname(), process.cwd()); // 机器名称+网站路径
|
|
121
|
+
return siteId;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function LoToDateTime(lo) {
|
|
125
|
+
return MinDateTime + lo;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function LoToLocalDateTime(lo) {
|
|
129
|
+
return new Date(LoToDateTime(lo));
|
|
130
|
+
}
|
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
var StringBuilder = require('../stringBuilder.js');
|
|
2
|
+
var Url = require('url');
|
|
3
|
+
var DebugDBHelper = require('debug')('aprac:DBHelper');
|
|
4
|
+
var SqlString = require('sqlstring');
|
|
5
|
+
var StringUtils = require("../stringUtils.js");
|
|
6
|
+
var Cuid = require('../cuid.js');
|
|
7
|
+
|
|
8
|
+
class BaseLib {
|
|
9
|
+
constructor(connectionSetting) {
|
|
10
|
+
this.connectionSetting = connectionSetting;
|
|
11
|
+
this.connectionStatusEnum = new Object();
|
|
12
|
+
this.connectionStatusEnum.Open = 1;
|
|
13
|
+
this.connectionStatusEnum.Close = 0;
|
|
14
|
+
this._initConnectionSetting();
|
|
15
|
+
//BaseLib._startKillLockConnectionService();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
_initConnectionSetting() {
|
|
19
|
+
let uriInfo = Url.parse(this.connectionSetting, true);
|
|
20
|
+
if (uriInfo.query.useConnectionPooling) {
|
|
21
|
+
this.isPool = true;
|
|
22
|
+
this.useConnectionPooling = uriInfo.query.useConnectionPooling;
|
|
23
|
+
} else {
|
|
24
|
+
this.isPool = false;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
this.hostname = uriInfo.hostname;
|
|
28
|
+
this.port = uriInfo.port;
|
|
29
|
+
this.user = uriInfo.auth.split(':')[0];
|
|
30
|
+
this.password = uriInfo.auth.split(':')[1];
|
|
31
|
+
this.database = uriInfo.pathname.indexOf('/') == 0 ? uriInfo.pathname.substr(1) : uriInfo.pathname;
|
|
32
|
+
this.connectionlimit = uriInfo.query && uriInfo.query.connectionLimit ? parseInt(uriInfo.query.connectionLimit) : 10;
|
|
33
|
+
if (isNaN(this.connectionlimit)) {
|
|
34
|
+
this.connectionlimit = 10;
|
|
35
|
+
}
|
|
36
|
+
this.connection = null;
|
|
37
|
+
this.sqlInterceptor = null;
|
|
38
|
+
this.connectionStatus = this.connectionStatusEnum.Close; //0:未连接 1:已连接
|
|
39
|
+
this.hasTransaction = false;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
get connectionlimit() {
|
|
43
|
+
return this._connectionlimit;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
set connectionlimit(value) {
|
|
47
|
+
this._connectionlimit = value;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
get hostname() {
|
|
51
|
+
return this._hostname;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
set hostname(value) {
|
|
55
|
+
this._hostname = value;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
get port() {
|
|
59
|
+
return this._port;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
set port(value) {
|
|
63
|
+
this._port = value;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
get user() {
|
|
67
|
+
return this._user;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
set user(value) {
|
|
71
|
+
this._user = value;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
get password() {
|
|
75
|
+
return this._password;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
set password(value) {
|
|
79
|
+
this._password = value;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
get database() {
|
|
83
|
+
return this._database;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
set database(value) {
|
|
87
|
+
this._database = value;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
get isPool() {
|
|
91
|
+
return this._isPool;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
set isPool(value) {
|
|
95
|
+
this._isPool = value;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
get connectionSetting() {
|
|
99
|
+
return this._connectionSetting;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
set connectionSetting(value) {
|
|
103
|
+
this._connectionSetting = value;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
get sqlInterceptor() {
|
|
107
|
+
return this._sqlInterceptor;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
set sqlInterceptor(value) {
|
|
111
|
+
this._sqlInterceptor = value;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
get connectionStatus() {
|
|
115
|
+
return this._connectionStatus;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
set connectionStatus(value) {
|
|
119
|
+
this._connectionStatus = value;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
get hasTransaction() {
|
|
123
|
+
return this._hasTransaction;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
set hasTransaction(value) {
|
|
127
|
+
this._hasTransaction = value;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
get errorStack() {
|
|
131
|
+
return this._errorStack;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
set errorStack(value) {
|
|
135
|
+
this._errorStack = value;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
get connection() {
|
|
139
|
+
return this._connection;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
set connection(value) {
|
|
143
|
+
this._connection = value;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
get useConnectionPooling() {
|
|
147
|
+
return this._useConnectionPooling;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
set useConnectionPooling(value) {
|
|
151
|
+
this._useConnectionPooling = value;
|
|
152
|
+
BaseLib.setPoolName(this.poolName);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
get poolName() {
|
|
156
|
+
return "_dataBasePool_" + (this.useConnectionPooling ? this.useConnectionPooling : "NoPool");
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
static setPoolName(poolName) {
|
|
160
|
+
if (BaseLib.poolNameList.indexOf(poolName) === -1) {
|
|
161
|
+
BaseLib.poolNameList.push(poolName);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
static get poolNameList() {
|
|
166
|
+
if (BaseLib._poolNameList == null) {
|
|
167
|
+
BaseLib._poolNameList = new Array();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return BaseLib._poolNameList;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
getConnectionFunction() { }
|
|
174
|
+
|
|
175
|
+
createPool() { }
|
|
176
|
+
|
|
177
|
+
createPoolConnection() {
|
|
178
|
+
var _this = this;
|
|
179
|
+
this.errorStack = new Error();
|
|
180
|
+
return new Promise(function(resolver, reject) {
|
|
181
|
+
_this.createPool();
|
|
182
|
+
_this.getConnectionFunction()(function(err, connection) {
|
|
183
|
+
_this.connection = connection;
|
|
184
|
+
_this._addError();
|
|
185
|
+
_this._connectionConnect(resolver, reject, err);
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
createSingleConnection() {
|
|
191
|
+
var _this = this;
|
|
192
|
+
this.errorStack = new Error();
|
|
193
|
+
this.connection = this.getConnectionFunction()(this.connectionSetting);
|
|
194
|
+
return new Promise(function(resolver, reject) {
|
|
195
|
+
_this._addError();
|
|
196
|
+
_this.connection.connect(function(err) {
|
|
197
|
+
_this._connectionConnect(resolver, reject, err);
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
getTableStruct(tablename) { }
|
|
203
|
+
|
|
204
|
+
beginTransaction() { }
|
|
205
|
+
|
|
206
|
+
commitTransaction() { }
|
|
207
|
+
|
|
208
|
+
rollbackTransaction() { }
|
|
209
|
+
|
|
210
|
+
close(callback, poolConnectionIsDestroy) {
|
|
211
|
+
if (this.connection == null) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
this.connectionStatus = this.connectionStatusEnum.Close;
|
|
216
|
+
BaseLib.removeConnectionRecord(this.poolName);
|
|
217
|
+
BaseLib.removeConnectionObject(this.connection);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
create() {
|
|
221
|
+
if (this.connection != null) {
|
|
222
|
+
return new Promise(function(resolver, reject) {
|
|
223
|
+
global.setTimeout(resolver);
|
|
224
|
+
});
|
|
225
|
+
} else {
|
|
226
|
+
return this.isPool ? this.createPoolConnection() : this.createSingleConnection();
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
query(sql, params) { }
|
|
231
|
+
|
|
232
|
+
_connectionEnd(err) {
|
|
233
|
+
if (err) {
|
|
234
|
+
DebugDBHelper("连接结束出错:" + err);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
_handleDataBaseConnectionError(qerr, resolver, reject, sql, params) {
|
|
239
|
+
if (qerr) {
|
|
240
|
+
//附加日志,方便排查可能存在的泄漏问题
|
|
241
|
+
let fullErrorMsg = " __rawSQL:【" + JSON.stringify(sql) + "】 __stack:【" + (this._errorStack ? this._errorStack.stack : "") + "】";
|
|
242
|
+
|
|
243
|
+
if (typeof qerr === "string")
|
|
244
|
+
qerr += fullErrorMsg;
|
|
245
|
+
else if (typeof qerr === "object") {
|
|
246
|
+
qerr.__fullErrorMsg = fullErrorMsg;
|
|
247
|
+
|
|
248
|
+
if (qerr.message && typeof qerr.message === "string")
|
|
249
|
+
qerr.message += fullErrorMsg;
|
|
250
|
+
|
|
251
|
+
if (qerr.stack && typeof qerr.stack === "string")
|
|
252
|
+
qerr.stack += fullErrorMsg;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
//统一处理错误
|
|
256
|
+
let errmsg = null;
|
|
257
|
+
this.close(null, true);
|
|
258
|
+
switch (qerr.code) {
|
|
259
|
+
case "PROTOCOL_CONNECTION_LOST":
|
|
260
|
+
case "PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR":
|
|
261
|
+
case "PROTOCOL_ENQUEUE_AFTER_QUIT":
|
|
262
|
+
let _this = this;
|
|
263
|
+
global.setTimeout(function() {
|
|
264
|
+
_this.query(sql, params).then(function(tempValue) {
|
|
265
|
+
resolver(tempValue);
|
|
266
|
+
},
|
|
267
|
+
function(tempErr) {
|
|
268
|
+
reject(tempErr);
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
break;
|
|
272
|
+
case "ECONNREFUSED":
|
|
273
|
+
errmsg = "数据库连接失败,请联系管理员";
|
|
274
|
+
global.setTimeout(function() {
|
|
275
|
+
reject(errmsg);
|
|
276
|
+
});
|
|
277
|
+
break;
|
|
278
|
+
default:
|
|
279
|
+
global.setTimeout(function() {
|
|
280
|
+
reject(qerr);
|
|
281
|
+
});
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
limit(start, count) { }
|
|
288
|
+
|
|
289
|
+
toNumber(value) { }
|
|
290
|
+
|
|
291
|
+
IF(condition, truedata, falsedata) { }
|
|
292
|
+
|
|
293
|
+
IFNULL(expression, altValue) { }
|
|
294
|
+
|
|
295
|
+
SUBSTRING_INDEX(field, split, index) { }
|
|
296
|
+
|
|
297
|
+
Like(value) {
|
|
298
|
+
if (StringUtils.isNullOrEmpty(value)) return "";
|
|
299
|
+
var str = this.sqlEscape(value, null, null, false);
|
|
300
|
+
var buffer = new StringBuilder();
|
|
301
|
+
var arr = str.split('');
|
|
302
|
+
for (var i = 0, count = arr.length; i < count; i++) {
|
|
303
|
+
var ch = arr[i];
|
|
304
|
+
if (ch == '%' || ch == '\\' || ch == '_') {
|
|
305
|
+
buffer.append('\\').append(ch);
|
|
306
|
+
} else {
|
|
307
|
+
buffer.append(ch);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return buffer.toString();
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
sqlEscape(value, stringifyObjects, timeZone, isAddMake) {
|
|
314
|
+
isAddMake = isAddMake == null ? true : isAddMake;
|
|
315
|
+
let result = SqlString.escape(value, stringifyObjects, timeZone);
|
|
316
|
+
if (isAddMake)
|
|
317
|
+
return result;
|
|
318
|
+
else {
|
|
319
|
+
if (result.startsWith('\'') && result.endsWith('\'')) {
|
|
320
|
+
return result.substr(1, result.length - 2);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
return result;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
_addError() {
|
|
328
|
+
if (this.connection && !this.connection.__addErrorListener) {
|
|
329
|
+
let fun = this.isPool ? this._onPoolConnectError.bind(this) : this._onSingleConnectError.bind(this);
|
|
330
|
+
this.connection.on('error', fun);
|
|
331
|
+
this.connection.__addErrorListener = true;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
_onPoolConnectError(error) {
|
|
336
|
+
DebugDBHelper("数据库连接池连接错误:" + error);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
_onSingleConnectError(error) {
|
|
340
|
+
DebugDBHelper("单个数据库连接错误:" + error);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
_connectionConnect(resolver, reject, err) {
|
|
344
|
+
if (this.connection) {
|
|
345
|
+
this.connection._stack = this.errorStack.stack;
|
|
346
|
+
this.connection._startDate = new Date();
|
|
347
|
+
this.connection._cuid = Cuid.newCuidString();
|
|
348
|
+
this.connection._isPool = this.isPool;
|
|
349
|
+
|
|
350
|
+
BaseLib.addConnectionRecord(this.poolName);
|
|
351
|
+
BaseLib.addConnectionObject(this.connection);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (err) {
|
|
355
|
+
this.close(null, true);
|
|
356
|
+
} else {
|
|
357
|
+
this.connectionStatus = this.connectionStatusEnum.Open;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
if (err == null)
|
|
361
|
+
resolver();
|
|
362
|
+
else
|
|
363
|
+
reject(err);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
_queryFormat(query, values) {
|
|
367
|
+
if (!values) {
|
|
368
|
+
if (this.sqlInterceptor) this.sqlInterceptor(this, { sql: query, values: null });
|
|
369
|
+
return query;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
let _this = this;
|
|
373
|
+
let encodeSql = query.replace(/\:(\w+)/g, function(txt, key) {
|
|
374
|
+
if (values.hasOwnProperty(key)) return _this.sqlEscape(values[key], false);
|
|
375
|
+
else return txt;
|
|
376
|
+
}.bind(this));
|
|
377
|
+
|
|
378
|
+
if (this.sqlInterceptor) this.sqlInterceptor(this, { sql: encodeSql, values: values });
|
|
379
|
+
return encodeSql;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
static addConnectionRecord(poolName) {
|
|
383
|
+
let value = BaseLib.getConnectionRecord(poolName);
|
|
384
|
+
value += 1;
|
|
385
|
+
BaseLib.getPoolConnectionRecordMap().set(poolName, value);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
static removeConnectionRecord(poolName) {
|
|
389
|
+
let value = BaseLib.getConnectionRecord(poolName);
|
|
390
|
+
value -= 1;
|
|
391
|
+
BaseLib.getPoolConnectionRecordMap().set(poolName, value);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
static getPoolInfoList() {
|
|
395
|
+
let poolNameList = BaseLib.poolNameList;
|
|
396
|
+
let poolInfoList = new Array();
|
|
397
|
+
for (let i = 0; i < poolNameList.length; i++) {
|
|
398
|
+
let poolName = poolNameList[i];
|
|
399
|
+
let nowConnectionCount = BaseLib.getConnectionRecord(poolName);
|
|
400
|
+
let poolInfo = {
|
|
401
|
+
name: poolName,
|
|
402
|
+
nowConnectionCount: nowConnectionCount
|
|
403
|
+
};
|
|
404
|
+
poolInfoList.push(poolInfo);
|
|
405
|
+
}
|
|
406
|
+
return poolInfoList;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
static getPoolConnectionRecordMap() {
|
|
410
|
+
if (BaseLib._poolConnectionRecordMap == null)
|
|
411
|
+
BaseLib._poolConnectionRecordMap = new Map();
|
|
412
|
+
return BaseLib._poolConnectionRecordMap;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
static getConnectionRecord(poolName) {
|
|
416
|
+
let value = BaseLib.getPoolConnectionRecordMap().get(poolName);
|
|
417
|
+
if (value == null)
|
|
418
|
+
value = 0;
|
|
419
|
+
return value;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
static addConnectionObject(connection) {
|
|
423
|
+
if (!connection) {
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
let map = BaseLib.getConnectionObjectMap();
|
|
428
|
+
if (map.has(connection._cuid)) {
|
|
429
|
+
console.log("连接ID重复:" + connection._cuid + " 堆栈:" + connection._stack);
|
|
430
|
+
} else {
|
|
431
|
+
map.set(connection._cuid, connection);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
static removeConnectionObject(connection) {
|
|
436
|
+
if (!connection) {
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
let map = BaseLib.getConnectionObjectMap();
|
|
441
|
+
if (map.has(connection._cuid)) {
|
|
442
|
+
map.delete(connection._cuid);
|
|
443
|
+
} else {
|
|
444
|
+
console.log("连接ID不存在:" + connection._cuid + " 堆栈:" + connection._stack);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
BaseLib._printfTimeoutConnectionObject();
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
static getConnectionObjectMap() {
|
|
451
|
+
if (BaseLib._connectionObjectMap == null)
|
|
452
|
+
BaseLib._connectionObjectMap = new Map();
|
|
453
|
+
return BaseLib._connectionObjectMap;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
static _printfTimeoutConnectionObject() {
|
|
457
|
+
let map = BaseLib.getConnectionObjectMap();
|
|
458
|
+
let nowDateTicks = new Date().getTime();
|
|
459
|
+
let dbConnectOccupyTimeout = 600000;
|
|
460
|
+
map.forEach(function(value, key, map) {
|
|
461
|
+
if (value._printfTimeoutConnectionObject == null && value._isPool && value._startDate.getTime() + dbConnectOccupyTimeout < nowDateTicks) {
|
|
462
|
+
console.log("连接池连接ID超时:" + value._cuid + " 堆栈:" + value._stack);
|
|
463
|
+
value._printfTimeoutConnectionObject = true;
|
|
464
|
+
}
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
static _startKillLockConnectionService() {
|
|
469
|
+
if (BaseLib._startKillLockConnection) return;
|
|
470
|
+
|
|
471
|
+
BaseLib._startKillLockConnection = true;
|
|
472
|
+
BaseLib._killLockConnectionService();
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
static _killLockConnectionService() {
|
|
476
|
+
//连接池死锁检测需求保留
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
module.exports = BaseLib;
|