metal-orm 1.1.23 → 1.1.24

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/dist/index.cjs CHANGED
@@ -109,6 +109,11 @@ __export(index_exports, {
109
109
  SelectQueryBuilder: () => SelectQueryBuilder,
110
110
  SqlServerDialect: () => SqlServerDialect,
111
111
  SqliteDialect: () => SqliteDialect,
112
+ StandardDeleteCompiler: () => StandardDeleteCompiler,
113
+ StandardInsertCompiler: () => StandardInsertCompiler,
114
+ StandardSelectCompiler: () => StandardSelectCompiler,
115
+ StandardSqlSourceCompiler: () => StandardSqlSourceCompiler,
116
+ StandardUpdateCompiler: () => StandardUpdateCompiler,
112
117
  StringTypeStrategy: () => StringTypeStrategy,
113
118
  TagIndex: () => TagIndex,
114
119
  Title: () => Title,
@@ -2132,6 +2137,50 @@ var DialectBase = class _DialectBase {
2132
2137
  }
2133
2138
  };
2134
2139
 
2140
+ // src/core/dialect/base/pagination-strategy.ts
2141
+ var StandardLimitOffsetPagination = class {
2142
+ /**
2143
+ * Compiles LIMIT/OFFSET pagination clause.
2144
+ * @param limit - The maximum number of rows to return.
2145
+ * @param offset - The number of rows to skip.
2146
+ * @returns SQL pagination clause with LIMIT and/or OFFSET.
2147
+ */
2148
+ compilePagination(limit, offset) {
2149
+ const parts = [];
2150
+ if (limit !== void 0) parts.push(`LIMIT ${limit}`);
2151
+ if (offset !== void 0) parts.push(`OFFSET ${offset}`);
2152
+ return parts.length ? ` ${parts.join(" ")}` : "";
2153
+ }
2154
+ };
2155
+
2156
+ // src/core/dialect/base/returning-strategy.ts
2157
+ var NoReturningStrategy = class {
2158
+ /**
2159
+ * Throws an error as RETURNING is not supported.
2160
+ * @param returning - Columns to return (causes error if non-empty).
2161
+ * @param _ctx - Compiler context (unused).
2162
+ * @throws Error indicating RETURNING is not supported.
2163
+ */
2164
+ compileReturning(returning, _ctx) {
2165
+ void _ctx;
2166
+ if (!returning || returning.length === 0) return "";
2167
+ throw new Error("RETURNING is not supported by this dialect.");
2168
+ }
2169
+ /**
2170
+ * Formats column names for RETURNING clause.
2171
+ * @param returning - Columns to format.
2172
+ * @param quoteIdentifier - Function to quote identifiers according to dialect rules.
2173
+ * @returns Simple comma-separated column names.
2174
+ */
2175
+ formatReturningColumns(returning, quoteIdentifier) {
2176
+ return returning.map((column) => {
2177
+ const tablePart = column.table ? `${quoteIdentifier(column.table)}.` : "";
2178
+ const aliasPart = column.alias ? ` AS ${quoteIdentifier(column.alias)}` : "";
2179
+ return `${tablePart}${quoteIdentifier(column.name)}${aliasPart}`;
2180
+ }).join(", ");
2181
+ }
2182
+ };
2183
+
2135
2184
  // src/core/dialect/base/function-table-formatter.ts
2136
2185
  var FunctionTableFormatter = class {
2137
2186
  static format(fn9, ctx, formatter) {
@@ -2168,19 +2217,71 @@ var FunctionTableFormatter = class {
2168
2217
  }
2169
2218
  };
2170
2219
 
2171
- // src/core/dialect/base/pagination-strategy.ts
2172
- var StandardLimitOffsetPagination = class {
2173
- /**
2174
- * Compiles LIMIT/OFFSET pagination clause.
2175
- * @param limit - The maximum number of rows to return.
2176
- * @param offset - The number of rows to skip.
2177
- * @returns SQL pagination clause with LIMIT and/or OFFSET.
2178
- */
2179
- compilePagination(limit, offset) {
2180
- const parts = [];
2181
- if (limit !== void 0) parts.push(`LIMIT ${limit}`);
2182
- if (offset !== void 0) parts.push(`OFFSET ${offset}`);
2183
- return parts.length ? ` ${parts.join(" ")}` : "";
2220
+ // src/core/dialect/base/standard-sql-source-compiler.ts
2221
+ var StandardSqlSourceCompiler = class {
2222
+ constructor(services) {
2223
+ this.services = services;
2224
+ }
2225
+ compileFrom(source, ctx) {
2226
+ if (source.type === "FunctionTable") return this.compileFunctionTable(source, ctx);
2227
+ if (source.type === "DerivedTable") return this.compileDerivedTable(source, ctx);
2228
+ return this.compileTableSource(source);
2229
+ }
2230
+ compileFunctionTable(fn9, ctx) {
2231
+ const key = fn9.key ?? fn9.name;
2232
+ if (ctx) {
2233
+ const renderer = this.services.getTableFunctionStrategy().getRenderer(key);
2234
+ if (renderer) {
2235
+ const compiledArgs = (fn9.args ?? []).map((arg) => this.services.compileOperand(arg, ctx));
2236
+ return renderer({
2237
+ node: fn9,
2238
+ compiledArgs,
2239
+ compileOperand: (operand) => this.services.compileOperand(operand, ctx),
2240
+ quoteIdentifier: (id) => this.services.quoteIdentifier(id)
2241
+ });
2242
+ }
2243
+ if (fn9.key) {
2244
+ throw new Error(
2245
+ `Table function "${key}" is not supported by dialect "${this.services.getDialectName()}".`
2246
+ );
2247
+ }
2248
+ }
2249
+ return FunctionTableFormatter.format(fn9, ctx, {
2250
+ quoteIdentifier: (id) => this.services.quoteIdentifier(id),
2251
+ compileOperand: (node, compilerContext) => this.services.compileOperand(node, compilerContext)
2252
+ });
2253
+ }
2254
+ compileDerivedTable(table, ctx) {
2255
+ if (!table.alias) throw new Error("Derived tables must have an alias.");
2256
+ if (!ctx) throw new Error("Derived table compilation requires a compiler context.");
2257
+ const normalized = this.services.normalizeSelectAst(table.query);
2258
+ const subquery = this.services.compileSelectAst(normalized, ctx).trim().replace(/;$/, "");
2259
+ const columns = table.columnAliases?.length ? ` (${table.columnAliases.map((column) => this.services.quoteIdentifier(column)).join(", ")})` : "";
2260
+ return `(${subquery}) AS ${this.services.quoteIdentifier(table.alias)}${columns}`;
2261
+ }
2262
+ compileTableSource(table) {
2263
+ if (table.type === "FunctionTable") return this.compileFunctionTable(table);
2264
+ if (table.type === "DerivedTable") {
2265
+ throw new Error("Derived table compilation requires a compiler context.");
2266
+ }
2267
+ const base = this.compileTableName(table);
2268
+ return table.alias ? `${base} AS ${this.services.quoteIdentifier(table.alias)}` : base;
2269
+ }
2270
+ compileTableName(table) {
2271
+ if (table.schema) {
2272
+ return `${this.services.quoteIdentifier(table.schema)}.${this.services.quoteIdentifier(table.name)}`;
2273
+ }
2274
+ return this.services.quoteIdentifier(table.name);
2275
+ }
2276
+ compileTableReference(table) {
2277
+ const base = this.compileTableName(table);
2278
+ return table.alias ? `${base} AS ${this.services.quoteIdentifier(table.alias)}` : base;
2279
+ }
2280
+ stripTrailingSemicolon(sql) {
2281
+ return sql.trim().replace(/;$/, "");
2282
+ }
2283
+ wrapSetOperand(sql) {
2284
+ return `(${this.stripTrailingSemicolon(sql)})`;
2184
2285
  }
2185
2286
  };
2186
2287
 
@@ -2210,34 +2311,6 @@ var CteCompiler = class {
2210
2311
  }
2211
2312
  };
2212
2313
 
2213
- // src/core/dialect/base/returning-strategy.ts
2214
- var NoReturningStrategy = class {
2215
- /**
2216
- * Throws an error as RETURNING is not supported.
2217
- * @param returning - Columns to return (causes error if non-empty).
2218
- * @param _ctx - Compiler context (unused).
2219
- * @throws Error indicating RETURNING is not supported.
2220
- */
2221
- compileReturning(returning, _ctx) {
2222
- void _ctx;
2223
- if (!returning || returning.length === 0) return "";
2224
- throw new Error("RETURNING is not supported by this dialect.");
2225
- }
2226
- /**
2227
- * Formats column names for RETURNING clause.
2228
- * @param returning - Columns to format.
2229
- * @param quoteIdentifier - Function to quote identifiers according to dialect rules.
2230
- * @returns Simple comma-separated column names.
2231
- */
2232
- formatReturningColumns(returning, quoteIdentifier) {
2233
- return returning.map((column) => {
2234
- const tablePart = column.table ? `${quoteIdentifier(column.table)}.` : "";
2235
- const aliasPart = column.alias ? ` AS ${quoteIdentifier(column.alias)}` : "";
2236
- return `${tablePart}${quoteIdentifier(column.name)}${aliasPart}`;
2237
- }).join(", ");
2238
- }
2239
- };
2240
-
2241
2314
  // src/core/dialect/base/join-compiler.ts
2242
2315
  var JoinCompiler = class {
2243
2316
  static compileJoins(joins, ctx, compileFrom, compileExpression) {
@@ -2288,108 +2361,229 @@ var OrderByCompiler = class {
2288
2361
  }
2289
2362
  };
2290
2363
 
2291
- // src/core/dialect/base/sql-dialect.ts
2292
- var SqlDialectBase = class extends DialectBase {
2293
- paginationStrategy = new StandardLimitOffsetPagination();
2294
- returningStrategy = new NoReturningStrategy();
2295
- compileSelectAst(ast, ctx) {
2364
+ // src/core/dialect/base/standard-select-compiler.ts
2365
+ var StandardSelectCompiler = class {
2366
+ constructor(services, sources) {
2367
+ this.services = services;
2368
+ this.sources = sources;
2369
+ }
2370
+ compile(ast, ctx) {
2296
2371
  const hasSetOps = !!(ast.setOps && ast.setOps.length);
2297
2372
  const ctes = CteCompiler.compileCtes(
2298
2373
  ast,
2299
2374
  ctx,
2300
- this.quoteIdentifier.bind(this),
2301
- this.compileSelectAst.bind(this),
2302
- this.normalizeSelectAst.bind(this),
2303
- this.stripTrailingSemicolon.bind(this)
2375
+ (id) => this.services.quoteIdentifier(id),
2376
+ (query, compilerContext) => this.services.compileSelectAst(query, compilerContext),
2377
+ (query) => this.services.normalizeSelectAst(query),
2378
+ (sql) => this.sources.stripTrailingSemicolon(sql)
2304
2379
  );
2305
2380
  const baseAst = hasSetOps ? { ...ast, setOps: void 0, orderBy: void 0, limit: void 0, offset: void 0 } : ast;
2306
- const baseSelect = this.compileSelectCore(baseAst, ctx);
2381
+ const baseSelect = this.compileCore(baseAst, ctx);
2307
2382
  if (!hasSetOps) return `${ctes}${baseSelect}`;
2308
- return this.compileSelectWithSetOps(ast, baseSelect, ctes, ctx);
2383
+ const compound = ast.setOps.map((op) => `${op.operator} ${this.sources.wrapSetOperand(this.services.compileSelectAst(op.query, ctx))}`).join(" ");
2384
+ const orderBy = this.compileOrderBy(ast, ctx);
2385
+ const pagination = this.services.getPaginationStrategy().compilePagination(ast.limit, ast.offset);
2386
+ const combined = `${this.sources.wrapSetOperand(baseSelect)} ${compound}`;
2387
+ return `${ctes}${combined}${orderBy}${pagination}`;
2309
2388
  }
2310
- compileSelectWithSetOps(ast, baseSelect, ctes, ctx) {
2311
- const compound = ast.setOps.map((op) => `${op.operator} ${this.wrapSetOperand(this.compileSelectAst(op.query, ctx))}`).join(" ");
2312
- const orderBy = OrderByCompiler.compileOrderBy(
2389
+ compileCore(ast, ctx) {
2390
+ const columns = this.compileColumns(ast, ctx);
2391
+ const from = this.sources.compileFrom(ast.from, ctx);
2392
+ const joins = JoinCompiler.compileJoins(
2393
+ ast.joins,
2394
+ ctx,
2395
+ (source, compilerContext) => this.sources.compileFrom(source, compilerContext),
2396
+ (expression, compilerContext) => this.services.compileExpression(expression, compilerContext)
2397
+ );
2398
+ const where = ast.where ? ` WHERE ${this.services.compileExpression(ast.where, ctx)}` : "";
2399
+ const groupBy = GroupByCompiler.compileGroupBy(
2313
2400
  ast,
2314
- (term) => this.compileOrderingTerm(term, ctx),
2315
- this.renderOrderByNulls.bind(this),
2316
- this.renderOrderByCollation.bind(this)
2401
+ (term) => this.services.compileOrderingTerm(term, ctx)
2317
2402
  );
2318
- const pagination = this.paginationStrategy.compilePagination(ast.limit, ast.offset);
2319
- const combined = `${this.wrapSetOperand(baseSelect)} ${compound}`;
2320
- return `${ctes}${combined}${orderBy}${pagination}`;
2403
+ const having = ast.having ? ` HAVING ${this.services.compileExpression(ast.having, ctx)}` : "";
2404
+ const orderBy = this.compileOrderBy(ast, ctx);
2405
+ const pagination = this.services.getPaginationStrategy().compilePagination(ast.limit, ast.offset);
2406
+ return `SELECT ${ast.distinct ? "DISTINCT " : ""}${columns} FROM ${from}${joins}${where}${groupBy}${having}${orderBy}${pagination}`;
2321
2407
  }
2322
- compileInsertAst(ast, ctx) {
2408
+ compileColumns(ast, ctx) {
2409
+ if (!ast.columns || ast.columns.length === 0) return "*";
2410
+ return ast.columns.map((column) => {
2411
+ const expr = this.services.compileOperand(column, ctx);
2412
+ if (!column.alias) return expr;
2413
+ if (column.alias.includes("(")) return column.alias;
2414
+ return `${expr} AS ${this.services.quoteIdentifier(column.alias)}`;
2415
+ }).join(", ");
2416
+ }
2417
+ compileOrderBy(ast, ctx) {
2418
+ return OrderByCompiler.compileOrderBy(
2419
+ ast,
2420
+ (term) => this.services.compileOrderingTerm(term, ctx),
2421
+ (order) => this.services.renderOrderByNulls(order),
2422
+ (order) => this.services.renderOrderByCollation(order)
2423
+ );
2424
+ }
2425
+ };
2426
+
2427
+ // src/core/dialect/base/standard-insert-compiler.ts
2428
+ var StandardInsertCompiler = class {
2429
+ constructor(services, sources) {
2430
+ this.services = services;
2431
+ this.sources = sources;
2432
+ }
2433
+ compile(ast, ctx) {
2323
2434
  if (!ast.columns.length) {
2324
2435
  throw new Error("INSERT queries must specify columns.");
2325
2436
  }
2326
- const table = this.compileTableName(ast.into);
2327
- const columnList = this.compileInsertColumnList(ast.columns);
2328
- const source = this.compileInsertSource(ast.source, ctx);
2329
- const upsert = this.compileUpsertClause(ast, ctx);
2330
- const returning = this.compileReturning(ast.returning, ctx);
2437
+ const table = this.sources.compileTableName(ast.into);
2438
+ const columnList = this.compileColumnList(ast.columns);
2439
+ const source = this.compileSource(ast.source, ctx);
2440
+ const upsert = this.services.compileUpsertClause(ast, ctx);
2441
+ const returning = this.services.compileReturning(ast.returning, ctx);
2331
2442
  return `INSERT INTO ${table} (${columnList}) ${source}${upsert}${returning}`;
2332
2443
  }
2333
- compileUpsertClause(ast, _ctx) {
2334
- void _ctx;
2335
- if (!ast.onConflict) return "";
2336
- throw new Error(`UPSERT/ON CONFLICT is not supported by dialect "${this.dialect}".`);
2337
- }
2338
- compileReturning(returning, ctx) {
2339
- return this.returningStrategy.compileReturning(returning, ctx);
2340
- }
2341
- compileInsertSource(source, ctx) {
2444
+ compileSource(source, ctx) {
2342
2445
  if (source.type === "InsertValues") {
2343
2446
  if (!source.rows.length) {
2344
2447
  throw new Error("INSERT ... VALUES requires at least one row.");
2345
2448
  }
2346
- const values = source.rows.map((row) => `(${row.map((value) => this.compileOperand(value, ctx)).join(", ")})`).join(", ");
2449
+ const values = source.rows.map((row) => `(${row.map((value) => this.services.compileOperand(value, ctx)).join(", ")})`).join(", ");
2347
2450
  return `VALUES ${values}`;
2348
2451
  }
2349
- const normalized = this.normalizeSelectAst(source.query);
2350
- return this.compileSelectAst(normalized, ctx).trim();
2452
+ const normalized = this.services.normalizeSelectAst(source.query);
2453
+ return this.services.compileSelectAst(normalized, ctx).trim();
2351
2454
  }
2352
- compileInsertColumnList(columns) {
2353
- return columns.map((column) => this.quoteIdentifier(column.name)).join(", ");
2455
+ compileColumnList(columns) {
2456
+ return columns.map((column) => this.services.quoteIdentifier(column.name)).join(", ");
2354
2457
  }
2355
2458
  ensureConflictColumns(clause, message) {
2356
2459
  if (!clause.target.columns.length) throw new Error(message);
2357
2460
  }
2358
- compileSelectCore(ast, ctx) {
2359
- const columns = this.compileSelectColumns(ast, ctx);
2360
- const from = this.compileFrom(ast.from, ctx);
2461
+ };
2462
+
2463
+ // src/core/dialect/base/standard-update-compiler.ts
2464
+ var StandardUpdateCompiler = class {
2465
+ constructor(services, sources) {
2466
+ this.services = services;
2467
+ this.sources = sources;
2468
+ }
2469
+ compile(ast, ctx) {
2470
+ const target = this.sources.compileTableReference(ast.table);
2471
+ const assignments = this.compileAssignments(ast.set, ast.table, ctx);
2472
+ const from = this.compileFromClause(ast, ctx);
2473
+ const where = ast.where ? ` WHERE ${this.services.compileExpression(ast.where, ctx)}` : "";
2474
+ const returning = this.services.compileReturning(ast.returning, ctx);
2475
+ return `UPDATE ${target} SET ${assignments}${from}${where}${returning}`;
2476
+ }
2477
+ compileAssignments(assignments, table, ctx) {
2478
+ return assignments.map((assignment) => {
2479
+ const target = this.services.compileSetTarget(assignment.column, table);
2480
+ const value = this.services.compileOperand(assignment.value, ctx);
2481
+ return `${target} = ${value}`;
2482
+ }).join(", ");
2483
+ }
2484
+ compileFromClause(ast, ctx) {
2485
+ if (!ast.from && (!ast.joins || ast.joins.length === 0)) return "";
2486
+ if (!ast.from) {
2487
+ throw new Error("UPDATE with JOINs requires an explicit FROM clause.");
2488
+ }
2489
+ const from = this.sources.compileFrom(ast.from, ctx);
2361
2490
  const joins = JoinCompiler.compileJoins(
2362
2491
  ast.joins,
2363
2492
  ctx,
2364
- this.compileFrom.bind(this),
2365
- this.compileExpression.bind(this)
2493
+ (source, compilerContext) => this.sources.compileFrom(source, compilerContext),
2494
+ (expression, compilerContext) => this.services.compileExpression(expression, compilerContext)
2366
2495
  );
2367
- const whereClause = this.compileWhere(ast.where, ctx);
2368
- const groupBy = GroupByCompiler.compileGroupBy(ast, (term) => this.compileOrderingTerm(term, ctx));
2369
- const having = this.compileHaving(ast, ctx);
2370
- const orderBy = OrderByCompiler.compileOrderBy(
2371
- ast,
2372
- (term) => this.compileOrderingTerm(term, ctx),
2373
- this.renderOrderByNulls.bind(this),
2374
- this.renderOrderByCollation.bind(this)
2496
+ return ` FROM ${from}${joins}`;
2497
+ }
2498
+ };
2499
+
2500
+ // src/core/dialect/base/standard-delete-compiler.ts
2501
+ var StandardDeleteCompiler = class {
2502
+ constructor(services, sources) {
2503
+ this.services = services;
2504
+ this.sources = sources;
2505
+ }
2506
+ compile(ast, ctx) {
2507
+ const target = this.sources.compileTableReference(ast.from);
2508
+ const using = this.compileUsingClause(ast, ctx);
2509
+ const where = ast.where ? ` WHERE ${this.services.compileExpression(ast.where, ctx)}` : "";
2510
+ const returning = this.services.compileReturning(ast.returning, ctx);
2511
+ return `DELETE FROM ${target}${using}${where}${returning}`;
2512
+ }
2513
+ compileUsingClause(ast, ctx) {
2514
+ if (!ast.using && (!ast.joins || ast.joins.length === 0)) return "";
2515
+ if (!ast.using) {
2516
+ throw new Error("DELETE with JOINs requires a USING clause.");
2517
+ }
2518
+ const usingTable = this.sources.compileFrom(ast.using, ctx);
2519
+ const joins = JoinCompiler.compileJoins(
2520
+ ast.joins,
2521
+ ctx,
2522
+ (source, compilerContext) => this.sources.compileFrom(source, compilerContext),
2523
+ (expression, compilerContext) => this.services.compileExpression(expression, compilerContext)
2375
2524
  );
2376
- const pagination = this.paginationStrategy.compilePagination(ast.limit, ast.offset);
2377
- return `SELECT ${this.compileDistinct(ast)}${columns} FROM ${from}${joins}${whereClause}${groupBy}${having}${orderBy}${pagination}`;
2525
+ return ` USING ${usingTable}${joins}`;
2526
+ }
2527
+ };
2528
+
2529
+ // src/core/dialect/base/sql-dialect.ts
2530
+ var SqlDialectBase = class extends DialectBase {
2531
+ paginationStrategy = new StandardLimitOffsetPagination();
2532
+ returningStrategy = new NoReturningStrategy();
2533
+ sourceCompiler;
2534
+ selectCompiler;
2535
+ insertCompiler;
2536
+ updateCompiler;
2537
+ deleteCompiler;
2538
+ constructor(functionStrategy, tableFunctionStrategy) {
2539
+ super(functionStrategy, tableFunctionStrategy);
2540
+ const services = {
2541
+ getDialectName: () => this.dialect,
2542
+ getPaginationStrategy: () => this.paginationStrategy,
2543
+ getTableFunctionStrategy: () => this.tableFunctionStrategy,
2544
+ quoteIdentifier: (id) => this.quoteIdentifier(id),
2545
+ compileOperand: (node, ctx) => this.compileOperand(node, ctx),
2546
+ compileExpression: (node, ctx) => this.compileExpression(node, ctx),
2547
+ compileOrderingTerm: (term, ctx) => this.compileOrderingTerm(term, ctx),
2548
+ normalizeSelectAst: (ast) => this.normalizeSelectAst(ast),
2549
+ compileSelectAst: (ast, ctx) => this.compileSelectAst(ast, ctx),
2550
+ compileReturning: (returning, ctx) => this.compileReturning(returning, ctx),
2551
+ compileUpsertClause: (ast, ctx) => this.compileUpsertClause(ast, ctx),
2552
+ compileSetTarget: (column, table) => this.compileSetTarget(column, table),
2553
+ renderOrderByNulls: (order) => this.renderOrderByNulls(order),
2554
+ renderOrderByCollation: (order) => this.renderOrderByCollation(order)
2555
+ };
2556
+ this.sourceCompiler = new StandardSqlSourceCompiler(services);
2557
+ this.selectCompiler = new StandardSelectCompiler(services, this.sourceCompiler);
2558
+ this.insertCompiler = new StandardInsertCompiler(services, this.sourceCompiler);
2559
+ this.updateCompiler = new StandardUpdateCompiler(services, this.sourceCompiler);
2560
+ this.deleteCompiler = new StandardDeleteCompiler(services, this.sourceCompiler);
2561
+ }
2562
+ compileSelectAst(ast, ctx) {
2563
+ return this.selectCompiler.compile(ast, ctx);
2564
+ }
2565
+ compileInsertAst(ast, ctx) {
2566
+ return this.insertCompiler.compile(ast, ctx);
2378
2567
  }
2379
2568
  compileUpdateAst(ast, ctx) {
2380
- const target = this.compileTableReference(ast.table);
2381
- const assignments = this.compileUpdateAssignments(ast.set, ast.table, ctx);
2382
- const fromClause = this.compileUpdateFromClause(ast, ctx);
2383
- const whereClause = this.compileWhere(ast.where, ctx);
2384
- const returning = this.compileReturning(ast.returning, ctx);
2385
- return `UPDATE ${target} SET ${assignments}${fromClause}${whereClause}${returning}`;
2569
+ return this.updateCompiler.compile(ast, ctx);
2570
+ }
2571
+ compileDeleteAst(ast, ctx) {
2572
+ return this.deleteCompiler.compile(ast, ctx);
2573
+ }
2574
+ compileUpsertClause(ast, _ctx) {
2575
+ void _ctx;
2576
+ if (!ast.onConflict) return "";
2577
+ throw new Error(`UPSERT/ON CONFLICT is not supported by dialect "${this.dialect}".`);
2578
+ }
2579
+ compileReturning(returning, ctx) {
2580
+ return this.returningStrategy.compileReturning(returning, ctx);
2581
+ }
2582
+ ensureConflictColumns(clause, message) {
2583
+ this.insertCompiler.ensureConflictColumns(clause, message);
2386
2584
  }
2387
2585
  compileUpdateAssignments(assignments, table, ctx) {
2388
- return assignments.map((assignment) => {
2389
- const target = this.compileSetTarget(assignment.column, table);
2390
- const value = this.compileOperand(assignment.value, ctx);
2391
- return `${target} = ${value}`;
2392
- }).join(", ");
2586
+ return this.updateCompiler.compileAssignments(assignments, table, ctx);
2393
2587
  }
2394
2588
  compileSetTarget(column, table) {
2395
2589
  return this.compileQualifiedColumn(column, table);
@@ -2402,112 +2596,35 @@ var SqlDialectBase = class extends DialectBase {
2402
2596
  if (!tableQualifier) return this.quoteIdentifier(column.name);
2403
2597
  return `${this.quoteIdentifier(tableQualifier)}.${this.quoteIdentifier(column.name)}`;
2404
2598
  }
2405
- compileDeleteAst(ast, ctx) {
2406
- const target = this.compileTableReference(ast.from);
2407
- const usingClause = this.compileDeleteUsingClause(ast, ctx);
2408
- const whereClause = this.compileWhere(ast.where, ctx);
2409
- const returning = this.compileReturning(ast.returning, ctx);
2410
- return `DELETE FROM ${target}${usingClause}${whereClause}${returning}`;
2411
- }
2412
2599
  formatReturningColumns(returning) {
2413
- return this.returningStrategy.formatReturningColumns(returning, this.quoteIdentifier.bind(this));
2414
- }
2415
- compileDistinct(ast) {
2416
- return ast.distinct ? "DISTINCT " : "";
2417
- }
2418
- compileSelectColumns(ast, ctx) {
2419
- if (!ast.columns || ast.columns.length === 0) return "*";
2420
- return ast.columns.map((column) => {
2421
- const expr = this.compileOperand(column, ctx);
2422
- if (column.alias) {
2423
- if (column.alias.includes("(")) return column.alias;
2424
- return `${expr} AS ${this.quoteIdentifier(column.alias)}`;
2425
- }
2426
- return expr;
2427
- }).join(", ");
2600
+ return this.returningStrategy.formatReturningColumns(
2601
+ returning,
2602
+ (id) => this.quoteIdentifier(id)
2603
+ );
2428
2604
  }
2429
- compileFrom(ast, ctx) {
2430
- if (ast.type === "FunctionTable") return this.compileFunctionTable(ast, ctx);
2431
- if (ast.type === "DerivedTable") return this.compileDerivedTable(ast, ctx);
2432
- return this.compileTableSource(ast);
2605
+ compileFrom(source, ctx) {
2606
+ return this.sourceCompiler.compileFrom(source, ctx);
2433
2607
  }
2434
2608
  compileFunctionTable(fn9, ctx) {
2435
- const key = fn9.key ?? fn9.name;
2436
- if (ctx) {
2437
- const renderer = this.tableFunctionStrategy.getRenderer(key);
2438
- if (renderer) {
2439
- const compiledArgs = (fn9.args ?? []).map((arg) => this.compileOperand(arg, ctx));
2440
- return renderer({
2441
- node: fn9,
2442
- compiledArgs,
2443
- compileOperand: (operand) => this.compileOperand(operand, ctx),
2444
- quoteIdentifier: this.quoteIdentifier.bind(this)
2445
- });
2446
- }
2447
- if (fn9.key) {
2448
- throw new Error(`Table function "${key}" is not supported by dialect "${this.dialect}".`);
2449
- }
2450
- }
2451
- return FunctionTableFormatter.format(fn9, ctx, {
2452
- quoteIdentifier: (id) => this.quoteIdentifier(id),
2453
- compileOperand: (node, compilerContext) => this.compileOperand(node, compilerContext)
2454
- });
2609
+ return this.sourceCompiler.compileFunctionTable(fn9, ctx);
2455
2610
  }
2456
2611
  compileDerivedTable(table, ctx) {
2457
- if (!table.alias) throw new Error("Derived tables must have an alias.");
2458
- const subquery = this.compileSelectAst(this.normalizeSelectAst(table.query), ctx).trim().replace(/;$/, "");
2459
- const columns = table.columnAliases?.length ? ` (${table.columnAliases.map((c) => this.quoteIdentifier(c)).join(", ")})` : "";
2460
- return `(${subquery}) AS ${this.quoteIdentifier(table.alias)}${columns}`;
2612
+ return this.sourceCompiler.compileDerivedTable(table, ctx);
2461
2613
  }
2462
2614
  compileTableSource(table) {
2463
- if (table.type === "FunctionTable") return this.compileFunctionTable(table);
2464
- if (table.type === "DerivedTable") return this.compileDerivedTable(table);
2465
- const base = this.compileTableName(table);
2466
- return table.alias ? `${base} AS ${this.quoteIdentifier(table.alias)}` : base;
2615
+ return this.sourceCompiler.compileTableSource(table);
2467
2616
  }
2468
2617
  compileTableName(table) {
2469
- if (table.schema) {
2470
- return `${this.quoteIdentifier(table.schema)}.${this.quoteIdentifier(table.name)}`;
2471
- }
2472
- return this.quoteIdentifier(table.name);
2618
+ return this.sourceCompiler.compileTableName(table);
2473
2619
  }
2474
2620
  compileTableReference(table) {
2475
- const base = this.compileTableName(table);
2476
- return table.alias ? `${base} AS ${this.quoteIdentifier(table.alias)}` : base;
2477
- }
2478
- compileUpdateFromClause(ast, ctx) {
2479
- if (!ast.from && (!ast.joins || ast.joins.length === 0)) return "";
2480
- if (!ast.from) throw new Error("UPDATE with JOINs requires an explicit FROM clause.");
2481
- const from = this.compileFrom(ast.from, ctx);
2482
- const joins = JoinCompiler.compileJoins(
2483
- ast.joins,
2484
- ctx,
2485
- this.compileFrom.bind(this),
2486
- this.compileExpression.bind(this)
2487
- );
2488
- return ` FROM ${from}${joins}`;
2489
- }
2490
- compileDeleteUsingClause(ast, ctx) {
2491
- if (!ast.using && (!ast.joins || ast.joins.length === 0)) return "";
2492
- if (!ast.using) throw new Error("DELETE with JOINs requires a USING clause.");
2493
- const usingTable = this.compileFrom(ast.using, ctx);
2494
- const joins = JoinCompiler.compileJoins(
2495
- ast.joins,
2496
- ctx,
2497
- this.compileFrom.bind(this),
2498
- this.compileExpression.bind(this)
2499
- );
2500
- return ` USING ${usingTable}${joins}`;
2501
- }
2502
- compileHaving(ast, ctx) {
2503
- if (!ast.having) return "";
2504
- return ` HAVING ${this.compileExpression(ast.having, ctx)}`;
2621
+ return this.sourceCompiler.compileTableReference(table);
2505
2622
  }
2506
2623
  stripTrailingSemicolon(sql) {
2507
- return sql.trim().replace(/;$/, "");
2624
+ return this.sourceCompiler.stripTrailingSemicolon(sql);
2508
2625
  }
2509
2626
  wrapSetOperand(sql) {
2510
- return `(${this.stripTrailingSemicolon(sql)})`;
2627
+ return this.sourceCompiler.wrapSetOperand(sql);
2511
2628
  }
2512
2629
  renderOrderByNulls(order) {
2513
2630
  return order.nulls ? ` NULLS ${order.nulls}` : "";
@@ -22122,6 +22239,11 @@ async function bulkUpsert(session, table, rows, options = {}) {
22122
22239
  SelectQueryBuilder,
22123
22240
  SqlServerDialect,
22124
22241
  SqliteDialect,
22242
+ StandardDeleteCompiler,
22243
+ StandardInsertCompiler,
22244
+ StandardSelectCompiler,
22245
+ StandardSqlSourceCompiler,
22246
+ StandardUpdateCompiler,
22125
22247
  StringTypeStrategy,
22126
22248
  TagIndex,
22127
22249
  Title,