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