@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.
Files changed (114) hide show
  1. package/.VSCodeCounter/2025-02-14_14-46-44/details.md +82 -0
  2. package/.VSCodeCounter/2025-02-14_14-46-44/diff-details.md +15 -0
  3. package/.VSCodeCounter/2025-02-14_14-46-44/diff.csv +2 -0
  4. package/.VSCodeCounter/2025-02-14_14-46-44/diff.md +19 -0
  5. package/.VSCodeCounter/2025-02-14_14-46-44/diff.txt +22 -0
  6. package/.VSCodeCounter/2025-02-14_14-46-44/results.csv +69 -0
  7. package/.VSCodeCounter/2025-02-14_14-46-44/results.json +1 -0
  8. package/.VSCodeCounter/2025-02-14_14-46-44/results.md +48 -0
  9. package/.VSCodeCounter/2025-02-14_14-46-44/results.txt +118 -0
  10. package/.vscode/tasks.json +15 -0
  11. package/LICENSE +201 -0
  12. package/README.md +201 -0
  13. package/bin/kebab.js +2 -0
  14. package/eslint.config.js +22 -0
  15. package/index.js +19 -0
  16. package/index.ts +33 -0
  17. package/lib/buffer.js +108 -0
  18. package/lib/buffer.ts +152 -0
  19. package/lib/captcha/zcool-addict-italic.ttf +0 -0
  20. package/lib/captcha.js +71 -0
  21. package/lib/captcha.ts +63 -0
  22. package/lib/consistent.js +171 -0
  23. package/lib/consistent.ts +219 -0
  24. package/lib/core.js +663 -0
  25. package/lib/core.ts +880 -0
  26. package/lib/crypto.js +256 -0
  27. package/lib/crypto.ts +384 -0
  28. package/lib/db.js +521 -0
  29. package/lib/db.ts +719 -0
  30. package/lib/dns.js +321 -0
  31. package/lib/dns.ts +405 -0
  32. package/lib/fs.js +405 -0
  33. package/lib/fs.ts +527 -0
  34. package/lib/jwt.js +223 -0
  35. package/lib/jwt.ts +276 -0
  36. package/lib/kv.js +1004 -0
  37. package/lib/kv.ts +1489 -0
  38. package/lib/lan.js +99 -0
  39. package/lib/lan.ts +87 -0
  40. package/lib/net/cacert.pem +3480 -0
  41. package/lib/net/formdata.js +137 -0
  42. package/lib/net/formdata.ts +166 -0
  43. package/lib/net/request.js +102 -0
  44. package/lib/net/request.ts +150 -0
  45. package/lib/net/response.js +28 -0
  46. package/lib/net/response.ts +59 -0
  47. package/lib/net.js +462 -0
  48. package/lib/net.ts +662 -0
  49. package/lib/s3.js +180 -0
  50. package/lib/s3.ts +235 -0
  51. package/lib/scan.js +276 -0
  52. package/lib/scan.ts +364 -0
  53. package/lib/session.js +177 -0
  54. package/lib/session.ts +230 -0
  55. package/lib/sql.js +818 -0
  56. package/lib/sql.ts +1151 -0
  57. package/lib/ssh/sftp.js +373 -0
  58. package/lib/ssh/sftp.ts +508 -0
  59. package/lib/ssh/shell.js +109 -0
  60. package/lib/ssh/shell.ts +123 -0
  61. package/lib/ssh.js +171 -0
  62. package/lib/ssh.ts +191 -0
  63. package/lib/text/tld.json +1 -0
  64. package/lib/text.js +452 -0
  65. package/lib/text.ts +607 -0
  66. package/lib/time.js +216 -0
  67. package/lib/time.ts +254 -0
  68. package/lib/ws.js +373 -0
  69. package/lib/ws.ts +523 -0
  70. package/lib/zip.js +381 -0
  71. package/lib/zip.ts +447 -0
  72. package/lib/zlib.js +289 -0
  73. package/lib/zlib.ts +350 -0
  74. package/main.js +51 -0
  75. package/main.ts +27 -0
  76. package/package.json +37 -0
  77. package/sys/child.js +585 -0
  78. package/sys/child.ts +678 -0
  79. package/sys/cmd.js +226 -0
  80. package/sys/cmd.ts +225 -0
  81. package/sys/ctr.js +608 -0
  82. package/sys/ctr.ts +904 -0
  83. package/sys/master.js +314 -0
  84. package/sys/master.ts +355 -0
  85. package/sys/mod.js +1273 -0
  86. package/sys/mod.ts +1871 -0
  87. package/sys/route.js +922 -0
  88. package/sys/route.ts +1113 -0
  89. package/types/index.d.ts +283 -0
  90. package/www/example/ctr/main.js +42 -0
  91. package/www/example/ctr/main.ts +9 -0
  92. package/www/example/ctr/middle.js +57 -0
  93. package/www/example/ctr/middle.ts +26 -0
  94. package/www/example/ctr/test.js +2818 -0
  95. package/www/example/ctr/test.ts +3218 -0
  96. package/www/example/data/locale/en.test.json +8 -0
  97. package/www/example/data/locale/index.html +1 -0
  98. package/www/example/data/locale/ja.test.json +8 -0
  99. package/www/example/data/locale/sc.test.json +8 -0
  100. package/www/example/data/locale/tc.test.json +8 -0
  101. package/www/example/data/test.zip +0 -0
  102. package/www/example/kebab.json +24 -0
  103. package/www/example/mod/test.js +49 -0
  104. package/www/example/mod/test.ts +47 -0
  105. package/www/example/mod/testdata.js +11 -0
  106. package/www/example/mod/testdata.ts +30 -0
  107. package/www/example/route.json +6 -0
  108. package/www/example/view/test.ejs +11 -0
  109. package/www/example/ws/mproxy.js +49 -0
  110. package/www/example/ws/mproxy.ts +16 -0
  111. package/www/example/ws/rproxy.js +47 -0
  112. package/www/example/ws/rproxy.ts +14 -0
  113. package/www/example/ws/test.js +68 -0
  114. package/www/example/ws/test.ts +36 -0
package/lib/sql.js ADDED
@@ -0,0 +1,818 @@
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.Sql = void 0;
37
+ exports.get = get;
38
+ exports.format = format;
39
+ exports.aoMix = aoMix;
40
+ exports.column = column;
41
+ const lText = __importStar(require("~/lib/text"));
42
+ const lCore = __importStar(require("~/lib/core"));
43
+ const mysql2 = __importStar(require("mysql2/promise"));
44
+ const ctr = __importStar(require("~/sys/ctr"));
45
+ let columnToken = '';
46
+ class Sql {
47
+ constructor(pre, opt = {}) {
48
+ this._pre = '';
49
+ this._sql = [];
50
+ this._data = [];
51
+ this._whereDataPosition = [0, 0];
52
+ this._pre = pre ?? '';
53
+ if (opt.data) {
54
+ this._data = opt.data;
55
+ }
56
+ if (opt.sql) {
57
+ this._sql = opt.sql;
58
+ }
59
+ }
60
+ insert(table) {
61
+ this._data = [];
62
+ const sql = 'INSERT INTO ' + this.field(table, this._pre);
63
+ this._sql = [sql];
64
+ return this;
65
+ }
66
+ replace(table) {
67
+ this._data = [];
68
+ const sql = 'REPLACE INTO ' + this.field(table, this._pre);
69
+ this._sql = [sql];
70
+ return this;
71
+ }
72
+ values(cs, vs = []) {
73
+ let sql = ' (';
74
+ if (Array.isArray(cs)) {
75
+ for (const i of cs) {
76
+ sql += this.field(i) + ', ';
77
+ }
78
+ sql = sql.slice(0, -2) + ') VALUES ';
79
+ if (!Array.isArray(vs[0])) {
80
+ vs = [vs];
81
+ }
82
+ for (const v of vs) {
83
+ sql += '(';
84
+ for (const v1 of v) {
85
+ if (v1 === undefined || Number.isNaN(v1)) {
86
+ lCore.log({
87
+ 'path': '',
88
+ 'urlFull': '',
89
+ 'hostname': '',
90
+ 'req': null,
91
+ 'get': {},
92
+ 'cookie': {},
93
+ 'headers': {}
94
+ }, '(sql.values) value error', '-error').catch(() => {
95
+ });
96
+ sql += `'', `;
97
+ }
98
+ else if (v1 === null) {
99
+ sql += 'NULL, ';
100
+ }
101
+ else if (typeof v1 === 'string' || typeof v1 === 'number') {
102
+ sql += '?, ';
103
+ this._data.push(v1);
104
+ }
105
+ else if (Array.isArray(v1)) {
106
+ if (v1[0]?.[0]?.x === undefined && typeof v1[0] === 'string' &&
107
+ v1[0].includes('(') && v1[0].includes(')')) {
108
+ sql += this.field(v1[0]) + ', ';
109
+ if (v1[1]) {
110
+ this._data.push(...v1[1]);
111
+ }
112
+ }
113
+ else if (v1[0]?.[0]?.y !== undefined) {
114
+ sql += 'ST_POLYGONFROMTEXT(?), ';
115
+ this._data.push(`POLYGON(${v1.map((item) => {
116
+ return `(${item.map((it) => {
117
+ return `${it.x} ${it.y}`;
118
+ }).join(', ')})`;
119
+ }).join(', ')})`);
120
+ }
121
+ else {
122
+ sql += '?, ';
123
+ this._data.push(lText.stringifyJson(v1));
124
+ }
125
+ }
126
+ else if (v1.x !== undefined) {
127
+ if (v1.y !== undefined) {
128
+ sql += 'ST_POINTFROMTEXT(?), ';
129
+ this._data.push(`POINT(${v1.x} ${v1.y})`);
130
+ }
131
+ else {
132
+ sql += '?, ';
133
+ this._data.push(lText.stringifyJson(v1));
134
+ }
135
+ }
136
+ else if (v1 instanceof Buffer) {
137
+ sql += '?, ';
138
+ this._data.push(v);
139
+ }
140
+ else if (this._isField(v1)) {
141
+ sql += this.field(v1.value) + ', ';
142
+ }
143
+ else {
144
+ sql += '?, ';
145
+ this._data.push(lText.stringifyJson(v1));
146
+ }
147
+ }
148
+ sql = sql.slice(0, -2) + '), ';
149
+ }
150
+ sql = sql.slice(0, -2);
151
+ }
152
+ else {
153
+ let values = '';
154
+ for (const k in cs) {
155
+ const v = cs[k];
156
+ sql += this.field(k) + ', ';
157
+ if (v === undefined || Number.isNaN(v)) {
158
+ lCore.log({
159
+ 'path': '',
160
+ 'urlFull': '',
161
+ 'hostname': '',
162
+ 'req': null,
163
+ 'get': {},
164
+ 'cookie': {},
165
+ 'headers': {}
166
+ }, '(sql.values) value error', '-error').catch(() => {
167
+ });
168
+ values += `'', `;
169
+ }
170
+ else if (v === null) {
171
+ values += 'NULL, ';
172
+ }
173
+ else if (typeof v === 'string' || typeof v === 'number') {
174
+ values += '?, ';
175
+ this._data.push(v);
176
+ }
177
+ else if (Array.isArray(v)) {
178
+ if (v[0]?.[0]?.x === undefined && typeof v[0] === 'string' &&
179
+ v[0].includes('(') && v[0].includes(')')) {
180
+ values += this.field(v[0]) + ', ';
181
+ if (v[1] !== undefined) {
182
+ this._data.push(...v[1]);
183
+ }
184
+ }
185
+ else if (v[0]?.[0]?.y !== undefined) {
186
+ values += 'ST_POLYGONFROMTEXT(?), ';
187
+ this._data.push(`POLYGON(${v.map((item) => {
188
+ return `(${item.map((it) => {
189
+ return `${it.x} ${it.y}`;
190
+ }).join(', ')})`;
191
+ }).join(', ')})`);
192
+ }
193
+ else {
194
+ values += '?, ';
195
+ this._data.push(lText.stringifyJson(v));
196
+ }
197
+ }
198
+ else if (v.x !== undefined) {
199
+ if (v.y !== undefined) {
200
+ values += 'ST_POINTFROMTEXT(?), ';
201
+ this._data.push(`POINT(${v.x} ${v.y})`);
202
+ }
203
+ else {
204
+ values += '?, ';
205
+ this._data.push(lText.stringifyJson(v));
206
+ }
207
+ }
208
+ else if (v instanceof Buffer) {
209
+ values += '?, ';
210
+ this._data.push(v);
211
+ }
212
+ else if (this._isField(v)) {
213
+ values += this.field(v.value) + ', ';
214
+ }
215
+ else {
216
+ values += '?, ';
217
+ this._data.push(lText.stringifyJson(v));
218
+ }
219
+ }
220
+ sql = sql.slice(0, -2) + ') VALUES (' + values.slice(0, -2) + ')';
221
+ }
222
+ this._sql.push(sql);
223
+ return this;
224
+ }
225
+ notExists(table, insert, where) {
226
+ let sql = '(';
227
+ const values = [];
228
+ for (const field in insert) {
229
+ const val = insert[field];
230
+ sql += this.field(field) + ', ';
231
+ values.push(val);
232
+ }
233
+ sql = sql.slice(0, -2) + ') SELECT ';
234
+ for (const value of values) {
235
+ if (Array.isArray(value)) {
236
+ sql += value[0] + ', ';
237
+ this._data.push(...value[1]);
238
+ }
239
+ else {
240
+ sql += '?, ';
241
+ this._data.push(value);
242
+ }
243
+ }
244
+ sql = sql.slice(0, -2) + ' FROM DUAL WHERE NOT EXISTS (SELECT `id` FROM ' + this.field(table, this._pre) + ' WHERE ' + this._whereSub(where) + ')';
245
+ this._sql.push(sql);
246
+ return this;
247
+ }
248
+ duplicate(s) {
249
+ if (Array.isArray(s) ? s.length : Object.keys(s).length) {
250
+ const sql = ' ON DUPLICATE KEY UPDATE ' + this._updateSub(s);
251
+ this._sql.push(sql);
252
+ }
253
+ return this;
254
+ }
255
+ select(c, f) {
256
+ this._data = [];
257
+ let sql = 'SELECT ';
258
+ if (typeof c === 'string') {
259
+ sql += this.field(c);
260
+ }
261
+ else {
262
+ for (const i of c) {
263
+ if (Array.isArray(i)) {
264
+ sql += this.field(i[0]) + ', ';
265
+ this._data.push(...i[1]);
266
+ }
267
+ else {
268
+ sql += this.field(i) + ', ';
269
+ }
270
+ }
271
+ sql = sql.slice(0, -2);
272
+ }
273
+ sql += ' FROM ';
274
+ if (typeof f === 'string') {
275
+ sql += this.field(f, this._pre);
276
+ }
277
+ else {
278
+ for (const i of f) {
279
+ sql += this.field(i, this._pre) + ', ';
280
+ }
281
+ sql = sql.slice(0, -2);
282
+ }
283
+ this._sql = [sql];
284
+ return this;
285
+ }
286
+ update(f, s) {
287
+ this._data = [];
288
+ const sql = `UPDATE ${this.field(f, this._pre)} SET ${this._updateSub(s)}`;
289
+ this._sql = [sql];
290
+ return this;
291
+ }
292
+ _updateSub(s) {
293
+ s = aoMix(s);
294
+ let sql = '';
295
+ for (const k in s) {
296
+ const v = s[k];
297
+ if (/^[0-9]+$/.test(k)) {
298
+ const nv = v[2];
299
+ const isf = this._isField(nv);
300
+ if (isf) {
301
+ if (v[1] === '=') {
302
+ sql += this.field(v[0]) + ' = ' + this.field(nv.value) + ', ';
303
+ }
304
+ else {
305
+ sql += this.field(v[0]) + ' = ' + this.field(v[0]) + ' ' + v[1] + ' ' + this.field(nv.value) + ', ';
306
+ }
307
+ }
308
+ else {
309
+ if (v[1] === '=') {
310
+ sql += this.field(v[0]) + ' = ?, ';
311
+ }
312
+ else {
313
+ sql += this.field(v[0]) + ' = ' + this.field(v[0]) + ' ' + v[1] + ' ?, ';
314
+ }
315
+ this._data.push(nv);
316
+ }
317
+ }
318
+ else {
319
+ sql += this.field(k) + ' = ';
320
+ if (v === undefined || Number.isNaN(v)) {
321
+ lCore.log({
322
+ 'path': '',
323
+ 'urlFull': '',
324
+ 'hostname': '',
325
+ 'req': null,
326
+ 'get': {},
327
+ 'cookie': {},
328
+ 'headers': {}
329
+ }, '(sql._updateSub) value error, key: ' + k, '-error').catch(() => {
330
+ });
331
+ sql += '"", ';
332
+ }
333
+ else if (v === null) {
334
+ sql += 'NULL, ';
335
+ }
336
+ else if (typeof v === 'string' || typeof v === 'number') {
337
+ sql += '?, ';
338
+ this._data.push(v);
339
+ }
340
+ else if (Array.isArray(v)) {
341
+ if (v[0]?.[0]?.x === undefined && typeof v[0] === 'string' &&
342
+ v[0].includes('(') && v[0].includes(')')) {
343
+ sql += this.field(v[0]) + ', ';
344
+ if (v[1] !== undefined) {
345
+ this._data.push(...v[1]);
346
+ }
347
+ }
348
+ else if (v[0]?.[0]?.y !== undefined) {
349
+ sql += 'ST_POLYGONFROMTEXT(?), ';
350
+ this._data.push(`POLYGON(${v.map((item) => {
351
+ return `(${item.map((it) => {
352
+ return `${it.x} ${it.y}`;
353
+ }).join(', ')})`;
354
+ }).join(', ')})`);
355
+ }
356
+ else {
357
+ sql += '?, ';
358
+ this._data.push(lText.stringifyJson(v));
359
+ }
360
+ }
361
+ else if (v.x !== undefined) {
362
+ if (v.y !== undefined) {
363
+ sql += 'ST_POINTFROMTEXT(?), ';
364
+ this._data.push(`POINT(${v.x} ${v.y})`);
365
+ }
366
+ else {
367
+ if (this._isField(v)) {
368
+ sql += this.field(v.value) + ', ';
369
+ }
370
+ else {
371
+ sql += '?, ';
372
+ this._data.push(lText.stringifyJson(v));
373
+ }
374
+ }
375
+ }
376
+ else if (v instanceof Buffer) {
377
+ sql += '?, ';
378
+ this._data.push(v);
379
+ }
380
+ else if (this._isField(v)) {
381
+ sql += this.field(v.value) + ', ';
382
+ }
383
+ else {
384
+ sql += '?, ';
385
+ this._data.push(lText.stringifyJson(v));
386
+ }
387
+ }
388
+ }
389
+ sql = sql.slice(0, -2);
390
+ return sql;
391
+ }
392
+ delete(f) {
393
+ this._data = [];
394
+ this._sql = ['DELETE FROM ' + this.field(f, this._pre)];
395
+ return this;
396
+ }
397
+ union(lsql, type = '') {
398
+ this._data = this._data.concat(lsql.getData());
399
+ this._sql.push(' UNION ' + (type ? type + ' ' : ''));
400
+ this._sql.push(lsql.getSql());
401
+ return this;
402
+ }
403
+ unionAll(lsql) {
404
+ return this.union(lsql, 'ALL');
405
+ }
406
+ join(f, s = [], type = 'INNER', suf = '', pre = '') {
407
+ let field = this.field(f, pre || this._pre, suf ? ('#' + suf) : '');
408
+ if (pre) {
409
+ field = field.replace(new RegExp(`AS \`${pre}(.+?)\``), `AS \`${this._pre}$1\``);
410
+ }
411
+ let sql = ' ' + type + ' JOIN ' + field;
412
+ if (Array.isArray(s) ? s.length : Object.keys(s).length) {
413
+ sql += ' ON ' + this._whereSub(s);
414
+ }
415
+ this._sql.push(sql);
416
+ return this;
417
+ }
418
+ leftJoin(f, s = [], suf = '', pre = '') {
419
+ return this.join(f, s, 'LEFT', suf, pre);
420
+ }
421
+ rightJoin(f, s = [], suf = '', pre = '') {
422
+ return this.join(f, s, 'RIGHT', suf, pre);
423
+ }
424
+ innerJoin(f, s = [], suf = '', pre = '') {
425
+ return this.join(f, s, 'INNER', suf, pre);
426
+ }
427
+ fullJoin(f, s = [], suf = '', pre = '') {
428
+ return this.join(f, s, 'FULL', suf, pre);
429
+ }
430
+ crossJoin(f, s = [], suf = '', pre = '') {
431
+ return this.join(f, s, 'CROSS', suf, pre);
432
+ }
433
+ having(s = '') {
434
+ if (typeof s === 'string') {
435
+ if (s !== '') {
436
+ this._sql.push(' HAVING ' + s);
437
+ }
438
+ }
439
+ else {
440
+ if (s.length) {
441
+ const whereSub = this._whereSub(s);
442
+ if (whereSub !== '') {
443
+ this._sql.push(' HAVING ' + whereSub);
444
+ }
445
+ }
446
+ }
447
+ return this;
448
+ }
449
+ where(s) {
450
+ this._whereDataPosition[0] = this._data.length;
451
+ if (typeof s === 'string') {
452
+ if (s !== '') {
453
+ this._sql.push(' WHERE ' + s);
454
+ this._whereDataPosition[1] = this._data.length;
455
+ }
456
+ }
457
+ else {
458
+ let go = false;
459
+ if (Array.isArray(s)) {
460
+ if (s.length) {
461
+ go = true;
462
+ }
463
+ }
464
+ else {
465
+ if (Object.keys(s).length) {
466
+ go = true;
467
+ }
468
+ }
469
+ if (go) {
470
+ const whereSub = this._whereSub(s);
471
+ if (whereSub !== '') {
472
+ this._sql.push(' WHERE ' + whereSub);
473
+ }
474
+ }
475
+ this._whereDataPosition[1] = this._data.length;
476
+ }
477
+ return this;
478
+ }
479
+ _whereSub(s, data) {
480
+ if (!data) {
481
+ data = this._data;
482
+ }
483
+ s = aoMix(s);
484
+ let sql = '';
485
+ for (const k in s) {
486
+ const v = s[k];
487
+ if (/^[0-9]+$/.test(k)) {
488
+ if (v[2] === undefined) {
489
+ sql += this.field(v[0]) + ' AND ';
490
+ if (v[1] !== undefined) {
491
+ data.push(...v[1]);
492
+ }
493
+ }
494
+ else if (v[2] === null) {
495
+ let opera = v[1];
496
+ if (opera === '!=' || opera === '!==' || opera === '<>') {
497
+ opera = 'IS NOT';
498
+ }
499
+ else if (opera === '=' || opera === '==' || opera === '===') {
500
+ opera = 'IS';
501
+ }
502
+ else {
503
+ opera = opera.toUpperCase();
504
+ }
505
+ sql += this.field(v[0]) + ' ' + opera + ' NULL AND ';
506
+ }
507
+ else if (Array.isArray(v[2])) {
508
+ sql += this.field(v[0]) + ' ' + v[1].toUpperCase() + ' (';
509
+ for (const v1 of v[2]) {
510
+ sql += '?, ';
511
+ data.push(v1);
512
+ }
513
+ sql = sql.slice(0, -2) + ') AND ';
514
+ }
515
+ else {
516
+ const nv = v[2];
517
+ const isf = this._isField(nv);
518
+ if (isf) {
519
+ sql += this.field(v[0]) + ' ' + v[1] + ' ' + this.field(nv.value) + ' AND ';
520
+ }
521
+ else {
522
+ sql += this.field(v[0]) + ' ' + v[1] + ' ? AND ';
523
+ data.push(nv);
524
+ }
525
+ }
526
+ }
527
+ else {
528
+ if (k.startsWith('$')) {
529
+ const sp = ' ' + k.slice(1).split('-')[0].toUpperCase() + ' ';
530
+ sql += '(';
531
+ for (let v1 of v) {
532
+ v1 = aoMix(v1);
533
+ if (Object.keys(v1).length > 1) {
534
+ sql += '(' + this._whereSub(v1, data) + ')' + sp;
535
+ }
536
+ else {
537
+ sql += this._whereSub(v1, data) + sp;
538
+ }
539
+ }
540
+ sql = sql.slice(0, -sp.length) + ') AND ';
541
+ }
542
+ else {
543
+ if (v === null) {
544
+ sql += this.field(k) + ' IS NULL AND ';
545
+ }
546
+ else if (typeof v === 'string' || typeof v === 'number') {
547
+ sql += this.field(k) + ' = ? AND ';
548
+ data.push(v);
549
+ }
550
+ else if (this._isField(v)) {
551
+ sql += this.field(k) + ' = ' + this.field(v.value) + ' AND ';
552
+ }
553
+ else {
554
+ if (v.length > 0) {
555
+ sql += this.field(k) + ' IN (';
556
+ for (const v1 of v) {
557
+ sql += '?, ';
558
+ data.push(v1);
559
+ }
560
+ sql = sql.slice(0, -2) + ') AND ';
561
+ }
562
+ else {
563
+ sql += this.field(k) + ' IN (NULL) AND ';
564
+ }
565
+ }
566
+ }
567
+ }
568
+ }
569
+ if (sql === '') {
570
+ return '';
571
+ }
572
+ return sql.slice(0, -5);
573
+ }
574
+ by(c, d = 'DESC') {
575
+ let sql = ' ORDER BY ';
576
+ if (typeof c === 'string') {
577
+ sql += this.field(c) + ' ' + d;
578
+ }
579
+ else {
580
+ for (const v of c) {
581
+ if (typeof v === 'string' || typeof v === 'number') {
582
+ sql += this.field(v) + ' ' + d + ', ';
583
+ }
584
+ else {
585
+ sql += this.field(v[0]) + ' ' + v[1] + ', ';
586
+ }
587
+ }
588
+ sql = sql.slice(0, -2);
589
+ }
590
+ this._sql.push(sql);
591
+ return this;
592
+ }
593
+ group(c) {
594
+ let sql = ' GROUP BY ';
595
+ if (typeof c === 'string') {
596
+ sql += this.field(c);
597
+ }
598
+ else {
599
+ for (const v of c) {
600
+ sql += this.field(v) + ', ';
601
+ }
602
+ sql = sql.slice(0, -2);
603
+ }
604
+ this._sql.push(sql);
605
+ return this;
606
+ }
607
+ limit(a, b = 0) {
608
+ this._sql.push(' LIMIT ' + a.toString() + (b > 0 ? ', ' + b.toString() : ''));
609
+ return this;
610
+ }
611
+ lock() {
612
+ this._sql.push(' FOR UPDATE');
613
+ return this;
614
+ }
615
+ copy(f, opt = {}) {
616
+ const sql = lCore.clone(this._sql);
617
+ const data = lCore.clone(this._data);
618
+ if (opt.where !== undefined) {
619
+ if (typeof opt.where === 'string') {
620
+ for (let i = 0; i < sql.length; ++i) {
621
+ if (!sql[i].startsWith(' WHERE ')) {
622
+ continue;
623
+ }
624
+ sql[i] = opt.where ? (' WHERE ' + opt.where) : '';
625
+ data.splice(this._whereDataPosition[0], this._whereDataPosition[1] - this._whereDataPosition[0]);
626
+ break;
627
+ }
628
+ }
629
+ else {
630
+ let go = false;
631
+ if (Array.isArray(opt.where)) {
632
+ if (opt.where.length) {
633
+ go = true;
634
+ }
635
+ }
636
+ else {
637
+ if (Object.keys(opt.where).length) {
638
+ go = true;
639
+ }
640
+ }
641
+ for (let i = 0; i < sql.length; ++i) {
642
+ if (!sql[i].startsWith(' WHERE ')) {
643
+ continue;
644
+ }
645
+ if (go) {
646
+ const d = [];
647
+ sql[i] = ' WHERE ' + this._whereSub(opt.where, d);
648
+ data.splice(this._whereDataPosition[0], this._whereDataPosition[1] - this._whereDataPosition[0], ...d);
649
+ }
650
+ else {
651
+ sql.splice(i, 1);
652
+ data.splice(this._whereDataPosition[0], this._whereDataPosition[1] - this._whereDataPosition[0]);
653
+ }
654
+ break;
655
+ }
656
+ }
657
+ }
658
+ if (f && sql[0]) {
659
+ let table = '';
660
+ if (typeof f === 'string') {
661
+ table = this.field(f, this._pre);
662
+ }
663
+ else {
664
+ for (const i of f) {
665
+ table += this.field(i, this._pre) + ', ';
666
+ }
667
+ table = table.slice(0, -2);
668
+ }
669
+ sql[0] = sql[0].replace(/FROM [`\w, ]+/, 'FROM ' + table);
670
+ }
671
+ return get(this.getPre(), {
672
+ 'data': data,
673
+ 'sql': sql
674
+ });
675
+ }
676
+ getSql() {
677
+ return this._sql.join('');
678
+ }
679
+ getData() {
680
+ return this._data;
681
+ }
682
+ getPre() {
683
+ return this._pre;
684
+ }
685
+ format(sql, data) {
686
+ return mysql2.format(sql ?? this.getSql(), data ?? this.getData());
687
+ }
688
+ append(sql) {
689
+ this._sql.push(sql);
690
+ return this;
691
+ }
692
+ field(str, pre = '', suf = '') {
693
+ let left = '';
694
+ let right = '';
695
+ if (Array.isArray(str)) {
696
+ this._data.push(...str[1]);
697
+ return this.field(str[0]);
698
+ }
699
+ if (typeof str === 'number') {
700
+ str = str.toString();
701
+ }
702
+ str = str.trim();
703
+ str = str.replace(/ {2,}/g, ' ');
704
+ str = str.replace(/ +([),])/g, ' $1');
705
+ str = str.replace(/([(,]) +/g, '$1 ');
706
+ str = str.replace(/(\W)(JOIN|WHERE|UNION)(\W)/ig, '$1$3');
707
+ let sufAs = false;
708
+ if (suf.startsWith('#')) {
709
+ suf = suf.slice(1);
710
+ sufAs = true;
711
+ }
712
+ const loStr = str.toLowerCase();
713
+ const asPos = loStr.indexOf(' as ');
714
+ if (asPos === -1) {
715
+ let spacePos = str.lastIndexOf(' ');
716
+ if (!/^[a-zA-Z_)]$/.test(str[spacePos - 1])) {
717
+ spacePos = -1;
718
+ }
719
+ if (spacePos !== -1) {
720
+ const spaceRight = str.slice(spacePos + 1);
721
+ if (/^[a-zA-Z_`][\w`]*$/.test(spaceRight)) {
722
+ left = str.slice(0, spacePos);
723
+ right = spaceRight;
724
+ }
725
+ else {
726
+ left = str;
727
+ right = '';
728
+ }
729
+ }
730
+ else {
731
+ left = str;
732
+ right = '';
733
+ }
734
+ }
735
+ else {
736
+ left = str.slice(0, asPos);
737
+ right = str.slice(asPos + 4);
738
+ }
739
+ if (right) {
740
+ if (right.startsWith('`')) {
741
+ right = '`' + pre + right.slice(1);
742
+ }
743
+ else {
744
+ right = '`' + pre + right + '`';
745
+ }
746
+ right = ' AS ' + right;
747
+ }
748
+ else {
749
+ if (sufAs) {
750
+ right = ' AS ' + this.field(left, pre);
751
+ }
752
+ }
753
+ if (/^[\w`_.*]+$/.test(left)) {
754
+ const l = left.split('.');
755
+ if (l[0] === '*') {
756
+ return '*' + right;
757
+ }
758
+ if (l[0].startsWith('`')) {
759
+ l[0] = l[0].replace(/`/g, '');
760
+ }
761
+ if (l[1] === undefined) {
762
+ if (/^[A-Z0-9_]+$/.test(l[0])) {
763
+ return l[0] + right;
764
+ }
765
+ return '`' + pre + l[0] + suf + '`' + right;
766
+ }
767
+ const w = l[1] === '*' ? '*' : (l[1].startsWith('`') ? l[1] : ('`' + l[1] + '`'));
768
+ return '`' + this._pre + l[0] + suf + '`.' + w + right;
769
+ }
770
+ else {
771
+ return left.replace(/(^|[(, ])([a-zA-Z`_][\w`_.]*)(?=[), ]|$)/g, (t, t1, t2) => {
772
+ return t1 + this.field(t2, pre, suf);
773
+ }) + right;
774
+ }
775
+ }
776
+ _isField(arg) {
777
+ if (arg.type !== 'column' || arg.token !== columnToken || arg.value === undefined) {
778
+ return false;
779
+ }
780
+ return true;
781
+ }
782
+ }
783
+ exports.Sql = Sql;
784
+ function get(ctrPre, opt = {}) {
785
+ return new Sql(ctrPre instanceof ctr.Ctr ? ctrPre.getPrototype('_config').sql.pre : ctrPre, opt);
786
+ }
787
+ function format(sql, data) {
788
+ return mysql2.format(sql, data);
789
+ }
790
+ function aoMix(arr) {
791
+ if (!Array.isArray(arr)) {
792
+ return arr;
793
+ }
794
+ const mix = {};
795
+ let i = 0;
796
+ for (const v of arr) {
797
+ if (Array.isArray(v)) {
798
+ mix[i] = v;
799
+ ++i;
800
+ }
801
+ else {
802
+ for (const k in v) {
803
+ mix[k] = v[k];
804
+ }
805
+ }
806
+ }
807
+ return mix;
808
+ }
809
+ function column(field) {
810
+ if (!columnToken) {
811
+ columnToken = lCore.random(8, lCore.RANDOM_LUNS);
812
+ }
813
+ return {
814
+ 'token': columnToken,
815
+ 'type': 'column',
816
+ 'value': field
817
+ };
818
+ }