metal-orm 1.1.25 → 1.1.26

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.
@@ -1,217 +0,0 @@
1
- import { DialectBase } from '../abstract.js';
2
- import type { CompilerContext } from '../abstract.js';
3
- import type {
4
- DeleteQueryNode,
5
- DerivedTableNode,
6
- FunctionTableNode,
7
- InsertQueryNode,
8
- OrderByNode,
9
- SelectQueryNode,
10
- TableNode,
11
- TableSourceNode,
12
- UpdateAssignmentNode,
13
- UpdateQueryNode,
14
- UpsertClause
15
- } from '../../ast/query.js';
16
- import type { ColumnNode } from '../../ast/expression.js';
17
- import type { FunctionStrategy } from '../../functions/types.js';
18
- import type { TableFunctionStrategy } from '../../functions/table-types.js';
19
- import { StandardLimitOffsetPagination } from './pagination-strategy.js';
20
- import type { PaginationStrategy } from './pagination-strategy.js';
21
- import { NoReturningStrategy } from './returning-strategy.js';
22
- import type { ReturningStrategy } from './returning-strategy.js';
23
- import { NoUpsertStrategy } from './upsert-strategy.js';
24
- import type { UpsertStrategy } from './upsert-strategy.js';
25
- import { StandardSqlSourceCompiler } from './standard-sql-source-compiler.js';
26
- import { StandardSelectCompiler } from './standard-select-compiler.js';
27
- import { StandardInsertCompiler } from './standard-insert-compiler.js';
28
- import { StandardUpdateCompiler } from './standard-update-compiler.js';
29
- import { StandardDeleteCompiler } from './standard-delete-compiler.js';
30
- import type { StandardSqlCompilerServices } from './standard-sql-services.js';
31
- import type { SqlCompilerFactory, SqlCompilerSet } from './sql-compiler-set.js';
32
-
33
- export interface SqlDialectBaseOptions {
34
- functionStrategy?: FunctionStrategy;
35
- tableFunctionStrategy?: TableFunctionStrategy;
36
- paginationStrategy?: PaginationStrategy;
37
- returningStrategy?: ReturningStrategy;
38
- upsertStrategy?: UpsertStrategy;
39
- compilerFactory?: SqlCompilerFactory;
40
- supportsDmlReturning?: boolean;
41
- }
42
-
43
- /**
44
- * Thin assembly base for dialects that use MetalORM's reusable SQL compiler pieces.
45
- *
46
- * Query orchestration, source rendering, upsert behavior and returning behavior are
47
- * injected components. Concrete dialects keep only syntax hooks that are genuinely
48
- * intrinsic to that backend.
49
- */
50
- export abstract class SqlDialectBase extends DialectBase {
51
- abstract quoteIdentifier(id: string): string;
52
-
53
- protected readonly paginationStrategy: PaginationStrategy;
54
- protected readonly returningStrategy: ReturningStrategy;
55
- protected readonly upsertStrategy: UpsertStrategy;
56
-
57
- private readonly dmlReturningSupported: boolean;
58
- private readonly sourceCompiler: StandardSqlSourceCompiler;
59
- private readonly standardUpdateCompiler: StandardUpdateCompiler;
60
- private readonly compilerSet: SqlCompilerSet;
61
-
62
- protected constructor(options: SqlDialectBaseOptions = {}) {
63
- super(options.functionStrategy, options.tableFunctionStrategy);
64
-
65
- this.paginationStrategy = options.paginationStrategy ?? new StandardLimitOffsetPagination();
66
- this.returningStrategy = options.returningStrategy ?? new NoReturningStrategy();
67
- this.upsertStrategy = options.upsertStrategy ?? new NoUpsertStrategy();
68
- this.dmlReturningSupported = options.supportsDmlReturning ?? false;
69
-
70
- const services: StandardSqlCompilerServices = {
71
- getDialectName: () => this.dialect,
72
- getPaginationStrategy: () => this.paginationStrategy,
73
- getTableFunctionStrategy: () => this.tableFunctionStrategy,
74
- quoteIdentifier: id => this.quoteIdentifier(id),
75
- compileOperand: (node, ctx) => this.compileOperand(node, ctx),
76
- compileExpression: (node, ctx) => this.compileExpression(node, ctx),
77
- compileOrderingTerm: (term, ctx) => this.compileOrderingTerm(term, ctx),
78
- normalizeSelectAst: ast => this.normalizeSelectAst(ast),
79
- compileSelectAst: (ast, ctx) => this.compileSelectAst(ast, ctx),
80
- compileReturning: (returning, ctx) => this.compileReturning(returning, ctx),
81
- compileUpsertClause: (ast, ctx) => this.compileUpsertClause(ast, ctx),
82
- compileSetTarget: (column, table) => this.compileSetTarget(column, table),
83
- renderOrderByNulls: order => this.renderOrderByNulls(order),
84
- renderOrderByCollation: order => this.renderOrderByCollation(order)
85
- };
86
-
87
- this.sourceCompiler = new StandardSqlSourceCompiler(services);
88
- const standardSelect = new StandardSelectCompiler(services, this.sourceCompiler);
89
- const standardInsert = new StandardInsertCompiler(services, this.sourceCompiler);
90
- this.standardUpdateCompiler = new StandardUpdateCompiler(services, this.sourceCompiler);
91
- const standardDelete = new StandardDeleteCompiler(services, this.sourceCompiler);
92
-
93
- const overrides = options.compilerFactory?.({
94
- services,
95
- sources: this.sourceCompiler
96
- }) ?? {};
97
-
98
- this.compilerSet = {
99
- select: overrides.select ?? standardSelect,
100
- insert: overrides.insert ?? standardInsert,
101
- update: overrides.update ?? this.standardUpdateCompiler,
102
- delete: overrides.delete ?? standardDelete
103
- };
104
- }
105
-
106
- override supportsDmlReturningClause(): boolean {
107
- return this.dmlReturningSupported;
108
- }
109
-
110
- protected compileSelectAst(ast: SelectQueryNode, ctx: CompilerContext): string {
111
- return this.compilerSet.select.compile(ast, ctx);
112
- }
113
-
114
- protected compileInsertAst(ast: InsertQueryNode, ctx: CompilerContext): string {
115
- return this.compilerSet.insert.compile(ast, ctx);
116
- }
117
-
118
- protected compileUpdateAst(ast: UpdateQueryNode, ctx: CompilerContext): string {
119
- return this.compilerSet.update.compile(ast, ctx);
120
- }
121
-
122
- protected compileDeleteAst(ast: DeleteQueryNode, ctx: CompilerContext): string {
123
- return this.compilerSet.delete.compile(ast, ctx);
124
- }
125
-
126
- protected compileUpsertClause(ast: InsertQueryNode, ctx: CompilerContext): string {
127
- return this.upsertStrategy.compile(ast, ctx, {
128
- getDialectName: () => this.dialect,
129
- quoteIdentifier: id => this.quoteIdentifier(id),
130
- compileOperand: (node, compilerContext) => this.compileOperand(node, compilerContext),
131
- compileExpression: (node, compilerContext) => this.compileExpression(node, compilerContext),
132
- compileUpdateAssignments: (assignments, table, compilerContext) =>
133
- this.standardUpdateCompiler.compileAssignments(assignments, table, compilerContext)
134
- });
135
- }
136
-
137
- protected compileReturning(returning: ColumnNode[] | undefined, ctx: CompilerContext): string {
138
- return this.returningStrategy.compileReturning(
139
- returning,
140
- ctx,
141
- id => this.quoteIdentifier(id)
142
- );
143
- }
144
-
145
- protected ensureConflictColumns(clause: UpsertClause, message: string): void {
146
- if (!clause.target.columns.length) throw new Error(message);
147
- }
148
-
149
- protected compileUpdateAssignments(
150
- assignments: UpdateAssignmentNode[],
151
- table: TableNode,
152
- ctx: CompilerContext
153
- ): string {
154
- return this.standardUpdateCompiler.compileAssignments(assignments, table, ctx);
155
- }
156
-
157
- protected compileSetTarget(column: ColumnNode, table: TableNode): string {
158
- return this.compileQualifiedColumn(column, table);
159
- }
160
-
161
- protected compileQualifiedColumn(column: ColumnNode, table: TableNode): string {
162
- const baseTableName = table.name;
163
- const alias = table.alias;
164
- const columnTable = column.table ?? alias ?? baseTableName;
165
- const tableQualifier = alias && column.table === baseTableName ? alias : columnTable;
166
-
167
- if (!tableQualifier) return this.quoteIdentifier(column.name);
168
- return `${this.quoteIdentifier(tableQualifier)}.${this.quoteIdentifier(column.name)}`;
169
- }
170
-
171
- protected formatReturningColumns(returning: ColumnNode[]): string {
172
- return this.returningStrategy.formatReturningColumns(
173
- returning,
174
- id => this.quoteIdentifier(id)
175
- );
176
- }
177
-
178
- protected compileFrom(source: TableSourceNode, ctx?: CompilerContext): string {
179
- return this.sourceCompiler.compileFrom(source, ctx);
180
- }
181
-
182
- protected compileFunctionTable(fn: FunctionTableNode, ctx?: CompilerContext): string {
183
- return this.sourceCompiler.compileFunctionTable(fn, ctx);
184
- }
185
-
186
- protected compileDerivedTable(table: DerivedTableNode, ctx?: CompilerContext): string {
187
- return this.sourceCompiler.compileDerivedTable(table, ctx);
188
- }
189
-
190
- protected compileTableSource(table: TableSourceNode): string {
191
- return this.sourceCompiler.compileTableSource(table);
192
- }
193
-
194
- protected compileTableName(table: { name: string; schema?: string }): string {
195
- return this.sourceCompiler.compileTableName(table);
196
- }
197
-
198
- protected compileTableReference(table: { name: string; schema?: string; alias?: string }): string {
199
- return this.sourceCompiler.compileTableReference(table);
200
- }
201
-
202
- protected stripTrailingSemicolon(sql: string): string {
203
- return this.sourceCompiler.stripTrailingSemicolon(sql);
204
- }
205
-
206
- protected wrapSetOperand(sql: string): string {
207
- return this.sourceCompiler.wrapSetOperand(sql);
208
- }
209
-
210
- protected renderOrderByNulls(order: OrderByNode): string | undefined {
211
- return order.nulls ? ` NULLS ${order.nulls}` : '';
212
- }
213
-
214
- protected renderOrderByCollation(order: OrderByNode): string | undefined {
215
- return order.collation ? ` COLLATE ${order.collation}` : '';
216
- }
217
- }