mas-server 2.0.0 → 2.0.1

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.
@@ -1,736 +0,0 @@
1
- import { sqlQuery } from "./esaysql";
2
- import state from "./state";
3
- import { getType } from "./utils";
4
- import c from "ansi-colors";
5
- export default class massql {
6
- /** 调试模式,0为关闭,1的时候打印sql语句,2的时候打印结果 */
7
- public debugMode = 0;
8
- public mysql = null;
9
- // ================
10
- private form = null;
11
- private formArr = {};
12
- private formArrObj = {};
13
- private lock = 0;
14
- private methods = null;
15
- // 查询变量
16
- private selectOne = 0;
17
- private page = {
18
- isPaging: false,
19
- pageNum: 1,
20
- pageSize: 20,
21
- total: 0,
22
- };
23
- private selectWhere: any = {};
24
- private whereStr = "";
25
- private keyStr = "*";
26
- private jdGetTime = 0;
27
- private isSort = 0;
28
- private sortObj = null;
29
- private sortArr = null;
30
- private isDistinct = 0;
31
- private distinctKey = "";
32
- private countName = null;
33
- private isGetdelete = 0;
34
- private joinSqlStr = "";
35
- // 自定义语句
36
- private defineSql = null;
37
- // 更新
38
- private updateOBj = null;
39
- private batchUpdateObj = null;
40
- // 插入
41
- private insertObject = null;
42
- private insertArray = null;
43
- constructor() {
44
- if (!state.sqlForm) {
45
- throw Error("请先初始化数据库!");
46
- }
47
- this.formArr = state.sqlForm;
48
- const formArr = JSON.parse(JSON.stringify(this.formArr));
49
- for (const k in formArr) {
50
- const obj = {};
51
- for (const item of formArr[k]) obj[item] = item;
52
- formArr[k] = obj;
53
- }
54
- this.formArrObj = formArr;
55
- this.mysql = state.connection;
56
- }
57
- // 条件转义
58
- escape(str: string | number): string | number {
59
- return state.connection.escape(str);
60
- }
61
- // 条件转义
62
- static escape(str: string | number): string | number {
63
- return state.connection.escape(str);
64
- }
65
- //表名转义
66
- escapeId(str: string): string {
67
- return state.connection.escapeId(str);
68
- }
69
- //表名转义
70
- static escapeId(str: string): string {
71
- return state.connection.escapeId(str);
72
- }
73
- /** 版本查看 */
74
- version() {
75
- sqlQuery("SELECT VERSION() AS SQL版本").then((sqlres) => {
76
- console.info(c.green(sqlres[0]));
77
- });
78
- return this;
79
- }
80
- /** 字段 */
81
- key(key: (string | string[])[]) {
82
- this.keyStr = key
83
- .map((item: any) => {
84
- const t = getType(item);
85
- if (t === "array") {
86
- return `${this.escapeId(item[0])} AS ${this.escapeId(item[1])}`;
87
- } else if (t === "string") {
88
- return `${this.escapeId(item)}`;
89
- } else {
90
- console.info(new Error("非法的key"));
91
- this.lock = 1;
92
- }
93
- })
94
- .join(",");
95
- return this;
96
- }
97
- /** 使用表 */
98
- use(form: string | string[]) {
99
- if (getType(form) == "array") {
100
- form = form[form.length - 1].replace("@!", "");
101
- }
102
- if (!form || !this.formArr[form as any]) {
103
- throw Error("表名不正确!");
104
- }
105
- this.form = form;
106
- return this;
107
- }
108
- /** where条件 */
109
- where(key: {} | string, value?: string | number) {
110
- if (getType(key) == "string" && !value) {
111
- console.info(Error("请传入value!"));
112
- this.lock = 1;
113
- return this;
114
- }
115
- if (getType(key) == "string") {
116
- this.whereStr += ` AND \`${key}\` = ${this.escape(value)}`;
117
- } else {
118
- for (const k in key as any) {
119
- this.whereStr += ` AND (\`${k}\` = ${this.escape(key[k])})`;
120
- }
121
- }
122
- return this;
123
- }
124
- /** 大于等于条件 */
125
- gte(key: {} | string, value?: string | number) {
126
- if (getType(key) == "string" && !value) {
127
- console.info(Error("请传入value!"));
128
- this.lock = 1;
129
- return this;
130
- }
131
- if (getType(key) == "string") {
132
- this.whereStr += ` AND \`${key}\` >= ${this.escape(value)}`;
133
- } else {
134
- for (const k in key as any) {
135
- this.whereStr += ` AND (\`${k}\` >= ${this.escape(key[k])})`;
136
- }
137
- }
138
- return this;
139
- }
140
- /** 大于条件 */
141
- gt(key: {} | string, value?: string | number) {
142
- if (getType(key) == "string" && !value) {
143
- console.info(Error("请传入value!"));
144
- this.lock = 1;
145
- return this;
146
- }
147
- if (getType(key) == "string") {
148
- this.whereStr += ` AND \`${key}\` > ${this.escape(value)}`;
149
- } else {
150
- for (const k in key as any) {
151
- this.whereStr += ` AND (\`${k}\` > ${this.escape(key[k])})`;
152
- }
153
- }
154
- return this;
155
- }
156
- /** 小于条件 */
157
- lt(key: {} | string, value?: string | number) {
158
- if (getType(key) == "string" && !value) {
159
- console.info(Error("请传入value!"));
160
- this.lock = 1;
161
- return this;
162
- }
163
- if (getType(key) == "string") {
164
- this.whereStr += ` AND \`${key}\` < ${this.escape(value)}`;
165
- } else {
166
- for (const k in key as any) {
167
- this.whereStr += ` AND (\`${k}\` < ${this.escape(key[k])})`;
168
- }
169
- }
170
- return this;
171
- }
172
- /** 小于等于条件 */
173
- lte(key: {} | string, value?: string | number) {
174
- if (getType(key) == "string" && !value) {
175
- console.info(Error("请传入value!"));
176
- this.lock = 1;
177
- return this;
178
- }
179
- if (getType(key) == "string") {
180
- this.whereStr += ` AND \`${key}\` <= ${this.escape(value)}`;
181
- } else {
182
- for (const k in key as any) {
183
- this.whereStr += ` AND (\`${k}\` <= ${this.escape(key[k])})`;
184
- }
185
- }
186
- return this;
187
- }
188
- /** where条件,用or连接 */
189
- whereOR(key: {} | string, value?: string | number) {
190
- if (getType(key) == "string" && !value) {
191
- console.info(Error("请传入value!"));
192
- this.lock = 1;
193
- return this;
194
- }
195
- if (getType(key) == "string") {
196
- this.whereStr += ` OR \`${key}\`= ${this.escape(value)}`;
197
- } else {
198
- for (const k in key as any) {
199
- this.whereStr += ` OR (\`${k}\` = ${this.escape(key[k])})`;
200
- }
201
- }
202
- return this;
203
- }
204
- /** like条件 */
205
- like(key: {} | string, value?: string | number) {
206
- if (getType(key) == "string" && !value) {
207
- console.info(Error("请传入value!"));
208
- this.lock = 1;
209
- return this;
210
- }
211
- if (getType(key) == "string") {
212
- this.whereStr += ` AND \`${key}\` LIKE ${this.escape("%" + value + "%")}`;
213
- } else {
214
- for (const k in key as any) {
215
- this.whereStr += ` AND (\`${k}\` LIKE ${this.escape(
216
- "%" + key[k] + "%"
217
- )})`;
218
- }
219
- }
220
- return this;
221
- }
222
- /** like条件 */
223
- likeOR(key: {} | string, value?: string | number) {
224
- if (getType(key) == "string" && !value) {
225
- console.info(Error("请传入value!"));
226
- this.lock = 1;
227
- return this;
228
- }
229
- if (getType(key) == "string") {
230
- this.whereStr += ` OR \`${key}\` LIKE ${this.escape("%" + value + "%")}`;
231
- } else {
232
- for (const k in key as any) {
233
- this.whereStr += ` OR (\`${k}\` LIKE ${this.escape(
234
- "%" + key[k] + "%"
235
- )})`;
236
- }
237
- }
238
- return this;
239
- }
240
- /** in条件 */
241
- in(key: string, value: string[] | number[]) {
242
- if (getType(value) != "array") {
243
- console.info(Error("请传入value或value格式错误! " + value));
244
- this.lock = 1;
245
- return this;
246
- }
247
- this.whereStr += ` AND (\`${key}\` in (${value
248
- .map((item) => this.escape(item))
249
- .join(",")}))`;
250
- return this;
251
- }
252
- /** in条件 */
253
- inOR(key: string, value?: string[] | number[]) {
254
- if (getType(value) != "array") {
255
- console.info(Error("请传入value或value格式错误! " + value));
256
- this.lock = 1;
257
- return this;
258
- }
259
- this.whereStr += ` OR (\`${key}\` in (${value
260
- .map((item) => this.escape(item))
261
- .join(",")}))`;
262
- return this;
263
- }
264
- /** 只查一个 */
265
- one() {
266
- this.selectOne = 1;
267
- return this;
268
- }
269
- /** 分页,在select时传入pageNum和pageSize */
270
- paging() {
271
- this.page.isPaging = true;
272
- return this;
273
- }
274
- /** 获得时间 */
275
- getTime() {
276
- this.jdGetTime = 1;
277
- return this;
278
- }
279
- /** 排序方法,mode中 DESC:降序,最新(大)的放前面 | ASC:升序,最早(小)的前面 */
280
- sort(
281
- obj: string | { key: string; mode: "DESC" | "ASC" }[],
282
- mode: "DESC" | "ASC" = "DESC"
283
- ) {
284
- const objT = getType(obj);
285
- if (objT == "string") {
286
- this.isSort = 1;
287
- this.sortObj = { key: obj, mode: mode };
288
- } else if (objT == "array") {
289
- this.isSort = 1;
290
- this.sortArr = obj;
291
- } else {
292
- console.info(Error("排序参数错误!"));
293
- this.lock = 1;
294
- }
295
- return this;
296
- }
297
- /** 统计数量 */
298
- count(name: string) {
299
- this.methods = "count";
300
- if (getType(name) != "string") {
301
- console.info(Error("统计参数错误!"));
302
- this.lock = 1;
303
- return this;
304
- }
305
- this.countName = name;
306
- return this;
307
- }
308
- /** 去重 */
309
- distinct(key: string) {
310
- this.methods = "distinct";
311
- this.isDistinct = 1;
312
- const keyT = getType(key);
313
- if (keyT == "string") {
314
- this.distinctKey = key;
315
- } else {
316
- console.info(Error("去重参数错误!"));
317
- this.lock = 1;
318
- }
319
- return this;
320
- }
321
- /** 连表 */
322
- leftJoin(joinFormName: string, joinFormKey: string, key: string) {
323
- this.joinSqlStr += `LEFT JOIN ${this.escapeId(
324
- joinFormName
325
- )} ON ${this.escapeId(joinFormName)}.${this.escapeId(
326
- joinFormKey
327
- )} = ${this.escapeId(this.form)}.${this.escapeId(key)}\n`;
328
- return this;
329
- }
330
- /** 内连表 */
331
- innerJoin(joinFormName: string, joinFormKey: string, key: string) {
332
- this.joinSqlStr += `INNER JOIN ${this.escapeId(
333
- joinFormName
334
- )} ON ${this.escapeId(joinFormName)}.${this.escapeId(
335
- joinFormKey
336
- )} = ${this.escapeId(this.form)}.${this.escapeId(key)}\n`;
337
- return this;
338
- }
339
- /** 右连表 */
340
- rightJoin(joinFormName: string, joinFormKey: string, key: string) {
341
- this.joinSqlStr += `RIGHT JOIN ${this.escapeId(
342
- joinFormName
343
- )} ON ${this.escapeId(joinFormName)}.${this.escapeId(
344
- joinFormKey
345
- )} = ${this.escapeId(this.form)}.${this.escapeId(key)}\n`;
346
- return this;
347
- }
348
-
349
- /**
350
- *
351
- * @param data 为string和number时,通过id查询,为数组时通过id批量查询,为json时,通过and关联查询
352
- * @param likeSearch dataObj为json时是否进行模糊查询,默认开启
353
- * @param filter dataObj为json时是否对其进行过滤,默认开启
354
- */
355
- select(
356
- dataObj?: [] | {} | string | number,
357
- likeSearch: 1 | 0 = 1,
358
- filter: 0 | 1 = 1
359
- ) {
360
- this.methods = "select";
361
- if (!dataObj) {
362
- return this;
363
- }
364
- let data: any = dataObj;
365
- const dataT = getType(data);
366
-
367
- if (dataT == "array") {
368
- // 为id数组情况,把id的string都转为number
369
- data = data
370
- .map((item) => parseInt(item))
371
- .filter((item) => !Number.isNaN(item));
372
- this.whereStr += ` AND (\`id\` in (${data.join(",")}))`;
373
- } else if (dataT == "string" || dataT == "number") {
374
- // 为id的情况,把id的string转为number
375
- this.whereStr += ` AND (\`id\`= ${parseInt(data)})`;
376
- } else if (dataT == "object") {
377
- // 为object的情况,默认为模糊搜索,且id不能模糊搜索
378
- this.selectWhere = dataObj;
379
- data = JSON.parse(JSON.stringify(data));
380
- let symbol = "=";
381
- let pct = "";
382
- likeSearch && (symbol = "LIKE");
383
- likeSearch && (pct = "%");
384
- //是否进行字段过滤
385
- if (filter) {
386
- if (!this.form) throw Error("请把use放置在select前!");
387
- for (const k in data) {
388
- if (!this.formArrObj[this.form][k]) continue;
389
- if (k == "id") data[k] = parseInt(data[k]);
390
- this.whereStr += ` AND (\`${k}\` ${
391
- k == "id" ? "=" : symbol
392
- } ${this.escape(pct + data[k] + pct)})`;
393
- }
394
- } else {
395
- for (const k in data) {
396
- //不进行字段过滤,分页参数也不能要
397
- if (k == "pageSize" || k == "pageNum") continue;
398
- if (k == "id") data[k] = parseInt(data[k]);
399
- this.whereStr += ` AND (\`${k}\` ${
400
- k == "id" ? "=" : symbol
401
- } ${this.escape(pct + data[k] + pct)})`;
402
- }
403
- }
404
- }
405
- return this;
406
- }
407
- getDelete() {
408
- this.isGetdelete = 1;
409
- return this;
410
- }
411
- /** 自定义语句 */
412
- define(sqlStr) {
413
- this.defineSql = sqlStr;
414
- return this;
415
- }
416
- /** 更新 */
417
- update(obj: object, id: string | number | (string | number[])) {
418
- if (!obj) {
419
- console.info(Error(`没有传入更新数据 ${obj}`));
420
- this.lock = 1;
421
- } else {
422
- this.updateOBj = obj;
423
- }
424
- this.methods = "update";
425
- const idT = getType(id);
426
- if (idT == "string") {
427
- id = parseInt(id as any);
428
- this.where("id", id);
429
- } else if (idT == "array") {
430
- this.in("id", id as any);
431
- } else if (idT == "number") {
432
- this.where("id", id as any);
433
- } else {
434
- this.lock = 1;
435
- console.log(Error(`更新id错误: id为 ${id}`));
436
- }
437
- return this;
438
- }
439
- /** 批量更新 */
440
- batchUpdate(obj: {}[]) {
441
- this.methods = "batchUpdate";
442
- if (getType(obj) == "array") {
443
- if (obj.length == 0) {
444
- console.info(Error(`更新数据不能为空 ${obj}`));
445
- this.lock = 1;
446
- }
447
- } else {
448
- console.info(Error(`更新数据格式错误 ${obj}`));
449
- this.lock = 1;
450
- }
451
- this.batchUpdateObj = obj;
452
- }
453
- /** set方法,有id则更新,无id则插入 */
454
- set(obj: {} | {}[]) {
455
- const objT = getType(obj);
456
- if (!obj) {
457
- console.info(Error(`没有传入数据 ${obj}`));
458
- this.lock = 1;
459
- } else if (objT == "array") {
460
- if (obj[0].id) {
461
- // this.update()
462
- this.batchUpdate(obj as any);
463
- } else {
464
- this.insert(obj);
465
- }
466
- } else if (objT == "object") {
467
- const id = (obj as any).id;
468
- if (id) {
469
- this.update(obj, id);
470
- } else {
471
- this.insert(obj);
472
- }
473
- } else {
474
- console.info(Error(`数据格式错误 ${obj}`));
475
- this.lock = 1;
476
- }
477
- return this;
478
- }
479
- /** 插入 */
480
- insert(obj: {} | {}[]) {
481
- this.methods = "insert";
482
- const objT = getType(obj);
483
- if (objT == "array") {
484
- this.insertArray = obj;
485
- } else if (objT == "object") {
486
- this.insertObject = obj;
487
- } else {
488
- this.lock = 1;
489
- console.info(Error(`插入数据错误 ${obj}`));
490
- }
491
- return this;
492
- }
493
- /** 删除 */
494
- delete(id: string | number | (string | number)[]) {
495
- this.methods = "delete";
496
- const idT = getType(id);
497
- if (idT == "string") {
498
- id = parseInt(id as any);
499
- this.where("id", id);
500
- } else if (idT == "array") {
501
- this.in("id", id as any);
502
- } else if (idT == "number") {
503
- this.where("id", id as any);
504
- } else {
505
- this.lock = 1;
506
- console.log(Error(`更新id错误: id为 ${id}`));
507
- }
508
- return this;
509
- }
510
- buildBatchUpdateSQL(tableName, updateRecords) {
511
- // 获取要更新的字段和对应的值
512
- const fields = Object.keys(updateRecords[0]).filter(
513
- (field) => field !== "id"
514
- );
515
-
516
- // 构建 SQL 语句
517
- const sql = `UPDATE ${tableName} SET ${fields
518
- .map(
519
- (field) =>
520
- `${this.escapeId(field)} = CASE id ${updateRecords
521
- .map(
522
- (record) =>
523
- `WHEN ${record.id ?? "-1"} THEN ${
524
- record[field]
525
- ? this.escape(record[field])
526
- : this.escapeId(field)
527
- }`
528
- )
529
- .join(" ")} END`
530
- )
531
- .join(", ")} WHERE id IN (${updateRecords
532
- .map((record) => parseInt(record.id ?? "-1"))
533
- .join(",")})`;
534
-
535
- return sql;
536
- }
537
- private _init() {
538
- // ================
539
- this.form = null;
540
- this.lock = 0;
541
- this.methods = null;
542
- // 查询变量
543
- this.selectOne = 0;
544
- this.page = {
545
- isPaging: false,
546
- pageNum: 1,
547
- pageSize: 20,
548
- total: 0,
549
- };
550
- this.selectWhere = {};
551
- this.whereStr = "";
552
- this.keyStr = "*";
553
- this.jdGetTime = 0;
554
- this.isSort = 0;
555
- this.sortObj = null;
556
- this.sortArr = null;
557
- this.isDistinct = 0;
558
- this.distinctKey = "";
559
- this.isGetdelete = 0;
560
- this.countName = null;
561
- this.joinSqlStr = "";
562
- // 自定义语句
563
- this.defineSql = null;
564
- // 更新
565
- this.updateOBj = null;
566
- this.batchUpdateObj = null;
567
- // 插入
568
- this.insertObject = null;
569
- this.insertArray = null;
570
- }
571
- private batchUpdateGetSql() {
572
- return this.buildBatchUpdateSQL(this.form, this.batchUpdateObj);
573
- }
574
- private selectGetSql() {
575
- let limit = "";
576
- let sort = "";
577
- // 是否获取时间
578
- if (this.jdGetTime && this.whereStr != "*") {
579
- this.keyStr += ",`update_time`,`create_time`";
580
- }
581
- // 是否分页
582
- if (this.page.isPaging) {
583
- this.selectWhere.pageNum &&
584
- (this.page.pageNum = parseInt(this.selectWhere.pageNum));
585
- this.selectWhere.pageSize &&
586
- (this.page.pageSize = parseInt(this.selectWhere.pageSize));
587
- const start = (this.page.pageNum - 1) * this.page.pageSize;
588
- limit = `LIMIT ${start},${this.page.pageSize}`;
589
- }
590
- // 是否排序
591
- if (this.isSort == 1) {
592
- if (this.sortObj) {
593
- sort = `ORDER BY ${this.sortObj.key} ${this.sortObj.mode}`;
594
- } else if (this.sortArr) {
595
- sort = `ORDER BY ${this.sortArr
596
- .map((item) => `${this.escapeId(item.key)} ${item.mode}`)
597
- .join(",")}`;
598
- }
599
- }
600
- const WHERE = this.whereStr == "" ? "" : ` AND (${this.whereStr.slice(5)})`;
601
- return `SELECT ${this.keyStr} from ${this.escapeId(this.form)} ${
602
- this.joinSqlStr
603
- } WHERE ${
604
- this.isGetdelete == 0
605
- ? `${this.escapeId(this.form)}.is_delete = 0`
606
- : "1=1"
607
- } ${WHERE} ${limit} ${sort}`;
608
- }
609
- private updateGetSql() {
610
- const WHERE = this.whereStr == "" ? "" : ` AND (${this.whereStr.slice(5)})`;
611
- let updateStr = "";
612
- for (let k in this.updateOBj) {
613
- if (!this.formArrObj[this.form][k] || k == "id") continue;
614
- updateStr += `${this.escapeId(k)} = ${this.escape(this.updateOBj[k])},`;
615
- }
616
- return `UPDATE ${this.escapeId(this.form)} SET ${updateStr.slice(
617
- 0,
618
- updateStr.length - 1
619
- )} WHERE is_delete = 0 ${WHERE};`;
620
- }
621
- private insertGetSql() {
622
- // 只插入一个,进行字段效验
623
- if (this.insertObject) {
624
- const KeyStr = [];
625
- const ValueStr = [];
626
- for (let k in this.insertObject) {
627
- if (!this.formArrObj[this.form][k]) continue;
628
- KeyStr.push(this.escapeId(k));
629
- ValueStr.push(this.escape(this.insertObject[k]));
630
- }
631
- return `INSERT INTO ${this.form} (${KeyStr.join(
632
- ","
633
- )}) VALUES (${ValueStr.join(",")});`;
634
- } else if (this.insertArray) {
635
- // 批量插入,不进行字段效验
636
- const KeyStr = [];
637
- const valueArr = [];
638
- if (this.insertArray[0]) {
639
- for (let k in this.insertArray[0]) {
640
- KeyStr.push(this.escapeId(k));
641
- }
642
- }
643
- for (let item in this.insertArray) {
644
- const ValueStr = [];
645
- for (let k in this.insertArray[item]) {
646
- ValueStr.push(this.escape(this.insertArray[item][k]));
647
- }
648
- valueArr.push(ValueStr);
649
- }
650
- return `INSERT INTO ${this.form} (${KeyStr.join(",")}) VALUES ${valueArr
651
- .map((vs) => `(${vs.join(",")})`)
652
- .join(",")};`;
653
- }
654
- }
655
- private deleteGetSql() {
656
- const WHERE = this.whereStr == "" ? "" : `(${this.whereStr.slice(5)})`;
657
- return `UPDATE ${this.escapeId(
658
- this.form
659
- )} SET is_delete = 1 where ${WHERE};`;
660
- }
661
- private distinctGetSql() {
662
- const WHERE = this.whereStr == "" ? "" : ` AND (${this.whereStr.slice(5)})`;
663
- return `SELECT DISTINCT ${this.escapeId(this.distinctKey)} FROM ${
664
- this.form
665
- } WHERE ${this.isGetdelete == 0 ? `is_delete = 0` : "1=1"} ${WHERE}`;
666
- }
667
- private countGetSql() {
668
- const WHERE = this.whereStr == "" ? "" : ` AND (${this.whereStr.slice(5)})`;
669
- return `SELECT COUNT(*) AS ${this.escapeId(
670
- this.countName
671
- )} FROM ${this.escapeId(this.form)} WHERE ${
672
- this.isGetdelete == 0 ? `is_delete = 0` : "1=1"
673
- } ${WHERE};`;
674
- }
675
- /**
676
- * 执行
677
- * @param mode 0执行,-2返回sql语句,-1打印sql语句
678
- */
679
- async go(mode: 0 | -1 | -2 = 0) {
680
- if (this.lock == 1) return null;
681
- let sqlStr = "";
682
- if (this.defineSql) {
683
- sqlStr = this.defineSql;
684
- } else {
685
- if (!this.methods) {
686
- throw Error("请选择方法");
687
- }
688
- if (!this.form) {
689
- throw Error("请选择表");
690
- }
691
- sqlStr = this[`${this.methods}GetSql`]();
692
- }
693
-
694
- if (this.debugMode) {
695
- console.info(c.greenBright("SQL: " + sqlStr));
696
- }
697
- if (mode == -1) {
698
- this._init();
699
- console.info(sqlStr);
700
- } else if (mode == -2) {
701
- this._init();
702
- return sqlStr;
703
- } else {
704
- return await this.runSql(sqlStr);
705
- }
706
- }
707
- private async runSql(sqlStr) {
708
- let sqlRes = (await sqlQuery(sqlStr)) ?? null;
709
-
710
- if (this.page.isPaging) {
711
- const total = await sqlQuery(
712
- `SELECT COUNT(*) as \`total\` FROM ${this.form}`
713
- );
714
- sqlRes = {
715
- list: sqlRes,
716
- pageNum: this.page.pageNum,
717
- pageSize: this.page.pageSize,
718
- total: total[0]["total"],
719
- };
720
- }
721
- if (this.isDistinct == 1 && sqlRes) {
722
- sqlRes = {
723
- [this.distinctKey]: (sqlRes as any).map(
724
- (item) => item[this.distinctKey]
725
- ),
726
- };
727
- }
728
- if (this.selectOne == 1) {
729
- if (this.page.isPaging) throw Error("分页和单个查询不能同时使用!");
730
- sqlRes = sqlRes[0];
731
- }
732
- this.debugMode === 2 && console.info(sqlRes, " SQL RESULT");
733
- this._init();
734
- return sqlRes;
735
- }
736
- }