@maiyunnet/kebab 2.0.3 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +14 -0
- package/index.js +1 -1
- package/lib/buffer.d.ts +25 -0
- package/lib/captcha.d.ts +12 -0
- package/lib/consistent.d.ts +20 -0
- package/lib/core.d.ts +85 -0
- package/lib/crypto.d.ts +47 -0
- package/lib/db.d.ts +88 -0
- package/lib/dns.d.ts +75 -0
- package/lib/fs.d.ts +49 -0
- package/lib/jwt.d.ts +30 -0
- package/lib/kv.d.ts +111 -0
- package/lib/lan.d.ts +10 -0
- package/lib/net/formdata.d.ts +23 -0
- package/lib/net/request.d.ts +23 -0
- package/lib/net/response.d.ts +14 -0
- package/lib/net.d.ts +65 -0
- package/lib/s3.d.ts +35 -0
- package/lib/scan.d.ts +31 -0
- package/lib/session.d.ts +22 -0
- package/lib/sql.d.ts +57 -0
- package/lib/ssh/sftp.d.ts +40 -0
- package/lib/ssh/shell.d.ts +13 -0
- package/lib/ssh.d.ts +24 -0
- package/lib/text.d.ts +40 -0
- package/lib/time.d.ts +22 -0
- package/lib/ws.d.ts +87 -0
- package/lib/zip.d.ts +40 -0
- package/lib/zlib.d.ts +25 -0
- package/main.d.ts +1 -0
- package/package.json +1 -1
- package/sys/child.d.ts +1 -0
- package/sys/cmd.d.ts +1 -0
- package/sys/ctr.d.ts +96 -0
- package/sys/master.d.ts +1 -0
- package/sys/mod.d.ts +205 -0
- package/sys/route.d.ts +31 -0
- package/tsconfig.json +1 -1
- package/www/example/ctr/main.d.ts +4 -0
- package/www/example/ctr/middle.d.ts +6 -0
- package/www/example/ctr/test.d.ts +94 -0
- package/www/example/mod/test.d.ts +20 -0
- package/www/example/mod/testdata.d.ts +9 -0
- package/www/example/ws/mproxy.d.ts +4 -0
- package/www/example/ws/rproxy.d.ts +4 -0
- package/www/example/ws/test.d.ts +7 -0
- package/index.ts +0 -33
- package/lib/buffer.ts +0 -152
- package/lib/captcha.ts +0 -63
- package/lib/consistent.ts +0 -219
- package/lib/core.ts +0 -880
- package/lib/crypto.ts +0 -384
- package/lib/db.ts +0 -719
- package/lib/dns.ts +0 -405
- package/lib/fs.ts +0 -527
- package/lib/jwt.ts +0 -276
- package/lib/kv.ts +0 -1489
- package/lib/lan.ts +0 -87
- package/lib/net/formdata.ts +0 -166
- package/lib/net/request.ts +0 -150
- package/lib/net/response.ts +0 -59
- package/lib/net.ts +0 -662
- package/lib/s3.ts +0 -235
- package/lib/scan.ts +0 -364
- package/lib/session.ts +0 -230
- package/lib/sql.ts +0 -1149
- package/lib/ssh/sftp.ts +0 -508
- package/lib/ssh/shell.ts +0 -123
- package/lib/ssh.ts +0 -191
- package/lib/text.ts +0 -626
- package/lib/time.ts +0 -254
- package/lib/ws.ts +0 -523
- package/lib/zip.ts +0 -447
- package/lib/zlib.ts +0 -350
- package/main.ts +0 -27
- package/sys/child.ts +0 -678
- package/sys/cmd.ts +0 -225
- package/sys/ctr.ts +0 -904
- package/sys/master.ts +0 -355
- package/sys/mod.ts +0 -1871
- package/sys/route.ts +0 -1113
- package/www/example/ctr/main.ts +0 -9
- package/www/example/ctr/middle.ts +0 -26
- package/www/example/ctr/test.ts +0 -3218
- package/www/example/mod/test.ts +0 -47
- package/www/example/mod/testdata.ts +0 -30
- package/www/example/ws/mproxy.ts +0 -16
- package/www/example/ws/rproxy.ts +0 -14
- package/www/example/ws/test.ts +0 -36
package/lib/sql.ts
DELETED
|
@@ -1,1149 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Project: Kebab, User: JianSuoQiYue
|
|
3
|
-
* Date: 2019-5-27 20:18:50
|
|
4
|
-
* Last: 2020-3-29 19:37:25, 2022-07-24 22:38:11, 2023-5-24 18:49:18, 2023-6-13 22:20:21, 2023-12-11 13:58:54, 2023-12-14 13:14:40, 2023-12-21 00:04:40, 2024-4-11 19:29:29, 2024-9-2 17:15:28
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import * as lText from '~/lib/text';
|
|
8
|
-
import * as lCore from '~/lib/core';
|
|
9
|
-
// --- 第三方 ---
|
|
10
|
-
import * as mysql2 from 'mysql2/promise';
|
|
11
|
-
// --- 库和定义 ---
|
|
12
|
-
import * as ctr from '~/sys/ctr';
|
|
13
|
-
import * as types from '~/types';
|
|
14
|
-
|
|
15
|
-
/** --- filed 用 token --- */
|
|
16
|
-
let columnToken = '';
|
|
17
|
-
|
|
18
|
-
export class Sql {
|
|
19
|
-
|
|
20
|
-
/** --- 前置 --- */
|
|
21
|
-
private readonly _pre: string = '';
|
|
22
|
-
|
|
23
|
-
/** --- 预拼装 Sql 数组 --- */
|
|
24
|
-
private _sql: string[] = [];
|
|
25
|
-
|
|
26
|
-
/** --- 所有 data 数据 --- */
|
|
27
|
-
private _data: types.DbValue[] = [];
|
|
28
|
-
|
|
29
|
-
// --- 实例化 ---
|
|
30
|
-
public constructor(pre?: string, opt: {
|
|
31
|
-
'data'?: types.DbValue[];
|
|
32
|
-
'sql'?: string[];
|
|
33
|
-
} = {}) {
|
|
34
|
-
this._pre = pre ?? '';
|
|
35
|
-
if (opt.data) {
|
|
36
|
-
this._data = opt.data;
|
|
37
|
-
}
|
|
38
|
-
if (opt.sql) {
|
|
39
|
-
this._sql = opt.sql;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// --- 前导 ---
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* --- 插入数据前导 ---
|
|
47
|
-
* @param table 表名
|
|
48
|
-
*/
|
|
49
|
-
public insert(table: string): this {
|
|
50
|
-
this._data = [];
|
|
51
|
-
const sql = 'INSERT INTO ' + this.field(table, this._pre);
|
|
52
|
-
this._sql = [sql];
|
|
53
|
-
return this;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* --- 替换已经存在的唯一索引数据,不存在则插入 ---
|
|
58
|
-
* @param table 表名
|
|
59
|
-
*/
|
|
60
|
-
public replace(table: string): this {
|
|
61
|
-
this._data = [];
|
|
62
|
-
const sql = 'REPLACE INTO ' + this.field(table, this._pre);
|
|
63
|
-
this._sql = [sql];
|
|
64
|
-
return this;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* --- 实际插入数据的数据 ---
|
|
69
|
-
* @param cs [] 数据列或字段列
|
|
70
|
-
* @param vs [] | [][] 数据
|
|
71
|
-
*/
|
|
72
|
-
public values(
|
|
73
|
-
cs: string[] | Record<string, types.DbValue>,
|
|
74
|
-
vs: types.DbValue[] | types.DbValue[][] = []
|
|
75
|
-
): this {
|
|
76
|
-
let sql = ' (';
|
|
77
|
-
if (Array.isArray(cs)) {
|
|
78
|
-
// --- ['id', 'name'], [['1', 'wow'], ['2', 'oh']] ---
|
|
79
|
-
// --- ['id', 'name'], ['1', 'wow'] ---
|
|
80
|
-
for (const i of cs) {
|
|
81
|
-
sql += this.field(i) + ', ';
|
|
82
|
-
}
|
|
83
|
-
sql = sql.slice(0, -2) + ') VALUES ';
|
|
84
|
-
if (!Array.isArray(vs[0])) {
|
|
85
|
-
vs = [vs];
|
|
86
|
-
}
|
|
87
|
-
// --- INSERT INTO xx (id, name) VALUES (?, ?) ---
|
|
88
|
-
// --- INSERT INTO xx (id, name) VALUES (?, ?), (?, ?) ---
|
|
89
|
-
for (const v of vs as types.DbValue[][]) {
|
|
90
|
-
sql += '(';
|
|
91
|
-
for (const v1 of v) {
|
|
92
|
-
// --- v1 是项目值,如 {'x': 1, 'y': 2}, 'string', 0 ---
|
|
93
|
-
if (v1 === undefined || Number.isNaN(v1)) {
|
|
94
|
-
// --- 异常情况 ---
|
|
95
|
-
lCore.log({
|
|
96
|
-
'path': '',
|
|
97
|
-
'urlFull': '',
|
|
98
|
-
'hostname': '',
|
|
99
|
-
'req': null,
|
|
100
|
-
'get': {},
|
|
101
|
-
'cookie': {},
|
|
102
|
-
'headers': {}
|
|
103
|
-
}, '(sql.values) value error', '-error').catch(() => {
|
|
104
|
-
//
|
|
105
|
-
});
|
|
106
|
-
sql += `'', `;
|
|
107
|
-
}
|
|
108
|
-
else if (v1 === null) {
|
|
109
|
-
sql += 'NULL, ';
|
|
110
|
-
}
|
|
111
|
-
else if (typeof v1 === 'string' || typeof v1 === 'number') {
|
|
112
|
-
sql += '?, ';
|
|
113
|
-
this._data.push(v1);
|
|
114
|
-
}
|
|
115
|
-
else if (Array.isArray(v1)) {
|
|
116
|
-
if (
|
|
117
|
-
v1[0]?.[0]?.x === undefined && typeof v1[0] === 'string' &&
|
|
118
|
-
v1[0].includes('(') && v1[0].includes(')')
|
|
119
|
-
) {
|
|
120
|
-
// --- v1: ['POINT(?)', ['20']] ---
|
|
121
|
-
sql += this.field(v1[0]) + ', ';
|
|
122
|
-
if (v1[1]) {
|
|
123
|
-
this._data.push(...v1[1]);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
else if (v1[0]?.[0]?.y !== undefined) {
|
|
127
|
-
// --- v1: [[{'x': 1, 'y': 2}, { ... }], [{ ... }, { ... }]] ---
|
|
128
|
-
sql += 'ST_POLYGONFROMTEXT(?), ';
|
|
129
|
-
this._data.push(`POLYGON(${v1.map((item) => {
|
|
130
|
-
return `(${item.map((it: Record<string, string | number>) => {
|
|
131
|
-
return `${it.x} ${it.y}`;
|
|
132
|
-
}).join(', ')})`;
|
|
133
|
-
}).join(', ')})`);
|
|
134
|
-
}
|
|
135
|
-
else {
|
|
136
|
-
// --- v1: json ---
|
|
137
|
-
sql += '?, ';
|
|
138
|
-
this._data.push(lText.stringifyJson(v1));
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
else if (v1.x !== undefined) {
|
|
142
|
-
if (v1.y !== undefined) {
|
|
143
|
-
// --- v1: {'x': 1, 'y': 2} ---
|
|
144
|
-
sql += 'ST_POINTFROMTEXT(?), ';
|
|
145
|
-
this._data.push(`POINT(${v1.x} ${v1.y})`);
|
|
146
|
-
}
|
|
147
|
-
else {
|
|
148
|
-
// --- v1: json ---
|
|
149
|
-
sql += '?, ';
|
|
150
|
-
this._data.push(lText.stringifyJson(v1));
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
else if (v1 instanceof Buffer) {
|
|
154
|
-
// --- Buffer ---
|
|
155
|
-
sql += '?, ';
|
|
156
|
-
this._data.push(v);
|
|
157
|
-
}
|
|
158
|
-
else if (this._isField(v1)) {
|
|
159
|
-
// --- 3 ---
|
|
160
|
-
sql += this.field(v1.value) + ', ';
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
// --- json ---
|
|
164
|
-
sql += '?, ';
|
|
165
|
-
this._data.push(lText.stringifyJson(v1));
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
sql = sql.slice(0, -2) + '), ';
|
|
169
|
-
}
|
|
170
|
-
sql = sql.slice(0, -2);
|
|
171
|
-
}
|
|
172
|
-
else {
|
|
173
|
-
// --- {'id': '1', 'name': 'wow'} ---
|
|
174
|
-
// --- INSERT INTO xx (id, name) VALUES (?, ?) ---
|
|
175
|
-
let values = '';
|
|
176
|
-
for (const k in cs) {
|
|
177
|
-
const v = cs[k];
|
|
178
|
-
sql += this.field(k) + ', ';
|
|
179
|
-
if (v === undefined || Number.isNaN(v)) {
|
|
180
|
-
// --- 异常情况 ---
|
|
181
|
-
lCore.log({
|
|
182
|
-
'path': '',
|
|
183
|
-
'urlFull': '',
|
|
184
|
-
'hostname': '',
|
|
185
|
-
'req': null,
|
|
186
|
-
'get': {},
|
|
187
|
-
'cookie': {},
|
|
188
|
-
'headers': {}
|
|
189
|
-
}, '(sql.values) value error', '-error').catch(() => {
|
|
190
|
-
//
|
|
191
|
-
});
|
|
192
|
-
values += `'', `;
|
|
193
|
-
}
|
|
194
|
-
else if (v === null) {
|
|
195
|
-
values += 'NULL, ';
|
|
196
|
-
}
|
|
197
|
-
else if (typeof v === 'string' || typeof v === 'number') {
|
|
198
|
-
values += '?, ';
|
|
199
|
-
this._data.push(v);
|
|
200
|
-
}
|
|
201
|
-
else if (Array.isArray(v)) {
|
|
202
|
-
if (
|
|
203
|
-
v[0]?.[0]?.x === undefined && typeof v[0] === 'string' &&
|
|
204
|
-
v[0].includes('(') && v[0].includes(')')
|
|
205
|
-
) {
|
|
206
|
-
// --- v: ['POINT(?)', ['20']] ---
|
|
207
|
-
values += this.field(v[0]) + ', ';
|
|
208
|
-
if (v[1] !== undefined) {
|
|
209
|
-
this._data.push(...v[1]);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
else if (v[0]?.[0]?.y !== undefined) {
|
|
213
|
-
// --- v: [[{'x': 1, 'y': 2}, { ... }], [{ ... }, { ... }]] ---
|
|
214
|
-
values += 'ST_POLYGONFROMTEXT(?), ';
|
|
215
|
-
this._data.push(`POLYGON(${v.map((item) => {
|
|
216
|
-
return `(${item.map((it: Record<string, string | number>) => {
|
|
217
|
-
return `${it.x} ${it.y}`;
|
|
218
|
-
}).join(', ')})`;
|
|
219
|
-
}).join(', ')})`);
|
|
220
|
-
}
|
|
221
|
-
else {
|
|
222
|
-
// --- v: json ---
|
|
223
|
-
values += '?, ';
|
|
224
|
-
this._data.push(lText.stringifyJson(v));
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
else if (v.x !== undefined) {
|
|
228
|
-
if (v.y !== undefined) {
|
|
229
|
-
// --- v: {'x': 1, 'y': 2} ---
|
|
230
|
-
values += 'ST_POINTFROMTEXT(?), ';
|
|
231
|
-
this._data.push(`POINT(${v.x} ${v.y})`);
|
|
232
|
-
}
|
|
233
|
-
else {
|
|
234
|
-
// --- v: json ---
|
|
235
|
-
values += '?, ';
|
|
236
|
-
this._data.push(lText.stringifyJson(v));
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
else if (v instanceof Buffer) {
|
|
240
|
-
// --- Buffer ---
|
|
241
|
-
values += '?, ';
|
|
242
|
-
this._data.push(v);
|
|
243
|
-
}
|
|
244
|
-
else if (this._isField(v)) {
|
|
245
|
-
// --- 3 ---
|
|
246
|
-
values += this.field(v.value) + ', ';
|
|
247
|
-
}
|
|
248
|
-
else {
|
|
249
|
-
// --- json ---
|
|
250
|
-
values += '?, ';
|
|
251
|
-
this._data.push(lText.stringifyJson(v));
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
sql = sql.slice(0, -2) + ') VALUES (' + values.slice(0, -2) + ')';
|
|
255
|
-
}
|
|
256
|
-
this._sql.push(sql);
|
|
257
|
-
return this;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* --- 不存在则插入,衔接在 insert 之后 ---
|
|
262
|
-
* @param table 表名
|
|
263
|
-
* @param insert {'xx': 'xx', 'xx': 'xx'}
|
|
264
|
-
* @param where [{'xx': 'xx', 'xx': 'xx'}], {'xx': 'xx'}
|
|
265
|
-
*/
|
|
266
|
-
public notExists(
|
|
267
|
-
table: string, insert: Record<string, types.DbValue>,
|
|
268
|
-
where: string | types.Json
|
|
269
|
-
): this {
|
|
270
|
-
let sql = '(';
|
|
271
|
-
const values = [];
|
|
272
|
-
for (const field in insert) {
|
|
273
|
-
const val = insert[field];
|
|
274
|
-
sql += this.field(field) + ', ';
|
|
275
|
-
values.push(val);
|
|
276
|
-
}
|
|
277
|
-
sql = sql.slice(0, -2) + ') SELECT ';
|
|
278
|
-
for (const value of values) {
|
|
279
|
-
if (Array.isArray(value)) {
|
|
280
|
-
sql += value[0] + ', ';
|
|
281
|
-
this._data.push(...value[1]);
|
|
282
|
-
}
|
|
283
|
-
else {
|
|
284
|
-
sql += '?, ';
|
|
285
|
-
this._data.push(value);
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
sql = sql.slice(0, -2) + ' FROM DUAL WHERE NOT EXISTS (SELECT `id` FROM ' + this.field(table, this._pre) + ' WHERE ' + this._whereSub(where) + ')';
|
|
289
|
-
this._sql.push(sql);
|
|
290
|
-
return this;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
/**
|
|
294
|
-
* --- 当不能 insert 时,update(仅能配合 insert 方法用) ---
|
|
295
|
-
* @param s 更新数据
|
|
296
|
-
*/
|
|
297
|
-
public duplicate(s: types.Json): this {
|
|
298
|
-
if (Array.isArray(s) ? s.length : Object.keys(s).length) {
|
|
299
|
-
const sql = ' ON DUPLICATE KEY UPDATE ' + this._updateSub(s);
|
|
300
|
-
this._sql.push(sql);
|
|
301
|
-
}
|
|
302
|
-
return this;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
/**
|
|
306
|
-
* --- '*', 'xx' ---
|
|
307
|
-
* @param c 字段字符串或字段数组
|
|
308
|
-
* @param f 表,允许多张表
|
|
309
|
-
*/
|
|
310
|
-
public select(c: string | Array<string | Array<string | string[]>>, f: string | string[]): this {
|
|
311
|
-
this._data = [];
|
|
312
|
-
let sql = 'SELECT ';
|
|
313
|
-
if (typeof c === 'string') {
|
|
314
|
-
sql += this.field(c);
|
|
315
|
-
}
|
|
316
|
-
else {
|
|
317
|
-
// --- c: ['id', 'name'] ---
|
|
318
|
-
for (const i of c) {
|
|
319
|
-
if (Array.isArray(i)) {
|
|
320
|
-
// --- i: ['xx', ['x']] ---
|
|
321
|
-
sql += this.field(i[0]) + ', ';
|
|
322
|
-
this._data.push(...i[1]);
|
|
323
|
-
}
|
|
324
|
-
else {
|
|
325
|
-
sql += this.field(i) + ', ';
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
sql = sql.slice(0, -2);
|
|
329
|
-
}
|
|
330
|
-
sql += ' FROM ';
|
|
331
|
-
if (typeof f === 'string') {
|
|
332
|
-
sql += this.field(f, this._pre);
|
|
333
|
-
}
|
|
334
|
-
else {
|
|
335
|
-
// --- f: ['user', 'order'] ---
|
|
336
|
-
for (const i of f) {
|
|
337
|
-
sql += this.field(i, this._pre) + ', ';
|
|
338
|
-
}
|
|
339
|
-
sql = sql.slice(0, -2);
|
|
340
|
-
}
|
|
341
|
-
this._sql = [sql];
|
|
342
|
-
return this;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* --- UPDATE SQL 方法 ---
|
|
347
|
-
* @param f 表名
|
|
348
|
-
* @param s 设定 update 的值
|
|
349
|
-
*/
|
|
350
|
-
public update(f: string, s: types.Json): this {
|
|
351
|
-
this._data = [];
|
|
352
|
-
const sql = `UPDATE ${this.field(f, this._pre)} SET ${this._updateSub(s)}`;
|
|
353
|
-
this._sql = [sql];
|
|
354
|
-
return this;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
private _updateSub(s: types.Json): string {
|
|
358
|
-
/*
|
|
359
|
-
[
|
|
360
|
-
['total', '+', '1'], // 1, '1' 可能也是 1 数字类型
|
|
361
|
-
{
|
|
362
|
-
'type': '6', // 2
|
|
363
|
-
'type': column('type2'), // 3
|
|
364
|
-
// 'type': ['type3'], // 4 - 此写法已被禁止,请用 (3) 代替
|
|
365
|
-
'type': ['(CASE `id` WHEN 1 THEN ? WHEN 2 THEN ? END)', ['val1', 'val2']], // 5
|
|
366
|
-
'point': { 'x': 0, 'y': 0 }, // 6
|
|
367
|
-
'polygon': [ [ { 'x': 0, 'y': 0 }, { ... } ], [ ... ] ], // 7
|
|
368
|
-
'json': { 'a': 1, 'b': { 'c': 2 }, 'c': [ { 'c': 2 } ] }, // 8 - 对象类 json,可能为空对象
|
|
369
|
-
'json2': ['abc'] // 9 - 数组类 json,可能为空数组
|
|
370
|
-
}
|
|
371
|
-
]
|
|
372
|
-
*/
|
|
373
|
-
s = aoMix(s);
|
|
374
|
-
let sql = '';
|
|
375
|
-
for (const k in s) {
|
|
376
|
-
const v = s[k];
|
|
377
|
-
if (/^[0-9]+$/.test(k)) {
|
|
378
|
-
// --- 1 ---
|
|
379
|
-
const nv = v[2];
|
|
380
|
-
const isf = this._isField(nv);
|
|
381
|
-
if (isf) {
|
|
382
|
-
if (v[1] === '=') {
|
|
383
|
-
sql += this.field(v[0]) + ' = ' + this.field(nv.value) + ', ';
|
|
384
|
-
}
|
|
385
|
-
else {
|
|
386
|
-
sql += this.field(v[0]) + ' = ' + this.field(v[0]) + ' ' + v[1] + ' ' + this.field(nv.value) + ', ';
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
else {
|
|
390
|
-
if (v[1] === '=') {
|
|
391
|
-
sql += this.field(v[0]) + ' = ?, ';
|
|
392
|
-
}
|
|
393
|
-
else {
|
|
394
|
-
sql += this.field(v[0]) + ' = ' + this.field(v[0]) + ' ' + v[1] + ' ?, ';
|
|
395
|
-
}
|
|
396
|
-
this._data.push(nv);
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
else {
|
|
400
|
-
// --- 2, 3, 4, 5, 6, 7, 8 ---
|
|
401
|
-
sql += this.field(k) + ' = ';
|
|
402
|
-
if (v === undefined || Number.isNaN(v)) {
|
|
403
|
-
// --- 异常情况 ---
|
|
404
|
-
lCore.log({
|
|
405
|
-
'path': '',
|
|
406
|
-
'urlFull': '',
|
|
407
|
-
'hostname': '',
|
|
408
|
-
'req': null,
|
|
409
|
-
'get': {},
|
|
410
|
-
'cookie': {},
|
|
411
|
-
'headers': {}
|
|
412
|
-
}, '(sql._updateSub) value error, key: ' + k, '-error').catch(() => {
|
|
413
|
-
//
|
|
414
|
-
});
|
|
415
|
-
sql += '"", ';
|
|
416
|
-
}
|
|
417
|
-
else if (v === null) {
|
|
418
|
-
sql += 'NULL, ';
|
|
419
|
-
}
|
|
420
|
-
else if (typeof v === 'string' || typeof v === 'number') {
|
|
421
|
-
// --- 2 ---
|
|
422
|
-
sql += '?, ';
|
|
423
|
-
this._data.push(v);
|
|
424
|
-
}
|
|
425
|
-
else if (Array.isArray(v)) {
|
|
426
|
-
if (
|
|
427
|
-
v[0]?.[0]?.x === undefined && typeof v[0] === 'string' &&
|
|
428
|
-
v[0].includes('(') && v[0].includes(')')
|
|
429
|
-
) {
|
|
430
|
-
// --- 4, 5: ['(CASE `id` WHEN 1 THEN ? WHEN 2 THEN ? END)', ['val1', 'val2']] ---
|
|
431
|
-
sql += this.field(v[0]) + ', ';
|
|
432
|
-
if (v[1] !== undefined) {
|
|
433
|
-
this._data.push(...v[1]);
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
else if (v[0]?.[0]?.y !== undefined) {
|
|
437
|
-
// --- 7 ---
|
|
438
|
-
sql += 'ST_POLYGONFROMTEXT(?), ';
|
|
439
|
-
this._data.push(`POLYGON(${v.map((item) => {
|
|
440
|
-
return `(${item.map((it: Record<string, string | number>) => {
|
|
441
|
-
return `${it.x} ${it.y}`;
|
|
442
|
-
}).join(', ')})`;
|
|
443
|
-
}).join(', ')})`);
|
|
444
|
-
}
|
|
445
|
-
else {
|
|
446
|
-
// --- v: json ---
|
|
447
|
-
sql += '?, ';
|
|
448
|
-
this._data.push(lText.stringifyJson(v));
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
else if (v.x !== undefined) {
|
|
452
|
-
if (v.y !== undefined) {
|
|
453
|
-
// --- 6: v: {'x': 1, 'y': 2} ---
|
|
454
|
-
sql += 'ST_POINTFROMTEXT(?), ';
|
|
455
|
-
this._data.push(`POINT(${v.x} ${v.y})`);
|
|
456
|
-
}
|
|
457
|
-
else {
|
|
458
|
-
// --- v: json ---
|
|
459
|
-
if (this._isField(v)) {
|
|
460
|
-
// --- 3 ---
|
|
461
|
-
sql += this.field(v.value) + ', ';
|
|
462
|
-
}
|
|
463
|
-
else {
|
|
464
|
-
// --- 8 ---
|
|
465
|
-
sql += '?, ';
|
|
466
|
-
this._data.push(lText.stringifyJson(v));
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
else if (v instanceof Buffer) {
|
|
471
|
-
// --- Buffer ---
|
|
472
|
-
sql += '?, ';
|
|
473
|
-
this._data.push(v);
|
|
474
|
-
}
|
|
475
|
-
else if (this._isField(v)) {
|
|
476
|
-
// --- 3 ---
|
|
477
|
-
sql += this.field(v.value) + ', ';
|
|
478
|
-
}
|
|
479
|
-
else {
|
|
480
|
-
// --- json ---
|
|
481
|
-
sql += '?, ';
|
|
482
|
-
this._data.push(lText.stringifyJson(v));
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
sql = sql.slice(0, -2);
|
|
487
|
-
return sql;
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
/**
|
|
491
|
-
* --- 'xx' ---
|
|
492
|
-
* @param f 表名
|
|
493
|
-
*/
|
|
494
|
-
public delete(f: string): this {
|
|
495
|
-
this._data = [];
|
|
496
|
-
this._sql = ['DELETE FROM ' + this.field(f, this._pre)];
|
|
497
|
-
return this;
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
/**
|
|
501
|
-
* --- 联查另一个 sql 对象 ---
|
|
502
|
-
* @param sql sql 对象
|
|
503
|
-
* @param type 类型
|
|
504
|
-
*/
|
|
505
|
-
public union(lsql: Sql, type: string = ''): this {
|
|
506
|
-
this._data = this._data.concat(lsql.getData());
|
|
507
|
-
this._sql.push(' UNION ' + (type ? type + ' ' : ''));
|
|
508
|
-
this._sql.push(lsql.getSql());
|
|
509
|
-
return this;
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
/**
|
|
513
|
-
* --- 所有联查另一个 sql 对象 ---
|
|
514
|
-
* @param sql sql 对象
|
|
515
|
-
*/
|
|
516
|
-
public unionAll(lsql: Sql): this {
|
|
517
|
-
return this.union(lsql, 'ALL');
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
/**
|
|
521
|
-
* --- join 方法 ---
|
|
522
|
-
* @param f 表名
|
|
523
|
-
* @param s ON 信息
|
|
524
|
-
* @param type 类型
|
|
525
|
-
* @param suf 表后缀
|
|
526
|
-
* @param pre 表前缀,仅在 join 非默认表前缀时填写
|
|
527
|
-
*/
|
|
528
|
-
public join(f: string, s: types.Json = [], type: string = 'INNER', suf: string = '', pre: string = ''): this {
|
|
529
|
-
let field = this.field(f, pre || this._pre, suf ? ('#' + suf) : '');
|
|
530
|
-
if (pre) {
|
|
531
|
-
// --- 处理不同 pre 的 as 前缀问题 ---
|
|
532
|
-
field = field.replace(new RegExp(`AS \`${pre}(.+?)\``), `AS \`${this._pre}$1\``);
|
|
533
|
-
}
|
|
534
|
-
let sql = ' ' + type + ' JOIN ' + field;
|
|
535
|
-
if (Array.isArray(s) ? s.length : Object.keys(s).length) {
|
|
536
|
-
sql += ' ON ' + this._whereSub(s);
|
|
537
|
-
}
|
|
538
|
-
this._sql.push(sql);
|
|
539
|
-
return this;
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
/**
|
|
543
|
-
* --- left join 方法 ---
|
|
544
|
-
* @param f 表名
|
|
545
|
-
* @param s ON 信息
|
|
546
|
-
* @param suf 表后缀
|
|
547
|
-
* @param pre 表前缀,仅在 join 非默认表前缀时填写
|
|
548
|
-
*/
|
|
549
|
-
public leftJoin(f: string, s: types.Json = [], suf: string = '', pre: string = ''): this {
|
|
550
|
-
return this.join(f, s, 'LEFT', suf, pre);
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
/**
|
|
554
|
-
* --- right join 方法 ---
|
|
555
|
-
* @param f 表名
|
|
556
|
-
* @param s ON 信息
|
|
557
|
-
* @param suf 表后缀
|
|
558
|
-
* @param pre 表前缀,仅在 join 非默认表前缀时填写
|
|
559
|
-
*/
|
|
560
|
-
public rightJoin(f: string, s: types.Json = [], suf: string = '', pre: string = ''): this {
|
|
561
|
-
return this.join(f, s, 'RIGHT', suf, pre);
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
/**
|
|
565
|
-
* --- inner join 方法 ---
|
|
566
|
-
* @param f 表名
|
|
567
|
-
* @param s ON 信息
|
|
568
|
-
* @param suf 表后缀
|
|
569
|
-
* @param pre 表前缀,仅在 join 非默认表前缀时填写
|
|
570
|
-
*/
|
|
571
|
-
public innerJoin(f: string, s: types.Json = [], suf: string = '', pre: string = ''): this {
|
|
572
|
-
return this.join(f, s, 'INNER', suf, pre);
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
/**
|
|
576
|
-
* --- full join 方法 ---
|
|
577
|
-
* @param f 表名
|
|
578
|
-
* @param s ON 信息
|
|
579
|
-
* @param suf 表后缀
|
|
580
|
-
* @param pre 表前缀,仅在 join 非默认表前缀时填写
|
|
581
|
-
*/
|
|
582
|
-
public fullJoin(f: string, s: types.Json = [], suf: string = '', pre: string = ''): this {
|
|
583
|
-
return this.join(f, s, 'FULL', suf, pre);
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
/**
|
|
587
|
-
* --- cross join 方法 ---
|
|
588
|
-
* @param f 表名
|
|
589
|
-
* @param s ON 信息
|
|
590
|
-
* @param suf 表后缀
|
|
591
|
-
* @param pre 表前缀,仅在 join 非默认表前缀时填写
|
|
592
|
-
*/
|
|
593
|
-
public crossJoin(f: string, s: types.Json = [], suf: string = '', pre: string = ''): this {
|
|
594
|
-
return this.join(f, s, 'CROSS', suf, pre);
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
/**
|
|
598
|
-
* --- having 后置筛选器,用法类似 where ---
|
|
599
|
-
*/
|
|
600
|
-
public having(s: string | types.Json = ''): this {
|
|
601
|
-
if (typeof s === 'string') {
|
|
602
|
-
// --- string ---
|
|
603
|
-
if (s !== '') {
|
|
604
|
-
this._sql.push(' HAVING ' + s);
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
else {
|
|
608
|
-
// --- array ---
|
|
609
|
-
if (s.length) {
|
|
610
|
-
const whereSub = this._whereSub(s);
|
|
611
|
-
if (whereSub !== '') {
|
|
612
|
-
this._sql.push(' HAVING ' + whereSub);
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
return this;
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
/** --- where 的 data 的开始处和结束处 --- */
|
|
620
|
-
private _whereDataPosition = [0, 0];
|
|
621
|
-
|
|
622
|
-
/**
|
|
623
|
-
* --- 筛选器 ---
|
|
624
|
-
* --- 1. 'city': 'bj', 'type': '2' ---
|
|
625
|
-
* --- 2. ['type', '>', '1'] ---
|
|
626
|
-
* --- 3. ['type', 'in', ['1', '2']] ---
|
|
627
|
-
* --- 4. 'type': ['1', '2'] ---
|
|
628
|
-
* --- 5. '$or': [{'city': 'bj'}, {'city': 'sh'}, [['age', '>', '10']]], 'type': '2' ---
|
|
629
|
-
* --- 6. 'city_in': column('city_out') ---
|
|
630
|
-
* --- 7. ['JSON_CONTAINS(`uid`, ?)', ['hello']] ---
|
|
631
|
-
* @param s 筛选数据
|
|
632
|
-
*/
|
|
633
|
-
public where(s: string | types.Json): this {
|
|
634
|
-
this._whereDataPosition[0] = this._data.length;
|
|
635
|
-
if (typeof s === 'string') {
|
|
636
|
-
// --- string ---
|
|
637
|
-
if (s !== '') {
|
|
638
|
-
this._sql.push(' WHERE ' + s);
|
|
639
|
-
this._whereDataPosition[1] = this._data.length;
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
else {
|
|
643
|
-
// --- array ---
|
|
644
|
-
let go: boolean = false;
|
|
645
|
-
if (Array.isArray(s)) {
|
|
646
|
-
if (s.length) {
|
|
647
|
-
go = true;
|
|
648
|
-
}
|
|
649
|
-
}
|
|
650
|
-
else {
|
|
651
|
-
if (Object.keys(s).length) {
|
|
652
|
-
go = true;
|
|
653
|
-
}
|
|
654
|
-
}
|
|
655
|
-
if (go) {
|
|
656
|
-
const whereSub = this._whereSub(s);
|
|
657
|
-
if (whereSub !== '') {
|
|
658
|
-
this._sql.push(' WHERE ' + whereSub);
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
this._whereDataPosition[1] = this._data.length;
|
|
662
|
-
}
|
|
663
|
-
return this;
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
private _whereSub(s: types.Json, data?: any[]): string {
|
|
667
|
-
data ??= this._data;
|
|
668
|
-
s = aoMix(s);
|
|
669
|
-
let sql = '';
|
|
670
|
-
for (const k in s) {
|
|
671
|
-
const v = s[k];
|
|
672
|
-
if (/^[0-9]+$/.test(k)) {
|
|
673
|
-
// --- 2, 3, 7 ---
|
|
674
|
-
if (v[2] === undefined) {
|
|
675
|
-
// --- 7 ---
|
|
676
|
-
sql += this.field(v[0]) + ' AND ';
|
|
677
|
-
if (v[1] !== undefined) {
|
|
678
|
-
data.push(...v[1]);
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
else if (v[2] === null) {
|
|
682
|
-
// --- 3: null ---
|
|
683
|
-
let opera = v[1];
|
|
684
|
-
if (opera === '!=' || opera === '!==' || opera === '<>') {
|
|
685
|
-
opera = 'IS NOT';
|
|
686
|
-
}
|
|
687
|
-
else if (opera === '=' || opera === '==' || opera === '===') {
|
|
688
|
-
opera = 'IS';
|
|
689
|
-
}
|
|
690
|
-
else {
|
|
691
|
-
opera = opera.toUpperCase();
|
|
692
|
-
}
|
|
693
|
-
sql += this.field(v[0]) + ' ' + opera + ' NULL AND ';
|
|
694
|
-
}
|
|
695
|
-
else if (Array.isArray(v[2])) {
|
|
696
|
-
// --- 3 ---
|
|
697
|
-
sql += this.field(v[0]) + ' ' + v[1].toUpperCase() + ' (';
|
|
698
|
-
for (const v1 of v[2]) {
|
|
699
|
-
sql += '?, ';
|
|
700
|
-
data.push(v1);
|
|
701
|
-
}
|
|
702
|
-
sql = sql.slice(0, -2) + ') AND ';
|
|
703
|
-
}
|
|
704
|
-
else {
|
|
705
|
-
// --- 2, 6 ---
|
|
706
|
-
const nv = v[2];
|
|
707
|
-
const isf = this._isField(nv);
|
|
708
|
-
if (isf) {
|
|
709
|
-
// --- 6. field ---
|
|
710
|
-
sql += this.field(v[0]) + ' ' + v[1] + ' ' + this.field(nv.value) + ' AND ';
|
|
711
|
-
}
|
|
712
|
-
else {
|
|
713
|
-
sql += this.field(v[0]) + ' ' + v[1] + ' ? AND ';
|
|
714
|
-
data.push(nv);
|
|
715
|
-
}
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
else {
|
|
719
|
-
// --- 1, 4, 5, 6 ---
|
|
720
|
-
if (k.startsWith('$')) {
|
|
721
|
-
// --- 5 - '$or': [{'city': 'bj'}, {'city': 'sh'}] ---
|
|
722
|
-
const sp = ' ' + k.slice(1).split('-')[0].toUpperCase() + ' ';
|
|
723
|
-
sql += '(';
|
|
724
|
-
for (let v1 of v) {
|
|
725
|
-
// --- v1 是 {'city': 'bj'} ---
|
|
726
|
-
v1 = aoMix(v1);
|
|
727
|
-
if (Object.keys(v1).length > 1) {
|
|
728
|
-
sql += '(' + this._whereSub(v1, data) + ')' + sp;
|
|
729
|
-
}
|
|
730
|
-
else {
|
|
731
|
-
sql += this._whereSub(v1, data) + sp;
|
|
732
|
-
}
|
|
733
|
-
}
|
|
734
|
-
sql = sql.slice(0, -sp.length) + ') AND ';
|
|
735
|
-
}
|
|
736
|
-
else {
|
|
737
|
-
// --- 1, 4, 6 ---
|
|
738
|
-
if (v === null) {
|
|
739
|
-
sql += this.field(k) + ' IS NULL AND ';
|
|
740
|
-
}
|
|
741
|
-
else if (typeof v === 'string' || typeof v === 'number') {
|
|
742
|
-
// --- 1 ---
|
|
743
|
-
sql += this.field(k) + ' = ? AND ';
|
|
744
|
-
data.push(v);
|
|
745
|
-
}
|
|
746
|
-
else if (this._isField(v)) {
|
|
747
|
-
// --- 6 ---
|
|
748
|
-
sql += this.field(k) + ' = ' + this.field(v.value) + ' AND ';
|
|
749
|
-
}
|
|
750
|
-
else {
|
|
751
|
-
// --- 4 - 'type': ['1', '2'] ---
|
|
752
|
-
if (v.length > 0) {
|
|
753
|
-
sql += this.field(k) + ' IN (';
|
|
754
|
-
for (const v1 of v) {
|
|
755
|
-
sql += '?, ';
|
|
756
|
-
data.push(v1);
|
|
757
|
-
}
|
|
758
|
-
sql = sql.slice(0, -2) + ') AND ';
|
|
759
|
-
}
|
|
760
|
-
else {
|
|
761
|
-
sql += this.field(k) + ' IN (NULL) AND ';
|
|
762
|
-
}
|
|
763
|
-
}
|
|
764
|
-
}
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
if (sql === '') {
|
|
768
|
-
return '';
|
|
769
|
-
}
|
|
770
|
-
return sql.slice(0, -5);
|
|
771
|
-
}
|
|
772
|
-
|
|
773
|
-
/**
|
|
774
|
-
* --- ORDER BY ---
|
|
775
|
-
* @param c 字段字符串或数组
|
|
776
|
-
* @param d 排序规则
|
|
777
|
-
*/
|
|
778
|
-
public by(c: string | Array<string | string[]>, d: 'DESC' | 'ASC' = 'DESC'): this {
|
|
779
|
-
let sql: string = ' ORDER BY ';
|
|
780
|
-
if (typeof c === 'string') {
|
|
781
|
-
sql += this.field(c) + ' ' + d;
|
|
782
|
-
}
|
|
783
|
-
else {
|
|
784
|
-
for (const v of c) {
|
|
785
|
-
if (typeof v === 'string' || typeof v === 'number') {
|
|
786
|
-
sql += this.field(v) + ' ' + d + ', ';
|
|
787
|
-
}
|
|
788
|
-
else {
|
|
789
|
-
sql += this.field(v[0]) + ' ' + v[1] + ', ';
|
|
790
|
-
}
|
|
791
|
-
}
|
|
792
|
-
sql = sql.slice(0, -2);
|
|
793
|
-
}
|
|
794
|
-
this._sql.push(sql);
|
|
795
|
-
return this;
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
/**
|
|
799
|
-
* --- GROUP BY ---
|
|
800
|
-
* @param c 字段字符串或数组
|
|
801
|
-
*/
|
|
802
|
-
public group(c: string | string[]): this {
|
|
803
|
-
let sql = ' GROUP BY ';
|
|
804
|
-
if (typeof c === 'string') {
|
|
805
|
-
sql += this.field(c);
|
|
806
|
-
}
|
|
807
|
-
else {
|
|
808
|
-
for (const v of c) {
|
|
809
|
-
sql += this.field(v) + ', ';
|
|
810
|
-
}
|
|
811
|
-
sql = sql.slice(0, -2);
|
|
812
|
-
}
|
|
813
|
-
this._sql.push(sql);
|
|
814
|
-
return this;
|
|
815
|
-
}
|
|
816
|
-
|
|
817
|
-
/**
|
|
818
|
-
* --- LIMIT ---
|
|
819
|
-
* @param a 起始
|
|
820
|
-
* @param b 长度
|
|
821
|
-
*/
|
|
822
|
-
public limit(a: number, b: number = 0): this {
|
|
823
|
-
this._sql.push(' LIMIT ' + a.toString() + (b > 0 ? ', ' + b.toString() : ''));
|
|
824
|
-
return this;
|
|
825
|
-
}
|
|
826
|
-
|
|
827
|
-
/**
|
|
828
|
-
* --- 追加消极锁,通常不建议使用 ---
|
|
829
|
-
*/
|
|
830
|
-
public lock(): this {
|
|
831
|
-
this._sql.push(' FOR UPDATE');
|
|
832
|
-
return this;
|
|
833
|
-
}
|
|
834
|
-
|
|
835
|
-
/**
|
|
836
|
-
* --- 创建一个本对象的一个新的 sql 对象拷贝 ---
|
|
837
|
-
* @param f 可为空,可设置新对象的 table 名变化
|
|
838
|
-
*/
|
|
839
|
-
public copy(f?: string | string[], opt: {
|
|
840
|
-
'where'?: string | types.Json;
|
|
841
|
-
} = {}): Sql {
|
|
842
|
-
const sql: string[] = lCore.clone(this._sql);
|
|
843
|
-
const data: any[] = lCore.clone(this._data);
|
|
844
|
-
if (opt.where !== undefined) {
|
|
845
|
-
if (typeof opt.where === 'string') {
|
|
846
|
-
// --- string ---
|
|
847
|
-
for (let i = 0; i < sql.length; ++i) {
|
|
848
|
-
if (!sql[i].startsWith(' WHERE ')) {
|
|
849
|
-
continue;
|
|
850
|
-
}
|
|
851
|
-
sql[i] = opt.where ? (' WHERE ' + opt.where) : '';
|
|
852
|
-
data.splice(
|
|
853
|
-
this._whereDataPosition[0],
|
|
854
|
-
this._whereDataPosition[1] - this._whereDataPosition[0]
|
|
855
|
-
);
|
|
856
|
-
break;
|
|
857
|
-
}
|
|
858
|
-
}
|
|
859
|
-
else {
|
|
860
|
-
// --- array ---
|
|
861
|
-
let go: boolean = false;
|
|
862
|
-
if (Array.isArray(opt.where)) {
|
|
863
|
-
if (opt.where.length) {
|
|
864
|
-
go = true;
|
|
865
|
-
}
|
|
866
|
-
}
|
|
867
|
-
else {
|
|
868
|
-
if (Object.keys(opt.where).length) {
|
|
869
|
-
go = true;
|
|
870
|
-
}
|
|
871
|
-
}
|
|
872
|
-
// --- 找到原来的 where ---
|
|
873
|
-
for (let i = 0; i < sql.length; ++i) {
|
|
874
|
-
if (!sql[i].startsWith(' WHERE ')) {
|
|
875
|
-
continue;
|
|
876
|
-
}
|
|
877
|
-
if (go) {
|
|
878
|
-
// --- 修改 where ---
|
|
879
|
-
const d: any[] = [];
|
|
880
|
-
sql[i] = ' WHERE ' + this._whereSub(opt.where, d);
|
|
881
|
-
data.splice(
|
|
882
|
-
this._whereDataPosition[0],
|
|
883
|
-
this._whereDataPosition[1] - this._whereDataPosition[0],
|
|
884
|
-
...d
|
|
885
|
-
);
|
|
886
|
-
}
|
|
887
|
-
else {
|
|
888
|
-
// --- 清除 where ---
|
|
889
|
-
sql.splice(i, 1);
|
|
890
|
-
data.splice(
|
|
891
|
-
this._whereDataPosition[0],
|
|
892
|
-
this._whereDataPosition[1] - this._whereDataPosition[0]
|
|
893
|
-
);
|
|
894
|
-
}
|
|
895
|
-
break;
|
|
896
|
-
}
|
|
897
|
-
}
|
|
898
|
-
}
|
|
899
|
-
// --- 替换表名 ---
|
|
900
|
-
if (f && sql[0]) {
|
|
901
|
-
let table = '';
|
|
902
|
-
if (typeof f === 'string') {
|
|
903
|
-
table = this.field(f, this._pre);
|
|
904
|
-
}
|
|
905
|
-
else {
|
|
906
|
-
// --- f: ['user', 'order'] ---
|
|
907
|
-
for (const i of f) {
|
|
908
|
-
table += this.field(i, this._pre) + ', ';
|
|
909
|
-
}
|
|
910
|
-
table = table.slice(0, -2);
|
|
911
|
-
}
|
|
912
|
-
sql[0] = sql[0].replace(/FROM [`\w, ]+/, 'FROM ' + table);
|
|
913
|
-
}
|
|
914
|
-
return get(this.getPre(), {
|
|
915
|
-
'data': data,
|
|
916
|
-
'sql': sql
|
|
917
|
-
});
|
|
918
|
-
}
|
|
919
|
-
|
|
920
|
-
// --- 操作 ---
|
|
921
|
-
|
|
922
|
-
/**
|
|
923
|
-
* --- 获取 sql 语句 ---
|
|
924
|
-
*/
|
|
925
|
-
public getSql(): string {
|
|
926
|
-
return this._sql.join('');
|
|
927
|
-
}
|
|
928
|
-
|
|
929
|
-
/**
|
|
930
|
-
* --- 获取全部 data ---
|
|
931
|
-
*/
|
|
932
|
-
public getData(): types.DbValue[] {
|
|
933
|
-
return this._data;
|
|
934
|
-
}
|
|
935
|
-
|
|
936
|
-
/**
|
|
937
|
-
* --- 获取定义的 pre ---
|
|
938
|
-
*/
|
|
939
|
-
public getPre(): string {
|
|
940
|
-
return this._pre;
|
|
941
|
-
}
|
|
942
|
-
|
|
943
|
-
/**
|
|
944
|
-
* --- 获取带 data 的 sql 语句 ---
|
|
945
|
-
* @param sql
|
|
946
|
-
* @param data
|
|
947
|
-
*/
|
|
948
|
-
public format(sql?: string, data?: types.DbValue[]): string {
|
|
949
|
-
return mysql2.format(sql ?? this.getSql(), data ?? this.getData());
|
|
950
|
-
}
|
|
951
|
-
|
|
952
|
-
// --- 特殊方法 ---
|
|
953
|
-
|
|
954
|
-
/**
|
|
955
|
-
* --- 在 sql 最后追加字符串 ---
|
|
956
|
-
* @param sql
|
|
957
|
-
*/
|
|
958
|
-
public append(sql: string): this {
|
|
959
|
-
this._sql.push(sql);
|
|
960
|
-
return this;
|
|
961
|
-
}
|
|
962
|
-
|
|
963
|
-
/**
|
|
964
|
-
* --- 对字段进行包裹 ---
|
|
965
|
-
* @param str
|
|
966
|
-
* @param pre 表前缀,仅请在 field 表名时倒入前缀
|
|
967
|
-
* @param suf 表后缀,仅请在 field 表名时倒入后缀,前面加 # 代表要强制 AS
|
|
968
|
-
*/
|
|
969
|
-
public field(str: string | number | Array<string | string[]>, pre: string = '', suf: string = ''): string {
|
|
970
|
-
let left: string = '';
|
|
971
|
-
let right: string = '';
|
|
972
|
-
if (Array.isArray(str)) {
|
|
973
|
-
this._data.push(...str[1]);
|
|
974
|
-
return this.field(str[0]);
|
|
975
|
-
}
|
|
976
|
-
if (typeof str === 'number') {
|
|
977
|
-
str = str.toString();
|
|
978
|
-
}
|
|
979
|
-
str = str.trim(); // --- 去除前导尾随 ---
|
|
980
|
-
str = str.replace(/ {2,}/g, ' '); // --- 去除多余的空格 ---
|
|
981
|
-
str = str.replace(/ +([),])/g, ' $1');
|
|
982
|
-
str = str.replace(/([(,]) +/g, '$1 ');
|
|
983
|
-
str = str.replace(/(\W)(JOIN|WHERE|UNION)(\W)/ig, '$1$3');
|
|
984
|
-
// --- 先判断 suf 强制性 AS ---
|
|
985
|
-
let sufAs = false;
|
|
986
|
-
if (suf.startsWith('#')) {
|
|
987
|
-
// --- 强制 AS ---
|
|
988
|
-
suf = suf.slice(1);
|
|
989
|
-
sufAs = true;
|
|
990
|
-
}
|
|
991
|
-
// --- 先判断有没有别名(也就是 as) ---
|
|
992
|
-
const loStr = str.toLowerCase();
|
|
993
|
-
const asPos = loStr.indexOf(' as ');
|
|
994
|
-
if (asPos === -1) {
|
|
995
|
-
// --- 没有 as ---
|
|
996
|
-
let spacePos = str.lastIndexOf(' ');
|
|
997
|
-
// --- 有可能有 aa + bb + cc + 10 这种情况 ---
|
|
998
|
-
if (!/^[a-zA-Z_)]$/.test(str[spacePos - 1])) {
|
|
999
|
-
// --- 连接符 ---
|
|
1000
|
-
spacePos = -1;
|
|
1001
|
-
}
|
|
1002
|
-
if (spacePos !== -1) {
|
|
1003
|
-
const spaceRight = str.slice(spacePos + 1);
|
|
1004
|
-
if (/^[a-zA-Z_`][\w`]*$/.test(spaceRight)) {
|
|
1005
|
-
// --- OK ---
|
|
1006
|
-
left = str.slice(0, spacePos);
|
|
1007
|
-
right = spaceRight;
|
|
1008
|
-
}
|
|
1009
|
-
else {
|
|
1010
|
-
left = str;
|
|
1011
|
-
right = '';
|
|
1012
|
-
}
|
|
1013
|
-
}
|
|
1014
|
-
else {
|
|
1015
|
-
left = str;
|
|
1016
|
-
right = '';
|
|
1017
|
-
}
|
|
1018
|
-
}
|
|
1019
|
-
else {
|
|
1020
|
-
// --- 有 as ---
|
|
1021
|
-
left = str.slice(0, asPos);
|
|
1022
|
-
right = str.slice(asPos + 4);
|
|
1023
|
-
}
|
|
1024
|
-
if (right) {
|
|
1025
|
-
// --- 处理右侧 ---
|
|
1026
|
-
if (right.startsWith('`')) {
|
|
1027
|
-
right = '`' + pre + right.slice(1);
|
|
1028
|
-
}
|
|
1029
|
-
else {
|
|
1030
|
-
right = '`' + pre + right + '`';
|
|
1031
|
-
}
|
|
1032
|
-
right = ' AS ' + right;
|
|
1033
|
-
}
|
|
1034
|
-
else {
|
|
1035
|
-
// --- 没有右侧 ---
|
|
1036
|
-
if (sufAs) {
|
|
1037
|
-
// --- 强制 AS ---
|
|
1038
|
-
right = ' AS ' + this.field(left, pre);
|
|
1039
|
-
}
|
|
1040
|
-
}
|
|
1041
|
-
// --- 处理 left ---
|
|
1042
|
-
if (/^[\w`_.*]+$/.test(left)) {
|
|
1043
|
-
const l = left.split('.');
|
|
1044
|
-
if (l[0] === '*') {
|
|
1045
|
-
return '*' + right;
|
|
1046
|
-
}
|
|
1047
|
-
if (l[0].startsWith('`')) {
|
|
1048
|
-
l[0] = l[0].replace(/`/g, '');
|
|
1049
|
-
}
|
|
1050
|
-
if (l[1] === undefined) {
|
|
1051
|
-
// --- xxx ---
|
|
1052
|
-
if (/^[A-Z0-9_]+$/.test(l[0])) {
|
|
1053
|
-
// --- 纯大写是内置函数,不能加 ` ---
|
|
1054
|
-
return l[0] + right;
|
|
1055
|
-
}
|
|
1056
|
-
return '`' + pre + l[0] + suf + '`' + right;
|
|
1057
|
-
}
|
|
1058
|
-
// --- x.xxx ---
|
|
1059
|
-
// --- 只有在此模式才知道 . 前面的一定是表名,因此自动加 sql 级的 _pre ---
|
|
1060
|
-
const w = l[1] === '*' ? '*' : (l[1].startsWith('`') ? l[1] : ('`' + l[1] + '`'));
|
|
1061
|
-
return '`' + this._pre + l[0] + suf + '`.' + w + right;
|
|
1062
|
-
}
|
|
1063
|
-
else {
|
|
1064
|
-
// return left.replace(/([(, ])([a-zA-Z`_][\w`_.]*)(?=[), ])/g, (
|
|
1065
|
-
return left.replace(/(^|[(, ])([a-zA-Z`_][\w`_.]*)(?=[), ]|$)/g, (
|
|
1066
|
-
t: string, t1: string, t2: string
|
|
1067
|
-
): string => {
|
|
1068
|
-
return t1 + this.field(t2, pre, suf);
|
|
1069
|
-
}) + right;
|
|
1070
|
-
}
|
|
1071
|
-
}
|
|
1072
|
-
|
|
1073
|
-
/**
|
|
1074
|
-
* --- 判断传入值是否是 field,还是别的对象 ---
|
|
1075
|
-
* @param str
|
|
1076
|
-
*/
|
|
1077
|
-
private _isField(arg: any): arg is {
|
|
1078
|
-
'type': 'column';
|
|
1079
|
-
'token': string;
|
|
1080
|
-
'value': string;
|
|
1081
|
-
} {
|
|
1082
|
-
if (arg.type !== 'column' || arg.token !== columnToken || arg.value === undefined) {
|
|
1083
|
-
return false;
|
|
1084
|
-
}
|
|
1085
|
-
return true;
|
|
1086
|
-
}
|
|
1087
|
-
|
|
1088
|
-
}
|
|
1089
|
-
|
|
1090
|
-
/**
|
|
1091
|
-
* --- 创建 sql 对象 ---
|
|
1092
|
-
* @param ctrPre ctr 对象或 pre 表前缀
|
|
1093
|
-
* @param opt 参数
|
|
1094
|
-
*/
|
|
1095
|
-
export function get(ctrPre?: ctr.Ctr | string, opt: {
|
|
1096
|
-
'data'?: types.DbValue[];
|
|
1097
|
-
'sql'?: string[];
|
|
1098
|
-
} = {}): Sql {
|
|
1099
|
-
return new Sql(ctrPre instanceof ctr.Ctr ? ctrPre.getPrototype('_config').sql.pre : ctrPre, opt);
|
|
1100
|
-
}
|
|
1101
|
-
|
|
1102
|
-
/**
|
|
1103
|
-
* --- 返回代入后的完整 SQL 字符串 ---
|
|
1104
|
-
* @param sql SQL 字符串
|
|
1105
|
-
* @param data DATA 数据
|
|
1106
|
-
*/
|
|
1107
|
-
export function format(sql: string, data: types.DbValue[]): string {
|
|
1108
|
-
return mysql2.format(sql, data);
|
|
1109
|
-
}
|
|
1110
|
-
|
|
1111
|
-
/**
|
|
1112
|
-
* --- 将数组兑换为组合的对象(Array/Object mix) ---
|
|
1113
|
-
* @param arr 要转换的数组
|
|
1114
|
-
*/
|
|
1115
|
-
export function aoMix(arr: types.Json): Record<string, string | number | types.Json> {
|
|
1116
|
-
if (!Array.isArray(arr)) {
|
|
1117
|
-
return arr;
|
|
1118
|
-
}
|
|
1119
|
-
const mix: Record<string, string | number | types.Json> = {};
|
|
1120
|
-
let i: number = 0;
|
|
1121
|
-
for (const v of arr) {
|
|
1122
|
-
if (Array.isArray(v)) {
|
|
1123
|
-
mix[i] = v;
|
|
1124
|
-
++i;
|
|
1125
|
-
}
|
|
1126
|
-
else {
|
|
1127
|
-
for (const k in v) {
|
|
1128
|
-
mix[k] = v[k];
|
|
1129
|
-
}
|
|
1130
|
-
}
|
|
1131
|
-
}
|
|
1132
|
-
return mix;
|
|
1133
|
-
}
|
|
1134
|
-
|
|
1135
|
-
/** --- 创建字段对象 --- */
|
|
1136
|
-
export function column(field: string): {
|
|
1137
|
-
'type': 'column';
|
|
1138
|
-
'token': string;
|
|
1139
|
-
'value': string;
|
|
1140
|
-
} {
|
|
1141
|
-
if (!columnToken) {
|
|
1142
|
-
columnToken = lCore.random(8, lCore.RANDOM_LUNS);
|
|
1143
|
-
}
|
|
1144
|
-
return {
|
|
1145
|
-
'token': columnToken,
|
|
1146
|
-
'type': 'column',
|
|
1147
|
-
'value': field
|
|
1148
|
-
};
|
|
1149
|
-
}
|