@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/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* --- 本文件用来定义每个目录实体地址的常量 ---
|
|
5
5
|
*/
|
|
6
6
|
/** --- 当前系统版本号 --- */
|
|
7
|
-
export const VER = '
|
|
7
|
+
export const VER = '11.0.0';
|
|
8
8
|
// --- 服务端用的路径 ---
|
|
9
9
|
const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
|
|
10
10
|
/** --- /xxx/xxx --- */
|
package/lib/db/conn.js
CHANGED
|
@@ -155,6 +155,17 @@ export class Connection {
|
|
|
155
155
|
'error': null,
|
|
156
156
|
'result': 1,
|
|
157
157
|
};
|
|
158
|
+
if (!sql?.trim()) {
|
|
159
|
+
rtn.error = {
|
|
160
|
+
'message': 'Empty SQL',
|
|
161
|
+
'errno': 0,
|
|
162
|
+
};
|
|
163
|
+
rtn.result = -500;
|
|
164
|
+
if (!this._transaction) {
|
|
165
|
+
this._using = false;
|
|
166
|
+
}
|
|
167
|
+
return rtn;
|
|
168
|
+
}
|
|
158
169
|
try {
|
|
159
170
|
values = lSqlValue.serializeList(values);
|
|
160
171
|
this.refreshLast();
|
|
@@ -213,6 +224,17 @@ export class Connection {
|
|
|
213
224
|
'error': null,
|
|
214
225
|
'result': 1,
|
|
215
226
|
};
|
|
227
|
+
if (!sql?.trim()) {
|
|
228
|
+
rtn.error = {
|
|
229
|
+
'message': 'Empty SQL',
|
|
230
|
+
'errno': 0,
|
|
231
|
+
};
|
|
232
|
+
rtn.result = -500;
|
|
233
|
+
if (!this._transaction) {
|
|
234
|
+
this._using = false;
|
|
235
|
+
}
|
|
236
|
+
return rtn;
|
|
237
|
+
}
|
|
216
238
|
try {
|
|
217
239
|
values = lSqlValue.serializeList(values);
|
|
218
240
|
this.refreshLast();
|
package/lib/sql.d.ts
CHANGED
|
@@ -22,6 +22,15 @@ export declare enum EJSON {
|
|
|
22
22
|
/** --- 简单数组重叠 (MySQL: JSON_OVERLAPS, PG: &&) --- */
|
|
23
23
|
'OVERLAPS' = "json_overlaps"
|
|
24
24
|
}
|
|
25
|
+
/** --- raw() 内部私有标记,防止通过外部 JSON 伪造原始 SQL 表达式 --- */
|
|
26
|
+
declare const rawSymbol: unique symbol;
|
|
27
|
+
/** --- 显式标记的原始 SQL 表达式及其绑定参数 --- */
|
|
28
|
+
export interface IRawValue {
|
|
29
|
+
readonly [rawSymbol]: true;
|
|
30
|
+
readonly 'sql': string;
|
|
31
|
+
readonly 'params': kebab.DbValue[];
|
|
32
|
+
readonly 'invalid'?: string;
|
|
33
|
+
}
|
|
25
34
|
export declare class Sql {
|
|
26
35
|
/** --- ctr 对象 --- */
|
|
27
36
|
private readonly _ctr?;
|
|
@@ -41,6 +50,10 @@ export declare class Sql {
|
|
|
41
50
|
private _ignore;
|
|
42
51
|
/** --- MySQL 优化器 Hint,如 INDEX(table idx) --- */
|
|
43
52
|
private _hint;
|
|
53
|
+
/** --- 当前构建器的粘性错误;非空时 getSql() 返回空串、getData() 返回空数组 --- */
|
|
54
|
+
private _error;
|
|
55
|
+
/** --- hint() 独立错误,避免先 hint() 再 select() 时被重置覆盖 --- */
|
|
56
|
+
private _hintError;
|
|
44
57
|
constructor(opt: {
|
|
45
58
|
'service': ESERVICE;
|
|
46
59
|
'ctr'?: ctr.Ctr;
|
|
@@ -50,6 +63,10 @@ export declare class Sql {
|
|
|
50
63
|
'sql'?: string[];
|
|
51
64
|
'alias'?: string[];
|
|
52
65
|
});
|
|
66
|
+
/** --- 记录构建错误并写入错误日志,保持首个错误不被覆盖 --- */
|
|
67
|
+
private _setError;
|
|
68
|
+
/** --- 获取当前构建错误,正常时为空字符串 --- */
|
|
69
|
+
getError(): string;
|
|
53
70
|
/**
|
|
54
71
|
* --- 设置 MySQL 优化器 Hint ---
|
|
55
72
|
* --- 会以 Optimizer Hint 注释语法注入到 SELECT 关键字后 ---
|
|
@@ -115,6 +132,12 @@ export declare class Sql {
|
|
|
115
132
|
* @param s 设定 update 的值
|
|
116
133
|
*/
|
|
117
134
|
update(f: string, s: kebab.Json): this;
|
|
135
|
+
/**
|
|
136
|
+
* --- 生成更新字段列表及其绑定参数 ---
|
|
137
|
+
* @param s 更新值
|
|
138
|
+
* @param table 已格式化的目标表名,UPSERT 从 INSERT 前导取表名
|
|
139
|
+
* @returns SET 子句中的赋值列表
|
|
140
|
+
*/
|
|
118
141
|
private _updateSub;
|
|
119
142
|
/**
|
|
120
143
|
* --- 'xx' ---
|
|
@@ -201,6 +224,33 @@ export declare class Sql {
|
|
|
201
224
|
* @param s 筛选数据
|
|
202
225
|
*/
|
|
203
226
|
where(s: string | kebab.Json): this;
|
|
227
|
+
/**
|
|
228
|
+
* --- 统计条件项数量,决定逻辑分组内是否需要外层括号 ---
|
|
229
|
+
* @param cond 条件对象或数组
|
|
230
|
+
* @returns 条件条数
|
|
231
|
+
*/
|
|
232
|
+
private _countConditions;
|
|
233
|
+
/**
|
|
234
|
+
* --- 生成单条数组条件 SQL 片段 ---
|
|
235
|
+
* @param item 数组条件项
|
|
236
|
+
* @param data 绑定参数列表
|
|
237
|
+
* @returns 带尾随 AND 的 SQL 片段,校验失败返回空字符串
|
|
238
|
+
*/
|
|
239
|
+
private _whereArrayItem;
|
|
240
|
+
/**
|
|
241
|
+
* --- 生成单个对象键的条件 SQL 片段 ---
|
|
242
|
+
* @param key 字段名或 $or/$and 逻辑分组键
|
|
243
|
+
* @param val 条件值
|
|
244
|
+
* @param data 绑定参数列表
|
|
245
|
+
* @returns 带尾随 AND 的 SQL 片段,校验失败或空分组返回空字符串
|
|
246
|
+
*/
|
|
247
|
+
private _whereObjectEntry;
|
|
248
|
+
/**
|
|
249
|
+
* --- 生成 WHERE / HAVING / ON 条件子句 ---
|
|
250
|
+
* @param s 条件对象或条件数组
|
|
251
|
+
* @param data 接收绑定参数的数组,默认使用当前实例的 _data
|
|
252
|
+
* @returns 条件 SQL 片段,无有效条件时返回空字符串
|
|
253
|
+
*/
|
|
204
254
|
private _whereSub;
|
|
205
255
|
/**
|
|
206
256
|
* --- 生成集合比较条件,按迭代顺序追加参数 ---
|
|
@@ -223,7 +273,7 @@ export declare class Sql {
|
|
|
223
273
|
* @param c 排序字段,支持表别名;不支持表达式、GROUP BY 或 UNION
|
|
224
274
|
* @param values 上一页末行的字段值,省略时读取首批
|
|
225
275
|
* @param d 扫描方向;读取前一批时反转方向并在取回后反转列表
|
|
226
|
-
* @returns 当前 SQL
|
|
276
|
+
* @returns 当前 SQL 对象;参数非法时记录构建错误并失败关闭
|
|
227
277
|
*/
|
|
228
278
|
seek(c: string | string[], values?: TCursorValue[], d?: 'DESC' | 'ASC'): this;
|
|
229
279
|
/**
|
|
@@ -233,7 +283,7 @@ export declare class Sql {
|
|
|
233
283
|
group(c: string | string[]): this;
|
|
234
284
|
/**
|
|
235
285
|
* --- LIMIT(limit、offset, limit) ---
|
|
236
|
-
* @param a 起始(offset)
|
|
286
|
+
* @param a 起始(offset)或仅传单参时的长度(limit)
|
|
237
287
|
* @param b 长度(limit)
|
|
238
288
|
*/
|
|
239
289
|
limit(a: number, b?: number): this;
|
|
@@ -253,11 +303,11 @@ export declare class Sql {
|
|
|
253
303
|
'limit'?: false;
|
|
254
304
|
}): Sql;
|
|
255
305
|
/**
|
|
256
|
-
* --- 获取 sql
|
|
306
|
+
* --- 获取 sql 语句;存在构建错误时返回空字符串 ---
|
|
257
307
|
*/
|
|
258
308
|
getSql(): string;
|
|
259
309
|
/**
|
|
260
|
-
* --- 获取全部 data ---
|
|
310
|
+
* --- 获取全部 data;存在构建错误时返回空数组 ---
|
|
261
311
|
*/
|
|
262
312
|
getData(): kebab.DbValue[];
|
|
263
313
|
/**
|
|
@@ -292,11 +342,21 @@ export declare class Sql {
|
|
|
292
342
|
* @param arg
|
|
293
343
|
*/
|
|
294
344
|
private _isValue;
|
|
345
|
+
/**
|
|
346
|
+
* --- 判断传入值是否是 raw 表达式对象(由 raw() 函数创建) ---
|
|
347
|
+
* @param arg 待判断的值
|
|
348
|
+
*/
|
|
349
|
+
private _isRaw;
|
|
295
350
|
/** --- 获取占位符 --- */
|
|
296
351
|
private _placeholder;
|
|
297
352
|
/**
|
|
298
|
-
* ---
|
|
353
|
+
* --- 生成单个值的 SQL 片段和绑定参数 ---
|
|
299
354
|
* @param v 要处理的值
|
|
355
|
+
* @param field 写入字段名
|
|
356
|
+
* @param row 批量写入行号,从 1 开始
|
|
357
|
+
* @param table 已格式化的 UPDATE 目标表名,INSERT 从前导取表名
|
|
358
|
+
* @param parameter SQL 表达式内的参数位置,从 1 开始,嵌套时逐级记录
|
|
359
|
+
* @returns SQL 片段和绑定参数,异常值沿用空字符串
|
|
300
360
|
*/
|
|
301
361
|
private _processValue;
|
|
302
362
|
}
|
|
@@ -346,3 +406,12 @@ export declare function value(val: kebab.DbValue): {
|
|
|
346
406
|
* @returns 类型保持不变的 JSON 包装值
|
|
347
407
|
*/
|
|
348
408
|
export declare function json<T>(obj: T): T;
|
|
409
|
+
/**
|
|
410
|
+
* --- 创建显式 SQL 表达式值,用于 INSERT / UPDATE 写入原生 SQL 函数或 CASE 表达式 ---
|
|
411
|
+
* --- 使用内部私有 Symbol 标记,外部 JSON 无法伪造;普通数组不再自动识别为 SQL 表达式 ---
|
|
412
|
+
* @param sql 原生 SQL 表达式,参数位置使用 ? 占位
|
|
413
|
+
* @param params 绑定参数列表
|
|
414
|
+
* @returns SQL 表达式包装对象
|
|
415
|
+
*/
|
|
416
|
+
export declare function raw(sql: string, params?: kebab.DbValue[]): IRawValue;
|
|
417
|
+
export {};
|