@maiyunnet/kebab 6.2.1 → 7.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/index.d.ts +1 -1
- package/index.js +1 -1
- package/lib/scan.d.ts +5 -3
- package/lib/scan.js +19 -7
- package/lib/session.js +5 -2
- package/lib/sql.d.ts +10 -5
- package/lib/sql.js +62 -91
- package/package.json +1 -1
- package/sys/cmd.js +1 -1
- package/sys/mod.js +27 -9
- package/www/example/ctr/test.js +15 -10
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* --- 本文件用来定义每个目录实体地址的常量 ---
|
|
7
7
|
*/
|
|
8
8
|
/** --- 当前系统版本号 --- */
|
|
9
|
-
export const VER = '
|
|
9
|
+
export const VER = '7.0.0';
|
|
10
10
|
// --- 服务端用的路径 ---
|
|
11
11
|
const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
|
|
12
12
|
/** --- /xxx/xxx --- */
|
package/lib/scan.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Project: Kebab, User: JianSuoQiYue
|
|
3
3
|
* Date: 2022-09-24 15:23:25
|
|
4
|
-
* Last: 2022-09-24 15:23:25, 2022-9-26 12:37:01, 2022-12-29 00:11:16, 2025-11-6 16:32:05
|
|
4
|
+
* Last: 2022-09-24 15:23:25, 2022-9-26 12:37:01, 2022-12-29 00:11:16, 2025-11-6 16:32:05, 2025-12-3 11:05:38
|
|
5
5
|
*/
|
|
6
6
|
import * as lDb from '#kebab/lib/db.js';
|
|
7
7
|
import * as lKv from '#kebab/lib/kv.js';
|
|
@@ -9,12 +9,14 @@ import * as sCtr from '#kebab/sys/ctr.js';
|
|
|
9
9
|
/** --- Scan 设置的选项 --- */
|
|
10
10
|
export interface IOptions {
|
|
11
11
|
'ttl'?: number;
|
|
12
|
-
'
|
|
12
|
+
'ctr'?: sCtr.Ctr;
|
|
13
|
+
'pre'?: string;
|
|
13
14
|
'name'?: string;
|
|
14
15
|
}
|
|
15
16
|
/** --- scanned 函数的选项 --- */
|
|
16
17
|
export interface IStaticOptions {
|
|
17
|
-
'
|
|
18
|
+
'ctr'?: sCtr.Ctr;
|
|
19
|
+
'pre'?: string;
|
|
18
20
|
'name'?: string;
|
|
19
21
|
}
|
|
20
22
|
export declare class Scan {
|
package/lib/scan.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Project: Kebab, User: JianSuoQiYue
|
|
3
3
|
* Date: 2022-09-24 15:23:25
|
|
4
|
-
* Last: 2022-09-24 15:23:25, 2022-9-26 12:37:01, 2022-12-29 00:11:16, 2025-11-6 16:32:05
|
|
4
|
+
* Last: 2022-09-24 15:23:25, 2022-9-26 12:37:01, 2022-12-29 00:11:16, 2025-11-6 16:32:05, 2025-12-3 11:05:38
|
|
5
5
|
*/
|
|
6
6
|
/*
|
|
7
7
|
CREATE TABLE IF NOT EXISTS `scan` (
|
|
@@ -41,7 +41,11 @@ export class Scan {
|
|
|
41
41
|
}
|
|
42
42
|
this._link = link;
|
|
43
43
|
if (link instanceof lDb.Pool) {
|
|
44
|
-
this._sql = lSql.get(
|
|
44
|
+
this._sql = lSql.get({
|
|
45
|
+
'service': link.getService(),
|
|
46
|
+
'ctr': opt.ctr,
|
|
47
|
+
'pre': opt.pre,
|
|
48
|
+
});
|
|
45
49
|
}
|
|
46
50
|
if (token) {
|
|
47
51
|
this._token = token;
|
|
@@ -227,15 +231,19 @@ export async function scanned(link, token, opt = {}) {
|
|
|
227
231
|
const name = opt.name ?? 'scan';
|
|
228
232
|
if (link instanceof lDb.Pool) {
|
|
229
233
|
// --- Db ---
|
|
230
|
-
const sql = lSql.get(
|
|
234
|
+
const sql = lSql.get({
|
|
235
|
+
'service': link.getService(),
|
|
236
|
+
'ctr': opt.ctr,
|
|
237
|
+
'pre': opt.pre,
|
|
238
|
+
});
|
|
231
239
|
sql.update(name, {
|
|
232
|
-
'time_update': time
|
|
240
|
+
'time_update': time,
|
|
233
241
|
}).where([
|
|
234
242
|
{
|
|
235
243
|
'token': token,
|
|
236
244
|
'time_update': '0'
|
|
237
245
|
},
|
|
238
|
-
['time_exp', '>', time]
|
|
246
|
+
['time_exp', '>', time],
|
|
239
247
|
]);
|
|
240
248
|
const r = await link.execute(sql.getSql(), sql.getData());
|
|
241
249
|
if (r.error) {
|
|
@@ -281,7 +289,11 @@ export async function setData(link, token, data, opt = {}) {
|
|
|
281
289
|
const name = opt.name ?? 'scan';
|
|
282
290
|
if (link instanceof lDb.Pool) {
|
|
283
291
|
// --- Db ---
|
|
284
|
-
const sql = lSql.get(
|
|
292
|
+
const sql = lSql.get({
|
|
293
|
+
'service': link.getService(),
|
|
294
|
+
'ctr': opt.ctr,
|
|
295
|
+
'pre': opt.pre,
|
|
296
|
+
});
|
|
285
297
|
sql.update(name, {
|
|
286
298
|
'data': lText.stringifyJson(data)
|
|
287
299
|
}).where([
|
|
@@ -289,7 +301,7 @@ export async function setData(link, token, data, opt = {}) {
|
|
|
289
301
|
'token': token,
|
|
290
302
|
},
|
|
291
303
|
['time_update', '>', '0'],
|
|
292
|
-
['time_exp', '>', time]
|
|
304
|
+
['time_exp', '>', time],
|
|
293
305
|
]);
|
|
294
306
|
const r = await link.execute(sql.getSql(), sql.getData());
|
|
295
307
|
if (r.error) {
|
package/lib/session.js
CHANGED
|
@@ -41,7 +41,6 @@ export class Session {
|
|
|
41
41
|
async init(ctr, link, auth = false, opt = {}) {
|
|
42
42
|
const config = ctr.getPrototype('_config');
|
|
43
43
|
const ssl = opt.ssl ?? config.session.ssl ?? false;
|
|
44
|
-
const pre = opt.sqlPre ?? null;
|
|
45
44
|
this._name = opt.name ?? config.session.name;
|
|
46
45
|
this._ttl = opt.ttl ?? config.session.ttl ?? 172800;
|
|
47
46
|
const tim = time.stamp();
|
|
@@ -58,7 +57,11 @@ export class Session {
|
|
|
58
57
|
}
|
|
59
58
|
this._link = link;
|
|
60
59
|
if (link instanceof db.Pool) {
|
|
61
|
-
this._sql = sql.get(
|
|
60
|
+
this._sql = sql.get({
|
|
61
|
+
'service': link.getService(),
|
|
62
|
+
'ctr': ctr,
|
|
63
|
+
'pre': opt.sqlPre,
|
|
64
|
+
});
|
|
62
65
|
await this._gc(); // --- 执行 gc ---
|
|
63
66
|
}
|
|
64
67
|
// --- 初始化 Session 数组 ---
|
package/lib/sql.d.ts
CHANGED
|
@@ -11,6 +11,8 @@ export declare enum ESERVICE {
|
|
|
11
11
|
'PGSQL' = 1
|
|
12
12
|
}
|
|
13
13
|
export declare class Sql {
|
|
14
|
+
/** --- ctr 对象 --- */
|
|
15
|
+
private readonly _ctr?;
|
|
14
16
|
/** --- 前置 --- */
|
|
15
17
|
private readonly _pre;
|
|
16
18
|
/** --- 服务商 --- */
|
|
@@ -23,10 +25,12 @@ export declare class Sql {
|
|
|
23
25
|
private readonly _alias;
|
|
24
26
|
/** --- PostgreSQL 占位符计数器 --- */
|
|
25
27
|
private _placeholderCounter;
|
|
26
|
-
constructor(
|
|
28
|
+
constructor(opt: {
|
|
29
|
+
'service': ESERVICE;
|
|
30
|
+
'ctr'?: ctr.Ctr;
|
|
31
|
+
'pre'?: string;
|
|
27
32
|
'data'?: kebab.DbValue[];
|
|
28
33
|
'sql'?: string[];
|
|
29
|
-
'service'?: ESERVICE;
|
|
30
34
|
});
|
|
31
35
|
/**
|
|
32
36
|
* --- 插入数据前导 ---
|
|
@@ -215,13 +219,14 @@ export declare class Sql {
|
|
|
215
219
|
}
|
|
216
220
|
/**
|
|
217
221
|
* --- 创建 sql 对象 ---
|
|
218
|
-
* @param ctrPre ctr 对象或 pre 表前缀
|
|
219
222
|
* @param opt 参数
|
|
220
223
|
*/
|
|
221
|
-
export declare function get(
|
|
224
|
+
export declare function get(opt: {
|
|
225
|
+
'service': ESERVICE;
|
|
226
|
+
'ctr'?: ctr.Ctr;
|
|
227
|
+
'pre'?: string;
|
|
222
228
|
'data'?: kebab.DbValue[];
|
|
223
229
|
'sql'?: string[];
|
|
224
|
-
'service'?: ESERVICE;
|
|
225
230
|
}): Sql;
|
|
226
231
|
/**
|
|
227
232
|
* --- 返回代入后的完整 SQL 字符串,这并不安全不能直接执行,只是用来调试打印 sql 语句 ---
|
package/lib/sql.js
CHANGED
|
@@ -2,8 +2,6 @@ import * as lText from '#kebab/lib/text.js';
|
|
|
2
2
|
import * as lCore from '#kebab/lib/core.js';
|
|
3
3
|
// --- 第三方 ---
|
|
4
4
|
import * as mysql2 from 'mysql2/promise';
|
|
5
|
-
// --- 库和定义 ---
|
|
6
|
-
import * as ctr from '#kebab/sys/ctr.js';
|
|
7
5
|
/** --- 服务商定义 --- */
|
|
8
6
|
export var ESERVICE;
|
|
9
7
|
(function (ESERVICE) {
|
|
@@ -14,7 +12,7 @@ export var ESERVICE;
|
|
|
14
12
|
let columnToken = '';
|
|
15
13
|
export class Sql {
|
|
16
14
|
// --- 实例化 ---
|
|
17
|
-
constructor(
|
|
15
|
+
constructor(opt) {
|
|
18
16
|
/** --- 前置 --- */
|
|
19
17
|
this._pre = '';
|
|
20
18
|
/** --- 预拼装 Sql 数组 --- */
|
|
@@ -27,7 +25,8 @@ export class Sql {
|
|
|
27
25
|
this._placeholderCounter = 1;
|
|
28
26
|
/** --- where 的 data 的开始处和结束处 --- */
|
|
29
27
|
this._whereDataPosition = [0, 0];
|
|
30
|
-
this.
|
|
28
|
+
this._ctr = opt.ctr;
|
|
29
|
+
this._pre = opt.pre ?? '';
|
|
31
30
|
this._service = opt.service ?? ESERVICE.MYSQL;
|
|
32
31
|
if (opt.data) {
|
|
33
32
|
this._data = opt.data;
|
|
@@ -646,7 +645,10 @@ export class Sql {
|
|
|
646
645
|
}
|
|
647
646
|
sql[0] = sql[0].replace(/FROM [`\w, ]+/, 'FROM ' + table);
|
|
648
647
|
}
|
|
649
|
-
return get(
|
|
648
|
+
return get({
|
|
649
|
+
'service': this._service,
|
|
650
|
+
'ctr': this._ctr,
|
|
651
|
+
'pre': this._pre,
|
|
650
652
|
'data': data,
|
|
651
653
|
'sql': sql,
|
|
652
654
|
});
|
|
@@ -703,8 +705,6 @@ export class Sql {
|
|
|
703
705
|
* @param suf 表后缀,仅请在 field 表名时倒入后缀,前面加 # 代表要强制 AS,可能是分表查询时用
|
|
704
706
|
*/
|
|
705
707
|
field(str, pre = '', suf = '') {
|
|
706
|
-
let left = '';
|
|
707
|
-
let right = '';
|
|
708
708
|
const q = this._service === ESERVICE.MYSQL ? '`' : '"';
|
|
709
709
|
if (Array.isArray(str)) {
|
|
710
710
|
this._data.push(...str[1]);
|
|
@@ -713,21 +713,21 @@ export class Sql {
|
|
|
713
713
|
if (typeof str === 'number') {
|
|
714
714
|
str = str.toString();
|
|
715
715
|
}
|
|
716
|
-
str = str.trim()
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
716
|
+
str = str.trim() // --- 去除前导尾随 ---
|
|
717
|
+
.replace(/ {2,}/g, ' ') // --- 去除多余的空格 ---
|
|
718
|
+
.replace(/ +([),])/g, ' $1')
|
|
719
|
+
.replace(/([(,]) +/g, '$1 ')
|
|
720
|
+
.replace(/(\W)(JOIN|WHERE|UNION)(\W)/ig, '$1$3');
|
|
721
721
|
// --- 先判断 suf 强制性 AS ---
|
|
722
|
-
|
|
723
|
-
if (
|
|
722
|
+
const sufAs = suf.startsWith('#');
|
|
723
|
+
if (sufAs) {
|
|
724
724
|
// --- 强制 AS ---
|
|
725
725
|
suf = suf.slice(1);
|
|
726
|
-
sufAs = true;
|
|
727
726
|
}
|
|
728
727
|
// --- 先判断有没有别名(也就是 as) ---
|
|
729
|
-
const
|
|
730
|
-
|
|
728
|
+
const asPos = str.toLowerCase().indexOf(' as ');
|
|
729
|
+
let left = '';
|
|
730
|
+
let right = '';
|
|
731
731
|
if (asPos === -1) {
|
|
732
732
|
// --- 没有 as ---
|
|
733
733
|
let spacePos = str.lastIndexOf(' ');
|
|
@@ -736,21 +736,12 @@ export class Sql {
|
|
|
736
736
|
// --- 连接符 ---
|
|
737
737
|
spacePos = -1;
|
|
738
738
|
}
|
|
739
|
-
if (spacePos !== -1) {
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
// --- OK ---
|
|
743
|
-
left = str.slice(0, spacePos);
|
|
744
|
-
right = spaceRight;
|
|
745
|
-
}
|
|
746
|
-
else {
|
|
747
|
-
left = str;
|
|
748
|
-
right = '';
|
|
749
|
-
}
|
|
739
|
+
if (spacePos !== -1 && /^[a-zA-Z_`"][\w`"]*$/.test(str.slice(spacePos + 1))) {
|
|
740
|
+
left = str.slice(0, spacePos);
|
|
741
|
+
right = str.slice(spacePos + 1);
|
|
750
742
|
}
|
|
751
743
|
else {
|
|
752
744
|
left = str;
|
|
753
|
-
right = '';
|
|
754
745
|
}
|
|
755
746
|
}
|
|
756
747
|
else {
|
|
@@ -760,90 +751,60 @@ export class Sql {
|
|
|
760
751
|
}
|
|
761
752
|
if (right) {
|
|
762
753
|
// --- 处理右侧 ---
|
|
754
|
+
const hasQuote = right.startsWith(q);
|
|
755
|
+
if (!left.includes('.')) {
|
|
756
|
+
// --- 存储表别名 ---
|
|
757
|
+
this._alias.push(hasQuote ? right.slice(1, -1) : right);
|
|
758
|
+
}
|
|
763
759
|
if (this._service === ESERVICE.MYSQL) {
|
|
764
|
-
|
|
765
|
-
if (!left.includes('.')) {
|
|
766
|
-
// --- 存储表别名 ---
|
|
767
|
-
this._alias.push(right.slice(1, -1));
|
|
768
|
-
}
|
|
769
|
-
right = '`' + pre + right.slice(1);
|
|
770
|
-
}
|
|
771
|
-
else {
|
|
772
|
-
if (!left.includes('.')) {
|
|
773
|
-
// --- 存储表别名 ---
|
|
774
|
-
this._alias.push(right);
|
|
775
|
-
}
|
|
776
|
-
right = '`' + pre + right + '`';
|
|
777
|
-
}
|
|
778
|
-
right = ' AS ' + right;
|
|
760
|
+
right = ' AS `' + pre + (hasQuote ? right.slice(1) : right + '`');
|
|
779
761
|
}
|
|
780
762
|
else {
|
|
781
|
-
|
|
782
|
-
if (!left.includes('.')) {
|
|
783
|
-
// --- 存储表别名 ---
|
|
784
|
-
this._alias.push(right.slice(1, -1));
|
|
785
|
-
}
|
|
786
|
-
if (pre) {
|
|
787
|
-
right = `"${pre}".${right}`;
|
|
788
|
-
}
|
|
789
|
-
}
|
|
790
|
-
else {
|
|
791
|
-
if (!left.includes('.')) {
|
|
792
|
-
// --- 存储表别名 ---
|
|
793
|
-
this._alias.push(right);
|
|
794
|
-
}
|
|
795
|
-
right = (pre ? `"${pre}".` : '') + `"${right}"`;
|
|
796
|
-
}
|
|
797
|
-
right = ' AS ' + right;
|
|
763
|
+
right = ' AS ' + (pre ? `"${pre}".` : '') + (hasQuote ? right : `"${right}"`);
|
|
798
764
|
}
|
|
799
765
|
}
|
|
800
|
-
else {
|
|
766
|
+
else if (sufAs) {
|
|
801
767
|
// --- 没有右侧 ---
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
this._alias.push(left.startsWith('`') || left.startsWith('"') ? left.slice(1, -1) : left);
|
|
807
|
-
}
|
|
808
|
-
right = ' AS ' + this.field(left, pre);
|
|
768
|
+
// --- 强制 AS ---
|
|
769
|
+
if (!left.includes('.')) {
|
|
770
|
+
// --- 存储表别名 ---
|
|
771
|
+
this._alias.push(left.startsWith(q) ? left.slice(1, -1) : left);
|
|
809
772
|
}
|
|
773
|
+
right = ' AS ' + this.field(left, pre);
|
|
810
774
|
}
|
|
811
775
|
// --- 处理 left ---
|
|
812
776
|
if (/^[\w`"_.*]+$/.test(left)) {
|
|
777
|
+
// --- 简单字段或表名 ---
|
|
778
|
+
/** --- 分左右 --- */
|
|
813
779
|
const l = left.split('.');
|
|
814
780
|
if (l[0] === '*') {
|
|
815
781
|
return '*' + right;
|
|
816
782
|
}
|
|
817
|
-
|
|
818
|
-
l[0] = l[0].replace(/[`"]/g, '');
|
|
819
|
-
}
|
|
783
|
+
l[0] = l[0].replace(this._service === ESERVICE.MYSQL ? /`/g : /"/g, '');
|
|
820
784
|
if (l[1] === undefined) {
|
|
821
|
-
// --- xxx ---
|
|
785
|
+
// --- xxx,代表无 . 右侧 ---
|
|
786
|
+
if ((this._service === ESERVICE.MYSQL && l[0].startsWith('"')) ||
|
|
787
|
+
(this._service === ESERVICE.PGSQL && l[0].startsWith(`'`))) {
|
|
788
|
+
// --- 字符串,无需包裹 ---
|
|
789
|
+
return l[0] + right;
|
|
790
|
+
}
|
|
822
791
|
if (/^[A-Z0-9_]+$/.test(l[0])) {
|
|
823
792
|
// --- 纯大写是内置函数,不能加 `" ---
|
|
824
793
|
return l[0] + right;
|
|
825
794
|
}
|
|
826
795
|
return this._service === ESERVICE.MYSQL ?
|
|
827
|
-
|
|
828
|
-
(pre ? `"${pre}".` : '') +
|
|
796
|
+
`\`${pre}${l[0]}${suf}\`${right}` :
|
|
797
|
+
(pre ? `"${pre}".` : '') + `"${l[0]}${suf}"${right}`;
|
|
829
798
|
}
|
|
830
799
|
// --- x.xxx ---
|
|
831
800
|
// --- 只有在此模式才知道 . 前面的一定是表名,因此自动加 sql 级的 _pre ---
|
|
832
|
-
const w = l[1] === '*'
|
|
833
|
-
? '*' :
|
|
834
|
-
(q + ((l[1].startsWith('`') || l[1].startsWith('"')) ?
|
|
835
|
-
l[1].slice(1, -1) :
|
|
836
|
-
l[1]) + q);
|
|
801
|
+
const w = l[1] === '*' ? '*' : q + (l[1].startsWith(q) ? l[1].slice(1, -1) : l[1]) + q;
|
|
837
802
|
return this._service === ESERVICE.MYSQL ?
|
|
838
|
-
|
|
839
|
-
(this._pre ? `"${this._pre}".` : '') +
|
|
840
|
-
}
|
|
841
|
-
else {
|
|
842
|
-
// return left.replace(/([(, ])([a-zA-Z`"_][\w`"_.]*)(?=[), ])/g, (
|
|
843
|
-
return left.replace(/(^|[(, ])([a-zA-Z`"_][\w`"_.]*)(?=[), ]|$)/g, (t, t1, t2) => {
|
|
844
|
-
return t1 + this.field(t2, pre, suf);
|
|
845
|
-
}) + right;
|
|
803
|
+
`\`${this._pre}${l[0]}${suf}\`.${w}${right}` :
|
|
804
|
+
(this._pre ? `"${this._pre}".` : '') + `"${l[0]}${suf}".${w}${right}`;
|
|
846
805
|
}
|
|
806
|
+
// --- 复杂字段或表达式 ---
|
|
807
|
+
return left.replace(/(^|[(, ])([a-zA-Z`"_][\w`"_.]*)(?=[), ]|$)/g, (t, t1, t2) => t1 + this.field(t2, pre, suf)) + right;
|
|
847
808
|
}
|
|
848
809
|
/**
|
|
849
810
|
* --- 判断传入值是否是 field,还是别的对象 ---
|
|
@@ -975,11 +936,21 @@ export class Sql {
|
|
|
975
936
|
}
|
|
976
937
|
/**
|
|
977
938
|
* --- 创建 sql 对象 ---
|
|
978
|
-
* @param ctrPre ctr 对象或 pre 表前缀
|
|
979
939
|
* @param opt 参数
|
|
980
940
|
*/
|
|
981
|
-
export function get(
|
|
982
|
-
|
|
941
|
+
export function get(opt) {
|
|
942
|
+
let pre = opt.pre ?? opt.ctr?.getPrototype('_config').sql.pre;
|
|
943
|
+
if (pre) {
|
|
944
|
+
if (opt.service === ESERVICE.MYSQL) {
|
|
945
|
+
if (!pre.endsWith('_')) {
|
|
946
|
+
pre += '_';
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
return new Sql({
|
|
951
|
+
...opt,
|
|
952
|
+
'pre': pre,
|
|
953
|
+
});
|
|
983
954
|
}
|
|
984
955
|
/**
|
|
985
956
|
* --- 返回代入后的完整 SQL 字符串,这并不安全不能直接执行,只是用来调试打印 sql 语句 ---
|
package/package.json
CHANGED
package/sys/cmd.js
CHANGED
package/sys/mod.js
CHANGED
|
@@ -79,8 +79,10 @@ export default class Mod {
|
|
|
79
79
|
/** --- 导入数据库连接 --- */
|
|
80
80
|
this._db = opt.db;
|
|
81
81
|
/** --- 新建 sql 对象 --- */
|
|
82
|
-
this._sql = lSql.get(
|
|
82
|
+
this._sql = lSql.get({
|
|
83
83
|
'service': this._db.getService() ?? lDb.ESERVICE.PGSQL,
|
|
84
|
+
'ctr': opt.ctr,
|
|
85
|
+
'pre': opt.pre,
|
|
84
86
|
});
|
|
85
87
|
if (opt.index) {
|
|
86
88
|
this._index = typeof opt.index === 'string' ? [opt.index] : [...new Set(opt.index)];
|
|
@@ -118,8 +120,10 @@ export default class Mod {
|
|
|
118
120
|
* @param opt 选项
|
|
119
121
|
*/
|
|
120
122
|
static async insert(db, cs, vs, opt = {}) {
|
|
121
|
-
const sq = lSql.get(
|
|
123
|
+
const sq = lSql.get({
|
|
122
124
|
'service': db.getService() ?? lDb.ESERVICE.PGSQL,
|
|
125
|
+
'ctr': opt.ctr,
|
|
126
|
+
'pre': opt.pre,
|
|
123
127
|
});
|
|
124
128
|
if (!vs) {
|
|
125
129
|
// --- 单行 ---
|
|
@@ -165,8 +169,10 @@ export default class Mod {
|
|
|
165
169
|
* @param opt 选项
|
|
166
170
|
*/
|
|
167
171
|
static insertSql(db, cs, vs, opt = {}) {
|
|
168
|
-
const sq = lSql.get(
|
|
172
|
+
const sq = lSql.get({
|
|
169
173
|
'service': db.getService() ?? lDb.ESERVICE.PGSQL,
|
|
174
|
+
'ctr': opt.ctr,
|
|
175
|
+
'pre': opt.pre,
|
|
170
176
|
});
|
|
171
177
|
sq.insert(this._$table + (opt.index ? ('_' + opt.index) : '')).values(cs, vs);
|
|
172
178
|
return sq.format();
|
|
@@ -181,8 +187,10 @@ export default class Mod {
|
|
|
181
187
|
const indexs = opt.index ? (typeof opt.index === 'string' ? [opt.index] : [...new Set(opt.index)]) : [''];
|
|
182
188
|
let ar = 0;
|
|
183
189
|
for (const index of indexs) {
|
|
184
|
-
const sq = lSql.get(
|
|
190
|
+
const sq = lSql.get({
|
|
185
191
|
'service': db.getService() ?? lDb.ESERVICE.PGSQL,
|
|
192
|
+
'ctr': opt.ctr,
|
|
193
|
+
'pre': opt.pre,
|
|
186
194
|
});
|
|
187
195
|
sq.delete(this._$table + (index ? ('_' + index) : '')).where(where);
|
|
188
196
|
if (opt.by) {
|
|
@@ -209,8 +217,10 @@ export default class Mod {
|
|
|
209
217
|
* @param opt 选项
|
|
210
218
|
*/
|
|
211
219
|
static removeByWhereSql(db, where, opt = {}) {
|
|
212
|
-
const sq = lSql.get(
|
|
220
|
+
const sq = lSql.get({
|
|
213
221
|
'service': db.getService() ?? lDb.ESERVICE.PGSQL,
|
|
222
|
+
'ctr': opt.ctr,
|
|
223
|
+
'pre': opt.pre,
|
|
214
224
|
});
|
|
215
225
|
sq.delete(this._$table + (opt.index ? ('_' + opt.index) : '')).where(where);
|
|
216
226
|
if (opt.by) {
|
|
@@ -232,8 +242,10 @@ export default class Mod {
|
|
|
232
242
|
const indexs = opt.index ? (typeof opt.index === 'string' ? [opt.index] : [...new Set(opt.index)]) : [''];
|
|
233
243
|
let ar = 0;
|
|
234
244
|
for (const index of indexs) {
|
|
235
|
-
const sq = lSql.get(
|
|
245
|
+
const sq = lSql.get({
|
|
236
246
|
'service': db.getService() ?? lDb.ESERVICE.PGSQL,
|
|
247
|
+
'ctr': opt.ctr,
|
|
248
|
+
'pre': opt.pre,
|
|
237
249
|
});
|
|
238
250
|
sq.update(this._$table + (index ? ('_' + index) : ''), data).where(where);
|
|
239
251
|
if (opt.by) {
|
|
@@ -261,8 +273,10 @@ export default class Mod {
|
|
|
261
273
|
* @param opt 选项
|
|
262
274
|
*/
|
|
263
275
|
static updateByWhereSql(db, data, where, opt = {}) {
|
|
264
|
-
const sq = lSql.get(
|
|
276
|
+
const sq = lSql.get({
|
|
265
277
|
'service': db.getService() ?? lDb.ESERVICE.PGSQL,
|
|
278
|
+
'ctr': opt.ctr,
|
|
279
|
+
'pre': opt.pre,
|
|
266
280
|
});
|
|
267
281
|
sq.update(this._$table + (opt.index ? ('_' + opt.index) : ''), data).where(where);
|
|
268
282
|
if (opt.by) {
|
|
@@ -307,8 +321,10 @@ export default class Mod {
|
|
|
307
321
|
batches.push(data.slice(i, i + batchSize));
|
|
308
322
|
}
|
|
309
323
|
for (const batch of batches) {
|
|
310
|
-
const sq = lSql.get(
|
|
324
|
+
const sq = lSql.get({
|
|
311
325
|
'service': db.getService() ?? lDb.ESERVICE.PGSQL,
|
|
326
|
+
'ctr': opt.ctr,
|
|
327
|
+
'pre': opt.pre,
|
|
312
328
|
});
|
|
313
329
|
const updates = {};
|
|
314
330
|
const keys = [];
|
|
@@ -475,8 +491,10 @@ export default class Mod {
|
|
|
475
491
|
* @param opt 选项
|
|
476
492
|
*/
|
|
477
493
|
static async primarys(db, where = '', opt = {}) {
|
|
478
|
-
const sq = lSql.get(
|
|
494
|
+
const sq = lSql.get({
|
|
479
495
|
'service': db.getService() ?? lDb.ESERVICE.PGSQL,
|
|
496
|
+
'ctr': opt.ctr,
|
|
497
|
+
'pre': opt.pre,
|
|
480
498
|
});
|
|
481
499
|
sq.select(this._$primary, this._$table + (opt.index ? ('_' + opt.index) : '')).where(where);
|
|
482
500
|
const r = await db.query(sq.getSql(), sq.getData());
|
package/www/example/ctr/test.js
CHANGED
|
@@ -2098,9 +2098,9 @@ error: <pre>${JSON.stringify(res.error, null, 4)}</pre>`);
|
|
|
2098
2098
|
}
|
|
2099
2099
|
const s = this._get['s'] ?? 'db';
|
|
2100
2100
|
const echo = [];
|
|
2101
|
-
const scan = await lScan.get(link, undefined, { 'ttl': 30, '
|
|
2101
|
+
const scan = await lScan.get(link, undefined, { 'ttl': 30, 'ctr': this });
|
|
2102
2102
|
const token = scan.getToken();
|
|
2103
|
-
echo.push(`<pre>const scan = await lScan.get(this, link, undefined, { 'ttl': 30, '
|
|
2103
|
+
echo.push(`<pre>const scan = await lScan.get(this, link, undefined, { 'ttl': 30, 'ctr': this });
|
|
2104
2104
|
const token = scan.getToken();</pre>
|
|
2105
2105
|
token: ${token ?? 'null'}<br><br>
|
|
2106
2106
|
Scan status: <b id="status" style="color: red;">Waiting...</b><br>
|
|
@@ -2189,7 +2189,7 @@ function confirm() {
|
|
|
2189
2189
|
return [0, 'Failed, link can not be connected.'];
|
|
2190
2190
|
}
|
|
2191
2191
|
const scan = await lScan.get(link, this._post['token'], {
|
|
2192
|
-
'
|
|
2192
|
+
'ctr': this
|
|
2193
2193
|
});
|
|
2194
2194
|
const rtn = await scan.poll();
|
|
2195
2195
|
switch (rtn) {
|
|
@@ -2214,7 +2214,7 @@ function confirm() {
|
|
|
2214
2214
|
return [0, 'Failed, link can not be connected.'];
|
|
2215
2215
|
}
|
|
2216
2216
|
if (!await lScan.scanned(link, this._post['token'], {
|
|
2217
|
-
'
|
|
2217
|
+
'ctr': this
|
|
2218
2218
|
})) {
|
|
2219
2219
|
return [0, 'Token has expired.'];
|
|
2220
2220
|
}
|
|
@@ -2228,7 +2228,7 @@ function confirm() {
|
|
|
2228
2228
|
if (!await lScan.setData(link, this._post['token'], {
|
|
2229
2229
|
'uid': '5'
|
|
2230
2230
|
}, {
|
|
2231
|
-
'
|
|
2231
|
+
'ctr': this
|
|
2232
2232
|
})) {
|
|
2233
2233
|
return [0, 'Token has expired.'];
|
|
2234
2234
|
}
|
|
@@ -2315,9 +2315,11 @@ Result:<pre id="result">Nothing.</pre>`);
|
|
|
2315
2315
|
}
|
|
2316
2316
|
sql() {
|
|
2317
2317
|
const echo = [];
|
|
2318
|
-
let sql =
|
|
2319
|
-
'service': lSql.ESERVICE.PGSQL,
|
|
2320
|
-
|
|
2318
|
+
let sql = lSql.get({
|
|
2319
|
+
'service': this._get['s'] === 'pgsql' ? lSql.ESERVICE.PGSQL : lSql.ESERVICE.MYSQL,
|
|
2320
|
+
'ctr': this,
|
|
2321
|
+
'pre': 'test'
|
|
2322
|
+
});
|
|
2321
2323
|
switch (this._get['type']) {
|
|
2322
2324
|
case 'insert': {
|
|
2323
2325
|
let s = sql.insert('user').values(['name', 'age'], [
|
|
@@ -2422,9 +2424,12 @@ Result:<pre id="result">Nothing.</pre>`);
|
|
|
2422
2424
|
<b>getSql() :</b> ${s}<br>
|
|
2423
2425
|
<b>getData():</b> <pre>${JSON.stringify(sd, undefined, 4)}</pre>
|
|
2424
2426
|
<b>format() :</b> ${sql.format(s, sd)}<hr>`);
|
|
2425
|
-
|
|
2427
|
+
const data = this._get['s'] === 'pgsql' ?
|
|
2428
|
+
[`IFNULL(user.nick, '') nick`, `IFNULL(user.nick, 'a2') nick2`] :
|
|
2429
|
+
[`IFNULL(user.nick, "") nick`, `IFNULL(user.nick, "a2") nick2`];
|
|
2430
|
+
s = sql.select(['SUM(user.age) age', 'UTC_TIMESTAMP', 'FROM_UNIXTIME(user.time, \'%Y-%m\') as time', ...data], 'order').leftJoin('user', { 'order.user_id': lSql.column('user.id') }).getSql();
|
|
2426
2431
|
sd = sql.getData();
|
|
2427
|
-
echo.push(`<pre>sql.select(['SUM(user.age) age', 'UTC_TIMESTAMP', 'FROM_UNIXTIME(user.time, \\'%Y-%m\\') as time'], 'order').leftJoin('user', { 'order.user_id': lSql.column('user.id') });</pre>
|
|
2432
|
+
echo.push(`<pre>sql.select(['SUM(user.age) age', 'UTC_TIMESTAMP', 'FROM_UNIXTIME(user.time, \\'%Y-%m\\') as time', \`${data[0]}\`, \`${data[1]}\`], 'order').leftJoin('user', { 'order.user_id': lSql.column('user.id') });</pre>
|
|
2428
2433
|
<b>getSql() :</b> ${s}<br>
|
|
2429
2434
|
<b>getData():</b> <pre>${JSON.stringify(sd, undefined, 4)}</pre>
|
|
2430
2435
|
<b>format() :</b> ${sql.format(s, sd)}<hr>`);
|