@simplysm/orm-common 13.0.98 → 13.0.100

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,196 +1,93 @@
1
1
  # Query Builder
2
2
 
3
- Render `QueryDef` JSON AST to dialect-specific SQL strings.
3
+ ## `createQueryBuilder`
4
4
 
5
- Source: `src/query-builder/query-builder.ts`, `src/query-builder/base/query-builder-base.ts`, `src/query-builder/base/expr-renderer-base.ts`, `src/query-builder/mysql/`, `src/query-builder/mssql/`, `src/query-builder/postgresql/`
6
-
7
- ## createQueryBuilder
8
-
9
- Factory function that returns a dialect-specific `QueryBuilderBase` implementation.
5
+ Generate a `QueryBuilderBase` instance matching the given dialect.
10
6
 
11
7
  ```typescript
12
8
  function createQueryBuilder(dialect: Dialect): QueryBuilderBase;
13
9
  ```
14
10
 
15
- | Dialect | QueryBuilder | ExprRenderer |
16
- |---------|-------------|--------------|
17
- | `"mysql"` | `MysqlQueryBuilder` | `MysqlExprRenderer` |
18
- | `"mssql"` | `MssqlQueryBuilder` | `MssqlExprRenderer` |
19
- | `"postgresql"` | `PostgresqlQueryBuilder` | `PostgresqlExprRenderer` |
20
-
21
- **Example:**
22
-
23
- ```typescript
24
- import { createQueryBuilder } from "@simplysm/orm-common";
25
-
26
- const builder = createQueryBuilder("mysql");
27
- const result = builder.build(queryDef);
28
- console.log(result.sql);
29
- ```
30
-
31
- ---
11
+ | Parameter | Type | Description |
12
+ |-----------|------|-------------|
13
+ | `dialect` | `Dialect` | Database dialect (`"mysql"`, `"mssql"`, `"postgresql"`) |
32
14
 
33
- ## QueryBuilderBase
15
+ ## `QueryBuilderBase`
34
16
 
35
- Abstract base class for `QueryDef` to SQL rendering. Implements common dispatch logic and rendering helpers.
17
+ Abstract base class that renders `QueryDef` JSON AST to dialect-specific SQL strings. Implements dispatch logic identical across all dialects; dialect-specific rendering is handled by subclasses.
36
18
 
37
19
  ```typescript
38
20
  abstract class QueryBuilderBase {
39
- /**
40
- * Main entry point: dispatch to the appropriate method based on def.type
41
- */
21
+ protected abstract expr: ExprRendererBase;
22
+
42
23
  build(def: QueryDef): QueryBuildResult;
43
24
  }
44
25
  ```
45
26
 
46
- ### Design Principles
47
-
48
- - Method names match `def.type` for dynamic dispatch
49
- - Only 100% dialect-identical logic is implemented in the base class
50
- - Any dialect-specific behavior is declared as `abstract`
51
-
52
- ### Common Render Methods (implemented)
53
-
54
27
  | Method | Description |
55
28
  |--------|-------------|
56
- | `renderWhere(wheres)` | Render WHERE clause |
57
- | `renderOrderBy(orderBy)` | Render ORDER BY clause |
58
- | `renderGroupBy(groupBy)` | Render GROUP BY clause |
59
- | `renderHaving(having)` | Render HAVING clause |
60
- | `renderJoins(joins)` | Render all JOIN clauses |
61
- | `renderFrom(from)` | Render FROM clause source (table, subquery, union, CTE reference) |
62
- | `needsLateral(join)` | Detect if JOIN needs LATERAL/CROSS APPLY |
29
+ | `build()` | Render a `QueryDef` to SQL. Dispatches to the appropriate method by `def.type`. |
63
30
 
64
- ### Abstract Methods (implemented per dialect)
31
+ ## `ExprRendererBase`
65
32
 
66
- **DML:**
67
- - `select(def: SelectQueryDef): QueryBuildResult`
68
- - `insert(def: InsertQueryDef): QueryBuildResult`
69
- - `insertIfNotExists(def: InsertIfNotExistsQueryDef): QueryBuildResult`
70
- - `insertInto(def: InsertIntoQueryDef): QueryBuildResult`
71
- - `update(def: UpdateQueryDef): QueryBuildResult`
72
- - `delete(def: DeleteQueryDef): QueryBuildResult`
73
- - `upsert(def: UpsertQueryDef): QueryBuildResult`
74
-
75
- **DDL - Table:**
76
- - `createTable(def)`, `dropTable(def)`, `renameTable(def)`, `truncate(def)`
77
-
78
- **DDL - Column:**
79
- - `addColumn(def)`, `dropColumn(def)`, `modifyColumn(def)`, `renameColumn(def)`
80
-
81
- **DDL - Constraint:**
82
- - `addPrimaryKey(def)`, `dropPrimaryKey(def)`, `addForeignKey(def)`, `dropForeignKey(def)`, `addIndex(def)`, `dropIndex(def)`
83
-
84
- **DDL - View/Procedure:**
85
- - `createView(def)`, `dropView(def)`, `createProc(def)`, `dropProc(def)`, `execProc(def)`
86
-
87
- **Utils:**
88
- - `clearSchema(def)`, `schemaExists(def)`, `switchFk(def)`
89
-
90
- ---
91
-
92
- ## ExprRendererBase
93
-
94
- Abstract base class for `Expr`/`WhereExpr` to SQL rendering. Implements dispatch logic.
33
+ Abstract base class for rendering `Expr` and `WhereExpr` JSON AST nodes to dialect-specific SQL strings. Each dialect (MySQL, MSSQL, PostgreSQL) extends this class with its own implementations.
95
34
 
96
35
  ```typescript
97
36
  abstract class ExprRendererBase {
98
- constructor(protected buildSelect: (def: SelectQueryDef) => string);
99
-
100
- /** Render a single expression to SQL */
101
- render(expr: Expr | WhereExpr): string;
102
-
103
- /** Render multiple WHERE expressions joined with AND */
104
- renderWhere(exprs: WhereExpr[]): string;
105
-
106
- /** Wrap identifier (table/column name) */
107
- abstract wrap(name: string): string;
108
-
109
- /** Escape string value for SQL literals */
110
- abstract escapeString(value: string): string;
111
-
112
- /** Escape any value to SQL literal */
113
- abstract escapeValue(value: unknown): string;
37
+ render(expr: Expr): string;
38
+ renderWhere(expr: WhereExpr): string;
114
39
  }
115
40
  ```
116
41
 
117
- ### Abstract Methods (per category)
118
-
119
- **Value:** `column`, `value`, `raw`
120
-
121
- **Comparison:** `eq`, `gt`, `lt`, `gte`, `lte`, `between`, `null`, `like`, `regexp`, `in`, `inQuery`, `exists`
122
-
123
- **Logic:** `not`, `and`, `or`
124
-
125
- **String:** `concat`, `left`, `right`, `trim`, `padStart`, `replace`, `upper`, `lower`, `length`, `byteLength`, `substring`, `indexOf`
126
-
127
- **Number:** `abs`, `round`, `ceil`, `floor`
128
-
129
- **Date:** `year`, `month`, `day`, `hour`, `minute`, `second`, `isoWeek`, `isoWeekStartDate`, `isoYearMonth`, `dateDiff`, `dateAdd`, `formatDate`
130
-
131
- **Condition:** `coalesce`, `nullIf`, `is`, `switch`, `if`
132
-
133
- **Aggregate:** `count`, `sum`, `avg`, `max`, `min`
134
-
135
- **Other:** `greatest`, `least`, `rowNum`, `random`, `cast`
136
-
137
- **Window:** `window`
138
-
139
- **System:** `subquery`
42
+ | Method | Description |
43
+ |--------|-------------|
44
+ | `render()` | Render a value expression to SQL |
45
+ | `renderWhere()` | Render a WHERE expression to SQL |
140
46
 
141
- ---
47
+ ## `MysqlQueryBuilder`
142
48
 
143
- ## Dialect Implementations
49
+ MySQL-specific query builder. Extends `QueryBuilderBase`.
144
50
 
145
- ### MysqlQueryBuilder / MysqlExprRenderer
51
+ ```typescript
52
+ class MysqlQueryBuilder extends QueryBuilderBase { ... }
53
+ ```
146
54
 
147
- MySQL-specific implementation.
55
+ ## `MysqlExprRenderer`
148
56
 
149
- - Identifier wrapping: `` `name` ``
150
- - NULL-safe equality: `<=>`
151
- - LATERAL JOIN support
152
- - UUID stored as `BINARY(16)`
153
- - BOOLEAN mapped to `TINYINT(1)`
57
+ MySQL-specific expression renderer. Extends `ExprRendererBase`.
154
58
 
155
- ### MssqlQueryBuilder / MssqlExprRenderer
59
+ ```typescript
60
+ class MysqlExprRenderer extends ExprRendererBase { ... }
61
+ ```
156
62
 
157
- Microsoft SQL Server-specific implementation.
63
+ ## `MssqlQueryBuilder`
158
64
 
159
- - Identifier wrapping: `[name]`
160
- - NULL-safe equality: `(source IS NULL AND target IS NULL) OR source = target`
161
- - CROSS APPLY for lateral joins
162
- - `TOP` and `OFFSET...FETCH` for pagination
163
- - `IDENTITY_INSERT` for explicit AI column values
164
- - UUID mapped to `UNIQUEIDENTIFIER`
65
+ MSSQL-specific query builder. Extends `QueryBuilderBase`.
165
66
 
166
- ### PostgresqlQueryBuilder / PostgresqlExprRenderer
67
+ ```typescript
68
+ class MssqlQueryBuilder extends QueryBuilderBase { ... }
69
+ ```
167
70
 
168
- PostgreSQL-specific implementation.
71
+ ## `MssqlExprRenderer`
169
72
 
170
- - Identifier wrapping: `"name"`
171
- - NULL-safe equality: `IS NOT DISTINCT FROM`
172
- - LATERAL JOIN support
173
- - `LIMIT...OFFSET` for pagination
174
- - UUID as native `UUID` type
175
- - `RETURN QUERY` in stored procedures
73
+ MSSQL-specific expression renderer. Extends `ExprRendererBase`.
176
74
 
177
- ---
75
+ ```typescript
76
+ class MssqlExprRenderer extends ExprRendererBase { ... }
77
+ ```
178
78
 
179
- ## Usage
79
+ ## `PostgresqlQueryBuilder`
180
80
 
181
- Typically used internally by `DbContextExecutor` implementations. Direct usage:
81
+ PostgreSQL-specific query builder. Extends `QueryBuilderBase`.
182
82
 
183
83
  ```typescript
184
- import { createQueryBuilder } from "@simplysm/orm-common";
84
+ class PostgresqlQueryBuilder extends QueryBuilderBase { ... }
85
+ ```
185
86
 
186
- const builder = createQueryBuilder("mysql");
87
+ ## `PostgresqlExprRenderer`
187
88
 
188
- // Build from a Queryable
189
- const qr = db.user()
190
- .where((u) => [expr.eq(u.status, "active")])
191
- .orderBy((u) => u.name);
89
+ PostgreSQL-specific expression renderer. Extends `ExprRendererBase`.
192
90
 
193
- const queryDef = qr.getSelectQueryDef();
194
- const result = builder.build(queryDef);
195
- // result.sql: "SELECT ... FROM `mydb`.`User` AS `T1` WHERE ..."
91
+ ```typescript
92
+ class PostgresqlExprRenderer extends ExprRendererBase { ... }
196
93
  ```