tspace-mysql 1.9.4 → 1.9.6
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 +376 -72
- package/dist/cli/dump/db.js +3 -15
- package/dist/cli/dump/db.js.map +1 -1
- package/dist/cli/index.js +0 -3
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/migrations/make-db.js +1 -1
- package/dist/cli/migrations/make-db.js.map +1 -1
- package/dist/cli/migrations/make-model.js +1 -6
- package/dist/cli/migrations/make-model.js.map +1 -1
- package/dist/lib/config/index.d.ts +6 -5
- package/dist/lib/config/index.js +18 -8
- package/dist/lib/config/index.js.map +1 -1
- package/dist/lib/constants/index.d.ts +1 -1
- package/dist/lib/constants/index.js +1 -1
- package/dist/lib/constants/index.js.map +1 -1
- package/dist/lib/core/Abstracts/AbstractBuilder.d.ts +2 -1
- package/dist/lib/core/Abstracts/AbstractDB.d.ts +5 -3
- package/dist/lib/core/Abstracts/AbstractDB.js.map +1 -1
- package/dist/lib/core/Abstracts/AbstractModel.d.ts +1 -2
- package/dist/lib/core/Abstracts/AbstractModel.js +0 -1
- package/dist/lib/core/Abstracts/AbstractModel.js.map +1 -1
- package/dist/lib/core/Abstracts/AbstractView.d.ts +2 -1
- package/dist/lib/core/Abstracts/AbstractView.js.map +1 -1
- package/dist/lib/core/Blueprint.d.ts +13 -3
- package/dist/lib/core/Blueprint.js +16 -1
- package/dist/lib/core/Blueprint.js.map +1 -1
- package/dist/lib/core/Builder.d.ts +172 -19
- package/dist/lib/core/Builder.js +564 -197
- package/dist/lib/core/Builder.js.map +1 -1
- package/dist/lib/core/Cache/DBCache.js +6 -5
- package/dist/lib/core/Cache/DBCache.js.map +1 -1
- package/dist/lib/core/Cache/index.js +1 -1
- package/dist/lib/core/Cache/index.js.map +1 -1
- package/dist/lib/core/Contracts/Audit.d.ts +5 -3
- package/dist/lib/core/Contracts/Audit.js +18 -2
- package/dist/lib/core/Contracts/Audit.js.map +1 -1
- package/dist/lib/core/DB.d.ts +148 -190
- package/dist/lib/core/DB.js +459 -525
- package/dist/lib/core/DB.js.map +1 -1
- package/dist/lib/core/Driver/index.d.ts +40 -15
- package/dist/lib/core/Driver/index.js +3 -0
- package/dist/lib/core/Driver/index.js.map +1 -1
- package/dist/lib/core/Driver/mariadb/MariadbDriver.d.ts +3 -2
- package/dist/lib/core/Driver/mariadb/MariadbDriver.js +7 -5
- package/dist/lib/core/Driver/mariadb/MariadbDriver.js.map +1 -1
- package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.d.ts +42 -17
- package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.js +282 -154
- package/dist/lib/core/Driver/mariadb/MariadbQueryBuilder.js.map +1 -1
- package/dist/lib/core/Driver/mongodb/MongodbDriver.d.ts +4 -3
- package/dist/lib/core/Driver/mongodb/MongodbDriver.js +7 -5
- package/dist/lib/core/Driver/mongodb/MongodbDriver.js.map +1 -1
- package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.d.ts +40 -15
- package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.js +205 -157
- package/dist/lib/core/Driver/mongodb/MongodbQueryBuilder.js.map +1 -1
- package/dist/lib/core/Driver/mysql/MysqlDriver.d.ts +3 -2
- package/dist/lib/core/Driver/mysql/MysqlDriver.js +6 -14
- package/dist/lib/core/Driver/mysql/MysqlDriver.js.map +1 -1
- package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.d.ts +42 -17
- package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.js +252 -144
- package/dist/lib/core/Driver/mysql/MysqlQueryBuilder.js.map +1 -1
- package/dist/lib/core/Driver/postgres/PostgresDriver.d.ts +3 -2
- package/dist/lib/core/Driver/postgres/PostgresDriver.js +7 -5
- package/dist/lib/core/Driver/postgres/PostgresDriver.js.map +1 -1
- package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.d.ts +45 -19
- package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.js +472 -287
- package/dist/lib/core/Driver/postgres/PostgresQueryBuilder.js.map +1 -1
- package/dist/lib/core/Driver/sqlite/SqliteDriver.d.ts +3 -2
- package/dist/lib/core/Driver/sqlite/SqliteDriver.js +8 -6
- package/dist/lib/core/Driver/sqlite/SqliteDriver.js.map +1 -1
- package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.d.ts +47 -19
- package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.js +332 -203
- package/dist/lib/core/Driver/sqlite/SqliteQueryBuilder.js.map +1 -1
- package/dist/lib/core/Model.d.ts +255 -52
- package/dist/lib/core/Model.js +459 -117
- package/dist/lib/core/Model.js.map +1 -1
- package/dist/lib/core/Package.d.ts +5 -0
- package/dist/lib/core/Package.js +7 -2
- package/dist/lib/core/Package.js.map +1 -1
- package/dist/lib/core/Pool.d.ts +1 -1
- package/dist/lib/core/Pool.js +53 -75
- package/dist/lib/core/Pool.js.map +1 -1
- package/dist/lib/core/Queue/index.d.ts +2 -11
- package/dist/lib/core/Queue/index.js +3 -3
- package/dist/lib/core/Queue/index.js.map +1 -1
- package/dist/lib/core/Queue/types.d.ts +32 -3
- package/dist/lib/core/Queue/worker-lock.d.ts +17 -0
- package/dist/lib/core/Queue/worker-lock.js +28 -0
- package/dist/lib/core/Queue/worker-lock.js.map +1 -0
- package/dist/lib/core/Queue/worker.d.ts +37 -23
- package/dist/lib/core/Queue/worker.js +385 -171
- package/dist/lib/core/Queue/worker.js.map +1 -1
- package/dist/lib/core/RelationManager.js +1 -1
- package/dist/lib/core/RelationManager.js.map +1 -1
- package/dist/lib/core/Repository.d.ts +164 -29
- package/dist/lib/core/Repository.js +181 -40
- package/dist/lib/core/Repository.js.map +1 -1
- package/dist/lib/core/Schema.d.ts +11 -7
- package/dist/lib/core/Schema.js +249 -80
- package/dist/lib/core/Schema.js.map +1 -1
- package/dist/lib/core/SqlLike.js +4 -4
- package/dist/lib/core/SqlLike.js.map +1 -1
- package/dist/lib/core/StateManager.d.ts +19 -0
- package/dist/lib/core/StateManager.js +9 -4
- package/dist/lib/core/StateManager.js.map +1 -1
- package/dist/lib/core/UtilityTypes.d.ts +17 -3
- package/dist/lib/core/UtilityTypes.js.map +1 -1
- package/dist/lib/core/View.d.ts +3 -1
- package/dist/lib/core/View.js +18 -25
- package/dist/lib/core/View.js.map +1 -1
- package/dist/lib/core/index.d.ts +2 -0
- package/dist/lib/core/index.js +3 -1
- package/dist/lib/core/index.js.map +1 -1
- package/dist/lib/types/index.d.ts +77 -90
- package/dist/lib/types/repository/index.d.ts +18 -26
- package/dist/lib/utils/index.d.ts +7 -3
- package/dist/lib/utils/index.js +99 -37
- package/dist/lib/utils/index.js.map +1 -1
- package/package.json +1 -2
- package/dist/cli/dump/table.d.ts +0 -4
- package/dist/cli/dump/table.js +0 -41
- package/dist/cli/dump/table.js.map +0 -1
|
@@ -8,6 +8,221 @@ class PostgresQueryBuilder extends __1.QueryBuilder {
|
|
|
8
8
|
constructor(state) {
|
|
9
9
|
super(state);
|
|
10
10
|
}
|
|
11
|
+
bindJoin(values) {
|
|
12
|
+
if (!Array.isArray(values) || !values.length)
|
|
13
|
+
return null;
|
|
14
|
+
return values.join(" ");
|
|
15
|
+
}
|
|
16
|
+
bindWhere(values) {
|
|
17
|
+
if (!Array.isArray(values) || !values.length)
|
|
18
|
+
return null;
|
|
19
|
+
const serializeWhere = (wheres) => {
|
|
20
|
+
const resolveValue = ({ operator, value }) => {
|
|
21
|
+
let valueStr = '';
|
|
22
|
+
if ((operator?.toUpperCase() === this.$constants('IN') ||
|
|
23
|
+
operator?.toUpperCase() === this.$constants('NOT_IN')) && Array.isArray(value)) {
|
|
24
|
+
valueStr = `(${value.map((v) => v).join(',')})`;
|
|
25
|
+
}
|
|
26
|
+
else if (operator?.toUpperCase() === this.$constants('IS_NULL') ||
|
|
27
|
+
operator?.toUpperCase() === this.$constants('IS_NOT_NULL')) {
|
|
28
|
+
valueStr = '';
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
valueStr = `${value}`;
|
|
32
|
+
}
|
|
33
|
+
return valueStr;
|
|
34
|
+
};
|
|
35
|
+
const conditionToSQL = (cond, isFirst = false) => {
|
|
36
|
+
const { column = '', operator = '', condition, value, nested } = cond;
|
|
37
|
+
if (nested && nested.length) {
|
|
38
|
+
const nestedSQL = nested
|
|
39
|
+
.map((c) => conditionToSQL(c))
|
|
40
|
+
.join(' ');
|
|
41
|
+
const valueStr = resolveValue({ operator, value });
|
|
42
|
+
if (!isFirst) {
|
|
43
|
+
return `${condition ?? this.$constants('AND')} (${column} ${operator} ${valueStr} ${nestedSQL})`;
|
|
44
|
+
}
|
|
45
|
+
return `(${column} ${operator} ${valueStr} ${nestedSQL})`;
|
|
46
|
+
}
|
|
47
|
+
const valueStr = resolveValue({ operator, value });
|
|
48
|
+
if (!isFirst) {
|
|
49
|
+
return `${condition ?? this.$constants('AND')} ${column} ${operator} ${valueStr}`.trim();
|
|
50
|
+
}
|
|
51
|
+
return `${column} ${operator} ${valueStr}`.trim();
|
|
52
|
+
};
|
|
53
|
+
return wheres.map((cond, i) => conditionToSQL(cond, !i)).join(' ');
|
|
54
|
+
};
|
|
55
|
+
return `${this.$constants("WHERE")} ${serializeWhere(values)}`;
|
|
56
|
+
}
|
|
57
|
+
bindOrderBy(values) {
|
|
58
|
+
if (!Array.isArray(values) || !values.length)
|
|
59
|
+
return null;
|
|
60
|
+
return `${this.$constants("ORDER_BY")} ${values
|
|
61
|
+
.map((v) => v.replace(/^\s/, "").replace(/\s+/g, " "))
|
|
62
|
+
.join(", ")}`;
|
|
63
|
+
}
|
|
64
|
+
bindGroupBy(values) {
|
|
65
|
+
if (!Array.isArray(values) || !values.length)
|
|
66
|
+
return null;
|
|
67
|
+
return `${this.$constants("GROUP_BY")} ${values
|
|
68
|
+
.map((v) => v.replace(/^\s/, "").replace(/\s+/g, " "))
|
|
69
|
+
.join(", ")}`;
|
|
70
|
+
}
|
|
71
|
+
bindSelect(values) {
|
|
72
|
+
if (!values.length) {
|
|
73
|
+
return `${this.$constants("SELECT")} *`;
|
|
74
|
+
}
|
|
75
|
+
const findIndexStar = values.indexOf("*");
|
|
76
|
+
if (findIndexStar > -1) {
|
|
77
|
+
const removed = values.splice(findIndexStar, 1);
|
|
78
|
+
values.unshift(removed[0]);
|
|
79
|
+
}
|
|
80
|
+
const findIndexDistinct = values.findIndex(v => new RegExp(`^${this.$constants("DISTINCT")}\\s*\\(`, 'i').test(v));
|
|
81
|
+
if (findIndexDistinct > -1) {
|
|
82
|
+
const [distinct] = values.splice(findIndexDistinct, 1);
|
|
83
|
+
values.unshift(distinct);
|
|
84
|
+
}
|
|
85
|
+
return `${this.$constants("SELECT")} ${values.join(", ")}`;
|
|
86
|
+
}
|
|
87
|
+
bindFrom({ from, alias, rawAlias, }) {
|
|
88
|
+
if (!from.length || from.every((f) => f == null || f === "")) {
|
|
89
|
+
return "";
|
|
90
|
+
}
|
|
91
|
+
if (alias != null && alias !== "") {
|
|
92
|
+
if (rawAlias != null && rawAlias !== "") {
|
|
93
|
+
const raw = String(rawAlias)
|
|
94
|
+
.replace(/^\(\s*|\s*\)$/g, "")
|
|
95
|
+
.trim();
|
|
96
|
+
const normalizedRawAlias = raw.startsWith("(") && raw.endsWith(")") ? raw.slice(1, -1) : raw;
|
|
97
|
+
raw.startsWith("(") && raw.endsWith(")") ? raw.slice(1, -1) : raw;
|
|
98
|
+
return `${this.$constants("FROM")} (${normalizedRawAlias}) ${this.$constants("AS")} \`${alias}\``;
|
|
99
|
+
}
|
|
100
|
+
return `${this.$constants("FROM")} ${from.join(", ")} ${this.$constants("AS")} \`${alias}\``;
|
|
101
|
+
}
|
|
102
|
+
return `${this.$constants("FROM")} ${from.join(", ")}`;
|
|
103
|
+
}
|
|
104
|
+
bindLimit(limit) {
|
|
105
|
+
if (limit === "" || limit == null)
|
|
106
|
+
return "";
|
|
107
|
+
return `${this.$constants("LIMIT")} ${limit}`;
|
|
108
|
+
}
|
|
109
|
+
bindOffset(offset) {
|
|
110
|
+
if (offset === "" || offset == null)
|
|
111
|
+
return "";
|
|
112
|
+
return `${this.$constants("OFFSET")} ${offset}`;
|
|
113
|
+
}
|
|
114
|
+
bindHaving(having) {
|
|
115
|
+
if (having == null || having === '')
|
|
116
|
+
return "";
|
|
117
|
+
return `${this.$constants("HAVING")} ${having}`;
|
|
118
|
+
}
|
|
119
|
+
bindRowLevelLock(rowLevelLock) {
|
|
120
|
+
if (rowLevelLock.mode == null)
|
|
121
|
+
return '';
|
|
122
|
+
let modeLock = rowLevelLock.mode === "FOR_UPDATE"
|
|
123
|
+
? this.$constants("ROW_LEVEL_LOCK").update
|
|
124
|
+
: this.$constants("ROW_LEVEL_LOCK").share;
|
|
125
|
+
if (rowLevelLock.skipLocked) {
|
|
126
|
+
modeLock += ` ${this.$constants("ROW_LEVEL_LOCK").skipLocked}`;
|
|
127
|
+
}
|
|
128
|
+
if (rowLevelLock.nowait) {
|
|
129
|
+
modeLock += ` ${this.$constants("ROW_LEVEL_LOCK").nowait}`;
|
|
130
|
+
}
|
|
131
|
+
return modeLock;
|
|
132
|
+
}
|
|
133
|
+
_formatedTypeAndAttributes({ table, type, attributes, key, changed }) {
|
|
134
|
+
let formatedType = type;
|
|
135
|
+
let formatedAttributes = attributes;
|
|
136
|
+
let raws = null;
|
|
137
|
+
if (type.startsWith("INT") && attributes.some((v) => v === "PRIMARY KEY")) {
|
|
138
|
+
formatedType = "SERIAL";
|
|
139
|
+
if (changed) {
|
|
140
|
+
formatedType = `INT USING "${key}"::INTEGER`;
|
|
141
|
+
}
|
|
142
|
+
formatedAttributes = attributes.filter((attr) => {
|
|
143
|
+
return !attr.startsWith("AUTO_INCREMENT");
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
if (type.startsWith("TINYINT")) {
|
|
147
|
+
formatedType = "SMALLINT";
|
|
148
|
+
}
|
|
149
|
+
if (type.startsWith("LONGTEXT") || type.startsWith("MEDIUMTEXT")) {
|
|
150
|
+
formatedType = "TEXT";
|
|
151
|
+
}
|
|
152
|
+
if (type.startsWith("ENUM")) {
|
|
153
|
+
const enums = type.replace("ENUM", "");
|
|
154
|
+
const totalLength = enums
|
|
155
|
+
.slice(1, -1)
|
|
156
|
+
.split(",")
|
|
157
|
+
.map((s) => s.replace(/'/g, ""))
|
|
158
|
+
.reduce((sum, item) => sum + item.length, 0);
|
|
159
|
+
formatedType = `VARCHAR(${totalLength}) CHECK (${key} IN ${enums})`;
|
|
160
|
+
}
|
|
161
|
+
if (changed && type.startsWith("ENUM")) {
|
|
162
|
+
const enums = type.replace("ENUM", "");
|
|
163
|
+
const totalLength = enums
|
|
164
|
+
.slice(1, -1)
|
|
165
|
+
.split(",")
|
|
166
|
+
.map((s) => s.replace(/'/g, ""))
|
|
167
|
+
.reduce((sum, item) => sum + item.length, 0);
|
|
168
|
+
const enumType = `${[(0, pluralize_1.singular)(String(table ?? '')), key].join('_')}`;
|
|
169
|
+
raws = `
|
|
170
|
+
DO $$
|
|
171
|
+
DECLARE tname text;
|
|
172
|
+
BEGIN
|
|
173
|
+
|
|
174
|
+
SELECT t.TYPNAME
|
|
175
|
+
INTO tname
|
|
176
|
+
FROM PG_ATTRIBUTE a
|
|
177
|
+
JOIN PG_CLASS c ON c.OID = a.ATTRELID
|
|
178
|
+
JOIN PG_TYPE t ON t.OID = a.ATTTYPID
|
|
179
|
+
WHERE c.RELNAME = '${table}'
|
|
180
|
+
AND a.ATTNAME = '${key}'
|
|
181
|
+
AND a.ATTNUM > 0
|
|
182
|
+
AND t.TYPTYPE = 'e'
|
|
183
|
+
LIMIT 1;
|
|
184
|
+
|
|
185
|
+
IF tname IS NOT NULL THEN
|
|
186
|
+
|
|
187
|
+
ALTER TABLE ${table}
|
|
188
|
+
ALTER COLUMN ${key} DROP DEFAULT;
|
|
189
|
+
|
|
190
|
+
ALTER TABLE ${table}
|
|
191
|
+
ALTER COLUMN ${key} TYPE varchar(${totalLength});
|
|
192
|
+
|
|
193
|
+
EXECUTE 'DROP TYPE IF EXISTS ' || quote_ident(tname);
|
|
194
|
+
|
|
195
|
+
END IF;
|
|
196
|
+
|
|
197
|
+
DROP TYPE IF EXISTS ${enumType};
|
|
198
|
+
|
|
199
|
+
CREATE TYPE ${enumType} AS ENUM ${enums};
|
|
200
|
+
|
|
201
|
+
ALTER TABLE ${table}
|
|
202
|
+
ALTER COLUMN ${key} DROP DEFAULT;
|
|
203
|
+
|
|
204
|
+
ALTER TABLE ${table}
|
|
205
|
+
ALTER COLUMN ${key} TYPE ${enumType}
|
|
206
|
+
USING ${key}::${enumType};
|
|
207
|
+
|
|
208
|
+
END
|
|
209
|
+
$$
|
|
210
|
+
`;
|
|
211
|
+
}
|
|
212
|
+
if (type.startsWith("BOOLEAN")) {
|
|
213
|
+
formatedAttributes = attributes.map((attr) => {
|
|
214
|
+
if (attr.startsWith("DEFAULT")) {
|
|
215
|
+
return attr.replace(/\b0\b/, "false").replace(/\b1\b/, "true");
|
|
216
|
+
}
|
|
217
|
+
return attr;
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
return {
|
|
221
|
+
formatedType,
|
|
222
|
+
formatedAttributes,
|
|
223
|
+
raws
|
|
224
|
+
};
|
|
225
|
+
}
|
|
11
226
|
select = (selectColumns) => {
|
|
12
227
|
const combindSQL = [
|
|
13
228
|
this.bindSelect(selectColumns ?? this.$state.get("SELECT")),
|
|
@@ -143,52 +358,160 @@ class PostgresQueryBuilder extends __1.QueryBuilder {
|
|
|
143
358
|
];
|
|
144
359
|
return this.format(sql);
|
|
145
360
|
}
|
|
146
|
-
getSchema({ database, table }) {
|
|
361
|
+
getSchema({ database, table, }) {
|
|
362
|
+
const safeTable = table.replace(/`/g, "");
|
|
363
|
+
const safeDatabase = database.replace(/`/g, "");
|
|
147
364
|
const sql = [
|
|
148
|
-
`
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
365
|
+
`
|
|
366
|
+
SELECT
|
|
367
|
+
c.COLUMN_NAME AS "Field",
|
|
368
|
+
|
|
369
|
+
CASE
|
|
370
|
+
WHEN c.column_default LIKE 'nextval(%'
|
|
371
|
+
THEN 'PRI'
|
|
152
372
|
ELSE NULL
|
|
153
|
-
|
|
373
|
+
END AS "Key",
|
|
154
374
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
375
|
+
CASE
|
|
376
|
+
WHEN c.DATA_TYPE = 'USER-DEFINED' THEN
|
|
377
|
+
'enum(' ||
|
|
378
|
+
(
|
|
379
|
+
SELECT string_agg(
|
|
380
|
+
quote_literal(e.enumlabel),
|
|
381
|
+
','
|
|
382
|
+
ORDER BY e.enumsortorder
|
|
383
|
+
)
|
|
384
|
+
FROM PG_TYPE t
|
|
385
|
+
JOIN PG_ENUM e
|
|
386
|
+
ON t.oid = e.enumtypid
|
|
387
|
+
WHERE t.typname = c.udt_name
|
|
388
|
+
) ||
|
|
389
|
+
')'
|
|
165
390
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
THEN
|
|
169
|
-
|
|
170
|
-
END AS "Type",
|
|
391
|
+
WHEN c.DATA_TYPE = 'character varying'
|
|
392
|
+
AND c.CHARACTER_MAXIMUM_LENGTH IS NOT NULL
|
|
393
|
+
THEN
|
|
394
|
+
c.DATA_TYPE || '(' || c.CHARACTER_MAXIMUM_LENGTH || ')'
|
|
171
395
|
|
|
172
|
-
|
|
396
|
+
ELSE c.DATA_TYPE
|
|
397
|
+
END AS "Type",
|
|
173
398
|
|
|
174
|
-
|
|
175
|
-
WHEN COLUMN_DEFAULT LIKE 'nextval(%' THEN NULL
|
|
176
|
-
WHEN COLUMN_DEFAULT = 'CURRENT_TIMESTAMP' THEN 'IS_CONST:CURRENT_TIMESTAMP'
|
|
177
|
-
ELSE COLUMN_DEFAULT
|
|
178
|
-
END AS "Default",
|
|
399
|
+
c.IS_NULLABLE AS "Nullable",
|
|
179
400
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
401
|
+
CASE
|
|
402
|
+
WHEN c.COLUMN_DEFAULT LIKE 'nextval(%'
|
|
403
|
+
THEN NULL
|
|
404
|
+
|
|
405
|
+
WHEN c.COLUMN_DEFAULT = 'CURRENT_TIMESTAMP'
|
|
406
|
+
THEN 'IS_CONST:CURRENT_TIMESTAMP'
|
|
407
|
+
|
|
408
|
+
ELSE c.COLUMN_DEFAULT
|
|
409
|
+
END AS "Default",
|
|
410
|
+
|
|
411
|
+
CASE
|
|
412
|
+
WHEN c.COLUMN_DEFAULT LIKE 'nextval(%'
|
|
413
|
+
THEN 'AUTO_INCREMENT'
|
|
414
|
+
ELSE NULL
|
|
415
|
+
END AS "Extra",
|
|
416
|
+
|
|
417
|
+
(
|
|
418
|
+
SELECT pg_get_constraintdef(con.oid)
|
|
419
|
+
FROM pg_constraint con
|
|
420
|
+
JOIN pg_class tbl
|
|
421
|
+
ON tbl.oid = con.conrelid
|
|
422
|
+
WHERE tbl.relname = c.TABLE_NAME
|
|
423
|
+
AND con.contype = 'c'
|
|
424
|
+
AND c.COLUMN_NAME = ANY (
|
|
425
|
+
SELECT att.attname
|
|
426
|
+
FROM pg_attribute att
|
|
427
|
+
WHERE att.attrelid = tbl.oid
|
|
428
|
+
AND att.attnum = ANY(con.conkey)
|
|
429
|
+
)
|
|
430
|
+
LIMIT 1
|
|
431
|
+
) AS "Check"
|
|
432
|
+
|
|
433
|
+
FROM INFORMATION_SCHEMA.COLUMNS c
|
|
434
|
+
|
|
435
|
+
WHERE c.TABLE_NAME = '${safeTable}'
|
|
436
|
+
AND c.TABLE_CATALOG = '${safeDatabase}'
|
|
437
|
+
|
|
438
|
+
ORDER BY c.ORDINAL_POSITION
|
|
439
|
+
`,
|
|
189
440
|
];
|
|
190
441
|
return this.format(sql);
|
|
191
442
|
}
|
|
443
|
+
mapSchema(schema) {
|
|
444
|
+
const normalizeDefault = (value) => {
|
|
445
|
+
if (value.includes("IS_CONST:")) {
|
|
446
|
+
return value.replace("IS_CONST:", "");
|
|
447
|
+
}
|
|
448
|
+
const castedString = value.match(/^'(.*)'::[\w\s.]+$/);
|
|
449
|
+
if (castedString) {
|
|
450
|
+
return `'${castedString[1].replace(/'/g, "''")}'`;
|
|
451
|
+
}
|
|
452
|
+
return value;
|
|
453
|
+
};
|
|
454
|
+
return schema.map((r) => {
|
|
455
|
+
const str = [];
|
|
456
|
+
str.push(`\`${r.Field}\``);
|
|
457
|
+
let checkValues = [];
|
|
458
|
+
if (r.Type.startsWith("enum(")) {
|
|
459
|
+
const enumContent = r.Type.match(/^enum\((.*)\)$/)?.[1] ?? "";
|
|
460
|
+
checkValues =
|
|
461
|
+
enumContent.match(/'(?:''|[^'])*'/g)?.map((value) => value
|
|
462
|
+
.slice(1, -1)
|
|
463
|
+
.replace(/''/g, "'")) ?? [];
|
|
464
|
+
}
|
|
465
|
+
if (checkValues.length === 0 && r.Check) {
|
|
466
|
+
checkValues =
|
|
467
|
+
r.Check
|
|
468
|
+
.match(/'((?:''|[^'])*)'::character varying/g)
|
|
469
|
+
?.map((value) => value
|
|
470
|
+
.replace(/^'|'::character varying$/g, "")
|
|
471
|
+
.replace(/''/g, "'")) ?? [];
|
|
472
|
+
}
|
|
473
|
+
if (checkValues.length > 0) {
|
|
474
|
+
const maxLength = Math.max(...checkValues.map((value) => value.length), 1);
|
|
475
|
+
str.push(`character varying(${maxLength})`);
|
|
476
|
+
}
|
|
477
|
+
else {
|
|
478
|
+
if (r.Key === 'PRI') {
|
|
479
|
+
str.push('serial');
|
|
480
|
+
}
|
|
481
|
+
else {
|
|
482
|
+
str.push(r.Type);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
if (r.Nullable === "YES") {
|
|
486
|
+
str.push("NULL");
|
|
487
|
+
}
|
|
488
|
+
if (r.Nullable === "NO") {
|
|
489
|
+
str.push("NOT NULL");
|
|
490
|
+
}
|
|
491
|
+
if (r.Key === "PRI") {
|
|
492
|
+
str.push("PRIMARY KEY");
|
|
493
|
+
}
|
|
494
|
+
if (r.Key === "UNI") {
|
|
495
|
+
str.push("UNIQUE");
|
|
496
|
+
}
|
|
497
|
+
if (r.Default !== null && r.Default !== undefined) {
|
|
498
|
+
const defaultValue = normalizeDefault(String(r.Default));
|
|
499
|
+
if (!/^NULL(?:::.*)?$/.test(defaultValue)) {
|
|
500
|
+
str.push(`DEFAULT ${defaultValue}`);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
if (r.Extra && r.Key !== 'PRI') {
|
|
504
|
+
str.push(r.Extra.toUpperCase());
|
|
505
|
+
}
|
|
506
|
+
if (checkValues.length > 0) {
|
|
507
|
+
const values = checkValues
|
|
508
|
+
.map((value) => `'${value.replace(/'/g, "''")}'`)
|
|
509
|
+
.join(", ");
|
|
510
|
+
str.push(`CHECK (\`${r.Field}\` IN (${values}))`);
|
|
511
|
+
}
|
|
512
|
+
return str.join(" ");
|
|
513
|
+
});
|
|
514
|
+
}
|
|
192
515
|
getTables(database) {
|
|
193
516
|
const sql = [
|
|
194
517
|
`
|
|
@@ -249,12 +572,15 @@ class PostgresQueryBuilder extends __1.QueryBuilder {
|
|
|
249
572
|
const sql = `CREATE DATABASE \`${database}\``;
|
|
250
573
|
return this.format(sql);
|
|
251
574
|
}
|
|
575
|
+
useDatabase(database) {
|
|
576
|
+
return `-- postgres does not support 'USE DATABASE'`;
|
|
577
|
+
}
|
|
252
578
|
createTable({ database, table, schema, }) {
|
|
253
579
|
let columns = [];
|
|
254
580
|
if (Array.isArray(schema)) {
|
|
255
581
|
const sql = [
|
|
256
582
|
`${this.$constants("CREATE_TABLE_NOT_EXISTS")}`,
|
|
257
|
-
`\`${
|
|
583
|
+
`\`${table}\``,
|
|
258
584
|
`(${schema.join(", ")})`,
|
|
259
585
|
];
|
|
260
586
|
return this.format(sql);
|
|
@@ -290,7 +616,8 @@ class PostgresQueryBuilder extends __1.QueryBuilder {
|
|
|
290
616
|
}
|
|
291
617
|
const sql = [
|
|
292
618
|
`${this.$constants("CREATE_TABLE_NOT_EXISTS")}`,
|
|
293
|
-
|
|
619
|
+
`\`${table}\``,
|
|
620
|
+
`(${columns.join(", ")})`,
|
|
294
621
|
];
|
|
295
622
|
return this.format(sql);
|
|
296
623
|
}
|
|
@@ -388,24 +715,31 @@ class PostgresQueryBuilder extends __1.QueryBuilder {
|
|
|
388
715
|
const sql = [
|
|
389
716
|
`
|
|
390
717
|
SELECT
|
|
391
|
-
ccu.TABLE_NAME
|
|
392
|
-
ccu.COLUMN_NAME
|
|
393
|
-
kcu.COLUMN_NAME
|
|
394
|
-
tc.CONSTRAINT_NAME
|
|
395
|
-
|
|
718
|
+
ccu.TABLE_NAME AS "RefTable",
|
|
719
|
+
ccu.COLUMN_NAME AS "RefColumn",
|
|
720
|
+
kcu.COLUMN_NAME AS "Column",
|
|
721
|
+
tc.CONSTRAINT_NAME AS "Constraint",
|
|
722
|
+
rc.DELETE_RULE AS "OnDelete",
|
|
723
|
+
rc.UPDATE_RULE AS "OnUpdate"
|
|
724
|
+
FROM
|
|
396
725
|
INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tc
|
|
397
|
-
JOIN
|
|
726
|
+
JOIN
|
|
398
727
|
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS kcu
|
|
399
|
-
ON tc.CONSTRAINT_NAME
|
|
400
|
-
AND tc.TABLE_SCHEMA
|
|
401
|
-
|
|
728
|
+
ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
|
|
729
|
+
AND tc.TABLE_SCHEMA = kcu.TABLE_SCHEMA
|
|
730
|
+
AND tc.TABLE_NAME = kcu.TABLE_NAME
|
|
731
|
+
JOIN
|
|
402
732
|
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS ccu
|
|
403
733
|
ON ccu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
|
|
404
|
-
AND ccu.
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
AND
|
|
734
|
+
AND ccu.CONSTRAINT_SCHEMA = tc.CONSTRAINT_SCHEMA
|
|
735
|
+
JOIN
|
|
736
|
+
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS rc
|
|
737
|
+
ON rc.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
|
|
738
|
+
AND rc.CONSTRAINT_SCHEMA = tc.CONSTRAINT_SCHEMA
|
|
739
|
+
WHERE
|
|
740
|
+
tc.CONSTRAINT_TYPE = 'FOREIGN KEY'
|
|
741
|
+
AND tc.TABLE_CATALOG = '${database.replace(/`/g, "")}'
|
|
742
|
+
AND tc.TABLE_NAME = '${table.replace(/`/g, "")}'
|
|
409
743
|
`,
|
|
410
744
|
];
|
|
411
745
|
return this.format(sql);
|
|
@@ -454,36 +788,62 @@ class PostgresQueryBuilder extends __1.QueryBuilder {
|
|
|
454
788
|
const sql = [
|
|
455
789
|
`
|
|
456
790
|
SELECT
|
|
457
|
-
DISTINCT ON (a.ATTNAME, i.RELNAME)
|
|
458
|
-
a.ATTNAME AS "Column",
|
|
459
791
|
i.RELNAME AS "IndexName",
|
|
792
|
+
|
|
793
|
+
STRING_AGG(
|
|
794
|
+
a.ATTNAME,
|
|
795
|
+
',' ORDER BY seq.NUMBER
|
|
796
|
+
) AS "Column",
|
|
797
|
+
|
|
460
798
|
UPPER(am.AMNAME) AS "IndexType",
|
|
461
|
-
|
|
462
|
-
CASE
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
799
|
+
|
|
800
|
+
CASE
|
|
801
|
+
WHEN BOOL_OR(NOT a.ATTNOTNULL) THEN 'YES'
|
|
802
|
+
ELSE 'NO'
|
|
803
|
+
END AS "Nullable",
|
|
804
|
+
|
|
805
|
+
CASE
|
|
806
|
+
WHEN ix.INDISUNIQUE THEN 'YES'
|
|
807
|
+
ELSE 'NO'
|
|
808
|
+
END AS "Unique"
|
|
809
|
+
|
|
810
|
+
FROM PG_CLASS t
|
|
811
|
+
|
|
812
|
+
JOIN PG_NAMESPACE n
|
|
813
|
+
ON n.OID = t.RELNAMESPACE
|
|
814
|
+
|
|
815
|
+
JOIN PG_INDEX ix
|
|
816
|
+
ON ix.INDRELID = t.OID
|
|
817
|
+
|
|
818
|
+
JOIN PG_CLASS i
|
|
819
|
+
ON i.OID = ix.INDEXRELID
|
|
820
|
+
|
|
821
|
+
JOIN PG_AM am
|
|
822
|
+
ON am.OID = i.RELAM
|
|
823
|
+
|
|
824
|
+
JOIN LATERAL (
|
|
825
|
+
SELECT
|
|
826
|
+
UNNEST(ix.INDKEY) AS ATTR_NUM,
|
|
827
|
+
GENERATE_SERIES(
|
|
828
|
+
1,
|
|
829
|
+
ARRAY_LENGTH(ix.INDKEY, 1)
|
|
830
|
+
) AS NUMBER
|
|
831
|
+
) seq
|
|
832
|
+
ON TRUE
|
|
833
|
+
|
|
834
|
+
JOIN PG_ATTRIBUTE a
|
|
835
|
+
ON a.ATTRELID = t.OID
|
|
836
|
+
AND a.ATTNUM = seq.ATTR_NUM
|
|
837
|
+
|
|
838
|
+
WHERE
|
|
839
|
+
t.RELKIND = 'r'
|
|
840
|
+
AND t.RELNAME = '${table.replace(/`/g, "")}'
|
|
841
|
+
AND ix.INDISPRIMARY = false
|
|
842
|
+
|
|
843
|
+
GROUP BY
|
|
844
|
+
i.RELNAME,
|
|
845
|
+
am.AMNAME,
|
|
846
|
+
ix.INDISUNIQUE
|
|
487
847
|
`,
|
|
488
848
|
];
|
|
489
849
|
return this.format(sql);
|
|
@@ -541,7 +901,7 @@ class PostgresQueryBuilder extends __1.QueryBuilder {
|
|
|
541
901
|
SELECT 1
|
|
542
902
|
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
|
|
543
903
|
WHERE
|
|
544
|
-
|
|
904
|
+
TABLE_CATALOG = '${database.replace(/['"`]/g, "")}'
|
|
545
905
|
AND TABLE_NAME = '${table.replace(/['"`]/g, "")}'
|
|
546
906
|
AND CONSTRAINT_NAME = '${name.replace(/['"`]/g, "")}'
|
|
547
907
|
AND CONSTRAINT_TYPE = '${this.$constants("UNIQUE")}'
|
|
@@ -580,7 +940,7 @@ class PostgresQueryBuilder extends __1.QueryBuilder {
|
|
|
580
940
|
SELECT 1
|
|
581
941
|
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
|
|
582
942
|
WHERE
|
|
583
|
-
|
|
943
|
+
TABLE_CATALOG = '${database.replace(/['"`]/g, "")}'
|
|
584
944
|
AND TABLE_NAME = '${table.replace(/['"`]/g, "")}'
|
|
585
945
|
AND CONSTRAINT_TYPE = '${this.$constants('PRIMARY_KEY')}'
|
|
586
946
|
) AS "IS_EXISTS"
|
|
@@ -640,7 +1000,8 @@ class PostgresQueryBuilder extends __1.QueryBuilder {
|
|
|
640
1000
|
truncate(table) {
|
|
641
1001
|
const sql = [
|
|
642
1002
|
`${this.$constants("TRUNCATE_TABLE")}`,
|
|
643
|
-
`\`${table.replace(/`/g, "")}
|
|
1003
|
+
`\`${table.replace(/`/g, "")}\``,
|
|
1004
|
+
`RESTART IDENTITY`
|
|
644
1005
|
].join(" ");
|
|
645
1006
|
return this.format(sql);
|
|
646
1007
|
}
|
|
@@ -708,219 +1069,43 @@ class PostgresQueryBuilder extends __1.QueryBuilder {
|
|
|
708
1069
|
// in postgress not have unlock table, unlock when trx COMMIT / ROLLBACK
|
|
709
1070
|
return null;
|
|
710
1071
|
}
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
return null;
|
|
714
|
-
return values.join(" ");
|
|
1072
|
+
explain(sql) {
|
|
1073
|
+
return this.format(`EXPLAIN (${sql})`);
|
|
715
1074
|
}
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
return null;
|
|
719
|
-
const serializeWhere = (wheres) => {
|
|
720
|
-
const resolveValue = ({ operator, value }) => {
|
|
721
|
-
let valueStr = '';
|
|
722
|
-
if (operator?.toUpperCase() === this.$constants('IN') && Array.isArray(value)) {
|
|
723
|
-
valueStr = `(${value.map((v) => v).join(',')})`;
|
|
724
|
-
}
|
|
725
|
-
else if (operator?.toUpperCase() === this.$constants('IS_NULL') ||
|
|
726
|
-
operator?.toUpperCase() === this.$constants('IS_NOT_NULL')) {
|
|
727
|
-
valueStr = '';
|
|
728
|
-
}
|
|
729
|
-
else {
|
|
730
|
-
valueStr = `${value}`;
|
|
731
|
-
}
|
|
732
|
-
return valueStr;
|
|
733
|
-
};
|
|
734
|
-
const conditionToSQL = (cond, isFirst = false) => {
|
|
735
|
-
const { column = '', operator = '', condition, value, nested } = cond;
|
|
736
|
-
if (nested && nested.length) {
|
|
737
|
-
const nestedSQL = nested
|
|
738
|
-
.map((c) => conditionToSQL(c))
|
|
739
|
-
.join(' ');
|
|
740
|
-
const valueStr = resolveValue({ operator, value });
|
|
741
|
-
if (!isFirst) {
|
|
742
|
-
return `${condition ?? this.$constants('AND')} (${column} ${operator} ${valueStr} ${nestedSQL})`;
|
|
743
|
-
}
|
|
744
|
-
return `(${column} ${operator} ${valueStr} ${nestedSQL})`;
|
|
745
|
-
}
|
|
746
|
-
const valueStr = resolveValue({ operator, value });
|
|
747
|
-
if (!isFirst) {
|
|
748
|
-
return `${condition ?? this.$constants('AND')} ${column} ${operator} ${valueStr}`.trim();
|
|
749
|
-
}
|
|
750
|
-
return `${column} ${operator} ${valueStr}`.trim();
|
|
751
|
-
};
|
|
752
|
-
return wheres.map((cond, i) => conditionToSQL(cond, !i)).join(' ');
|
|
753
|
-
};
|
|
754
|
-
return `${this.$constants("WHERE")} ${serializeWhere(values)}`;
|
|
1075
|
+
nameSession(name) {
|
|
1076
|
+
return this.format(`_sessions.${name}`);
|
|
755
1077
|
}
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
return null;
|
|
759
|
-
return `${this.$constants("ORDER_BY")} ${values
|
|
760
|
-
.map((v) => v.replace(/^\s/, "").replace(/\s+/g, " "))
|
|
761
|
-
.join(", ")}`;
|
|
1078
|
+
getSession(name) {
|
|
1079
|
+
return this.format(`SELECT CURRENT_SETTING('${this.nameSession(name)}') AS ${name}`);
|
|
762
1080
|
}
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
return null;
|
|
766
|
-
return `${this.$constants("GROUP_BY")} ${values
|
|
767
|
-
.map((v) => v.replace(/^\s/, "").replace(/\s+/g, " "))
|
|
768
|
-
.join(", ")}`;
|
|
1081
|
+
setSession(name, value) {
|
|
1082
|
+
return this.format(`SET ${this.nameSession(name)} = '${value}'`);
|
|
769
1083
|
}
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
return `${this.$constants("SELECT")} *`;
|
|
773
|
-
}
|
|
774
|
-
const findIndexStar = values.indexOf("*");
|
|
775
|
-
if (findIndexStar > -1) {
|
|
776
|
-
const removed = values.splice(findIndexStar, 1);
|
|
777
|
-
values.unshift(removed[0]);
|
|
778
|
-
}
|
|
779
|
-
const findIndexDistinct = values.findIndex(v => new RegExp(`^${this.$constants("DISTINCT")}\\s*\\(`, 'i').test(v));
|
|
780
|
-
if (findIndexDistinct > -1) {
|
|
781
|
-
const [distinct] = values.splice(findIndexDistinct, 1);
|
|
782
|
-
values.unshift(distinct);
|
|
783
|
-
}
|
|
784
|
-
return `${this.$constants("SELECT")} ${values.join(", ")}`;
|
|
785
|
-
}
|
|
786
|
-
bindFrom({ from, alias, rawAlias, }) {
|
|
787
|
-
if (!from.length || from.every((f) => f == null || f === "")) {
|
|
788
|
-
return "";
|
|
789
|
-
}
|
|
790
|
-
if (alias != null && alias !== "") {
|
|
791
|
-
if (rawAlias != null && rawAlias !== "") {
|
|
792
|
-
const raw = String(rawAlias)
|
|
793
|
-
.replace(/^\(\s*|\s*\)$/g, "")
|
|
794
|
-
.trim();
|
|
795
|
-
const normalizedRawAlias = raw.startsWith("(") && raw.endsWith(")") ? raw.slice(1, -1) : raw;
|
|
796
|
-
raw.startsWith("(") && raw.endsWith(")") ? raw.slice(1, -1) : raw;
|
|
797
|
-
return `${this.$constants("FROM")} (${normalizedRawAlias}) ${this.$constants("AS")} \`${alias}\``;
|
|
798
|
-
}
|
|
799
|
-
return `${this.$constants("FROM")} ${from.join(", ")} ${this.$constants("AS")} \`${alias}\``;
|
|
800
|
-
}
|
|
801
|
-
return `${this.$constants("FROM")} ${from.join(", ")}`;
|
|
1084
|
+
deleteSession(name) {
|
|
1085
|
+
return this.format(`RESET ${this.nameSession(name)}`);
|
|
802
1086
|
}
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
return "";
|
|
806
|
-
return `${this.$constants("LIMIT")} ${limit}`;
|
|
1087
|
+
createView({ expression, name, }) {
|
|
1088
|
+
return this.format(`CREATE VIEW ${name} AS ${expression}`);
|
|
807
1089
|
}
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
1090
|
+
hasView({ database, name, }) {
|
|
1091
|
+
const sql = `
|
|
1092
|
+
SELECT EXISTS (
|
|
1093
|
+
SELECT 1
|
|
1094
|
+
FROM INFORMATION_SCHEMA.VIEWS
|
|
1095
|
+
WHERE TABLE_CATALOG = '${database.replace(/\`/g, "")}'
|
|
1096
|
+
AND TABLE_NAME = '${name.replace(/\`/g, "")}'
|
|
1097
|
+
) AS "IS_EXISTS"
|
|
1098
|
+
`;
|
|
1099
|
+
return this.format(sql);
|
|
812
1100
|
}
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
return "";
|
|
816
|
-
return `${this.$constants("HAVING")} ${having}`;
|
|
1101
|
+
random() {
|
|
1102
|
+
return this.format(`RANDOM()`);
|
|
817
1103
|
}
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
return '';
|
|
821
|
-
let modeLock = rowLevelLock.mode === "FOR_UPDATE"
|
|
822
|
-
? this.$constants("ROW_LEVEL_LOCK").update
|
|
823
|
-
: this.$constants("ROW_LEVEL_LOCK").share;
|
|
824
|
-
if (rowLevelLock.skipLocked) {
|
|
825
|
-
modeLock += ` ${this.$constants("ROW_LEVEL_LOCK").skipLocked}`;
|
|
826
|
-
}
|
|
827
|
-
if (rowLevelLock.nowait) {
|
|
828
|
-
modeLock += ` ${this.$constants("ROW_LEVEL_LOCK").nowait}`;
|
|
829
|
-
}
|
|
830
|
-
return modeLock;
|
|
1104
|
+
whereJson(column, path) {
|
|
1105
|
+
return this.format(`${column}->>'${path}'`);
|
|
831
1106
|
}
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
let formatedAttributes = attributes;
|
|
835
|
-
let raws = null;
|
|
836
|
-
if (type.startsWith("INT") && attributes.some((v) => v === "PRIMARY KEY")) {
|
|
837
|
-
formatedType = "SERIAL";
|
|
838
|
-
if (changed) {
|
|
839
|
-
formatedType = `INT USING "${key}"::INTEGER`;
|
|
840
|
-
}
|
|
841
|
-
formatedAttributes = attributes.filter((attr) => {
|
|
842
|
-
return !attr.startsWith("AUTO_INCREMENT");
|
|
843
|
-
});
|
|
844
|
-
}
|
|
845
|
-
if (type.startsWith("TINYINT")) {
|
|
846
|
-
formatedType = "SMALLINT";
|
|
847
|
-
}
|
|
848
|
-
if (type.startsWith("LONGTEXT") || type.startsWith("MEDIUMTEXT")) {
|
|
849
|
-
formatedType = "TEXT";
|
|
850
|
-
}
|
|
851
|
-
if (type.startsWith("ENUM")) {
|
|
852
|
-
const enums = type.replace("ENUM", "");
|
|
853
|
-
const totalLength = enums
|
|
854
|
-
.slice(1, -1)
|
|
855
|
-
.split(",")
|
|
856
|
-
.map((s) => s.replace(/'/g, ""))
|
|
857
|
-
.reduce((sum, item) => sum + item.length, 0);
|
|
858
|
-
formatedType = `VARCHAR(${totalLength}) CHECK (${key} IN ${enums})`;
|
|
859
|
-
}
|
|
860
|
-
if (changed && type.startsWith("ENUM")) {
|
|
861
|
-
const enums = type.replace("ENUM", "");
|
|
862
|
-
const totalLength = enums
|
|
863
|
-
.slice(1, -1)
|
|
864
|
-
.split(",")
|
|
865
|
-
.map((s) => s.replace(/'/g, ""))
|
|
866
|
-
.reduce((sum, item) => sum + item.length, 0);
|
|
867
|
-
const enumType = `${[(0, pluralize_1.singular)(String(table ?? '')), key].join('_')}`;
|
|
868
|
-
raws = `
|
|
869
|
-
DO $$
|
|
870
|
-
DECLARE tname text;
|
|
871
|
-
BEGIN
|
|
872
|
-
|
|
873
|
-
SELECT t.TYPNAME
|
|
874
|
-
INTO tname
|
|
875
|
-
FROM PG_ATTRIBUTE a
|
|
876
|
-
JOIN PG_CLASS c ON c.OID = a.ATTRELID
|
|
877
|
-
JOIN PG_TYPE t ON t.OID = a.ATTTYPID
|
|
878
|
-
WHERE c.RELNAME = '${table}'
|
|
879
|
-
AND a.ATTNAME = '${key}'
|
|
880
|
-
AND a.ATTNUM > 0
|
|
881
|
-
AND t.TYPTYPE = 'e'
|
|
882
|
-
LIMIT 1;
|
|
883
|
-
|
|
884
|
-
IF tname IS NOT NULL THEN
|
|
885
|
-
|
|
886
|
-
ALTER TABLE ${table}
|
|
887
|
-
ALTER COLUMN ${key} DROP DEFAULT;
|
|
888
|
-
|
|
889
|
-
ALTER TABLE ${table}
|
|
890
|
-
ALTER COLUMN ${key} TYPE varchar(${totalLength});
|
|
891
|
-
|
|
892
|
-
EXECUTE 'DROP TYPE IF EXISTS ' || quote_ident(tname);
|
|
893
|
-
|
|
894
|
-
END IF;
|
|
895
|
-
|
|
896
|
-
DROP TYPE IF EXISTS ${enumType};
|
|
897
|
-
|
|
898
|
-
CREATE TYPE ${enumType} AS ENUM ${enums};
|
|
899
|
-
|
|
900
|
-
ALTER TABLE ${table}
|
|
901
|
-
ALTER COLUMN ${key} DROP DEFAULT;
|
|
902
|
-
|
|
903
|
-
ALTER TABLE ${table}
|
|
904
|
-
ALTER COLUMN ${key} TYPE ${enumType}
|
|
905
|
-
USING ${key}::${enumType};
|
|
906
|
-
|
|
907
|
-
END
|
|
908
|
-
$$
|
|
909
|
-
`;
|
|
910
|
-
}
|
|
911
|
-
if (type.startsWith("BOOLEAN")) {
|
|
912
|
-
formatedAttributes = attributes.map((attr) => {
|
|
913
|
-
if (attr.startsWith("DEFAULT")) {
|
|
914
|
-
return attr.replace(/\b0\b/, "false").replace(/\b1\b/, "true");
|
|
915
|
-
}
|
|
916
|
-
return attr;
|
|
917
|
-
});
|
|
918
|
-
}
|
|
919
|
-
return {
|
|
920
|
-
formatedType,
|
|
921
|
-
formatedAttributes,
|
|
922
|
-
raws
|
|
923
|
-
};
|
|
1107
|
+
version() {
|
|
1108
|
+
return this.format(`SELECT SPLIT_PART(CURRENT_SETTING('server_version'), ' ', 1) AS version`);
|
|
924
1109
|
}
|
|
925
1110
|
}
|
|
926
1111
|
exports.PostgresQueryBuilder = PostgresQueryBuilder;
|