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/cjs/sql.js
CHANGED
|
@@ -34,10 +34,10 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
34
34
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
35
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
36
|
};
|
|
37
|
-
var
|
|
38
|
-
var
|
|
37
|
+
var _b, _c, _d;
|
|
38
|
+
var _e, _f, _g, _h;
|
|
39
39
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
-
exports.SqlService = exports.DB = exports.Field = exports.SqlCache = exports.SqliteRemote = exports.SqliteRemoteConnection = exports.Sqlite = exports.Mysql = exports._defOption = exports.SqliteMemory = exports.ColumnMode = exports.SqlType = exports.SelectResult = exports.TemplateResult = exports.SelectMode = exports.DeleteMode = exports.InsertMode = exports.SyncMode = exports.MapperIfUndefined = exports.DBType = exports.logger = exports._fs = exports._path = exports._EventBus = exports._GlobalSqlOption = exports._Hump = exports._primaryDB = exports._dao = exports._sqlCache = void 0;
|
|
40
|
+
exports.SqlService = exports.DB = exports.Field = exports.SqlCache = exports.SqliteRemote = exports.SqliteRemoteConnection = exports.Sqlite = exports.Mysql = exports._defOption = exports.SqliteMemory = exports.ColumnMode = exports.SqlType = exports.SelectResult = exports.TemplateResult = exports.SelectMode = exports.DeleteMode = exports.InsertMode = exports.SyncMode = exports.MapperIfUndefined = exports.DBType = exports.logger = exports._fs = exports._path = exports._EventBus = exports._GlobalSqlOption = exports._Hump = exports._enums = exports._primaryDB = exports._dao = exports._sqlCache = exports.extensionCodec = void 0;
|
|
41
41
|
exports.flatData = flatData;
|
|
42
42
|
exports.DeclareClass = DeclareClass;
|
|
43
43
|
exports.DeclareService = DeclareService;
|
|
@@ -63,6 +63,33 @@ const html_parse_stringify_1 = __importDefault(require("html-parse-stringify"));
|
|
|
63
63
|
const convert_xml_1 = require("./convert-xml");
|
|
64
64
|
const list_1 = require("./list");
|
|
65
65
|
const lodash_get_1 = __importDefault(require("lodash.get"));
|
|
66
|
+
const msgpack_1 = require("@msgpack/msgpack");
|
|
67
|
+
BigInt.prototype.toJSON = function () { return this.toString(); };
|
|
68
|
+
const BIGINT_EXT_TYPE = 0;
|
|
69
|
+
exports.extensionCodec = new msgpack_1.ExtensionCodec();
|
|
70
|
+
exports.extensionCodec.register({
|
|
71
|
+
type: BIGINT_EXT_TYPE,
|
|
72
|
+
encode(input) {
|
|
73
|
+
if (typeof input === "bigint") {
|
|
74
|
+
if (input <= Number.MAX_SAFE_INTEGER && input >= Number.MIN_SAFE_INTEGER) {
|
|
75
|
+
return (0, msgpack_1.encode)(Number(input));
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
return (0, msgpack_1.encode)(String(input));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
decode(data) {
|
|
86
|
+
const val = (0, msgpack_1.decode)(data);
|
|
87
|
+
if (!(typeof val === "string" || typeof val === "number")) {
|
|
88
|
+
throw new msgpack_1.DecodeError(`unexpected BigInt source: ${val} (${typeof val})`);
|
|
89
|
+
}
|
|
90
|
+
return BigInt(val);
|
|
91
|
+
},
|
|
92
|
+
});
|
|
66
93
|
// #region 常量
|
|
67
94
|
const _daoDBName = Symbol('dbName');
|
|
68
95
|
const _tableName = Symbol('tableName');
|
|
@@ -70,6 +97,7 @@ const _className = Symbol('className');
|
|
|
70
97
|
const _ClassName = Symbol('ClassName');
|
|
71
98
|
const _vueName = Symbol('vueName');
|
|
72
99
|
const _ids = Symbol('ids');
|
|
100
|
+
const _logicIds = Symbol('logicIds');
|
|
73
101
|
const _columns = Symbol('columns');
|
|
74
102
|
const _columnsNoId = Symbol('columnsNoId');
|
|
75
103
|
const _fields = Symbol('fields');
|
|
@@ -78,10 +106,12 @@ const _deleteState = Symbol('deleteState');
|
|
|
78
106
|
const _transformer = Symbol('transformer');
|
|
79
107
|
const _index = Symbol('index');
|
|
80
108
|
const _def = Symbol('def');
|
|
109
|
+
const _comment = Symbol('comment');
|
|
81
110
|
exports._sqlCache = Symbol('sqlMap');
|
|
82
111
|
exports._dao = Symbol('dao');
|
|
83
112
|
exports._primaryDB = Symbol('primaryDB');
|
|
84
113
|
const _dbType = Symbol('dbType');
|
|
114
|
+
const _formatDialect = Symbol('FormatDialect');
|
|
85
115
|
const _sqlite_version = Symbol('sqlite_version');
|
|
86
116
|
const _daoConnection = Symbol('daoConnection');
|
|
87
117
|
const _inTransaction = Symbol('inTransaction');
|
|
@@ -90,6 +120,7 @@ const _sqliteRemoteName = Symbol('sqliteRemoteName');
|
|
|
90
120
|
const _SqlOption = Symbol('SqlOption');
|
|
91
121
|
const _resultMap = Symbol('resultMap');
|
|
92
122
|
const _resultMap_SQLID = Symbol('resultMap_SQLID');
|
|
123
|
+
exports._enums = Symbol('_enums');
|
|
93
124
|
exports._Hump = Symbol('Hump');
|
|
94
125
|
exports._GlobalSqlOption = Symbol('GlobalSqlOption');
|
|
95
126
|
exports._EventBus = Symbol('EventBus');
|
|
@@ -274,7 +305,7 @@ exports._defOption = {
|
|
|
274
305
|
;
|
|
275
306
|
class MysqlConnection {
|
|
276
307
|
constructor(conn) {
|
|
277
|
-
this[
|
|
308
|
+
this[_b] = false;
|
|
278
309
|
this[_daoConnection] = conn;
|
|
279
310
|
}
|
|
280
311
|
execute(sync, sql, params) {
|
|
@@ -457,7 +488,7 @@ class MysqlConnection {
|
|
|
457
488
|
;
|
|
458
489
|
}
|
|
459
490
|
}
|
|
460
|
-
|
|
491
|
+
_b = _inTransaction;
|
|
461
492
|
class Mysql {
|
|
462
493
|
constructor(pool) {
|
|
463
494
|
this[_daoDB] = pool;
|
|
@@ -549,7 +580,7 @@ class Mysql {
|
|
|
549
580
|
exports.Mysql = Mysql;
|
|
550
581
|
class SqliteConnection {
|
|
551
582
|
constructor(conn) {
|
|
552
|
-
this[
|
|
583
|
+
this[_c] = false;
|
|
553
584
|
this[_daoConnection] = conn;
|
|
554
585
|
}
|
|
555
586
|
execute(sync, sql, params) {
|
|
@@ -689,7 +720,7 @@ class SqliteConnection {
|
|
|
689
720
|
realse(sync) {
|
|
690
721
|
}
|
|
691
722
|
}
|
|
692
|
-
|
|
723
|
+
_c = _inTransaction;
|
|
693
724
|
class Sqlite {
|
|
694
725
|
constructor(db) {
|
|
695
726
|
this[_daoDB] = db;
|
|
@@ -755,7 +786,7 @@ class Sqlite {
|
|
|
755
786
|
exports.Sqlite = Sqlite;
|
|
756
787
|
class SqliteRemoteConnection {
|
|
757
788
|
constructor(conn, name) {
|
|
758
|
-
this[
|
|
789
|
+
this[_d] = false;
|
|
759
790
|
this[_daoConnection] = conn;
|
|
760
791
|
this[_sqliteRemoteName] = name;
|
|
761
792
|
}
|
|
@@ -775,7 +806,8 @@ class SqliteRemoteConnection {
|
|
|
775
806
|
}
|
|
776
807
|
return new Promise(async (resolve, reject) => {
|
|
777
808
|
try {
|
|
778
|
-
const
|
|
809
|
+
const data = await this[_daoConnection].execute((0, msgpack_1.encode)([this[_sqliteRemoteName], sql, params], { extensionCodec: exports.extensionCodec }));
|
|
810
|
+
const { affectedRows, insertId } = (0, msgpack_1.decode)(data, { extensionCodec: exports.extensionCodec });
|
|
779
811
|
resolve({ affectedRows, insertId: insertId ? BigInt(insertId) : 0n });
|
|
780
812
|
}
|
|
781
813
|
catch (error) {
|
|
@@ -804,7 +836,8 @@ class SqliteRemoteConnection {
|
|
|
804
836
|
}
|
|
805
837
|
return new Promise(async (resolve, reject) => {
|
|
806
838
|
try {
|
|
807
|
-
const
|
|
839
|
+
const data = await this[_daoConnection].pluck((0, msgpack_1.encode)([this[_sqliteRemoteName], sql, params], { extensionCodec: exports.extensionCodec }));
|
|
840
|
+
const r = (0, msgpack_1.decode)(data, { extensionCodec: exports.extensionCodec });
|
|
808
841
|
resolve(r);
|
|
809
842
|
}
|
|
810
843
|
catch (error) {
|
|
@@ -833,7 +866,8 @@ class SqliteRemoteConnection {
|
|
|
833
866
|
}
|
|
834
867
|
return new Promise(async (resolve, reject) => {
|
|
835
868
|
try {
|
|
836
|
-
const
|
|
869
|
+
const data = await this[_daoConnection].get((0, msgpack_1.encode)([this[_sqliteRemoteName], sql, params], { extensionCodec: exports.extensionCodec }));
|
|
870
|
+
const r = (0, msgpack_1.decode)(data, { extensionCodec: exports.extensionCodec });
|
|
837
871
|
resolve(r);
|
|
838
872
|
}
|
|
839
873
|
catch (error) {
|
|
@@ -862,7 +896,8 @@ class SqliteRemoteConnection {
|
|
|
862
896
|
}
|
|
863
897
|
return new Promise(async (resolve, reject) => {
|
|
864
898
|
try {
|
|
865
|
-
const
|
|
899
|
+
const data = await this[_daoConnection].raw((0, msgpack_1.encode)([this[_sqliteRemoteName], sql, params], { extensionCodec: exports.extensionCodec }));
|
|
900
|
+
const r = (0, msgpack_1.decode)(data, { extensionCodec: exports.extensionCodec });
|
|
866
901
|
resolve(r);
|
|
867
902
|
}
|
|
868
903
|
catch (error) {
|
|
@@ -891,7 +926,8 @@ class SqliteRemoteConnection {
|
|
|
891
926
|
}
|
|
892
927
|
return new Promise(async (resolve, reject) => {
|
|
893
928
|
try {
|
|
894
|
-
const
|
|
929
|
+
const data = await this[_daoConnection].query((0, msgpack_1.encode)([this[_sqliteRemoteName], sql, params], { extensionCodec: exports.extensionCodec }));
|
|
930
|
+
const r = (0, msgpack_1.decode)(data, { extensionCodec: exports.extensionCodec });
|
|
895
931
|
resolve(r);
|
|
896
932
|
}
|
|
897
933
|
catch (error) {
|
|
@@ -908,7 +944,7 @@ class SqliteRemoteConnection {
|
|
|
908
944
|
}
|
|
909
945
|
}
|
|
910
946
|
exports.SqliteRemoteConnection = SqliteRemoteConnection;
|
|
911
|
-
|
|
947
|
+
_d = _inTransaction;
|
|
912
948
|
class SqliteRemote {
|
|
913
949
|
constructor(db, name) {
|
|
914
950
|
this[_daoDB] = db;
|
|
@@ -921,8 +957,11 @@ class SqliteRemote {
|
|
|
921
957
|
}
|
|
922
958
|
;
|
|
923
959
|
return new Promise(async (resolve, reject) => {
|
|
960
|
+
if (!this.connection) {
|
|
961
|
+
this.connection = new SqliteRemoteConnection(this[_daoDB], this[_sqliteRemoteName]);
|
|
962
|
+
}
|
|
924
963
|
try {
|
|
925
|
-
resolve(
|
|
964
|
+
resolve(this.connection);
|
|
926
965
|
}
|
|
927
966
|
catch (error) {
|
|
928
967
|
reject(error);
|
|
@@ -1251,6 +1290,31 @@ class Build {
|
|
|
1251
1290
|
}
|
|
1252
1291
|
};
|
|
1253
1292
|
}
|
|
1293
|
+
/**
|
|
1294
|
+
* * PROBLEM_TYPE = 枚举名
|
|
1295
|
+
* * t.problemtype = 列名
|
|
1296
|
+
*
|
|
1297
|
+
* ```
|
|
1298
|
+
* {{#enumTag}} PROBLEM_TYPE(t.problemtype) {{/enumTag}}
|
|
1299
|
+
* ```
|
|
1300
|
+
*/
|
|
1301
|
+
enum() {
|
|
1302
|
+
return (text) => {
|
|
1303
|
+
const matchs = text.match(/([a-zA-Z_]+)\(([^()]+)\)/);
|
|
1304
|
+
if (matchs) {
|
|
1305
|
+
const [_a, MapName, Column] = matchs;
|
|
1306
|
+
if (MapName && Column) {
|
|
1307
|
+
const map = globalThis[exports._enums].GlobalMap[MapName.trim()];
|
|
1308
|
+
if (map) {
|
|
1309
|
+
return ` CASE
|
|
1310
|
+
${Object.entries(map).map(([k, v]) => `WHEN ${Column} = '${k}' THEN '${v}'`).join(' ')}
|
|
1311
|
+
END `;
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
return "''";
|
|
1316
|
+
};
|
|
1317
|
+
}
|
|
1254
1318
|
}
|
|
1255
1319
|
Build.page = 'COUNT(1) zccw1986 ';
|
|
1256
1320
|
function replaceCdata(rawText) {
|
|
@@ -1271,10 +1335,10 @@ function replaceCdata(rawText) {
|
|
|
1271
1335
|
return rawText;
|
|
1272
1336
|
}
|
|
1273
1337
|
function _flatData(result, i, length, keys, V) {
|
|
1274
|
-
var
|
|
1338
|
+
var _e;
|
|
1275
1339
|
const key = keys[i];
|
|
1276
1340
|
if (i < length) {
|
|
1277
|
-
result[
|
|
1341
|
+
result[_e = key] ?? (result[_e] = {});
|
|
1278
1342
|
i++;
|
|
1279
1343
|
_flatData(result[key], i, length, keys, V);
|
|
1280
1344
|
}
|
|
@@ -1469,19 +1533,14 @@ class SqlCache {
|
|
|
1469
1533
|
const buildParam = new Build(options.isCount === true, options.isSum === true, options);
|
|
1470
1534
|
if (typeof sqlSource === 'function') {
|
|
1471
1535
|
const _sql = sqlSource(options);
|
|
1472
|
-
|
|
1473
|
-
return (0, sql_formatter_1.format)(sql);
|
|
1536
|
+
return mustache_1.default.render(_sql, buildParam, this.sqlFNMap);
|
|
1474
1537
|
}
|
|
1475
1538
|
else if (typeof sqlSource === 'string') {
|
|
1476
|
-
|
|
1477
|
-
return (0, sql_formatter_1.format)(sql);
|
|
1539
|
+
return mustache_1.default.render(sqlSource, buildParam, this.sqlFNMap);
|
|
1478
1540
|
}
|
|
1479
1541
|
else if (typeof sqlSource === 'object') {
|
|
1480
1542
|
const _sql = (0, convert_xml_1.convert)(sqlSource, options, matchSqlid, this.sqlMap);
|
|
1481
|
-
|
|
1482
|
-
const sql = mustache_1.default.render(_sql, buildParam, this.sqlFNMap);
|
|
1483
|
-
console.log(sql);
|
|
1484
|
-
return (0, sql_formatter_1.format)(sql);
|
|
1543
|
+
return mustache_1.default.render(_sql, buildParam, this.sqlFNMap);
|
|
1485
1544
|
}
|
|
1486
1545
|
return '';
|
|
1487
1546
|
}
|
|
@@ -1513,12 +1572,9 @@ function P(skipConn = false) {
|
|
|
1513
1572
|
const option = args[0] = Object.assign({}, globalThis[exports._GlobalSqlOption], this[_SqlOption], args[0]);
|
|
1514
1573
|
option.sync ?? (option.sync = SyncMode.Async);
|
|
1515
1574
|
const dbName = option?.dbName ?? this[_daoDBName] ?? exports._primaryDB;
|
|
1516
|
-
const dddx = this[_dbType];
|
|
1517
|
-
exports.logger.info(dddx);
|
|
1518
1575
|
option.dao = globalThis[exports._dao][this[_dbType]][dbName];
|
|
1519
1576
|
error_1.Throw.if(!option.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
|
|
1520
1577
|
option.tableName = option?.tableName ?? this[_tableName];
|
|
1521
|
-
const tableES = sqlstring_1.default.escapeId(option.tableName);
|
|
1522
1578
|
if (this[_dbType] === DBType.Sqlite) {
|
|
1523
1579
|
error_1.Throw.if(option.sync === SyncMode.Async, 'sqlite can not Async!');
|
|
1524
1580
|
// 连接共享
|
|
@@ -1528,60 +1584,6 @@ function P(skipConn = false) {
|
|
|
1528
1584
|
else {
|
|
1529
1585
|
needRealseConn = false;
|
|
1530
1586
|
}
|
|
1531
|
-
if (skipConn === false) {
|
|
1532
|
-
const lastVersion = this[_sqlite_version] ?? '0.0.1';
|
|
1533
|
-
// 检查表
|
|
1534
|
-
const tableCheckResult = option.conn.pluck(SyncMode.Sync, `SELECT COUNT(1) t FROM sqlite_master WHERE TYPE = 'table' AND name = ?`, [option.tableName]);
|
|
1535
|
-
if (tableCheckResult) {
|
|
1536
|
-
// 旧版本
|
|
1537
|
-
const tableVersion = option.conn.pluck(SyncMode.Sync, 'SELECT ______version v from TABLE_VERSION WHERE ______tableName = ?', [option.tableName]);
|
|
1538
|
-
if (tableVersion && tableVersion < lastVersion) { // 发现需要升级的版本
|
|
1539
|
-
// 更新版本
|
|
1540
|
-
const columns = (0, iterare_1.default)(option.conn.query(SyncMode.Sync, `PRAGMA table_info(${tableES})`))
|
|
1541
|
-
.filter(c => this[_fields].hasOwnProperty((0, object_1.C2P)(c.name, globalThis[exports._Hump])))
|
|
1542
|
-
.map(c => sqlstring_1.default.escapeId(c.name))
|
|
1543
|
-
.join(',');
|
|
1544
|
-
const rtable = sqlstring_1.default.escapeId(`${option.tableName}_${tableVersion.replace(/\./, '_')}`);
|
|
1545
|
-
option.conn.execute(SyncMode.Sync, `DROP TABLE IF EXISTS ${rtable};`);
|
|
1546
|
-
option.conn.execute(SyncMode.Sync, `ALTER TABLE ${tableES} RENAME TO ${rtable};`);
|
|
1547
|
-
option.conn.execute(SyncMode.Sync, `
|
|
1548
|
-
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
1549
|
-
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
1550
|
-
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
1551
|
-
);
|
|
1552
|
-
`);
|
|
1553
|
-
if (this[_index] && this[_index].length) {
|
|
1554
|
-
for (const index of this[_index]) {
|
|
1555
|
-
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${sqlstring_1.default.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${this[_fields][index]?.C2()}");`);
|
|
1556
|
-
}
|
|
1557
|
-
}
|
|
1558
|
-
option.conn.execute(SyncMode.Sync, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
1559
|
-
option.conn.execute(SyncMode.Sync, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
1560
|
-
option.conn.execute(SyncMode.Sync, `DROP TABLE IF EXISTS ${rtable};`);
|
|
1561
|
-
// 更新完毕,保存版本号
|
|
1562
|
-
option.conn.execute(SyncMode.Sync, 'UPDATE TABLE_VERSION SET ______version = ? WHERE ______tableName = ?', [option.tableName, lastVersion]);
|
|
1563
|
-
}
|
|
1564
|
-
else if (!tableVersion) { // 不需要升级情况:没有旧的版本号
|
|
1565
|
-
option.conn.execute(SyncMode.Sync, 'INSERT INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
1566
|
-
}
|
|
1567
|
-
}
|
|
1568
|
-
else { // 表不存在
|
|
1569
|
-
// 创建表
|
|
1570
|
-
option.conn.execute(SyncMode.Sync, `
|
|
1571
|
-
CREATE TABLE IF NOT EXISTS ${tableES} (
|
|
1572
|
-
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
1573
|
-
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
1574
|
-
|
|
1575
|
-
);
|
|
1576
|
-
`);
|
|
1577
|
-
if (this[_index] && this[_index].length) {
|
|
1578
|
-
for (const index of this[_index]) {
|
|
1579
|
-
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${sqlstring_1.default.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${this[_fields][index]?.C2()}");`);
|
|
1580
|
-
}
|
|
1581
|
-
}
|
|
1582
|
-
option.conn.execute(SyncMode.Sync, 'INSERT OR REPLACE INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
1583
|
-
}
|
|
1584
|
-
}
|
|
1585
1587
|
try {
|
|
1586
1588
|
const result = fn.call(this, ...args);
|
|
1587
1589
|
exports.logger.info(`${propertyKey}:${option.tableName}:use ${+new Date() - startTime}ms`);
|
|
@@ -1615,59 +1617,6 @@ function P(skipConn = false) {
|
|
|
1615
1617
|
else {
|
|
1616
1618
|
needRealseConn = false;
|
|
1617
1619
|
}
|
|
1618
|
-
if (skipConn === false) {
|
|
1619
|
-
const lastVersion = this[_sqlite_version] ?? '0.0.1';
|
|
1620
|
-
// 检查表
|
|
1621
|
-
const tableCheckResult = await option.conn.pluck(SyncMode.Async, `SELECT COUNT(1) t FROM sqlite_master WHERE TYPE = 'table' AND name = ?`, [option.tableName]);
|
|
1622
|
-
if (tableCheckResult) {
|
|
1623
|
-
// 旧版本
|
|
1624
|
-
const tableVersion = await option.conn.pluck(SyncMode.Async, 'SELECT ______version v from TABLE_VERSION WHERE ______tableName = ?', [option.tableName]);
|
|
1625
|
-
if (tableVersion && tableVersion < lastVersion) { // 发现需要升级的版本
|
|
1626
|
-
// 更新版本
|
|
1627
|
-
const columns = (0, iterare_1.default)(await option.conn.query(SyncMode.Async, `PRAGMA table_info(${tableES})`))
|
|
1628
|
-
.filter(c => this[_fields].hasOwnProperty((0, object_1.C2P)(c.name, globalThis[exports._Hump])))
|
|
1629
|
-
.map(c => sqlstring_1.default.escapeId(c.name))
|
|
1630
|
-
.join(',');
|
|
1631
|
-
const rtable = `${option.tableName}_${tableVersion.replace(/\./, '_')}`;
|
|
1632
|
-
await option.conn.execute(SyncMode.Async, `DROP TABLE IF EXISTS ${rtable};`);
|
|
1633
|
-
await option.conn.execute(SyncMode.Async, `ALTER TABLE ${tableES} RENAME TO ${rtable};`);
|
|
1634
|
-
await option.conn.execute(SyncMode.Async, `
|
|
1635
|
-
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
1636
|
-
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
1637
|
-
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
1638
|
-
);
|
|
1639
|
-
`);
|
|
1640
|
-
if (this[_index] && this[_index].length) {
|
|
1641
|
-
for (const index of this[_index]) {
|
|
1642
|
-
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${sqlstring_1.default.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${this[_fields][index]?.C2()}");`);
|
|
1643
|
-
}
|
|
1644
|
-
}
|
|
1645
|
-
await option.conn.execute(SyncMode.Async, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
1646
|
-
await option.conn.execute(SyncMode.Async, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
1647
|
-
await option.conn.execute(SyncMode.Async, `DROP TABLE IF EXISTS ${rtable};`);
|
|
1648
|
-
// 更新完毕,保存版本号
|
|
1649
|
-
await option.conn.execute(SyncMode.Async, 'UPDATE TABLE_VERSION SET ______version = ? WHERE ______tableName = ?', [option.tableName, lastVersion]);
|
|
1650
|
-
}
|
|
1651
|
-
else if (!tableVersion) { // 不需要升级情况:没有旧的版本号
|
|
1652
|
-
await option.conn.execute(SyncMode.Async, 'INSERT INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
1653
|
-
}
|
|
1654
|
-
}
|
|
1655
|
-
else { // 表不存在
|
|
1656
|
-
// 创建表
|
|
1657
|
-
await option.conn.execute(SyncMode.Async, `
|
|
1658
|
-
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
1659
|
-
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
1660
|
-
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
1661
|
-
);
|
|
1662
|
-
`);
|
|
1663
|
-
if (this[_index] && this[_index].length) {
|
|
1664
|
-
for (const index of this[_index]) {
|
|
1665
|
-
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${sqlstring_1.default.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${sqlstring_1.default.escapeId(option.tableName)} ("${this[_fields][index]?.C2()}");`);
|
|
1666
|
-
}
|
|
1667
|
-
}
|
|
1668
|
-
await option.conn.execute(SyncMode.Async, 'INSERT OR REPLACE INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
1669
|
-
}
|
|
1670
|
-
}
|
|
1671
1620
|
try {
|
|
1672
1621
|
const result = await fn.call(this, ...args);
|
|
1673
1622
|
exports.logger.info(`${propertyKey}:${option.tableName}:use ${+new Date() - startTime}ms`);
|
|
@@ -1799,6 +1748,7 @@ const Field = (config) => {
|
|
|
1799
1748
|
case SqlType.bigint: {
|
|
1800
1749
|
field[DBType.Mysql] = () => `${field.C2()} bigint ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1801
1750
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} integer`;
|
|
1751
|
+
field.Data2SQL = (data) => BigInt(data ?? 0);
|
|
1802
1752
|
break;
|
|
1803
1753
|
}
|
|
1804
1754
|
case SqlType.float: {
|
|
@@ -1834,26 +1784,31 @@ const Field = (config) => {
|
|
|
1834
1784
|
case SqlType.date: {
|
|
1835
1785
|
field[DBType.Mysql] = () => `${field.C2()} date ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1836
1786
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1787
|
+
field.Data2SQL = (data) => typeof data === 'string' ? new Date(data) : data;
|
|
1837
1788
|
break;
|
|
1838
1789
|
}
|
|
1839
1790
|
case SqlType.time: {
|
|
1840
1791
|
field[DBType.Mysql] = () => `${field.C2()} time ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1841
1792
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1793
|
+
field.Data2SQL = (data) => typeof data === 'string' ? new Date(data) : data;
|
|
1842
1794
|
break;
|
|
1843
1795
|
}
|
|
1844
1796
|
case SqlType.year: {
|
|
1845
1797
|
field[DBType.Mysql] = () => `${field.C2()} year ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1846
1798
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1799
|
+
field.Data2SQL = (data) => typeof data === 'string' ? new Date(data) : data;
|
|
1847
1800
|
break;
|
|
1848
1801
|
}
|
|
1849
1802
|
case SqlType.datetime: {
|
|
1850
1803
|
field[DBType.Mysql] = () => `${field.C2()} datetime ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1851
1804
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} text`;
|
|
1805
|
+
field.Data2SQL = (data) => typeof data === 'string' ? new Date(data) : data;
|
|
1852
1806
|
break;
|
|
1853
1807
|
}
|
|
1854
1808
|
case SqlType.timestamp: {
|
|
1855
1809
|
field[DBType.Mysql] = () => `${field.C2()} timestamp ${config.notNull === true ? 'NOT NULL' : ''} ${MYSQLCHARSET} ${hasDef ? `DEFAULT '${field.def}'` : ''}`;
|
|
1856
1810
|
field[DBType.SqliteRemote] = field[DBType.Sqlite] = () => `${field.C2()} integer`;
|
|
1811
|
+
field.Data2SQL = (data) => typeof data === 'string' ? +new Date(data) : data;
|
|
1857
1812
|
break;
|
|
1858
1813
|
}
|
|
1859
1814
|
case SqlType.char: {
|
|
@@ -1967,6 +1922,7 @@ const Field = (config) => {
|
|
|
1967
1922
|
let __columns = Reflect.getMetadata(_columns, object);
|
|
1968
1923
|
let __columnsNoId = Reflect.getMetadata(_columnsNoId, object);
|
|
1969
1924
|
let __ids = Reflect.getMetadata(_ids, object);
|
|
1925
|
+
let __logicIds = Reflect.getMetadata(_logicIds, object);
|
|
1970
1926
|
let __index = Reflect.getMetadata(_index, object);
|
|
1971
1927
|
let __def = Reflect.getMetadata(_def, object);
|
|
1972
1928
|
if (!__fields) {
|
|
@@ -1985,16 +1941,23 @@ const Field = (config) => {
|
|
|
1985
1941
|
else {
|
|
1986
1942
|
__columnsNoId.push(propertyName);
|
|
1987
1943
|
}
|
|
1944
|
+
if (field.logicId === true) {
|
|
1945
|
+
__logicIds.push(propertyName);
|
|
1946
|
+
}
|
|
1988
1947
|
if (field.index === true) {
|
|
1989
1948
|
__index.push(propertyName);
|
|
1990
1949
|
}
|
|
1991
1950
|
if (hasDef) {
|
|
1992
1951
|
__def[propertyName] = field.def;
|
|
1993
1952
|
}
|
|
1953
|
+
if (field.comment) {
|
|
1954
|
+
__def[propertyName] = field.comment;
|
|
1955
|
+
}
|
|
1994
1956
|
Reflect.defineMetadata(_fields, __fields, object);
|
|
1995
1957
|
Reflect.defineMetadata(_columns, __columns, object);
|
|
1996
1958
|
Reflect.defineMetadata(_columnsNoId, __columnsNoId, object);
|
|
1997
1959
|
Reflect.defineMetadata(_ids, __ids, object);
|
|
1960
|
+
Reflect.defineMetadata(_logicIds, __logicIds, object);
|
|
1998
1961
|
Reflect.defineMetadata(_index, __index, object);
|
|
1999
1962
|
Reflect.defineMetadata(_def, __def, object);
|
|
2000
1963
|
if (field.hasOwnProperty('logicDelete')) {
|
|
@@ -2004,44 +1967,58 @@ const Field = (config) => {
|
|
|
2004
1967
|
};
|
|
2005
1968
|
};
|
|
2006
1969
|
exports.Field = Field;
|
|
1970
|
+
const formatDialects = {
|
|
1971
|
+
[DBType.Mysql]: sql_formatter_1.mysql,
|
|
1972
|
+
[DBType.Sqlite]: sql_formatter_1.sqlite,
|
|
1973
|
+
[DBType.SqliteRemote]: sql_formatter_1.sqlite
|
|
1974
|
+
};
|
|
2007
1975
|
const DB = (config) => {
|
|
2008
1976
|
return function (constructor) {
|
|
2009
|
-
var
|
|
1977
|
+
var _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0;
|
|
2010
1978
|
const __ids = Reflect.getMetadata(_ids, config.clz.prototype) || new Array;
|
|
1979
|
+
const __logicIds = Reflect.getMetadata(_logicIds, config.clz.prototype) || new Array;
|
|
2011
1980
|
const __fields = Reflect.getMetadata(_fields, config.clz.prototype);
|
|
2012
1981
|
const __columns = Reflect.getMetadata(_columns, config.clz.prototype);
|
|
2013
|
-
const __columnsNoId =
|
|
1982
|
+
const __columnsNoId = Reflect.getMetadata(_columnsNoId, config.clz.prototype);
|
|
2014
1983
|
const __stateFileName = Reflect.getMetadata(_stateFileName, config.clz.prototype);
|
|
2015
1984
|
const __deleteState = Reflect.getMetadata(_deleteState, config.clz.prototype);
|
|
2016
1985
|
const __index = Reflect.getMetadata(_index, config.clz.prototype);
|
|
2017
1986
|
const __def = Reflect.getMetadata(_def, config.clz.prototype);
|
|
1987
|
+
const __dbType = config.dbType ?? DBType.Mysql;
|
|
1988
|
+
const __formatDialect = formatDialects[__dbType];
|
|
2018
1989
|
const className = config.tableName?.replace(/_(\w)/g, (a, b) => b.toUpperCase());
|
|
2019
1990
|
const ClassName = className?.replace(/\w/, (v) => v.toUpperCase());
|
|
2020
1991
|
const vueName = config.tableName?.replace(/_/g, '-');
|
|
2021
|
-
return
|
|
1992
|
+
return _0 = class extends constructor {
|
|
2022
1993
|
constructor() {
|
|
2023
1994
|
super(...arguments);
|
|
2024
|
-
this[
|
|
2025
|
-
this[
|
|
2026
|
-
this[
|
|
2027
|
-
this[
|
|
2028
|
-
this[
|
|
2029
|
-
this[
|
|
2030
|
-
this[
|
|
2031
|
-
this[
|
|
2032
|
-
this[
|
|
2033
|
-
this[
|
|
2034
|
-
this[
|
|
2035
|
-
this[
|
|
2036
|
-
this[
|
|
2037
|
-
this[
|
|
2038
|
-
this[
|
|
2039
|
-
this[
|
|
2040
|
-
this[
|
|
1995
|
+
this[_e] = config.tableName;
|
|
1996
|
+
this[_f] = className;
|
|
1997
|
+
this[_g] = ClassName;
|
|
1998
|
+
this[_h] = vueName;
|
|
1999
|
+
this[_j] = config.dbName;
|
|
2000
|
+
this[_k] = __dbType;
|
|
2001
|
+
this[_l] = __formatDialect;
|
|
2002
|
+
this[_m] = config.sqliteVersion;
|
|
2003
|
+
this[_o] = Object.assign({}, exports._defOption, config);
|
|
2004
|
+
this[_p] = __ids;
|
|
2005
|
+
this[_q] = __logicIds;
|
|
2006
|
+
this[_r] = __fields;
|
|
2007
|
+
this[_s] = __columns;
|
|
2008
|
+
this[_t] = __columnsNoId;
|
|
2009
|
+
this[_u] = __index;
|
|
2010
|
+
this[_v] = __def;
|
|
2011
|
+
this[_w] = config.comment;
|
|
2012
|
+
this[_x] = __stateFileName;
|
|
2013
|
+
this[_y] = __deleteState;
|
|
2014
|
+
this[_z] = (data, option) => {
|
|
2041
2015
|
return Object.fromEntries((0, iterare_1.default)(option?.skipId === true ? __columnsNoId : __columns)
|
|
2042
2016
|
.map(K => [K, FieldFilter(K, data[K], __def, __fields[K].uuid === true || __fields[K].uuidShort === true, option)])
|
|
2043
2017
|
.filter(data => {
|
|
2044
2018
|
if (data[1][0] === 1) {
|
|
2019
|
+
if (__fields[data[0]].Data2SQL) {
|
|
2020
|
+
data[1][1] = __fields[data[0]].Data2SQL(data[1][1]);
|
|
2021
|
+
}
|
|
2045
2022
|
if (option?.onFieldExists) {
|
|
2046
2023
|
option.onFieldExists(data[0], data[1][1]);
|
|
2047
2024
|
}
|
|
@@ -2056,24 +2033,27 @@ const DB = (config) => {
|
|
|
2056
2033
|
};
|
|
2057
2034
|
}
|
|
2058
2035
|
},
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2036
|
+
_e = _tableName,
|
|
2037
|
+
_f = _className,
|
|
2038
|
+
_g = _ClassName,
|
|
2039
|
+
_h = _vueName,
|
|
2040
|
+
_j = _daoDBName,
|
|
2041
|
+
_k = _dbType,
|
|
2042
|
+
_l = _formatDialect,
|
|
2043
|
+
_m = _sqlite_version,
|
|
2044
|
+
_o = _SqlOption,
|
|
2045
|
+
_p = _ids,
|
|
2046
|
+
_q = _logicIds,
|
|
2047
|
+
_r = _fields,
|
|
2048
|
+
_s = _columns,
|
|
2049
|
+
_t = _columnsNoId,
|
|
2050
|
+
_u = _index,
|
|
2051
|
+
_v = _def,
|
|
2052
|
+
_w = _comment,
|
|
2053
|
+
_x = _stateFileName,
|
|
2054
|
+
_y = _deleteState,
|
|
2055
|
+
_z = _transformer,
|
|
2056
|
+
_0;
|
|
2077
2057
|
};
|
|
2078
2058
|
};
|
|
2079
2059
|
exports.DB = DB;
|
|
@@ -2170,10 +2150,10 @@ class SqlService {
|
|
|
2170
2150
|
return `SELECT ${questMark.join(',')} FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM ${tableName} WHERE ${where})`;
|
|
2171
2151
|
});
|
|
2172
2152
|
const columnNames = (0, iterare_1.default)(finalColumns).map(i => this[_fields][i]?.C2()).join(',');
|
|
2173
|
-
const sql = (0, sql_formatter_1.
|
|
2153
|
+
const sql = (0, sql_formatter_1.formatDialect)(`INSERT INTO
|
|
2174
2154
|
${tableName}
|
|
2175
2155
|
(${columnNames})
|
|
2176
|
-
${questMarks.join(' UNION ALL ')}
|
|
2156
|
+
${questMarks.join(' UNION ALL ')};`, { dialect: this[_formatDialect] });
|
|
2177
2157
|
sqls.push({ sql, params });
|
|
2178
2158
|
break;
|
|
2179
2159
|
}
|
|
@@ -2211,12 +2191,12 @@ class SqlService {
|
|
|
2211
2191
|
return `(${questMark.join(',')})`;
|
|
2212
2192
|
});
|
|
2213
2193
|
const columnNames = (0, iterare_1.default)(finalColumns).map(i => this[_fields][i]?.C2()).join(',');
|
|
2214
|
-
const sql = (0, sql_formatter_1.
|
|
2194
|
+
const sql = (0, sql_formatter_1.formatDialect)(`
|
|
2215
2195
|
${this[_dbType] === DBType.Mysql ? '' : 'INSERT OR'} REPLACE INTO
|
|
2216
2196
|
${tableName}
|
|
2217
2197
|
(${columnNames})
|
|
2218
2198
|
VALUES ${questMarks};
|
|
2219
|
-
|
|
2199
|
+
`, { dialect: this[_formatDialect] });
|
|
2220
2200
|
sqls.push({ sql, params });
|
|
2221
2201
|
break;
|
|
2222
2202
|
}
|
|
@@ -2254,12 +2234,12 @@ class SqlService {
|
|
|
2254
2234
|
return `(${questMark.join(',')})`;
|
|
2255
2235
|
});
|
|
2256
2236
|
const columnNames = (0, iterare_1.default)(finalColumns).map(i => this[_fields][i]?.C2()).join(',');
|
|
2257
|
-
const sql = (0, sql_formatter_1.
|
|
2237
|
+
const sql = (0, sql_formatter_1.formatDialect)(`
|
|
2258
2238
|
INSERT INTO
|
|
2259
2239
|
${tableName}
|
|
2260
2240
|
(${columnNames})
|
|
2261
2241
|
VALUES ${questMarks};
|
|
2262
|
-
|
|
2242
|
+
`, { dialect: this[_formatDialect] });
|
|
2263
2243
|
sqls.push({ sql, params });
|
|
2264
2244
|
break;
|
|
2265
2245
|
}
|
|
@@ -2303,16 +2283,16 @@ class SqlService {
|
|
|
2303
2283
|
sqls.push(..._sqls);
|
|
2304
2284
|
const columnNames = (0, iterare_1.default)(finalColumns).map(i => this[_fields][i]?.C2()).join(',');
|
|
2305
2285
|
sqls.push({
|
|
2306
|
-
sql: (0, sql_formatter_1.
|
|
2286
|
+
sql: (0, sql_formatter_1.formatDialect)(`
|
|
2307
2287
|
INSERT INTO
|
|
2308
2288
|
${tableTemp}
|
|
2309
2289
|
(${columnNames})
|
|
2310
2290
|
VALUES ${questMarks};
|
|
2311
|
-
|
|
2291
|
+
`, { dialect: this[_formatDialect] }), params
|
|
2312
2292
|
});
|
|
2313
2293
|
sqls.push({
|
|
2314
|
-
sql: (0, sql_formatter_1.
|
|
2315
|
-
SELECT ${columnNames} FROM ${tableTemp}
|
|
2294
|
+
sql: (0, sql_formatter_1.formatDialect)(`INSERT INTO ${sqlstring_1.default.escapeId(option.tableName)} (${columnNames})
|
|
2295
|
+
SELECT ${columnNames} FROM ${tableTemp};`, { dialect: this[_formatDialect] })
|
|
2316
2296
|
});
|
|
2317
2297
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
2318
2298
|
break;
|
|
@@ -2351,20 +2331,18 @@ class SqlService {
|
|
|
2351
2331
|
}
|
|
2352
2332
|
else if (isArray) {
|
|
2353
2333
|
const fn = async () => {
|
|
2354
|
-
|
|
2355
|
-
const
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
result += dd.insertId;
|
|
2362
|
-
}
|
|
2334
|
+
const result = await (0, fn_1.excuteSplit)(fn_1.ExcuteSplitMode.AsyncTrust, datas, async (_data) => {
|
|
2335
|
+
const sqls = this._insert(_data, option);
|
|
2336
|
+
let result = 0n;
|
|
2337
|
+
for (const { sql, params } of sqls) {
|
|
2338
|
+
const dd = await option.conn.execute(SyncMode.Async, sql, params);
|
|
2339
|
+
if (dd.insertId) {
|
|
2340
|
+
result += dd.insertId;
|
|
2363
2341
|
}
|
|
2364
|
-
|
|
2365
|
-
}, { everyLength: option?.every === true ? 1 : option?.maxDeal });
|
|
2342
|
+
}
|
|
2366
2343
|
return result;
|
|
2367
|
-
}, option?.
|
|
2344
|
+
}, { everyLength: option?.every === true ? 1 : option?.maxDeal });
|
|
2345
|
+
return result;
|
|
2368
2346
|
};
|
|
2369
2347
|
return new Promise(async (resolve, reject) => {
|
|
2370
2348
|
try {
|
|
@@ -2433,13 +2411,13 @@ class SqlService {
|
|
|
2433
2411
|
}
|
|
2434
2412
|
});
|
|
2435
2413
|
}
|
|
2436
|
-
const sql = (0, sql_formatter_1.
|
|
2414
|
+
const sql = (0, sql_formatter_1.formatDialect)(`UPDATE ${tableName} SET ${(0, iterare_1.default)(this[_columnsNoId])
|
|
2437
2415
|
.filter(K => columnMaps[K].where.length > 0)
|
|
2438
2416
|
.map(K => {
|
|
2439
2417
|
params.push(...columnMaps[K].params);
|
|
2440
2418
|
return `${this[_fields][K]?.C2()} = CASE ${columnMaps[K].where.join(' ')} ELSE ${this[_fields][K]?.C2()} END`;
|
|
2441
2419
|
})
|
|
2442
|
-
.join(',')}
|
|
2420
|
+
.join(',')};`, { dialect: this[_formatDialect] });
|
|
2443
2421
|
sqls.push({ sql, params });
|
|
2444
2422
|
return sqls;
|
|
2445
2423
|
}
|
|
@@ -2528,14 +2506,14 @@ class SqlService {
|
|
|
2528
2506
|
if (this[_stateFileName] !== undefined && option.forceDelete !== true) {
|
|
2529
2507
|
params.unshift(this[_deleteState]);
|
|
2530
2508
|
sqls.push({
|
|
2531
|
-
sql: (0, sql_formatter_1.
|
|
2509
|
+
sql: (0, sql_formatter_1.formatDialect)(`
|
|
2532
2510
|
UPDATE ${tableNameESC} SET ${this[_fields][this[_stateFileName]]?.C2()} = ?
|
|
2533
2511
|
WHERE ${whereSql};
|
|
2534
|
-
|
|
2512
|
+
`, { dialect: this[_formatDialect] }), params
|
|
2535
2513
|
});
|
|
2536
2514
|
}
|
|
2537
2515
|
else {
|
|
2538
|
-
sqls.push({ sql: (0, sql_formatter_1.
|
|
2516
|
+
sqls.push({ sql: (0, sql_formatter_1.formatDialect)(`DELETE FROM ${tableNameESC} WHERE ${whereSql};`, { dialect: this[_formatDialect] }), params });
|
|
2539
2517
|
}
|
|
2540
2518
|
}
|
|
2541
2519
|
else {
|
|
@@ -2547,14 +2525,14 @@ class SqlService {
|
|
|
2547
2525
|
case DBType.Mysql: {
|
|
2548
2526
|
if (this[_stateFileName] !== undefined && option.forceDelete !== true) {
|
|
2549
2527
|
sqls.push({
|
|
2550
|
-
sql: (0, sql_formatter_1.
|
|
2551
|
-
SET a.${this[_fields][this[_stateFileName]]?.C2()} =
|
|
2528
|
+
sql: (0, sql_formatter_1.formatDialect)(`UPDATE ${tableNameESC} a INNER JOIN ${tableTempESC} b ON ${delWhere.map(K => `a.${this[_fields][K]?.C2()} = b.${this[_fields][K]?.C2()}`).join(' AND ')}
|
|
2529
|
+
SET a.${this[_fields][this[_stateFileName]]?.C2()} = ?;`, { dialect: this[_formatDialect] }),
|
|
2552
2530
|
params: [this[_deleteState]]
|
|
2553
2531
|
});
|
|
2554
2532
|
}
|
|
2555
2533
|
else {
|
|
2556
2534
|
sqls.push({
|
|
2557
|
-
sql: (0, sql_formatter_1.
|
|
2535
|
+
sql: (0, sql_formatter_1.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] })
|
|
2558
2536
|
});
|
|
2559
2537
|
}
|
|
2560
2538
|
break;
|
|
@@ -2564,13 +2542,13 @@ class SqlService {
|
|
|
2564
2542
|
const columnNames = (0, iterare_1.default)(delWhere).map(K => this[_fields][K]?.C2()).join(',');
|
|
2565
2543
|
if (this[_stateFileName] !== undefined && option.forceDelete !== true) {
|
|
2566
2544
|
sqls.push({
|
|
2567
|
-
sql: (0, sql_formatter_1.
|
|
2568
|
-
WHERE (${columnNames}) IN (SELECT ${columnNames} FROM ${tableTempESC})
|
|
2545
|
+
sql: (0, sql_formatter_1.formatDialect)(`UPDATE ${tableNameESC} SET ${this[_fields][this[_stateFileName]]?.C2()} = ?
|
|
2546
|
+
WHERE (${columnNames}) IN (SELECT ${columnNames} FROM ${tableTempESC});`, { dialect: this[_formatDialect] }),
|
|
2569
2547
|
params: [this[_deleteState]]
|
|
2570
2548
|
});
|
|
2571
2549
|
}
|
|
2572
2550
|
else {
|
|
2573
|
-
sqls.push({ sql: (0, sql_formatter_1.
|
|
2551
|
+
sqls.push({ sql: (0, sql_formatter_1.formatDialect)(`DELETE FROM ${tableNameESC} WHERE (${columnNames}) IN (SELECT ${columnNames} FROM ${tableTempESC});`, { dialect: this[_formatDialect] }) });
|
|
2574
2552
|
}
|
|
2575
2553
|
break;
|
|
2576
2554
|
}
|
|
@@ -2624,8 +2602,7 @@ class SqlService {
|
|
|
2624
2602
|
return result[0];
|
|
2625
2603
|
}
|
|
2626
2604
|
case TemplateResult.NotSureOne: {
|
|
2627
|
-
|
|
2628
|
-
return result[0] ?? null;
|
|
2605
|
+
return result && result.length > 0 ? result[0] ?? null : null;
|
|
2629
2606
|
}
|
|
2630
2607
|
case TemplateResult.Many: {
|
|
2631
2608
|
return result;
|
|
@@ -2661,13 +2638,13 @@ class SqlService {
|
|
|
2661
2638
|
let resultIndex = -1;
|
|
2662
2639
|
if (option.mode === SelectMode.Common) {
|
|
2663
2640
|
const params = new Array();
|
|
2664
|
-
const whereSql = (0, sql_formatter_1.
|
|
2641
|
+
const whereSql = (0, sql_formatter_1.formatDialect)((0, iterare_1.default)(wheres).map(where => this[_transformer](where, option)).map(where => {
|
|
2665
2642
|
return `SELECT ${columns} FROM ${tableNameESC} a WHERE
|
|
2666
2643
|
${Object.entries(where).map(([K, V]) => {
|
|
2667
2644
|
params.push(V);
|
|
2668
2645
|
return `${this[_fields][K]?.C2()} = ?`;
|
|
2669
2646
|
}).join(' AND ')}`;
|
|
2670
|
-
}).join(' UNION ALL '));
|
|
2647
|
+
}).join(' UNION ALL '), { dialect: this[_formatDialect] });
|
|
2671
2648
|
sqls.push({ sql: whereSql, params });
|
|
2672
2649
|
resultIndex = 0;
|
|
2673
2650
|
}
|
|
@@ -2677,7 +2654,7 @@ class SqlService {
|
|
|
2677
2654
|
const _sqls = this._createTable({ tableName: tableTemp, temp: true, columns: delWhere, data: wheres, index: 'all', id: 'none' });
|
|
2678
2655
|
sqls.push(..._sqls);
|
|
2679
2656
|
resultIndex = sqls.length;
|
|
2680
|
-
sqls.push({ sql: (0, sql_formatter_1.
|
|
2657
|
+
sqls.push({ sql: (0, sql_formatter_1.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] }) });
|
|
2681
2658
|
sqls.push({ sql: `DROP TABLE IF EXISTS ${tableTempESC};` });
|
|
2682
2659
|
}
|
|
2683
2660
|
if (option.sync === SyncMode.Sync) {
|
|
@@ -2806,7 +2783,7 @@ class SqlService {
|
|
|
2806
2783
|
const _params = Object.assign({}, option.context, option.params);
|
|
2807
2784
|
option.sql ?? (option.sql = globalThis[exports._sqlCache].load(this._matchSqlid(option.sqlId), { ctx: option.context, isCount: option.isCount, ..._params }));
|
|
2808
2785
|
const params = [];
|
|
2809
|
-
const sql = option.sql?.replace(/\:([A-Za-z0-9._]+)/g, (txt, key) => {
|
|
2786
|
+
const sql = (0, sql_formatter_1.formatDialect)(option.sql?.replace(/\:([A-Za-z0-9._]+)/g, (txt, key) => {
|
|
2810
2787
|
let V = (0, lodash_get_1.default)(_params, key);
|
|
2811
2788
|
if (V) {
|
|
2812
2789
|
if (V !== undefined) {
|
|
@@ -2831,7 +2808,7 @@ class SqlService {
|
|
|
2831
2808
|
return '?';
|
|
2832
2809
|
}
|
|
2833
2810
|
return txt;
|
|
2834
|
-
});
|
|
2811
|
+
}), { dialect: this[_formatDialect] });
|
|
2835
2812
|
if (option.sync === SyncMode.Sync) {
|
|
2836
2813
|
const result = option.conn.query(SyncMode.Sync, sql, params);
|
|
2837
2814
|
return this._select(option.selectResult, result, option.defValue, option.errorMsg, option.hump, option.mapper, option.mapperIfUndefined);
|
|
@@ -2853,7 +2830,7 @@ class SqlService {
|
|
|
2853
2830
|
const _params = Object.assign({}, option.context, option.params);
|
|
2854
2831
|
option.sql ?? (option.sql = globalThis[exports._sqlCache].load(this._matchSqlid(option.sqlId), { ctx: option.context, ..._params }));
|
|
2855
2832
|
const params = [];
|
|
2856
|
-
const sql = option.sql?.replace(/\:(\w+)/g, (txt, key) => {
|
|
2833
|
+
const sql = (0, sql_formatter_1.formatDialect)(option.sql?.replace(/\:(\w+)/g, (txt, key) => {
|
|
2857
2834
|
let V = (0, lodash_get_1.default)(_params, key);
|
|
2858
2835
|
if (V) {
|
|
2859
2836
|
if (V !== undefined) {
|
|
@@ -2878,7 +2855,7 @@ class SqlService {
|
|
|
2878
2855
|
return '?';
|
|
2879
2856
|
}
|
|
2880
2857
|
return txt;
|
|
2881
|
-
});
|
|
2858
|
+
}), { dialect: this[_formatDialect] });
|
|
2882
2859
|
if (option.sync === SyncMode.Sync) {
|
|
2883
2860
|
const result = option.conn.execute(SyncMode.Sync, sql, params);
|
|
2884
2861
|
return result.affectedRows;
|
|
@@ -2921,6 +2898,8 @@ class SqlService {
|
|
|
2921
2898
|
size: 0,
|
|
2922
2899
|
total: 0
|
|
2923
2900
|
};
|
|
2901
|
+
option.pageNumber ?? (option.pageNumber = 1);
|
|
2902
|
+
option.pageSize ?? (option.pageSize = 0);
|
|
2924
2903
|
Object.assign(option.params, {
|
|
2925
2904
|
limitStart: (0, math_1.calc)(option.pageNumber).sub(1).mul(option.pageSize).over(),
|
|
2926
2905
|
limitEnd: (0, math_1.calc)(option.pageSize).over(),
|
|
@@ -2993,7 +2972,7 @@ class SqlService {
|
|
|
2993
2972
|
selectResult: SelectResult.R_C_Assert
|
|
2994
2973
|
});
|
|
2995
2974
|
result.size = (0, math_1.calc)(result.total)
|
|
2996
|
-
.add(option.pageSize - 1)
|
|
2975
|
+
.add(option.pageSize ?? 10 - 1)
|
|
2997
2976
|
.div(option.pageSize)
|
|
2998
2977
|
.round(0, 2)
|
|
2999
2978
|
.over();
|
|
@@ -3022,6 +3001,170 @@ class SqlService {
|
|
|
3022
3001
|
});
|
|
3023
3002
|
}
|
|
3024
3003
|
}
|
|
3004
|
+
/**
|
|
3005
|
+
* 导出数据,可以为EJS-EXCEL直接使用
|
|
3006
|
+
* @param list
|
|
3007
|
+
* @returns
|
|
3008
|
+
*/
|
|
3009
|
+
exp(list) {
|
|
3010
|
+
error_1.Throw.if(list.length === 0, 'not found data!');
|
|
3011
|
+
const columnTitles = new Array();
|
|
3012
|
+
const keys = this[_fields] ?
|
|
3013
|
+
(0, iterare_1.default)(Object.entries(this[_fields]))
|
|
3014
|
+
.filter(([K, F]) => (F.id !== true && F.exportable !== false) || (F.id === true && F.exportable === true))
|
|
3015
|
+
.map(([K, F]) => {
|
|
3016
|
+
columnTitles.push(F.comment ?? K);
|
|
3017
|
+
return K;
|
|
3018
|
+
}).toArray()
|
|
3019
|
+
: Object.keys(list[0]).filter(K => !this[_ids]?.includes(K));
|
|
3020
|
+
const title = this[_comment] ?? this[_tableName];
|
|
3021
|
+
const titleSpan = `A1:${(0, math_1.ten2Any)(keys.length)}1`;
|
|
3022
|
+
const datas = list.map(data => keys.map(k => data[k] ?? ''));
|
|
3023
|
+
return { title, titleSpan, columnTitles, datas };
|
|
3024
|
+
}
|
|
3025
|
+
/**
|
|
3026
|
+
* 导入数据的模板
|
|
3027
|
+
* @returns
|
|
3028
|
+
*/
|
|
3029
|
+
imp() {
|
|
3030
|
+
error_1.Throw.if(!this[_fields], 'not set fields!');
|
|
3031
|
+
const columnTitles = new Array();
|
|
3032
|
+
const keys = (0, iterare_1.default)(Object.entries(this[_fields]))
|
|
3033
|
+
.filter(([K, F]) => (F.id !== true && F.exportable !== false) || (F.id === true && F.exportable === true))
|
|
3034
|
+
.map(([K, F]) => {
|
|
3035
|
+
columnTitles.push(F.comment ?? K);
|
|
3036
|
+
return K;
|
|
3037
|
+
}).toArray();
|
|
3038
|
+
const title = this[_comment] ?? this[_tableName];
|
|
3039
|
+
const titleSpan = `A1:${(0, math_1.ten2Any)(keys.length)}1`;
|
|
3040
|
+
return { title, titleSpan, columnTitles };
|
|
3041
|
+
}
|
|
3042
|
+
init(option) {
|
|
3043
|
+
var _e;
|
|
3044
|
+
const tableES = sqlstring_1.default.escapeId(option.tableName);
|
|
3045
|
+
(_e = option).force ?? (_e.force = false);
|
|
3046
|
+
if (this[_dbType] === DBType.Sqlite) {
|
|
3047
|
+
if (option?.force) {
|
|
3048
|
+
option.conn.execute(SyncMode.Sync, `DROP TABLE IF EXISTS ${tableES};`);
|
|
3049
|
+
}
|
|
3050
|
+
const lastVersion = this[_sqlite_version] ?? '0.0.1';
|
|
3051
|
+
// 检查表
|
|
3052
|
+
const tableCheckResult = option.conn.pluck(SyncMode.Sync, `SELECT COUNT(1) t FROM sqlite_master WHERE TYPE = 'table' AND name = ?`, [option.tableName]);
|
|
3053
|
+
if (tableCheckResult) {
|
|
3054
|
+
// 旧版本
|
|
3055
|
+
const tableVersion = option.conn.pluck(SyncMode.Sync, 'SELECT ______version v from TABLE_VERSION WHERE ______tableName = ?', [option.tableName]);
|
|
3056
|
+
if (tableVersion && tableVersion < lastVersion) { // 发现需要升级的版本
|
|
3057
|
+
// 更新版本
|
|
3058
|
+
const columns = (0, iterare_1.default)(option.conn.query(SyncMode.Sync, `PRAGMA table_info(${tableES})`))
|
|
3059
|
+
.filter(c => this[_fields].hasOwnProperty((0, object_1.C2P)(c.name, globalThis[exports._Hump])))
|
|
3060
|
+
.map(c => sqlstring_1.default.escapeId(c.name))
|
|
3061
|
+
.join(',');
|
|
3062
|
+
const rtable = sqlstring_1.default.escapeId(`${option.tableName}_${tableVersion.replace(/\./, '_')}`);
|
|
3063
|
+
option.conn.execute(SyncMode.Sync, `DROP TABLE IF EXISTS ${rtable};`);
|
|
3064
|
+
option.conn.execute(SyncMode.Sync, `ALTER TABLE ${tableES} RENAME TO ${rtable};`);
|
|
3065
|
+
option.conn.execute(SyncMode.Sync, `
|
|
3066
|
+
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
3067
|
+
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
3068
|
+
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
3069
|
+
);
|
|
3070
|
+
`);
|
|
3071
|
+
if (this[_index] && this[_index].length) {
|
|
3072
|
+
for (const index of this[_index]) {
|
|
3073
|
+
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${sqlstring_1.default.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${this[_fields][index]?.C2()}");`);
|
|
3074
|
+
}
|
|
3075
|
+
}
|
|
3076
|
+
option.conn.execute(SyncMode.Sync, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
3077
|
+
option.conn.execute(SyncMode.Sync, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
3078
|
+
option.conn.execute(SyncMode.Sync, `DROP TABLE IF EXISTS ${rtable};`);
|
|
3079
|
+
// 更新完毕,保存版本号
|
|
3080
|
+
option.conn.execute(SyncMode.Sync, 'UPDATE TABLE_VERSION SET ______version = ? WHERE ______tableName = ?', [option.tableName, lastVersion]);
|
|
3081
|
+
}
|
|
3082
|
+
else if (!tableVersion) { // 不需要升级情况:没有旧的版本号
|
|
3083
|
+
option.conn.execute(SyncMode.Sync, 'INSERT INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
3084
|
+
}
|
|
3085
|
+
}
|
|
3086
|
+
else { // 表不存在
|
|
3087
|
+
// 创建表
|
|
3088
|
+
option.conn.execute(SyncMode.Sync, `
|
|
3089
|
+
CREATE TABLE IF NOT EXISTS ${tableES} (
|
|
3090
|
+
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
3091
|
+
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
3092
|
+
|
|
3093
|
+
);
|
|
3094
|
+
`);
|
|
3095
|
+
if (this[_index] && this[_index].length) {
|
|
3096
|
+
for (const index of this[_index]) {
|
|
3097
|
+
option.conn.execute(SyncMode.Sync, `CREATE INDEX ${sqlstring_1.default.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${this[_fields][index]?.C2()}");`);
|
|
3098
|
+
}
|
|
3099
|
+
}
|
|
3100
|
+
option.conn.execute(SyncMode.Sync, 'INSERT OR REPLACE INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
3101
|
+
}
|
|
3102
|
+
}
|
|
3103
|
+
else if (this[_dbType] === DBType.SqliteRemote) {
|
|
3104
|
+
return new Promise(async (resolve, reject) => {
|
|
3105
|
+
try {
|
|
3106
|
+
if (option?.force) {
|
|
3107
|
+
await option.conn.execute(SyncMode.Async, `DROP TABLE IF EXISTS ${tableES};`);
|
|
3108
|
+
}
|
|
3109
|
+
const lastVersion = this[_sqlite_version] ?? '0.0.1';
|
|
3110
|
+
// 检查表
|
|
3111
|
+
const tableCheckResult = await option.conn.pluck(SyncMode.Async, `SELECT COUNT(1) t FROM sqlite_master WHERE TYPE = 'table' AND name = ?`, [option.tableName]);
|
|
3112
|
+
if (tableCheckResult) {
|
|
3113
|
+
// 旧版本
|
|
3114
|
+
const tableVersion = await option.conn.pluck(SyncMode.Async, 'SELECT ______version v from TABLE_VERSION WHERE ______tableName = ?', [option.tableName]);
|
|
3115
|
+
if (tableVersion && tableVersion < lastVersion) { // 发现需要升级的版本
|
|
3116
|
+
// 更新版本
|
|
3117
|
+
const columns = (0, iterare_1.default)(await option.conn.query(SyncMode.Async, `PRAGMA table_info(${tableES})`))
|
|
3118
|
+
.filter(c => this[_fields].hasOwnProperty((0, object_1.C2P)(c.name, globalThis[exports._Hump])))
|
|
3119
|
+
.map(c => sqlstring_1.default.escapeId(c.name))
|
|
3120
|
+
.join(',');
|
|
3121
|
+
const rtable = `${option.tableName}_${tableVersion.replace(/\./, '_')}`;
|
|
3122
|
+
await option.conn.execute(SyncMode.Async, `DROP TABLE IF EXISTS ${rtable};`);
|
|
3123
|
+
await option.conn.execute(SyncMode.Async, `ALTER TABLE ${tableES} RENAME TO ${rtable};`);
|
|
3124
|
+
await option.conn.execute(SyncMode.Async, `
|
|
3125
|
+
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
3126
|
+
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
3127
|
+
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
3128
|
+
);
|
|
3129
|
+
`);
|
|
3130
|
+
if (this[_index] && this[_index].length) {
|
|
3131
|
+
for (const index of this[_index]) {
|
|
3132
|
+
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${sqlstring_1.default.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableES} ("${this[_fields][index]?.C2()}");`);
|
|
3133
|
+
}
|
|
3134
|
+
}
|
|
3135
|
+
await option.conn.execute(SyncMode.Async, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
3136
|
+
await option.conn.execute(SyncMode.Async, `INSERT INTO ${tableES} (${columns}) SELECT ${columns} FROM ${rtable};`);
|
|
3137
|
+
await option.conn.execute(SyncMode.Async, `DROP TABLE IF EXISTS ${rtable};`);
|
|
3138
|
+
// 更新完毕,保存版本号
|
|
3139
|
+
await option.conn.execute(SyncMode.Async, 'UPDATE TABLE_VERSION SET ______version = ? WHERE ______tableName = ?', [option.tableName, lastVersion]);
|
|
3140
|
+
}
|
|
3141
|
+
else if (!tableVersion) { // 不需要升级情况:没有旧的版本号
|
|
3142
|
+
await option.conn.execute(SyncMode.Async, 'INSERT INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
3143
|
+
}
|
|
3144
|
+
}
|
|
3145
|
+
else { // 表不存在
|
|
3146
|
+
// 创建表
|
|
3147
|
+
await option.conn.execute(SyncMode.Async, `
|
|
3148
|
+
CREATE TABLE IF NOT EXISTS ${tableES}(
|
|
3149
|
+
${Object.values(this[_fields]).map(K => K[DBType.Sqlite]()).join(',')}
|
|
3150
|
+
${this[_ids] && this[_ids].length ? `, PRIMARY KEY (${this[_ids].map(i => this[_fields][i]?.C2()).join(',')})` : ''}
|
|
3151
|
+
);
|
|
3152
|
+
`);
|
|
3153
|
+
if (this[_index] && this[_index].length) {
|
|
3154
|
+
for (const index of this[_index]) {
|
|
3155
|
+
await option.conn.execute(SyncMode.Async, `CREATE INDEX ${sqlstring_1.default.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${sqlstring_1.default.escapeId(option.tableName)} ("${this[_fields][index]?.C2()}");`);
|
|
3156
|
+
}
|
|
3157
|
+
}
|
|
3158
|
+
await option.conn.execute(SyncMode.Async, 'INSERT OR REPLACE INTO TABLE_VERSION (______tableName, ______version ) VALUES ( ?, ? )', [option.tableName, lastVersion]);
|
|
3159
|
+
}
|
|
3160
|
+
resolve();
|
|
3161
|
+
}
|
|
3162
|
+
catch (error) {
|
|
3163
|
+
reject(error);
|
|
3164
|
+
}
|
|
3165
|
+
});
|
|
3166
|
+
}
|
|
3167
|
+
}
|
|
3025
3168
|
/**
|
|
3026
3169
|
#创建表
|
|
3027
3170
|
** `tableName` 表名称
|
|
@@ -3066,16 +3209,16 @@ class SqlService {
|
|
|
3066
3209
|
tableName = sqlstring_1.default.escapeId(tableName ?? this[_tableName]);
|
|
3067
3210
|
switch (this[_dbType]) {
|
|
3068
3211
|
case DBType.Mysql: {
|
|
3069
|
-
let sql = (0, sql_formatter_1.
|
|
3212
|
+
let sql = (0, sql_formatter_1.formatDialect)(`CREATE ${temp === true ? 'TEMPORARY' : ''} TABLE IF NOT EXISTS ${tableName}(
|
|
3070
3213
|
${columns.map(K => this[_fields][K][DBType.Mysql]()).join(',')}
|
|
3071
3214
|
${ids && ids.length ? `,PRIMARY KEY (${ids.map(i => this[_fields][i]?.C2()).join(',')}) USING BTREE ` : ''}
|
|
3072
3215
|
${indexs && indexs.length ? `,${indexs.map(i => `KEY ${this[_fields][i]?.C2()} (${this[_fields][i]?.C2()})`).join(',')} ` : ''}
|
|
3073
|
-
) ENGINE=MEMORY
|
|
3216
|
+
) ENGINE=MEMORY;`, { dialect: this[_formatDialect] });
|
|
3074
3217
|
sqls.push({ sql });
|
|
3075
3218
|
if (data && data.length > 0) {
|
|
3076
3219
|
const params = [];
|
|
3077
3220
|
let first = true;
|
|
3078
|
-
sql = (0, sql_formatter_1.
|
|
3221
|
+
sql = (0, sql_formatter_1.formatDialect)(`INSERT INTO ${tableName} (${columns.map(c => this[_fields][c]?.C2()).join(',')})
|
|
3079
3222
|
${(data).map(d => {
|
|
3080
3223
|
const r = `SELECT ${Object.entries(d).map(([K, V]) => {
|
|
3081
3224
|
params.push(V);
|
|
@@ -3083,28 +3226,28 @@ class SqlService {
|
|
|
3083
3226
|
}).join(',')}`;
|
|
3084
3227
|
first = false;
|
|
3085
3228
|
return r;
|
|
3086
|
-
}).join(' UNION ALL ')}
|
|
3229
|
+
}).join(' UNION ALL ')}`, { dialect: this[_formatDialect] });
|
|
3087
3230
|
sqls.push({ sql, params });
|
|
3088
3231
|
}
|
|
3089
3232
|
break;
|
|
3090
3233
|
}
|
|
3091
3234
|
case DBType.Sqlite:
|
|
3092
3235
|
case DBType.SqliteRemote: {
|
|
3093
|
-
let sql = (0, sql_formatter_1.
|
|
3236
|
+
let sql = (0, sql_formatter_1.formatDialect)(`CREATE ${temp === true ? 'TEMPORARY' : ''} TABLE IF NOT EXISTS ${tableName}(
|
|
3094
3237
|
${columns.map(K => this[_fields][K][DBType.Sqlite]()).join(',')}
|
|
3095
3238
|
${ids && ids.length ? `,PRIMARY KEY (${ids.map(i => this[_fields][i]?.C2()).join(',')}) ` : ''}
|
|
3096
|
-
)
|
|
3239
|
+
);`, { dialect: this[_formatDialect] });
|
|
3097
3240
|
sqls.push({ sql });
|
|
3098
3241
|
if (indexs) {
|
|
3099
3242
|
for (const index of indexs) {
|
|
3100
|
-
sql = (0, sql_formatter_1.
|
|
3243
|
+
sql = (0, sql_formatter_1.formatDialect)(`CREATE INDEX ${sqlstring_1.default.escapeId(`${index}_${Math.random()}`.replace(/\./, ''))} ON ${tableName} (${this[_fields][index]?.C2()});`, { dialect: this[_formatDialect] });
|
|
3101
3244
|
sqls.push({ sql });
|
|
3102
3245
|
}
|
|
3103
3246
|
}
|
|
3104
3247
|
if (data && data.length > 0) {
|
|
3105
3248
|
const params = [];
|
|
3106
3249
|
let first = true;
|
|
3107
|
-
sql = (0, sql_formatter_1.
|
|
3250
|
+
sql = (0, sql_formatter_1.formatDialect)(`INSERT INTO ${tableName} (${columns.map(c => this[_fields][c]?.C2()).join(',')})
|
|
3108
3251
|
${(data).map(d => {
|
|
3109
3252
|
const r = `SELECT ${Object.entries(d).map(([K, V]) => {
|
|
3110
3253
|
params.push(V);
|
|
@@ -3112,7 +3255,7 @@ class SqlService {
|
|
|
3112
3255
|
}).join(',')}`;
|
|
3113
3256
|
first = false;
|
|
3114
3257
|
return r;
|
|
3115
|
-
}).join(' UNION ALL ')}
|
|
3258
|
+
}).join(' UNION ALL ')}`, { dialect: this[_formatDialect] });
|
|
3116
3259
|
sqls.push({ sql, params });
|
|
3117
3260
|
}
|
|
3118
3261
|
break;
|
|
@@ -3177,6 +3320,12 @@ __decorate([
|
|
|
3177
3320
|
__metadata("design:paramtypes", [Object]),
|
|
3178
3321
|
__metadata("design:returntype", Object)
|
|
3179
3322
|
], SqlService.prototype, "page", null);
|
|
3323
|
+
__decorate([
|
|
3324
|
+
P(),
|
|
3325
|
+
__metadata("design:type", Function),
|
|
3326
|
+
__metadata("design:paramtypes", [Object]),
|
|
3327
|
+
__metadata("design:returntype", Object)
|
|
3328
|
+
], SqlService.prototype, "init", null);
|
|
3180
3329
|
/** 是否进行下一个动作 */
|
|
3181
3330
|
const IF_PROCEED = function () {
|
|
3182
3331
|
return function (_target, _propertyKey, descriptor) {
|
|
@@ -3554,7 +3703,10 @@ class StreamQuery {
|
|
|
3554
3703
|
}
|
|
3555
3704
|
}
|
|
3556
3705
|
if (sets.length > 0) {
|
|
3557
|
-
const sql = `UPDATE ${this._table} SET ${sets.join(',')}
|
|
3706
|
+
const sql = `UPDATE ${this._table} SET ${sets.join(',')}
|
|
3707
|
+
${where ? ' WHERE ' : ''}
|
|
3708
|
+
${where}
|
|
3709
|
+
`;
|
|
3558
3710
|
if (option.sync === SyncMode.Async) {
|
|
3559
3711
|
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
3560
3712
|
}
|
|
@@ -3570,7 +3722,10 @@ class StreamQuery {
|
|
|
3570
3722
|
option ?? (option = {});
|
|
3571
3723
|
option.sync ?? (option.sync = SyncMode.Async);
|
|
3572
3724
|
const { where, params } = this._where();
|
|
3573
|
-
const sql = `DELETE FROM ${this._table}
|
|
3725
|
+
const sql = `DELETE FROM ${this._table}
|
|
3726
|
+
${where ? ' WHERE ' : ''}
|
|
3727
|
+
${where}
|
|
3728
|
+
`;
|
|
3574
3729
|
if (option.sync === SyncMode.Async) {
|
|
3575
3730
|
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
3576
3731
|
}
|