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,570 @@
|
|
|
1
|
+
import { C as sql, b as is, d as Table, f as TableName, g as entityKind, 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, F as orderSelectedFields, P as normalizeRelation, S as getOperators, _ as and, a as Many, b as eq, c as One, g as aliasedTableColumn, h as aliasedTable, k as mapColumnsInAliasedSQLToAlias, r as DrizzleError, t as CasingCache } from "./logger-TIsK7375-CLNNa25D.js";
|
|
3
|
+
//#region ../db/dist/dialect-pBb6Cg_F.js
|
|
4
|
+
function uniqueKeyName(table, columns) {
|
|
5
|
+
return `${table[TableName]}_${columns.join("_")}_unique`;
|
|
6
|
+
}
|
|
7
|
+
var UniqueConstraintBuilder = class {
|
|
8
|
+
constructor(columns, name) {
|
|
9
|
+
this.name = name;
|
|
10
|
+
this.columns = columns;
|
|
11
|
+
}
|
|
12
|
+
static [entityKind] = "SingleStoreUniqueConstraintBuilder";
|
|
13
|
+
/** @internal */
|
|
14
|
+
columns;
|
|
15
|
+
/** @internal */
|
|
16
|
+
build(table) {
|
|
17
|
+
return new UniqueConstraint(table, this.columns, this.name);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
var UniqueConstraint = class {
|
|
21
|
+
constructor(table, columns, name) {
|
|
22
|
+
this.table = table;
|
|
23
|
+
this.columns = columns;
|
|
24
|
+
this.name = name ?? uniqueKeyName(this.table, this.columns.map((column) => column.name));
|
|
25
|
+
}
|
|
26
|
+
static [entityKind] = "SingleStoreUniqueConstraint";
|
|
27
|
+
columns;
|
|
28
|
+
name;
|
|
29
|
+
nullsNotDistinct = false;
|
|
30
|
+
getName() {
|
|
31
|
+
return this.name;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var SingleStoreColumn = class extends Column {
|
|
35
|
+
constructor(table, config) {
|
|
36
|
+
if (!config.uniqueName) config.uniqueName = uniqueKeyName(table, [config.name]);
|
|
37
|
+
super(table, config);
|
|
38
|
+
this.table = table;
|
|
39
|
+
}
|
|
40
|
+
static [entityKind] = "SingleStoreColumn";
|
|
41
|
+
};
|
|
42
|
+
var IndexBuilder = class {
|
|
43
|
+
static [entityKind] = "SingleStoreIndexBuilder";
|
|
44
|
+
/** @internal */
|
|
45
|
+
config;
|
|
46
|
+
constructor(name, columns, unique) {
|
|
47
|
+
this.config = {
|
|
48
|
+
name,
|
|
49
|
+
columns,
|
|
50
|
+
unique
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
using(using) {
|
|
54
|
+
this.config.using = using;
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
algorithm(algorithm) {
|
|
58
|
+
this.config.algorithm = algorithm;
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
lock(lock) {
|
|
62
|
+
this.config.lock = lock;
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
/** @internal */
|
|
66
|
+
build(table) {
|
|
67
|
+
return new Index(this.config, table);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
var Index = class {
|
|
71
|
+
static [entityKind] = "SingleStoreIndex";
|
|
72
|
+
config;
|
|
73
|
+
constructor(config, table) {
|
|
74
|
+
this.config = {
|
|
75
|
+
...config,
|
|
76
|
+
table
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
var SingleStoreTable = class extends Table {
|
|
81
|
+
static [entityKind] = "SingleStoreTable";
|
|
82
|
+
/** @internal */
|
|
83
|
+
static Symbol = Object.assign({}, Table.Symbol, {});
|
|
84
|
+
/** @internal */
|
|
85
|
+
[Table.Symbol.Columns];
|
|
86
|
+
/** @internal */
|
|
87
|
+
[Table.Symbol.ExtraConfigBuilder] = void 0;
|
|
88
|
+
};
|
|
89
|
+
var PrimaryKeyBuilder = class {
|
|
90
|
+
static [entityKind] = "SingleStorePrimaryKeyBuilder";
|
|
91
|
+
/** @internal */
|
|
92
|
+
columns;
|
|
93
|
+
/** @internal */
|
|
94
|
+
name;
|
|
95
|
+
constructor(columns, name) {
|
|
96
|
+
this.columns = columns;
|
|
97
|
+
this.name = name;
|
|
98
|
+
}
|
|
99
|
+
/** @internal */
|
|
100
|
+
build(table) {
|
|
101
|
+
return new PrimaryKey(table, this.columns, this.name);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
var PrimaryKey = class {
|
|
105
|
+
constructor(table, columns, name) {
|
|
106
|
+
this.table = table;
|
|
107
|
+
this.columns = columns;
|
|
108
|
+
this.name = name;
|
|
109
|
+
}
|
|
110
|
+
static [entityKind] = "SingleStorePrimaryKey";
|
|
111
|
+
columns;
|
|
112
|
+
name;
|
|
113
|
+
getName() {
|
|
114
|
+
return this.name ?? `${this.table[SingleStoreTable.Symbol.Name]}_${this.columns.map((column) => column.name).join("_")}_pk`;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
function extractUsedTable(table) {
|
|
118
|
+
if (is(table, SingleStoreTable)) return [`${table[Table.Symbol.BaseName]}`];
|
|
119
|
+
if (is(table, Subquery)) return table._.usedTables ?? [];
|
|
120
|
+
if (is(table, SQL)) return table.usedTables ?? [];
|
|
121
|
+
return [];
|
|
122
|
+
}
|
|
123
|
+
function getTableConfig(table) {
|
|
124
|
+
const columns = Object.values(table[SingleStoreTable.Symbol.Columns]);
|
|
125
|
+
const indexes = [];
|
|
126
|
+
const primaryKeys = [];
|
|
127
|
+
const uniqueConstraints = [];
|
|
128
|
+
const name = table[Table.Symbol.Name];
|
|
129
|
+
const schema = table[Table.Symbol.Schema];
|
|
130
|
+
const baseName = table[Table.Symbol.BaseName];
|
|
131
|
+
const extraConfigBuilder = table[SingleStoreTable.Symbol.ExtraConfigBuilder];
|
|
132
|
+
if (extraConfigBuilder !== void 0) {
|
|
133
|
+
const extraConfig = extraConfigBuilder(table[SingleStoreTable.Symbol.Columns]);
|
|
134
|
+
const extraValues = Array.isArray(extraConfig) ? extraConfig.flat(1) : Object.values(extraConfig);
|
|
135
|
+
for (const builder of Object.values(extraValues)) if (is(builder, IndexBuilder)) indexes.push(builder.build(table));
|
|
136
|
+
else if (is(builder, UniqueConstraintBuilder)) uniqueConstraints.push(builder.build(table));
|
|
137
|
+
else if (is(builder, PrimaryKeyBuilder)) primaryKeys.push(builder.build(table));
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
columns,
|
|
141
|
+
indexes,
|
|
142
|
+
primaryKeys,
|
|
143
|
+
uniqueConstraints,
|
|
144
|
+
name,
|
|
145
|
+
schema,
|
|
146
|
+
baseName
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
var SingleStoreDialect = class {
|
|
150
|
+
static [entityKind] = "SingleStoreDialect";
|
|
151
|
+
/** @internal */
|
|
152
|
+
casing;
|
|
153
|
+
constructor(config) {
|
|
154
|
+
this.casing = new CasingCache(config?.casing);
|
|
155
|
+
}
|
|
156
|
+
async migrate(migrations, session, config) {
|
|
157
|
+
const migrationsTable = config.migrationsTable ?? "__drizzle_migrations";
|
|
158
|
+
const migrationTableCreate = sql`
|
|
159
|
+
create table if not exists ${sql.identifier(migrationsTable)} (
|
|
160
|
+
id serial primary key,
|
|
161
|
+
hash text not null,
|
|
162
|
+
created_at bigint
|
|
163
|
+
)
|
|
164
|
+
`;
|
|
165
|
+
await session.execute(migrationTableCreate);
|
|
166
|
+
const lastDbMigration = (await session.all(sql`select id, hash, created_at from ${sql.identifier(migrationsTable)} order by created_at desc limit 1`))[0];
|
|
167
|
+
await session.transaction(async (tx) => {
|
|
168
|
+
for (const migration of migrations) if (!lastDbMigration || Number(lastDbMigration.created_at) < migration.folderMillis) {
|
|
169
|
+
for (const stmt of migration.sql) await tx.execute(sql.raw(stmt));
|
|
170
|
+
await tx.execute(sql`insert into ${sql.identifier(migrationsTable)} (\`hash\`, \`created_at\`) values(${migration.hash}, ${migration.folderMillis})`);
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
escapeName(name) {
|
|
175
|
+
return `\`${name.replace(/`/g, "``")}\``;
|
|
176
|
+
}
|
|
177
|
+
escapeParam(_num) {
|
|
178
|
+
return `?`;
|
|
179
|
+
}
|
|
180
|
+
escapeString(str) {
|
|
181
|
+
return `'${str.replace(/'/g, "''")}'`;
|
|
182
|
+
}
|
|
183
|
+
buildWithCTE(queries) {
|
|
184
|
+
if (!queries?.length) return void 0;
|
|
185
|
+
const withSqlChunks = [sql`with `];
|
|
186
|
+
for (const [i, w] of queries.entries()) {
|
|
187
|
+
withSqlChunks.push(sql`${sql.identifier(w._.alias)} as (${w._.sql})`);
|
|
188
|
+
if (i < queries.length - 1) withSqlChunks.push(sql`, `);
|
|
189
|
+
}
|
|
190
|
+
withSqlChunks.push(sql` `);
|
|
191
|
+
return sql.join(withSqlChunks);
|
|
192
|
+
}
|
|
193
|
+
buildDeleteQuery({ table, where, returning, withList, limit, orderBy }) {
|
|
194
|
+
const withSql = this.buildWithCTE(withList);
|
|
195
|
+
const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
|
|
196
|
+
return sql`${withSql}delete from ${table}${where ? sql` where ${where}` : void 0}${this.buildOrderBy(orderBy)}${this.buildLimit(limit)}${returningSql}`;
|
|
197
|
+
}
|
|
198
|
+
buildUpdateSet(table, set) {
|
|
199
|
+
const tableColumns = table[Table.Symbol.Columns];
|
|
200
|
+
const columnNames = Object.keys(tableColumns).filter((colName) => set[colName] !== void 0 || tableColumns[colName]?.onUpdateFn !== void 0);
|
|
201
|
+
const setSize = columnNames.length;
|
|
202
|
+
return sql.join(columnNames.flatMap((colName, i) => {
|
|
203
|
+
const col = tableColumns[colName];
|
|
204
|
+
const onUpdateFnResult = col.onUpdateFn?.();
|
|
205
|
+
const value = set[colName] ?? (is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col));
|
|
206
|
+
const res = sql`${sql.identifier(this.casing.getColumnCasing(col))} = ${value}`;
|
|
207
|
+
if (i < setSize - 1) return [res, sql.raw(", ")];
|
|
208
|
+
return [res];
|
|
209
|
+
}));
|
|
210
|
+
}
|
|
211
|
+
buildUpdateQuery({ table, set, where, returning, withList, limit, orderBy }) {
|
|
212
|
+
const withSql = this.buildWithCTE(withList);
|
|
213
|
+
const setSql = this.buildUpdateSet(table, set);
|
|
214
|
+
const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
|
|
215
|
+
return sql`${withSql}update ${table} set ${setSql}${where ? sql` where ${where}` : void 0}${this.buildOrderBy(orderBy)}${this.buildLimit(limit)}${returningSql}`;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Builds selection SQL with provided fields/expressions
|
|
219
|
+
*
|
|
220
|
+
* Examples:
|
|
221
|
+
*
|
|
222
|
+
* `select <selection> from`
|
|
223
|
+
*
|
|
224
|
+
* `insert ... returning <selection>`
|
|
225
|
+
*
|
|
226
|
+
* If `isSingleTable` is true, then columns won't be prefixed with table name
|
|
227
|
+
*/
|
|
228
|
+
buildSelection(fields, { isSingleTable = false } = {}) {
|
|
229
|
+
const columnsLen = fields.length;
|
|
230
|
+
const chunks = fields.flatMap(({ field }, i) => {
|
|
231
|
+
const chunk = [];
|
|
232
|
+
if (is(field, SQL.Aliased) && field.isSelectionField) chunk.push(sql.identifier(field.fieldAlias));
|
|
233
|
+
else if (is(field, SQL.Aliased) || is(field, SQL)) {
|
|
234
|
+
const query = is(field, SQL.Aliased) ? field.sql : field;
|
|
235
|
+
if (isSingleTable) chunk.push(new SQL(query.queryChunks.map((c) => {
|
|
236
|
+
if (is(c, SingleStoreColumn)) return sql.identifier(this.casing.getColumnCasing(c));
|
|
237
|
+
return c;
|
|
238
|
+
})));
|
|
239
|
+
else chunk.push(query);
|
|
240
|
+
if (is(field, SQL.Aliased)) chunk.push(sql` as ${sql.identifier(field.fieldAlias)}`);
|
|
241
|
+
} else if (is(field, Column)) if (isSingleTable) chunk.push(sql.identifier(this.casing.getColumnCasing(field)));
|
|
242
|
+
else chunk.push(field);
|
|
243
|
+
else if (is(field, Subquery)) {
|
|
244
|
+
const entries = Object.entries(field._.selectedFields);
|
|
245
|
+
if (entries.length === 1) {
|
|
246
|
+
const entry = entries[0][1];
|
|
247
|
+
const fieldDecoder = is(entry, SQL) ? entry.decoder : is(entry, Column) ? { mapFromDriverValue: (v) => entry.mapFromDriverValue(v) } : entry.sql.decoder;
|
|
248
|
+
if (fieldDecoder) field._.sql.decoder = fieldDecoder;
|
|
249
|
+
}
|
|
250
|
+
chunk.push(field);
|
|
251
|
+
}
|
|
252
|
+
if (i < columnsLen - 1) chunk.push(sql`, `);
|
|
253
|
+
return chunk;
|
|
254
|
+
});
|
|
255
|
+
return sql.join(chunks);
|
|
256
|
+
}
|
|
257
|
+
buildLimit(limit) {
|
|
258
|
+
return typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql` limit ${limit}` : void 0;
|
|
259
|
+
}
|
|
260
|
+
buildOrderBy(orderBy) {
|
|
261
|
+
return orderBy && orderBy.length > 0 ? sql` order by ${sql.join(orderBy, sql`, `)}` : void 0;
|
|
262
|
+
}
|
|
263
|
+
buildSelectQuery({ withList, fields, fieldsFlat, where, having, table, joins, orderBy, groupBy, limit, offset, lockingClause, distinct, setOperators }) {
|
|
264
|
+
const fieldsList = fieldsFlat ?? orderSelectedFields(fields);
|
|
265
|
+
for (const f of fieldsList) if (is(f.field, Column) && getTableName(f.field.table) !== (is(table, Subquery) ? table._.alias : 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)) {
|
|
266
|
+
const tableName = getTableName(f.field.table);
|
|
267
|
+
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?`);
|
|
268
|
+
}
|
|
269
|
+
const isSingleTable = !joins || joins.length === 0;
|
|
270
|
+
const withSql = this.buildWithCTE(withList);
|
|
271
|
+
const distinctSql = distinct ? sql` distinct` : void 0;
|
|
272
|
+
const selection = this.buildSelection(fieldsList, { isSingleTable });
|
|
273
|
+
const tableSql = (() => {
|
|
274
|
+
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])}`;
|
|
275
|
+
return table;
|
|
276
|
+
})();
|
|
277
|
+
const joinsArray = [];
|
|
278
|
+
if (joins) for (const [index, joinMeta] of joins.entries()) {
|
|
279
|
+
if (index === 0) joinsArray.push(sql` `);
|
|
280
|
+
const table2 = joinMeta.table;
|
|
281
|
+
const lateralSql = joinMeta.lateral ? sql` lateral` : void 0;
|
|
282
|
+
const onSql = joinMeta.on ? sql` on ${joinMeta.on}` : void 0;
|
|
283
|
+
if (is(table2, SingleStoreTable)) {
|
|
284
|
+
const tableName = table2[SingleStoreTable.Symbol.Name];
|
|
285
|
+
const tableSchema = table2[SingleStoreTable.Symbol.Schema];
|
|
286
|
+
const origTableName = table2[SingleStoreTable.Symbol.OriginalName];
|
|
287
|
+
const alias = tableName === origTableName ? void 0 : joinMeta.alias;
|
|
288
|
+
joinsArray.push(sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`}${onSql}`);
|
|
289
|
+
} else if (is(table2, View)) {
|
|
290
|
+
const viewName = table2[ViewBaseConfig].name;
|
|
291
|
+
const viewSchema = table2[ViewBaseConfig].schema;
|
|
292
|
+
const origViewName = table2[ViewBaseConfig].originalName;
|
|
293
|
+
const alias = viewName === origViewName ? void 0 : joinMeta.alias;
|
|
294
|
+
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}`);
|
|
295
|
+
} else joinsArray.push(sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${table2}${onSql}`);
|
|
296
|
+
if (index < joins.length - 1) joinsArray.push(sql` `);
|
|
297
|
+
}
|
|
298
|
+
const joinsSql = sql.join(joinsArray);
|
|
299
|
+
const whereSql = where ? sql` where ${where}` : void 0;
|
|
300
|
+
const havingSql = having ? sql` having ${having}` : void 0;
|
|
301
|
+
const orderBySql = this.buildOrderBy(orderBy);
|
|
302
|
+
const groupBySql = groupBy && groupBy.length > 0 ? sql` group by ${sql.join(groupBy, sql`, `)}` : void 0;
|
|
303
|
+
const limitSql = this.buildLimit(limit);
|
|
304
|
+
const offsetSql = offset ? sql` offset ${offset}` : void 0;
|
|
305
|
+
let lockingClausesSql;
|
|
306
|
+
if (lockingClause) {
|
|
307
|
+
const { config, strength } = lockingClause;
|
|
308
|
+
lockingClausesSql = sql` for ${sql.raw(strength)}`;
|
|
309
|
+
if (config.noWait) lockingClausesSql.append(sql` nowait`);
|
|
310
|
+
else if (config.skipLocked) lockingClausesSql.append(sql` skip locked`);
|
|
311
|
+
}
|
|
312
|
+
const finalQuery = sql`${withSql}select${distinctSql} ${selection} from ${tableSql}${joinsSql}${whereSql}${groupBySql}${havingSql}${orderBySql}${limitSql}${offsetSql}${lockingClausesSql}`;
|
|
313
|
+
if (setOperators.length > 0) return this.buildSetOperations(finalQuery, setOperators);
|
|
314
|
+
return finalQuery;
|
|
315
|
+
}
|
|
316
|
+
buildSetOperations(leftSelect, setOperators) {
|
|
317
|
+
const [setOperator, ...rest] = setOperators;
|
|
318
|
+
if (!setOperator) throw new Error("Cannot pass undefined values to any set operator");
|
|
319
|
+
if (rest.length === 0) return this.buildSetOperationQuery({
|
|
320
|
+
leftSelect,
|
|
321
|
+
setOperator
|
|
322
|
+
});
|
|
323
|
+
return this.buildSetOperations(this.buildSetOperationQuery({
|
|
324
|
+
leftSelect,
|
|
325
|
+
setOperator
|
|
326
|
+
}), rest);
|
|
327
|
+
}
|
|
328
|
+
buildSetOperationQuery({ leftSelect, setOperator: { type, isAll, rightSelect, limit, orderBy, offset } }) {
|
|
329
|
+
const leftChunk = sql`(${leftSelect.getSQL()}) `;
|
|
330
|
+
const rightChunk = sql`(${rightSelect.getSQL()})`;
|
|
331
|
+
let orderBySql;
|
|
332
|
+
if (orderBy && orderBy.length > 0) {
|
|
333
|
+
const orderByValues = [];
|
|
334
|
+
for (const orderByUnit of orderBy) if (is(orderByUnit, SingleStoreColumn)) orderByValues.push(sql.identifier(this.casing.getColumnCasing(orderByUnit)));
|
|
335
|
+
else if (is(orderByUnit, SQL)) {
|
|
336
|
+
for (let i = 0; i < orderByUnit.queryChunks.length; i++) {
|
|
337
|
+
const chunk = orderByUnit.queryChunks[i];
|
|
338
|
+
if (is(chunk, SingleStoreColumn)) orderByUnit.queryChunks[i] = sql.identifier(this.casing.getColumnCasing(chunk));
|
|
339
|
+
}
|
|
340
|
+
orderByValues.push(sql`${orderByUnit}`);
|
|
341
|
+
} else orderByValues.push(sql`${orderByUnit}`);
|
|
342
|
+
orderBySql = sql` order by ${sql.join(orderByValues, sql`, `)} `;
|
|
343
|
+
}
|
|
344
|
+
const limitSql = typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql` limit ${limit}` : void 0;
|
|
345
|
+
const operatorChunk = sql.raw(`${type} ${isAll ? "all " : ""}`);
|
|
346
|
+
const offsetSql = offset ? sql` offset ${offset}` : void 0;
|
|
347
|
+
return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
|
|
348
|
+
}
|
|
349
|
+
buildInsertQuery({ table, values, ignore, onConflict }) {
|
|
350
|
+
const valuesSqlList = [];
|
|
351
|
+
const columns = table[Table.Symbol.Columns];
|
|
352
|
+
const colEntries = Object.entries(columns).filter(([_, col]) => !col.shouldDisableInsert());
|
|
353
|
+
const insertOrder = colEntries.map(([, column]) => sql.identifier(this.casing.getColumnCasing(column)));
|
|
354
|
+
const generatedIdsResponse = [];
|
|
355
|
+
for (const [valueIndex, value] of values.entries()) {
|
|
356
|
+
const generatedIds = {};
|
|
357
|
+
const valueList = [];
|
|
358
|
+
for (const [fieldName, col] of colEntries) {
|
|
359
|
+
const colValue = value[fieldName];
|
|
360
|
+
if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) if (col.defaultFn !== void 0) {
|
|
361
|
+
const defaultFnResult = col.defaultFn();
|
|
362
|
+
generatedIds[fieldName] = defaultFnResult;
|
|
363
|
+
const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
|
|
364
|
+
valueList.push(defaultValue);
|
|
365
|
+
} else if (!col.default && col.onUpdateFn !== void 0) {
|
|
366
|
+
const onUpdateFnResult = col.onUpdateFn();
|
|
367
|
+
const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
|
|
368
|
+
valueList.push(newValue);
|
|
369
|
+
} else valueList.push(sql`default`);
|
|
370
|
+
else {
|
|
371
|
+
if (col.defaultFn && is(colValue, Param)) generatedIds[fieldName] = colValue.value;
|
|
372
|
+
valueList.push(colValue);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
generatedIdsResponse.push(generatedIds);
|
|
376
|
+
valuesSqlList.push(valueList);
|
|
377
|
+
if (valueIndex < values.length - 1) valuesSqlList.push(sql`, `);
|
|
378
|
+
}
|
|
379
|
+
const valuesSql = sql.join(valuesSqlList);
|
|
380
|
+
return {
|
|
381
|
+
sql: sql`insert${ignore ? sql` ignore` : void 0} into ${table} ${insertOrder} values ${valuesSql}${onConflict ? sql` on duplicate key ${onConflict}` : void 0}`,
|
|
382
|
+
generatedIds: generatedIdsResponse
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
sqlToQuery(sql2, invokeSource) {
|
|
386
|
+
return sql2.toQuery({
|
|
387
|
+
casing: this.casing,
|
|
388
|
+
escapeName: this.escapeName,
|
|
389
|
+
escapeParam: this.escapeParam,
|
|
390
|
+
escapeString: this.escapeString,
|
|
391
|
+
invokeSource
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
buildRelationalQuery({ fullSchema, schema, tableNamesMap, table, tableConfig, queryConfig: config, tableAlias, nestedQueryRelation, joinOn }) {
|
|
395
|
+
let selection = [];
|
|
396
|
+
let limit, offset, orderBy, where;
|
|
397
|
+
const joins = [];
|
|
398
|
+
if (config === true) selection = Object.entries(tableConfig.columns).map(([key, value]) => ({
|
|
399
|
+
dbKey: value.name,
|
|
400
|
+
tsKey: key,
|
|
401
|
+
field: aliasedTableColumn(value, tableAlias),
|
|
402
|
+
relationTableTsKey: void 0,
|
|
403
|
+
isJson: false,
|
|
404
|
+
selection: []
|
|
405
|
+
}));
|
|
406
|
+
else {
|
|
407
|
+
const aliasedColumns = Object.fromEntries(Object.entries(tableConfig.columns).map(([key, value]) => [key, aliasedTableColumn(value, tableAlias)]));
|
|
408
|
+
if (config.where) {
|
|
409
|
+
const whereSql = typeof config.where === "function" ? config.where(aliasedColumns, getOperators()) : config.where;
|
|
410
|
+
where = whereSql && mapColumnsInSQLToAlias(whereSql, tableAlias);
|
|
411
|
+
}
|
|
412
|
+
const fieldsSelection = [];
|
|
413
|
+
let selectedColumns = [];
|
|
414
|
+
if (config.columns) {
|
|
415
|
+
let isIncludeMode = false;
|
|
416
|
+
for (const [field, value] of Object.entries(config.columns)) {
|
|
417
|
+
if (value === void 0) continue;
|
|
418
|
+
if (field in tableConfig.columns) {
|
|
419
|
+
if (!isIncludeMode && value === true) isIncludeMode = true;
|
|
420
|
+
selectedColumns.push(field);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
if (selectedColumns.length > 0) selectedColumns = isIncludeMode ? selectedColumns.filter((c) => config.columns?.[c] === true) : Object.keys(tableConfig.columns).filter((key) => !selectedColumns.includes(key));
|
|
424
|
+
} else selectedColumns = Object.keys(tableConfig.columns);
|
|
425
|
+
for (const field of selectedColumns) {
|
|
426
|
+
const column = tableConfig.columns[field];
|
|
427
|
+
fieldsSelection.push({
|
|
428
|
+
tsKey: field,
|
|
429
|
+
value: column
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
let selectedRelations = [];
|
|
433
|
+
if (config.with) selectedRelations = Object.entries(config.with).filter((entry) => !!entry[1]).map(([tsKey, queryConfig]) => ({
|
|
434
|
+
tsKey,
|
|
435
|
+
queryConfig,
|
|
436
|
+
relation: tableConfig.relations[tsKey]
|
|
437
|
+
}));
|
|
438
|
+
let extras;
|
|
439
|
+
if (config.extras) {
|
|
440
|
+
extras = typeof config.extras === "function" ? config.extras(aliasedColumns, { sql }) : config.extras;
|
|
441
|
+
for (const [tsKey, value] of Object.entries(extras)) fieldsSelection.push({
|
|
442
|
+
tsKey,
|
|
443
|
+
value: mapColumnsInAliasedSQLToAlias(value, tableAlias)
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
for (const { tsKey, value } of fieldsSelection) selection.push({
|
|
447
|
+
dbKey: is(value, SQL.Aliased) ? value.fieldAlias : tableConfig.columns[tsKey].name,
|
|
448
|
+
tsKey,
|
|
449
|
+
field: is(value, Column) ? aliasedTableColumn(value, tableAlias) : value,
|
|
450
|
+
relationTableTsKey: void 0,
|
|
451
|
+
isJson: false,
|
|
452
|
+
selection: []
|
|
453
|
+
});
|
|
454
|
+
let orderByOrig = typeof config.orderBy === "function" ? config.orderBy(aliasedColumns, getOrderByOperators()) : config.orderBy ?? [];
|
|
455
|
+
if (!Array.isArray(orderByOrig)) orderByOrig = [orderByOrig];
|
|
456
|
+
orderBy = orderByOrig.map((orderByValue) => {
|
|
457
|
+
if (is(orderByValue, Column)) return aliasedTableColumn(orderByValue, tableAlias);
|
|
458
|
+
return mapColumnsInSQLToAlias(orderByValue, tableAlias);
|
|
459
|
+
});
|
|
460
|
+
limit = config.limit;
|
|
461
|
+
offset = config.offset;
|
|
462
|
+
for (const { tsKey: selectedRelationTsKey, queryConfig: selectedRelationConfigValue, relation } of selectedRelations) {
|
|
463
|
+
const normalizedRelation = normalizeRelation(schema, tableNamesMap, relation);
|
|
464
|
+
const relationTableTsName = tableNamesMap[getTableUniqueName(relation.referencedTable)];
|
|
465
|
+
const relationTableAlias = `${tableAlias}_${selectedRelationTsKey}`;
|
|
466
|
+
const joinOn2 = and(...normalizedRelation.fields.map((field2, i) => eq(aliasedTableColumn(normalizedRelation.references[i], relationTableAlias), aliasedTableColumn(field2, tableAlias))));
|
|
467
|
+
const builtRelation = this.buildRelationalQuery({
|
|
468
|
+
fullSchema,
|
|
469
|
+
schema,
|
|
470
|
+
tableNamesMap,
|
|
471
|
+
table: fullSchema[relationTableTsName],
|
|
472
|
+
tableConfig: schema[relationTableTsName],
|
|
473
|
+
queryConfig: is(relation, One) ? selectedRelationConfigValue === true ? { limit: 1 } : {
|
|
474
|
+
...selectedRelationConfigValue,
|
|
475
|
+
limit: 1
|
|
476
|
+
} : selectedRelationConfigValue,
|
|
477
|
+
tableAlias: relationTableAlias,
|
|
478
|
+
joinOn: joinOn2,
|
|
479
|
+
nestedQueryRelation: relation
|
|
480
|
+
});
|
|
481
|
+
const field = sql`${sql.identifier(relationTableAlias)}.${sql.identifier("data")}`.as(selectedRelationTsKey);
|
|
482
|
+
joins.push({
|
|
483
|
+
on: sql`true`,
|
|
484
|
+
table: new Subquery(builtRelation.sql, {}, relationTableAlias),
|
|
485
|
+
alias: relationTableAlias,
|
|
486
|
+
joinType: "left",
|
|
487
|
+
lateral: true
|
|
488
|
+
});
|
|
489
|
+
selection.push({
|
|
490
|
+
dbKey: selectedRelationTsKey,
|
|
491
|
+
tsKey: selectedRelationTsKey,
|
|
492
|
+
field,
|
|
493
|
+
relationTableTsKey: relationTableTsName,
|
|
494
|
+
isJson: true,
|
|
495
|
+
selection: builtRelation.selection
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
if (selection.length === 0) throw new DrizzleError({ message: `No fields selected for table "${tableConfig.tsName}" ("${tableAlias}")` });
|
|
500
|
+
let result;
|
|
501
|
+
where = and(joinOn, where);
|
|
502
|
+
if (nestedQueryRelation) {
|
|
503
|
+
let field = sql`JSON_TO_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`, `)})`;
|
|
504
|
+
if (is(nestedQueryRelation, Many)) field = sql`json_agg(${field})`;
|
|
505
|
+
const nestedSelection = [{
|
|
506
|
+
dbKey: "data",
|
|
507
|
+
tsKey: "data",
|
|
508
|
+
field: field.as("data"),
|
|
509
|
+
isJson: true,
|
|
510
|
+
relationTableTsKey: tableConfig.tsName,
|
|
511
|
+
selection
|
|
512
|
+
}];
|
|
513
|
+
if (limit !== void 0 || offset !== void 0 || (orderBy?.length ?? 0) > 0) {
|
|
514
|
+
result = this.buildSelectQuery({
|
|
515
|
+
table: aliasedTable(table, tableAlias),
|
|
516
|
+
fields: {},
|
|
517
|
+
fieldsFlat: [{
|
|
518
|
+
path: [],
|
|
519
|
+
field: sql.raw("*")
|
|
520
|
+
}, ...(orderBy?.length ?? 0) > 0 ? [{
|
|
521
|
+
path: [],
|
|
522
|
+
field: sql`row_number() over (order by ${sql.join(orderBy, sql`, `)})`
|
|
523
|
+
}] : []],
|
|
524
|
+
where,
|
|
525
|
+
limit,
|
|
526
|
+
offset,
|
|
527
|
+
setOperators: []
|
|
528
|
+
});
|
|
529
|
+
where = void 0;
|
|
530
|
+
limit = void 0;
|
|
531
|
+
offset = void 0;
|
|
532
|
+
orderBy = void 0;
|
|
533
|
+
} else result = aliasedTable(table, tableAlias);
|
|
534
|
+
result = this.buildSelectQuery({
|
|
535
|
+
table: is(result, SingleStoreTable) ? result : new Subquery(result, {}, tableAlias),
|
|
536
|
+
fields: {},
|
|
537
|
+
fieldsFlat: nestedSelection.map(({ field: field2 }) => ({
|
|
538
|
+
path: [],
|
|
539
|
+
field: is(field2, Column) ? aliasedTableColumn(field2, tableAlias) : field2
|
|
540
|
+
})),
|
|
541
|
+
joins,
|
|
542
|
+
where,
|
|
543
|
+
limit,
|
|
544
|
+
offset,
|
|
545
|
+
orderBy,
|
|
546
|
+
setOperators: []
|
|
547
|
+
});
|
|
548
|
+
} else result = this.buildSelectQuery({
|
|
549
|
+
table: aliasedTable(table, tableAlias),
|
|
550
|
+
fields: {},
|
|
551
|
+
fieldsFlat: selection.map(({ field }) => ({
|
|
552
|
+
path: [],
|
|
553
|
+
field: is(field, Column) ? aliasedTableColumn(field, tableAlias) : field
|
|
554
|
+
})),
|
|
555
|
+
joins,
|
|
556
|
+
where,
|
|
557
|
+
limit,
|
|
558
|
+
offset,
|
|
559
|
+
orderBy,
|
|
560
|
+
setOperators: []
|
|
561
|
+
});
|
|
562
|
+
return {
|
|
563
|
+
tableTsKey: tableConfig.tsName,
|
|
564
|
+
sql: result,
|
|
565
|
+
selection
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
//#endregion
|
|
570
|
+
export { getTableConfig as i, SingleStoreTable as n, extractUsedTable as r, SingleStoreDialect as t };
|