baja-lite 1.0.7 → 1.0.16
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/cjs/boot-remote.js +4 -0
- package/cjs/boot.js +4 -0
- package/cjs/code.js +27 -12
- package/cjs/convert-xml.js +5 -1
- package/cjs/enum.d.ts +8 -0
- package/cjs/enum.js +33 -1
- package/cjs/fn.js +45 -35
- package/cjs/list.js +24 -0
- package/cjs/math.d.ts +14 -0
- package/cjs/math.js +39 -0
- package/cjs/set-ex.d.ts +8 -7
- package/cjs/set-ex.js +51 -58
- package/cjs/sql.d.ts +91 -34
- package/cjs/sql.js +382 -227
- package/cjs/sqlite.d.ts +5 -8
- package/cjs/sqlite.js +25 -22
- package/cjs/test-mysql.js +29 -21
- package/es/boot-remote.js +5 -1
- package/es/boot.js +5 -1
- package/es/code.js +27 -12
- package/es/convert-xml.js +2 -1
- package/es/enum.d.ts +8 -0
- package/es/enum.js +31 -0
- package/es/fn.js +45 -35
- package/es/list.js +24 -0
- package/es/math.d.ts +14 -0
- package/es/math.js +37 -0
- package/es/set-ex.d.ts +8 -7
- package/es/set-ex.js +48 -58
- package/es/sql.d.ts +91 -34
- package/es/sql.js +383 -228
- package/es/sqlite.d.ts +5 -8
- package/es/sqlite.js +26 -23
- package/es/test-mysql.js +30 -22
- package/package.json +70 -70
- package/src/boot-remote.ts +5 -1
- package/src/boot.ts +5 -1
- package/src/code.ts +45 -14
- package/src/convert-xml.ts +2 -2
- package/src/enum.ts +43 -3
- package/src/fn.ts +41 -33
- package/src/list.ts +27 -1
- package/src/math.ts +41 -3
- package/src/set-ex.ts +49 -58
- package/src/sql.ts +391 -217
- package/src/sqlite.ts +26 -20
- package/src/test-mysql.ts +30 -28
package/es/sql.js
CHANGED
|
@@ -7,8 +7,8 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
7
7
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
9
|
};
|
|
10
|
-
var
|
|
11
|
-
var
|
|
10
|
+
var _b, _c, _d;
|
|
11
|
+
var _e, _f, _g, _h;
|
|
12
12
|
import { Throw } from './error';
|
|
13
13
|
import tslib from 'tslib';
|
|
14
14
|
import Sqlstring from 'sqlstring';
|
|
@@ -16,14 +16,41 @@ import iterare from 'iterare';
|
|
|
16
16
|
import { emptyString } from './string';
|
|
17
17
|
import pino from 'pino';
|
|
18
18
|
import { excuteSplit, ExcuteSplitMode, sleep } from './fn';
|
|
19
|
-
import { add, calc } from './math';
|
|
19
|
+
import { add, calc, ten2Any } from './math';
|
|
20
20
|
import mustache from 'mustache';
|
|
21
21
|
import { C2P, C2P2, P2C } from './object';
|
|
22
|
-
import {
|
|
22
|
+
import { formatDialect, sqlite, mysql } from 'sql-formatter';
|
|
23
23
|
import HTML from 'html-parse-stringify';
|
|
24
24
|
import { convert } from './convert-xml';
|
|
25
25
|
import { ArrayList } from './list';
|
|
26
26
|
import LGet from 'lodash.get';
|
|
27
|
+
import { encode, decode, ExtensionCodec, DecodeError } from "@msgpack/msgpack";
|
|
28
|
+
BigInt.prototype.toJSON = function () { return this.toString(); };
|
|
29
|
+
const BIGINT_EXT_TYPE = 0;
|
|
30
|
+
export const extensionCodec = new ExtensionCodec();
|
|
31
|
+
extensionCodec.register({
|
|
32
|
+
type: BIGINT_EXT_TYPE,
|
|
33
|
+
encode(input) {
|
|
34
|
+
if (typeof input === "bigint") {
|
|
35
|
+
if (input <= Number.MAX_SAFE_INTEGER && input >= Number.MIN_SAFE_INTEGER) {
|
|
36
|
+
return encode(Number(input));
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
return encode(String(input));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
decode(data) {
|
|
47
|
+
const val = decode(data);
|
|
48
|
+
if (!(typeof val === "string" || typeof val === "number")) {
|
|
49
|
+
throw new DecodeError(`unexpected BigInt source: ${val} (${typeof val})`);
|
|
50
|
+
}
|
|
51
|
+
return BigInt(val);
|
|
52
|
+
},
|
|
53
|
+
});
|
|
27
54
|
// #region 常量
|
|
28
55
|
const _daoDBName = Symbol('dbName');
|
|
29
56
|
const _tableName = Symbol('tableName');
|
|
@@ -31,6 +58,7 @@ const _className = Symbol('className');
|
|
|
31
58
|
const _ClassName = Symbol('ClassName');
|
|
32
59
|
const _vueName = Symbol('vueName');
|
|
33
60
|
const _ids = Symbol('ids');
|
|
61
|
+
const _logicIds = Symbol('logicIds');
|
|
34
62
|
const _columns = Symbol('columns');
|
|
35
63
|
const _columnsNoId = Symbol('columnsNoId');
|
|
36
64
|
const _fields = Symbol('fields');
|
|
@@ -39,10 +67,12 @@ const _deleteState = Symbol('deleteState');
|
|
|
39
67
|
const _transformer = Symbol('transformer');
|
|
40
68
|
const _index = Symbol('index');
|
|
41
69
|
const _def = Symbol('def');
|
|
70
|
+
const _comment = Symbol('comment');
|
|
42
71
|
export const _sqlCache = Symbol('sqlMap');
|
|
43
72
|
export const _dao = Symbol('dao');
|
|
44
73
|
export const _primaryDB = Symbol('primaryDB');
|
|
45
74
|
const _dbType = Symbol('dbType');
|
|
75
|
+
const _formatDialect = Symbol('FormatDialect');
|
|
46
76
|
const _sqlite_version = Symbol('sqlite_version');
|
|
47
77
|
const _daoConnection = Symbol('daoConnection');
|
|
48
78
|
const _inTransaction = Symbol('inTransaction');
|
|
@@ -51,6 +81,7 @@ const _sqliteRemoteName = Symbol('sqliteRemoteName');
|
|
|
51
81
|
const _SqlOption = Symbol('SqlOption');
|
|
52
82
|
const _resultMap = Symbol('resultMap');
|
|
53
83
|
const _resultMap_SQLID = Symbol('resultMap_SQLID');
|
|
84
|
+
export const _enums = Symbol('_enums');
|
|
54
85
|
export const _Hump = Symbol('Hump');
|
|
55
86
|
export const _GlobalSqlOption = Symbol('GlobalSqlOption');
|
|
56
87
|
export const _EventBus = Symbol('EventBus');
|
|
@@ -235,7 +266,7 @@ export const _defOption = {
|
|
|
235
266
|
;
|
|
236
267
|
class MysqlConnection {
|
|
237
268
|
constructor(conn) {
|
|
238
|
-
this[
|
|
269
|
+
this[_b] = false;
|
|
239
270
|
this[_daoConnection] = conn;
|
|
240
271
|
}
|
|
241
272
|
execute(sync, sql, params) {
|
|
@@ -418,7 +449,7 @@ class MysqlConnection {
|
|
|
418
449
|
;
|
|
419
450
|
}
|
|
420
451
|
}
|
|
421
|
-
|
|
452
|
+
_b = _inTransaction;
|
|
422
453
|
export class Mysql {
|
|
423
454
|
constructor(pool) {
|
|
424
455
|
this[_daoDB] = pool;
|
|
@@ -509,7 +540,7 @@ export class Mysql {
|
|
|
509
540
|
}
|
|
510
541
|
class SqliteConnection {
|
|
511
542
|
constructor(conn) {
|
|
512
|
-
this[
|
|
543
|
+
this[_c] = false;
|
|
513
544
|
this[_daoConnection] = conn;
|
|
514
545
|
}
|
|
515
546
|
execute(sync, sql, params) {
|
|
@@ -649,7 +680,7 @@ class SqliteConnection {
|
|
|
649
680
|
realse(sync) {
|
|
650
681
|
}
|
|
651
682
|
}
|
|
652
|
-
|
|
683
|
+
_c = _inTransaction;
|
|
653
684
|
export class Sqlite {
|
|
654
685
|
constructor(db) {
|
|
655
686
|
this[_daoDB] = db;
|
|
@@ -714,7 +745,7 @@ export class Sqlite {
|
|
|
714
745
|
}
|
|
715
746
|
export class SqliteRemoteConnection {
|
|
716
747
|
constructor(conn, name) {
|
|
717
|
-
this[
|
|
748
|
+
this[_d] = false;
|
|
718
749
|
this[_daoConnection] = conn;
|
|
719
750
|
this[_sqliteRemoteName] = name;
|
|
720
751
|
}
|
|
@@ -734,7 +765,8 @@ export class SqliteRemoteConnection {
|
|
|
734
765
|
}
|
|
735
766
|
return new Promise(async (resolve, reject) => {
|
|
736
767
|
try {
|
|
737
|
-
const
|
|
768
|
+
const data = await this[_daoConnection].execute(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
|
|
769
|
+
const { affectedRows, insertId } = decode(data, { extensionCodec });
|
|
738
770
|
resolve({ affectedRows, insertId: insertId ? BigInt(insertId) : 0n });
|
|
739
771
|
}
|
|
740
772
|
catch (error) {
|
|
@@ -763,7 +795,8 @@ export class SqliteRemoteConnection {
|
|
|
763
795
|
}
|
|
764
796
|
return new Promise(async (resolve, reject) => {
|
|
765
797
|
try {
|
|
766
|
-
const
|
|
798
|
+
const data = await this[_daoConnection].pluck(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
|
|
799
|
+
const r = decode(data, { extensionCodec });
|
|
767
800
|
resolve(r);
|
|
768
801
|
}
|
|
769
802
|
catch (error) {
|
|
@@ -792,7 +825,8 @@ export class SqliteRemoteConnection {
|
|
|
792
825
|
}
|
|
793
826
|
return new Promise(async (resolve, reject) => {
|
|
794
827
|
try {
|
|
795
|
-
const
|
|
828
|
+
const data = await this[_daoConnection].get(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
|
|
829
|
+
const r = decode(data, { extensionCodec });
|
|
796
830
|
resolve(r);
|
|
797
831
|
}
|
|
798
832
|
catch (error) {
|
|
@@ -821,7 +855,8 @@ export class SqliteRemoteConnection {
|
|
|
821
855
|
}
|
|
822
856
|
return new Promise(async (resolve, reject) => {
|
|
823
857
|
try {
|
|
824
|
-
const
|
|
858
|
+
const data = await this[_daoConnection].raw(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
|
|
859
|
+
const r = decode(data, { extensionCodec });
|
|
825
860
|
resolve(r);
|
|
826
861
|
}
|
|
827
862
|
catch (error) {
|
|
@@ -850,7 +885,8 @@ export class SqliteRemoteConnection {
|
|
|
850
885
|
}
|
|
851
886
|
return new Promise(async (resolve, reject) => {
|
|
852
887
|
try {
|
|
853
|
-
const
|
|
888
|
+
const data = await this[_daoConnection].query(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
|
|
889
|
+
const r = decode(data, { extensionCodec });
|
|
854
890
|
resolve(r);
|
|
855
891
|
}
|
|
856
892
|
catch (error) {
|
|
@@ -866,7 +902,7 @@ export class SqliteRemoteConnection {
|
|
|
866
902
|
realse(sync) {
|
|
867
903
|
}
|
|
868
904
|
}
|
|
869
|
-
|
|
905
|
+
_d = _inTransaction;
|
|
870
906
|
export class SqliteRemote {
|
|
871
907
|
constructor(db, name) {
|
|
872
908
|
this[_daoDB] = db;
|
|
@@ -879,8 +915,11 @@ export class SqliteRemote {
|
|
|
879
915
|
}
|
|
880
916
|
;
|
|
881
917
|
return new Promise(async (resolve, reject) => {
|
|
918
|
+
if (!this.connection) {
|
|
919
|
+
this.connection = new SqliteRemoteConnection(this[_daoDB], this[_sqliteRemoteName]);
|
|
920
|
+
}
|
|
882
921
|
try {
|
|
883
|
-
resolve(
|
|
922
|
+
resolve(this.connection);
|
|
884
923
|
}
|
|
885
924
|
catch (error) {
|
|
886
925
|
reject(error);
|
|
@@ -1208,6 +1247,31 @@ class Build {
|
|
|
1208
1247
|
}
|
|
1209
1248
|
};
|
|
1210
1249
|
}
|
|
1250
|
+
/**
|
|
1251
|
+
* * PROBLEM_TYPE = 枚举名
|
|
1252
|
+
* * t.problemtype = 列名
|
|
1253
|
+
*
|
|
1254
|
+
* ```
|
|
1255
|
+
* {{#enumTag}} PROBLEM_TYPE(t.problemtype) {{/enumTag}}
|
|
1256
|
+
* ```
|
|
1257
|
+
*/
|
|
1258
|
+
enum() {
|
|
1259
|
+
return (text) => {
|
|
1260
|
+
const matchs = text.match(/([a-zA-Z_]+)\(([^()]+)\)/);
|
|
1261
|
+
if (matchs) {
|
|
1262
|
+
const [_a, MapName, Column] = matchs;
|
|
1263
|
+
if (MapName && Column) {
|
|
1264
|
+
const map = globalThis[_enums].GlobalMap[MapName.trim()];
|
|
1265
|
+
if (map) {
|
|
1266
|
+
return ` CASE
|
|
1267
|
+
${Object.entries(map).map(([k, v]) => `WHEN ${Column} = '${k}' THEN '${v}'`).join(' ')}
|
|
1268
|
+
END `;
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
return "''";
|
|
1273
|
+
};
|
|
1274
|
+
}
|
|
1211
1275
|
}
|
|
1212
1276
|
Build.page = 'COUNT(1) zccw1986 ';
|
|
1213
1277
|
function replaceCdata(rawText) {
|
|
@@ -1228,10 +1292,10 @@ function replaceCdata(rawText) {
|
|
|
1228
1292
|
return rawText;
|
|
1229
1293
|
}
|
|
1230
1294
|
function _flatData(result, i, length, keys, V) {
|
|
1231
|
-
var
|
|
1295
|
+
var _e;
|
|
1232
1296
|
const key = keys[i];
|
|
1233
1297
|
if (i < length) {
|
|
1234
|
-
result[
|
|
1298
|
+
result[_e = key] ?? (result[_e] = {});
|
|
1235
1299
|
i++;
|
|
1236
1300
|
_flatData(result[key], i, length, keys, V);
|
|
1237
1301
|
}
|
|
@@ -1426,19 +1490,14 @@ export class SqlCache {
|
|
|
1426
1490
|
const buildParam = new Build(options.isCount === true, options.isSum === true, options);
|
|
1427
1491
|
if (typeof sqlSource === 'function') {
|
|
1428
1492
|
const _sql = sqlSource(options);
|
|
1429
|
-
|
|
1430
|
-
return format(sql);
|
|
1493
|
+
return mustache.render(_sql, buildParam, this.sqlFNMap);
|
|
1431
1494
|
}
|
|
1432
1495
|
else if (typeof sqlSource === 'string') {
|
|
1433
|
-
|
|
1434
|
-
return format(sql);
|
|
1496
|
+
return mustache.render(sqlSource, buildParam, this.sqlFNMap);
|
|
1435
1497
|
}
|
|
1436
1498
|
else if (typeof sqlSource === 'object') {
|
|
1437
1499
|
const _sql = convert(sqlSource, options, matchSqlid, this.sqlMap);
|
|
1438
|
-
|
|
1439
|
-
const sql = mustache.render(_sql, buildParam, this.sqlFNMap);
|
|
1440
|
-
console.log(sql);
|
|
1441
|
-
return format(sql);
|
|
1500
|
+
return mustache.render(_sql, buildParam, this.sqlFNMap);
|
|
1442
1501
|
}
|
|
1443
1502
|
return '';
|
|
1444
1503
|
}
|
|
@@ -1469,12 +1528,9 @@ function P(skipConn = false) {
|
|
|
1469
1528
|
const option = args[0] = Object.assign({}, globalThis[_GlobalSqlOption], this[_SqlOption], args[0]);
|
|
1470
1529
|
option.sync ?? (option.sync = SyncMode.Async);
|
|
1471
1530
|
const dbName = option?.dbName ?? this[_daoDBName] ?? _primaryDB;
|
|
1472
|
-
const dddx = this[_dbType];
|
|
1473
|
-
logger.info(dddx);
|
|
1474
1531
|
option.dao = globalThis[_dao][this[_dbType]][dbName];
|
|
1475
1532
|
Throw.if(!option.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
|
|
1476
1533
|
option.tableName = option?.tableName ?? this[_tableName];
|
|
1477
|
-
const tableES = Sqlstring.escapeId(option.tableName);
|
|
1478
1534
|
if (this[_dbType] === DBType.Sqlite) {
|
|
1479
1535
|
Throw.if(option.sync === SyncMode.Async, 'sqlite can not Async!');
|
|
1480
1536
|
// 连接共享
|
|
@@ -1484,60 +1540,6 @@ function P(skipConn = false) {
|
|
|
1484
1540
|
else {
|
|
1485
1541
|
needRealseConn = false;
|
|
1486
1542
|
}
|
|
1487
|
-
if (skipConn === false) {
|
|
1488
|
-
const lastVersion = this[_sqlite_version] ?? '0.0.1';
|
|
1489
|
-
// 检查表
|
|
1490
|
-
const tableCheckResult = option.conn.pluck(SyncMode.Sync, `SELECT COUNT(1) t FROM sqlite_master WHERE TYPE = 'table' AND name = ?`, [option.tableName]);
|
|
1491
|
-
if (tableCheckResult) {
|
|
1492
|
-
// 旧版本
|
|
1493
|
-
const tableVersion = option.conn.pluck(SyncMode.Sync, 'SELECT ______version v from TABLE_VERSION WHERE ______tableName = ?', [option.tableName]);
|
|
1494
|
-
if (tableVersion && tableVersion < lastVersion) { // 发现需要升级的版本
|
|
1495
|
-
// 更新版本
|
|
1496
|
-
const columns = iterare(option.conn.query(SyncMode.Sync, `PRAGMA table_info(${tableES})`))
|
|
1497
|
-
.filter(c => this[_fields].hasOwnProperty(C2P(c.name, globalThis[_Hump])))
|
|
1498
|
-
.map(c => Sqlstring.escapeId(c.name))
|
|
1499
|
-
.join(',');
|
|
1500
|
-
const rtable = Sqlstring.escapeId(`${option.tableName}_${tableVersion.replace(/\./, '_')}`);
|
|
1501
|
-
option.conn.execute(SyncMode.Sync, `DROP TABLE IF EXISTS ${rtable};`);
|
|
1502
|
-
option.conn.execute(SyncMode.Sync, `ALTER TABLE ${tableES} RENAME TO ${rtable};`);
|
|
1503
|
-
option.conn.execute(SyncMode.Sync, `
|
|
1504
|
-
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
1505
|
-
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
1506
|
-
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
1507
|
-
);
|
|
1508
|
-
`);
|
|
1509
|
-
if (this[_index] && this[_index].length) {
|
|
1510
|
-
for (const index of this[_index]) {
|
|
1511
|
-
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${this[_fields][index]?.C2()}");`);
|
|
1512
|
-
}
|
|
1513
|
-
}
|
|
1514
|
-
option.conn.execute(SyncMode.Sync, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
1515
|
-
option.conn.execute(SyncMode.Sync, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
1516
|
-
option.conn.execute(SyncMode.Sync, `DROP TABLE IF EXISTS ${rtable};`);
|
|
1517
|
-
// 更新完毕,保存版本号
|
|
1518
|
-
option.conn.execute(SyncMode.Sync, 'UPDATE TABLE_VERSION SET ______version = ? WHERE ______tableName = ?', [option.tableName, lastVersion]);
|
|
1519
|
-
}
|
|
1520
|
-
else if (!tableVersion) { // 不需要升级情况:没有旧的版本号
|
|
1521
|
-
option.conn.execute(SyncMode.Sync, 'INSERT INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
1522
|
-
}
|
|
1523
|
-
}
|
|
1524
|
-
else { // 表不存在
|
|
1525
|
-
// 创建表
|
|
1526
|
-
option.conn.execute(SyncMode.Sync, `
|
|
1527
|
-
CREATE TABLE IF NOT EXISTS ${tableES} (
|
|
1528
|
-
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
1529
|
-
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
1530
|
-
|
|
1531
|
-
);
|
|
1532
|
-
`);
|
|
1533
|
-
if (this[_index] && this[_index].length) {
|
|
1534
|
-
for (const index of this[_index]) {
|
|
1535
|
-
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${this[_fields][index]?.C2()}");`);
|
|
1536
|
-
}
|
|
1537
|
-
}
|
|
1538
|
-
option.conn.execute(SyncMode.Sync, 'INSERT OR REPLACE INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
1539
|
-
}
|
|
1540
|
-
}
|
|
1541
1543
|
try {
|
|
1542
1544
|
const result = fn.call(this, ...args);
|
|
1543
1545
|
logger.info(`${propertyKey}:${option.tableName}:use ${+new Date() - startTime}ms`);
|
|
@@ -1571,59 +1573,6 @@ function P(skipConn = false) {
|
|
|
1571
1573
|
else {
|
|
1572
1574
|
needRealseConn = false;
|
|
1573
1575
|
}
|
|
1574
|
-
if (skipConn === false) {
|
|
1575
|
-
const lastVersion = this[_sqlite_version] ?? '0.0.1';
|
|
1576
|
-
// 检查表
|
|
1577
|
-
const tableCheckResult = await option.conn.pluck(SyncMode.Async, `SELECT COUNT(1) t FROM sqlite_master WHERE TYPE = 'table' AND name = ?`, [option.tableName]);
|
|
1578
|
-
if (tableCheckResult) {
|
|
1579
|
-
// 旧版本
|
|
1580
|
-
const tableVersion = await option.conn.pluck(SyncMode.Async, 'SELECT ______version v from TABLE_VERSION WHERE ______tableName = ?', [option.tableName]);
|
|
1581
|
-
if (tableVersion && tableVersion < lastVersion) { // 发现需要升级的版本
|
|
1582
|
-
// 更新版本
|
|
1583
|
-
const columns = iterare(await option.conn.query(SyncMode.Async, `PRAGMA table_info(${tableES})`))
|
|
1584
|
-
.filter(c => this[_fields].hasOwnProperty(C2P(c.name, globalThis[_Hump])))
|
|
1585
|
-
.map(c => Sqlstring.escapeId(c.name))
|
|
1586
|
-
.join(',');
|
|
1587
|
-
const rtable = `${option.tableName}_${tableVersion.replace(/\./, '_')}`;
|
|
1588
|
-
await option.conn.execute(SyncMode.Async, `DROP TABLE IF EXISTS ${rtable};`);
|
|
1589
|
-
await option.conn.execute(SyncMode.Async, `ALTER TABLE ${tableES} RENAME TO ${rtable};`);
|
|
1590
|
-
await option.conn.execute(SyncMode.Async, `
|
|
1591
|
-
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
1592
|
-
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
1593
|
-
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
1594
|
-
);
|
|
1595
|
-
`);
|
|
1596
|
-
if (this[_index] && this[_index].length) {
|
|
1597
|
-
for (const index of this[_index]) {
|
|
1598
|
-
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${this[_fields][index]?.C2()}");`);
|
|
1599
|
-
}
|
|
1600
|
-
}
|
|
1601
|
-
await option.conn.execute(SyncMode.Async, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
1602
|
-
await option.conn.execute(SyncMode.Async, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
1603
|
-
await option.conn.execute(SyncMode.Async, `DROP TABLE IF EXISTS ${rtable};`);
|
|
1604
|
-
// 更新完毕,保存版本号
|
|
1605
|
-
await option.conn.execute(SyncMode.Async, 'UPDATE TABLE_VERSION SET ______version = ? WHERE ______tableName = ?', [option.tableName, lastVersion]);
|
|
1606
|
-
}
|
|
1607
|
-
else if (!tableVersion) { // 不需要升级情况:没有旧的版本号
|
|
1608
|
-
await option.conn.execute(SyncMode.Async, 'INSERT INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
1609
|
-
}
|
|
1610
|
-
}
|
|
1611
|
-
else { // 表不存在
|
|
1612
|
-
// 创建表
|
|
1613
|
-
await option.conn.execute(SyncMode.Async, `
|
|
1614
|
-
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
1615
|
-
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
1616
|
-
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
1617
|
-
);
|
|
1618
|
-
`);
|
|
1619
|
-
if (this[_index] && this[_index].length) {
|
|
1620
|
-
for (const index of this[_index]) {
|
|
1621
|
-
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${Sqlstring.escapeId(option.tableName)} ("${this[_fields][index]?.C2()}");`);
|
|
1622
|
-
}
|
|
1623
|
-
}
|
|
1624
|
-
await option.conn.execute(SyncMode.Async, 'INSERT OR REPLACE INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
1625
|
-
}
|
|
1626
|
-
}
|
|
1627
1576
|
try {
|
|
1628
1577
|
const result = await fn.call(this, ...args);
|
|
1629
1578
|
logger.info(`${propertyKey}:${option.tableName}:use ${+new Date() - startTime}ms`);
|
|
@@ -1755,6 +1704,7 @@ export const Field = (config) => {
|
|
|
1755
1704
|
case SqlType.bigint: {
|
|
1756
1705
|
field[DBType.Mysql] = () => `${field.C2()} bigint ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1757
1706
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} integer`;
|
|
1707
|
+
field.Data2SQL = (data) => BigInt(data ?? 0);
|
|
1758
1708
|
break;
|
|
1759
1709
|
}
|
|
1760
1710
|
case SqlType.float: {
|
|
@@ -1790,26 +1740,31 @@ export const Field = (config) => {
|
|
|
1790
1740
|
case SqlType.date: {
|
|
1791
1741
|
field[DBType.Mysql] = () => `${field.C2()} date ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1792
1742
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1743
|
+
field.Data2SQL = (data) => typeof data === 'string' ? new Date(data) : data;
|
|
1793
1744
|
break;
|
|
1794
1745
|
}
|
|
1795
1746
|
case SqlType.time: {
|
|
1796
1747
|
field[DBType.Mysql] = () => `${field.C2()} time ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1797
1748
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1749
|
+
field.Data2SQL = (data) => typeof data === 'string' ? new Date(data) : data;
|
|
1798
1750
|
break;
|
|
1799
1751
|
}
|
|
1800
1752
|
case SqlType.year: {
|
|
1801
1753
|
field[DBType.Mysql] = () => `${field.C2()} year ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1802
1754
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1755
|
+
field.Data2SQL = (data) => typeof data === 'string' ? new Date(data) : data;
|
|
1803
1756
|
break;
|
|
1804
1757
|
}
|
|
1805
1758
|
case SqlType.datetime: {
|
|
1806
1759
|
field[DBType.Mysql] = () => `${field.C2()} datetime ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1807
1760
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1761
|
+
field.Data2SQL = (data) => typeof data === 'string' ? new Date(data) : data;
|
|
1808
1762
|
break;
|
|
1809
1763
|
}
|
|
1810
1764
|
case SqlType.timestamp: {
|
|
1811
1765
|
field[DBType.Mysql] = () => `${field.C2()} timestamp ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1812
1766
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} integer`;
|
|
1767
|
+
field.Data2SQL = (data) => typeof data === 'string' ? +new Date(data) : data;
|
|
1813
1768
|
break;
|
|
1814
1769
|
}
|
|
1815
1770
|
case SqlType.char: {
|
|
@@ -1923,6 +1878,7 @@ export const Field = (config) => {
|
|
|
1923
1878
|
let __columns = Reflect.getMetadata(_columns, object);
|
|
1924
1879
|
let __columnsNoId = Reflect.getMetadata(_columnsNoId, object);
|
|
1925
1880
|
let __ids = Reflect.getMetadata(_ids, object);
|
|
1881
|
+
let __logicIds = Reflect.getMetadata(_logicIds, object);
|
|
1926
1882
|
let __index = Reflect.getMetadata(_index, object);
|
|
1927
1883
|
let __def = Reflect.getMetadata(_def, object);
|
|
1928
1884
|
if (!__fields) {
|
|
@@ -1941,16 +1897,23 @@ export const Field = (config) => {
|
|
|
1941
1897
|
else {
|
|
1942
1898
|
__columnsNoId.push(propertyName);
|
|
1943
1899
|
}
|
|
1900
|
+
if (field.logicId === true) {
|
|
1901
|
+
__logicIds.push(propertyName);
|
|
1902
|
+
}
|
|
1944
1903
|
if (field.index === true) {
|
|
1945
1904
|
__index.push(propertyName);
|
|
1946
1905
|
}
|
|
1947
1906
|
if (hasDef) {
|
|
1948
1907
|
__def[propertyName] = field.def;
|
|
1949
1908
|
}
|
|
1909
|
+
if (field.comment) {
|
|
1910
|
+
__def[propertyName] = field.comment;
|
|
1911
|
+
}
|
|
1950
1912
|
Reflect.defineMetadata(_fields, __fields, object);
|
|
1951
1913
|
Reflect.defineMetadata(_columns, __columns, object);
|
|
1952
1914
|
Reflect.defineMetadata(_columnsNoId, __columnsNoId, object);
|
|
1953
1915
|
Reflect.defineMetadata(_ids, __ids, object);
|
|
1916
|
+
Reflect.defineMetadata(_logicIds, __logicIds, object);
|
|
1954
1917
|
Reflect.defineMetadata(_index, __index, object);
|
|
1955
1918
|
Reflect.defineMetadata(_def, __def, object);
|
|
1956
1919
|
if (field.hasOwnProperty('logicDelete')) {
|
|
@@ -1959,44 +1922,58 @@ export const Field = (config) => {
|
|
|
1959
1922
|
}
|
|
1960
1923
|
};
|
|
1961
1924
|
};
|
|
1925
|
+
const formatDialects = {
|
|
1926
|
+
[DBType.Mysql]: mysql,
|
|
1927
|
+
[DBType.Sqlite]: sqlite,
|
|
1928
|
+
[DBType.SqliteRemote]: sqlite
|
|
1929
|
+
};
|
|
1962
1930
|
export const DB = (config) => {
|
|
1963
1931
|
return function (constructor) {
|
|
1964
|
-
var
|
|
1932
|
+
var _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0;
|
|
1965
1933
|
const __ids = Reflect.getMetadata(_ids, config.clz.prototype) || new Array;
|
|
1934
|
+
const __logicIds = Reflect.getMetadata(_logicIds, config.clz.prototype) || new Array;
|
|
1966
1935
|
const __fields = Reflect.getMetadata(_fields, config.clz.prototype);
|
|
1967
1936
|
const __columns = Reflect.getMetadata(_columns, config.clz.prototype);
|
|
1968
|
-
const __columnsNoId =
|
|
1937
|
+
const __columnsNoId = Reflect.getMetadata(_columnsNoId, config.clz.prototype);
|
|
1969
1938
|
const __stateFileName = Reflect.getMetadata(_stateFileName, config.clz.prototype);
|
|
1970
1939
|
const __deleteState = Reflect.getMetadata(_deleteState, config.clz.prototype);
|
|
1971
1940
|
const __index = Reflect.getMetadata(_index, config.clz.prototype);
|
|
1972
1941
|
const __def = Reflect.getMetadata(_def, config.clz.prototype);
|
|
1942
|
+
const __dbType = config.dbType ?? DBType.Mysql;
|
|
1943
|
+
const __formatDialect = formatDialects[__dbType];
|
|
1973
1944
|
const className = config.tableName?.replace(/_(\w)/g, (a, b) => b.toUpperCase());
|
|
1974
1945
|
const ClassName = className?.replace(/\w/, (v) => v.toUpperCase());
|
|
1975
1946
|
const vueName = config.tableName?.replace(/_/g, '-');
|
|
1976
|
-
return
|
|
1947
|
+
return _0 = class extends constructor {
|
|
1977
1948
|
constructor() {
|
|
1978
1949
|
super(...arguments);
|
|
1979
|
-
this[
|
|
1980
|
-
this[
|
|
1981
|
-
this[
|
|
1982
|
-
this[
|
|
1983
|
-
this[
|
|
1984
|
-
this[
|
|
1985
|
-
this[
|
|
1986
|
-
this[
|
|
1987
|
-
this[
|
|
1988
|
-
this[
|
|
1989
|
-
this[
|
|
1990
|
-
this[
|
|
1991
|
-
this[
|
|
1992
|
-
this[
|
|
1993
|
-
this[
|
|
1994
|
-
this[
|
|
1995
|
-
this[
|
|
1950
|
+
this[_e] = config.tableName;
|
|
1951
|
+
this[_f] = className;
|
|
1952
|
+
this[_g] = ClassName;
|
|
1953
|
+
this[_h] = vueName;
|
|
1954
|
+
this[_j] = config.dbName;
|
|
1955
|
+
this[_k] = __dbType;
|
|
1956
|
+
this[_l] = __formatDialect;
|
|
1957
|
+
this[_m] = config.sqliteVersion;
|
|
1958
|
+
this[_o] = Object.assign({}, _defOption, config);
|
|
1959
|
+
this[_p] = __ids;
|
|
1960
|
+
this[_q] = __logicIds;
|
|
1961
|
+
this[_r] = __fields;
|
|
1962
|
+
this[_s] = __columns;
|
|
1963
|
+
this[_t] = __columnsNoId;
|
|
1964
|
+
this[_u] = __index;
|
|
1965
|
+
this[_v] = __def;
|
|
1966
|
+
this[_w] = config.comment;
|
|
1967
|
+
this[_x] = __stateFileName;
|
|
1968
|
+
this[_y] = __deleteState;
|
|
1969
|
+
this[_z] = (data, option) => {
|
|
1996
1970
|
return Object.fromEntries(iterare(option?.skipId === true ? __columnsNoId : __columns)
|
|
1997
1971
|
.map(K => [K, FieldFilter(K, data[K], __def, __fields[K].uuid === true || __fields[K].uuidShort === true, option)])
|
|
1998
1972
|
.filter(data => {
|
|
1999
1973
|
if (data[1][0] === 1) {
|
|
1974
|
+
if (__fields[data[0]].Data2SQL) {
|
|
1975
|
+
data[1][1] = __fields[data[0]].Data2SQL(data[1][1]);
|
|
1976
|
+
}
|
|
2000
1977
|
if (option?.onFieldExists) {
|
|
2001
1978
|
option.onFieldExists(data[0], data[1][1]);
|
|
2002
1979
|
}
|
|
@@ -2011,24 +1988,27 @@ export const DB = (config) => {
|
|
|
2011
1988
|
};
|
|
2012
1989
|
}
|
|
2013
1990
|
},
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
1991
|
+
_e = _tableName,
|
|
1992
|
+
_f = _className,
|
|
1993
|
+
_g = _ClassName,
|
|
1994
|
+
_h = _vueName,
|
|
1995
|
+
_j = _daoDBName,
|
|
1996
|
+
_k = _dbType,
|
|
1997
|
+
_l = _formatDialect,
|
|
1998
|
+
_m = _sqlite_version,
|
|
1999
|
+
_o = _SqlOption,
|
|
2000
|
+
_p = _ids,
|
|
2001
|
+
_q = _logicIds,
|
|
2002
|
+
_r = _fields,
|
|
2003
|
+
_s = _columns,
|
|
2004
|
+
_t = _columnsNoId,
|
|
2005
|
+
_u = _index,
|
|
2006
|
+
_v = _def,
|
|
2007
|
+
_w = _comment,
|
|
2008
|
+
_x = _stateFileName,
|
|
2009
|
+
_y = _deleteState,
|
|
2010
|
+
_z = _transformer,
|
|
2011
|
+
_0;
|
|
2032
2012
|
};
|
|
2033
2013
|
};
|
|
2034
2014
|
/**
|
|
@@ -2124,10 +2104,10 @@ export class SqlService {
|
|
|
2124
2104
|
return `SELECT ${questMark.join(',')} FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM ${tableName} WHERE ${where})`;
|
|
2125
2105
|
});
|
|
2126
2106
|
const columnNames = iterare(finalColumns).map(i => this[_fields][i]?.C2()).join(',');
|
|
2127
|
-
const sql =
|
|
2107
|
+
const sql = formatDialect(`INSERT INTO
|
|
2128
2108
|
${tableName}
|
|
2129
2109
|
(${columnNames})
|
|
2130
|
-
${questMarks.join(' UNION ALL ')}
|
|
2110
|
+
${questMarks.join(' UNION ALL ')};`, { dialect: this[_formatDialect] });
|
|
2131
2111
|
sqls.push({ sql, params });
|
|
2132
2112
|
break;
|
|
2133
2113
|
}
|
|
@@ -2165,12 +2145,12 @@ export class SqlService {
|
|
|
2165
2145
|
return `(${questMark.join(',')})`;
|
|
2166
2146
|
});
|
|
2167
2147
|
const columnNames = iterare(finalColumns).map(i => this[_fields][i]?.C2()).join(',');
|
|
2168
|
-
const sql =
|
|
2148
|
+
const sql = formatDialect(`
|
|
2169
2149
|
${this[_dbType] === DBType.Mysql ? '' : 'INSERT OR'} REPLACE INTO
|
|
2170
2150
|
${tableName}
|
|
2171
2151
|
(${columnNames})
|
|
2172
2152
|
VALUES ${questMarks};
|
|
2173
|
-
|
|
2153
|
+
`, { dialect: this[_formatDialect] });
|
|
2174
2154
|
sqls.push({ sql, params });
|
|
2175
2155
|
break;
|
|
2176
2156
|
}
|
|
@@ -2208,12 +2188,12 @@ export class SqlService {
|
|
|
2208
2188
|
return `(${questMark.join(',')})`;
|
|
2209
2189
|
});
|
|
2210
2190
|
const columnNames = iterare(finalColumns).map(i => this[_fields][i]?.C2()).join(',');
|
|
2211
|
-
const sql =
|
|
2191
|
+
const sql = formatDialect(`
|
|
2212
2192
|
INSERT INTO
|
|
2213
2193
|
${tableName}
|
|
2214
2194
|
(${columnNames})
|
|
2215
2195
|
VALUES ${questMarks};
|
|
2216
|
-
|
|
2196
|
+
`, { dialect: this[_formatDialect] });
|
|
2217
2197
|
sqls.push({ sql, params });
|
|
2218
2198
|
break;
|
|
2219
2199
|
}
|
|
@@ -2257,16 +2237,16 @@ export class SqlService {
|
|
|
2257
2237
|
sqls.push(..._sqls);
|
|
2258
2238
|
const columnNames = iterare(finalColumns).map(i => this[_fields][i]?.C2()).join(',');
|
|
2259
2239
|
sqls.push({
|
|
2260
|
-
sql:
|
|
2240
|
+
sql: formatDialect(`
|
|
2261
2241
|
INSERT INTO
|
|
2262
2242
|
${tableTemp}
|
|
2263
2243
|
(${columnNames})
|
|
2264
2244
|
VALUES ${questMarks};
|
|
2265
|
-
|
|
2245
|
+
`, { dialect: this[_formatDialect] }), params
|
|
2266
2246
|
});
|
|
2267
2247
|
sqls.push({
|
|
2268
|
-
sql:
|
|
2269
|
-
SELECT ${columnNames} FROM ${tableTemp}
|
|
2248
|
+
sql: formatDialect(`INSERT INTO ${Sqlstring.escapeId(option.tableName)} (${columnNames})
|
|
2249
|
+
SELECT ${columnNames} FROM ${tableTemp};`, { dialect: this[_formatDialect] })
|
|
2270
2250
|
});
|
|
2271
2251
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
2272
2252
|
break;
|
|
@@ -2305,20 +2285,18 @@ export class SqlService {
|
|
|
2305
2285
|
}
|
|
2306
2286
|
else if (isArray) {
|
|
2307
2287
|
const fn = async () => {
|
|
2308
|
-
|
|
2309
|
-
const
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
result += dd.insertId;
|
|
2316
|
-
}
|
|
2288
|
+
const result = await excuteSplit(ExcuteSplitMode.AsyncTrust, datas, async (_data) => {
|
|
2289
|
+
const sqls = this._insert(_data, option);
|
|
2290
|
+
let result = 0n;
|
|
2291
|
+
for (const { sql, params } of sqls) {
|
|
2292
|
+
const dd = await option.conn.execute(SyncMode.Async, sql, params);
|
|
2293
|
+
if (dd.insertId) {
|
|
2294
|
+
result += dd.insertId;
|
|
2317
2295
|
}
|
|
2318
|
-
|
|
2319
|
-
}, { everyLength: option?.every === true ? 1 : option?.maxDeal });
|
|
2296
|
+
}
|
|
2320
2297
|
return result;
|
|
2321
|
-
}, option?.
|
|
2298
|
+
}, { everyLength: option?.every === true ? 1 : option?.maxDeal });
|
|
2299
|
+
return result;
|
|
2322
2300
|
};
|
|
2323
2301
|
return new Promise(async (resolve, reject) => {
|
|
2324
2302
|
try {
|
|
@@ -2387,13 +2365,13 @@ export class SqlService {
|
|
|
2387
2365
|
}
|
|
2388
2366
|
});
|
|
2389
2367
|
}
|
|
2390
|
-
const sql =
|
|
2368
|
+
const sql = formatDialect(`UPDATE ${tableName} SET ${iterare(this[_columnsNoId])
|
|
2391
2369
|
.filter(K => columnMaps[K].where.length > 0)
|
|
2392
2370
|
.map(K => {
|
|
2393
2371
|
params.push(...columnMaps[K].params);
|
|
2394
2372
|
return `${this[_fields][K]?.C2()} = CASE ${columnMaps[K].where.join(' ')} ELSE ${this[_fields][K]?.C2()} END`;
|
|
2395
2373
|
})
|
|
2396
|
-
.join(',')}
|
|
2374
|
+
.join(',')};`, { dialect: this[_formatDialect] });
|
|
2397
2375
|
sqls.push({ sql, params });
|
|
2398
2376
|
return sqls;
|
|
2399
2377
|
}
|
|
@@ -2482,14 +2460,14 @@ export class SqlService {
|
|
|
2482
2460
|
if (this[_stateFileName] !== undefined && option.forceDelete !== true) {
|
|
2483
2461
|
params.unshift(this[_deleteState]);
|
|
2484
2462
|
sqls.push({
|
|
2485
|
-
sql:
|
|
2463
|
+
sql: formatDialect(`
|
|
2486
2464
|
UPDATE ${tableNameESC} SET ${this[_fields][this[_stateFileName]]?.C2()} = ?
|
|
2487
2465
|
WHERE ${whereSql};
|
|
2488
|
-
|
|
2466
|
+
`, { dialect: this[_formatDialect] }), params
|
|
2489
2467
|
});
|
|
2490
2468
|
}
|
|
2491
2469
|
else {
|
|
2492
|
-
sqls.push({ sql:
|
|
2470
|
+
sqls.push({ sql: formatDialect(`DELETE FROM ${tableNameESC} WHERE ${whereSql};`, { dialect: this[_formatDialect] }), params });
|
|
2493
2471
|
}
|
|
2494
2472
|
}
|
|
2495
2473
|
else {
|
|
@@ -2501,14 +2479,14 @@ export class SqlService {
|
|
|
2501
2479
|
case DBType.Mysql: {
|
|
2502
2480
|
if (this[_stateFileName] !== undefined && option.forceDelete !== true) {
|
|
2503
2481
|
sqls.push({
|
|
2504
|
-
sql:
|
|
2505
|
-
SET a.${this[_fields][this[_stateFileName]]?.C2()} =
|
|
2482
|
+
sql: formatDialect(`UPDATE ${tableNameESC} a INNER JOIN ${tableTempESC} b ON ${delWhere.map(K => `a.${this[_fields][K]?.C2()} = b.${this[_fields][K]?.C2()}`).join(' AND ')}
|
|
2483
|
+
SET a.${this[_fields][this[_stateFileName]]?.C2()} = ?;`, { dialect: this[_formatDialect] }),
|
|
2506
2484
|
params: [this[_deleteState]]
|
|
2507
2485
|
});
|
|
2508
2486
|
}
|
|
2509
2487
|
else {
|
|
2510
2488
|
sqls.push({
|
|
2511
|
-
sql:
|
|
2489
|
+
sql: formatDialect(`DELETE a.* FROM ${tableNameESC} a INNER JOIN ${tableTempESC} b ON ${delWhere.map(K => `a.${this[_fields][K]?.C2()} = b.${this[_fields][K]?.C2()}`).join(' AND ')};`, { dialect: this[_formatDialect] })
|
|
2512
2490
|
});
|
|
2513
2491
|
}
|
|
2514
2492
|
break;
|
|
@@ -2518,13 +2496,13 @@ export class SqlService {
|
|
|
2518
2496
|
const columnNames = iterare(delWhere).map(K => this[_fields][K]?.C2()).join(',');
|
|
2519
2497
|
if (this[_stateFileName] !== undefined && option.forceDelete !== true) {
|
|
2520
2498
|
sqls.push({
|
|
2521
|
-
sql:
|
|
2522
|
-
WHERE (${columnNames}) IN (SELECT ${columnNames} FROM ${tableTempESC})
|
|
2499
|
+
sql: formatDialect(`UPDATE ${tableNameESC} SET ${this[_fields][this[_stateFileName]]?.C2()} = ?
|
|
2500
|
+
WHERE (${columnNames}) IN (SELECT ${columnNames} FROM ${tableTempESC});`, { dialect: this[_formatDialect] }),
|
|
2523
2501
|
params: [this[_deleteState]]
|
|
2524
2502
|
});
|
|
2525
2503
|
}
|
|
2526
2504
|
else {
|
|
2527
|
-
sqls.push({ sql:
|
|
2505
|
+
sqls.push({ sql: formatDialect(`DELETE FROM ${tableNameESC} WHERE (${columnNames}) IN (SELECT ${columnNames} FROM ${tableTempESC});`, { dialect: this[_formatDialect] }) });
|
|
2528
2506
|
}
|
|
2529
2507
|
break;
|
|
2530
2508
|
}
|
|
@@ -2578,8 +2556,7 @@ export class SqlService {
|
|
|
2578
2556
|
return result[0];
|
|
2579
2557
|
}
|
|
2580
2558
|
case TemplateResult.NotSureOne: {
|
|
2581
|
-
|
|
2582
|
-
return result[0] ?? null;
|
|
2559
|
+
return result && result.length > 0 ? result[0] ?? null : null;
|
|
2583
2560
|
}
|
|
2584
2561
|
case TemplateResult.Many: {
|
|
2585
2562
|
return result;
|
|
@@ -2615,13 +2592,13 @@ export class SqlService {
|
|
|
2615
2592
|
let resultIndex = -1;
|
|
2616
2593
|
if (option.mode === SelectMode.Common) {
|
|
2617
2594
|
const params = new Array();
|
|
2618
|
-
const whereSql =
|
|
2595
|
+
const whereSql = formatDialect(iterare(wheres).map(where => this[_transformer](where, option)).map(where => {
|
|
2619
2596
|
return `SELECT ${columns} FROM ${tableNameESC} a WHERE
|
|
2620
2597
|
${Object.entries(where).map(([K, V]) => {
|
|
2621
2598
|
params.push(V);
|
|
2622
2599
|
return `${this[_fields][K]?.C2()} = ?`;
|
|
2623
2600
|
}).join(' AND ')}`;
|
|
2624
|
-
}).join(' UNION ALL '));
|
|
2601
|
+
}).join(' UNION ALL '), { dialect: this[_formatDialect] });
|
|
2625
2602
|
sqls.push({ sql: whereSql, params });
|
|
2626
2603
|
resultIndex = 0;
|
|
2627
2604
|
}
|
|
@@ -2631,7 +2608,7 @@ export class SqlService {
|
|
|
2631
2608
|
const _sqls = this._createTable({ tableName: tableTemp, temp: true, columns: delWhere, data: wheres, index: 'all', id: 'none' });
|
|
2632
2609
|
sqls.push(..._sqls);
|
|
2633
2610
|
resultIndex = sqls.length;
|
|
2634
|
-
sqls.push({ sql:
|
|
2611
|
+
sqls.push({ sql: formatDialect(`SELECT ${columns} FROM ${tableNameESC} a INNER JOIN ${tableTempESC} b ON ${delWhere.map(K => `a.${this[_fields][K]?.C2()} = b.${this[_fields][K]?.C2()}`).join(' AND ')};`, { dialect: this[_formatDialect] }) });
|
|
2635
2612
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
2636
2613
|
}
|
|
2637
2614
|
if (option.sync === SyncMode.Sync) {
|
|
@@ -2760,7 +2737,7 @@ export class SqlService {
|
|
|
2760
2737
|
const _params = Object.assign({}, option.context, option.params);
|
|
2761
2738
|
option.sql ?? (option.sql = globalThis[_sqlCache].load(this._matchSqlid(option.sqlId), { ctx: option.context, isCount: option.isCount, ..._params }));
|
|
2762
2739
|
const params = [];
|
|
2763
|
-
const sql = option.sql?.replace(/\:([A-Za-z0-9._]+)/g, (txt, key) => {
|
|
2740
|
+
const sql = formatDialect(option.sql?.replace(/\:([A-Za-z0-9._]+)/g, (txt, key) => {
|
|
2764
2741
|
let V = LGet(_params, key);
|
|
2765
2742
|
if (V) {
|
|
2766
2743
|
if (V !== undefined) {
|
|
@@ -2785,7 +2762,7 @@ export class SqlService {
|
|
|
2785
2762
|
return '?';
|
|
2786
2763
|
}
|
|
2787
2764
|
return txt;
|
|
2788
|
-
});
|
|
2765
|
+
}), { dialect: this[_formatDialect] });
|
|
2789
2766
|
if (option.sync === SyncMode.Sync) {
|
|
2790
2767
|
const result = option.conn.query(SyncMode.Sync, sql, params);
|
|
2791
2768
|
return this._select(option.selectResult, result, option.defValue, option.errorMsg, option.hump, option.mapper, option.mapperIfUndefined);
|
|
@@ -2807,7 +2784,7 @@ export class SqlService {
|
|
|
2807
2784
|
const _params = Object.assign({}, option.context, option.params);
|
|
2808
2785
|
option.sql ?? (option.sql = globalThis[_sqlCache].load(this._matchSqlid(option.sqlId), { ctx: option.context, ..._params }));
|
|
2809
2786
|
const params = [];
|
|
2810
|
-
const sql = option.sql?.replace(/\:(\w+)/g, (txt, key) => {
|
|
2787
|
+
const sql = formatDialect(option.sql?.replace(/\:(\w+)/g, (txt, key) => {
|
|
2811
2788
|
let V = LGet(_params, key);
|
|
2812
2789
|
if (V) {
|
|
2813
2790
|
if (V !== undefined) {
|
|
@@ -2832,7 +2809,7 @@ export class SqlService {
|
|
|
2832
2809
|
return '?';
|
|
2833
2810
|
}
|
|
2834
2811
|
return txt;
|
|
2835
|
-
});
|
|
2812
|
+
}), { dialect: this[_formatDialect] });
|
|
2836
2813
|
if (option.sync === SyncMode.Sync) {
|
|
2837
2814
|
const result = option.conn.execute(SyncMode.Sync, sql, params);
|
|
2838
2815
|
return result.affectedRows;
|
|
@@ -2875,6 +2852,8 @@ export class SqlService {
|
|
|
2875
2852
|
size: 0,
|
|
2876
2853
|
total: 0
|
|
2877
2854
|
};
|
|
2855
|
+
option.pageNumber ?? (option.pageNumber = 1);
|
|
2856
|
+
option.pageSize ?? (option.pageSize = 0);
|
|
2878
2857
|
Object.assign(option.params, {
|
|
2879
2858
|
limitStart: calc(option.pageNumber).sub(1).mul(option.pageSize).over(),
|
|
2880
2859
|
limitEnd: calc(option.pageSize).over(),
|
|
@@ -2947,7 +2926,7 @@ export class SqlService {
|
|
|
2947
2926
|
selectResult: SelectResult.R_C_Assert
|
|
2948
2927
|
});
|
|
2949
2928
|
result.size = calc(result.total)
|
|
2950
|
-
.add(option.pageSize - 1)
|
|
2929
|
+
.add(option.pageSize ?? 10 - 1)
|
|
2951
2930
|
.div(option.pageSize)
|
|
2952
2931
|
.round(0, 2)
|
|
2953
2932
|
.over();
|
|
@@ -2976,6 +2955,170 @@ export class SqlService {
|
|
|
2976
2955
|
});
|
|
2977
2956
|
}
|
|
2978
2957
|
}
|
|
2958
|
+
/**
|
|
2959
|
+
* 导出数据,可以为EJS-EXCEL直接使用
|
|
2960
|
+
* @param list
|
|
2961
|
+
* @returns
|
|
2962
|
+
*/
|
|
2963
|
+
exp(list) {
|
|
2964
|
+
Throw.if(list.length === 0, 'not found data!');
|
|
2965
|
+
const columnTitles = new Array();
|
|
2966
|
+
const keys = this[_fields] ?
|
|
2967
|
+
iterare(Object.entries(this[_fields]))
|
|
2968
|
+
.filter(([K, F]) => (F.id !== true && F.exportable !== false) || (F.id === true && F.exportable === true))
|
|
2969
|
+
.map(([K, F]) => {
|
|
2970
|
+
columnTitles.push(F.comment ?? K);
|
|
2971
|
+
return K;
|
|
2972
|
+
}).toArray()
|
|
2973
|
+
: Object.keys(list[0]).filter(K => !this[_ids]?.includes(K));
|
|
2974
|
+
const title = this[_comment] ?? this[_tableName];
|
|
2975
|
+
const titleSpan = `A1:${ten2Any(keys.length)}1`;
|
|
2976
|
+
const datas = list.map(data => keys.map(k => data[k] ?? ''));
|
|
2977
|
+
return { title, titleSpan, columnTitles, datas };
|
|
2978
|
+
}
|
|
2979
|
+
/**
|
|
2980
|
+
* 导入数据的模板
|
|
2981
|
+
* @returns
|
|
2982
|
+
*/
|
|
2983
|
+
imp() {
|
|
2984
|
+
Throw.if(!this[_fields], 'not set fields!');
|
|
2985
|
+
const columnTitles = new Array();
|
|
2986
|
+
const keys = iterare(Object.entries(this[_fields]))
|
|
2987
|
+
.filter(([K, F]) => (F.id !== true && F.exportable !== false) || (F.id === true && F.exportable === true))
|
|
2988
|
+
.map(([K, F]) => {
|
|
2989
|
+
columnTitles.push(F.comment ?? K);
|
|
2990
|
+
return K;
|
|
2991
|
+
}).toArray();
|
|
2992
|
+
const title = this[_comment] ?? this[_tableName];
|
|
2993
|
+
const titleSpan = `A1:${ten2Any(keys.length)}1`;
|
|
2994
|
+
return { title, titleSpan, columnTitles };
|
|
2995
|
+
}
|
|
2996
|
+
init(option) {
|
|
2997
|
+
var _e;
|
|
2998
|
+
const tableES = Sqlstring.escapeId(option.tableName);
|
|
2999
|
+
(_e = option).force ?? (_e.force = false);
|
|
3000
|
+
if (this[_dbType] === DBType.Sqlite) {
|
|
3001
|
+
if (option?.force) {
|
|
3002
|
+
option.conn.execute(SyncMode.Sync, `DROP TABLE IF EXISTS ${tableES};`);
|
|
3003
|
+
}
|
|
3004
|
+
const lastVersion = this[_sqlite_version] ?? '0.0.1';
|
|
3005
|
+
// 检查表
|
|
3006
|
+
const tableCheckResult = option.conn.pluck(SyncMode.Sync, `SELECT COUNT(1) t FROM sqlite_master WHERE TYPE = 'table' AND name = ?`, [option.tableName]);
|
|
3007
|
+
if (tableCheckResult) {
|
|
3008
|
+
// 旧版本
|
|
3009
|
+
const tableVersion = option.conn.pluck(SyncMode.Sync, 'SELECT ______version v from TABLE_VERSION WHERE ______tableName = ?', [option.tableName]);
|
|
3010
|
+
if (tableVersion && tableVersion < lastVersion) { // 发现需要升级的版本
|
|
3011
|
+
// 更新版本
|
|
3012
|
+
const columns = iterare(option.conn.query(SyncMode.Sync, `PRAGMA table_info(${tableES})`))
|
|
3013
|
+
.filter(c => this[_fields].hasOwnProperty(C2P(c.name, globalThis[_Hump])))
|
|
3014
|
+
.map(c => Sqlstring.escapeId(c.name))
|
|
3015
|
+
.join(',');
|
|
3016
|
+
const rtable = Sqlstring.escapeId(`${option.tableName}_${tableVersion.replace(/\./, '_')}`);
|
|
3017
|
+
option.conn.execute(SyncMode.Sync, `DROP TABLE IF EXISTS ${rtable};`);
|
|
3018
|
+
option.conn.execute(SyncMode.Sync, `ALTER TABLE ${tableES} RENAME TO ${rtable};`);
|
|
3019
|
+
option.conn.execute(SyncMode.Sync, `
|
|
3020
|
+
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
3021
|
+
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
3022
|
+
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
3023
|
+
);
|
|
3024
|
+
`);
|
|
3025
|
+
if (this[_index] && this[_index].length) {
|
|
3026
|
+
for (const index of this[_index]) {
|
|
3027
|
+
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${this[_fields][index]?.C2()}");`);
|
|
3028
|
+
}
|
|
3029
|
+
}
|
|
3030
|
+
option.conn.execute(SyncMode.Sync, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
3031
|
+
option.conn.execute(SyncMode.Sync, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
3032
|
+
option.conn.execute(SyncMode.Sync, `DROP TABLE IF EXISTS ${rtable};`);
|
|
3033
|
+
// 更新完毕,保存版本号
|
|
3034
|
+
option.conn.execute(SyncMode.Sync, 'UPDATE TABLE_VERSION SET ______version = ? WHERE ______tableName = ?', [option.tableName, lastVersion]);
|
|
3035
|
+
}
|
|
3036
|
+
else if (!tableVersion) { // 不需要升级情况:没有旧的版本号
|
|
3037
|
+
option.conn.execute(SyncMode.Sync, 'INSERT INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
3038
|
+
}
|
|
3039
|
+
}
|
|
3040
|
+
else { // 表不存在
|
|
3041
|
+
// 创建表
|
|
3042
|
+
option.conn.execute(SyncMode.Sync, `
|
|
3043
|
+
CREATE TABLE IF NOT EXISTS ${tableES} (
|
|
3044
|
+
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
3045
|
+
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
3046
|
+
|
|
3047
|
+
);
|
|
3048
|
+
`);
|
|
3049
|
+
if (this[_index] && this[_index].length) {
|
|
3050
|
+
for (const index of this[_index]) {
|
|
3051
|
+
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${this[_fields][index]?.C2()}");`);
|
|
3052
|
+
}
|
|
3053
|
+
}
|
|
3054
|
+
option.conn.execute(SyncMode.Sync, 'INSERT OR REPLACE INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
3055
|
+
}
|
|
3056
|
+
}
|
|
3057
|
+
else if (this[_dbType] === DBType.SqliteRemote) {
|
|
3058
|
+
return new Promise(async (resolve, reject) => {
|
|
3059
|
+
try {
|
|
3060
|
+
if (option?.force) {
|
|
3061
|
+
await option.conn.execute(SyncMode.Async, `DROP TABLE IF EXISTS ${tableES};`);
|
|
3062
|
+
}
|
|
3063
|
+
const lastVersion = this[_sqlite_version] ?? '0.0.1';
|
|
3064
|
+
// 检查表
|
|
3065
|
+
const tableCheckResult = await option.conn.pluck(SyncMode.Async, `SELECT COUNT(1) t FROM sqlite_master WHERE TYPE = 'table' AND name = ?`, [option.tableName]);
|
|
3066
|
+
if (tableCheckResult) {
|
|
3067
|
+
// 旧版本
|
|
3068
|
+
const tableVersion = await option.conn.pluck(SyncMode.Async, 'SELECT ______version v from TABLE_VERSION WHERE ______tableName = ?', [option.tableName]);
|
|
3069
|
+
if (tableVersion && tableVersion < lastVersion) { // 发现需要升级的版本
|
|
3070
|
+
// 更新版本
|
|
3071
|
+
const columns = iterare(await option.conn.query(SyncMode.Async, `PRAGMA table_info(${tableES})`))
|
|
3072
|
+
.filter(c => this[_fields].hasOwnProperty(C2P(c.name, globalThis[_Hump])))
|
|
3073
|
+
.map(c => Sqlstring.escapeId(c.name))
|
|
3074
|
+
.join(',');
|
|
3075
|
+
const rtable = `${option.tableName}_${tableVersion.replace(/\./, '_')}`;
|
|
3076
|
+
await option.conn.execute(SyncMode.Async, `DROP TABLE IF EXISTS ${rtable};`);
|
|
3077
|
+
await option.conn.execute(SyncMode.Async, `ALTER TABLE ${tableES} RENAME TO ${rtable};`);
|
|
3078
|
+
await option.conn.execute(SyncMode.Async, `
|
|
3079
|
+
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
3080
|
+
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
3081
|
+
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
3082
|
+
);
|
|
3083
|
+
`);
|
|
3084
|
+
if (this[_index] && this[_index].length) {
|
|
3085
|
+
for (const index of this[_index]) {
|
|
3086
|
+
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${this[_fields][index]?.C2()}");`);
|
|
3087
|
+
}
|
|
3088
|
+
}
|
|
3089
|
+
await option.conn.execute(SyncMode.Async, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
3090
|
+
await option.conn.execute(SyncMode.Async, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
3091
|
+
await option.conn.execute(SyncMode.Async, `DROP TABLE IF EXISTS ${rtable};`);
|
|
3092
|
+
// 更新完毕,保存版本号
|
|
3093
|
+
await option.conn.execute(SyncMode.Async, 'UPDATE TABLE_VERSION SET ______version = ? WHERE ______tableName = ?', [option.tableName, lastVersion]);
|
|
3094
|
+
}
|
|
3095
|
+
else if (!tableVersion) { // 不需要升级情况:没有旧的版本号
|
|
3096
|
+
await option.conn.execute(SyncMode.Async, 'INSERT INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
3097
|
+
}
|
|
3098
|
+
}
|
|
3099
|
+
else { // 表不存在
|
|
3100
|
+
// 创建表
|
|
3101
|
+
await option.conn.execute(SyncMode.Async, `
|
|
3102
|
+
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
3103
|
+
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
3104
|
+
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
3105
|
+
);
|
|
3106
|
+
`);
|
|
3107
|
+
if (this[_index] && this[_index].length) {
|
|
3108
|
+
for (const index of this[_index]) {
|
|
3109
|
+
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${Sqlstring.escapeId(option.tableName)} ("${this[_fields][index]?.C2()}");`);
|
|
3110
|
+
}
|
|
3111
|
+
}
|
|
3112
|
+
await option.conn.execute(SyncMode.Async, 'INSERT OR REPLACE INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
3113
|
+
}
|
|
3114
|
+
resolve();
|
|
3115
|
+
}
|
|
3116
|
+
catch (error) {
|
|
3117
|
+
reject(error);
|
|
3118
|
+
}
|
|
3119
|
+
});
|
|
3120
|
+
}
|
|
3121
|
+
}
|
|
2979
3122
|
/**
|
|
2980
3123
|
#创建表
|
|
2981
3124
|
** `tableName` 表名称
|
|
@@ -3020,16 +3163,16 @@ export class SqlService {
|
|
|
3020
3163
|
tableName = Sqlstring.escapeId(tableName ?? this[_tableName]);
|
|
3021
3164
|
switch (this[_dbType]) {
|
|
3022
3165
|
case DBType.Mysql: {
|
|
3023
|
-
let sql =
|
|
3166
|
+
let sql = formatDialect(`CREATE ${temp === true ? 'TEMPORARY' : ''} TABLE IF NOT EXISTS ${tableName}(
|
|
3024
3167
|
${columns.map(K => this[_fields][K][DBType.Mysql]()).join(',')}
|
|
3025
3168
|
${ids && ids.length ? `,PRIMARY KEY (${ids.map(i => this[_fields][i]?.C2()).join(',')}) USING BTREE ` : ''}
|
|
3026
3169
|
${indexs && indexs.length ? `,${indexs.map(i => `KEY ${this[_fields][i]?.C2()} (${this[_fields][i]?.C2()})`).join(',')} ` : ''}
|
|
3027
|
-
) ENGINE=MEMORY
|
|
3170
|
+
) ENGINE=MEMORY;`, { dialect: this[_formatDialect] });
|
|
3028
3171
|
sqls.push({ sql });
|
|
3029
3172
|
if (data && data.length > 0) {
|
|
3030
3173
|
const params = [];
|
|
3031
3174
|
let first = true;
|
|
3032
|
-
sql =
|
|
3175
|
+
sql = formatDialect(`INSERT INTO ${tableName} (${columns.map(c => this[_fields][c]?.C2()).join(',')})
|
|
3033
3176
|
${(data).map(d => {
|
|
3034
3177
|
const r = `SELECT ${Object.entries(d).map(([K, V]) => {
|
|
3035
3178
|
params.push(V);
|
|
@@ -3037,28 +3180,28 @@ export class SqlService {
|
|
|
3037
3180
|
}).join(',')}`;
|
|
3038
3181
|
first = false;
|
|
3039
3182
|
return r;
|
|
3040
|
-
}).join(' UNION ALL ')}
|
|
3183
|
+
}).join(' UNION ALL ')}`, { dialect: this[_formatDialect] });
|
|
3041
3184
|
sqls.push({ sql, params });
|
|
3042
3185
|
}
|
|
3043
3186
|
break;
|
|
3044
3187
|
}
|
|
3045
3188
|
case DBType.Sqlite:
|
|
3046
3189
|
case DBType.SqliteRemote: {
|
|
3047
|
-
let sql =
|
|
3190
|
+
let sql = formatDialect(`CREATE ${temp === true ? 'TEMPORARY' : ''} TABLE IF NOT EXISTS ${tableName}(
|
|
3048
3191
|
${columns.map(K => this[_fields][K][DBType.Sqlite]()).join(',')}
|
|
3049
3192
|
${ids && ids.length ? `,PRIMARY KEY (${ids.map(i => this[_fields][i]?.C2()).join(',')}) ` : ''}
|
|
3050
|
-
)
|
|
3193
|
+
);`, { dialect: this[_formatDialect] });
|
|
3051
3194
|
sqls.push({ sql });
|
|
3052
3195
|
if (indexs) {
|
|
3053
3196
|
for (const index of indexs) {
|
|
3054
|
-
sql =
|
|
3197
|
+
sql = formatDialect(`CREATE INDEX ${Sqlstring.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableName} (${this[_fields][index]?.C2()});`, { dialect: this[_formatDialect] });
|
|
3055
3198
|
sqls.push({ sql });
|
|
3056
3199
|
}
|
|
3057
3200
|
}
|
|
3058
3201
|
if (data && data.length > 0) {
|
|
3059
3202
|
const params = [];
|
|
3060
3203
|
let first = true;
|
|
3061
|
-
sql =
|
|
3204
|
+
sql = formatDialect(`INSERT INTO ${tableName} (${columns.map(c => this[_fields][c]?.C2()).join(',')})
|
|
3062
3205
|
${(data).map(d => {
|
|
3063
3206
|
const r = `SELECT ${Object.entries(d).map(([K, V]) => {
|
|
3064
3207
|
params.push(V);
|
|
@@ -3066,7 +3209,7 @@ export class SqlService {
|
|
|
3066
3209
|
}).join(',')}`;
|
|
3067
3210
|
first = false;
|
|
3068
3211
|
return r;
|
|
3069
|
-
}).join(' UNION ALL ')}
|
|
3212
|
+
}).join(' UNION ALL ')}`, { dialect: this[_formatDialect] });
|
|
3070
3213
|
sqls.push({ sql, params });
|
|
3071
3214
|
}
|
|
3072
3215
|
break;
|
|
@@ -3130,6 +3273,12 @@ __decorate([
|
|
|
3130
3273
|
__metadata("design:paramtypes", [Object]),
|
|
3131
3274
|
__metadata("design:returntype", Object)
|
|
3132
3275
|
], SqlService.prototype, "page", null);
|
|
3276
|
+
__decorate([
|
|
3277
|
+
P(),
|
|
3278
|
+
__metadata("design:type", Function),
|
|
3279
|
+
__metadata("design:paramtypes", [Object]),
|
|
3280
|
+
__metadata("design:returntype", Object)
|
|
3281
|
+
], SqlService.prototype, "init", null);
|
|
3133
3282
|
/** 是否进行下一个动作 */
|
|
3134
3283
|
const IF_PROCEED = function () {
|
|
3135
3284
|
return function (_target, _propertyKey, descriptor) {
|
|
@@ -3507,7 +3656,10 @@ class StreamQuery {
|
|
|
3507
3656
|
}
|
|
3508
3657
|
}
|
|
3509
3658
|
if (sets.length > 0) {
|
|
3510
|
-
const sql = `UPDATE ${this._table} SET ${sets.join(',')}
|
|
3659
|
+
const sql = `UPDATE ${this._table} SET ${sets.join(',')}
|
|
3660
|
+
${where ? ' WHERE ' : ''}
|
|
3661
|
+
${where}
|
|
3662
|
+
`;
|
|
3511
3663
|
if (option.sync === SyncMode.Async) {
|
|
3512
3664
|
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
3513
3665
|
}
|
|
@@ -3523,7 +3675,10 @@ class StreamQuery {
|
|
|
3523
3675
|
option ?? (option = {});
|
|
3524
3676
|
option.sync ?? (option.sync = SyncMode.Async);
|
|
3525
3677
|
const { where, params } = this._where();
|
|
3526
|
-
const sql = `DELETE FROM ${this._table}
|
|
3678
|
+
const sql = `DELETE FROM ${this._table}
|
|
3679
|
+
${where ? ' WHERE ' : ''}
|
|
3680
|
+
${where}
|
|
3681
|
+
`;
|
|
3527
3682
|
if (option.sync === SyncMode.Async) {
|
|
3528
3683
|
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
3529
3684
|
}
|