azure-mock 2.20.0 → 2.22.0
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/LICENSE +201 -201
- package/dist/better-sqlite3-CtLaF97z-DWR9uC3b.js +129 -0
- package/dist/chunk-2X2C5D2I-dJHtPFe9-DxsO-1YF.js +4026 -0
- package/dist/d1-pK2J-_yx-BoQTXsTa.js +185 -0
- package/dist/dialect-pBb6Cg_F-xgpUtfmk.js +570 -0
- package/dist/dist-C481edUb-CHbOkeXH.js +10131 -0
- package/dist/dist-DUzzleZr-DxHmeYjp.js +2 -0
- package/dist/index.d.ts +21 -21
- package/dist/index.js +20396 -18921
- package/dist/libsql-Nq-S0r5x-d2dAzhgO.js +240 -0
- package/dist/logger-TIsK7375-CLNNa25D.js +652 -0
- package/dist/migrator-B9kYwLqO-4g9JX3_v.js +8 -0
- package/dist/migrator-BfTfJctM-DrNlb1BS.js +8 -0
- package/dist/migrator-BjoW0_3j-CD-XdZ5a.js +8 -0
- package/dist/migrator-CWWlo_Jk-C4Hw4PPN.js +8 -0
- package/dist/migrator-CpiXRkls-COYfhljd.js +24 -0
- package/dist/migrator-CtqJkLVI-D1lU6ZV0.js +8 -0
- package/dist/migrator-CxcBjmET-Dcd21VIf.js +8 -0
- package/dist/migrator-Cyhbhxo_-Bh7sF0R3.js +21 -0
- package/dist/migrator-DG1mWUoR-DVfkqn_v.js +31 -0
- package/dist/migrator-DrWmHwY2-YjAfotd7.js +8 -0
- package/dist/migrator-WAPonQxA-CRvJJhKG.js +8 -0
- package/dist/migrator-Zdh8WMMx-gsmNXspd.js +8 -0
- package/dist/migrator-xRJ6NOTS-SsAuB-Gi.js +24 -0
- package/dist/migrator-xbWwvFYF-BjyVdQS8.js +8 -0
- package/dist/mysql2-DkoPEsRu-BMwVSCmY.js +251 -0
- package/dist/neon-serverless-BEVFA7yv-DGSPAG1z.js +205 -0
- package/dist/node-postgres-DQA7bEhW-CkM_cVLD.js +216 -0
- package/dist/nodefs-Bc8b83o_-DetP9qUa.js +24 -0
- package/dist/opfs-ahp-DbstDvx--CHskKtzG.js +365 -0
- package/dist/pg-ylxXyvKj-Hm8vcZoi.js +279 -0
- package/dist/pglite-CPs4w-D9-DHOg0D8W.js +179 -0
- package/dist/pglite-DMWgTUE6-BOIRUWUT.js +2 -0
- package/dist/planetscale-serverless-DUMheN-f-Dyc_W6V_.js +172 -0
- package/dist/query-builder-CLJAKedv-DTZiP7B6.js +1715 -0
- package/dist/query-builder-CT3_liD0-hhg5kRTk.js +1347 -0
- package/dist/session-BOEirggu-DTmpyU_x.js +2485 -0
- package/dist/session-CAUQtT0A-BoVK2x7A.js +745 -0
- package/dist/session-Cjeygn2Z-BO0mi6pq.js +989 -0
- package/dist/singlestore-Cdlo23hW-BuFJ4Zqb.js +1647 -0
- package/dist/sql-CNZp2yLp-Bwugq384.js +611 -0
- package/dist/sqlite-proxy-BgUfVEbZ-CsSkc-_K.js +190 -0
- package/dist/src-LcyXhCXE-C_vKJiLK.js +1920 -0
- package/dist/vercel-postgres-BYmFKsTS-CpV2usun.js +203 -0
- package/package.json +7 -8
|
@@ -0,0 +1,1715 @@
|
|
|
1
|
+
import { C as sql, b as is, d as Table, f as TableName, g as entityKind, h as WithSubquery, m as ViewBaseConfig, p as View, r as Param, s as SQL, t as Column, u as Subquery, v as getTableName, y as getTableUniqueName } from "./sql-CNZp2yLp-Bwugq384.js";
|
|
2
|
+
import { A as mapColumnsInSQLToAlias, C as getOrderByOperators, D as haveSameKeys, F as orderSelectedFields, P as normalizeRelation, S as getOperators, T as getTableLikeName, _ as and, a as Many, b as eq, c as One, f as SelectionProxyHandler, g as aliasedTableColumn, h as aliasedTable, k as mapColumnsInAliasedSQLToAlias, m as TypedQueryBuilder, r as DrizzleError, t as CasingCache, u as QueryPromise, v as applyMixins, w as getTableColumns } from "./logger-TIsK7375-CLNNa25D.js";
|
|
3
|
+
//#region ../db/dist/query-builder-CLJAKedv.js
|
|
4
|
+
var CheckBuilder = class {
|
|
5
|
+
constructor(name, value) {
|
|
6
|
+
this.name = name;
|
|
7
|
+
this.value = value;
|
|
8
|
+
}
|
|
9
|
+
static [entityKind] = "MySqlCheckBuilder";
|
|
10
|
+
brand;
|
|
11
|
+
/** @internal */
|
|
12
|
+
build(table) {
|
|
13
|
+
return new Check(table, this);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
var Check = class {
|
|
17
|
+
constructor(table, builder) {
|
|
18
|
+
this.table = table;
|
|
19
|
+
this.name = builder.name;
|
|
20
|
+
this.value = builder.value;
|
|
21
|
+
}
|
|
22
|
+
static [entityKind] = "MySqlCheck";
|
|
23
|
+
name;
|
|
24
|
+
value;
|
|
25
|
+
};
|
|
26
|
+
var ForeignKeyBuilder = class {
|
|
27
|
+
static [entityKind] = "MySqlForeignKeyBuilder";
|
|
28
|
+
/** @internal */
|
|
29
|
+
reference;
|
|
30
|
+
/** @internal */
|
|
31
|
+
_onUpdate;
|
|
32
|
+
/** @internal */
|
|
33
|
+
_onDelete;
|
|
34
|
+
constructor(config, actions) {
|
|
35
|
+
this.reference = () => {
|
|
36
|
+
const { name, columns, foreignColumns } = config();
|
|
37
|
+
return {
|
|
38
|
+
name,
|
|
39
|
+
columns,
|
|
40
|
+
foreignTable: foreignColumns[0].table,
|
|
41
|
+
foreignColumns
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
if (actions) {
|
|
45
|
+
this._onUpdate = actions.onUpdate;
|
|
46
|
+
this._onDelete = actions.onDelete;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
onUpdate(action) {
|
|
50
|
+
this._onUpdate = action;
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
onDelete(action) {
|
|
54
|
+
this._onDelete = action;
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
/** @internal */
|
|
58
|
+
build(table) {
|
|
59
|
+
return new ForeignKey(table, this);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
var ForeignKey = class {
|
|
63
|
+
constructor(table, builder) {
|
|
64
|
+
this.table = table;
|
|
65
|
+
this.reference = builder.reference;
|
|
66
|
+
this.onUpdate = builder._onUpdate;
|
|
67
|
+
this.onDelete = builder._onDelete;
|
|
68
|
+
}
|
|
69
|
+
static [entityKind] = "MySqlForeignKey";
|
|
70
|
+
reference;
|
|
71
|
+
onUpdate;
|
|
72
|
+
onDelete;
|
|
73
|
+
getName() {
|
|
74
|
+
const { name, columns, foreignColumns } = this.reference();
|
|
75
|
+
const columnNames = columns.map((column) => column.name);
|
|
76
|
+
const foreignColumnNames = foreignColumns.map((column) => column.name);
|
|
77
|
+
const chunks = [
|
|
78
|
+
this.table[TableName],
|
|
79
|
+
...columnNames,
|
|
80
|
+
foreignColumns[0].table[TableName],
|
|
81
|
+
...foreignColumnNames
|
|
82
|
+
];
|
|
83
|
+
return name ?? `${chunks.join("_")}_fk`;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
function uniqueKeyName(table, columns) {
|
|
87
|
+
return `${table[TableName]}_${columns.join("_")}_unique`;
|
|
88
|
+
}
|
|
89
|
+
var UniqueConstraintBuilder = class {
|
|
90
|
+
constructor(columns, name) {
|
|
91
|
+
this.name = name;
|
|
92
|
+
this.columns = columns;
|
|
93
|
+
}
|
|
94
|
+
static [entityKind] = "MySqlUniqueConstraintBuilder";
|
|
95
|
+
/** @internal */
|
|
96
|
+
columns;
|
|
97
|
+
/** @internal */
|
|
98
|
+
build(table) {
|
|
99
|
+
return new UniqueConstraint(table, this.columns, this.name);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
var UniqueConstraint = class {
|
|
103
|
+
constructor(table, columns, name) {
|
|
104
|
+
this.table = table;
|
|
105
|
+
this.columns = columns;
|
|
106
|
+
this.name = name ?? uniqueKeyName(this.table, this.columns.map((column) => column.name));
|
|
107
|
+
}
|
|
108
|
+
static [entityKind] = "MySqlUniqueConstraint";
|
|
109
|
+
columns;
|
|
110
|
+
name;
|
|
111
|
+
nullsNotDistinct = false;
|
|
112
|
+
getName() {
|
|
113
|
+
return this.name;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
var MySqlColumn = class extends Column {
|
|
117
|
+
constructor(table, config) {
|
|
118
|
+
if (!config.uniqueName) config.uniqueName = uniqueKeyName(table, [config.name]);
|
|
119
|
+
super(table, config);
|
|
120
|
+
this.table = table;
|
|
121
|
+
}
|
|
122
|
+
static [entityKind] = "MySqlColumn";
|
|
123
|
+
};
|
|
124
|
+
var IndexBuilder = class {
|
|
125
|
+
static [entityKind] = "MySqlIndexBuilder";
|
|
126
|
+
/** @internal */
|
|
127
|
+
config;
|
|
128
|
+
constructor(name, columns, unique) {
|
|
129
|
+
this.config = {
|
|
130
|
+
name,
|
|
131
|
+
columns,
|
|
132
|
+
unique
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
using(using) {
|
|
136
|
+
this.config.using = using;
|
|
137
|
+
return this;
|
|
138
|
+
}
|
|
139
|
+
algorithm(algorithm) {
|
|
140
|
+
this.config.algorithm = algorithm;
|
|
141
|
+
return this;
|
|
142
|
+
}
|
|
143
|
+
lock(lock) {
|
|
144
|
+
this.config.lock = lock;
|
|
145
|
+
return this;
|
|
146
|
+
}
|
|
147
|
+
/** @internal */
|
|
148
|
+
build(table) {
|
|
149
|
+
return new Index(this.config, table);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
var Index = class {
|
|
153
|
+
static [entityKind] = "MySqlIndex";
|
|
154
|
+
config;
|
|
155
|
+
constructor(config, table) {
|
|
156
|
+
this.config = {
|
|
157
|
+
...config,
|
|
158
|
+
table
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
const InlineForeignKeys = Symbol.for("drizzle:MySqlInlineForeignKeys");
|
|
163
|
+
var MySqlTable = class extends Table {
|
|
164
|
+
static [entityKind] = "MySqlTable";
|
|
165
|
+
/** @internal */
|
|
166
|
+
static Symbol = Object.assign({}, Table.Symbol, { InlineForeignKeys });
|
|
167
|
+
/** @internal */
|
|
168
|
+
[Table.Symbol.Columns];
|
|
169
|
+
/** @internal */
|
|
170
|
+
[InlineForeignKeys] = [];
|
|
171
|
+
/** @internal */
|
|
172
|
+
[Table.Symbol.ExtraConfigBuilder] = void 0;
|
|
173
|
+
};
|
|
174
|
+
var PrimaryKeyBuilder = class {
|
|
175
|
+
static [entityKind] = "MySqlPrimaryKeyBuilder";
|
|
176
|
+
/** @internal */
|
|
177
|
+
columns;
|
|
178
|
+
/** @internal */
|
|
179
|
+
name;
|
|
180
|
+
constructor(columns, name) {
|
|
181
|
+
this.columns = columns;
|
|
182
|
+
this.name = name;
|
|
183
|
+
}
|
|
184
|
+
/** @internal */
|
|
185
|
+
build(table) {
|
|
186
|
+
return new PrimaryKey(table, this.columns, this.name);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
var PrimaryKey = class {
|
|
190
|
+
constructor(table, columns, name) {
|
|
191
|
+
this.table = table;
|
|
192
|
+
this.columns = columns;
|
|
193
|
+
this.name = name;
|
|
194
|
+
}
|
|
195
|
+
static [entityKind] = "MySqlPrimaryKey";
|
|
196
|
+
columns;
|
|
197
|
+
name;
|
|
198
|
+
getName() {
|
|
199
|
+
return this.name ?? `${this.table[MySqlTable.Symbol.Name]}_${this.columns.map((column) => column.name).join("_")}_pk`;
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
const MySqlViewConfig = Symbol.for("drizzle:MySqlViewConfig");
|
|
203
|
+
function extractUsedTable(table) {
|
|
204
|
+
if (is(table, MySqlTable)) return [`${table[Table.Symbol.BaseName]}`];
|
|
205
|
+
if (is(table, Subquery)) return table._.usedTables ?? [];
|
|
206
|
+
if (is(table, SQL)) return table.usedTables ?? [];
|
|
207
|
+
return [];
|
|
208
|
+
}
|
|
209
|
+
function getTableConfig(table) {
|
|
210
|
+
const columns = Object.values(table[MySqlTable.Symbol.Columns]);
|
|
211
|
+
const indexes = [];
|
|
212
|
+
const checks = [];
|
|
213
|
+
const primaryKeys = [];
|
|
214
|
+
const uniqueConstraints = [];
|
|
215
|
+
const foreignKeys = Object.values(table[MySqlTable.Symbol.InlineForeignKeys]);
|
|
216
|
+
const name = table[Table.Symbol.Name];
|
|
217
|
+
const schema = table[Table.Symbol.Schema];
|
|
218
|
+
const baseName = table[Table.Symbol.BaseName];
|
|
219
|
+
const extraConfigBuilder = table[MySqlTable.Symbol.ExtraConfigBuilder];
|
|
220
|
+
if (extraConfigBuilder !== void 0) {
|
|
221
|
+
const extraConfig = extraConfigBuilder(table[MySqlTable.Symbol.Columns]);
|
|
222
|
+
const extraValues = Array.isArray(extraConfig) ? extraConfig.flat(1) : Object.values(extraConfig);
|
|
223
|
+
for (const builder of Object.values(extraValues)) if (is(builder, IndexBuilder)) indexes.push(builder.build(table));
|
|
224
|
+
else if (is(builder, CheckBuilder)) checks.push(builder.build(table));
|
|
225
|
+
else if (is(builder, UniqueConstraintBuilder)) uniqueConstraints.push(builder.build(table));
|
|
226
|
+
else if (is(builder, PrimaryKeyBuilder)) primaryKeys.push(builder.build(table));
|
|
227
|
+
else if (is(builder, ForeignKeyBuilder)) foreignKeys.push(builder.build(table));
|
|
228
|
+
}
|
|
229
|
+
return {
|
|
230
|
+
columns,
|
|
231
|
+
indexes,
|
|
232
|
+
foreignKeys,
|
|
233
|
+
checks,
|
|
234
|
+
primaryKeys,
|
|
235
|
+
uniqueConstraints,
|
|
236
|
+
name,
|
|
237
|
+
schema,
|
|
238
|
+
baseName
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
function convertIndexToString(indexes) {
|
|
242
|
+
return indexes.map((idx) => {
|
|
243
|
+
return typeof idx === "object" ? idx.config.name : idx;
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
function toArray(value) {
|
|
247
|
+
return Array.isArray(value) ? value : [value];
|
|
248
|
+
}
|
|
249
|
+
var MySqlViewBase = class extends View {
|
|
250
|
+
static [entityKind] = "MySqlViewBase";
|
|
251
|
+
};
|
|
252
|
+
var MySqlDialect = class {
|
|
253
|
+
static [entityKind] = "MySqlDialect";
|
|
254
|
+
/** @internal */
|
|
255
|
+
casing;
|
|
256
|
+
constructor(config) {
|
|
257
|
+
this.casing = new CasingCache(config?.casing);
|
|
258
|
+
}
|
|
259
|
+
async migrate(migrations, session, config) {
|
|
260
|
+
const migrationsTable = config.migrationsTable ?? "__drizzle_migrations";
|
|
261
|
+
const migrationTableCreate = sql`
|
|
262
|
+
create table if not exists ${sql.identifier(migrationsTable)} (
|
|
263
|
+
id serial primary key,
|
|
264
|
+
hash text not null,
|
|
265
|
+
created_at bigint
|
|
266
|
+
)
|
|
267
|
+
`;
|
|
268
|
+
await session.execute(migrationTableCreate);
|
|
269
|
+
const lastDbMigration = (await session.all(sql`select id, hash, created_at from ${sql.identifier(migrationsTable)} order by created_at desc limit 1`))[0];
|
|
270
|
+
await session.transaction(async (tx) => {
|
|
271
|
+
for (const migration of migrations) if (!lastDbMigration || Number(lastDbMigration.created_at) < migration.folderMillis) {
|
|
272
|
+
for (const stmt of migration.sql) await tx.execute(sql.raw(stmt));
|
|
273
|
+
await tx.execute(sql`insert into ${sql.identifier(migrationsTable)} (\`hash\`, \`created_at\`) values(${migration.hash}, ${migration.folderMillis})`);
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
escapeName(name) {
|
|
278
|
+
return `\`${name.replace(/`/g, "``")}\``;
|
|
279
|
+
}
|
|
280
|
+
escapeParam(_num) {
|
|
281
|
+
return `?`;
|
|
282
|
+
}
|
|
283
|
+
escapeString(str) {
|
|
284
|
+
return `'${str.replace(/'/g, "''")}'`;
|
|
285
|
+
}
|
|
286
|
+
buildWithCTE(queries) {
|
|
287
|
+
if (!queries?.length) return void 0;
|
|
288
|
+
const withSqlChunks = [sql`with `];
|
|
289
|
+
for (const [i, w] of queries.entries()) {
|
|
290
|
+
withSqlChunks.push(sql`${sql.identifier(w._.alias)} as (${w._.sql})`);
|
|
291
|
+
if (i < queries.length - 1) withSqlChunks.push(sql`, `);
|
|
292
|
+
}
|
|
293
|
+
withSqlChunks.push(sql` `);
|
|
294
|
+
return sql.join(withSqlChunks);
|
|
295
|
+
}
|
|
296
|
+
buildDeleteQuery({ table, where, returning, withList, limit, orderBy }) {
|
|
297
|
+
const withSql = this.buildWithCTE(withList);
|
|
298
|
+
const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
|
|
299
|
+
return sql`${withSql}delete from ${table}${where ? sql` where ${where}` : void 0}${this.buildOrderBy(orderBy)}${this.buildLimit(limit)}${returningSql}`;
|
|
300
|
+
}
|
|
301
|
+
buildUpdateSet(table, set) {
|
|
302
|
+
const tableColumns = table[Table.Symbol.Columns];
|
|
303
|
+
const columnNames = Object.keys(tableColumns).filter((colName) => set[colName] !== void 0 || tableColumns[colName]?.onUpdateFn !== void 0);
|
|
304
|
+
const setSize = columnNames.length;
|
|
305
|
+
return sql.join(columnNames.flatMap((colName, i) => {
|
|
306
|
+
const col = tableColumns[colName];
|
|
307
|
+
const onUpdateFnResult = col.onUpdateFn?.();
|
|
308
|
+
const value = set[colName] ?? (is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col));
|
|
309
|
+
const res = sql`${sql.identifier(this.casing.getColumnCasing(col))} = ${value}`;
|
|
310
|
+
if (i < setSize - 1) return [res, sql.raw(", ")];
|
|
311
|
+
return [res];
|
|
312
|
+
}));
|
|
313
|
+
}
|
|
314
|
+
buildUpdateQuery({ table, set, where, returning, withList, limit, orderBy }) {
|
|
315
|
+
const withSql = this.buildWithCTE(withList);
|
|
316
|
+
const setSql = this.buildUpdateSet(table, set);
|
|
317
|
+
const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
|
|
318
|
+
return sql`${withSql}update ${table} set ${setSql}${where ? sql` where ${where}` : void 0}${this.buildOrderBy(orderBy)}${this.buildLimit(limit)}${returningSql}`;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Builds selection SQL with provided fields/expressions
|
|
322
|
+
*
|
|
323
|
+
* Examples:
|
|
324
|
+
*
|
|
325
|
+
* `select <selection> from`
|
|
326
|
+
*
|
|
327
|
+
* `insert ... returning <selection>`
|
|
328
|
+
*
|
|
329
|
+
* If `isSingleTable` is true, then columns won't be prefixed with table name
|
|
330
|
+
*/
|
|
331
|
+
buildSelection(fields, { isSingleTable = false } = {}) {
|
|
332
|
+
const columnsLen = fields.length;
|
|
333
|
+
const chunks = fields.flatMap(({ field }, i) => {
|
|
334
|
+
const chunk = [];
|
|
335
|
+
if (is(field, SQL.Aliased) && field.isSelectionField) chunk.push(sql.identifier(field.fieldAlias));
|
|
336
|
+
else if (is(field, SQL.Aliased) || is(field, SQL)) {
|
|
337
|
+
const query = is(field, SQL.Aliased) ? field.sql : field;
|
|
338
|
+
if (isSingleTable) chunk.push(new SQL(query.queryChunks.map((c) => {
|
|
339
|
+
if (is(c, MySqlColumn)) return sql.identifier(this.casing.getColumnCasing(c));
|
|
340
|
+
return c;
|
|
341
|
+
})));
|
|
342
|
+
else chunk.push(query);
|
|
343
|
+
if (is(field, SQL.Aliased)) chunk.push(sql` as ${sql.identifier(field.fieldAlias)}`);
|
|
344
|
+
} else if (is(field, Column)) if (isSingleTable) chunk.push(sql.identifier(this.casing.getColumnCasing(field)));
|
|
345
|
+
else chunk.push(field);
|
|
346
|
+
else if (is(field, Subquery)) {
|
|
347
|
+
const entries = Object.entries(field._.selectedFields);
|
|
348
|
+
if (entries.length === 1) {
|
|
349
|
+
const entry = entries[0][1];
|
|
350
|
+
const fieldDecoder = is(entry, SQL) ? entry.decoder : is(entry, Column) ? { mapFromDriverValue: (v) => entry.mapFromDriverValue(v) } : entry.sql.decoder;
|
|
351
|
+
if (fieldDecoder) field._.sql.decoder = fieldDecoder;
|
|
352
|
+
}
|
|
353
|
+
chunk.push(field);
|
|
354
|
+
}
|
|
355
|
+
if (i < columnsLen - 1) chunk.push(sql`, `);
|
|
356
|
+
return chunk;
|
|
357
|
+
});
|
|
358
|
+
return sql.join(chunks);
|
|
359
|
+
}
|
|
360
|
+
buildLimit(limit) {
|
|
361
|
+
return typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql` limit ${limit}` : void 0;
|
|
362
|
+
}
|
|
363
|
+
buildOrderBy(orderBy) {
|
|
364
|
+
return orderBy && orderBy.length > 0 ? sql` order by ${sql.join(orderBy, sql`, `)}` : void 0;
|
|
365
|
+
}
|
|
366
|
+
buildIndex({ indexes, indexFor }) {
|
|
367
|
+
return indexes && indexes.length > 0 ? sql` ${sql.raw(indexFor)} INDEX (${sql.raw(indexes.join(`, `))})` : void 0;
|
|
368
|
+
}
|
|
369
|
+
buildSelectQuery({ withList, fields, fieldsFlat, where, having, table, joins, orderBy, groupBy, limit, offset, lockingClause, distinct, setOperators, useIndex, forceIndex, ignoreIndex }) {
|
|
370
|
+
const fieldsList = fieldsFlat ?? orderSelectedFields(fields);
|
|
371
|
+
for (const f of fieldsList) if (is(f.field, Column) && getTableName(f.field.table) !== (is(table, Subquery) ? table._.alias : is(table, MySqlViewBase) ? table[ViewBaseConfig].name : is(table, SQL) ? void 0 : getTableName(table)) && !((table2) => joins?.some(({ alias }) => alias === (table2[Table.Symbol.IsAlias] ? getTableName(table2) : table2[Table.Symbol.BaseName])))(f.field.table)) {
|
|
372
|
+
const tableName = getTableName(f.field.table);
|
|
373
|
+
throw new Error(`Your "${f.path.join("->")}" field references a column "${tableName}"."${f.field.name}", but the table "${tableName}" is not part of the query! Did you forget to join it?`);
|
|
374
|
+
}
|
|
375
|
+
const isSingleTable = !joins || joins.length === 0;
|
|
376
|
+
const withSql = this.buildWithCTE(withList);
|
|
377
|
+
const distinctSql = distinct ? sql` distinct` : void 0;
|
|
378
|
+
const selection = this.buildSelection(fieldsList, { isSingleTable });
|
|
379
|
+
const tableSql = (() => {
|
|
380
|
+
if (is(table, Table) && table[Table.Symbol.IsAlias]) return sql`${sql`${sql.identifier(table[Table.Symbol.Schema] ?? "")}.`.if(table[Table.Symbol.Schema])}${sql.identifier(table[Table.Symbol.OriginalName])} ${sql.identifier(table[Table.Symbol.Name])}`;
|
|
381
|
+
return table;
|
|
382
|
+
})();
|
|
383
|
+
const joinsArray = [];
|
|
384
|
+
if (joins) for (const [index, joinMeta] of joins.entries()) {
|
|
385
|
+
if (index === 0) joinsArray.push(sql` `);
|
|
386
|
+
const table2 = joinMeta.table;
|
|
387
|
+
const lateralSql = joinMeta.lateral ? sql` lateral` : void 0;
|
|
388
|
+
const onSql = joinMeta.on ? sql` on ${joinMeta.on}` : void 0;
|
|
389
|
+
if (is(table2, MySqlTable)) {
|
|
390
|
+
const tableName = table2[MySqlTable.Symbol.Name];
|
|
391
|
+
const tableSchema = table2[MySqlTable.Symbol.Schema];
|
|
392
|
+
const origTableName = table2[MySqlTable.Symbol.OriginalName];
|
|
393
|
+
const alias = tableName === origTableName ? void 0 : joinMeta.alias;
|
|
394
|
+
const useIndexSql2 = this.buildIndex({
|
|
395
|
+
indexes: joinMeta.useIndex,
|
|
396
|
+
indexFor: "USE"
|
|
397
|
+
});
|
|
398
|
+
const forceIndexSql2 = this.buildIndex({
|
|
399
|
+
indexes: joinMeta.forceIndex,
|
|
400
|
+
indexFor: "FORCE"
|
|
401
|
+
});
|
|
402
|
+
const ignoreIndexSql2 = this.buildIndex({
|
|
403
|
+
indexes: joinMeta.ignoreIndex,
|
|
404
|
+
indexFor: "IGNORE"
|
|
405
|
+
});
|
|
406
|
+
joinsArray.push(sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${useIndexSql2}${forceIndexSql2}${ignoreIndexSql2}${alias && sql` ${sql.identifier(alias)}`}${onSql}`);
|
|
407
|
+
} else if (is(table2, View)) {
|
|
408
|
+
const viewName = table2[ViewBaseConfig].name;
|
|
409
|
+
const viewSchema = table2[ViewBaseConfig].schema;
|
|
410
|
+
const origViewName = table2[ViewBaseConfig].originalName;
|
|
411
|
+
const alias = viewName === origViewName ? void 0 : joinMeta.alias;
|
|
412
|
+
joinsArray.push(sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${viewSchema ? sql`${sql.identifier(viewSchema)}.` : void 0}${sql.identifier(origViewName)}${alias && sql` ${sql.identifier(alias)}`}${onSql}`);
|
|
413
|
+
} else joinsArray.push(sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${table2}${onSql}`);
|
|
414
|
+
if (index < joins.length - 1) joinsArray.push(sql` `);
|
|
415
|
+
}
|
|
416
|
+
const joinsSql = sql.join(joinsArray);
|
|
417
|
+
const whereSql = where ? sql` where ${where}` : void 0;
|
|
418
|
+
const havingSql = having ? sql` having ${having}` : void 0;
|
|
419
|
+
const orderBySql = this.buildOrderBy(orderBy);
|
|
420
|
+
const groupBySql = groupBy && groupBy.length > 0 ? sql` group by ${sql.join(groupBy, sql`, `)}` : void 0;
|
|
421
|
+
const limitSql = this.buildLimit(limit);
|
|
422
|
+
const offsetSql = offset ? sql` offset ${offset}` : void 0;
|
|
423
|
+
const useIndexSql = this.buildIndex({
|
|
424
|
+
indexes: useIndex,
|
|
425
|
+
indexFor: "USE"
|
|
426
|
+
});
|
|
427
|
+
const forceIndexSql = this.buildIndex({
|
|
428
|
+
indexes: forceIndex,
|
|
429
|
+
indexFor: "FORCE"
|
|
430
|
+
});
|
|
431
|
+
const ignoreIndexSql = this.buildIndex({
|
|
432
|
+
indexes: ignoreIndex,
|
|
433
|
+
indexFor: "IGNORE"
|
|
434
|
+
});
|
|
435
|
+
let lockingClausesSql;
|
|
436
|
+
if (lockingClause) {
|
|
437
|
+
const { config, strength } = lockingClause;
|
|
438
|
+
lockingClausesSql = sql` for ${sql.raw(strength)}`;
|
|
439
|
+
if (config.noWait) lockingClausesSql.append(sql` nowait`);
|
|
440
|
+
else if (config.skipLocked) lockingClausesSql.append(sql` skip locked`);
|
|
441
|
+
}
|
|
442
|
+
const finalQuery = sql`${withSql}select${distinctSql} ${selection} from ${tableSql}${useIndexSql}${forceIndexSql}${ignoreIndexSql}${joinsSql}${whereSql}${groupBySql}${havingSql}${orderBySql}${limitSql}${offsetSql}${lockingClausesSql}`;
|
|
443
|
+
if (setOperators.length > 0) return this.buildSetOperations(finalQuery, setOperators);
|
|
444
|
+
return finalQuery;
|
|
445
|
+
}
|
|
446
|
+
buildSetOperations(leftSelect, setOperators) {
|
|
447
|
+
const [setOperator, ...rest] = setOperators;
|
|
448
|
+
if (!setOperator) throw new Error("Cannot pass undefined values to any set operator");
|
|
449
|
+
if (rest.length === 0) return this.buildSetOperationQuery({
|
|
450
|
+
leftSelect,
|
|
451
|
+
setOperator
|
|
452
|
+
});
|
|
453
|
+
return this.buildSetOperations(this.buildSetOperationQuery({
|
|
454
|
+
leftSelect,
|
|
455
|
+
setOperator
|
|
456
|
+
}), rest);
|
|
457
|
+
}
|
|
458
|
+
buildSetOperationQuery({ leftSelect, setOperator: { type, isAll, rightSelect, limit, orderBy, offset } }) {
|
|
459
|
+
const leftChunk = sql`(${leftSelect.getSQL()}) `;
|
|
460
|
+
const rightChunk = sql`(${rightSelect.getSQL()})`;
|
|
461
|
+
let orderBySql;
|
|
462
|
+
if (orderBy && orderBy.length > 0) {
|
|
463
|
+
const orderByValues = [];
|
|
464
|
+
for (const orderByUnit of orderBy) if (is(orderByUnit, MySqlColumn)) orderByValues.push(sql.identifier(this.casing.getColumnCasing(orderByUnit)));
|
|
465
|
+
else if (is(orderByUnit, SQL)) {
|
|
466
|
+
for (let i = 0; i < orderByUnit.queryChunks.length; i++) {
|
|
467
|
+
const chunk = orderByUnit.queryChunks[i];
|
|
468
|
+
if (is(chunk, MySqlColumn)) orderByUnit.queryChunks[i] = sql.identifier(this.casing.getColumnCasing(chunk));
|
|
469
|
+
}
|
|
470
|
+
orderByValues.push(sql`${orderByUnit}`);
|
|
471
|
+
} else orderByValues.push(sql`${orderByUnit}`);
|
|
472
|
+
orderBySql = sql` order by ${sql.join(orderByValues, sql`, `)} `;
|
|
473
|
+
}
|
|
474
|
+
const limitSql = typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql` limit ${limit}` : void 0;
|
|
475
|
+
const operatorChunk = sql.raw(`${type} ${isAll ? "all " : ""}`);
|
|
476
|
+
const offsetSql = offset ? sql` offset ${offset}` : void 0;
|
|
477
|
+
return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
|
|
478
|
+
}
|
|
479
|
+
buildInsertQuery({ table, values: valuesOrSelect, ignore, onConflict, select }) {
|
|
480
|
+
const valuesSqlList = [];
|
|
481
|
+
const columns = table[Table.Symbol.Columns];
|
|
482
|
+
const colEntries = Object.entries(columns).filter(([_, col]) => !col.shouldDisableInsert());
|
|
483
|
+
const insertOrder = colEntries.map(([, column]) => sql.identifier(this.casing.getColumnCasing(column)));
|
|
484
|
+
const generatedIdsResponse = [];
|
|
485
|
+
if (select) {
|
|
486
|
+
const select2 = valuesOrSelect;
|
|
487
|
+
if (is(select2, SQL)) valuesSqlList.push(select2);
|
|
488
|
+
else valuesSqlList.push(select2.getSQL());
|
|
489
|
+
} else {
|
|
490
|
+
const values = valuesOrSelect;
|
|
491
|
+
valuesSqlList.push(sql.raw("values "));
|
|
492
|
+
for (const [valueIndex, value] of values.entries()) {
|
|
493
|
+
const generatedIds = {};
|
|
494
|
+
const valueList = [];
|
|
495
|
+
for (const [fieldName, col] of colEntries) {
|
|
496
|
+
const colValue = value[fieldName];
|
|
497
|
+
if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) if (col.defaultFn !== void 0) {
|
|
498
|
+
const defaultFnResult = col.defaultFn();
|
|
499
|
+
generatedIds[fieldName] = defaultFnResult;
|
|
500
|
+
const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
|
|
501
|
+
valueList.push(defaultValue);
|
|
502
|
+
} else if (!col.default && col.onUpdateFn !== void 0) {
|
|
503
|
+
const onUpdateFnResult = col.onUpdateFn();
|
|
504
|
+
const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
|
|
505
|
+
valueList.push(newValue);
|
|
506
|
+
} else valueList.push(sql`default`);
|
|
507
|
+
else {
|
|
508
|
+
if (col.defaultFn && is(colValue, Param)) generatedIds[fieldName] = colValue.value;
|
|
509
|
+
valueList.push(colValue);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
generatedIdsResponse.push(generatedIds);
|
|
513
|
+
valuesSqlList.push(valueList);
|
|
514
|
+
if (valueIndex < values.length - 1) valuesSqlList.push(sql`, `);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
const valuesSql = sql.join(valuesSqlList);
|
|
518
|
+
return {
|
|
519
|
+
sql: sql`insert${ignore ? sql` ignore` : void 0} into ${table} ${insertOrder} ${valuesSql}${onConflict ? sql` on duplicate key ${onConflict}` : void 0}`,
|
|
520
|
+
generatedIds: generatedIdsResponse
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
sqlToQuery(sql2, invokeSource) {
|
|
524
|
+
return sql2.toQuery({
|
|
525
|
+
casing: this.casing,
|
|
526
|
+
escapeName: this.escapeName,
|
|
527
|
+
escapeParam: this.escapeParam,
|
|
528
|
+
escapeString: this.escapeString,
|
|
529
|
+
invokeSource
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
buildRelationalQuery({ fullSchema, schema, tableNamesMap, table, tableConfig, queryConfig: config, tableAlias, nestedQueryRelation, joinOn }) {
|
|
533
|
+
let selection = [];
|
|
534
|
+
let limit, offset, orderBy, where;
|
|
535
|
+
const joins = [];
|
|
536
|
+
if (config === true) selection = Object.entries(tableConfig.columns).map(([key, value]) => ({
|
|
537
|
+
dbKey: value.name,
|
|
538
|
+
tsKey: key,
|
|
539
|
+
field: aliasedTableColumn(value, tableAlias),
|
|
540
|
+
relationTableTsKey: void 0,
|
|
541
|
+
isJson: false,
|
|
542
|
+
selection: []
|
|
543
|
+
}));
|
|
544
|
+
else {
|
|
545
|
+
const aliasedColumns = Object.fromEntries(Object.entries(tableConfig.columns).map(([key, value]) => [key, aliasedTableColumn(value, tableAlias)]));
|
|
546
|
+
if (config.where) {
|
|
547
|
+
const whereSql = typeof config.where === "function" ? config.where(aliasedColumns, getOperators()) : config.where;
|
|
548
|
+
where = whereSql && mapColumnsInSQLToAlias(whereSql, tableAlias);
|
|
549
|
+
}
|
|
550
|
+
const fieldsSelection = [];
|
|
551
|
+
let selectedColumns = [];
|
|
552
|
+
if (config.columns) {
|
|
553
|
+
let isIncludeMode = false;
|
|
554
|
+
for (const [field, value] of Object.entries(config.columns)) {
|
|
555
|
+
if (value === void 0) continue;
|
|
556
|
+
if (field in tableConfig.columns) {
|
|
557
|
+
if (!isIncludeMode && value === true) isIncludeMode = true;
|
|
558
|
+
selectedColumns.push(field);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
if (selectedColumns.length > 0) selectedColumns = isIncludeMode ? selectedColumns.filter((c) => config.columns?.[c] === true) : Object.keys(tableConfig.columns).filter((key) => !selectedColumns.includes(key));
|
|
562
|
+
} else selectedColumns = Object.keys(tableConfig.columns);
|
|
563
|
+
for (const field of selectedColumns) {
|
|
564
|
+
const column = tableConfig.columns[field];
|
|
565
|
+
fieldsSelection.push({
|
|
566
|
+
tsKey: field,
|
|
567
|
+
value: column
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
let selectedRelations = [];
|
|
571
|
+
if (config.with) selectedRelations = Object.entries(config.with).filter((entry) => !!entry[1]).map(([tsKey, queryConfig]) => ({
|
|
572
|
+
tsKey,
|
|
573
|
+
queryConfig,
|
|
574
|
+
relation: tableConfig.relations[tsKey]
|
|
575
|
+
}));
|
|
576
|
+
let extras;
|
|
577
|
+
if (config.extras) {
|
|
578
|
+
extras = typeof config.extras === "function" ? config.extras(aliasedColumns, { sql }) : config.extras;
|
|
579
|
+
for (const [tsKey, value] of Object.entries(extras)) fieldsSelection.push({
|
|
580
|
+
tsKey,
|
|
581
|
+
value: mapColumnsInAliasedSQLToAlias(value, tableAlias)
|
|
582
|
+
});
|
|
583
|
+
}
|
|
584
|
+
for (const { tsKey, value } of fieldsSelection) selection.push({
|
|
585
|
+
dbKey: is(value, SQL.Aliased) ? value.fieldAlias : tableConfig.columns[tsKey].name,
|
|
586
|
+
tsKey,
|
|
587
|
+
field: is(value, Column) ? aliasedTableColumn(value, tableAlias) : value,
|
|
588
|
+
relationTableTsKey: void 0,
|
|
589
|
+
isJson: false,
|
|
590
|
+
selection: []
|
|
591
|
+
});
|
|
592
|
+
let orderByOrig = typeof config.orderBy === "function" ? config.orderBy(aliasedColumns, getOrderByOperators()) : config.orderBy ?? [];
|
|
593
|
+
if (!Array.isArray(orderByOrig)) orderByOrig = [orderByOrig];
|
|
594
|
+
orderBy = orderByOrig.map((orderByValue) => {
|
|
595
|
+
if (is(orderByValue, Column)) return aliasedTableColumn(orderByValue, tableAlias);
|
|
596
|
+
return mapColumnsInSQLToAlias(orderByValue, tableAlias);
|
|
597
|
+
});
|
|
598
|
+
limit = config.limit;
|
|
599
|
+
offset = config.offset;
|
|
600
|
+
for (const { tsKey: selectedRelationTsKey, queryConfig: selectedRelationConfigValue, relation } of selectedRelations) {
|
|
601
|
+
const normalizedRelation = normalizeRelation(schema, tableNamesMap, relation);
|
|
602
|
+
const relationTableTsName = tableNamesMap[getTableUniqueName(relation.referencedTable)];
|
|
603
|
+
const relationTableAlias = `${tableAlias}_${selectedRelationTsKey}`;
|
|
604
|
+
const joinOn2 = and(...normalizedRelation.fields.map((field2, i) => eq(aliasedTableColumn(normalizedRelation.references[i], relationTableAlias), aliasedTableColumn(field2, tableAlias))));
|
|
605
|
+
const builtRelation = this.buildRelationalQuery({
|
|
606
|
+
fullSchema,
|
|
607
|
+
schema,
|
|
608
|
+
tableNamesMap,
|
|
609
|
+
table: fullSchema[relationTableTsName],
|
|
610
|
+
tableConfig: schema[relationTableTsName],
|
|
611
|
+
queryConfig: is(relation, One) ? selectedRelationConfigValue === true ? { limit: 1 } : {
|
|
612
|
+
...selectedRelationConfigValue,
|
|
613
|
+
limit: 1
|
|
614
|
+
} : selectedRelationConfigValue,
|
|
615
|
+
tableAlias: relationTableAlias,
|
|
616
|
+
joinOn: joinOn2,
|
|
617
|
+
nestedQueryRelation: relation
|
|
618
|
+
});
|
|
619
|
+
const field = sql`${sql.identifier(relationTableAlias)}.${sql.identifier("data")}`.as(selectedRelationTsKey);
|
|
620
|
+
joins.push({
|
|
621
|
+
on: sql`true`,
|
|
622
|
+
table: new Subquery(builtRelation.sql, {}, relationTableAlias),
|
|
623
|
+
alias: relationTableAlias,
|
|
624
|
+
joinType: "left",
|
|
625
|
+
lateral: true
|
|
626
|
+
});
|
|
627
|
+
selection.push({
|
|
628
|
+
dbKey: selectedRelationTsKey,
|
|
629
|
+
tsKey: selectedRelationTsKey,
|
|
630
|
+
field,
|
|
631
|
+
relationTableTsKey: relationTableTsName,
|
|
632
|
+
isJson: true,
|
|
633
|
+
selection: builtRelation.selection
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
if (selection.length === 0) throw new DrizzleError({ message: `No fields selected for table "${tableConfig.tsName}" ("${tableAlias}")` });
|
|
638
|
+
let result;
|
|
639
|
+
where = and(joinOn, where);
|
|
640
|
+
if (nestedQueryRelation) {
|
|
641
|
+
let field = sql`json_array(${sql.join(selection.map(({ field: field2, tsKey, isJson }) => isJson ? sql`${sql.identifier(`${tableAlias}_${tsKey}`)}.${sql.identifier("data")}` : is(field2, SQL.Aliased) ? field2.sql : field2), sql`, `)})`;
|
|
642
|
+
if (is(nestedQueryRelation, Many)) field = sql`coalesce(json_arrayagg(${field}), json_array())`;
|
|
643
|
+
const nestedSelection = [{
|
|
644
|
+
dbKey: "data",
|
|
645
|
+
tsKey: "data",
|
|
646
|
+
field: field.as("data"),
|
|
647
|
+
isJson: true,
|
|
648
|
+
relationTableTsKey: tableConfig.tsName,
|
|
649
|
+
selection
|
|
650
|
+
}];
|
|
651
|
+
if (limit !== void 0 || offset !== void 0 || (orderBy?.length ?? 0) > 0) {
|
|
652
|
+
result = this.buildSelectQuery({
|
|
653
|
+
table: aliasedTable(table, tableAlias),
|
|
654
|
+
fields: {},
|
|
655
|
+
fieldsFlat: [{
|
|
656
|
+
path: [],
|
|
657
|
+
field: sql.raw("*")
|
|
658
|
+
}, ...(orderBy?.length ?? 0) > 0 ? [{
|
|
659
|
+
path: [],
|
|
660
|
+
field: sql`row_number() over (order by ${sql.join(orderBy, sql`, `)})`
|
|
661
|
+
}] : []],
|
|
662
|
+
where,
|
|
663
|
+
limit,
|
|
664
|
+
offset,
|
|
665
|
+
setOperators: []
|
|
666
|
+
});
|
|
667
|
+
where = void 0;
|
|
668
|
+
limit = void 0;
|
|
669
|
+
offset = void 0;
|
|
670
|
+
orderBy = void 0;
|
|
671
|
+
} else result = aliasedTable(table, tableAlias);
|
|
672
|
+
result = this.buildSelectQuery({
|
|
673
|
+
table: is(result, MySqlTable) ? result : new Subquery(result, {}, tableAlias),
|
|
674
|
+
fields: {},
|
|
675
|
+
fieldsFlat: nestedSelection.map(({ field: field2 }) => ({
|
|
676
|
+
path: [],
|
|
677
|
+
field: is(field2, Column) ? aliasedTableColumn(field2, tableAlias) : field2
|
|
678
|
+
})),
|
|
679
|
+
joins,
|
|
680
|
+
where,
|
|
681
|
+
limit,
|
|
682
|
+
offset,
|
|
683
|
+
orderBy,
|
|
684
|
+
setOperators: []
|
|
685
|
+
});
|
|
686
|
+
} else result = this.buildSelectQuery({
|
|
687
|
+
table: aliasedTable(table, tableAlias),
|
|
688
|
+
fields: {},
|
|
689
|
+
fieldsFlat: selection.map(({ field }) => ({
|
|
690
|
+
path: [],
|
|
691
|
+
field: is(field, Column) ? aliasedTableColumn(field, tableAlias) : field
|
|
692
|
+
})),
|
|
693
|
+
joins,
|
|
694
|
+
where,
|
|
695
|
+
limit,
|
|
696
|
+
offset,
|
|
697
|
+
orderBy,
|
|
698
|
+
setOperators: []
|
|
699
|
+
});
|
|
700
|
+
return {
|
|
701
|
+
tableTsKey: tableConfig.tsName,
|
|
702
|
+
sql: result,
|
|
703
|
+
selection
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
buildRelationalQueryWithoutLateralSubqueries({ fullSchema, schema, tableNamesMap, table, tableConfig, queryConfig: config, tableAlias, nestedQueryRelation, joinOn }) {
|
|
707
|
+
let selection = [];
|
|
708
|
+
let limit, offset, orderBy = [], where;
|
|
709
|
+
if (config === true) selection = Object.entries(tableConfig.columns).map(([key, value]) => ({
|
|
710
|
+
dbKey: value.name,
|
|
711
|
+
tsKey: key,
|
|
712
|
+
field: aliasedTableColumn(value, tableAlias),
|
|
713
|
+
relationTableTsKey: void 0,
|
|
714
|
+
isJson: false,
|
|
715
|
+
selection: []
|
|
716
|
+
}));
|
|
717
|
+
else {
|
|
718
|
+
const aliasedColumns = Object.fromEntries(Object.entries(tableConfig.columns).map(([key, value]) => [key, aliasedTableColumn(value, tableAlias)]));
|
|
719
|
+
if (config.where) {
|
|
720
|
+
const whereSql = typeof config.where === "function" ? config.where(aliasedColumns, getOperators()) : config.where;
|
|
721
|
+
where = whereSql && mapColumnsInSQLToAlias(whereSql, tableAlias);
|
|
722
|
+
}
|
|
723
|
+
const fieldsSelection = [];
|
|
724
|
+
let selectedColumns = [];
|
|
725
|
+
if (config.columns) {
|
|
726
|
+
let isIncludeMode = false;
|
|
727
|
+
for (const [field, value] of Object.entries(config.columns)) {
|
|
728
|
+
if (value === void 0) continue;
|
|
729
|
+
if (field in tableConfig.columns) {
|
|
730
|
+
if (!isIncludeMode && value === true) isIncludeMode = true;
|
|
731
|
+
selectedColumns.push(field);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
if (selectedColumns.length > 0) selectedColumns = isIncludeMode ? selectedColumns.filter((c) => config.columns?.[c] === true) : Object.keys(tableConfig.columns).filter((key) => !selectedColumns.includes(key));
|
|
735
|
+
} else selectedColumns = Object.keys(tableConfig.columns);
|
|
736
|
+
for (const field of selectedColumns) {
|
|
737
|
+
const column = tableConfig.columns[field];
|
|
738
|
+
fieldsSelection.push({
|
|
739
|
+
tsKey: field,
|
|
740
|
+
value: column
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
let selectedRelations = [];
|
|
744
|
+
if (config.with) selectedRelations = Object.entries(config.with).filter((entry) => !!entry[1]).map(([tsKey, queryConfig]) => ({
|
|
745
|
+
tsKey,
|
|
746
|
+
queryConfig,
|
|
747
|
+
relation: tableConfig.relations[tsKey]
|
|
748
|
+
}));
|
|
749
|
+
let extras;
|
|
750
|
+
if (config.extras) {
|
|
751
|
+
extras = typeof config.extras === "function" ? config.extras(aliasedColumns, { sql }) : config.extras;
|
|
752
|
+
for (const [tsKey, value] of Object.entries(extras)) fieldsSelection.push({
|
|
753
|
+
tsKey,
|
|
754
|
+
value: mapColumnsInAliasedSQLToAlias(value, tableAlias)
|
|
755
|
+
});
|
|
756
|
+
}
|
|
757
|
+
for (const { tsKey, value } of fieldsSelection) selection.push({
|
|
758
|
+
dbKey: is(value, SQL.Aliased) ? value.fieldAlias : tableConfig.columns[tsKey].name,
|
|
759
|
+
tsKey,
|
|
760
|
+
field: is(value, Column) ? aliasedTableColumn(value, tableAlias) : value,
|
|
761
|
+
relationTableTsKey: void 0,
|
|
762
|
+
isJson: false,
|
|
763
|
+
selection: []
|
|
764
|
+
});
|
|
765
|
+
let orderByOrig = typeof config.orderBy === "function" ? config.orderBy(aliasedColumns, getOrderByOperators()) : config.orderBy ?? [];
|
|
766
|
+
if (!Array.isArray(orderByOrig)) orderByOrig = [orderByOrig];
|
|
767
|
+
orderBy = orderByOrig.map((orderByValue) => {
|
|
768
|
+
if (is(orderByValue, Column)) return aliasedTableColumn(orderByValue, tableAlias);
|
|
769
|
+
return mapColumnsInSQLToAlias(orderByValue, tableAlias);
|
|
770
|
+
});
|
|
771
|
+
limit = config.limit;
|
|
772
|
+
offset = config.offset;
|
|
773
|
+
for (const { tsKey: selectedRelationTsKey, queryConfig: selectedRelationConfigValue, relation } of selectedRelations) {
|
|
774
|
+
const normalizedRelation = normalizeRelation(schema, tableNamesMap, relation);
|
|
775
|
+
const relationTableTsName = tableNamesMap[getTableUniqueName(relation.referencedTable)];
|
|
776
|
+
const relationTableAlias = `${tableAlias}_${selectedRelationTsKey}`;
|
|
777
|
+
const joinOn2 = and(...normalizedRelation.fields.map((field2, i) => eq(aliasedTableColumn(normalizedRelation.references[i], relationTableAlias), aliasedTableColumn(field2, tableAlias))));
|
|
778
|
+
const builtRelation = this.buildRelationalQueryWithoutLateralSubqueries({
|
|
779
|
+
fullSchema,
|
|
780
|
+
schema,
|
|
781
|
+
tableNamesMap,
|
|
782
|
+
table: fullSchema[relationTableTsName],
|
|
783
|
+
tableConfig: schema[relationTableTsName],
|
|
784
|
+
queryConfig: is(relation, One) ? selectedRelationConfigValue === true ? { limit: 1 } : {
|
|
785
|
+
...selectedRelationConfigValue,
|
|
786
|
+
limit: 1
|
|
787
|
+
} : selectedRelationConfigValue,
|
|
788
|
+
tableAlias: relationTableAlias,
|
|
789
|
+
joinOn: joinOn2,
|
|
790
|
+
nestedQueryRelation: relation
|
|
791
|
+
});
|
|
792
|
+
let fieldSql = sql`(${builtRelation.sql})`;
|
|
793
|
+
if (is(relation, Many)) fieldSql = sql`coalesce(${fieldSql}, json_array())`;
|
|
794
|
+
const field = fieldSql.as(selectedRelationTsKey);
|
|
795
|
+
selection.push({
|
|
796
|
+
dbKey: selectedRelationTsKey,
|
|
797
|
+
tsKey: selectedRelationTsKey,
|
|
798
|
+
field,
|
|
799
|
+
relationTableTsKey: relationTableTsName,
|
|
800
|
+
isJson: true,
|
|
801
|
+
selection: builtRelation.selection
|
|
802
|
+
});
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
if (selection.length === 0) throw new DrizzleError({ message: `No fields selected for table "${tableConfig.tsName}" ("${tableAlias}"). You need to have at least one item in "columns", "with" or "extras". If you need to select all columns, omit the "columns" key or set it to undefined.` });
|
|
806
|
+
let result;
|
|
807
|
+
where = and(joinOn, where);
|
|
808
|
+
if (nestedQueryRelation) {
|
|
809
|
+
let field = sql`json_array(${sql.join(selection.map(({ field: field2 }) => is(field2, MySqlColumn) ? sql.identifier(this.casing.getColumnCasing(field2)) : is(field2, SQL.Aliased) ? field2.sql : field2), sql`, `)})`;
|
|
810
|
+
if (is(nestedQueryRelation, Many)) field = sql`json_arrayagg(${field})`;
|
|
811
|
+
const nestedSelection = [{
|
|
812
|
+
dbKey: "data",
|
|
813
|
+
tsKey: "data",
|
|
814
|
+
field,
|
|
815
|
+
isJson: true,
|
|
816
|
+
relationTableTsKey: tableConfig.tsName,
|
|
817
|
+
selection
|
|
818
|
+
}];
|
|
819
|
+
if (limit !== void 0 || offset !== void 0 || orderBy.length > 0) {
|
|
820
|
+
result = this.buildSelectQuery({
|
|
821
|
+
table: aliasedTable(table, tableAlias),
|
|
822
|
+
fields: {},
|
|
823
|
+
fieldsFlat: [{
|
|
824
|
+
path: [],
|
|
825
|
+
field: sql.raw("*")
|
|
826
|
+
}, ...orderBy.length > 0 ? [{
|
|
827
|
+
path: [],
|
|
828
|
+
field: sql`row_number() over (order by ${sql.join(orderBy, sql`, `)})`
|
|
829
|
+
}] : []],
|
|
830
|
+
where,
|
|
831
|
+
limit,
|
|
832
|
+
offset,
|
|
833
|
+
setOperators: []
|
|
834
|
+
});
|
|
835
|
+
where = void 0;
|
|
836
|
+
limit = void 0;
|
|
837
|
+
offset = void 0;
|
|
838
|
+
orderBy = void 0;
|
|
839
|
+
} else result = aliasedTable(table, tableAlias);
|
|
840
|
+
result = this.buildSelectQuery({
|
|
841
|
+
table: is(result, MySqlTable) ? result : new Subquery(result, {}, tableAlias),
|
|
842
|
+
fields: {},
|
|
843
|
+
fieldsFlat: nestedSelection.map(({ field: field2 }) => ({
|
|
844
|
+
path: [],
|
|
845
|
+
field: is(field2, Column) ? aliasedTableColumn(field2, tableAlias) : field2
|
|
846
|
+
})),
|
|
847
|
+
where,
|
|
848
|
+
limit,
|
|
849
|
+
offset,
|
|
850
|
+
orderBy,
|
|
851
|
+
setOperators: []
|
|
852
|
+
});
|
|
853
|
+
} else result = this.buildSelectQuery({
|
|
854
|
+
table: aliasedTable(table, tableAlias),
|
|
855
|
+
fields: {},
|
|
856
|
+
fieldsFlat: selection.map(({ field }) => ({
|
|
857
|
+
path: [],
|
|
858
|
+
field: is(field, Column) ? aliasedTableColumn(field, tableAlias) : field
|
|
859
|
+
})),
|
|
860
|
+
where,
|
|
861
|
+
limit,
|
|
862
|
+
offset,
|
|
863
|
+
orderBy,
|
|
864
|
+
setOperators: []
|
|
865
|
+
});
|
|
866
|
+
return {
|
|
867
|
+
tableTsKey: tableConfig.tsName,
|
|
868
|
+
sql: result,
|
|
869
|
+
selection
|
|
870
|
+
};
|
|
871
|
+
}
|
|
872
|
+
};
|
|
873
|
+
var MySqlSelectBuilder = class {
|
|
874
|
+
static [entityKind] = "MySqlSelectBuilder";
|
|
875
|
+
fields;
|
|
876
|
+
session;
|
|
877
|
+
dialect;
|
|
878
|
+
withList = [];
|
|
879
|
+
distinct;
|
|
880
|
+
constructor(config) {
|
|
881
|
+
this.fields = config.fields;
|
|
882
|
+
this.session = config.session;
|
|
883
|
+
this.dialect = config.dialect;
|
|
884
|
+
if (config.withList) this.withList = config.withList;
|
|
885
|
+
this.distinct = config.distinct;
|
|
886
|
+
}
|
|
887
|
+
from(source, onIndex) {
|
|
888
|
+
const isPartialSelect = !!this.fields;
|
|
889
|
+
let fields;
|
|
890
|
+
if (this.fields) fields = this.fields;
|
|
891
|
+
else if (is(source, Subquery)) fields = Object.fromEntries(Object.keys(source._.selectedFields).map((key) => [key, source[key]]));
|
|
892
|
+
else if (is(source, MySqlViewBase)) fields = source[ViewBaseConfig].selectedFields;
|
|
893
|
+
else if (is(source, SQL)) fields = {};
|
|
894
|
+
else fields = getTableColumns(source);
|
|
895
|
+
let useIndex = [];
|
|
896
|
+
let forceIndex = [];
|
|
897
|
+
let ignoreIndex = [];
|
|
898
|
+
if (is(source, MySqlTable) && onIndex && typeof onIndex !== "string") {
|
|
899
|
+
if (onIndex.useIndex) useIndex = convertIndexToString(toArray(onIndex.useIndex));
|
|
900
|
+
if (onIndex.forceIndex) forceIndex = convertIndexToString(toArray(onIndex.forceIndex));
|
|
901
|
+
if (onIndex.ignoreIndex) ignoreIndex = convertIndexToString(toArray(onIndex.ignoreIndex));
|
|
902
|
+
}
|
|
903
|
+
return new MySqlSelectBase({
|
|
904
|
+
table: source,
|
|
905
|
+
fields,
|
|
906
|
+
isPartialSelect,
|
|
907
|
+
session: this.session,
|
|
908
|
+
dialect: this.dialect,
|
|
909
|
+
withList: this.withList,
|
|
910
|
+
distinct: this.distinct,
|
|
911
|
+
useIndex,
|
|
912
|
+
forceIndex,
|
|
913
|
+
ignoreIndex
|
|
914
|
+
});
|
|
915
|
+
}
|
|
916
|
+
};
|
|
917
|
+
var MySqlSelectQueryBuilderBase = class extends TypedQueryBuilder {
|
|
918
|
+
static [entityKind] = "MySqlSelectQueryBuilder";
|
|
919
|
+
_;
|
|
920
|
+
config;
|
|
921
|
+
joinsNotNullableMap;
|
|
922
|
+
tableName;
|
|
923
|
+
isPartialSelect;
|
|
924
|
+
/** @internal */
|
|
925
|
+
session;
|
|
926
|
+
dialect;
|
|
927
|
+
cacheConfig = void 0;
|
|
928
|
+
usedTables = /* @__PURE__ */ new Set();
|
|
929
|
+
constructor({ table, fields, isPartialSelect, session, dialect, withList, distinct, useIndex, forceIndex, ignoreIndex }) {
|
|
930
|
+
super();
|
|
931
|
+
this.config = {
|
|
932
|
+
withList,
|
|
933
|
+
table,
|
|
934
|
+
fields: { ...fields },
|
|
935
|
+
distinct,
|
|
936
|
+
setOperators: [],
|
|
937
|
+
useIndex,
|
|
938
|
+
forceIndex,
|
|
939
|
+
ignoreIndex
|
|
940
|
+
};
|
|
941
|
+
this.isPartialSelect = isPartialSelect;
|
|
942
|
+
this.session = session;
|
|
943
|
+
this.dialect = dialect;
|
|
944
|
+
this._ = {
|
|
945
|
+
selectedFields: fields,
|
|
946
|
+
config: this.config
|
|
947
|
+
};
|
|
948
|
+
this.tableName = getTableLikeName(table);
|
|
949
|
+
this.joinsNotNullableMap = typeof this.tableName === "string" ? { [this.tableName]: true } : {};
|
|
950
|
+
for (const item of extractUsedTable(table)) this.usedTables.add(item);
|
|
951
|
+
}
|
|
952
|
+
/** @internal */
|
|
953
|
+
getUsedTables() {
|
|
954
|
+
return [...this.usedTables];
|
|
955
|
+
}
|
|
956
|
+
createJoin(joinType, lateral) {
|
|
957
|
+
return (table, a, b) => {
|
|
958
|
+
const isCrossJoin = joinType === "cross";
|
|
959
|
+
let on = isCrossJoin ? void 0 : a;
|
|
960
|
+
const onIndex = isCrossJoin ? a : b;
|
|
961
|
+
const baseTableName = this.tableName;
|
|
962
|
+
const tableName = getTableLikeName(table);
|
|
963
|
+
for (const item of extractUsedTable(table)) this.usedTables.add(item);
|
|
964
|
+
if (typeof tableName === "string" && this.config.joins?.some((join) => join.alias === tableName)) throw new Error(`Alias "${tableName}" is already used in this query`);
|
|
965
|
+
if (!this.isPartialSelect) {
|
|
966
|
+
if (Object.keys(this.joinsNotNullableMap).length === 1 && typeof baseTableName === "string") this.config.fields = { [baseTableName]: this.config.fields };
|
|
967
|
+
if (typeof tableName === "string" && !is(table, SQL)) {
|
|
968
|
+
const selection = is(table, Subquery) ? table._.selectedFields : is(table, View) ? table[ViewBaseConfig].selectedFields : table[Table.Symbol.Columns];
|
|
969
|
+
this.config.fields[tableName] = selection;
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
if (typeof on === "function") on = on(new Proxy(this.config.fields, new SelectionProxyHandler({
|
|
973
|
+
sqlAliasedBehavior: "sql",
|
|
974
|
+
sqlBehavior: "sql"
|
|
975
|
+
})));
|
|
976
|
+
if (!this.config.joins) this.config.joins = [];
|
|
977
|
+
let useIndex = [];
|
|
978
|
+
let forceIndex = [];
|
|
979
|
+
let ignoreIndex = [];
|
|
980
|
+
if (is(table, MySqlTable) && onIndex && typeof onIndex !== "string") {
|
|
981
|
+
if (onIndex.useIndex) useIndex = convertIndexToString(toArray(onIndex.useIndex));
|
|
982
|
+
if (onIndex.forceIndex) forceIndex = convertIndexToString(toArray(onIndex.forceIndex));
|
|
983
|
+
if (onIndex.ignoreIndex) ignoreIndex = convertIndexToString(toArray(onIndex.ignoreIndex));
|
|
984
|
+
}
|
|
985
|
+
this.config.joins.push({
|
|
986
|
+
on,
|
|
987
|
+
table,
|
|
988
|
+
joinType,
|
|
989
|
+
alias: tableName,
|
|
990
|
+
useIndex,
|
|
991
|
+
forceIndex,
|
|
992
|
+
ignoreIndex,
|
|
993
|
+
lateral
|
|
994
|
+
});
|
|
995
|
+
if (typeof tableName === "string") switch (joinType) {
|
|
996
|
+
case "left":
|
|
997
|
+
this.joinsNotNullableMap[tableName] = false;
|
|
998
|
+
break;
|
|
999
|
+
case "right":
|
|
1000
|
+
this.joinsNotNullableMap = Object.fromEntries(Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false]));
|
|
1001
|
+
this.joinsNotNullableMap[tableName] = true;
|
|
1002
|
+
break;
|
|
1003
|
+
case "cross":
|
|
1004
|
+
case "inner":
|
|
1005
|
+
this.joinsNotNullableMap[tableName] = true;
|
|
1006
|
+
break;
|
|
1007
|
+
}
|
|
1008
|
+
return this;
|
|
1009
|
+
};
|
|
1010
|
+
}
|
|
1011
|
+
/**
|
|
1012
|
+
* Executes a `left join` operation by adding another table to the current query.
|
|
1013
|
+
*
|
|
1014
|
+
* Calling this method associates each row of the table with the corresponding row from the joined table, if a match is found. If no matching row exists, it sets all columns of the joined table to null.
|
|
1015
|
+
*
|
|
1016
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#left-join}
|
|
1017
|
+
*
|
|
1018
|
+
* @param table the table to join.
|
|
1019
|
+
* @param on the `on` clause.
|
|
1020
|
+
* @param onIndex index hint.
|
|
1021
|
+
*
|
|
1022
|
+
* @example
|
|
1023
|
+
*
|
|
1024
|
+
* ```ts
|
|
1025
|
+
* // Select all users and their pets
|
|
1026
|
+
* const usersWithPets: { user: User; pets: Pet | null; }[] = await db.select()
|
|
1027
|
+
* .from(users)
|
|
1028
|
+
* .leftJoin(pets, eq(users.id, pets.ownerId))
|
|
1029
|
+
*
|
|
1030
|
+
* // Select userId and petId
|
|
1031
|
+
* const usersIdsAndPetIds: { userId: number; petId: number | null; }[] = await db.select({
|
|
1032
|
+
* userId: users.id,
|
|
1033
|
+
* petId: pets.id,
|
|
1034
|
+
* })
|
|
1035
|
+
* .from(users)
|
|
1036
|
+
* .leftJoin(pets, eq(users.id, pets.ownerId))
|
|
1037
|
+
*
|
|
1038
|
+
* // Select userId and petId with use index hint
|
|
1039
|
+
* const usersIdsAndPetIds: { userId: number; petId: number | null; }[] = await db.select({
|
|
1040
|
+
* userId: users.id,
|
|
1041
|
+
* petId: pets.id,
|
|
1042
|
+
* })
|
|
1043
|
+
* .from(users)
|
|
1044
|
+
* .leftJoin(pets, eq(users.id, pets.ownerId), {
|
|
1045
|
+
* useIndex: ['pets_owner_id_index']
|
|
1046
|
+
* })
|
|
1047
|
+
* ```
|
|
1048
|
+
*/
|
|
1049
|
+
leftJoin = this.createJoin("left", false);
|
|
1050
|
+
/**
|
|
1051
|
+
* Executes a `left join lateral` operation by adding subquery to the current query.
|
|
1052
|
+
*
|
|
1053
|
+
* A `lateral` join allows the right-hand expression to refer to columns from the left-hand side.
|
|
1054
|
+
*
|
|
1055
|
+
* Calling this method associates each row of the table with the corresponding row from the joined table, if a match is found. If no matching row exists, it sets all columns of the joined table to null.
|
|
1056
|
+
*
|
|
1057
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#left-join-lateral}
|
|
1058
|
+
*
|
|
1059
|
+
* @param table the subquery to join.
|
|
1060
|
+
* @param on the `on` clause.
|
|
1061
|
+
*/
|
|
1062
|
+
leftJoinLateral = this.createJoin("left", true);
|
|
1063
|
+
/**
|
|
1064
|
+
* Executes a `right join` operation by adding another table to the current query.
|
|
1065
|
+
*
|
|
1066
|
+
* Calling this method associates each row of the joined table with the corresponding row from the main table, if a match is found. If no matching row exists, it sets all columns of the main table to null.
|
|
1067
|
+
*
|
|
1068
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#right-join}
|
|
1069
|
+
*
|
|
1070
|
+
* @param table the table to join.
|
|
1071
|
+
* @param on the `on` clause.
|
|
1072
|
+
* @param onIndex index hint.
|
|
1073
|
+
*
|
|
1074
|
+
* @example
|
|
1075
|
+
*
|
|
1076
|
+
* ```ts
|
|
1077
|
+
* // Select all users and their pets
|
|
1078
|
+
* const usersWithPets: { user: User | null; pets: Pet; }[] = await db.select()
|
|
1079
|
+
* .from(users)
|
|
1080
|
+
* .rightJoin(pets, eq(users.id, pets.ownerId))
|
|
1081
|
+
*
|
|
1082
|
+
* // Select userId and petId
|
|
1083
|
+
* const usersIdsAndPetIds: { userId: number | null; petId: number; }[] = await db.select({
|
|
1084
|
+
* userId: users.id,
|
|
1085
|
+
* petId: pets.id,
|
|
1086
|
+
* })
|
|
1087
|
+
* .from(users)
|
|
1088
|
+
* .rightJoin(pets, eq(users.id, pets.ownerId))
|
|
1089
|
+
*
|
|
1090
|
+
* // Select userId and petId with use index hint
|
|
1091
|
+
* const usersIdsAndPetIds: { userId: number; petId: number | null; }[] = await db.select({
|
|
1092
|
+
* userId: users.id,
|
|
1093
|
+
* petId: pets.id,
|
|
1094
|
+
* })
|
|
1095
|
+
* .from(users)
|
|
1096
|
+
* .leftJoin(pets, eq(users.id, pets.ownerId), {
|
|
1097
|
+
* useIndex: ['pets_owner_id_index']
|
|
1098
|
+
* })
|
|
1099
|
+
* ```
|
|
1100
|
+
*/
|
|
1101
|
+
rightJoin = this.createJoin("right", false);
|
|
1102
|
+
/**
|
|
1103
|
+
* Executes an `inner join` operation, creating a new table by combining rows from two tables that have matching values.
|
|
1104
|
+
*
|
|
1105
|
+
* Calling this method retrieves rows that have corresponding entries in both joined tables. Rows without matching entries in either table are excluded, resulting in a table that includes only matching pairs.
|
|
1106
|
+
*
|
|
1107
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#inner-join}
|
|
1108
|
+
*
|
|
1109
|
+
* @param table the table to join.
|
|
1110
|
+
* @param on the `on` clause.
|
|
1111
|
+
* @param onIndex index hint.
|
|
1112
|
+
*
|
|
1113
|
+
* @example
|
|
1114
|
+
*
|
|
1115
|
+
* ```ts
|
|
1116
|
+
* // Select all users and their pets
|
|
1117
|
+
* const usersWithPets: { user: User; pets: Pet; }[] = await db.select()
|
|
1118
|
+
* .from(users)
|
|
1119
|
+
* .innerJoin(pets, eq(users.id, pets.ownerId))
|
|
1120
|
+
*
|
|
1121
|
+
* // Select userId and petId
|
|
1122
|
+
* const usersIdsAndPetIds: { userId: number; petId: number; }[] = await db.select({
|
|
1123
|
+
* userId: users.id,
|
|
1124
|
+
* petId: pets.id,
|
|
1125
|
+
* })
|
|
1126
|
+
* .from(users)
|
|
1127
|
+
* .innerJoin(pets, eq(users.id, pets.ownerId))
|
|
1128
|
+
*
|
|
1129
|
+
* // Select userId and petId with use index hint
|
|
1130
|
+
* const usersIdsAndPetIds: { userId: number; petId: number | null; }[] = await db.select({
|
|
1131
|
+
* userId: users.id,
|
|
1132
|
+
* petId: pets.id,
|
|
1133
|
+
* })
|
|
1134
|
+
* .from(users)
|
|
1135
|
+
* .leftJoin(pets, eq(users.id, pets.ownerId), {
|
|
1136
|
+
* useIndex: ['pets_owner_id_index']
|
|
1137
|
+
* })
|
|
1138
|
+
* ```
|
|
1139
|
+
*/
|
|
1140
|
+
innerJoin = this.createJoin("inner", false);
|
|
1141
|
+
/**
|
|
1142
|
+
* Executes an `inner join lateral` operation, creating a new table by combining rows from two queries that have matching values.
|
|
1143
|
+
*
|
|
1144
|
+
* A `lateral` join allows the right-hand expression to refer to columns from the left-hand side.
|
|
1145
|
+
*
|
|
1146
|
+
* Calling this method retrieves rows that have corresponding entries in both joined tables. Rows without matching entries in either table are excluded, resulting in a table that includes only matching pairs.
|
|
1147
|
+
*
|
|
1148
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#inner-join-lateral}
|
|
1149
|
+
*
|
|
1150
|
+
* @param table the subquery to join.
|
|
1151
|
+
* @param on the `on` clause.
|
|
1152
|
+
*/
|
|
1153
|
+
innerJoinLateral = this.createJoin("inner", true);
|
|
1154
|
+
/**
|
|
1155
|
+
* Executes a `cross join` operation by combining rows from two tables into a new table.
|
|
1156
|
+
*
|
|
1157
|
+
* Calling this method retrieves all rows from both main and joined tables, merging all rows from each table.
|
|
1158
|
+
*
|
|
1159
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#cross-join}
|
|
1160
|
+
*
|
|
1161
|
+
* @param table the table to join.
|
|
1162
|
+
* @param onIndex index hint.
|
|
1163
|
+
*
|
|
1164
|
+
* @example
|
|
1165
|
+
*
|
|
1166
|
+
* ```ts
|
|
1167
|
+
* // Select all users, each user with every pet
|
|
1168
|
+
* const usersWithPets: { user: User; pets: Pet; }[] = await db.select()
|
|
1169
|
+
* .from(users)
|
|
1170
|
+
* .crossJoin(pets)
|
|
1171
|
+
*
|
|
1172
|
+
* // Select userId and petId
|
|
1173
|
+
* const usersIdsAndPetIds: { userId: number; petId: number; }[] = await db.select({
|
|
1174
|
+
* userId: users.id,
|
|
1175
|
+
* petId: pets.id,
|
|
1176
|
+
* })
|
|
1177
|
+
* .from(users)
|
|
1178
|
+
* .crossJoin(pets)
|
|
1179
|
+
*
|
|
1180
|
+
* // Select userId and petId with use index hint
|
|
1181
|
+
* const usersIdsAndPetIds: { userId: number; petId: number; }[] = await db.select({
|
|
1182
|
+
* userId: users.id,
|
|
1183
|
+
* petId: pets.id,
|
|
1184
|
+
* })
|
|
1185
|
+
* .from(users)
|
|
1186
|
+
* .crossJoin(pets, {
|
|
1187
|
+
* useIndex: ['pets_owner_id_index']
|
|
1188
|
+
* })
|
|
1189
|
+
* ```
|
|
1190
|
+
*/
|
|
1191
|
+
crossJoin = this.createJoin("cross", false);
|
|
1192
|
+
/**
|
|
1193
|
+
* Executes a `cross join lateral` operation by combining rows from two queries into a new table.
|
|
1194
|
+
*
|
|
1195
|
+
* A `lateral` join allows the right-hand expression to refer to columns from the left-hand side.
|
|
1196
|
+
*
|
|
1197
|
+
* Calling this method retrieves all rows from both main and joined queries, merging all rows from each query.
|
|
1198
|
+
*
|
|
1199
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#cross-join-lateral}
|
|
1200
|
+
*
|
|
1201
|
+
* @param table the query to join.
|
|
1202
|
+
*/
|
|
1203
|
+
crossJoinLateral = this.createJoin("cross", true);
|
|
1204
|
+
createSetOperator(type, isAll) {
|
|
1205
|
+
return (rightSelection) => {
|
|
1206
|
+
const rightSelect = typeof rightSelection === "function" ? rightSelection(getMySqlSetOperators()) : rightSelection;
|
|
1207
|
+
if (!haveSameKeys(this.getSelectedFields(), rightSelect.getSelectedFields())) throw new Error("Set operator error (union / intersect / except): selected fields are not the same or are in a different order");
|
|
1208
|
+
this.config.setOperators.push({
|
|
1209
|
+
type,
|
|
1210
|
+
isAll,
|
|
1211
|
+
rightSelect
|
|
1212
|
+
});
|
|
1213
|
+
return this;
|
|
1214
|
+
};
|
|
1215
|
+
}
|
|
1216
|
+
/**
|
|
1217
|
+
* Adds `union` set operator to the query.
|
|
1218
|
+
*
|
|
1219
|
+
* Calling this method will combine the result sets of the `select` statements and remove any duplicate rows that appear across them.
|
|
1220
|
+
*
|
|
1221
|
+
* See docs: {@link https://orm.drizzle.team/docs/set-operations#union}
|
|
1222
|
+
*
|
|
1223
|
+
* @example
|
|
1224
|
+
*
|
|
1225
|
+
* ```ts
|
|
1226
|
+
* // Select all unique names from customers and users tables
|
|
1227
|
+
* await db.select({ name: users.name })
|
|
1228
|
+
* .from(users)
|
|
1229
|
+
* .union(
|
|
1230
|
+
* db.select({ name: customers.name }).from(customers)
|
|
1231
|
+
* );
|
|
1232
|
+
* // or
|
|
1233
|
+
* import { union } from 'drizzle-orm/mysql-core'
|
|
1234
|
+
*
|
|
1235
|
+
* await union(
|
|
1236
|
+
* db.select({ name: users.name }).from(users),
|
|
1237
|
+
* db.select({ name: customers.name }).from(customers)
|
|
1238
|
+
* );
|
|
1239
|
+
* ```
|
|
1240
|
+
*/
|
|
1241
|
+
union = this.createSetOperator("union", false);
|
|
1242
|
+
/**
|
|
1243
|
+
* Adds `union all` set operator to the query.
|
|
1244
|
+
*
|
|
1245
|
+
* Calling this method will combine the result-set of the `select` statements and keep all duplicate rows that appear across them.
|
|
1246
|
+
*
|
|
1247
|
+
* See docs: {@link https://orm.drizzle.team/docs/set-operations#union-all}
|
|
1248
|
+
*
|
|
1249
|
+
* @example
|
|
1250
|
+
*
|
|
1251
|
+
* ```ts
|
|
1252
|
+
* // Select all transaction ids from both online and in-store sales
|
|
1253
|
+
* await db.select({ transaction: onlineSales.transactionId })
|
|
1254
|
+
* .from(onlineSales)
|
|
1255
|
+
* .unionAll(
|
|
1256
|
+
* db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales)
|
|
1257
|
+
* );
|
|
1258
|
+
* // or
|
|
1259
|
+
* import { unionAll } from 'drizzle-orm/mysql-core'
|
|
1260
|
+
*
|
|
1261
|
+
* await unionAll(
|
|
1262
|
+
* db.select({ transaction: onlineSales.transactionId }).from(onlineSales),
|
|
1263
|
+
* db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales)
|
|
1264
|
+
* );
|
|
1265
|
+
* ```
|
|
1266
|
+
*/
|
|
1267
|
+
unionAll = this.createSetOperator("union", true);
|
|
1268
|
+
/**
|
|
1269
|
+
* Adds `intersect` set operator to the query.
|
|
1270
|
+
*
|
|
1271
|
+
* Calling this method will retain only the rows that are present in both result sets and eliminate duplicates.
|
|
1272
|
+
*
|
|
1273
|
+
* See docs: {@link https://orm.drizzle.team/docs/set-operations#intersect}
|
|
1274
|
+
*
|
|
1275
|
+
* @example
|
|
1276
|
+
*
|
|
1277
|
+
* ```ts
|
|
1278
|
+
* // Select course names that are offered in both departments A and B
|
|
1279
|
+
* await db.select({ courseName: depA.courseName })
|
|
1280
|
+
* .from(depA)
|
|
1281
|
+
* .intersect(
|
|
1282
|
+
* db.select({ courseName: depB.courseName }).from(depB)
|
|
1283
|
+
* );
|
|
1284
|
+
* // or
|
|
1285
|
+
* import { intersect } from 'drizzle-orm/mysql-core'
|
|
1286
|
+
*
|
|
1287
|
+
* await intersect(
|
|
1288
|
+
* db.select({ courseName: depA.courseName }).from(depA),
|
|
1289
|
+
* db.select({ courseName: depB.courseName }).from(depB)
|
|
1290
|
+
* );
|
|
1291
|
+
* ```
|
|
1292
|
+
*/
|
|
1293
|
+
intersect = this.createSetOperator("intersect", false);
|
|
1294
|
+
/**
|
|
1295
|
+
* Adds `intersect all` set operator to the query.
|
|
1296
|
+
*
|
|
1297
|
+
* Calling this method will retain only the rows that are present in both result sets including all duplicates.
|
|
1298
|
+
*
|
|
1299
|
+
* See docs: {@link https://orm.drizzle.team/docs/set-operations#intersect-all}
|
|
1300
|
+
*
|
|
1301
|
+
* @example
|
|
1302
|
+
*
|
|
1303
|
+
* ```ts
|
|
1304
|
+
* // Select all products and quantities that are ordered by both regular and VIP customers
|
|
1305
|
+
* await db.select({
|
|
1306
|
+
* productId: regularCustomerOrders.productId,
|
|
1307
|
+
* quantityOrdered: regularCustomerOrders.quantityOrdered
|
|
1308
|
+
* })
|
|
1309
|
+
* .from(regularCustomerOrders)
|
|
1310
|
+
* .intersectAll(
|
|
1311
|
+
* db.select({
|
|
1312
|
+
* productId: vipCustomerOrders.productId,
|
|
1313
|
+
* quantityOrdered: vipCustomerOrders.quantityOrdered
|
|
1314
|
+
* })
|
|
1315
|
+
* .from(vipCustomerOrders)
|
|
1316
|
+
* );
|
|
1317
|
+
* // or
|
|
1318
|
+
* import { intersectAll } from 'drizzle-orm/mysql-core'
|
|
1319
|
+
*
|
|
1320
|
+
* await intersectAll(
|
|
1321
|
+
* db.select({
|
|
1322
|
+
* productId: regularCustomerOrders.productId,
|
|
1323
|
+
* quantityOrdered: regularCustomerOrders.quantityOrdered
|
|
1324
|
+
* })
|
|
1325
|
+
* .from(regularCustomerOrders),
|
|
1326
|
+
* db.select({
|
|
1327
|
+
* productId: vipCustomerOrders.productId,
|
|
1328
|
+
* quantityOrdered: vipCustomerOrders.quantityOrdered
|
|
1329
|
+
* })
|
|
1330
|
+
* .from(vipCustomerOrders)
|
|
1331
|
+
* );
|
|
1332
|
+
* ```
|
|
1333
|
+
*/
|
|
1334
|
+
intersectAll = this.createSetOperator("intersect", true);
|
|
1335
|
+
/**
|
|
1336
|
+
* Adds `except` set operator to the query.
|
|
1337
|
+
*
|
|
1338
|
+
* Calling this method will retrieve all unique rows from the left query, except for the rows that are present in the result set of the right query.
|
|
1339
|
+
*
|
|
1340
|
+
* See docs: {@link https://orm.drizzle.team/docs/set-operations#except}
|
|
1341
|
+
*
|
|
1342
|
+
* @example
|
|
1343
|
+
*
|
|
1344
|
+
* ```ts
|
|
1345
|
+
* // Select all courses offered in department A but not in department B
|
|
1346
|
+
* await db.select({ courseName: depA.courseName })
|
|
1347
|
+
* .from(depA)
|
|
1348
|
+
* .except(
|
|
1349
|
+
* db.select({ courseName: depB.courseName }).from(depB)
|
|
1350
|
+
* );
|
|
1351
|
+
* // or
|
|
1352
|
+
* import { except } from 'drizzle-orm/mysql-core'
|
|
1353
|
+
*
|
|
1354
|
+
* await except(
|
|
1355
|
+
* db.select({ courseName: depA.courseName }).from(depA),
|
|
1356
|
+
* db.select({ courseName: depB.courseName }).from(depB)
|
|
1357
|
+
* );
|
|
1358
|
+
* ```
|
|
1359
|
+
*/
|
|
1360
|
+
except = this.createSetOperator("except", false);
|
|
1361
|
+
/**
|
|
1362
|
+
* Adds `except all` set operator to the query.
|
|
1363
|
+
*
|
|
1364
|
+
* Calling this method will retrieve all rows from the left query, except for the rows that are present in the result set of the right query.
|
|
1365
|
+
*
|
|
1366
|
+
* See docs: {@link https://orm.drizzle.team/docs/set-operations#except-all}
|
|
1367
|
+
*
|
|
1368
|
+
* @example
|
|
1369
|
+
*
|
|
1370
|
+
* ```ts
|
|
1371
|
+
* // Select all products that are ordered by regular customers but not by VIP customers
|
|
1372
|
+
* await db.select({
|
|
1373
|
+
* productId: regularCustomerOrders.productId,
|
|
1374
|
+
* quantityOrdered: regularCustomerOrders.quantityOrdered,
|
|
1375
|
+
* })
|
|
1376
|
+
* .from(regularCustomerOrders)
|
|
1377
|
+
* .exceptAll(
|
|
1378
|
+
* db.select({
|
|
1379
|
+
* productId: vipCustomerOrders.productId,
|
|
1380
|
+
* quantityOrdered: vipCustomerOrders.quantityOrdered,
|
|
1381
|
+
* })
|
|
1382
|
+
* .from(vipCustomerOrders)
|
|
1383
|
+
* );
|
|
1384
|
+
* // or
|
|
1385
|
+
* import { exceptAll } from 'drizzle-orm/mysql-core'
|
|
1386
|
+
*
|
|
1387
|
+
* await exceptAll(
|
|
1388
|
+
* db.select({
|
|
1389
|
+
* productId: regularCustomerOrders.productId,
|
|
1390
|
+
* quantityOrdered: regularCustomerOrders.quantityOrdered
|
|
1391
|
+
* })
|
|
1392
|
+
* .from(regularCustomerOrders),
|
|
1393
|
+
* db.select({
|
|
1394
|
+
* productId: vipCustomerOrders.productId,
|
|
1395
|
+
* quantityOrdered: vipCustomerOrders.quantityOrdered
|
|
1396
|
+
* })
|
|
1397
|
+
* .from(vipCustomerOrders)
|
|
1398
|
+
* );
|
|
1399
|
+
* ```
|
|
1400
|
+
*/
|
|
1401
|
+
exceptAll = this.createSetOperator("except", true);
|
|
1402
|
+
/** @internal */
|
|
1403
|
+
addSetOperators(setOperators) {
|
|
1404
|
+
this.config.setOperators.push(...setOperators);
|
|
1405
|
+
return this;
|
|
1406
|
+
}
|
|
1407
|
+
/**
|
|
1408
|
+
* Adds a `where` clause to the query.
|
|
1409
|
+
*
|
|
1410
|
+
* Calling this method will select only those rows that fulfill a specified condition.
|
|
1411
|
+
*
|
|
1412
|
+
* See docs: {@link https://orm.drizzle.team/docs/select#filtering}
|
|
1413
|
+
*
|
|
1414
|
+
* @param where the `where` clause.
|
|
1415
|
+
*
|
|
1416
|
+
* @example
|
|
1417
|
+
* You can use conditional operators and `sql function` to filter the rows to be selected.
|
|
1418
|
+
*
|
|
1419
|
+
* ```ts
|
|
1420
|
+
* // Select all cars with green color
|
|
1421
|
+
* await db.select().from(cars).where(eq(cars.color, 'green'));
|
|
1422
|
+
* // or
|
|
1423
|
+
* await db.select().from(cars).where(sql`${cars.color} = 'green'`)
|
|
1424
|
+
* ```
|
|
1425
|
+
*
|
|
1426
|
+
* You can logically combine conditional operators with `and()` and `or()` operators:
|
|
1427
|
+
*
|
|
1428
|
+
* ```ts
|
|
1429
|
+
* // Select all BMW cars with a green color
|
|
1430
|
+
* await db.select().from(cars).where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
|
|
1431
|
+
*
|
|
1432
|
+
* // Select all cars with the green or blue color
|
|
1433
|
+
* await db.select().from(cars).where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
|
|
1434
|
+
* ```
|
|
1435
|
+
*/
|
|
1436
|
+
where(where) {
|
|
1437
|
+
if (typeof where === "function") where = where(new Proxy(this.config.fields, new SelectionProxyHandler({
|
|
1438
|
+
sqlAliasedBehavior: "sql",
|
|
1439
|
+
sqlBehavior: "sql"
|
|
1440
|
+
})));
|
|
1441
|
+
this.config.where = where;
|
|
1442
|
+
return this;
|
|
1443
|
+
}
|
|
1444
|
+
/**
|
|
1445
|
+
* Adds a `having` clause to the query.
|
|
1446
|
+
*
|
|
1447
|
+
* Calling this method will select only those rows that fulfill a specified condition. It is typically used with aggregate functions to filter the aggregated data based on a specified condition.
|
|
1448
|
+
*
|
|
1449
|
+
* See docs: {@link https://orm.drizzle.team/docs/select#aggregations}
|
|
1450
|
+
*
|
|
1451
|
+
* @param having the `having` clause.
|
|
1452
|
+
*
|
|
1453
|
+
* @example
|
|
1454
|
+
*
|
|
1455
|
+
* ```ts
|
|
1456
|
+
* // Select all brands with more than one car
|
|
1457
|
+
* await db.select({
|
|
1458
|
+
* brand: cars.brand,
|
|
1459
|
+
* count: sql<number>`cast(count(${cars.id}) as int)`,
|
|
1460
|
+
* })
|
|
1461
|
+
* .from(cars)
|
|
1462
|
+
* .groupBy(cars.brand)
|
|
1463
|
+
* .having(({ count }) => gt(count, 1));
|
|
1464
|
+
* ```
|
|
1465
|
+
*/
|
|
1466
|
+
having(having) {
|
|
1467
|
+
if (typeof having === "function") having = having(new Proxy(this.config.fields, new SelectionProxyHandler({
|
|
1468
|
+
sqlAliasedBehavior: "sql",
|
|
1469
|
+
sqlBehavior: "sql"
|
|
1470
|
+
})));
|
|
1471
|
+
this.config.having = having;
|
|
1472
|
+
return this;
|
|
1473
|
+
}
|
|
1474
|
+
groupBy(...columns) {
|
|
1475
|
+
if (typeof columns[0] === "function") {
|
|
1476
|
+
const groupBy = columns[0](new Proxy(this.config.fields, new SelectionProxyHandler({
|
|
1477
|
+
sqlAliasedBehavior: "alias",
|
|
1478
|
+
sqlBehavior: "sql"
|
|
1479
|
+
})));
|
|
1480
|
+
this.config.groupBy = Array.isArray(groupBy) ? groupBy : [groupBy];
|
|
1481
|
+
} else this.config.groupBy = columns;
|
|
1482
|
+
return this;
|
|
1483
|
+
}
|
|
1484
|
+
orderBy(...columns) {
|
|
1485
|
+
if (typeof columns[0] === "function") {
|
|
1486
|
+
const orderBy = columns[0](new Proxy(this.config.fields, new SelectionProxyHandler({
|
|
1487
|
+
sqlAliasedBehavior: "alias",
|
|
1488
|
+
sqlBehavior: "sql"
|
|
1489
|
+
})));
|
|
1490
|
+
const orderByArray = Array.isArray(orderBy) ? orderBy : [orderBy];
|
|
1491
|
+
if (this.config.setOperators.length > 0) this.config.setOperators.at(-1).orderBy = orderByArray;
|
|
1492
|
+
else this.config.orderBy = orderByArray;
|
|
1493
|
+
} else {
|
|
1494
|
+
const orderByArray = columns;
|
|
1495
|
+
if (this.config.setOperators.length > 0) this.config.setOperators.at(-1).orderBy = orderByArray;
|
|
1496
|
+
else this.config.orderBy = orderByArray;
|
|
1497
|
+
}
|
|
1498
|
+
return this;
|
|
1499
|
+
}
|
|
1500
|
+
/**
|
|
1501
|
+
* Adds a `limit` clause to the query.
|
|
1502
|
+
*
|
|
1503
|
+
* Calling this method will set the maximum number of rows that will be returned by this query.
|
|
1504
|
+
*
|
|
1505
|
+
* See docs: {@link https://orm.drizzle.team/docs/select#limit--offset}
|
|
1506
|
+
*
|
|
1507
|
+
* @param limit the `limit` clause.
|
|
1508
|
+
*
|
|
1509
|
+
* @example
|
|
1510
|
+
*
|
|
1511
|
+
* ```ts
|
|
1512
|
+
* // Get the first 10 people from this query.
|
|
1513
|
+
* await db.select().from(people).limit(10);
|
|
1514
|
+
* ```
|
|
1515
|
+
*/
|
|
1516
|
+
limit(limit) {
|
|
1517
|
+
if (this.config.setOperators.length > 0) this.config.setOperators.at(-1).limit = limit;
|
|
1518
|
+
else this.config.limit = limit;
|
|
1519
|
+
return this;
|
|
1520
|
+
}
|
|
1521
|
+
/**
|
|
1522
|
+
* Adds an `offset` clause to the query.
|
|
1523
|
+
*
|
|
1524
|
+
* Calling this method will skip a number of rows when returning results from this query.
|
|
1525
|
+
*
|
|
1526
|
+
* See docs: {@link https://orm.drizzle.team/docs/select#limit--offset}
|
|
1527
|
+
*
|
|
1528
|
+
* @param offset the `offset` clause.
|
|
1529
|
+
*
|
|
1530
|
+
* @example
|
|
1531
|
+
*
|
|
1532
|
+
* ```ts
|
|
1533
|
+
* // Get the 10th-20th people from this query.
|
|
1534
|
+
* await db.select().from(people).offset(10).limit(10);
|
|
1535
|
+
* ```
|
|
1536
|
+
*/
|
|
1537
|
+
offset(offset) {
|
|
1538
|
+
if (this.config.setOperators.length > 0) this.config.setOperators.at(-1).offset = offset;
|
|
1539
|
+
else this.config.offset = offset;
|
|
1540
|
+
return this;
|
|
1541
|
+
}
|
|
1542
|
+
/**
|
|
1543
|
+
* Adds a `for` clause to the query.
|
|
1544
|
+
*
|
|
1545
|
+
* Calling this method will specify a lock strength for this query that controls how strictly it acquires exclusive access to the rows being queried.
|
|
1546
|
+
*
|
|
1547
|
+
* See docs: {@link https://dev.mysql.com/doc/refman/8.0/en/innodb-locking-reads.html}
|
|
1548
|
+
*
|
|
1549
|
+
* @param strength the lock strength.
|
|
1550
|
+
* @param config the lock configuration.
|
|
1551
|
+
*/
|
|
1552
|
+
for(strength, config = {}) {
|
|
1553
|
+
this.config.lockingClause = {
|
|
1554
|
+
strength,
|
|
1555
|
+
config
|
|
1556
|
+
};
|
|
1557
|
+
return this;
|
|
1558
|
+
}
|
|
1559
|
+
/** @internal */
|
|
1560
|
+
getSQL() {
|
|
1561
|
+
return this.dialect.buildSelectQuery(this.config);
|
|
1562
|
+
}
|
|
1563
|
+
toSQL() {
|
|
1564
|
+
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
|
|
1565
|
+
return rest;
|
|
1566
|
+
}
|
|
1567
|
+
as(alias) {
|
|
1568
|
+
const usedTables = [];
|
|
1569
|
+
usedTables.push(...extractUsedTable(this.config.table));
|
|
1570
|
+
if (this.config.joins) for (const it of this.config.joins) usedTables.push(...extractUsedTable(it.table));
|
|
1571
|
+
return new Proxy(new Subquery(this.getSQL(), this.config.fields, alias, false, [...new Set(usedTables)]), new SelectionProxyHandler({
|
|
1572
|
+
alias,
|
|
1573
|
+
sqlAliasedBehavior: "alias",
|
|
1574
|
+
sqlBehavior: "error"
|
|
1575
|
+
}));
|
|
1576
|
+
}
|
|
1577
|
+
/** @internal */
|
|
1578
|
+
getSelectedFields() {
|
|
1579
|
+
return new Proxy(this.config.fields, new SelectionProxyHandler({
|
|
1580
|
+
alias: this.tableName,
|
|
1581
|
+
sqlAliasedBehavior: "alias",
|
|
1582
|
+
sqlBehavior: "error"
|
|
1583
|
+
}));
|
|
1584
|
+
}
|
|
1585
|
+
$dynamic() {
|
|
1586
|
+
return this;
|
|
1587
|
+
}
|
|
1588
|
+
$withCache(config) {
|
|
1589
|
+
this.cacheConfig = config === void 0 ? {
|
|
1590
|
+
config: {},
|
|
1591
|
+
enable: true,
|
|
1592
|
+
autoInvalidate: true
|
|
1593
|
+
} : config === false ? { enable: false } : {
|
|
1594
|
+
enable: true,
|
|
1595
|
+
autoInvalidate: true,
|
|
1596
|
+
...config
|
|
1597
|
+
};
|
|
1598
|
+
return this;
|
|
1599
|
+
}
|
|
1600
|
+
};
|
|
1601
|
+
var MySqlSelectBase = class extends MySqlSelectQueryBuilderBase {
|
|
1602
|
+
static [entityKind] = "MySqlSelect";
|
|
1603
|
+
prepare() {
|
|
1604
|
+
if (!this.session) throw new Error("Cannot execute a query on a query builder. Please use a database instance instead.");
|
|
1605
|
+
const fieldsList = orderSelectedFields(this.config.fields);
|
|
1606
|
+
const query = this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), fieldsList, void 0, void 0, void 0, {
|
|
1607
|
+
type: "select",
|
|
1608
|
+
tables: [...this.usedTables]
|
|
1609
|
+
}, this.cacheConfig);
|
|
1610
|
+
query.joinsNotNullableMap = this.joinsNotNullableMap;
|
|
1611
|
+
return query;
|
|
1612
|
+
}
|
|
1613
|
+
execute = (placeholderValues) => {
|
|
1614
|
+
return this.prepare().execute(placeholderValues);
|
|
1615
|
+
};
|
|
1616
|
+
createIterator = () => {
|
|
1617
|
+
const self = this;
|
|
1618
|
+
return async function* (placeholderValues) {
|
|
1619
|
+
yield* self.prepare().iterator(placeholderValues);
|
|
1620
|
+
};
|
|
1621
|
+
};
|
|
1622
|
+
iterator = this.createIterator();
|
|
1623
|
+
};
|
|
1624
|
+
applyMixins(MySqlSelectBase, [QueryPromise]);
|
|
1625
|
+
function createSetOperator(type, isAll) {
|
|
1626
|
+
return (leftSelect, rightSelect, ...restSelects) => {
|
|
1627
|
+
const setOperators = [rightSelect, ...restSelects].map((select) => ({
|
|
1628
|
+
type,
|
|
1629
|
+
isAll,
|
|
1630
|
+
rightSelect: select
|
|
1631
|
+
}));
|
|
1632
|
+
for (const setOperator of setOperators) if (!haveSameKeys(leftSelect.getSelectedFields(), setOperator.rightSelect.getSelectedFields())) throw new Error("Set operator error (union / intersect / except): selected fields are not the same or are in a different order");
|
|
1633
|
+
return leftSelect.addSetOperators(setOperators);
|
|
1634
|
+
};
|
|
1635
|
+
}
|
|
1636
|
+
const getMySqlSetOperators = () => ({
|
|
1637
|
+
union,
|
|
1638
|
+
unionAll,
|
|
1639
|
+
intersect,
|
|
1640
|
+
intersectAll,
|
|
1641
|
+
except,
|
|
1642
|
+
exceptAll
|
|
1643
|
+
});
|
|
1644
|
+
const union = createSetOperator("union", false);
|
|
1645
|
+
const unionAll = createSetOperator("union", true);
|
|
1646
|
+
const intersect = createSetOperator("intersect", false);
|
|
1647
|
+
const intersectAll = createSetOperator("intersect", true);
|
|
1648
|
+
const except = createSetOperator("except", false);
|
|
1649
|
+
const exceptAll = createSetOperator("except", true);
|
|
1650
|
+
var QueryBuilder = class {
|
|
1651
|
+
static [entityKind] = "MySqlQueryBuilder";
|
|
1652
|
+
dialect;
|
|
1653
|
+
dialectConfig;
|
|
1654
|
+
constructor(dialect) {
|
|
1655
|
+
this.dialect = is(dialect, MySqlDialect) ? dialect : void 0;
|
|
1656
|
+
this.dialectConfig = is(dialect, MySqlDialect) ? void 0 : dialect;
|
|
1657
|
+
}
|
|
1658
|
+
$with = (alias, selection) => {
|
|
1659
|
+
const queryBuilder = this;
|
|
1660
|
+
const as = (qb) => {
|
|
1661
|
+
if (typeof qb === "function") qb = qb(queryBuilder);
|
|
1662
|
+
return new Proxy(new WithSubquery(qb.getSQL(), selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}), alias, true), new SelectionProxyHandler({
|
|
1663
|
+
alias,
|
|
1664
|
+
sqlAliasedBehavior: "alias",
|
|
1665
|
+
sqlBehavior: "error"
|
|
1666
|
+
}));
|
|
1667
|
+
};
|
|
1668
|
+
return { as };
|
|
1669
|
+
};
|
|
1670
|
+
with(...queries) {
|
|
1671
|
+
const self = this;
|
|
1672
|
+
function select(fields) {
|
|
1673
|
+
return new MySqlSelectBuilder({
|
|
1674
|
+
fields: fields ?? void 0,
|
|
1675
|
+
session: void 0,
|
|
1676
|
+
dialect: self.getDialect(),
|
|
1677
|
+
withList: queries
|
|
1678
|
+
});
|
|
1679
|
+
}
|
|
1680
|
+
function selectDistinct(fields) {
|
|
1681
|
+
return new MySqlSelectBuilder({
|
|
1682
|
+
fields: fields ?? void 0,
|
|
1683
|
+
session: void 0,
|
|
1684
|
+
dialect: self.getDialect(),
|
|
1685
|
+
withList: queries,
|
|
1686
|
+
distinct: true
|
|
1687
|
+
});
|
|
1688
|
+
}
|
|
1689
|
+
return {
|
|
1690
|
+
select,
|
|
1691
|
+
selectDistinct
|
|
1692
|
+
};
|
|
1693
|
+
}
|
|
1694
|
+
select(fields) {
|
|
1695
|
+
return new MySqlSelectBuilder({
|
|
1696
|
+
fields: fields ?? void 0,
|
|
1697
|
+
session: void 0,
|
|
1698
|
+
dialect: this.getDialect()
|
|
1699
|
+
});
|
|
1700
|
+
}
|
|
1701
|
+
selectDistinct(fields) {
|
|
1702
|
+
return new MySqlSelectBuilder({
|
|
1703
|
+
fields: fields ?? void 0,
|
|
1704
|
+
session: void 0,
|
|
1705
|
+
dialect: this.getDialect(),
|
|
1706
|
+
distinct: true
|
|
1707
|
+
});
|
|
1708
|
+
}
|
|
1709
|
+
getDialect() {
|
|
1710
|
+
if (!this.dialect) this.dialect = new MySqlDialect(this.dialectConfig);
|
|
1711
|
+
return this.dialect;
|
|
1712
|
+
}
|
|
1713
|
+
};
|
|
1714
|
+
//#endregion
|
|
1715
|
+
export { MySqlViewConfig as a, getTableConfig as c, MySqlViewBase as i, MySqlSelectBuilder as n, QueryBuilder as o, MySqlTable as r, extractUsedTable as s, MySqlDialect as t };
|