tspace-mysql 1.9.0 → 1.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4357 -133
- package/dist/lib/config/index.d.ts +2 -2
- package/dist/lib/config/index.js +24 -16
- package/dist/lib/config/index.js.map +1 -1
- package/dist/lib/constants/index.d.ts +10 -1
- package/dist/lib/constants/index.js +11 -2
- package/dist/lib/constants/index.js.map +1 -1
- package/dist/lib/core/Abstracts/AbstractBuilder.d.ts +11 -7
- package/dist/lib/core/Abstracts/AbstractBuilder.js +11 -13
- package/dist/lib/core/Abstracts/AbstractBuilder.js.map +1 -1
- package/dist/lib/core/Abstracts/AbstractDB.d.ts +4 -1
- package/dist/lib/core/Abstracts/AbstractDB.js.map +1 -1
- package/dist/lib/core/Abstracts/AbstractView.js.map +1 -1
- package/dist/lib/core/Blueprint.d.ts +30 -10
- package/dist/lib/core/Blueprint.js +39 -14
- package/dist/lib/core/Blueprint.js.map +1 -1
- package/dist/lib/core/Builder.d.ts +263 -9
- package/dist/lib/core/Builder.js +610 -347
- package/dist/lib/core/Builder.js.map +1 -1
- package/dist/lib/core/Cache/DBCache.js +19 -20
- package/dist/lib/core/Cache/DBCache.js.map +1 -1
- package/dist/lib/core/Cache/MemoryCache.js +3 -3
- package/dist/lib/core/Cache/MemoryCache.js.map +1 -1
- package/dist/lib/core/Cache/RedisCache.js +27 -17
- package/dist/lib/core/Cache/RedisCache.js.map +1 -1
- package/dist/lib/core/Cache/index.js +5 -4
- package/dist/lib/core/Cache/index.js.map +1 -1
- package/dist/lib/core/Contracts/AlterTable.d.ts +152 -0
- package/dist/lib/core/Contracts/AlterTable.js +243 -0
- package/dist/lib/core/Contracts/AlterTable.js.map +1 -0
- package/dist/lib/core/Contracts/Audit.js +2 -5
- package/dist/lib/core/Contracts/Audit.js.map +1 -1
- package/dist/lib/core/DB.d.ts +101 -9
- package/dist/lib/core/DB.js +168 -29
- package/dist/lib/core/DB.js.map +1 -1
- package/dist/lib/core/Decorator.d.ts +144 -16
- package/dist/lib/core/Decorator.js +156 -14
- package/dist/lib/core/Decorator.js.map +1 -1
- package/dist/lib/core/Driver/index.d.ts +43 -9
- package/dist/lib/core/Driver/index.js +9 -7
- package/dist/lib/core/Driver/index.js.map +1 -1
- package/dist/lib/core/Driver/mariadb/MariadbDriver.js +74 -27
- package/dist/lib/core/Driver/mariadb/MariadbDriver.js.map +1 -1
- package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.d.ts +45 -9
- package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.js +165 -21
- package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.js.map +1 -1
- package/dist/lib/core/Driver/mongodb/MongodbDriver.d.ts +24 -0
- package/dist/lib/core/Driver/mongodb/MongodbDriver.js +255 -0
- package/dist/lib/core/Driver/mongodb/MongodbDriver.js.map +1 -0
- package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.d.ts +141 -0
- package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.js +563 -0
- package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.js.map +1 -0
- package/dist/lib/core/Driver/mysql/MysqlDriver.js +105 -95
- package/dist/lib/core/Driver/mysql/MysqlDriver.js.map +1 -1
- package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.d.ts +45 -9
- package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.js +165 -21
- package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.js.map +1 -1
- package/dist/lib/core/Driver/postgres/PostgresDriver.js +97 -72
- package/dist/lib/core/Driver/postgres/PostgresDriver.js.map +1 -1
- package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.d.ts +50 -9
- package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.js +296 -41
- package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.js.map +1 -1
- package/dist/lib/core/Driver/sqlite/SqliteDriver.d.ts +20 -0
- package/dist/lib/core/Driver/sqlite/SqliteDriver.js +192 -0
- package/dist/lib/core/Driver/sqlite/SqliteDriver.js.map +1 -0
- package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.d.ts +144 -0
- package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.js +689 -0
- package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.js.map +1 -0
- package/dist/lib/core/JoinModel.js +2 -2
- package/dist/lib/core/JoinModel.js.map +1 -1
- package/dist/lib/core/Meta.d.ts +17 -11
- package/dist/lib/core/Meta.js +25 -19
- package/dist/lib/core/Meta.js.map +1 -1
- package/dist/lib/core/Model.d.ts +450 -108
- package/dist/lib/core/Model.js +1031 -587
- package/dist/lib/core/Model.js.map +1 -1
- package/dist/lib/{tools/index.d.ts → core/Package.d.ts} +11 -3
- package/dist/lib/{tools/index.js → core/Package.js} +20 -7
- package/dist/lib/core/Package.js.map +1 -0
- package/dist/lib/core/Pool.js +42 -30
- package/dist/lib/core/Pool.js.map +1 -1
- package/dist/lib/core/Queue.d.ts +240 -0
- package/dist/lib/core/Queue.js +686 -0
- package/dist/lib/core/Queue.js.map +1 -0
- package/dist/lib/core/RelationManager.js +27 -25
- package/dist/lib/core/RelationManager.js.map +1 -1
- package/dist/lib/core/Repository.d.ts +73 -207
- package/dist/lib/core/Repository.js +120 -235
- package/dist/lib/core/Repository.js.map +1 -1
- package/dist/lib/core/Schema.d.ts +218 -0
- package/dist/lib/core/Schema.js +370 -65
- package/dist/lib/core/Schema.js.map +1 -1
- package/dist/lib/core/StateManager.d.ts +25 -12
- package/dist/lib/core/StateManager.js +10 -5
- package/dist/lib/core/StateManager.js.map +1 -1
- package/dist/lib/core/UtilityTypes.d.ts +85 -56
- package/dist/lib/core/UtilityTypes.js.map +1 -1
- package/dist/lib/core/index.d.ts +4 -0
- package/dist/lib/core/index.js +6 -2
- package/dist/lib/core/index.js.map +1 -1
- package/dist/lib/types/decorator/index.d.ts +1 -1
- package/dist/lib/types/index.d.ts +69 -22
- package/dist/lib/types/repository/index.d.ts +68 -37
- package/dist/lib/utils/index.d.ts +11 -1
- package/dist/lib/utils/index.js +58 -2
- package/dist/lib/utils/index.js.map +1 -1
- package/package.json +14 -6
- package/dist/lib/tools/index.js.map +0 -1
|
@@ -0,0 +1,689 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqliteQueryBuilder = void 0;
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
class SqliteQueryBuilder extends __1.QueryBuilder {
|
|
6
|
+
constructor(state) {
|
|
7
|
+
super(state);
|
|
8
|
+
}
|
|
9
|
+
select = () => {
|
|
10
|
+
const combindSQL = [
|
|
11
|
+
this.bindSelect(this.$state.get("SELECT")),
|
|
12
|
+
this.bindFrom({
|
|
13
|
+
from: !this.$state.get("FROM").length
|
|
14
|
+
? [this.$state.get("TABLE_NAME")].filter(Boolean).map(String)
|
|
15
|
+
: [this.$state.get("TABLE_NAME"), ...this.$state.get("FROM")].filter(Boolean).map(String),
|
|
16
|
+
alias: this.$state.get("ALIAS"),
|
|
17
|
+
rawAlias: this.$state.get("RAW_ALIAS"),
|
|
18
|
+
}),
|
|
19
|
+
this.bindJoin(this.$state.get("JOIN")),
|
|
20
|
+
this.bindWhere(this.$state.get("WHERE")),
|
|
21
|
+
this.bindGroupBy(this.$state.get("GROUP_BY")),
|
|
22
|
+
this.bindHaving(this.$state.get("HAVING")),
|
|
23
|
+
this.bindOrderBy(this.$state.get("ORDER_BY")),
|
|
24
|
+
this.bindLimit(this.$state.get("LIMIT")),
|
|
25
|
+
this.bindOffset(this.$state.get("OFFSET")),
|
|
26
|
+
this.bindRowLevelLock(this.$state.get("ROW_LEVEL_LOCK")),
|
|
27
|
+
];
|
|
28
|
+
let sql = this.format(combindSQL).trimEnd();
|
|
29
|
+
if (this.$state.get("UNION").length) {
|
|
30
|
+
sql = `(${sql}) ${this.$state
|
|
31
|
+
.get("UNION")
|
|
32
|
+
.map((union) => `${this.$constants("UNION")} (${union})`)
|
|
33
|
+
.join(" ")}`;
|
|
34
|
+
}
|
|
35
|
+
if (this.$state.get("UNION_ALL").length) {
|
|
36
|
+
sql = `(${sql}) ${this.$state
|
|
37
|
+
.get("UNION_ALL")
|
|
38
|
+
.map((union) => `${this.$constants("UNION_ALL")} (${union})`)
|
|
39
|
+
.join(" ")}`;
|
|
40
|
+
}
|
|
41
|
+
if (this.$state.get("CTE").length) {
|
|
42
|
+
sql = `${this.$constants("WITH")} ${this.$state
|
|
43
|
+
.get("CTE")
|
|
44
|
+
.join(", ")} ${sql}`;
|
|
45
|
+
}
|
|
46
|
+
return sql;
|
|
47
|
+
};
|
|
48
|
+
insert() {
|
|
49
|
+
const query = this.$state.get("INSERT");
|
|
50
|
+
if (!query)
|
|
51
|
+
return '';
|
|
52
|
+
const table = this.$state.get("TABLE_NAME");
|
|
53
|
+
const columns = `(${query.columns})`;
|
|
54
|
+
const values = query.values.map(v => `(${v})`).join(', ');
|
|
55
|
+
const sql = this.format([
|
|
56
|
+
this.$constants("INSERT"),
|
|
57
|
+
table,
|
|
58
|
+
columns,
|
|
59
|
+
this.$constants("VALUES"),
|
|
60
|
+
values
|
|
61
|
+
]);
|
|
62
|
+
return sql;
|
|
63
|
+
}
|
|
64
|
+
update() {
|
|
65
|
+
const query = this.$state.get("UPDATE");
|
|
66
|
+
if (query == null) {
|
|
67
|
+
return '';
|
|
68
|
+
}
|
|
69
|
+
const sql = this.format([
|
|
70
|
+
`${this.$constants("UPDATE")}`,
|
|
71
|
+
`${this.$state.get("TABLE_NAME")}`,
|
|
72
|
+
`${this.$constants("SET")}`,
|
|
73
|
+
`${query}`,
|
|
74
|
+
this.bindWhere(this.$state.get("WHERE")),
|
|
75
|
+
this.bindOrderBy(this.$state.get("ORDER_BY")),
|
|
76
|
+
this.bindLimit(this.$state.get("LIMIT")),
|
|
77
|
+
]);
|
|
78
|
+
return sql;
|
|
79
|
+
}
|
|
80
|
+
remove() {
|
|
81
|
+
const query = this.$state.get("DELETE");
|
|
82
|
+
if (!query) {
|
|
83
|
+
throw new Error("Bad query builder: DELETE state not found. Please check your query configuration.");
|
|
84
|
+
}
|
|
85
|
+
let sql = this.format([
|
|
86
|
+
this.$constants("DELETE"),
|
|
87
|
+
this.$constants("FROM"),
|
|
88
|
+
this.$state.get("TABLE_NAME"),
|
|
89
|
+
this.bindWhere(this.$state.get("WHERE")),
|
|
90
|
+
this.bindOrderBy(this.$state.get("ORDER_BY")),
|
|
91
|
+
this.bindLimit(this.$state.get("LIMIT")),
|
|
92
|
+
]);
|
|
93
|
+
if (this.$state.get("CTE").length) {
|
|
94
|
+
sql = `${this.$constants("WITH")} ${this.$state
|
|
95
|
+
.get("CTE")
|
|
96
|
+
.join(", ")} ${sql}`;
|
|
97
|
+
}
|
|
98
|
+
return sql;
|
|
99
|
+
}
|
|
100
|
+
any() {
|
|
101
|
+
if (this.$state.get("INSERT"))
|
|
102
|
+
return this.insert();
|
|
103
|
+
if (this.$state.get("UPDATE"))
|
|
104
|
+
return this.update();
|
|
105
|
+
if (this.$state.get("DELETE"))
|
|
106
|
+
return this.remove();
|
|
107
|
+
return this.select();
|
|
108
|
+
}
|
|
109
|
+
getColumns({ database, table }) {
|
|
110
|
+
const sql = [`
|
|
111
|
+
SELECT
|
|
112
|
+
ti.name AS "Field",
|
|
113
|
+
ti.type AS "ColumnType",
|
|
114
|
+
LOWER(ti.type) AS "Type",
|
|
115
|
+
CASE WHEN ti."notnull" = 0 THEN 'YES' ELSE 'NO' END AS "Nullable",
|
|
116
|
+
ti.dflt_value AS "Default"
|
|
117
|
+
FROM sqlite_master AS m
|
|
118
|
+
JOIN pragma_table_info(m.name) AS ti
|
|
119
|
+
WHERE m.type = 'table'
|
|
120
|
+
AND m.name = '${table.replace(/["`]/g, "")}'
|
|
121
|
+
ORDER BY ti.cid
|
|
122
|
+
`];
|
|
123
|
+
return this.format(sql);
|
|
124
|
+
}
|
|
125
|
+
getSchema({ database, table }) {
|
|
126
|
+
const sql = [`
|
|
127
|
+
SELECT
|
|
128
|
+
ti.name AS "Field",
|
|
129
|
+
|
|
130
|
+
CASE
|
|
131
|
+
WHEN ti.pk = 1 THEN 'PRI'
|
|
132
|
+
WHEN il."unique" = 1 THEN 'UNI'
|
|
133
|
+
ELSE NULL
|
|
134
|
+
END AS "Key",
|
|
135
|
+
|
|
136
|
+
ti.type AS "Type",
|
|
137
|
+
|
|
138
|
+
CASE
|
|
139
|
+
WHEN ti."notnull" = 0 THEN 'YES'
|
|
140
|
+
ELSE 'NO'
|
|
141
|
+
END AS "Nullable",
|
|
142
|
+
|
|
143
|
+
CASE
|
|
144
|
+
WHEN ti.dflt_value = 'CURRENT_TIMESTAMP' THEN 'IS_CONST:CURRENT_TIMESTAMP'
|
|
145
|
+
ELSE ti.dflt_value
|
|
146
|
+
END AS "Default",
|
|
147
|
+
|
|
148
|
+
NULL AS "Extra",
|
|
149
|
+
|
|
150
|
+
CASE
|
|
151
|
+
WHEN ti.type LIKE '%(%'
|
|
152
|
+
THEN substr(ti.type, instr(ti.type, '(') + 1, instr(ti.type, ')') - instr(ti.type, '(') - 1)
|
|
153
|
+
ELSE NULL
|
|
154
|
+
END AS "TypeValue"
|
|
155
|
+
|
|
156
|
+
FROM sqlite_master AS m
|
|
157
|
+
JOIN pragma_table_info(m.name) AS ti
|
|
158
|
+
|
|
159
|
+
LEFT JOIN pragma_index_list(m.name) AS il
|
|
160
|
+
ON il."unique" = 1
|
|
161
|
+
|
|
162
|
+
LEFT JOIN pragma_index_info(il.name) AS ii
|
|
163
|
+
ON ii.name = ti.name
|
|
164
|
+
|
|
165
|
+
WHERE m.type = 'table'
|
|
166
|
+
AND m.name = '${table.replace(/["`]/g, "")}'
|
|
167
|
+
|
|
168
|
+
GROUP BY ti.cid
|
|
169
|
+
|
|
170
|
+
ORDER BY ti.cid
|
|
171
|
+
`];
|
|
172
|
+
return this.format(sql);
|
|
173
|
+
}
|
|
174
|
+
getTables(database) {
|
|
175
|
+
const sql = [
|
|
176
|
+
`SELECT name AS table_name
|
|
177
|
+
FROM sqlite_master
|
|
178
|
+
WHERE type = 'table'`,
|
|
179
|
+
];
|
|
180
|
+
return this.format(sql);
|
|
181
|
+
}
|
|
182
|
+
hasTable({ database, table }) {
|
|
183
|
+
const sql = [`
|
|
184
|
+
SELECT EXISTS(
|
|
185
|
+
SELECT 1
|
|
186
|
+
FROM sqlite_master
|
|
187
|
+
WHERE type = 'table'
|
|
188
|
+
AND name = '${table.replace(/["`]/g, "")}'
|
|
189
|
+
) AS "IS_EXISTS"
|
|
190
|
+
`];
|
|
191
|
+
return this.format(sql);
|
|
192
|
+
}
|
|
193
|
+
createDatabase(database) {
|
|
194
|
+
throw new Error("Method not implemented.");
|
|
195
|
+
return '';
|
|
196
|
+
}
|
|
197
|
+
createTable({ database, table, schema, }) {
|
|
198
|
+
let columns = [];
|
|
199
|
+
if (Array.isArray(schema)) {
|
|
200
|
+
const sql = [
|
|
201
|
+
`${this.$constants("CREATE_TABLE_NOT_EXISTS")}`,
|
|
202
|
+
`\`${database.replace(/`/g, "")}\`.\`${table.replace(/`/g, "")}\``,
|
|
203
|
+
`(${schema.join(", ")})`
|
|
204
|
+
];
|
|
205
|
+
return this.format(sql);
|
|
206
|
+
}
|
|
207
|
+
const detectSchema = (schema) => {
|
|
208
|
+
try {
|
|
209
|
+
return {
|
|
210
|
+
type: schema?.type ?? schema["_type"] ?? null,
|
|
211
|
+
attributes: schema?.attributes ?? schema["_attributes"] ?? null,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
catch (e) {
|
|
215
|
+
return {
|
|
216
|
+
type: null,
|
|
217
|
+
attributes: null,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
for (const key in schema) {
|
|
222
|
+
const data = schema[key];
|
|
223
|
+
const { type, attributes } = detectSchema(data);
|
|
224
|
+
if (type == null || attributes == null)
|
|
225
|
+
continue;
|
|
226
|
+
const { formatedAttributes, formatedType } = this._formatedTypeAndAttributes({
|
|
227
|
+
type,
|
|
228
|
+
attributes,
|
|
229
|
+
key,
|
|
230
|
+
});
|
|
231
|
+
columns = [
|
|
232
|
+
...columns,
|
|
233
|
+
`\`${key}\` ${formatedType} ${formatedAttributes.join(" ")}`,
|
|
234
|
+
];
|
|
235
|
+
}
|
|
236
|
+
const sql = [
|
|
237
|
+
`${this.$constants("CREATE_TABLE_NOT_EXISTS")}`,
|
|
238
|
+
`\`${table.replace(/`/g, "")}\` (${columns.join(", ")})`,
|
|
239
|
+
];
|
|
240
|
+
return this.format(sql);
|
|
241
|
+
}
|
|
242
|
+
addColumn({ table, column, type, attributes, after, }) {
|
|
243
|
+
const sql = [
|
|
244
|
+
this.$constants("ALTER_TABLE"),
|
|
245
|
+
`\`${table}\``,
|
|
246
|
+
this.$constants("ADD"),
|
|
247
|
+
`\`${column}\` ${type} ${attributes != null && attributes.length ? `${attributes.join(" ")}` : ""}`,
|
|
248
|
+
this.$constants("AFTER"),
|
|
249
|
+
`\`${after}\``,
|
|
250
|
+
];
|
|
251
|
+
return this.format(sql);
|
|
252
|
+
}
|
|
253
|
+
changeColumn({ table, column, type, attributes, }) {
|
|
254
|
+
const sql = [
|
|
255
|
+
this.$constants("ALTER_TABLE"),
|
|
256
|
+
`\`${table.replace(/`/g, "")}\``,
|
|
257
|
+
this.$constants("CHANGE"),
|
|
258
|
+
`\`${column}\``,
|
|
259
|
+
`\`${column}\` ${type} ${attributes != null && attributes.length
|
|
260
|
+
? `${attributes
|
|
261
|
+
.filter((v) => !["PRIMARY KEY"].includes(v))
|
|
262
|
+
.join(" ")}`
|
|
263
|
+
: ""}`,
|
|
264
|
+
];
|
|
265
|
+
return this.format(sql);
|
|
266
|
+
}
|
|
267
|
+
getChildFKs({ database, table }) {
|
|
268
|
+
const sql = [`
|
|
269
|
+
SELECT
|
|
270
|
+
'fk_' || m.name || '_' || f."from" AS "Constraint",
|
|
271
|
+
m.name AS "ChildTable",
|
|
272
|
+
f."from" AS "ChildColumn",
|
|
273
|
+
f."table" AS "ParentTable",
|
|
274
|
+
f."to" AS "ParentColumn"
|
|
275
|
+
FROM sqlite_master AS m
|
|
276
|
+
JOIN pragma_foreign_key_list(m.name) AS f
|
|
277
|
+
WHERE m.type = 'table'
|
|
278
|
+
AND m.name NOT LIKE 'sqlite_%'
|
|
279
|
+
AND f."table" = '${table.replace(/"/g, "")}'
|
|
280
|
+
`];
|
|
281
|
+
return this.format(sql);
|
|
282
|
+
}
|
|
283
|
+
getFKs({ database, table }) {
|
|
284
|
+
const sql = [`
|
|
285
|
+
SELECT
|
|
286
|
+
f."table" AS "RefTable",
|
|
287
|
+
f."to" AS "RefColumn",
|
|
288
|
+
f."from" AS "Column",
|
|
289
|
+
'fk_' || m.name || '_' || f."from" AS "Constraint"
|
|
290
|
+
FROM sqlite_master AS m
|
|
291
|
+
JOIN pragma_foreign_key_list(m.name) AS f
|
|
292
|
+
WHERE m.type = 'table'
|
|
293
|
+
AND m.name = '${table.replace(/["`]/g, "")}'
|
|
294
|
+
AND f."table" IS NOT NULL
|
|
295
|
+
`];
|
|
296
|
+
return this.format(sql);
|
|
297
|
+
}
|
|
298
|
+
hasFK({ database, table, constraint, }) {
|
|
299
|
+
const sql = [`
|
|
300
|
+
SELECT EXISTS(
|
|
301
|
+
SELECT 1
|
|
302
|
+
FROM sqlite_master AS m
|
|
303
|
+
JOIN pragma_foreign_key_list(m.name) AS f
|
|
304
|
+
WHERE m.type = 'table'
|
|
305
|
+
AND m.name = '${table.replace(/["`]/g, "")}'
|
|
306
|
+
AND ('fk_' || m.name || '_' || f."from") = '${constraint.replace(/["`]/g, "")}'
|
|
307
|
+
) AS "IS_EXISTS"
|
|
308
|
+
`];
|
|
309
|
+
return this.format(sql);
|
|
310
|
+
}
|
|
311
|
+
addFK({ table, tableRef, key, constraint, foreign, }) {
|
|
312
|
+
const sql = [
|
|
313
|
+
`${this.$constants("ALTER_TABLE")}`,
|
|
314
|
+
`\`${table}\``,
|
|
315
|
+
`${this.$constants("ADD_CONSTRAINT")}`,
|
|
316
|
+
`\`${constraint}\``,
|
|
317
|
+
`${this.$constants("FOREIGN_KEY")}(\`${key}\`)`,
|
|
318
|
+
`${this.$constants("REFERENCES")} \`${tableRef}\`(\`${foreign.references}\`)`,
|
|
319
|
+
`${this.$constants("ON_DELETE")} ${foreign.onDelete}`,
|
|
320
|
+
`${this.$constants("ON_UPDATE")} ${foreign.onUpdate}`,
|
|
321
|
+
].join(" ");
|
|
322
|
+
return this.format(sql);
|
|
323
|
+
}
|
|
324
|
+
dropFK({ table, constraint, }) {
|
|
325
|
+
const sql = [
|
|
326
|
+
`${this.$constants("ALTER_TABLE")}`,
|
|
327
|
+
`\`${table}\``,
|
|
328
|
+
`DROP FOREIGN KEY`,
|
|
329
|
+
`\`${constraint}\``,
|
|
330
|
+
].join(" ");
|
|
331
|
+
return this.format(sql);
|
|
332
|
+
}
|
|
333
|
+
getIndexes({ database, table }) {
|
|
334
|
+
const sql = [`
|
|
335
|
+
SELECT
|
|
336
|
+
ii.name AS "Column",
|
|
337
|
+
il.name AS "IndexName",
|
|
338
|
+
il.origin AS "IndexType",
|
|
339
|
+
CASE WHEN ti."notnull" = 0 THEN 'YES' ELSE 'NO' END AS "Nullable",
|
|
340
|
+
CASE WHEN il."unique" = 1 THEN 'YES' ELSE 'NO' END AS "Unique"
|
|
341
|
+
FROM sqlite_master AS m
|
|
342
|
+
JOIN pragma_index_list(m.name) AS il
|
|
343
|
+
JOIN pragma_index_info(il.name) AS ii
|
|
344
|
+
LEFT JOIN pragma_table_info(m.name) AS ti
|
|
345
|
+
ON ti.name = ii.name
|
|
346
|
+
WHERE m.type = 'table'
|
|
347
|
+
AND m.name = '${table.replace(/["`]/g, "")}'
|
|
348
|
+
AND il.origin != 'pk'
|
|
349
|
+
`];
|
|
350
|
+
return this.format(sql);
|
|
351
|
+
}
|
|
352
|
+
hasIndex({ database, table, name, }) {
|
|
353
|
+
const sql = [`
|
|
354
|
+
SELECT EXISTS(
|
|
355
|
+
SELECT 1
|
|
356
|
+
FROM sqlite_master AS m
|
|
357
|
+
JOIN pragma_index_list(m.name) AS il
|
|
358
|
+
WHERE m.type = 'table'
|
|
359
|
+
AND m.name = '${table.replace(/["`]/g, "")}'
|
|
360
|
+
AND il.name = '${name.replace(/["`]/g, "")}'
|
|
361
|
+
AND il.origin != 'pk'
|
|
362
|
+
) AS "IS_EXISTS"
|
|
363
|
+
`];
|
|
364
|
+
return this.format(sql);
|
|
365
|
+
}
|
|
366
|
+
addIndex({ table, name, columns, }) {
|
|
367
|
+
const cols = columns
|
|
368
|
+
.map(col => `\`${col}\``)
|
|
369
|
+
.join(", ");
|
|
370
|
+
const sql = [
|
|
371
|
+
this.$constants("ALTER_TABLE"),
|
|
372
|
+
`\`${table}\``,
|
|
373
|
+
this.$constants("ADD_INDEX"),
|
|
374
|
+
`\`${name}\``,
|
|
375
|
+
`(${cols})`
|
|
376
|
+
];
|
|
377
|
+
return this.format(sql);
|
|
378
|
+
}
|
|
379
|
+
dropIndex({ table, name, }) {
|
|
380
|
+
const sql = [
|
|
381
|
+
this.$constants("ALTER_TABLE"),
|
|
382
|
+
`\`${table}\``,
|
|
383
|
+
this.$constants("DROP"),
|
|
384
|
+
this.$constants("INDEX"),
|
|
385
|
+
`\`${name}\``
|
|
386
|
+
];
|
|
387
|
+
return this.format(sql);
|
|
388
|
+
}
|
|
389
|
+
hasUnique({ database, table, name, }) {
|
|
390
|
+
const sql = [`
|
|
391
|
+
SELECT EXISTS(
|
|
392
|
+
SELECT 1
|
|
393
|
+
FROM sqlite_master AS m
|
|
394
|
+
WHERE m.type = 'table'
|
|
395
|
+
AND m.name = '${table.replace(/["`]/g, "")}'
|
|
396
|
+
AND (
|
|
397
|
+
('${name}' = 'PRIMARY KEY' AND m.sql LIKE '%PRIMARY KEY%')
|
|
398
|
+
OR
|
|
399
|
+
('${name}' = 'UNIQUE' AND EXISTS (
|
|
400
|
+
SELECT 1 FROM pragma_index_list(m.name) il WHERE il."unique" = 1
|
|
401
|
+
))
|
|
402
|
+
OR
|
|
403
|
+
('${name}' = 'FOREIGN KEY' AND EXISTS (
|
|
404
|
+
SELECT 1 FROM pragma_foreign_key_list(m.name)
|
|
405
|
+
))
|
|
406
|
+
)
|
|
407
|
+
) AS "IS_EXISTS"
|
|
408
|
+
`];
|
|
409
|
+
return this.format(sql);
|
|
410
|
+
}
|
|
411
|
+
addUnique({ table, name, columns }) {
|
|
412
|
+
const cols = columns
|
|
413
|
+
.map(col => `\`${col}\``)
|
|
414
|
+
.join(", ");
|
|
415
|
+
const sql = [
|
|
416
|
+
this.$constants("ALTER_TABLE"),
|
|
417
|
+
`\`${table}\``,
|
|
418
|
+
this.$constants("ADD_CONSTRAINT"),
|
|
419
|
+
`\`${name}\``,
|
|
420
|
+
this.$constants("UNIQUE"),
|
|
421
|
+
`(${cols})`
|
|
422
|
+
];
|
|
423
|
+
return this.format(sql);
|
|
424
|
+
}
|
|
425
|
+
dropUnique({ table, name, }) {
|
|
426
|
+
const sql = [
|
|
427
|
+
this.$constants("ALTER_TABLE"),
|
|
428
|
+
`\`${table}\``,
|
|
429
|
+
this.$constants("DROP"),
|
|
430
|
+
this.$constants("INDEX"),
|
|
431
|
+
`\`${name}\``
|
|
432
|
+
];
|
|
433
|
+
return this.format(sql);
|
|
434
|
+
}
|
|
435
|
+
hasPrimaryKey({ database, table, }) {
|
|
436
|
+
const sql = `
|
|
437
|
+
SELECT EXISTS(
|
|
438
|
+
SELECT 1
|
|
439
|
+
FROM pragma_table_info('${table.replace(/["'`]/g, "")}')
|
|
440
|
+
WHERE pk = 1
|
|
441
|
+
) AS IS_EXISTS
|
|
442
|
+
`;
|
|
443
|
+
return this.format(sql);
|
|
444
|
+
}
|
|
445
|
+
addPrimaryKey({ table, columns, }) {
|
|
446
|
+
const cols = columns
|
|
447
|
+
.map(col => `\`${col}\``)
|
|
448
|
+
.join(", ");
|
|
449
|
+
const sql = `
|
|
450
|
+
${this.$constants('ALTER_TABLE')} \`${table}\`
|
|
451
|
+
${this.$constants('ADD')} ${this.$constants('PRIMARY_KEY')} (${cols})
|
|
452
|
+
`;
|
|
453
|
+
return this.format(sql);
|
|
454
|
+
}
|
|
455
|
+
dropPrimaryKey({ table }) {
|
|
456
|
+
const sql = [
|
|
457
|
+
this.$constants("ALTER_TABLE"),
|
|
458
|
+
`\`${table}\``,
|
|
459
|
+
this.$constants("DROP"),
|
|
460
|
+
this.$constants("PRIMARY_KEY")
|
|
461
|
+
];
|
|
462
|
+
return this.format(sql);
|
|
463
|
+
}
|
|
464
|
+
getDatabase(database) {
|
|
465
|
+
const sql = [
|
|
466
|
+
`SELECT '${database.replace(/`/g, "")}' AS DB`,
|
|
467
|
+
].join(" ");
|
|
468
|
+
return this.format(sql);
|
|
469
|
+
}
|
|
470
|
+
dropDatabase(database) {
|
|
471
|
+
const sql = [
|
|
472
|
+
`${this.$constants("DROP_DATABASE")}`,
|
|
473
|
+
`\`${database.replace(/`/g, "")}\``
|
|
474
|
+
].join(" ");
|
|
475
|
+
return this.format(sql);
|
|
476
|
+
}
|
|
477
|
+
dropView(view) {
|
|
478
|
+
const sql = [
|
|
479
|
+
`${this.$constants("DROP_VIEW")}`,
|
|
480
|
+
`\`${view.replace(/`/g, "")}\``
|
|
481
|
+
].join(" ");
|
|
482
|
+
return this.format(sql);
|
|
483
|
+
}
|
|
484
|
+
dropTable(table) {
|
|
485
|
+
const sql = [
|
|
486
|
+
`${this.$constants("DROP_TABLE")}`,
|
|
487
|
+
`\`${table.replace(/`/g, "")}\``
|
|
488
|
+
].join(" ");
|
|
489
|
+
return this.format(sql);
|
|
490
|
+
}
|
|
491
|
+
truncate(table) {
|
|
492
|
+
const sql = [
|
|
493
|
+
`DELETE FROM "${table}"`,
|
|
494
|
+
].join(" ");
|
|
495
|
+
return this.format(sql);
|
|
496
|
+
}
|
|
497
|
+
sleep(second) {
|
|
498
|
+
return `SLEEP(${second})`;
|
|
499
|
+
}
|
|
500
|
+
format(sql) {
|
|
501
|
+
if (typeof sql === "string")
|
|
502
|
+
sql = [sql];
|
|
503
|
+
const formated = sql
|
|
504
|
+
.filter((s) => s !== "" || s == null)
|
|
505
|
+
.join(" ")
|
|
506
|
+
.replace(/\s+/g, " ");
|
|
507
|
+
const replaceBackticksWithDoubleQuotes = (sqlString) => {
|
|
508
|
+
const updateRegex = /^UPDATE\b/i;
|
|
509
|
+
const insertRegex = /^INSERT\b/i;
|
|
510
|
+
const deleteRegex = /^DELETE\b/i;
|
|
511
|
+
const truncateRegex = /^TRUNCATE\b/i;
|
|
512
|
+
if (updateRegex.test(sqlString)) {
|
|
513
|
+
sqlString = sqlString.replace(/(SET\s+)(.*?)(\s+WHERE)/is, (_, start, setPart, end) => {
|
|
514
|
+
const cleaned = setPart.replace(/`[\w$_]+`\./g, '');
|
|
515
|
+
return start + cleaned + end;
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
if (insertRegex.test(sqlString) ||
|
|
519
|
+
deleteRegex.test(sqlString) ||
|
|
520
|
+
truncateRegex.test(sqlString)) {
|
|
521
|
+
return sqlString
|
|
522
|
+
.replace(/`[\w$_]+`\.`([\w$_]+)`/g, "`$1`")
|
|
523
|
+
.replace(/`([^`]+)`/g, '"$1"');
|
|
524
|
+
}
|
|
525
|
+
return sqlString.replace(/`([^`]+)`/g, '"$1"');
|
|
526
|
+
};
|
|
527
|
+
return replaceBackticksWithDoubleQuotes(formated);
|
|
528
|
+
}
|
|
529
|
+
getActiveConnections() {
|
|
530
|
+
const sql = `SELECT 1 AS Connections`;
|
|
531
|
+
return this.format(sql);
|
|
532
|
+
}
|
|
533
|
+
getMaxConnections() {
|
|
534
|
+
const sql = `SELECT 151 AS MaxConnections`;
|
|
535
|
+
return this.format(sql);
|
|
536
|
+
}
|
|
537
|
+
bindJoin(values) {
|
|
538
|
+
if (!Array.isArray(values) || !values.length)
|
|
539
|
+
return null;
|
|
540
|
+
return values.join(" ");
|
|
541
|
+
}
|
|
542
|
+
bindWhere(values) {
|
|
543
|
+
if (!Array.isArray(values) || !values.length)
|
|
544
|
+
return null;
|
|
545
|
+
const serializeWhere = (wheres) => {
|
|
546
|
+
const resolveValue = ({ operator, value }) => {
|
|
547
|
+
let valueStr = '';
|
|
548
|
+
if (operator?.toUpperCase() === this.$constants('IN') && Array.isArray(value)) {
|
|
549
|
+
valueStr = `(${value.map((v) => v).join(',')})`;
|
|
550
|
+
}
|
|
551
|
+
else if (operator?.toUpperCase() === this.$constants('IS_NULL') ||
|
|
552
|
+
operator?.toUpperCase() === this.$constants('IS_NOT_NULL')) {
|
|
553
|
+
valueStr = '';
|
|
554
|
+
}
|
|
555
|
+
else {
|
|
556
|
+
valueStr = `${value}`;
|
|
557
|
+
}
|
|
558
|
+
return valueStr;
|
|
559
|
+
};
|
|
560
|
+
const conditionToSQL = (cond, isFirst = false) => {
|
|
561
|
+
const { column = '', operator = '', condition, value, nested } = cond;
|
|
562
|
+
if (nested && nested.length) {
|
|
563
|
+
const nestedSQL = nested
|
|
564
|
+
.map((c) => conditionToSQL(c))
|
|
565
|
+
.join(' ');
|
|
566
|
+
const valueStr = resolveValue({ operator, value });
|
|
567
|
+
if (!isFirst) {
|
|
568
|
+
return `${condition ?? this.$constants('AND')} (${column} ${operator} ${valueStr} ${nestedSQL})`;
|
|
569
|
+
}
|
|
570
|
+
return `(${column} ${operator} ${valueStr} ${nestedSQL})`;
|
|
571
|
+
}
|
|
572
|
+
const valueStr = resolveValue({ operator, value });
|
|
573
|
+
if (!isFirst) {
|
|
574
|
+
return `${condition ?? this.$constants('AND')} ${column} ${operator} ${valueStr}`.trim();
|
|
575
|
+
}
|
|
576
|
+
return `${column} ${operator} ${valueStr}`.trim();
|
|
577
|
+
};
|
|
578
|
+
return wheres.map((cond, i) => conditionToSQL(cond, !i)).join(' ');
|
|
579
|
+
};
|
|
580
|
+
return `${this.$constants("WHERE")} ${serializeWhere(values)}`;
|
|
581
|
+
}
|
|
582
|
+
bindOrderBy(values) {
|
|
583
|
+
if (!Array.isArray(values) || !values.length)
|
|
584
|
+
return null;
|
|
585
|
+
return `${this.$constants("ORDER_BY")} ${values
|
|
586
|
+
.map((v) => v.replace(/^\s/, "").replace(/\s+/g, " "))
|
|
587
|
+
.join(", ")}`;
|
|
588
|
+
}
|
|
589
|
+
bindGroupBy(values) {
|
|
590
|
+
if (!Array.isArray(values) || !values.length)
|
|
591
|
+
return null;
|
|
592
|
+
return `${this.$constants("GROUP_BY")} ${values
|
|
593
|
+
.map((v) => v.replace(/^\s/, "").replace(/\s+/g, " "))
|
|
594
|
+
.join(", ")}`;
|
|
595
|
+
}
|
|
596
|
+
bindSelect(values, { distinct } = {}) {
|
|
597
|
+
if (!values.length) {
|
|
598
|
+
if (!distinct)
|
|
599
|
+
return `${this.$constants("SELECT")} *`;
|
|
600
|
+
return `${this.$constants("SELECT")} ${this.$constants("DISTINCT")} *`;
|
|
601
|
+
}
|
|
602
|
+
const findIndex = values.indexOf("*");
|
|
603
|
+
if (findIndex > -1) {
|
|
604
|
+
const removed = values.splice(findIndex, 1);
|
|
605
|
+
values.unshift(removed[0]);
|
|
606
|
+
}
|
|
607
|
+
return `${this.$constants("SELECT")} ${values.join(", ")}`;
|
|
608
|
+
}
|
|
609
|
+
bindFrom({ from, alias, rawAlias, }) {
|
|
610
|
+
if (!from.length || from.every((f) => f == null || f === "")) {
|
|
611
|
+
return "";
|
|
612
|
+
}
|
|
613
|
+
if (alias != null && alias !== "") {
|
|
614
|
+
if (rawAlias != null && rawAlias !== "") {
|
|
615
|
+
const raw = String(rawAlias)
|
|
616
|
+
.replace(/^\(\s*|\s*\)$/g, "")
|
|
617
|
+
.trim();
|
|
618
|
+
const normalizedRawAlias = raw.startsWith("(") && raw.endsWith(")") ? raw.slice(1, -1) : raw;
|
|
619
|
+
raw.startsWith("(") && raw.endsWith(")") ? raw.slice(1, -1) : raw;
|
|
620
|
+
return `${this.$constants("FROM")} (${normalizedRawAlias}) ${this.$constants("AS")} \`${alias}\``;
|
|
621
|
+
}
|
|
622
|
+
return `${this.$constants("FROM")} ${from.join(", ")} ${this.$constants("AS")} \`${alias}\``;
|
|
623
|
+
}
|
|
624
|
+
return `${this.$constants("FROM")} ${from.join(", ")}`;
|
|
625
|
+
}
|
|
626
|
+
bindLimit(limit) {
|
|
627
|
+
if (limit === "" || limit == null)
|
|
628
|
+
return "";
|
|
629
|
+
return `${this.$constants("LIMIT")} ${limit}`;
|
|
630
|
+
}
|
|
631
|
+
bindOffset(offset) {
|
|
632
|
+
if (offset === "" || offset == null)
|
|
633
|
+
return "";
|
|
634
|
+
return `${this.$constants("OFFSET")} ${offset}`;
|
|
635
|
+
}
|
|
636
|
+
bindHaving(having) {
|
|
637
|
+
if (having == null || having === '')
|
|
638
|
+
return "";
|
|
639
|
+
return `${this.$constants("HAVING")} ${having}`;
|
|
640
|
+
}
|
|
641
|
+
bindRowLevelLock(rowLevelLock) {
|
|
642
|
+
// SQLite does NOT support SELECT … FOR UPDATE or SKIP LOCKED
|
|
643
|
+
return '';
|
|
644
|
+
}
|
|
645
|
+
_formatedTypeAndAttributes({ table, type, attributes, key, changed }) {
|
|
646
|
+
let formatedType = type;
|
|
647
|
+
let formatedAttributes = attributes;
|
|
648
|
+
let raws = null;
|
|
649
|
+
if (type.startsWith("INT") && !attributes.some((v) => v === "PRIMARY KEY")) {
|
|
650
|
+
formatedType = "INTEGER";
|
|
651
|
+
}
|
|
652
|
+
if (type.startsWith("INT") && attributes.some((v) => v === "PRIMARY KEY")) {
|
|
653
|
+
formatedType = "INTEGER";
|
|
654
|
+
formatedAttributes = attributes.filter((attr) => {
|
|
655
|
+
return !attr.startsWith("AUTO_INCREMENT");
|
|
656
|
+
});
|
|
657
|
+
}
|
|
658
|
+
if (type.startsWith("TINYINT")) {
|
|
659
|
+
formatedType = "SMALLINT";
|
|
660
|
+
}
|
|
661
|
+
if (type.startsWith("LONGTEXT") || type.startsWith("MEDIUMTEXT")) {
|
|
662
|
+
formatedType = "TEXT";
|
|
663
|
+
}
|
|
664
|
+
if (type.startsWith("ENUM")) {
|
|
665
|
+
const enums = type.replace("ENUM", "");
|
|
666
|
+
const totalLength = enums
|
|
667
|
+
.slice(1, -1)
|
|
668
|
+
.split(",")
|
|
669
|
+
.map((s) => s.replace(/'/g, ""))
|
|
670
|
+
.reduce((sum, item) => sum + item.length, 0);
|
|
671
|
+
formatedType = `VARCHAR(${totalLength}) CHECK (${key} IN ${enums})`;
|
|
672
|
+
}
|
|
673
|
+
if (type.startsWith("BOOLEAN")) {
|
|
674
|
+
formatedAttributes = attributes.map((attr) => {
|
|
675
|
+
if (attr.startsWith("DEFAULT")) {
|
|
676
|
+
return attr.replace(/\b0\b/, "false").replace(/\b1\b/, "true");
|
|
677
|
+
}
|
|
678
|
+
return attr;
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
return {
|
|
682
|
+
formatedType,
|
|
683
|
+
formatedAttributes,
|
|
684
|
+
raws
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
exports.SqliteQueryBuilder = SqliteQueryBuilder;
|
|
689
|
+
//# sourceMappingURL=SqliteQueryBuilder.js.map
|