@maiyunnet/kebab 10.1.1 → 11.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/doc/kebab-rag.md +385 -199
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/lib/db/conn.js +22 -0
- package/lib/sql.d.ts +74 -5
- package/lib/sql.js +519 -264
- package/lib/text.d.ts +10 -0
- package/lib/text.js +35 -0
- package/package.json +1 -1
- package/sys/mod.d.ts +9 -5
- package/sys/mod.js +121 -20
- package/www/example/ctr/test.js +18 -10
package/lib/text.d.ts
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import * as kebab from '#kebab/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* --- 将数字或纯整数字符串严格解析为安全整数,支持可选范围边界 ---
|
|
4
|
+
* @param val 待解析的值(number 或十进制整数字符串,允许前后空白和可选负号)
|
|
5
|
+
* @param opt 可选范围限制(min 最小值、max 最大值)
|
|
6
|
+
* @returns 规范化后的安全整数,不合法或超出范围返回 null
|
|
7
|
+
*/
|
|
8
|
+
export declare function parseSafeInt(val: unknown, opt?: {
|
|
9
|
+
'min'?: number;
|
|
10
|
+
'max'?: number;
|
|
11
|
+
}): number | null;
|
|
2
12
|
/**
|
|
3
13
|
* --- 解析三段正式发行版本 X.Y.Z,不接受前导零、预发行和构建后缀 ---
|
|
4
14
|
* @param value 待解析的版本
|
package/lib/text.js
CHANGED
|
@@ -9,6 +9,41 @@ import * as textPath from '#kebab/abox/text-path.js';
|
|
|
9
9
|
import * as kebab from '#kebab/index.js';
|
|
10
10
|
import * as lFs from './fs.js';
|
|
11
11
|
import * as lCore from './core.js';
|
|
12
|
+
/**
|
|
13
|
+
* --- 将数字或纯整数字符串严格解析为安全整数,支持可选范围边界 ---
|
|
14
|
+
* @param val 待解析的值(number 或十进制整数字符串,允许前后空白和可选负号)
|
|
15
|
+
* @param opt 可选范围限制(min 最小值、max 最大值)
|
|
16
|
+
* @returns 规范化后的安全整数,不合法或超出范围返回 null
|
|
17
|
+
*/
|
|
18
|
+
export function parseSafeInt(val, opt = {}) {
|
|
19
|
+
let num;
|
|
20
|
+
if (typeof val === 'number') {
|
|
21
|
+
num = val;
|
|
22
|
+
}
|
|
23
|
+
else if (typeof val === 'string') {
|
|
24
|
+
const str = val.trim();
|
|
25
|
+
if (!/^-?\d+$/.test(str)) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
num = Number(str);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
if (!Number.isSafeInteger(num)) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
if (num === 0) {
|
|
37
|
+
num = 0;
|
|
38
|
+
}
|
|
39
|
+
if (opt.min !== undefined && num < opt.min) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
if (opt.max !== undefined && num > opt.max) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
return num;
|
|
46
|
+
}
|
|
12
47
|
/**
|
|
13
48
|
* --- 解析三段正式发行版本 X.Y.Z,不接受前导零、预发行和构建后缀 ---
|
|
14
49
|
* @param value 待解析的版本
|
package/package.json
CHANGED
package/sys/mod.d.ts
CHANGED
|
@@ -141,6 +141,8 @@ export default class Mod {
|
|
|
141
141
|
};
|
|
142
142
|
/** --- 标记 JSON 字段;模型内保持原始对象,写入数据库时再序列化 --- */
|
|
143
143
|
static json<T>(obj: T): T;
|
|
144
|
+
/** --- 创建显式 SQL 表达式值,用于写入函数或 CASE 等原生表达式 --- */
|
|
145
|
+
static raw(sql: string, params?: kebab.DbValue[]): lSql.IRawValue;
|
|
144
146
|
/**
|
|
145
147
|
* --- 添加一个序列(允许超过 65536 的占位符会被拆分多次执行) ---
|
|
146
148
|
* @param db 数据库对象
|
|
@@ -354,8 +356,8 @@ export default class Mod {
|
|
|
354
356
|
* @param value 字段值
|
|
355
357
|
*/
|
|
356
358
|
private _setProperty;
|
|
357
|
-
set<T extends this, TK extends keyof T>(n: Record<TK, T[TK] | undefined>): void;
|
|
358
|
-
set<T extends this, TK extends keyof T>(n: TK, v: T[TK]): void;
|
|
359
|
+
set<T extends this, TK extends keyof T>(n: Record<TK, T[TK] | lSql.IRawValue | undefined>): void;
|
|
360
|
+
set<T extends this, TK extends keyof T>(n: TK, v: T[TK] | lSql.IRawValue): void;
|
|
359
361
|
/**
|
|
360
362
|
* --- 获取一个已加载的字段值 ---
|
|
361
363
|
* @param n 字段名
|
|
@@ -613,16 +615,18 @@ export default class Mod {
|
|
|
613
615
|
private _limit;
|
|
614
616
|
/**
|
|
615
617
|
* --- LIMIT ---
|
|
616
|
-
* @param a
|
|
617
|
-
* @param b
|
|
618
|
+
* @param a 起始(offset)或仅传单参时的长度(limit)
|
|
619
|
+
* @param b 长度(limit)
|
|
618
620
|
*/
|
|
619
621
|
limit(a: number, b?: number): this;
|
|
620
622
|
/**
|
|
621
623
|
* --- 分页 ---
|
|
622
624
|
* @param count 每页条数
|
|
623
|
-
* @param page
|
|
625
|
+
* @param page 当前页数(从 1 开始)
|
|
624
626
|
*/
|
|
625
627
|
page(count: number, page?: number): this;
|
|
628
|
+
/** --- 获取当前 SQL 构建错误,正常时为空字符串 --- */
|
|
629
|
+
getError(): string;
|
|
626
630
|
/**
|
|
627
631
|
* --- 在 sql 最后追加字符串 ---
|
|
628
632
|
* @param sql
|
package/sys/mod.js
CHANGED
|
@@ -142,6 +142,10 @@ export default class Mod {
|
|
|
142
142
|
static json(obj) {
|
|
143
143
|
return lSql.json(obj);
|
|
144
144
|
}
|
|
145
|
+
/** --- 创建显式 SQL 表达式值,用于写入函数或 CASE 等原生表达式 --- */
|
|
146
|
+
static raw(sql, params = []) {
|
|
147
|
+
return lSql.raw(sql, params);
|
|
148
|
+
}
|
|
145
149
|
/**
|
|
146
150
|
* --- 添加一个序列(允许超过 65536 的占位符会被拆分多次执行) ---
|
|
147
151
|
* @param db 数据库对象
|
|
@@ -159,6 +163,9 @@ export default class Mod {
|
|
|
159
163
|
// --- 单行 ---
|
|
160
164
|
sq.insert(this._$table + (opt.index ? ('_' + opt.index) : ''), opt.ignore);
|
|
161
165
|
sq.values(cs);
|
|
166
|
+
if (sq.getError()) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
162
169
|
const r = await db.execute(sq.getSql(), sq.getData());
|
|
163
170
|
if (r.packet === null) {
|
|
164
171
|
lCore.log(opt.ctr ?? {}, '[MOD][insert] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + '-' + sq.format()).replaceAll('"', '""'), '-error');
|
|
@@ -182,6 +189,9 @@ export default class Mod {
|
|
|
182
189
|
for (let i = 0; i < batch; ++i) {
|
|
183
190
|
sq.insert(this._$table + (opt.index ? ('_' + opt.index) : ''), opt.ignore);
|
|
184
191
|
sq.values(cs, vs.slice(i * line, (i + 1) * line));
|
|
192
|
+
if (sq.getError()) {
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
185
195
|
const r = await db.execute(sq.getSql(), sq.getData());
|
|
186
196
|
if (r.packet === null) {
|
|
187
197
|
lCore.log(opt.ctr ?? {}, '[MOD][insert] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + '-' + sq.format()).replaceAll('"', '""'), '-error');
|
|
@@ -232,6 +242,9 @@ export default class Mod {
|
|
|
232
242
|
if (opt.limit) {
|
|
233
243
|
sq.limit(opt.limit[0], opt.limit[1]);
|
|
234
244
|
}
|
|
245
|
+
if (sq.getError()) {
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
235
248
|
const r = await db.execute(sq.getSql(), sq.getData());
|
|
236
249
|
if (r.packet === null) {
|
|
237
250
|
lCore.log(opt.ctr ?? {}, '[MOD][removeByWhere] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + '-' + sq.format()).replaceAll('"', '""'), '-error');
|
|
@@ -287,6 +300,9 @@ export default class Mod {
|
|
|
287
300
|
if (opt.limit) {
|
|
288
301
|
sq.limit(opt.limit[0], opt.limit[1]);
|
|
289
302
|
}
|
|
303
|
+
if (sq.getError()) {
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
290
306
|
const r = await db.execute(sq.getSql(), sq.getData());
|
|
291
307
|
if (r.packet === null) {
|
|
292
308
|
lCore.log(opt.ctr ?? {}, '[MOD][updateByWhere] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + '-' + sq.format()).replaceAll('"', '""'), '-error');
|
|
@@ -389,6 +405,9 @@ export default class Mod {
|
|
|
389
405
|
});
|
|
390
406
|
const rows = batch.map(item => allCols.map(c => item[c] ?? null));
|
|
391
407
|
sq.updateByValues(tableName, key, cols, rows);
|
|
408
|
+
if (sq.getError()) {
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
392
411
|
const r = await db.execute(sq.getSql(), sq.getData());
|
|
393
412
|
if (r.packet === null) {
|
|
394
413
|
lCore.log(opt.ctr ?? {}, '[MOD][updateList] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + ' - ' + sq.format()).replaceAll('"', '""'), '-error');
|
|
@@ -531,6 +550,9 @@ export default class Mod {
|
|
|
531
550
|
'pre': opt.pre ?? this._$pre,
|
|
532
551
|
});
|
|
533
552
|
sq.select(this._$primary, this._$table + (opt.index ? ('_' + opt.index) : '')).where(where);
|
|
553
|
+
if (sq.getError()) {
|
|
554
|
+
return false;
|
|
555
|
+
}
|
|
534
556
|
const r = await db.query(sq.getSql(), sq.getData(), { 'read': opt.read });
|
|
535
557
|
if (r.rows === null) {
|
|
536
558
|
lCore.log(opt.ctr ?? {}, '[MOD][PRIMARYS] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + ' - ' + sq.format()).replaceAll('"', '""'), '-error');
|
|
@@ -660,6 +682,9 @@ export default class Mod {
|
|
|
660
682
|
if (service === lDb.ESERVICE.PGSQL) {
|
|
661
683
|
this._sql.append(' ON CONFLICT (' + keyFields.map(field => this._sql.field(field)).join(', ') + ') DO NOTHING');
|
|
662
684
|
}
|
|
685
|
+
if (this._sql.getError()) {
|
|
686
|
+
return false;
|
|
687
|
+
}
|
|
663
688
|
r = await this._db.execute(this._sql.getSql(), this._sql.getData());
|
|
664
689
|
++count;
|
|
665
690
|
if (!r.error) {
|
|
@@ -694,6 +719,9 @@ export default class Mod {
|
|
|
694
719
|
else {
|
|
695
720
|
this._sql.insert(cstr._$table + (this._index ? ('_' + this._index[0]) : ''));
|
|
696
721
|
this._sql.values(updates);
|
|
722
|
+
if (this._sql.getError()) {
|
|
723
|
+
return false;
|
|
724
|
+
}
|
|
697
725
|
r = await this._db.execute(this._sql.getSql(), this._sql.getData());
|
|
698
726
|
if (r.error) {
|
|
699
727
|
if (r.error.errno !== 1062) {
|
|
@@ -771,6 +799,9 @@ export default class Mod {
|
|
|
771
799
|
this._sql.append(` RETURNING ${identity.map(field => this._sql.field(field)).join(', ')}`);
|
|
772
800
|
}
|
|
773
801
|
// --- 读取目标身份并在 MySQL 下补做更新 ---
|
|
802
|
+
if (this._sql.getError()) {
|
|
803
|
+
return false;
|
|
804
|
+
}
|
|
774
805
|
const result = await this._db.query(this._sql.getSql(), this._sql.getData());
|
|
775
806
|
if (result.error) {
|
|
776
807
|
lCore.log(this._ctr ?? {}, `[MOD][upsert][${cstr._$table}] database error: ${result.error.errno}`, '-error');
|
|
@@ -783,6 +814,9 @@ export default class Mod {
|
|
|
783
814
|
if (mysql) {
|
|
784
815
|
// --- 同时匹配身份字段和业务键,目标变化时不回填旧身份 ---
|
|
785
816
|
this._sql.update(table, updates).where({ ...where, ...row });
|
|
817
|
+
if (this._sql.getError()) {
|
|
818
|
+
return false;
|
|
819
|
+
}
|
|
786
820
|
const saved = await this._db.execute(this._sql.getSql(), this._sql.getData());
|
|
787
821
|
if (saved.error) {
|
|
788
822
|
lCore.log(this._ctr ?? {}, `[MOD][upsert][${cstr._$table}] database error: ${saved.error.errno}`, '-error');
|
|
@@ -850,7 +884,7 @@ export default class Mod {
|
|
|
850
884
|
this._sql.update(cstr._$table + (this._index ? ('_' + this._index[0]) : ''), [updates]);
|
|
851
885
|
const updateSql = this._sql.getSql();
|
|
852
886
|
this._sql.where(filter);
|
|
853
|
-
if (this._sql.getSql() === updateSql) {
|
|
887
|
+
if (this._sql.getError() || this._sql.getSql() === updateSql) {
|
|
854
888
|
return false;
|
|
855
889
|
}
|
|
856
890
|
const r = await this._db.execute(this._sql.getSql(), this._sql.getData());
|
|
@@ -900,6 +934,9 @@ export default class Mod {
|
|
|
900
934
|
return this;
|
|
901
935
|
}
|
|
902
936
|
async first(lock = false, array = false) {
|
|
937
|
+
if (this._sql.getError()) {
|
|
938
|
+
return false;
|
|
939
|
+
}
|
|
903
940
|
this._sql.limit(1);
|
|
904
941
|
if (lock) {
|
|
905
942
|
this._sql.lock();
|
|
@@ -988,6 +1025,9 @@ export default class Mod {
|
|
|
988
1025
|
}
|
|
989
1026
|
async all(key) {
|
|
990
1027
|
this._total.length = 0;
|
|
1028
|
+
if (this._sql.getError()) {
|
|
1029
|
+
return false;
|
|
1030
|
+
}
|
|
991
1031
|
if (this._index && this._index.length > 1) {
|
|
992
1032
|
// --- 多表 ---
|
|
993
1033
|
let sql = this._sql.getSql();
|
|
@@ -1149,6 +1189,9 @@ export default class Mod {
|
|
|
1149
1189
|
}
|
|
1150
1190
|
async allArray(key) {
|
|
1151
1191
|
this._total.length = 0;
|
|
1192
|
+
if (this._sql.getError()) {
|
|
1193
|
+
return false;
|
|
1194
|
+
}
|
|
1152
1195
|
if (this._index && this._index.length > 1) {
|
|
1153
1196
|
// --- 多表 ---
|
|
1154
1197
|
let sql = this._sql.getSql();
|
|
@@ -1267,6 +1310,9 @@ export default class Mod {
|
|
|
1267
1310
|
* @param all 是否获取完全的情况,默认不获取,只返回扫描情况
|
|
1268
1311
|
*/
|
|
1269
1312
|
async explain(all = false) {
|
|
1313
|
+
if (this._sql.getError()) {
|
|
1314
|
+
return false;
|
|
1315
|
+
}
|
|
1270
1316
|
const r = await this._db.query('EXPLAIN ' + this._sql.getSql(), this._sql.getData(), { 'read': this._read });
|
|
1271
1317
|
if (r.rows === null) {
|
|
1272
1318
|
lCore.log(this._ctr ?? {}, '[MOD][explain] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + ' - ' + this._sql.format()).replaceAll('"', '""'), '-error');
|
|
@@ -1316,6 +1362,9 @@ export default class Mod {
|
|
|
1316
1362
|
* --- 获取总条数,自动抛弃 LIMIT,仅用于获取数据的情况(select) ---
|
|
1317
1363
|
*/
|
|
1318
1364
|
async total(f = '*') {
|
|
1365
|
+
if (this._sql.getError()) {
|
|
1366
|
+
return 0;
|
|
1367
|
+
}
|
|
1319
1368
|
if (this._total.length) {
|
|
1320
1369
|
let count = 0;
|
|
1321
1370
|
for (const item of this._total) {
|
|
@@ -1379,22 +1428,30 @@ export default class Mod {
|
|
|
1379
1428
|
* @returns 列表、首末行游标和扫描方向上的 hasMore,数据库错误返回 false
|
|
1380
1429
|
*/
|
|
1381
1430
|
async allCursor(count, opt) {
|
|
1431
|
+
if (this._sql.getError()) {
|
|
1432
|
+
return false;
|
|
1433
|
+
}
|
|
1382
1434
|
// --- 校验分页与游标参数 ---
|
|
1383
1435
|
if (!Number.isSafeInteger(count) || count < 1 || count >= Number.MAX_SAFE_INTEGER ||
|
|
1384
1436
|
this._index !== null || this._contain || (opt.before && !opt.cursor) ||
|
|
1385
1437
|
(opt.order !== undefined && !['ASC', 'DESC'].includes(opt.order))) {
|
|
1386
|
-
|
|
1438
|
+
lCore.log(this._ctr ?? {}, '[MOD][allCursor] Invalid cursor page options; framework sharding and contain are unsupported', '-error');
|
|
1439
|
+
return false;
|
|
1387
1440
|
}
|
|
1388
1441
|
const fields = typeof opt.by === 'string' ? [opt.by] : opt.by;
|
|
1389
1442
|
const keys = opt.keys ?? fields.map(field => field.split('.').pop());
|
|
1390
1443
|
if (keys.length !== fields.length || new Set(keys).size !== keys.length) {
|
|
1391
|
-
|
|
1444
|
+
lCore.log(this._ctr ?? {}, '[MOD][allCursor] Cursor result keys must match ordering fields', '-error');
|
|
1445
|
+
return false;
|
|
1392
1446
|
}
|
|
1393
1447
|
// --- 构造游标查询(多取 1 条判断 hasMore) ---
|
|
1394
1448
|
const order = opt.order ?? 'DESC';
|
|
1395
1449
|
const direction = opt.before ? (order === 'DESC' ? 'ASC' : 'DESC') : order;
|
|
1396
1450
|
const sql = this._sql.copy(undefined, { 'order': false, 'limit': false });
|
|
1397
1451
|
sql.seek(fields, opt.cursor, direction).limit(count + 1);
|
|
1452
|
+
if (sql.getError()) {
|
|
1453
|
+
return false;
|
|
1454
|
+
}
|
|
1398
1455
|
const result = await this._db.query(sql.getSql(), sql.getData(), { 'read': this._read });
|
|
1399
1456
|
if (result.rows === null) {
|
|
1400
1457
|
lCore.log(this._ctr ?? {}, '[MOD][allCursor] Query failed', '-error');
|
|
@@ -1406,25 +1463,46 @@ export default class Mod {
|
|
|
1406
1463
|
if (opt.before) {
|
|
1407
1464
|
list.reverse();
|
|
1408
1465
|
}
|
|
1409
|
-
const
|
|
1410
|
-
const
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1466
|
+
const readBoundary = (row) => {
|
|
1467
|
+
const values = [];
|
|
1468
|
+
for (const key of keys) {
|
|
1469
|
+
const val = row[key];
|
|
1470
|
+
if ((typeof val !== 'string' && typeof val !== 'number') ||
|
|
1471
|
+
(typeof val === 'number' && (!Number.isFinite(val) || (Number.isInteger(val) && !Number.isSafeInteger(val))))) {
|
|
1472
|
+
return null;
|
|
1473
|
+
}
|
|
1474
|
+
values.push(val);
|
|
1414
1475
|
}
|
|
1415
|
-
return
|
|
1416
|
-
}
|
|
1476
|
+
return values;
|
|
1477
|
+
};
|
|
1478
|
+
if (!list.length) {
|
|
1479
|
+
return {
|
|
1480
|
+
'list': [],
|
|
1481
|
+
'hasMore': false,
|
|
1482
|
+
'next': null,
|
|
1483
|
+
'previous': null,
|
|
1484
|
+
};
|
|
1485
|
+
}
|
|
1486
|
+
const next = readBoundary(list[list.length - 1]);
|
|
1487
|
+
const previous = readBoundary(list[0]);
|
|
1488
|
+
if (next === null || previous === null) {
|
|
1489
|
+
lCore.log(this._ctr ?? {}, '[MOD][allCursor] Cursor columns must be non-null strings or safe numbers', '-error');
|
|
1490
|
+
return false;
|
|
1491
|
+
}
|
|
1417
1492
|
return {
|
|
1418
1493
|
'list': list,
|
|
1419
1494
|
'hasMore': hasMore,
|
|
1420
|
-
'next':
|
|
1421
|
-
'previous':
|
|
1495
|
+
'next': next,
|
|
1496
|
+
'previous': previous,
|
|
1422
1497
|
};
|
|
1423
1498
|
}
|
|
1424
1499
|
/**
|
|
1425
1500
|
* --- 根据当前条件,筛选出当前条目该有的数据条数 ---
|
|
1426
1501
|
*/
|
|
1427
1502
|
async count() {
|
|
1503
|
+
if (this._sql.getError()) {
|
|
1504
|
+
return 0;
|
|
1505
|
+
}
|
|
1428
1506
|
const sql = this._sql.getSql()
|
|
1429
1507
|
.replace(/SELECT .+? FROM/is, this._db.getService() === lDb.ESERVICE.MYSQL ?
|
|
1430
1508
|
'SELECT COUNT(0) AS `count` FROM' :
|
|
@@ -1586,25 +1664,48 @@ export default class Mod {
|
|
|
1586
1664
|
_limit = [0, 0];
|
|
1587
1665
|
/**
|
|
1588
1666
|
* --- LIMIT ---
|
|
1589
|
-
* @param a
|
|
1590
|
-
* @param b
|
|
1667
|
+
* @param a 起始(offset)或仅传单参时的长度(limit)
|
|
1668
|
+
* @param b 长度(limit)
|
|
1591
1669
|
*/
|
|
1592
1670
|
limit(a, b = 0) {
|
|
1593
|
-
|
|
1594
|
-
|
|
1671
|
+
const first = lText.parseSafeInt(a, { 'min': 0 });
|
|
1672
|
+
const second = lText.parseSafeInt(b, { 'min': 0 });
|
|
1673
|
+
if (first === null || second === null) {
|
|
1674
|
+
this._sql.limit(Number.NaN);
|
|
1675
|
+
this._limit = [0, 0];
|
|
1676
|
+
return this;
|
|
1677
|
+
}
|
|
1678
|
+
this._sql.limit(first, second);
|
|
1679
|
+
this._limit = [first, second];
|
|
1595
1680
|
return this;
|
|
1596
1681
|
}
|
|
1597
1682
|
/**
|
|
1598
1683
|
* --- 分页 ---
|
|
1599
1684
|
* @param count 每页条数
|
|
1600
|
-
* @param page
|
|
1685
|
+
* @param page 当前页数(从 1 开始)
|
|
1601
1686
|
*/
|
|
1602
1687
|
page(count, page = 1) {
|
|
1603
|
-
const
|
|
1604
|
-
|
|
1605
|
-
|
|
1688
|
+
const size = lText.parseSafeInt(count, { 'min': 0 });
|
|
1689
|
+
const cur = lText.parseSafeInt(page, { 'min': 1 });
|
|
1690
|
+
if (size === null || cur === null) {
|
|
1691
|
+
this._sql.limit(Number.NaN);
|
|
1692
|
+
this._limit = [0, 0];
|
|
1693
|
+
return this;
|
|
1694
|
+
}
|
|
1695
|
+
const offset = size * (cur - 1);
|
|
1696
|
+
if (!Number.isSafeInteger(offset)) {
|
|
1697
|
+
this._sql.limit(Number.NaN);
|
|
1698
|
+
this._limit = [0, 0];
|
|
1699
|
+
return this;
|
|
1700
|
+
}
|
|
1701
|
+
this._sql.limit(offset, size);
|
|
1702
|
+
this._limit = [offset, size];
|
|
1606
1703
|
return this;
|
|
1607
1704
|
}
|
|
1705
|
+
/** --- 获取当前 SQL 构建错误,正常时为空字符串 --- */
|
|
1706
|
+
getError() {
|
|
1707
|
+
return this._sql.getError();
|
|
1708
|
+
}
|
|
1608
1709
|
/**
|
|
1609
1710
|
* --- 在 sql 最后追加字符串 ---
|
|
1610
1711
|
* @param sql
|
package/www/example/ctr/test.js
CHANGED
|
@@ -3112,16 +3112,16 @@ Result:<pre id="result">Nothing.</pre>`);
|
|
|
3112
3112
|
<b>getSql() :</b> ${s}<br>
|
|
3113
3113
|
<b>getData():</b> <pre>${JSON.stringify(sd, undefined, 4)}</pre>
|
|
3114
3114
|
<b>format() :</b> ${sql.format(s, sd)}<hr>`);
|
|
3115
|
-
// --- insert
|
|
3116
|
-
s = sql.insert('geo').values({ 'point':
|
|
3115
|
+
// --- insert 中使用函数(必须用 lSql.raw 显式标记,普通数组按普通值绑定) ---
|
|
3116
|
+
s = sql.insert('geo').values({ 'point': lSql.raw('ST_POINTFROMTEXT(?)', ['POINT(122.147775 30.625014)']) }).getSql();
|
|
3117
3117
|
sd = sql.getData();
|
|
3118
|
-
echo.push(`<pre>sql.insert('geo').values({ 'point':
|
|
3118
|
+
echo.push(`<pre>sql.insert('geo').values({ 'point': lSql.raw('ST_POINTFROMTEXT(?)', ['POINT(122.147775 30.625014)']) });</pre>
|
|
3119
3119
|
<b>getSql() :</b> ${s}<br>
|
|
3120
3120
|
<b>getData():</b> <pre>${JSON.stringify(sd, undefined, 4)}</pre>
|
|
3121
3121
|
<b>format() :</b> ${sql.format(s, sd)}<hr>`);
|
|
3122
3122
|
s = sql.insert('geo').values(['name', 'point', 'point2', 'polygon', 'json'], [
|
|
3123
3123
|
[
|
|
3124
|
-
'POINT A',
|
|
3124
|
+
'POINT A', lSql.raw('ST_POINTFROMTEXT(?)', ['POINT(122.147775 30.625015)']), { 'x': 1, 'y': 1 }, [
|
|
3125
3125
|
{ 'x': 1, 'y': 1 },
|
|
3126
3126
|
{ 'x': 2, 'y': 2 },
|
|
3127
3127
|
{ 'x': 3, 'y': 3 },
|
|
@@ -3130,13 +3130,13 @@ Result:<pre id="result">Nothing.</pre>`);
|
|
|
3130
3130
|
lSql.json({ 'x': { 'y': 'ghi' } })
|
|
3131
3131
|
],
|
|
3132
3132
|
[
|
|
3133
|
-
'POINT B',
|
|
3133
|
+
'POINT B', lSql.raw('ST_POINTFROMTEXT(?)', ['POINT(123.147775 30.625016)']), { 'x': 1, 'y': 1 }, null, null
|
|
3134
3134
|
]
|
|
3135
3135
|
]).getSql();
|
|
3136
3136
|
sd = sql.getData();
|
|
3137
3137
|
echo.push(`<pre>sql.insert('geo').values(['name', 'point', 'point2', 'polygon', 'json'], [
|
|
3138
3138
|
[
|
|
3139
|
-
'POINT A',
|
|
3139
|
+
'POINT A', lSql.raw('ST_POINTFROMTEXT(?)', ['POINT(122.147775 30.625015)']), { 'x': 1, 'y': 1 }, [
|
|
3140
3140
|
{ 'x': 1, 'y': 1 },
|
|
3141
3141
|
{ 'x': 2, 'y': 2 },
|
|
3142
3142
|
{ 'x': 3, 'y': 3 },
|
|
@@ -3145,7 +3145,7 @@ Result:<pre id="result">Nothing.</pre>`);
|
|
|
3145
3145
|
lSql.json({ 'x': { 'y': 'ghi' } })
|
|
3146
3146
|
],
|
|
3147
3147
|
[
|
|
3148
|
-
'POINT B',
|
|
3148
|
+
'POINT B', lSql.raw('ST_POINTFROMTEXT(?)', ['POINT(123.147775 30.625016)']), { 'x': 1, 'y': 1 }, null, null
|
|
3149
3149
|
]
|
|
3150
3150
|
]);</pre>
|
|
3151
3151
|
<b>getSql() :</b> ${s}<br>
|
|
@@ -3228,10 +3228,18 @@ Result:<pre id="result">Nothing.</pre>`);
|
|
|
3228
3228
|
<b>getSql() :</b> ${s}<br>
|
|
3229
3229
|
<b>getData():</b> <pre>${JSON.stringify(sd, undefined, 4)}</pre>
|
|
3230
3230
|
<b>format() :</b> ${sql.format(s, sd)}<hr>`);
|
|
3231
|
-
// --- 3 ---
|
|
3232
|
-
s = sql.update('user', {
|
|
3231
|
+
// --- 3:原生 SQL 表达式使用 lSql.raw() 显式标记;普通数组(即使含括号)一律按普通参数绑定 ---
|
|
3232
|
+
s = sql.update('user', {
|
|
3233
|
+
'name': 'Serene',
|
|
3234
|
+
'type': lSql.raw('(CASE `id` WHEN 1 THEN ? WHEN 2 THEN ? END)', ['val1', 'val2']),
|
|
3235
|
+
'tags': ['Item (1)']
|
|
3236
|
+
}).where({ 'name': 'Ah' }).getSql();
|
|
3233
3237
|
sd = sql.getData();
|
|
3234
|
-
echo.push(`<pre>sql.update('user', {
|
|
3238
|
+
echo.push(`<pre>sql.update('user', {
|
|
3239
|
+
'name': 'Serene',
|
|
3240
|
+
'type': lSql.raw('(CASE \`id\` WHEN 1 THEN ? WHEN 2 THEN ? END)', ['val1', 'val2']),
|
|
3241
|
+
'tags': ['Item (1)']
|
|
3242
|
+
}).where({ 'name': 'Ah' });</pre>
|
|
3235
3243
|
<b>getSql() :</b> ${s}<br>
|
|
3236
3244
|
<b>getData():</b> <pre>${JSON.stringify(sd, undefined, 4)}</pre>
|
|
3237
3245
|
<b>format() :</b> ${sql.format(s, sd)}<hr>`);
|