@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/sys/mod.js
ADDED
|
@@ -0,0 +1,1273 @@
|
|
|
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
|
+
const lSql = __importStar(require("~/lib/sql"));
|
|
37
|
+
const lTime = __importStar(require("~/lib/time"));
|
|
38
|
+
const lCore = __importStar(require("~/lib/core"));
|
|
39
|
+
const lText = __importStar(require("~/lib/text"));
|
|
40
|
+
const sCtr = __importStar(require("~/sys/ctr"));
|
|
41
|
+
class Rows {
|
|
42
|
+
constructor(initialItems = []) {
|
|
43
|
+
this._items = [];
|
|
44
|
+
this._items = initialItems;
|
|
45
|
+
}
|
|
46
|
+
get length() {
|
|
47
|
+
return this._items.length;
|
|
48
|
+
}
|
|
49
|
+
item(index) {
|
|
50
|
+
return this._items[index];
|
|
51
|
+
}
|
|
52
|
+
toArray() {
|
|
53
|
+
const arr = [];
|
|
54
|
+
for (const item of this._items) {
|
|
55
|
+
arr.push(item.toArray());
|
|
56
|
+
}
|
|
57
|
+
return arr;
|
|
58
|
+
}
|
|
59
|
+
filter(predicate) {
|
|
60
|
+
const items = [];
|
|
61
|
+
for (let i = 0; i < this._items.length; ++i) {
|
|
62
|
+
if (!predicate(this._items[i], i)) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
items.push(this._items[i]);
|
|
66
|
+
}
|
|
67
|
+
return new Rows(items);
|
|
68
|
+
}
|
|
69
|
+
map(allbackfn) {
|
|
70
|
+
const items = [];
|
|
71
|
+
for (let i = 0; i < this._items.length; ++i) {
|
|
72
|
+
items.push(allbackfn(this._items[i], i));
|
|
73
|
+
}
|
|
74
|
+
return items;
|
|
75
|
+
}
|
|
76
|
+
[Symbol.iterator]() {
|
|
77
|
+
let index = 0;
|
|
78
|
+
return {
|
|
79
|
+
next: () => {
|
|
80
|
+
if (index < this._items.length) {
|
|
81
|
+
return {
|
|
82
|
+
value: this._items[index++],
|
|
83
|
+
done: false
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
return {
|
|
88
|
+
value: undefined,
|
|
89
|
+
done: true
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
class Mod {
|
|
97
|
+
constructor(opt) {
|
|
98
|
+
this._updates = {};
|
|
99
|
+
this._data = {};
|
|
100
|
+
this._index = null;
|
|
101
|
+
this._contain = null;
|
|
102
|
+
this._total = [];
|
|
103
|
+
this._ctr = undefined;
|
|
104
|
+
this._limit = [0, 0];
|
|
105
|
+
this._ctr = opt.ctr;
|
|
106
|
+
this._db = opt.db;
|
|
107
|
+
this._sql = lSql.get(opt.pre ?? opt.ctr);
|
|
108
|
+
if (opt.index) {
|
|
109
|
+
this._index = typeof opt.index === 'string' ? [opt.index] : [...new Set(opt.index)];
|
|
110
|
+
}
|
|
111
|
+
if (opt.contain) {
|
|
112
|
+
this._contain = opt.contain;
|
|
113
|
+
}
|
|
114
|
+
if (opt.row) {
|
|
115
|
+
for (const k in opt.row) {
|
|
116
|
+
const v = opt.row[k];
|
|
117
|
+
this._data[k] = v;
|
|
118
|
+
this[k] = v;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const select = opt.select ?? (opt.where ? '*' : '');
|
|
122
|
+
if (select) {
|
|
123
|
+
this._sql.select(select, this.constructor._$table +
|
|
124
|
+
(this._index !== null ? ('_' + this._index[0]) : '') +
|
|
125
|
+
(opt.alias ? ' ' + opt.alias : ''));
|
|
126
|
+
}
|
|
127
|
+
if (opt.where !== undefined) {
|
|
128
|
+
if (this.constructor._soft && !opt.raw) {
|
|
129
|
+
if (typeof opt.where === 'string') {
|
|
130
|
+
opt.where = opt.where ? ('(' + opt.where + ') AND ') : '`time_remove` = 0';
|
|
131
|
+
}
|
|
132
|
+
else if (Array.isArray(opt.where)) {
|
|
133
|
+
opt.where.push({
|
|
134
|
+
'time_remove': 0
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
opt.where['time_remove'] = 0;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
this._sql.where(opt.where);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
static column(field) {
|
|
145
|
+
return lSql.column(field);
|
|
146
|
+
}
|
|
147
|
+
static async insert(db, cs, vs, opt = {}) {
|
|
148
|
+
const sq = lSql.get(opt.pre);
|
|
149
|
+
sq.insert(this._$table + (opt.index ? ('_' + opt.index) : '')).values(cs, vs);
|
|
150
|
+
const r = await db.execute(sq.getSql(), sq.getData());
|
|
151
|
+
if (r.packet === null) {
|
|
152
|
+
await lCore.log(opt.pre instanceof sCtr.Ctr ? opt.pre : {
|
|
153
|
+
'path': '',
|
|
154
|
+
'urlFull': '',
|
|
155
|
+
'hostname': '',
|
|
156
|
+
'req': null,
|
|
157
|
+
'get': {},
|
|
158
|
+
'cookie': {},
|
|
159
|
+
'headers': {}
|
|
160
|
+
}, '[insert, mod] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1), '-error');
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
if (r.packet.affectedRows > 0) {
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
static insertSql(cs, vs, opt = {}) {
|
|
171
|
+
const sq = lSql.get(opt.pre);
|
|
172
|
+
sq.insert(this._$table + (opt.index ? ('_' + opt.index) : '')).values(cs, vs);
|
|
173
|
+
return sq.format();
|
|
174
|
+
}
|
|
175
|
+
static async insertDuplicate(db, data, update, opt = {}) {
|
|
176
|
+
const sq = lSql.get(opt.pre);
|
|
177
|
+
sq.insert(this._$table + (opt.index ? ('_' + opt.index) : '')).values(data).duplicate(update);
|
|
178
|
+
const r = await db.execute(sq.getSql(), sq.getData());
|
|
179
|
+
if (r.packet === null) {
|
|
180
|
+
await lCore.log(opt.pre instanceof sCtr.Ctr ? opt.pre : {
|
|
181
|
+
'path': '',
|
|
182
|
+
'urlFull': '',
|
|
183
|
+
'hostname': '',
|
|
184
|
+
'req': null,
|
|
185
|
+
'get': {},
|
|
186
|
+
'cookie': {},
|
|
187
|
+
'headers': {}
|
|
188
|
+
}, '[insertDuplicate, mod] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
if (r.packet.affectedRows > 0) {
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
static async removeByWhere(db, where, opt = {}) {
|
|
199
|
+
const tim = lTime.stamp();
|
|
200
|
+
const sq = lSql.get(opt.pre);
|
|
201
|
+
if (this._$soft && !opt.raw) {
|
|
202
|
+
sq.update(this._$table + (opt.index ? ('_' + opt.index) : ''), [{
|
|
203
|
+
'time_remove': tim
|
|
204
|
+
}]);
|
|
205
|
+
if (typeof where === 'string') {
|
|
206
|
+
where = '(' + where + ') AND `time_remove` = 0';
|
|
207
|
+
}
|
|
208
|
+
else if (Array.isArray(where)) {
|
|
209
|
+
where.push({
|
|
210
|
+
'time_remove': 0
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
where['time_remove'] = 0;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
sq.delete(this._$table + (opt.index ? ('_' + opt.index) : ''));
|
|
219
|
+
}
|
|
220
|
+
sq.where(where);
|
|
221
|
+
if (opt.by) {
|
|
222
|
+
sq.by(opt.by[0], opt.by[1]);
|
|
223
|
+
}
|
|
224
|
+
if (opt.limit) {
|
|
225
|
+
sq.limit(opt.limit[0], opt.limit[1]);
|
|
226
|
+
}
|
|
227
|
+
const r = await db.execute(sq.getSql(), sq.getData());
|
|
228
|
+
if (r.packet === null) {
|
|
229
|
+
await lCore.log(opt.pre instanceof sCtr.Ctr ? opt.pre : {
|
|
230
|
+
'path': '',
|
|
231
|
+
'urlFull': '',
|
|
232
|
+
'hostname': '',
|
|
233
|
+
'req': null,
|
|
234
|
+
'get': {},
|
|
235
|
+
'cookie': {},
|
|
236
|
+
'headers': {}
|
|
237
|
+
}, '[removeByWhere, mod] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
if (r.packet.affectedRows > 0) {
|
|
241
|
+
return r.packet.affectedRows;
|
|
242
|
+
}
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
static removeByWhereSql(db, where, opt = {}) {
|
|
246
|
+
const tim = lTime.stamp();
|
|
247
|
+
const sq = lSql.get(opt.pre);
|
|
248
|
+
if (this._$soft && !opt.raw) {
|
|
249
|
+
sq.update(this._$table + (opt.index ? ('_' + opt.index) : ''), [{
|
|
250
|
+
'time_remove': tim
|
|
251
|
+
}]);
|
|
252
|
+
if (typeof where === 'string') {
|
|
253
|
+
where = '(' + where + ') AND `time_remove` = 0';
|
|
254
|
+
}
|
|
255
|
+
else if (Array.isArray(where)) {
|
|
256
|
+
where.push({
|
|
257
|
+
'time_remove': 0
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
where['time_remove'] = 0;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
sq.delete(this._$table + (opt.index ? ('_' + opt.index) : ''));
|
|
266
|
+
}
|
|
267
|
+
sq.where(where);
|
|
268
|
+
if (opt.by) {
|
|
269
|
+
sq.by(opt.by[0], opt.by[1]);
|
|
270
|
+
}
|
|
271
|
+
if (opt.limit) {
|
|
272
|
+
sq.limit(opt.limit[0], opt.limit[1]);
|
|
273
|
+
}
|
|
274
|
+
return sq;
|
|
275
|
+
}
|
|
276
|
+
static async updateByWhere(db, data, where, opt = {}) {
|
|
277
|
+
if (typeof opt.index === 'string') {
|
|
278
|
+
opt.index = [opt.index];
|
|
279
|
+
}
|
|
280
|
+
else if (!opt.index) {
|
|
281
|
+
opt.index = [''];
|
|
282
|
+
}
|
|
283
|
+
if (this._$soft && !opt.raw) {
|
|
284
|
+
if (typeof where === 'string') {
|
|
285
|
+
where = '(' + where + ') AND `time_remove` = 0';
|
|
286
|
+
}
|
|
287
|
+
else if (Array.isArray(where)) {
|
|
288
|
+
where.push({
|
|
289
|
+
'time_remove': 0
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
where['time_remove'] = 0;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
let ar = 0;
|
|
297
|
+
for (const index of opt.index) {
|
|
298
|
+
const sq = lSql.get(opt.pre);
|
|
299
|
+
sq.update(this._$table + (index ? ('_' + index) : ''), data);
|
|
300
|
+
sq.where(where);
|
|
301
|
+
if (opt.by) {
|
|
302
|
+
sq.by(opt.by[0], opt.by[1]);
|
|
303
|
+
}
|
|
304
|
+
if (opt.limit) {
|
|
305
|
+
sq.limit(opt.limit[0], opt.limit[1]);
|
|
306
|
+
}
|
|
307
|
+
const r = await db.execute(sq.getSql(), sq.getData());
|
|
308
|
+
if (r.packet === null) {
|
|
309
|
+
await lCore.log(opt.pre instanceof sCtr.Ctr ? opt.pre : {
|
|
310
|
+
'path': '',
|
|
311
|
+
'urlFull': '',
|
|
312
|
+
'hostname': '',
|
|
313
|
+
'req': null,
|
|
314
|
+
'get': {},
|
|
315
|
+
'cookie': {},
|
|
316
|
+
'headers': {}
|
|
317
|
+
}, '[updateByWhere, mod] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
|
|
318
|
+
return false;
|
|
319
|
+
}
|
|
320
|
+
if (r.packet.affectedRows > 0) {
|
|
321
|
+
ar += r.packet.affectedRows;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return ar ? ar : null;
|
|
325
|
+
}
|
|
326
|
+
static updateByWhereSql(data, where, opt = {}) {
|
|
327
|
+
const sq = lSql.get(opt.pre);
|
|
328
|
+
sq.update(this._$table + (opt.index ? ('_' + opt.index) : ''), data);
|
|
329
|
+
if (this._$soft && !opt.raw) {
|
|
330
|
+
if (typeof where === 'string') {
|
|
331
|
+
where = '(' + where + ') AND `time_remove` = 0';
|
|
332
|
+
}
|
|
333
|
+
else if (Array.isArray(where)) {
|
|
334
|
+
where.push({
|
|
335
|
+
'time_remove': 0
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
where['time_remove'] = 0;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
sq.where(where);
|
|
343
|
+
if (opt.by) {
|
|
344
|
+
sq.by(opt.by[0], opt.by[1]);
|
|
345
|
+
}
|
|
346
|
+
if (opt.limit) {
|
|
347
|
+
sq.limit(opt.limit[0], opt.limit[1]);
|
|
348
|
+
}
|
|
349
|
+
return sq;
|
|
350
|
+
}
|
|
351
|
+
static select(db, c, opt = {}) {
|
|
352
|
+
return new this({
|
|
353
|
+
'db': db,
|
|
354
|
+
'ctr': opt.ctr,
|
|
355
|
+
'pre': opt.pre,
|
|
356
|
+
'select': c,
|
|
357
|
+
'index': opt.index,
|
|
358
|
+
'alias': opt.alias,
|
|
359
|
+
'contain': opt.contain
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
static where(db, s = '', opt = {}) {
|
|
363
|
+
return new this({
|
|
364
|
+
'db': db,
|
|
365
|
+
'ctr': opt.ctr,
|
|
366
|
+
'pre': opt.pre,
|
|
367
|
+
'where': s,
|
|
368
|
+
'raw': opt.raw,
|
|
369
|
+
'index': opt.index,
|
|
370
|
+
'contain': opt.contain
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
static getCreate(db, opt = {}) {
|
|
374
|
+
return new this({
|
|
375
|
+
'db': db,
|
|
376
|
+
'ctr': opt.ctr,
|
|
377
|
+
'pre': opt.pre,
|
|
378
|
+
'index': opt.index
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
static find(db, val, opt = {}) {
|
|
382
|
+
return new this({
|
|
383
|
+
'db': db,
|
|
384
|
+
'ctr': opt.ctr,
|
|
385
|
+
'pre': opt.pre,
|
|
386
|
+
'where': [{
|
|
387
|
+
[this._$primary]: val
|
|
388
|
+
}],
|
|
389
|
+
'raw': opt.raw,
|
|
390
|
+
'index': opt.index
|
|
391
|
+
}).first(opt.lock);
|
|
392
|
+
}
|
|
393
|
+
static async one(db, s, opt = {}) {
|
|
394
|
+
if (!opt.index) {
|
|
395
|
+
const o = new this({
|
|
396
|
+
'select': opt.select,
|
|
397
|
+
'db': db,
|
|
398
|
+
'ctr': opt.ctr,
|
|
399
|
+
'pre': opt.pre,
|
|
400
|
+
'where': s,
|
|
401
|
+
'raw': opt.raw
|
|
402
|
+
});
|
|
403
|
+
if (opt.by) {
|
|
404
|
+
o.by(opt.by[0], opt.by[1]);
|
|
405
|
+
}
|
|
406
|
+
return opt.array ? o.firstArray() : o.first();
|
|
407
|
+
}
|
|
408
|
+
opt.index = typeof opt.index === 'string' ? [opt.index] : [...new Set(opt.index)];
|
|
409
|
+
for (const item of opt.index) {
|
|
410
|
+
const row = new this({
|
|
411
|
+
'select': opt.select,
|
|
412
|
+
'db': db,
|
|
413
|
+
'ctr': opt.ctr,
|
|
414
|
+
'pre': opt.pre,
|
|
415
|
+
'where': s,
|
|
416
|
+
'raw': opt.raw,
|
|
417
|
+
'index': item
|
|
418
|
+
});
|
|
419
|
+
if (opt.by) {
|
|
420
|
+
row.by(opt.by[0], opt.by[1]);
|
|
421
|
+
}
|
|
422
|
+
const rowr = await (opt.array ? row.firstArray() : row.first());
|
|
423
|
+
if (rowr) {
|
|
424
|
+
return rowr;
|
|
425
|
+
}
|
|
426
|
+
if (rowr === false) {
|
|
427
|
+
return false;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return null;
|
|
431
|
+
}
|
|
432
|
+
static async oneArray(db, s, opt = {}) {
|
|
433
|
+
opt.array = true;
|
|
434
|
+
return this.one(db, s, opt);
|
|
435
|
+
}
|
|
436
|
+
static async primarys(db, where = '', opt = {}) {
|
|
437
|
+
const sq = lSql.get(opt.pre ?? opt.ctr);
|
|
438
|
+
if (this._$soft && !opt.raw) {
|
|
439
|
+
if (typeof where === 'string') {
|
|
440
|
+
if (where !== '') {
|
|
441
|
+
where = '(' + where + ') AND `time_remove` = 0';
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
else if (Array.isArray(where)) {
|
|
445
|
+
where.push({
|
|
446
|
+
'time_remove': 0
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
else {
|
|
450
|
+
where['time_remove'] = 0;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
sq.select(this._$primary, this._$table + (opt.index ? ('_' + opt.index) : '')).where(where);
|
|
454
|
+
const r = await db.query(sq.getSql(), sq.getData());
|
|
455
|
+
if (r.rows === null) {
|
|
456
|
+
await lCore.log(opt.ctr ?? {
|
|
457
|
+
'path': '',
|
|
458
|
+
'urlFull': '',
|
|
459
|
+
'hostname': '',
|
|
460
|
+
'req': null,
|
|
461
|
+
'get': {},
|
|
462
|
+
'cookie': {},
|
|
463
|
+
'headers': {}
|
|
464
|
+
}, '[primarys, mod] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
|
|
465
|
+
return false;
|
|
466
|
+
}
|
|
467
|
+
const primarys = [];
|
|
468
|
+
for (const row of r.rows) {
|
|
469
|
+
primarys.push(row[this._$primary]);
|
|
470
|
+
}
|
|
471
|
+
return primarys;
|
|
472
|
+
}
|
|
473
|
+
static toArrayByRecord(obj) {
|
|
474
|
+
const rtn = {};
|
|
475
|
+
for (const key in obj) {
|
|
476
|
+
rtn[key] = obj[key].toArray();
|
|
477
|
+
}
|
|
478
|
+
return rtn;
|
|
479
|
+
}
|
|
480
|
+
set(n, v) {
|
|
481
|
+
if (typeof n === 'object') {
|
|
482
|
+
for (const k in n) {
|
|
483
|
+
const v = n[k];
|
|
484
|
+
if (v === undefined) {
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
this._updates[k] = true;
|
|
488
|
+
this._data[k] = v;
|
|
489
|
+
this[k] = v;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
else {
|
|
493
|
+
if (v === undefined) {
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
if (typeof n !== 'string') {
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
this._updates[n] = true;
|
|
500
|
+
this._data[n] = v;
|
|
501
|
+
this[n] = v;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
get(n) {
|
|
505
|
+
return this._data[n];
|
|
506
|
+
}
|
|
507
|
+
async create(notWhere, table) {
|
|
508
|
+
const cstr = this.constructor;
|
|
509
|
+
const updates = {};
|
|
510
|
+
for (const k in this._updates) {
|
|
511
|
+
updates[k] = this._data[k];
|
|
512
|
+
}
|
|
513
|
+
if (!table) {
|
|
514
|
+
table = cstr._$table + (this._index ? ('_' + this._index[0]) : '');
|
|
515
|
+
}
|
|
516
|
+
let r = null;
|
|
517
|
+
if ((cstr._$key !== '') && (updates[cstr._$key] === undefined)) {
|
|
518
|
+
let count = 0;
|
|
519
|
+
while (true) {
|
|
520
|
+
if (count === 3) {
|
|
521
|
+
return false;
|
|
522
|
+
}
|
|
523
|
+
updates[cstr._$key] = this._keyGenerator();
|
|
524
|
+
this._data[cstr._$key] = updates[cstr._$key];
|
|
525
|
+
this[cstr._$key] = updates[cstr._$key];
|
|
526
|
+
this._sql.insert(cstr._$table + (this._index ? ('_' + this._index[0]) : ''));
|
|
527
|
+
if (notWhere) {
|
|
528
|
+
this._sql.notExists(table, updates, notWhere);
|
|
529
|
+
}
|
|
530
|
+
else {
|
|
531
|
+
this._sql.values(updates);
|
|
532
|
+
}
|
|
533
|
+
r = await this._db.execute(this._sql.getSql(), this._sql.getData());
|
|
534
|
+
++count;
|
|
535
|
+
if (!r.error) {
|
|
536
|
+
break;
|
|
537
|
+
}
|
|
538
|
+
if (r.error.errno !== 1062) {
|
|
539
|
+
await lCore.log(this._ctr ?? {
|
|
540
|
+
'path': '',
|
|
541
|
+
'urlFull': '',
|
|
542
|
+
'hostname': '',
|
|
543
|
+
'req': null,
|
|
544
|
+
'get': {},
|
|
545
|
+
'cookie': {},
|
|
546
|
+
'headers': {}
|
|
547
|
+
}, '[create0, mod] [' + table + '] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
|
|
548
|
+
return false;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
else {
|
|
553
|
+
this._sql.insert(cstr._$table + (this._index ? ('_' + this._index[0]) : ''));
|
|
554
|
+
if (notWhere) {
|
|
555
|
+
this._sql.notExists(table, updates, notWhere);
|
|
556
|
+
}
|
|
557
|
+
else {
|
|
558
|
+
this._sql.values(updates);
|
|
559
|
+
}
|
|
560
|
+
r = await this._db.execute(this._sql.getSql(), this._sql.getData());
|
|
561
|
+
if (r.error) {
|
|
562
|
+
if (r.error.errno !== 1062) {
|
|
563
|
+
await lCore.log(this._ctr ?? {
|
|
564
|
+
'path': '',
|
|
565
|
+
'urlFull': '',
|
|
566
|
+
'hostname': '',
|
|
567
|
+
'req': null,
|
|
568
|
+
'get': {},
|
|
569
|
+
'cookie': {},
|
|
570
|
+
'headers': {}
|
|
571
|
+
}, '[create1, mod] [' + table + '] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
|
|
572
|
+
}
|
|
573
|
+
return false;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
if (r?.packet?.affectedRows && r?.packet?.affectedRows > 0) {
|
|
577
|
+
this._updates = {};
|
|
578
|
+
this._data[cstr._$primary] = r.packet.insertId;
|
|
579
|
+
this[cstr._$primary] = this._data[cstr._$primary];
|
|
580
|
+
return true;
|
|
581
|
+
}
|
|
582
|
+
else {
|
|
583
|
+
return false;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
async replace() {
|
|
587
|
+
const cstr = this.constructor;
|
|
588
|
+
const updates = {};
|
|
589
|
+
for (const k in this._updates) {
|
|
590
|
+
updates[k] = this._data[k];
|
|
591
|
+
}
|
|
592
|
+
this._sql.replace(cstr._$table + (this._index ? ('_' + this._index[0]) : '')).values(updates);
|
|
593
|
+
const r = await this._db.execute(this._sql.getSql(), this._sql.getData());
|
|
594
|
+
if (r.packet === null) {
|
|
595
|
+
await lCore.log(this._ctr ?? {
|
|
596
|
+
'path': '',
|
|
597
|
+
'urlFull': '',
|
|
598
|
+
'hostname': '',
|
|
599
|
+
'req': null,
|
|
600
|
+
'get': {},
|
|
601
|
+
'cookie': {},
|
|
602
|
+
'headers': {}
|
|
603
|
+
}, '[replace, mod] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
|
|
604
|
+
return false;
|
|
605
|
+
}
|
|
606
|
+
if (r.packet.affectedRows > 0) {
|
|
607
|
+
this._updates = {};
|
|
608
|
+
this._data[cstr._$primary] = r.packet.insertId;
|
|
609
|
+
this[cstr._$primary] = this._data[cstr._$primary];
|
|
610
|
+
return true;
|
|
611
|
+
}
|
|
612
|
+
else {
|
|
613
|
+
return false;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
async refresh(lock = false) {
|
|
617
|
+
const cstr = this.constructor;
|
|
618
|
+
this._sql.select('*', cstr._$table + (this._index ? ('_' + this._index[0]) : '')).where([{
|
|
619
|
+
[cstr._$primary]: this._data[cstr._$primary]
|
|
620
|
+
}]);
|
|
621
|
+
if (lock) {
|
|
622
|
+
this._sql.lock();
|
|
623
|
+
}
|
|
624
|
+
const r = await this._db.query(this._sql.getSql(), this._sql.getData());
|
|
625
|
+
if (r.rows === null) {
|
|
626
|
+
await lCore.log(this._ctr ?? {
|
|
627
|
+
'path': '',
|
|
628
|
+
'urlFull': '',
|
|
629
|
+
'hostname': '',
|
|
630
|
+
'req': null,
|
|
631
|
+
'get': {},
|
|
632
|
+
'cookie': {},
|
|
633
|
+
'headers': {}
|
|
634
|
+
}, '[refresh, mod] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
|
|
635
|
+
return false;
|
|
636
|
+
}
|
|
637
|
+
if (r.rows.length === 0) {
|
|
638
|
+
return null;
|
|
639
|
+
}
|
|
640
|
+
for (const k in r.rows[0]) {
|
|
641
|
+
const v = r.rows[0][k];
|
|
642
|
+
this._data[k] = v;
|
|
643
|
+
this[k] = v;
|
|
644
|
+
}
|
|
645
|
+
return true;
|
|
646
|
+
}
|
|
647
|
+
async save() {
|
|
648
|
+
if (Object.keys(this._updates).length === 0) {
|
|
649
|
+
return true;
|
|
650
|
+
}
|
|
651
|
+
const cstr = this.constructor;
|
|
652
|
+
const updates = {};
|
|
653
|
+
for (const k in this._updates) {
|
|
654
|
+
updates[k] = this._data[k];
|
|
655
|
+
}
|
|
656
|
+
this._sql.update(cstr._$table + (this._index ? ('_' + this._index[0]) : ''), [updates]).where([{
|
|
657
|
+
[cstr._$primary]: this._data[cstr._$primary]
|
|
658
|
+
}]);
|
|
659
|
+
const r = await this._db.execute(this._sql.getSql(), this._sql.getData());
|
|
660
|
+
if (r.packet === null) {
|
|
661
|
+
await lCore.log(this._ctr ?? {
|
|
662
|
+
'path': '',
|
|
663
|
+
'urlFull': '',
|
|
664
|
+
'hostname': '',
|
|
665
|
+
'req': null,
|
|
666
|
+
'get': {},
|
|
667
|
+
'cookie': {},
|
|
668
|
+
'headers': {}
|
|
669
|
+
}, '[save, mod] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
|
|
670
|
+
return false;
|
|
671
|
+
}
|
|
672
|
+
if (r.packet.affectedRows > 0) {
|
|
673
|
+
this._updates = {};
|
|
674
|
+
return true;
|
|
675
|
+
}
|
|
676
|
+
else {
|
|
677
|
+
return false;
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
async remove(raw = false) {
|
|
681
|
+
const tim = lTime.stamp();
|
|
682
|
+
const cstr = this.constructor;
|
|
683
|
+
if (cstr._$soft && !raw) {
|
|
684
|
+
this._sql.update(cstr._$table + (this._index ? ('_' + this._index[0]) : ''), [{
|
|
685
|
+
'time_remove': tim
|
|
686
|
+
}]).where([{
|
|
687
|
+
[cstr._$primary]: this._data[cstr._$primary],
|
|
688
|
+
'time_remove': 0
|
|
689
|
+
}]);
|
|
690
|
+
}
|
|
691
|
+
else {
|
|
692
|
+
this._sql.delete(cstr._$table + (this._index ? ('_' + this._index[0]) : '')).where([{
|
|
693
|
+
[cstr._$primary]: this._data[cstr._$primary]
|
|
694
|
+
}]);
|
|
695
|
+
}
|
|
696
|
+
const r = await this._db.execute(this._sql.getSql(), this._sql.getData());
|
|
697
|
+
if (r.packet === null) {
|
|
698
|
+
await lCore.log(this._ctr ?? {
|
|
699
|
+
'path': '',
|
|
700
|
+
'urlFull': '',
|
|
701
|
+
'hostname': '',
|
|
702
|
+
'req': null,
|
|
703
|
+
'get': {},
|
|
704
|
+
'cookie': {},
|
|
705
|
+
'headers': {}
|
|
706
|
+
}, '[remove, mod] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
|
|
707
|
+
return false;
|
|
708
|
+
}
|
|
709
|
+
if (r.packet.affectedRows > 0) {
|
|
710
|
+
return true;
|
|
711
|
+
}
|
|
712
|
+
else {
|
|
713
|
+
return false;
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
async first(lock = false, array = false) {
|
|
717
|
+
this._sql.limit(1);
|
|
718
|
+
if (lock) {
|
|
719
|
+
this._sql.lock();
|
|
720
|
+
}
|
|
721
|
+
const r = await this._db.query(this._sql.getSql(), this._sql.getData());
|
|
722
|
+
if (r.rows === null) {
|
|
723
|
+
await lCore.log(this._ctr ?? {
|
|
724
|
+
'path': '',
|
|
725
|
+
'urlFull': '',
|
|
726
|
+
'hostname': '',
|
|
727
|
+
'req': null,
|
|
728
|
+
'get': {},
|
|
729
|
+
'cookie': {},
|
|
730
|
+
'headers': {}
|
|
731
|
+
}, '[first, mod] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
|
|
732
|
+
return false;
|
|
733
|
+
}
|
|
734
|
+
if (r.rows.length === 0) {
|
|
735
|
+
return null;
|
|
736
|
+
}
|
|
737
|
+
if (array) {
|
|
738
|
+
return r.rows[0];
|
|
739
|
+
}
|
|
740
|
+
for (const k in r.rows[0]) {
|
|
741
|
+
const v = r.rows[0][k];
|
|
742
|
+
this._data[k] = v;
|
|
743
|
+
this[k] = v;
|
|
744
|
+
}
|
|
745
|
+
return this;
|
|
746
|
+
}
|
|
747
|
+
async firstArray(lock = false) {
|
|
748
|
+
return this.first(lock, true);
|
|
749
|
+
}
|
|
750
|
+
union(f, type = '') {
|
|
751
|
+
if (f instanceof lSql.Sql) {
|
|
752
|
+
this._sql.union(f, type);
|
|
753
|
+
return this;
|
|
754
|
+
}
|
|
755
|
+
if (typeof f === 'string') {
|
|
756
|
+
f = {
|
|
757
|
+
'field': f
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
if (!Array.isArray(f)) {
|
|
761
|
+
f = [f];
|
|
762
|
+
}
|
|
763
|
+
for (let item of f) {
|
|
764
|
+
if (typeof item === 'string') {
|
|
765
|
+
item = {
|
|
766
|
+
'field': item
|
|
767
|
+
};
|
|
768
|
+
}
|
|
769
|
+
this._sql.union(this._sql.copy(item.field, {
|
|
770
|
+
'where': item.where
|
|
771
|
+
}), type);
|
|
772
|
+
}
|
|
773
|
+
return this;
|
|
774
|
+
}
|
|
775
|
+
unionAll(f) {
|
|
776
|
+
if (f instanceof lSql.Sql) {
|
|
777
|
+
this._sql.unionAll(f);
|
|
778
|
+
return this;
|
|
779
|
+
}
|
|
780
|
+
if (typeof f === 'string') {
|
|
781
|
+
f = {
|
|
782
|
+
'field': f
|
|
783
|
+
};
|
|
784
|
+
}
|
|
785
|
+
if (!Array.isArray(f)) {
|
|
786
|
+
f = [f];
|
|
787
|
+
}
|
|
788
|
+
for (let item of f) {
|
|
789
|
+
if (typeof item === 'string') {
|
|
790
|
+
item = {
|
|
791
|
+
'field': item
|
|
792
|
+
};
|
|
793
|
+
}
|
|
794
|
+
this._sql.unionAll(this._sql.copy(item.field, {
|
|
795
|
+
'where': item.where
|
|
796
|
+
}));
|
|
797
|
+
}
|
|
798
|
+
return this;
|
|
799
|
+
}
|
|
800
|
+
async all(key) {
|
|
801
|
+
this._total.length = 0;
|
|
802
|
+
if (this._index && this._index.length > 1) {
|
|
803
|
+
let sql = this._sql.getSql();
|
|
804
|
+
const list = key ? {} : [];
|
|
805
|
+
const limit = [this._limit[0] ?? 0, this._limit[1] ?? 200];
|
|
806
|
+
let offset = 0;
|
|
807
|
+
let remain = limit[1];
|
|
808
|
+
for (let i = 0; i < this._index.length; ++i) {
|
|
809
|
+
if (i > 0) {
|
|
810
|
+
sql = sql.replace(/(FROM [a-zA-Z0-9`_.]+?_)[0-9_]+/, '$1' + this._index[i]);
|
|
811
|
+
}
|
|
812
|
+
const tsql = this._formatTotal(sql);
|
|
813
|
+
const tr = await this._db.query(tsql, this._sql.getData());
|
|
814
|
+
if (tr.rows === null) {
|
|
815
|
+
return false;
|
|
816
|
+
}
|
|
817
|
+
let count = 0;
|
|
818
|
+
for (const item of tr.rows) {
|
|
819
|
+
count += item.count;
|
|
820
|
+
}
|
|
821
|
+
this._total.push(count);
|
|
822
|
+
if (remain === 0) {
|
|
823
|
+
continue;
|
|
824
|
+
}
|
|
825
|
+
let cz = 0;
|
|
826
|
+
if (offset > -1) {
|
|
827
|
+
cz = limit[0] - offset;
|
|
828
|
+
if (cz >= count) {
|
|
829
|
+
offset += count;
|
|
830
|
+
continue;
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
const lsql = sql.replace(/ LIMIT [0-9 ,]/g, ` LIMIT ${cz}, ${remain}`);
|
|
834
|
+
const r = await this._db.query(lsql, this._sql.getData());
|
|
835
|
+
if (r.rows === null) {
|
|
836
|
+
await lCore.log(this._ctr ?? {
|
|
837
|
+
'path': '',
|
|
838
|
+
'urlFull': '',
|
|
839
|
+
'hostname': '',
|
|
840
|
+
'req': null,
|
|
841
|
+
'get': {},
|
|
842
|
+
'cookie': {},
|
|
843
|
+
'headers': {}
|
|
844
|
+
}, '[all, mod] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
|
|
845
|
+
return false;
|
|
846
|
+
}
|
|
847
|
+
if (key) {
|
|
848
|
+
for (const row of r.rows) {
|
|
849
|
+
const obj = new this.constructor({
|
|
850
|
+
'db': this._db,
|
|
851
|
+
'ctr': this._ctr,
|
|
852
|
+
'pre': this._sql.getPre(),
|
|
853
|
+
'row': row,
|
|
854
|
+
'index': this._index
|
|
855
|
+
});
|
|
856
|
+
list[row[key]] = obj;
|
|
857
|
+
--remain;
|
|
858
|
+
}
|
|
859
|
+
continue;
|
|
860
|
+
}
|
|
861
|
+
for (const row of r.rows) {
|
|
862
|
+
const obj = new this.constructor({
|
|
863
|
+
'db': this._db,
|
|
864
|
+
'ctr': this._ctr,
|
|
865
|
+
'pre': this._sql.getPre(),
|
|
866
|
+
'row': row,
|
|
867
|
+
'index': this._index
|
|
868
|
+
});
|
|
869
|
+
list.push(obj);
|
|
870
|
+
--remain;
|
|
871
|
+
}
|
|
872
|
+
continue;
|
|
873
|
+
}
|
|
874
|
+
return Array.isArray(list) ? new Rows(list) : list;
|
|
875
|
+
}
|
|
876
|
+
const contain = this._contain ? lCore.clone(this._contain.list) : null;
|
|
877
|
+
const r = await this._db.query(this._sql.getSql(), this._sql.getData());
|
|
878
|
+
if (r.rows === null) {
|
|
879
|
+
await lCore.log(this._ctr ?? {
|
|
880
|
+
'path': '',
|
|
881
|
+
'urlFull': '',
|
|
882
|
+
'hostname': '',
|
|
883
|
+
'req': null,
|
|
884
|
+
'get': {},
|
|
885
|
+
'cookie': {},
|
|
886
|
+
'headers': {}
|
|
887
|
+
}, '[all, mod] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
|
|
888
|
+
return false;
|
|
889
|
+
}
|
|
890
|
+
for (const row of r.rows) {
|
|
891
|
+
if (this._contain && contain) {
|
|
892
|
+
const io = contain.indexOf(row[this._contain.key]);
|
|
893
|
+
if (io !== -1) {
|
|
894
|
+
contain.splice(io, 1);
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
let cr = null;
|
|
899
|
+
if (this._contain && contain?.length) {
|
|
900
|
+
const csql = this._sql.copy(undefined, {
|
|
901
|
+
'where': {
|
|
902
|
+
[this._contain.key]: this._contain.list
|
|
903
|
+
}
|
|
904
|
+
});
|
|
905
|
+
cr = await this._db.query(csql.getSql(), csql.getData());
|
|
906
|
+
}
|
|
907
|
+
if (key) {
|
|
908
|
+
const list = {};
|
|
909
|
+
for (const row of r.rows) {
|
|
910
|
+
const obj = new this.constructor({
|
|
911
|
+
'db': this._db,
|
|
912
|
+
'ctr': this._ctr,
|
|
913
|
+
'pre': this._sql.getPre(),
|
|
914
|
+
'row': row,
|
|
915
|
+
'index': this._index
|
|
916
|
+
});
|
|
917
|
+
list[row[key]] = obj;
|
|
918
|
+
}
|
|
919
|
+
if (cr?.rows) {
|
|
920
|
+
for (const crow of cr.rows) {
|
|
921
|
+
const obj = new this.constructor({
|
|
922
|
+
'db': this._db,
|
|
923
|
+
'ctr': this._ctr,
|
|
924
|
+
'pre': this._sql.getPre(),
|
|
925
|
+
'row': crow,
|
|
926
|
+
'index': this._index
|
|
927
|
+
});
|
|
928
|
+
list[crow[key]] = obj;
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
return list;
|
|
932
|
+
}
|
|
933
|
+
const list = [];
|
|
934
|
+
for (const row of r.rows) {
|
|
935
|
+
const obj = new this.constructor({
|
|
936
|
+
'db': this._db,
|
|
937
|
+
'ctr': this._ctr,
|
|
938
|
+
'pre': this._sql.getPre(),
|
|
939
|
+
'row': row,
|
|
940
|
+
'index': this._index
|
|
941
|
+
});
|
|
942
|
+
list.push(obj);
|
|
943
|
+
}
|
|
944
|
+
if (cr?.rows) {
|
|
945
|
+
for (const crow of cr.rows) {
|
|
946
|
+
const obj = new this.constructor({
|
|
947
|
+
'db': this._db,
|
|
948
|
+
'ctr': this._ctr,
|
|
949
|
+
'pre': this._sql.getPre(),
|
|
950
|
+
'row': crow,
|
|
951
|
+
'index': this._index
|
|
952
|
+
});
|
|
953
|
+
list.push(obj);
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
return new Rows(list);
|
|
957
|
+
}
|
|
958
|
+
async allArray(key) {
|
|
959
|
+
this._total.length = 0;
|
|
960
|
+
if (this._index && this._index.length > 1) {
|
|
961
|
+
let sql = this._sql.getSql();
|
|
962
|
+
const list = key ? {} : [];
|
|
963
|
+
const limit = [this._limit[0] ?? 0, this._limit[1] ?? 200];
|
|
964
|
+
let offset = 0;
|
|
965
|
+
let remain = limit[1];
|
|
966
|
+
for (let i = 0; i < this._index.length; ++i) {
|
|
967
|
+
if (i > 0) {
|
|
968
|
+
sql = sql.replace(/(FROM [a-zA-Z0-9`_.]+?_)[0-9_]+/, '$1' + this._index[i]);
|
|
969
|
+
}
|
|
970
|
+
const tsql = this._formatTotal(sql);
|
|
971
|
+
const tr = await this._db.query(tsql, this._sql.getData());
|
|
972
|
+
if (tr.rows === null) {
|
|
973
|
+
return false;
|
|
974
|
+
}
|
|
975
|
+
let count = 0;
|
|
976
|
+
for (const item of tr.rows) {
|
|
977
|
+
count += item.count;
|
|
978
|
+
}
|
|
979
|
+
this._total.push(count);
|
|
980
|
+
if (remain === 0) {
|
|
981
|
+
continue;
|
|
982
|
+
}
|
|
983
|
+
let cz = 0;
|
|
984
|
+
if (offset > -1) {
|
|
985
|
+
cz = limit[0] - offset;
|
|
986
|
+
if (cz >= count) {
|
|
987
|
+
offset += count;
|
|
988
|
+
continue;
|
|
989
|
+
}
|
|
990
|
+
offset = -1;
|
|
991
|
+
}
|
|
992
|
+
const lsql = sql.replace(/ LIMIT [0-9 ,]+/g, ` LIMIT ${cz}, ${remain}`);
|
|
993
|
+
const r = await this._db.query(lsql, this._sql.getData());
|
|
994
|
+
if (r.rows === null) {
|
|
995
|
+
await lCore.log(this._ctr ?? {
|
|
996
|
+
'path': '',
|
|
997
|
+
'urlFull': '',
|
|
998
|
+
'hostname': '',
|
|
999
|
+
'req': null,
|
|
1000
|
+
'get': {},
|
|
1001
|
+
'cookie': {},
|
|
1002
|
+
'headers': {}
|
|
1003
|
+
}, '[allArray, mod] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
|
|
1004
|
+
return false;
|
|
1005
|
+
}
|
|
1006
|
+
if (key) {
|
|
1007
|
+
for (const row of r.rows) {
|
|
1008
|
+
list[row[key]] = row;
|
|
1009
|
+
--remain;
|
|
1010
|
+
}
|
|
1011
|
+
continue;
|
|
1012
|
+
}
|
|
1013
|
+
for (const row of r.rows) {
|
|
1014
|
+
list.push(row);
|
|
1015
|
+
--remain;
|
|
1016
|
+
}
|
|
1017
|
+
continue;
|
|
1018
|
+
}
|
|
1019
|
+
return list;
|
|
1020
|
+
}
|
|
1021
|
+
const contain = this._contain ? lCore.clone(this._contain.list) : null;
|
|
1022
|
+
const r = await this._db.query(this._sql.getSql(), this._sql.getData());
|
|
1023
|
+
if (r.rows === null) {
|
|
1024
|
+
await lCore.log(this._ctr ?? {
|
|
1025
|
+
'path': '',
|
|
1026
|
+
'urlFull': '',
|
|
1027
|
+
'hostname': '',
|
|
1028
|
+
'req': null,
|
|
1029
|
+
'get': {},
|
|
1030
|
+
'cookie': {},
|
|
1031
|
+
'headers': {}
|
|
1032
|
+
}, '[allArray, mod] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
|
|
1033
|
+
return false;
|
|
1034
|
+
}
|
|
1035
|
+
for (const row of r.rows) {
|
|
1036
|
+
if (this._contain && contain) {
|
|
1037
|
+
const io = contain.indexOf(row[this._contain.key]);
|
|
1038
|
+
if (io !== -1) {
|
|
1039
|
+
contain.splice(io, 1);
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
let cr = null;
|
|
1044
|
+
if (this._contain && contain?.length) {
|
|
1045
|
+
const csql = this._sql.copy(undefined, {
|
|
1046
|
+
'where': {
|
|
1047
|
+
[this._contain.key]: this._contain.list
|
|
1048
|
+
}
|
|
1049
|
+
});
|
|
1050
|
+
cr = await this._db.query(csql.getSql(), csql.getData());
|
|
1051
|
+
}
|
|
1052
|
+
if (key) {
|
|
1053
|
+
const list = {};
|
|
1054
|
+
for (const row of r.rows) {
|
|
1055
|
+
list[row[key]] = row;
|
|
1056
|
+
}
|
|
1057
|
+
if (cr?.rows) {
|
|
1058
|
+
for (const crow of cr.rows) {
|
|
1059
|
+
list[crow[key]] = crow;
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
return list;
|
|
1063
|
+
}
|
|
1064
|
+
if (cr?.rows) {
|
|
1065
|
+
for (const crow of cr.rows) {
|
|
1066
|
+
r.rows.push(crow);
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
return r.rows;
|
|
1070
|
+
}
|
|
1071
|
+
async explain(all = false) {
|
|
1072
|
+
const r = await this._db.query('EXPLAIN ' + this._sql.getSql(), this._sql.getData());
|
|
1073
|
+
if (r.rows === null) {
|
|
1074
|
+
await lCore.log(this._ctr ?? {
|
|
1075
|
+
'path': '',
|
|
1076
|
+
'urlFull': '',
|
|
1077
|
+
'hostname': '',
|
|
1078
|
+
'req': null,
|
|
1079
|
+
'get': {},
|
|
1080
|
+
'cookie': {},
|
|
1081
|
+
'headers': {}
|
|
1082
|
+
}, '[explain, mod] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
|
|
1083
|
+
return false;
|
|
1084
|
+
}
|
|
1085
|
+
if (!r.rows[0]) {
|
|
1086
|
+
return false;
|
|
1087
|
+
}
|
|
1088
|
+
if (!all) {
|
|
1089
|
+
return r.rows[0].type;
|
|
1090
|
+
}
|
|
1091
|
+
return r.rows[0];
|
|
1092
|
+
}
|
|
1093
|
+
_formatTotal(sql, f = '*') {
|
|
1094
|
+
sql = sql
|
|
1095
|
+
.replace(/ LIMIT [0-9 ,]+/g, '')
|
|
1096
|
+
.replace(/ ORDER BY [\w`,. ]+(DESC|ASC)?/g, '');
|
|
1097
|
+
if (sql.includes(' GROUP BY ')) {
|
|
1098
|
+
return 'SELECT COUNT(0) AS `count` FROM(' + sql + ') AS `f`';
|
|
1099
|
+
}
|
|
1100
|
+
return sql
|
|
1101
|
+
.replace(/SELECT .+? FROM/g, 'SELECT COUNT(' + this._sql.field(f) + ') AS `count` FROM');
|
|
1102
|
+
}
|
|
1103
|
+
async total(f = '*') {
|
|
1104
|
+
if (this._total.length) {
|
|
1105
|
+
let count = 0;
|
|
1106
|
+
for (const item of this._total) {
|
|
1107
|
+
count += item;
|
|
1108
|
+
}
|
|
1109
|
+
return count;
|
|
1110
|
+
}
|
|
1111
|
+
const sql = this._formatTotal(this._sql.getSql(), f);
|
|
1112
|
+
const r = await this._db.query(sql, this._sql.getData());
|
|
1113
|
+
if (r.rows === null) {
|
|
1114
|
+
await lCore.log(this._ctr ?? {
|
|
1115
|
+
'path': '',
|
|
1116
|
+
'urlFull': '',
|
|
1117
|
+
'hostname': '',
|
|
1118
|
+
'req': null,
|
|
1119
|
+
'get': {},
|
|
1120
|
+
'cookie': {},
|
|
1121
|
+
'headers': {}
|
|
1122
|
+
}, '[total, mod] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
|
|
1123
|
+
return 0;
|
|
1124
|
+
}
|
|
1125
|
+
let count = 0;
|
|
1126
|
+
for (const item of r.rows) {
|
|
1127
|
+
count += item.count;
|
|
1128
|
+
}
|
|
1129
|
+
return count;
|
|
1130
|
+
}
|
|
1131
|
+
async count() {
|
|
1132
|
+
const sql = this._sql.getSql().replace(/SELECT .+? FROM/, 'SELECT COUNT(*) AS `count` FROM');
|
|
1133
|
+
const r = await this._db.query(sql, this._sql.getData());
|
|
1134
|
+
if (r.rows === null) {
|
|
1135
|
+
await lCore.log(this._ctr ?? {
|
|
1136
|
+
'path': '',
|
|
1137
|
+
'urlFull': '',
|
|
1138
|
+
'hostname': '',
|
|
1139
|
+
'req': null,
|
|
1140
|
+
'get': {},
|
|
1141
|
+
'cookie': {},
|
|
1142
|
+
'headers': {}
|
|
1143
|
+
}, '[count, mod] ' + lText.stringifyJson(r.error?.message ?? '').slice(1, -1).replace(/"/g, '""'), '-error');
|
|
1144
|
+
return 0;
|
|
1145
|
+
}
|
|
1146
|
+
let count = 0;
|
|
1147
|
+
for (const item of r.rows) {
|
|
1148
|
+
count += item.count;
|
|
1149
|
+
}
|
|
1150
|
+
return count;
|
|
1151
|
+
}
|
|
1152
|
+
join(f, s = [], type = 'INNER', index = '', pre = '') {
|
|
1153
|
+
this._sql.join(f, s, type, index ? '_' + index : '', pre);
|
|
1154
|
+
return this;
|
|
1155
|
+
}
|
|
1156
|
+
leftJoin(f, s, index = '', pre = '') {
|
|
1157
|
+
this._sql.leftJoin(f, s, index ? '_' + index : '', pre);
|
|
1158
|
+
return this;
|
|
1159
|
+
}
|
|
1160
|
+
rightJoin(f, s, index = '', pre = '') {
|
|
1161
|
+
this._sql.rightJoin(f, s, index ? '_' + index : '', pre);
|
|
1162
|
+
return this;
|
|
1163
|
+
}
|
|
1164
|
+
innerJoin(f, s, index = '', pre = '') {
|
|
1165
|
+
this._sql.innerJoin(f, s, index ? '_' + index : '', pre);
|
|
1166
|
+
return this;
|
|
1167
|
+
}
|
|
1168
|
+
fullJoin(f, s, index = '', pre = '') {
|
|
1169
|
+
this._sql.fullJoin(f, s, index ? '_' + index : '', pre);
|
|
1170
|
+
return this;
|
|
1171
|
+
}
|
|
1172
|
+
crossJoin(f, s, index = '', pre = '') {
|
|
1173
|
+
this._sql.crossJoin(f, s, index ? '_' + index : '', pre);
|
|
1174
|
+
return this;
|
|
1175
|
+
}
|
|
1176
|
+
having(s) {
|
|
1177
|
+
this._sql.having(s);
|
|
1178
|
+
return this;
|
|
1179
|
+
}
|
|
1180
|
+
filter(s, raw = false) {
|
|
1181
|
+
const cstr = this.constructor;
|
|
1182
|
+
if (cstr._soft && !raw) {
|
|
1183
|
+
if (typeof s === 'string') {
|
|
1184
|
+
s = '(' + s + ') AND `time_remove` = 0';
|
|
1185
|
+
}
|
|
1186
|
+
else if (Array.isArray(s)) {
|
|
1187
|
+
s.push({
|
|
1188
|
+
'time_remove': 0
|
|
1189
|
+
});
|
|
1190
|
+
}
|
|
1191
|
+
else {
|
|
1192
|
+
s['time_remove'] = 0;
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
this._sql.where(s);
|
|
1196
|
+
return this;
|
|
1197
|
+
}
|
|
1198
|
+
where(s, raw = false) {
|
|
1199
|
+
return this.filter(s, raw);
|
|
1200
|
+
}
|
|
1201
|
+
by(c, d = 'DESC') {
|
|
1202
|
+
this._sql.by(c, d);
|
|
1203
|
+
return this;
|
|
1204
|
+
}
|
|
1205
|
+
group(c) {
|
|
1206
|
+
this._sql.group(c);
|
|
1207
|
+
return this;
|
|
1208
|
+
}
|
|
1209
|
+
limit(a, b = 0) {
|
|
1210
|
+
this._sql.limit(a, b);
|
|
1211
|
+
this._limit = [a, b];
|
|
1212
|
+
return this;
|
|
1213
|
+
}
|
|
1214
|
+
page(count, page = 1) {
|
|
1215
|
+
const a = count * (page - 1);
|
|
1216
|
+
this._sql.limit(a, count);
|
|
1217
|
+
this._limit = [a, count];
|
|
1218
|
+
return this;
|
|
1219
|
+
}
|
|
1220
|
+
append(sql) {
|
|
1221
|
+
this._sql.append(sql);
|
|
1222
|
+
return this;
|
|
1223
|
+
}
|
|
1224
|
+
contain(contain) {
|
|
1225
|
+
this._contain = contain;
|
|
1226
|
+
return this;
|
|
1227
|
+
}
|
|
1228
|
+
getSql() {
|
|
1229
|
+
return this._sql.getSql();
|
|
1230
|
+
}
|
|
1231
|
+
getData() {
|
|
1232
|
+
return this._sql.getData();
|
|
1233
|
+
}
|
|
1234
|
+
format(sql, data) {
|
|
1235
|
+
return this._sql.format(sql, data);
|
|
1236
|
+
}
|
|
1237
|
+
toArray() {
|
|
1238
|
+
return this._data;
|
|
1239
|
+
}
|
|
1240
|
+
updates() {
|
|
1241
|
+
const updates = {};
|
|
1242
|
+
for (const k in this._updates) {
|
|
1243
|
+
updates[k] = this._data[k];
|
|
1244
|
+
}
|
|
1245
|
+
return updates;
|
|
1246
|
+
}
|
|
1247
|
+
unsaved() {
|
|
1248
|
+
return Object.keys(this._updates).length ? true : false;
|
|
1249
|
+
}
|
|
1250
|
+
langText(col, lang) {
|
|
1251
|
+
const key = `${col}_${lang}`;
|
|
1252
|
+
if (this._data[key]) {
|
|
1253
|
+
return this._data[key];
|
|
1254
|
+
}
|
|
1255
|
+
if (lang !== 'en' && this._data[`${col}_en`]) {
|
|
1256
|
+
return this._data[`${col}_en`];
|
|
1257
|
+
}
|
|
1258
|
+
for (const k in this._data) {
|
|
1259
|
+
if (k.startsWith(`${col}_`) && this._data[k]) {
|
|
1260
|
+
return this._data[k];
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
return '';
|
|
1264
|
+
}
|
|
1265
|
+
_keyGenerator() {
|
|
1266
|
+
return '';
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
Mod._$table = '';
|
|
1270
|
+
Mod._$primary = 'id';
|
|
1271
|
+
Mod._$key = '';
|
|
1272
|
+
Mod._$soft = false;
|
|
1273
|
+
exports.default = Mod;
|