@mikro-orm/sql 7.0.3 → 7.0.4-dev.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/AbstractSqlConnection.d.ts +58 -94
- package/AbstractSqlConnection.js +238 -235
- package/AbstractSqlDriver.d.ts +155 -410
- package/AbstractSqlDriver.js +1941 -2064
- package/AbstractSqlPlatform.d.ts +73 -83
- package/AbstractSqlPlatform.js +158 -162
- package/PivotCollectionPersister.d.ts +15 -33
- package/PivotCollectionPersister.js +160 -158
- package/README.md +1 -1
- package/SqlEntityManager.d.ts +22 -67
- package/SqlEntityManager.js +38 -54
- package/SqlEntityRepository.d.ts +14 -14
- package/SqlEntityRepository.js +23 -23
- package/dialects/mssql/MsSqlNativeQueryBuilder.d.ts +12 -12
- package/dialects/mssql/MsSqlNativeQueryBuilder.js +194 -192
- package/dialects/mysql/BaseMySqlPlatform.d.ts +45 -64
- package/dialects/mysql/BaseMySqlPlatform.js +131 -134
- package/dialects/mysql/MySqlExceptionConverter.d.ts +6 -6
- package/dialects/mysql/MySqlExceptionConverter.js +77 -91
- package/dialects/mysql/MySqlNativeQueryBuilder.d.ts +3 -3
- package/dialects/mysql/MySqlNativeQueryBuilder.js +69 -66
- package/dialects/mysql/MySqlSchemaHelper.d.ts +39 -39
- package/dialects/mysql/MySqlSchemaHelper.js +319 -327
- package/dialects/oracledb/OracleDialect.d.ts +52 -81
- package/dialects/oracledb/OracleDialect.js +149 -155
- package/dialects/oracledb/OracleNativeQueryBuilder.d.ts +12 -12
- package/dialects/oracledb/OracleNativeQueryBuilder.js +236 -232
- package/dialects/postgresql/BasePostgreSqlPlatform.d.ts +105 -108
- package/dialects/postgresql/BasePostgreSqlPlatform.js +350 -351
- package/dialects/postgresql/FullTextType.d.ts +6 -10
- package/dialects/postgresql/FullTextType.js +51 -51
- package/dialects/postgresql/PostgreSqlExceptionConverter.d.ts +5 -5
- package/dialects/postgresql/PostgreSqlExceptionConverter.js +43 -55
- package/dialects/postgresql/PostgreSqlNativeQueryBuilder.d.ts +1 -1
- package/dialects/postgresql/PostgreSqlNativeQueryBuilder.js +4 -4
- package/dialects/postgresql/PostgreSqlSchemaHelper.d.ts +82 -102
- package/dialects/postgresql/PostgreSqlSchemaHelper.js +705 -733
- package/dialects/sqlite/BaseSqliteConnection.d.ts +5 -3
- package/dialects/sqlite/BaseSqliteConnection.js +19 -21
- package/dialects/sqlite/NodeSqliteDialect.d.ts +1 -1
- package/dialects/sqlite/NodeSqliteDialect.js +23 -23
- package/dialects/sqlite/SqliteDriver.d.ts +1 -1
- package/dialects/sqlite/SqliteDriver.js +3 -3
- package/dialects/sqlite/SqliteExceptionConverter.d.ts +6 -6
- package/dialects/sqlite/SqliteExceptionConverter.js +51 -67
- package/dialects/sqlite/SqliteNativeQueryBuilder.d.ts +2 -2
- package/dialects/sqlite/SqliteNativeQueryBuilder.js +7 -7
- package/dialects/sqlite/SqlitePlatform.d.ts +72 -63
- package/dialects/sqlite/SqlitePlatform.js +139 -139
- package/dialects/sqlite/SqliteSchemaHelper.d.ts +60 -70
- package/dialects/sqlite/SqliteSchemaHelper.js +520 -533
- package/package.json +3 -3
- package/plugin/index.d.ts +35 -42
- package/plugin/index.js +36 -43
- package/plugin/transformer.d.ts +94 -117
- package/plugin/transformer.js +881 -890
- package/query/ArrayCriteriaNode.d.ts +4 -4
- package/query/ArrayCriteriaNode.js +18 -18
- package/query/CriteriaNode.d.ts +25 -35
- package/query/CriteriaNode.js +123 -133
- package/query/CriteriaNodeFactory.d.ts +6 -49
- package/query/CriteriaNodeFactory.js +94 -97
- package/query/NativeQueryBuilder.d.ts +118 -118
- package/query/NativeQueryBuilder.js +480 -484
- package/query/ObjectCriteriaNode.d.ts +12 -12
- package/query/ObjectCriteriaNode.js +282 -298
- package/query/QueryBuilder.d.ts +904 -1546
- package/query/QueryBuilder.js +2164 -2294
- package/query/QueryBuilderHelper.d.ts +72 -153
- package/query/QueryBuilderHelper.js +1028 -1079
- package/query/ScalarCriteriaNode.d.ts +3 -3
- package/query/ScalarCriteriaNode.js +46 -53
- package/query/enums.d.ts +14 -14
- package/query/enums.js +14 -14
- package/query/raw.d.ts +6 -16
- package/query/raw.js +10 -10
- package/schema/DatabaseSchema.d.ts +50 -73
- package/schema/DatabaseSchema.js +307 -331
- package/schema/DatabaseTable.d.ts +73 -96
- package/schema/DatabaseTable.js +927 -1012
- package/schema/SchemaComparator.d.ts +66 -70
- package/schema/SchemaComparator.js +740 -766
- package/schema/SchemaHelper.d.ts +95 -109
- package/schema/SchemaHelper.js +659 -675
- package/schema/SqlSchemaGenerator.d.ts +58 -78
- package/schema/SqlSchemaGenerator.js +501 -535
- package/typings.d.ts +266 -380
|
@@ -1,490 +1,486 @@
|
|
|
1
|
-
import { LockMode, raw, RawQueryFragment, Utils } from '@mikro-orm/core';
|
|
1
|
+
import { LockMode, raw, RawQueryFragment, Utils, } from '@mikro-orm/core';
|
|
2
2
|
import { QueryType } from './enums.js';
|
|
3
3
|
/** @internal */
|
|
4
4
|
export class NativeQueryBuilder {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
5
|
+
platform;
|
|
6
|
+
type;
|
|
7
|
+
parts = [];
|
|
8
|
+
params = [];
|
|
9
|
+
options = {};
|
|
10
|
+
constructor(platform) {
|
|
11
|
+
this.platform = platform;
|
|
12
|
+
}
|
|
13
|
+
select(fields) {
|
|
14
|
+
this.type = QueryType.SELECT;
|
|
15
|
+
this.options.select ??= [];
|
|
16
|
+
this.options.select.push(...Utils.asArray(fields));
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
count(fields = '*', distinct) {
|
|
20
|
+
this.type = QueryType.COUNT;
|
|
21
|
+
this.options.select = Utils.asArray(fields);
|
|
22
|
+
this.options.distinct = distinct;
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
into(tableName, options) {
|
|
26
|
+
return this.from(tableName, options);
|
|
27
|
+
}
|
|
28
|
+
from(tableName, options) {
|
|
29
|
+
if (tableName instanceof NativeQueryBuilder) {
|
|
30
|
+
tableName = tableName.toRaw();
|
|
31
|
+
}
|
|
32
|
+
if (typeof tableName === 'string') {
|
|
33
|
+
const asKeyword = this.platform.usesAsKeyword() ? ' as ' : ' ';
|
|
34
|
+
const alias = options?.alias ? `${asKeyword}${this.platform.quoteIdentifier(options.alias)}` : '';
|
|
35
|
+
const schema = options?.schema && options.schema !== this.platform.getDefaultSchemaName() ? `${options.schema}.` : '';
|
|
36
|
+
tableName = this.quote(schema + tableName) + alias;
|
|
37
|
+
}
|
|
38
|
+
this.options.tableName = tableName;
|
|
39
|
+
this.options.indexHint = options?.indexHint;
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
where(sql, params) {
|
|
43
|
+
this.options.where = { sql, params };
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
having(sql, params) {
|
|
47
|
+
this.options.having = { sql, params };
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
groupBy(groupBy) {
|
|
51
|
+
this.options.groupBy = groupBy;
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
join(sql, params) {
|
|
55
|
+
this.options.joins ??= [];
|
|
56
|
+
this.options.joins.push({ sql, params });
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
orderBy(orderBy) {
|
|
60
|
+
this.options.orderBy = orderBy;
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The sub-query is compiled eagerly at call time — later mutations to the
|
|
65
|
+
* sub-query builder will not be reflected in this CTE.
|
|
66
|
+
*/
|
|
67
|
+
with(name, query, options) {
|
|
68
|
+
return this.addCte(name, query, options);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Adds a recursive CTE (`WITH RECURSIVE` on PostgreSQL/MySQL/SQLite, plain `WITH` on MSSQL).
|
|
72
|
+
* The sub-query is compiled eagerly — later mutations will not be reflected.
|
|
73
|
+
*/
|
|
74
|
+
withRecursive(name, query, options) {
|
|
75
|
+
return this.addCte(name, query, options, true);
|
|
76
|
+
}
|
|
77
|
+
addCte(name, query, options, recursive) {
|
|
78
|
+
this.options.ctes ??= [];
|
|
79
|
+
if (this.options.ctes.some(cte => cte.name === name)) {
|
|
80
|
+
throw new Error(`CTE with name '${name}' already exists`);
|
|
81
|
+
}
|
|
82
|
+
const { sql, params } = query instanceof NativeQueryBuilder ? query.compile() : { sql: query.sql, params: [...query.params] };
|
|
83
|
+
this.options.ctes.push({
|
|
84
|
+
name,
|
|
85
|
+
sql,
|
|
86
|
+
params,
|
|
87
|
+
recursive,
|
|
88
|
+
columns: options?.columns,
|
|
89
|
+
materialized: options?.materialized,
|
|
90
|
+
});
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
toString() {
|
|
94
|
+
const { sql, params } = this.compile();
|
|
95
|
+
return this.platform.formatQuery(sql, params);
|
|
96
|
+
}
|
|
97
|
+
compile() {
|
|
98
|
+
if (!this.type) {
|
|
99
|
+
throw new Error('No query type provided');
|
|
100
|
+
}
|
|
101
|
+
this.parts.length = 0;
|
|
102
|
+
this.params.length = 0;
|
|
103
|
+
if (this.options.comment) {
|
|
104
|
+
this.parts.push(...this.options.comment.map(comment => `/* ${comment} */`));
|
|
105
|
+
}
|
|
106
|
+
this.compileCtes();
|
|
107
|
+
switch (this.type) {
|
|
108
|
+
case QueryType.SELECT:
|
|
109
|
+
case QueryType.COUNT:
|
|
110
|
+
this.compileSelect();
|
|
111
|
+
break;
|
|
112
|
+
case QueryType.INSERT:
|
|
113
|
+
this.compileInsert();
|
|
114
|
+
break;
|
|
115
|
+
case QueryType.UPDATE:
|
|
116
|
+
this.compileUpdate();
|
|
117
|
+
break;
|
|
118
|
+
case QueryType.DELETE:
|
|
119
|
+
this.compileDelete();
|
|
120
|
+
break;
|
|
121
|
+
case QueryType.TRUNCATE:
|
|
122
|
+
this.compileTruncate();
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
this.addOnConflictClause();
|
|
126
|
+
if (this.options.returning && this.platform.usesReturningStatement()) {
|
|
127
|
+
const fields = this.options.returning.map(field => this.quote(field));
|
|
128
|
+
this.parts.push(`returning ${fields.join(', ')}`);
|
|
129
|
+
}
|
|
130
|
+
this.addLockClause();
|
|
131
|
+
return this.combineParts();
|
|
132
|
+
}
|
|
133
|
+
addLockClause() {
|
|
134
|
+
if (!this.options.lockMode) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if ([LockMode.PESSIMISTIC_READ, LockMode.PESSIMISTIC_PARTIAL_READ, LockMode.PESSIMISTIC_READ_OR_FAIL].includes(this.options.lockMode)) {
|
|
138
|
+
this.parts.push('for share');
|
|
139
|
+
}
|
|
140
|
+
if ([LockMode.PESSIMISTIC_WRITE, LockMode.PESSIMISTIC_PARTIAL_WRITE, LockMode.PESSIMISTIC_WRITE_OR_FAIL].includes(this.options.lockMode)) {
|
|
141
|
+
this.parts.push('for update');
|
|
142
|
+
}
|
|
143
|
+
if (this.options.lockTables?.length) {
|
|
144
|
+
const fields = this.options.lockTables.map(field => this.quote(field));
|
|
145
|
+
this.parts.push(`of ${fields.join(', ')}`);
|
|
146
|
+
}
|
|
147
|
+
if ([LockMode.PESSIMISTIC_PARTIAL_READ, LockMode.PESSIMISTIC_PARTIAL_WRITE].includes(this.options.lockMode)) {
|
|
148
|
+
this.parts.push('skip locked');
|
|
149
|
+
}
|
|
150
|
+
if ([LockMode.PESSIMISTIC_READ_OR_FAIL, LockMode.PESSIMISTIC_WRITE_OR_FAIL].includes(this.options.lockMode)) {
|
|
151
|
+
this.parts.push('nowait');
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
addOnConflictClause() {
|
|
155
|
+
const clause = this.options.onConflict;
|
|
156
|
+
if (!clause) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
this.parts.push('on conflict');
|
|
160
|
+
if (clause.fields instanceof RawQueryFragment) {
|
|
161
|
+
this.parts.push(clause.fields.sql);
|
|
162
|
+
this.params.push(...clause.fields.params);
|
|
163
|
+
}
|
|
164
|
+
else if (clause.fields.length > 0) {
|
|
165
|
+
const fields = clause.fields.map(field => this.quote(field));
|
|
166
|
+
this.parts.push(`(${fields.join(', ')})`);
|
|
167
|
+
}
|
|
168
|
+
if (clause.ignore) {
|
|
169
|
+
this.parts.push('do nothing');
|
|
170
|
+
}
|
|
171
|
+
if (Utils.isObject(clause.merge)) {
|
|
172
|
+
this.parts.push('do update set');
|
|
173
|
+
const fields = Object.keys(clause.merge).map(field => {
|
|
174
|
+
this.params.push(clause.merge[field]);
|
|
175
|
+
return `${this.quote(field)} = ?`;
|
|
176
|
+
});
|
|
177
|
+
this.parts.push(fields.join(', '));
|
|
178
|
+
}
|
|
179
|
+
else if (clause.merge) {
|
|
180
|
+
this.parts.push('do update set');
|
|
181
|
+
if (clause.merge.length) {
|
|
182
|
+
const fields = clause.merge.map(field => `${this.quote(field)} = excluded.${this.quote(field)}`);
|
|
183
|
+
this.parts.push(fields.join(', '));
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
const dataAsArray = Utils.asArray(this.options.data);
|
|
187
|
+
const keys = Object.keys(dataAsArray[0]);
|
|
188
|
+
const fields = keys.map(field => `${this.quote(field)} = excluded.${this.quote(field)}`);
|
|
189
|
+
this.parts.push(fields.join(', '));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (clause.where) {
|
|
193
|
+
this.parts.push(`where ${clause.where.sql}`);
|
|
194
|
+
this.params.push(...clause.where.params);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
combineParts() {
|
|
198
|
+
let sql = this.parts.join(' ');
|
|
199
|
+
if (this.options.wrap) {
|
|
200
|
+
const [a, b] = this.options.wrap;
|
|
201
|
+
sql = `${a}${sql}${b}`;
|
|
202
|
+
}
|
|
203
|
+
return { sql, params: this.params };
|
|
204
|
+
}
|
|
205
|
+
limit(limit) {
|
|
206
|
+
this.options.limit = limit;
|
|
207
|
+
return this;
|
|
208
|
+
}
|
|
209
|
+
offset(offset) {
|
|
210
|
+
this.options.offset = offset;
|
|
211
|
+
return this;
|
|
212
|
+
}
|
|
213
|
+
insert(data) {
|
|
214
|
+
this.type = QueryType.INSERT;
|
|
215
|
+
this.options.data = data;
|
|
216
|
+
return this;
|
|
217
|
+
}
|
|
218
|
+
update(data) {
|
|
219
|
+
this.type = QueryType.UPDATE;
|
|
220
|
+
this.options.data ??= {};
|
|
221
|
+
Object.assign(this.options.data, data);
|
|
222
|
+
return this;
|
|
223
|
+
}
|
|
224
|
+
delete() {
|
|
225
|
+
this.type = QueryType.DELETE;
|
|
226
|
+
return this;
|
|
227
|
+
}
|
|
228
|
+
truncate() {
|
|
229
|
+
this.type = QueryType.TRUNCATE;
|
|
230
|
+
return this;
|
|
231
|
+
}
|
|
232
|
+
distinct() {
|
|
233
|
+
this.options.distinct = true;
|
|
234
|
+
return this;
|
|
235
|
+
}
|
|
236
|
+
distinctOn(fields) {
|
|
237
|
+
this.options.distinctOn = fields;
|
|
238
|
+
return this;
|
|
239
|
+
}
|
|
240
|
+
onConflict(options) {
|
|
241
|
+
this.options.onConflict = options;
|
|
242
|
+
return options;
|
|
243
|
+
}
|
|
244
|
+
returning(fields) {
|
|
245
|
+
this.options.returning = fields;
|
|
246
|
+
return this;
|
|
247
|
+
}
|
|
248
|
+
lockMode(lockMode, lockTables) {
|
|
249
|
+
this.options.lockMode = lockMode;
|
|
250
|
+
this.options.lockTables = lockTables;
|
|
251
|
+
return this;
|
|
252
|
+
}
|
|
253
|
+
comment(comment) {
|
|
254
|
+
this.options.comment ??= [];
|
|
255
|
+
this.options.comment.push(...Utils.asArray(comment));
|
|
256
|
+
return this;
|
|
257
|
+
}
|
|
258
|
+
hintComment(comment) {
|
|
259
|
+
this.options.hintComment ??= [];
|
|
260
|
+
this.options.hintComment.push(...Utils.asArray(comment));
|
|
261
|
+
return this;
|
|
262
|
+
}
|
|
263
|
+
setFlags(flags) {
|
|
264
|
+
this.options.flags = flags;
|
|
265
|
+
return this;
|
|
266
|
+
}
|
|
267
|
+
clear(clause) {
|
|
268
|
+
delete this.options[clause];
|
|
269
|
+
return this;
|
|
270
|
+
}
|
|
271
|
+
wrap(prefix, suffix) {
|
|
272
|
+
this.options.wrap = [prefix, suffix];
|
|
273
|
+
return this;
|
|
274
|
+
}
|
|
275
|
+
as(alias) {
|
|
276
|
+
this.wrap('(', `) as ${this.platform.quoteIdentifier(alias)}`);
|
|
277
|
+
return this;
|
|
278
|
+
}
|
|
279
|
+
toRaw() {
|
|
280
|
+
const { sql, params } = this.compile();
|
|
281
|
+
return raw(sql, params);
|
|
282
|
+
}
|
|
283
|
+
compileSelect() {
|
|
284
|
+
this.parts.push('select');
|
|
285
|
+
this.addHintComment();
|
|
286
|
+
this.parts.push(`${this.getFields()} from ${this.getTableName()}`);
|
|
287
|
+
if (this.options.joins) {
|
|
288
|
+
for (const join of this.options.joins) {
|
|
289
|
+
this.parts.push(join.sql);
|
|
290
|
+
this.params.push(...join.params);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if (this.options.where?.sql.trim()) {
|
|
294
|
+
this.parts.push(`where ${this.options.where.sql}`);
|
|
295
|
+
this.options.where.params.forEach(p => this.params.push(p));
|
|
296
|
+
}
|
|
297
|
+
if (this.options.groupBy) {
|
|
298
|
+
const fields = this.options.groupBy.map(field => this.quote(field));
|
|
299
|
+
this.parts.push(`group by ${fields.join(', ')}`);
|
|
300
|
+
}
|
|
301
|
+
if (this.options.having) {
|
|
302
|
+
this.parts.push(`having ${this.options.having.sql}`);
|
|
303
|
+
this.params.push(...this.options.having.params);
|
|
304
|
+
}
|
|
305
|
+
if (this.options.orderBy) {
|
|
306
|
+
this.parts.push(`order by ${this.options.orderBy}`);
|
|
307
|
+
}
|
|
308
|
+
if (this.options.limit != null) {
|
|
309
|
+
this.parts.push(`limit ?`);
|
|
310
|
+
this.params.push(this.options.limit);
|
|
311
|
+
}
|
|
312
|
+
if (this.options.offset != null) {
|
|
313
|
+
this.parts.push(`offset ?`);
|
|
314
|
+
this.params.push(this.options.offset);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
getFields() {
|
|
318
|
+
if (!this.options.select || this.options.select.length === 0) {
|
|
319
|
+
throw new Error('No fields selected');
|
|
320
|
+
}
|
|
321
|
+
let fields = this.options.select.map(field => this.quote(field)).join(', ');
|
|
322
|
+
if (this.options.distinct) {
|
|
323
|
+
fields = `distinct ${fields}`;
|
|
324
|
+
}
|
|
325
|
+
else if (this.options.distinctOn) {
|
|
326
|
+
fields = `distinct on (${this.options.distinctOn.map(field => this.quote(field)).join(', ')}) ${fields}`;
|
|
327
|
+
}
|
|
328
|
+
if (this.type === QueryType.COUNT) {
|
|
329
|
+
fields = `count(${fields}) as ${this.quote('count')}`;
|
|
330
|
+
}
|
|
331
|
+
return fields;
|
|
332
|
+
}
|
|
333
|
+
compileInsert() {
|
|
334
|
+
if (!this.options.data) {
|
|
335
|
+
throw new Error('No data provided');
|
|
336
|
+
}
|
|
337
|
+
this.parts.push('insert');
|
|
338
|
+
this.addHintComment();
|
|
339
|
+
this.parts.push(`into ${this.getTableName()}`);
|
|
340
|
+
if (Object.keys(this.options.data).length === 0) {
|
|
341
|
+
this.addOutputClause('inserted');
|
|
342
|
+
this.parts.push('default values');
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
const parts = this.processInsertData();
|
|
346
|
+
this.parts.push(parts.join(', '));
|
|
347
|
+
}
|
|
348
|
+
addOutputClause(type) {
|
|
349
|
+
if (this.options.returning && this.platform.usesOutputStatement()) {
|
|
350
|
+
const fields = this.options.returning.map(field => `${type}.${this.quote(field)}`);
|
|
351
|
+
this.parts.push(`output ${fields.join(', ')}`);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
processInsertData() {
|
|
193
355
|
const dataAsArray = Utils.asArray(this.options.data);
|
|
194
356
|
const keys = Object.keys(dataAsArray[0]);
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
getFields() {
|
|
325
|
-
if (!this.options.select || this.options.select.length === 0) {
|
|
326
|
-
throw new Error('No fields selected');
|
|
327
|
-
}
|
|
328
|
-
let fields = this.options.select.map(field => this.quote(field)).join(', ');
|
|
329
|
-
if (this.options.distinct) {
|
|
330
|
-
fields = `distinct ${fields}`;
|
|
331
|
-
} else if (this.options.distinctOn) {
|
|
332
|
-
fields = `distinct on (${this.options.distinctOn.map(field => this.quote(field)).join(', ')}) ${fields}`;
|
|
333
|
-
}
|
|
334
|
-
if (this.type === QueryType.COUNT) {
|
|
335
|
-
fields = `count(${fields}) as ${this.quote('count')}`;
|
|
336
|
-
}
|
|
337
|
-
return fields;
|
|
338
|
-
}
|
|
339
|
-
compileInsert() {
|
|
340
|
-
if (!this.options.data) {
|
|
341
|
-
throw new Error('No data provided');
|
|
342
|
-
}
|
|
343
|
-
this.parts.push('insert');
|
|
344
|
-
this.addHintComment();
|
|
345
|
-
this.parts.push(`into ${this.getTableName()}`);
|
|
346
|
-
if (Object.keys(this.options.data).length === 0) {
|
|
347
|
-
this.addOutputClause('inserted');
|
|
348
|
-
this.parts.push('default values');
|
|
349
|
-
return;
|
|
350
|
-
}
|
|
351
|
-
const parts = this.processInsertData();
|
|
352
|
-
this.parts.push(parts.join(', '));
|
|
353
|
-
}
|
|
354
|
-
addOutputClause(type) {
|
|
355
|
-
if (this.options.returning && this.platform.usesOutputStatement()) {
|
|
356
|
-
const fields = this.options.returning.map(field => `${type}.${this.quote(field)}`);
|
|
357
|
-
this.parts.push(`output ${fields.join(', ')}`);
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
processInsertData() {
|
|
361
|
-
const dataAsArray = Utils.asArray(this.options.data);
|
|
362
|
-
const keys = Object.keys(dataAsArray[0]);
|
|
363
|
-
const values = keys.map(() => '?');
|
|
364
|
-
const parts = [];
|
|
365
|
-
this.parts.push(`(${keys.map(key => this.quote(key)).join(', ')})`);
|
|
366
|
-
this.addOutputClause('inserted');
|
|
367
|
-
this.parts.push('values');
|
|
368
|
-
for (const data of dataAsArray) {
|
|
369
|
-
for (const key of keys) {
|
|
370
|
-
if (typeof data[key] === 'undefined') {
|
|
371
|
-
this.params.push(this.platform.usesDefaultKeyword() ? raw('default') : null);
|
|
372
|
-
} else {
|
|
373
|
-
this.params.push(data[key]);
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
parts.push(`(${values.join(', ')})`);
|
|
377
|
-
}
|
|
378
|
-
return parts;
|
|
379
|
-
}
|
|
380
|
-
compileUpdate() {
|
|
381
|
-
if (!this.options.data || Object.keys(this.options.data).length === 0) {
|
|
382
|
-
throw new Error('No data provided');
|
|
383
|
-
}
|
|
384
|
-
this.parts.push('update');
|
|
385
|
-
this.addHintComment();
|
|
386
|
-
this.parts.push(this.getTableName());
|
|
387
|
-
if (this.options.joins) {
|
|
388
|
-
for (const join of this.options.joins) {
|
|
389
|
-
this.parts.push(join.sql);
|
|
390
|
-
this.params.push(...join.params);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
this.parts.push('set');
|
|
394
|
-
if (this.options.data) {
|
|
395
|
-
const parts = [];
|
|
396
|
-
for (const key of Object.keys(this.options.data)) {
|
|
397
|
-
parts.push(`${this.quote(key)} = ?`);
|
|
398
|
-
this.params.push(this.options.data[key]);
|
|
399
|
-
}
|
|
400
|
-
this.parts.push(parts.join(', '));
|
|
401
|
-
}
|
|
402
|
-
this.addOutputClause('inserted');
|
|
403
|
-
if (this.options.where?.sql.trim()) {
|
|
404
|
-
this.parts.push(`where ${this.options.where.sql}`);
|
|
405
|
-
this.params.push(...this.options.where.params);
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
compileDelete() {
|
|
409
|
-
this.parts.push('delete');
|
|
410
|
-
this.addHintComment();
|
|
411
|
-
this.parts.push(`from ${this.getTableName()}`);
|
|
412
|
-
this.addOutputClause('deleted');
|
|
413
|
-
if (this.options.where?.sql.trim()) {
|
|
414
|
-
this.parts.push(`where ${this.options.where.sql}`);
|
|
415
|
-
this.params.push(...this.options.where.params);
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
compileTruncate() {
|
|
419
|
-
const sql = `truncate table ${this.getTableName()}`;
|
|
420
|
-
this.parts.push(sql);
|
|
421
|
-
}
|
|
422
|
-
addHintComment() {
|
|
423
|
-
if (this.options.hintComment) {
|
|
424
|
-
this.parts.push(`/*+ ${this.options.hintComment.join(' ')} */`);
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
compileCtes() {
|
|
428
|
-
const ctes = this.options.ctes;
|
|
429
|
-
if (!ctes || ctes.length === 0) {
|
|
430
|
-
return;
|
|
431
|
-
}
|
|
432
|
-
const hasRecursive = ctes.some(cte => cte.recursive);
|
|
433
|
-
const keyword = this.getCteKeyword(hasRecursive);
|
|
434
|
-
const cteParts = [];
|
|
435
|
-
for (const cte of ctes) {
|
|
436
|
-
let part = this.quote(cte.name);
|
|
437
|
-
if (cte.columns?.length) {
|
|
438
|
-
part += ` (${cte.columns.map(c => this.quote(c)).join(', ')})`;
|
|
439
|
-
}
|
|
440
|
-
part += ' as';
|
|
441
|
-
if (cte.materialized === true) {
|
|
442
|
-
part += ' materialized';
|
|
443
|
-
} else if (cte.materialized === false) {
|
|
444
|
-
part += ' not materialized';
|
|
445
|
-
}
|
|
446
|
-
part += ` (${cte.sql})`;
|
|
447
|
-
this.params.push(...cte.params);
|
|
448
|
-
cteParts.push(part);
|
|
449
|
-
}
|
|
450
|
-
this.parts.push(`${keyword} ${cteParts.join(', ')}`);
|
|
451
|
-
}
|
|
452
|
-
getCteKeyword(hasRecursive) {
|
|
453
|
-
return hasRecursive ? 'with recursive' : 'with';
|
|
454
|
-
}
|
|
455
|
-
getTableName() {
|
|
456
|
-
if (!this.options.tableName) {
|
|
457
|
-
throw new Error('No table name provided');
|
|
458
|
-
}
|
|
459
|
-
const indexHint = this.options.indexHint ? ' ' + this.options.indexHint : '';
|
|
460
|
-
if (this.options.tableName instanceof RawQueryFragment) {
|
|
461
|
-
this.params.push(...this.options.tableName.params);
|
|
462
|
-
return this.options.tableName.sql + indexHint;
|
|
463
|
-
}
|
|
464
|
-
return this.options.tableName + indexHint;
|
|
465
|
-
}
|
|
466
|
-
quote(id) {
|
|
467
|
-
if (id instanceof RawQueryFragment) {
|
|
468
|
-
return this.platform.formatQuery(id.sql, id.params);
|
|
469
|
-
}
|
|
470
|
-
if (id instanceof NativeQueryBuilder) {
|
|
471
|
-
const { sql, params } = id.compile();
|
|
472
|
-
return this.platform.formatQuery(sql, params);
|
|
473
|
-
}
|
|
474
|
-
if (id.endsWith('.*')) {
|
|
475
|
-
const schema = this.platform.quoteIdentifier(id.substring(0, id.indexOf('.')));
|
|
476
|
-
return schema + '.*';
|
|
477
|
-
}
|
|
478
|
-
if (id.toLowerCase().includes(' as ')) {
|
|
479
|
-
const parts = id.split(/ as /i);
|
|
480
|
-
const a = this.platform.quoteIdentifier(parts[0]);
|
|
481
|
-
const b = this.platform.quoteIdentifier(parts[1]);
|
|
482
|
-
const asKeyword = this.platform.usesAsKeyword() ? ' as ' : ' ';
|
|
483
|
-
return `${a}${asKeyword}${b}`;
|
|
484
|
-
}
|
|
485
|
-
if (id === '*') {
|
|
486
|
-
return id;
|
|
487
|
-
}
|
|
488
|
-
return this.platform.quoteIdentifier(id);
|
|
489
|
-
}
|
|
357
|
+
const values = keys.map(() => '?');
|
|
358
|
+
const parts = [];
|
|
359
|
+
this.parts.push(`(${keys.map(key => this.quote(key)).join(', ')})`);
|
|
360
|
+
this.addOutputClause('inserted');
|
|
361
|
+
this.parts.push('values');
|
|
362
|
+
for (const data of dataAsArray) {
|
|
363
|
+
for (const key of keys) {
|
|
364
|
+
if (typeof data[key] === 'undefined') {
|
|
365
|
+
this.params.push(this.platform.usesDefaultKeyword() ? raw('default') : null);
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
this.params.push(data[key]);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
parts.push(`(${values.join(', ')})`);
|
|
372
|
+
}
|
|
373
|
+
return parts;
|
|
374
|
+
}
|
|
375
|
+
compileUpdate() {
|
|
376
|
+
if (!this.options.data || Object.keys(this.options.data).length === 0) {
|
|
377
|
+
throw new Error('No data provided');
|
|
378
|
+
}
|
|
379
|
+
this.parts.push('update');
|
|
380
|
+
this.addHintComment();
|
|
381
|
+
this.parts.push(this.getTableName());
|
|
382
|
+
if (this.options.joins) {
|
|
383
|
+
for (const join of this.options.joins) {
|
|
384
|
+
this.parts.push(join.sql);
|
|
385
|
+
this.params.push(...join.params);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
this.parts.push('set');
|
|
389
|
+
if (this.options.data) {
|
|
390
|
+
const parts = [];
|
|
391
|
+
for (const key of Object.keys(this.options.data)) {
|
|
392
|
+
parts.push(`${this.quote(key)} = ?`);
|
|
393
|
+
this.params.push(this.options.data[key]);
|
|
394
|
+
}
|
|
395
|
+
this.parts.push(parts.join(', '));
|
|
396
|
+
}
|
|
397
|
+
this.addOutputClause('inserted');
|
|
398
|
+
if (this.options.where?.sql.trim()) {
|
|
399
|
+
this.parts.push(`where ${this.options.where.sql}`);
|
|
400
|
+
this.params.push(...this.options.where.params);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
compileDelete() {
|
|
404
|
+
this.parts.push('delete');
|
|
405
|
+
this.addHintComment();
|
|
406
|
+
this.parts.push(`from ${this.getTableName()}`);
|
|
407
|
+
this.addOutputClause('deleted');
|
|
408
|
+
if (this.options.where?.sql.trim()) {
|
|
409
|
+
this.parts.push(`where ${this.options.where.sql}`);
|
|
410
|
+
this.params.push(...this.options.where.params);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
compileTruncate() {
|
|
414
|
+
const sql = `truncate table ${this.getTableName()}`;
|
|
415
|
+
this.parts.push(sql);
|
|
416
|
+
}
|
|
417
|
+
addHintComment() {
|
|
418
|
+
if (this.options.hintComment) {
|
|
419
|
+
this.parts.push(`/*+ ${this.options.hintComment.join(' ')} */`);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
compileCtes() {
|
|
423
|
+
const ctes = this.options.ctes;
|
|
424
|
+
if (!ctes || ctes.length === 0) {
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
const hasRecursive = ctes.some(cte => cte.recursive);
|
|
428
|
+
const keyword = this.getCteKeyword(hasRecursive);
|
|
429
|
+
const cteParts = [];
|
|
430
|
+
for (const cte of ctes) {
|
|
431
|
+
let part = this.quote(cte.name);
|
|
432
|
+
if (cte.columns?.length) {
|
|
433
|
+
part += ` (${cte.columns.map(c => this.quote(c)).join(', ')})`;
|
|
434
|
+
}
|
|
435
|
+
part += ' as';
|
|
436
|
+
if (cte.materialized === true) {
|
|
437
|
+
part += ' materialized';
|
|
438
|
+
}
|
|
439
|
+
else if (cte.materialized === false) {
|
|
440
|
+
part += ' not materialized';
|
|
441
|
+
}
|
|
442
|
+
part += ` (${cte.sql})`;
|
|
443
|
+
this.params.push(...cte.params);
|
|
444
|
+
cteParts.push(part);
|
|
445
|
+
}
|
|
446
|
+
this.parts.push(`${keyword} ${cteParts.join(', ')}`);
|
|
447
|
+
}
|
|
448
|
+
getCteKeyword(hasRecursive) {
|
|
449
|
+
return hasRecursive ? 'with recursive' : 'with';
|
|
450
|
+
}
|
|
451
|
+
getTableName() {
|
|
452
|
+
if (!this.options.tableName) {
|
|
453
|
+
throw new Error('No table name provided');
|
|
454
|
+
}
|
|
455
|
+
const indexHint = this.options.indexHint ? ' ' + this.options.indexHint : '';
|
|
456
|
+
if (this.options.tableName instanceof RawQueryFragment) {
|
|
457
|
+
this.params.push(...this.options.tableName.params);
|
|
458
|
+
return this.options.tableName.sql + indexHint;
|
|
459
|
+
}
|
|
460
|
+
return this.options.tableName + indexHint;
|
|
461
|
+
}
|
|
462
|
+
quote(id) {
|
|
463
|
+
if (id instanceof RawQueryFragment) {
|
|
464
|
+
return this.platform.formatQuery(id.sql, id.params);
|
|
465
|
+
}
|
|
466
|
+
if (id instanceof NativeQueryBuilder) {
|
|
467
|
+
const { sql, params } = id.compile();
|
|
468
|
+
return this.platform.formatQuery(sql, params);
|
|
469
|
+
}
|
|
470
|
+
if (id.endsWith('.*')) {
|
|
471
|
+
const schema = this.platform.quoteIdentifier(id.substring(0, id.indexOf('.')));
|
|
472
|
+
return schema + '.*';
|
|
473
|
+
}
|
|
474
|
+
if (id.toLowerCase().includes(' as ')) {
|
|
475
|
+
const parts = id.split(/ as /i);
|
|
476
|
+
const a = this.platform.quoteIdentifier(parts[0]);
|
|
477
|
+
const b = this.platform.quoteIdentifier(parts[1]);
|
|
478
|
+
const asKeyword = this.platform.usesAsKeyword() ? ' as ' : ' ';
|
|
479
|
+
return `${a}${asKeyword}${b}`;
|
|
480
|
+
}
|
|
481
|
+
if (id === '*') {
|
|
482
|
+
return id;
|
|
483
|
+
}
|
|
484
|
+
return this.platform.quoteIdentifier(id);
|
|
485
|
+
}
|
|
490
486
|
}
|