@maiyunnet/kebab 2.0.0
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/.VSCodeCounter/2025-02-14_14-46-44/details.md +82 -0
- package/.VSCodeCounter/2025-02-14_14-46-44/diff-details.md +15 -0
- package/.VSCodeCounter/2025-02-14_14-46-44/diff.csv +2 -0
- package/.VSCodeCounter/2025-02-14_14-46-44/diff.md +19 -0
- package/.VSCodeCounter/2025-02-14_14-46-44/diff.txt +22 -0
- package/.VSCodeCounter/2025-02-14_14-46-44/results.csv +69 -0
- package/.VSCodeCounter/2025-02-14_14-46-44/results.json +1 -0
- package/.VSCodeCounter/2025-02-14_14-46-44/results.md +48 -0
- package/.VSCodeCounter/2025-02-14_14-46-44/results.txt +118 -0
- package/.vscode/tasks.json +15 -0
- package/LICENSE +201 -0
- package/README.md +201 -0
- package/bin/kebab.js +2 -0
- package/eslint.config.js +22 -0
- package/index.js +19 -0
- package/index.ts +33 -0
- package/lib/buffer.js +108 -0
- package/lib/buffer.ts +152 -0
- package/lib/captcha/zcool-addict-italic.ttf +0 -0
- package/lib/captcha.js +71 -0
- package/lib/captcha.ts +63 -0
- package/lib/consistent.js +171 -0
- package/lib/consistent.ts +219 -0
- package/lib/core.js +663 -0
- package/lib/core.ts +880 -0
- package/lib/crypto.js +256 -0
- package/lib/crypto.ts +384 -0
- package/lib/db.js +521 -0
- package/lib/db.ts +719 -0
- package/lib/dns.js +321 -0
- package/lib/dns.ts +405 -0
- package/lib/fs.js +405 -0
- package/lib/fs.ts +527 -0
- package/lib/jwt.js +223 -0
- package/lib/jwt.ts +276 -0
- package/lib/kv.js +1004 -0
- package/lib/kv.ts +1489 -0
- package/lib/lan.js +99 -0
- package/lib/lan.ts +87 -0
- package/lib/net/cacert.pem +3480 -0
- package/lib/net/formdata.js +137 -0
- package/lib/net/formdata.ts +166 -0
- package/lib/net/request.js +102 -0
- package/lib/net/request.ts +150 -0
- package/lib/net/response.js +28 -0
- package/lib/net/response.ts +59 -0
- package/lib/net.js +462 -0
- package/lib/net.ts +662 -0
- package/lib/s3.js +180 -0
- package/lib/s3.ts +235 -0
- package/lib/scan.js +276 -0
- package/lib/scan.ts +364 -0
- package/lib/session.js +177 -0
- package/lib/session.ts +230 -0
- package/lib/sql.js +818 -0
- package/lib/sql.ts +1151 -0
- package/lib/ssh/sftp.js +373 -0
- package/lib/ssh/sftp.ts +508 -0
- package/lib/ssh/shell.js +109 -0
- package/lib/ssh/shell.ts +123 -0
- package/lib/ssh.js +171 -0
- package/lib/ssh.ts +191 -0
- package/lib/text/tld.json +1 -0
- package/lib/text.js +452 -0
- package/lib/text.ts +607 -0
- package/lib/time.js +216 -0
- package/lib/time.ts +254 -0
- package/lib/ws.js +373 -0
- package/lib/ws.ts +523 -0
- package/lib/zip.js +381 -0
- package/lib/zip.ts +447 -0
- package/lib/zlib.js +289 -0
- package/lib/zlib.ts +350 -0
- package/main.js +51 -0
- package/main.ts +27 -0
- package/package.json +37 -0
- package/sys/child.js +585 -0
- package/sys/child.ts +678 -0
- package/sys/cmd.js +226 -0
- package/sys/cmd.ts +225 -0
- package/sys/ctr.js +608 -0
- package/sys/ctr.ts +904 -0
- package/sys/master.js +314 -0
- package/sys/master.ts +355 -0
- package/sys/mod.js +1273 -0
- package/sys/mod.ts +1871 -0
- package/sys/route.js +922 -0
- package/sys/route.ts +1113 -0
- package/types/index.d.ts +283 -0
- package/www/example/ctr/main.js +42 -0
- package/www/example/ctr/main.ts +9 -0
- package/www/example/ctr/middle.js +57 -0
- package/www/example/ctr/middle.ts +26 -0
- package/www/example/ctr/test.js +2818 -0
- package/www/example/ctr/test.ts +3218 -0
- package/www/example/data/locale/en.test.json +8 -0
- package/www/example/data/locale/index.html +1 -0
- package/www/example/data/locale/ja.test.json +8 -0
- package/www/example/data/locale/sc.test.json +8 -0
- package/www/example/data/locale/tc.test.json +8 -0
- package/www/example/data/test.zip +0 -0
- package/www/example/kebab.json +24 -0
- package/www/example/mod/test.js +49 -0
- package/www/example/mod/test.ts +47 -0
- package/www/example/mod/testdata.js +11 -0
- package/www/example/mod/testdata.ts +30 -0
- package/www/example/route.json +6 -0
- package/www/example/view/test.ejs +11 -0
- package/www/example/ws/mproxy.js +49 -0
- package/www/example/ws/mproxy.ts +16 -0
- package/www/example/ws/rproxy.js +47 -0
- package/www/example/ws/rproxy.ts +14 -0
- package/www/example/ws/test.js +68 -0
- package/www/example/ws/test.ts +36 -0
package/lib/db.js
ADDED
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.Connection = exports.Transaction = exports.Pool = void 0;
|
|
37
|
+
exports.get = get;
|
|
38
|
+
exports.getConnectionList = getConnectionList;
|
|
39
|
+
const mysql2 = __importStar(require("mysql2/promise"));
|
|
40
|
+
const time = __importStar(require("~/lib/time"));
|
|
41
|
+
const lSql = __importStar(require("~/lib/sql"));
|
|
42
|
+
const core = __importStar(require("~/lib/core"));
|
|
43
|
+
const text = __importStar(require("~/lib/text"));
|
|
44
|
+
const ctr = __importStar(require("~/sys/ctr"));
|
|
45
|
+
const connections = [];
|
|
46
|
+
async function checkConnection() {
|
|
47
|
+
const now = time.stamp();
|
|
48
|
+
for (let i = 0; i < connections.length; ++i) {
|
|
49
|
+
const connection = connections[i];
|
|
50
|
+
if (connection.isLost()) {
|
|
51
|
+
await connection.end();
|
|
52
|
+
connections.splice(i, 1);
|
|
53
|
+
--i;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (connection.isUsing()) {
|
|
57
|
+
if (connection.getLast() <= now - 60) {
|
|
58
|
+
const ls = connection.getLastSql();
|
|
59
|
+
const sql = ls[1] ?? ls[0];
|
|
60
|
+
console.log(`[child] [db] [error] There is a transactional connection[${i}] that is not closed, last sql: ${sql?.sql ?? 'undefined'}.`);
|
|
61
|
+
const newarr = ls.map(item => {
|
|
62
|
+
if (!item.values) {
|
|
63
|
+
return item.sql;
|
|
64
|
+
}
|
|
65
|
+
return lSql.format(item.sql, item.values);
|
|
66
|
+
});
|
|
67
|
+
await core.log({
|
|
68
|
+
'path': '',
|
|
69
|
+
'urlFull': '',
|
|
70
|
+
'hostname': '',
|
|
71
|
+
'req': null,
|
|
72
|
+
'get': {},
|
|
73
|
+
'cookie': {},
|
|
74
|
+
'headers': {}
|
|
75
|
+
}, `(db.checkConnection)There is a transactional connection[${i}] that is not closed, last sql: ${newarr.join(', ')}.`, '-error');
|
|
76
|
+
await connection.rollback();
|
|
77
|
+
}
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (connection.getLast() > now - 180) {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
await connection.end();
|
|
84
|
+
connections.splice(i, 1);
|
|
85
|
+
--i;
|
|
86
|
+
}
|
|
87
|
+
setTimeout(function () {
|
|
88
|
+
checkConnection().catch(e => { console.log('[DB]', e); });
|
|
89
|
+
}, 30000);
|
|
90
|
+
}
|
|
91
|
+
setTimeout(function () {
|
|
92
|
+
checkConnection().catch(e => { console.log('[DB]', e); });
|
|
93
|
+
}, 30000);
|
|
94
|
+
class Pool {
|
|
95
|
+
constructor(etc) {
|
|
96
|
+
this._queries = 0;
|
|
97
|
+
this._etc = etc;
|
|
98
|
+
}
|
|
99
|
+
async query(sql, values) {
|
|
100
|
+
++this._queries;
|
|
101
|
+
const conn = await this._getConnection();
|
|
102
|
+
if (!conn) {
|
|
103
|
+
return {
|
|
104
|
+
'rows': null,
|
|
105
|
+
'fields': [],
|
|
106
|
+
'error': {
|
|
107
|
+
'message': 'null',
|
|
108
|
+
'errno': 0
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
return conn.query(sql, values);
|
|
113
|
+
}
|
|
114
|
+
async execute(sql, values) {
|
|
115
|
+
++this._queries;
|
|
116
|
+
const conn = await this._getConnection();
|
|
117
|
+
if (!conn) {
|
|
118
|
+
return {
|
|
119
|
+
'packet': null,
|
|
120
|
+
'fields': [],
|
|
121
|
+
'error': {
|
|
122
|
+
'message': 'null',
|
|
123
|
+
'errno': 0
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
return conn.execute(sql, values);
|
|
128
|
+
}
|
|
129
|
+
async beginTransaction(ctr) {
|
|
130
|
+
const conn = await this._getConnection();
|
|
131
|
+
if (!conn) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
if (!await conn.beginTransaction()) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
return new Transaction(ctr, conn);
|
|
138
|
+
}
|
|
139
|
+
async _getConnection() {
|
|
140
|
+
let conn = null;
|
|
141
|
+
for (const connection of connections) {
|
|
142
|
+
const etc = connection.getEtc();
|
|
143
|
+
if ((etc.host !== this._etc.host) ||
|
|
144
|
+
(etc.port !== this._etc.port) ||
|
|
145
|
+
(etc.name !== this._etc.name) ||
|
|
146
|
+
(etc.user !== this._etc.user)) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (!connection.using()) {
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
conn = connection;
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
if (!conn) {
|
|
156
|
+
try {
|
|
157
|
+
const link = await mysql2.createConnection({
|
|
158
|
+
'host': this._etc.host,
|
|
159
|
+
'port': this._etc.port,
|
|
160
|
+
'charset': this._etc.charset,
|
|
161
|
+
'database': this._etc.name,
|
|
162
|
+
'user': this._etc.user,
|
|
163
|
+
'password': this._etc.pwd,
|
|
164
|
+
'connectTimeout': 3000
|
|
165
|
+
});
|
|
166
|
+
const c = new Connection(this._etc, link);
|
|
167
|
+
c.using();
|
|
168
|
+
link.on('error', function (err) {
|
|
169
|
+
if (err.code !== 'PROTOCOL_CONNECTION_LOST') {
|
|
170
|
+
console.log(err);
|
|
171
|
+
}
|
|
172
|
+
c.setLost();
|
|
173
|
+
}).on('end', function () {
|
|
174
|
+
c.setLost();
|
|
175
|
+
});
|
|
176
|
+
conn = c;
|
|
177
|
+
connections.push(conn);
|
|
178
|
+
}
|
|
179
|
+
catch (e) {
|
|
180
|
+
await core.log({
|
|
181
|
+
'path': '',
|
|
182
|
+
'urlFull': '',
|
|
183
|
+
'hostname': '',
|
|
184
|
+
'req': null,
|
|
185
|
+
'get': {},
|
|
186
|
+
'cookie': {},
|
|
187
|
+
'headers': {}
|
|
188
|
+
}, '(db._getConnection)' + text.stringifyJson(e.stack).slice(1, -1), '-error');
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return conn;
|
|
192
|
+
}
|
|
193
|
+
getQueries() {
|
|
194
|
+
return this._queries;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
exports.Pool = Pool;
|
|
198
|
+
class Transaction {
|
|
199
|
+
constructor(ctr, conn, opts = {}) {
|
|
200
|
+
this._queries = 0;
|
|
201
|
+
this._timer = {
|
|
202
|
+
'warning': undefined,
|
|
203
|
+
'danger': undefined
|
|
204
|
+
};
|
|
205
|
+
this._ctr = ctr;
|
|
206
|
+
if (ctr) {
|
|
207
|
+
++ctr.getPrototype('_waitInfo').transaction;
|
|
208
|
+
}
|
|
209
|
+
this._conn = conn;
|
|
210
|
+
const warning = opts.warning ?? 1_500;
|
|
211
|
+
this._timer.warning = setTimeout(() => {
|
|
212
|
+
this._timer.warning = undefined;
|
|
213
|
+
console.log('[WARNING][DB][Transaction] time too long, ms:', warning, this._ctr?.getPrototype('_config').const.path ?? 'no ctr');
|
|
214
|
+
}, warning);
|
|
215
|
+
const danger = opts.danger ?? 5_000;
|
|
216
|
+
this._timer.danger = setTimeout(() => {
|
|
217
|
+
this._timer.danger = undefined;
|
|
218
|
+
console.log('[DANGER][DB][Transaction] time too long, ms:', danger, this._ctr?.getPrototype('_config').const.path ?? 'no ctr');
|
|
219
|
+
}, danger);
|
|
220
|
+
}
|
|
221
|
+
async query(sql, values) {
|
|
222
|
+
if (!this._conn) {
|
|
223
|
+
console.log('[ERROR][DB][Transaction.query] has been closed.', this._ctr?.getPrototype('_config').const.path ?? 'no ctr', sql);
|
|
224
|
+
await core.log({
|
|
225
|
+
'path': '',
|
|
226
|
+
'urlFull': '',
|
|
227
|
+
'hostname': '',
|
|
228
|
+
'req': null,
|
|
229
|
+
'get': {},
|
|
230
|
+
'cookie': {},
|
|
231
|
+
'headers': {}
|
|
232
|
+
}, '(db.Transaction.query) has been closed, ' + (this._ctr?.getPrototype('_config').const.path ?? 'no ctr') + ': ' + sql, '-error');
|
|
233
|
+
return {
|
|
234
|
+
'rows': null,
|
|
235
|
+
'fields': [],
|
|
236
|
+
'error': {
|
|
237
|
+
'message': 'null',
|
|
238
|
+
'errno': 0
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
++this._queries;
|
|
243
|
+
return this._conn.query(sql, values);
|
|
244
|
+
}
|
|
245
|
+
async execute(sql, values) {
|
|
246
|
+
if (!this._conn) {
|
|
247
|
+
console.log('[ERROR][DB][Transaction.execute] has been closed.', this._ctr?.getPrototype('_config').const.path ?? 'no ctr', sql);
|
|
248
|
+
await core.log({
|
|
249
|
+
'path': '',
|
|
250
|
+
'urlFull': '',
|
|
251
|
+
'hostname': '',
|
|
252
|
+
'req': null,
|
|
253
|
+
'get': {},
|
|
254
|
+
'cookie': {},
|
|
255
|
+
'headers': {}
|
|
256
|
+
}, '(db.Transaction.execute) has been closed, ' + (this._ctr?.getPrototype('_config').const.path ?? 'no ctr') + ': ' + sql, '-error');
|
|
257
|
+
return {
|
|
258
|
+
'packet': null,
|
|
259
|
+
'fields': [],
|
|
260
|
+
'error': {
|
|
261
|
+
'message': 'null',
|
|
262
|
+
'errno': 0
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
++this._queries;
|
|
267
|
+
return this._conn.execute(sql, values);
|
|
268
|
+
}
|
|
269
|
+
async commit() {
|
|
270
|
+
if (!this._conn) {
|
|
271
|
+
console.log('[ERROR][DB][Transaction.commit] has been closed.', this._ctr?.getPrototype('_config').const.path ?? 'no ctr');
|
|
272
|
+
await core.log({
|
|
273
|
+
'path': '',
|
|
274
|
+
'urlFull': '',
|
|
275
|
+
'hostname': '',
|
|
276
|
+
'req': null,
|
|
277
|
+
'get': {},
|
|
278
|
+
'cookie': {},
|
|
279
|
+
'headers': {}
|
|
280
|
+
}, '(db.Transaction.commit) has been closed: ' + (this._ctr?.getPrototype('_config').const.path ?? 'no ctr'), '-error');
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
const r = await this._conn.commit();
|
|
284
|
+
if (!r) {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
this._conn = null;
|
|
288
|
+
if (this._ctr) {
|
|
289
|
+
--this._ctr.getPrototype('_waitInfo').transaction;
|
|
290
|
+
}
|
|
291
|
+
clearTimeout(this._timer.warning);
|
|
292
|
+
this._timer.warning = undefined;
|
|
293
|
+
clearTimeout(this._timer.danger);
|
|
294
|
+
this._timer.danger = undefined;
|
|
295
|
+
return true;
|
|
296
|
+
}
|
|
297
|
+
async rollback() {
|
|
298
|
+
if (!this._conn) {
|
|
299
|
+
console.log('[ERROR][DB][Transaction.rollback] has been closed.', this._ctr?.getPrototype('_config').const.path ?? 'no ctr');
|
|
300
|
+
await core.log({
|
|
301
|
+
'path': '',
|
|
302
|
+
'urlFull': '',
|
|
303
|
+
'hostname': '',
|
|
304
|
+
'req': null,
|
|
305
|
+
'get': {},
|
|
306
|
+
'cookie': {},
|
|
307
|
+
'headers': {}
|
|
308
|
+
}, '(db.Transaction.rollback) has been closed: ' + (this._ctr?.getPrototype('_config').const.path ?? 'no ctr'), '-error');
|
|
309
|
+
return false;
|
|
310
|
+
}
|
|
311
|
+
const r = await this._conn.rollback();
|
|
312
|
+
if (!r) {
|
|
313
|
+
return false;
|
|
314
|
+
}
|
|
315
|
+
this._conn = null;
|
|
316
|
+
if (this._ctr) {
|
|
317
|
+
--this._ctr.getPrototype('_waitInfo').transaction;
|
|
318
|
+
}
|
|
319
|
+
clearTimeout(this._timer.warning);
|
|
320
|
+
this._timer.warning = undefined;
|
|
321
|
+
clearTimeout(this._timer.danger);
|
|
322
|
+
this._timer.danger = undefined;
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
exports.Transaction = Transaction;
|
|
327
|
+
class Connection {
|
|
328
|
+
constructor(etc, link) {
|
|
329
|
+
this._last = 0;
|
|
330
|
+
this._lastSql = [];
|
|
331
|
+
this._using = false;
|
|
332
|
+
this._lost = false;
|
|
333
|
+
this._transaction = false;
|
|
334
|
+
this._etc = etc;
|
|
335
|
+
this._link = link;
|
|
336
|
+
this.refreshLast();
|
|
337
|
+
}
|
|
338
|
+
getEtc() {
|
|
339
|
+
return this._etc;
|
|
340
|
+
}
|
|
341
|
+
getLast() {
|
|
342
|
+
return this._last;
|
|
343
|
+
}
|
|
344
|
+
getLastSql() {
|
|
345
|
+
return this._lastSql;
|
|
346
|
+
}
|
|
347
|
+
setLost() {
|
|
348
|
+
this._lost = true;
|
|
349
|
+
}
|
|
350
|
+
isLost() {
|
|
351
|
+
return this._lost;
|
|
352
|
+
}
|
|
353
|
+
isTransaction() {
|
|
354
|
+
return this._transaction;
|
|
355
|
+
}
|
|
356
|
+
isUsing() {
|
|
357
|
+
return this._using;
|
|
358
|
+
}
|
|
359
|
+
using() {
|
|
360
|
+
if (this._lost || this._using) {
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
this.refreshLast();
|
|
365
|
+
this._using = true;
|
|
366
|
+
return true;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
used() {
|
|
370
|
+
this._using = false;
|
|
371
|
+
}
|
|
372
|
+
refreshLast() {
|
|
373
|
+
this._last = time.stamp();
|
|
374
|
+
}
|
|
375
|
+
async isAvailable() {
|
|
376
|
+
this.refreshLast();
|
|
377
|
+
try {
|
|
378
|
+
await this._link.query('SELECT 1');
|
|
379
|
+
return true;
|
|
380
|
+
}
|
|
381
|
+
catch {
|
|
382
|
+
return false;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
async query(sql, values) {
|
|
386
|
+
let res;
|
|
387
|
+
try {
|
|
388
|
+
this.refreshLast();
|
|
389
|
+
if (this._lastSql.length === 2) {
|
|
390
|
+
this._lastSql.splice(0, 1);
|
|
391
|
+
}
|
|
392
|
+
this._lastSql.push({
|
|
393
|
+
'sql': sql,
|
|
394
|
+
'values': values
|
|
395
|
+
});
|
|
396
|
+
res = await this._link.query(sql, values);
|
|
397
|
+
}
|
|
398
|
+
catch (e) {
|
|
399
|
+
if (!this._transaction) {
|
|
400
|
+
this._using = false;
|
|
401
|
+
}
|
|
402
|
+
return {
|
|
403
|
+
'rows': null,
|
|
404
|
+
'fields': [],
|
|
405
|
+
'error': e
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
if (!this._transaction) {
|
|
409
|
+
this._using = false;
|
|
410
|
+
}
|
|
411
|
+
return {
|
|
412
|
+
'rows': res[0],
|
|
413
|
+
'fields': res[1],
|
|
414
|
+
'error': null
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
async execute(sql, values) {
|
|
418
|
+
let res;
|
|
419
|
+
try {
|
|
420
|
+
this.refreshLast();
|
|
421
|
+
if (this._lastSql.length === 2) {
|
|
422
|
+
this._lastSql.splice(0, 1);
|
|
423
|
+
}
|
|
424
|
+
this._lastSql.push({
|
|
425
|
+
'sql': sql,
|
|
426
|
+
'values': values
|
|
427
|
+
});
|
|
428
|
+
res = await this._link.execute(sql, values);
|
|
429
|
+
}
|
|
430
|
+
catch (e) {
|
|
431
|
+
if (!this._transaction) {
|
|
432
|
+
this._using = false;
|
|
433
|
+
}
|
|
434
|
+
return {
|
|
435
|
+
'packet': null,
|
|
436
|
+
'fields': [],
|
|
437
|
+
'error': e
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
if (!this._transaction) {
|
|
441
|
+
this._using = false;
|
|
442
|
+
}
|
|
443
|
+
return {
|
|
444
|
+
'packet': res[0],
|
|
445
|
+
'fields': res[1],
|
|
446
|
+
'error': null
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
async end() {
|
|
450
|
+
try {
|
|
451
|
+
await this._link.end();
|
|
452
|
+
return true;
|
|
453
|
+
}
|
|
454
|
+
catch {
|
|
455
|
+
return false;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
async beginTransaction() {
|
|
459
|
+
if (this._using) {
|
|
460
|
+
try {
|
|
461
|
+
this._transaction = true;
|
|
462
|
+
await this._link.beginTransaction();
|
|
463
|
+
return true;
|
|
464
|
+
}
|
|
465
|
+
catch {
|
|
466
|
+
return false;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
else {
|
|
470
|
+
return false;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
async commit() {
|
|
474
|
+
try {
|
|
475
|
+
await this._link.commit();
|
|
476
|
+
this.refreshLast();
|
|
477
|
+
this._transaction = false;
|
|
478
|
+
this._using = false;
|
|
479
|
+
return true;
|
|
480
|
+
}
|
|
481
|
+
catch {
|
|
482
|
+
return false;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
async rollback() {
|
|
486
|
+
try {
|
|
487
|
+
await this._link.rollback();
|
|
488
|
+
this.refreshLast();
|
|
489
|
+
this._transaction = false;
|
|
490
|
+
this._using = false;
|
|
491
|
+
return true;
|
|
492
|
+
}
|
|
493
|
+
catch {
|
|
494
|
+
return false;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
exports.Connection = Connection;
|
|
499
|
+
function get(ctrEtc) {
|
|
500
|
+
const etc = ctrEtc instanceof ctr.Ctr ? ctrEtc.getPrototype('_config').db : ctrEtc;
|
|
501
|
+
return new Pool(etc);
|
|
502
|
+
}
|
|
503
|
+
function getConnectionList() {
|
|
504
|
+
const list = [];
|
|
505
|
+
for (let i = 0; i < connections.length; ++i) {
|
|
506
|
+
const connection = connections[i];
|
|
507
|
+
const etc = connection.getEtc();
|
|
508
|
+
list.push({
|
|
509
|
+
'id': i,
|
|
510
|
+
'last': connection.getLast(),
|
|
511
|
+
'host': etc.host,
|
|
512
|
+
'port': etc.port,
|
|
513
|
+
'name': etc.name,
|
|
514
|
+
'user': etc.user,
|
|
515
|
+
'lost': connection.isLost(),
|
|
516
|
+
'using': connection.isUsing(),
|
|
517
|
+
'transaction': connection.isTransaction()
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
return list;
|
|
521
|
+
}
|