@mikro-orm/sql 7.0.2-dev.8 → 7.0.2

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.
Files changed (87) hide show
  1. package/AbstractSqlConnection.d.ts +95 -47
  2. package/AbstractSqlConnection.js +240 -232
  3. package/AbstractSqlDriver.d.ts +412 -155
  4. package/AbstractSqlDriver.js +2062 -1937
  5. package/AbstractSqlPlatform.d.ts +84 -73
  6. package/AbstractSqlPlatform.js +163 -158
  7. package/PivotCollectionPersister.d.ts +33 -15
  8. package/PivotCollectionPersister.js +158 -160
  9. package/README.md +128 -294
  10. package/SqlEntityManager.d.ts +68 -20
  11. package/SqlEntityManager.js +54 -37
  12. package/SqlEntityRepository.d.ts +15 -14
  13. package/SqlEntityRepository.js +24 -23
  14. package/dialects/mssql/MsSqlNativeQueryBuilder.d.ts +12 -12
  15. package/dialects/mssql/MsSqlNativeQueryBuilder.js +192 -194
  16. package/dialects/mysql/BaseMySqlPlatform.d.ts +64 -45
  17. package/dialects/mysql/BaseMySqlPlatform.js +134 -131
  18. package/dialects/mysql/MySqlExceptionConverter.d.ts +6 -6
  19. package/dialects/mysql/MySqlExceptionConverter.js +91 -77
  20. package/dialects/mysql/MySqlNativeQueryBuilder.d.ts +3 -3
  21. package/dialects/mysql/MySqlNativeQueryBuilder.js +66 -69
  22. package/dialects/mysql/MySqlSchemaHelper.d.ts +39 -39
  23. package/dialects/mysql/MySqlSchemaHelper.js +327 -319
  24. package/dialects/oracledb/OracleDialect.d.ts +81 -52
  25. package/dialects/oracledb/OracleDialect.js +155 -149
  26. package/dialects/oracledb/OracleNativeQueryBuilder.d.ts +12 -12
  27. package/dialects/oracledb/OracleNativeQueryBuilder.js +232 -236
  28. package/dialects/postgresql/BasePostgreSqlPlatform.d.ts +108 -105
  29. package/dialects/postgresql/BasePostgreSqlPlatform.js +351 -350
  30. package/dialects/postgresql/FullTextType.d.ts +10 -6
  31. package/dialects/postgresql/FullTextType.js +51 -51
  32. package/dialects/postgresql/PostgreSqlExceptionConverter.d.ts +5 -5
  33. package/dialects/postgresql/PostgreSqlExceptionConverter.js +55 -43
  34. package/dialects/postgresql/PostgreSqlNativeQueryBuilder.d.ts +1 -1
  35. package/dialects/postgresql/PostgreSqlNativeQueryBuilder.js +4 -4
  36. package/dialects/postgresql/PostgreSqlSchemaHelper.d.ts +102 -82
  37. package/dialects/postgresql/PostgreSqlSchemaHelper.js +711 -683
  38. package/dialects/sqlite/BaseSqliteConnection.d.ts +3 -5
  39. package/dialects/sqlite/BaseSqliteConnection.js +21 -19
  40. package/dialects/sqlite/NodeSqliteDialect.d.ts +1 -1
  41. package/dialects/sqlite/NodeSqliteDialect.js +23 -23
  42. package/dialects/sqlite/SqliteDriver.d.ts +1 -1
  43. package/dialects/sqlite/SqliteDriver.js +3 -3
  44. package/dialects/sqlite/SqliteExceptionConverter.d.ts +6 -6
  45. package/dialects/sqlite/SqliteExceptionConverter.js +67 -51
  46. package/dialects/sqlite/SqliteNativeQueryBuilder.d.ts +2 -2
  47. package/dialects/sqlite/SqliteNativeQueryBuilder.js +7 -7
  48. package/dialects/sqlite/SqlitePlatform.d.ts +63 -72
  49. package/dialects/sqlite/SqlitePlatform.js +139 -139
  50. package/dialects/sqlite/SqliteSchemaHelper.d.ts +70 -60
  51. package/dialects/sqlite/SqliteSchemaHelper.js +533 -520
  52. package/package.json +4 -4
  53. package/plugin/index.d.ts +44 -35
  54. package/plugin/index.js +44 -36
  55. package/plugin/transformer.d.ts +117 -94
  56. package/plugin/transformer.js +890 -881
  57. package/query/ArrayCriteriaNode.d.ts +4 -4
  58. package/query/ArrayCriteriaNode.js +18 -18
  59. package/query/CriteriaNode.d.ts +35 -25
  60. package/query/CriteriaNode.js +133 -123
  61. package/query/CriteriaNodeFactory.d.ts +49 -6
  62. package/query/CriteriaNodeFactory.js +97 -94
  63. package/query/NativeQueryBuilder.d.ts +120 -117
  64. package/query/NativeQueryBuilder.js +484 -480
  65. package/query/ObjectCriteriaNode.d.ts +12 -12
  66. package/query/ObjectCriteriaNode.js +298 -282
  67. package/query/QueryBuilder.d.ts +1546 -904
  68. package/query/QueryBuilder.js +2270 -2145
  69. package/query/QueryBuilderHelper.d.ts +153 -72
  70. package/query/QueryBuilderHelper.js +1079 -1028
  71. package/query/ScalarCriteriaNode.d.ts +3 -3
  72. package/query/ScalarCriteriaNode.js +53 -46
  73. package/query/enums.d.ts +16 -14
  74. package/query/enums.js +16 -14
  75. package/query/raw.d.ts +16 -6
  76. package/query/raw.js +10 -10
  77. package/schema/DatabaseSchema.d.ts +73 -50
  78. package/schema/DatabaseSchema.js +331 -307
  79. package/schema/DatabaseTable.d.ts +96 -73
  80. package/schema/DatabaseTable.js +1012 -927
  81. package/schema/SchemaComparator.d.ts +58 -54
  82. package/schema/SchemaComparator.js +745 -719
  83. package/schema/SchemaHelper.d.ts +110 -80
  84. package/schema/SchemaHelper.js +676 -645
  85. package/schema/SqlSchemaGenerator.d.ts +79 -58
  86. package/schema/SqlSchemaGenerator.js +536 -501
  87. package/typings.d.ts +380 -266
@@ -3,247 +3,243 @@ import { QueryType } from '../../query/enums.js';
3
3
  import { NativeQueryBuilder } from '../../query/NativeQueryBuilder.js';
4
4
  /** @internal */
5
5
  export function markOutBindings(obj) {
6
- Object.defineProperty(obj, '__outBindings', {
7
- value: true,
8
- writable: true,
9
- configurable: true,
10
- enumerable: false,
11
- });
6
+ Object.defineProperty(obj, '__outBindings', {
7
+ value: true,
8
+ writable: true,
9
+ configurable: true,
10
+ enumerable: false,
11
+ });
12
12
  }
13
13
  /** @internal */
14
14
  export class OracleNativeQueryBuilder extends NativeQueryBuilder {
15
- as(alias) {
16
- this.wrap('(', `) ${this.platform.quoteIdentifier(alias)}`);
17
- return this;
15
+ as(alias) {
16
+ this.wrap('(', `) ${this.platform.quoteIdentifier(alias)}`);
17
+ return this;
18
+ }
19
+ compile() {
20
+ if (!this.type) {
21
+ throw new Error('No query type provided');
18
22
  }
19
- compile() {
20
- if (!this.type) {
21
- throw new Error('No query type provided');
22
- }
23
- this.parts.length = 0;
24
- this.params.length = 0;
25
- /* v8 ignore next 3: query comment branch */
26
- if (this.options.comment) {
27
- this.parts.push(...this.options.comment.map(comment => `/* ${comment} */`));
28
- }
29
- let copy;
30
- if (this.options.onConflict && !Utils.isEmpty(Utils.asArray(this.options.data)[0])) {
31
- this.compileUpsert();
32
- }
33
- else {
34
- if (this.options.returning && Array.isArray(this.options.data) && this.options.data.length > 1) {
35
- copy = [...this.options.data];
36
- this.options.data.length = 1;
37
- }
38
- switch (this.type) {
39
- case QueryType.SELECT:
40
- case QueryType.COUNT:
41
- this.compileSelect();
42
- break;
43
- case QueryType.INSERT:
44
- this.compileInsert();
45
- break;
46
- case QueryType.UPDATE:
47
- this.compileUpdate();
48
- break;
49
- case QueryType.DELETE:
50
- this.compileDelete();
51
- break;
52
- case QueryType.TRUNCATE:
53
- this.compileTruncate();
54
- break;
55
- }
56
- this.addOnConflictClause();
57
- }
58
- if (this.options.returning) {
59
- const isUpsert = this.options.onConflict && !Utils.isEmpty(Utils.asArray(this.options.data)[0]);
60
- const prefix = isUpsert ? `${this.getTableName()}.` : '';
61
- const fields = this.options.returning.map(field => prefix + this.quote(Array.isArray(field) ? field[0] : field));
62
- const into = this.options.returning.map(field => ':out_' + (Array.isArray(field) ? field[0] : field));
63
- const outBindings = this.options.returning.map(field => {
64
- const name = 'out_' + (Array.isArray(field) ? field[0] : field);
65
- const type = Array.isArray(field) ? field[1] : 'string';
66
- return [name, type];
67
- });
68
- markOutBindings(outBindings);
69
- this.parts.push(`returning ${fields.join(', ')}`);
70
- this.parts.push(`into ${into.join(', ')}`);
71
- this.params.push(outBindings);
72
- }
73
- this.addLockClause();
74
- if (!copy) {
75
- return this.combineParts();
76
- }
77
- // multi insert with returning
78
- const sql = this.parts.join(' ');
79
- const blockLines = [];
80
- const block2Lines = [];
81
- const keys = Object.keys(copy[0]);
82
- const last = this.params[this.params.length - 1];
83
- /* v8 ignore next 3: defensive check — output bindings are always set by compile() */
84
- if (!Array.isArray(last) || !('__outBindings' in last) || !last.__outBindings) {
85
- throw new Error('Output bindings are required for multi insert with returning');
86
- }
87
- const outBindings = {};
88
- markOutBindings(outBindings);
89
- for (let i = 0; i < copy.length; i++) {
90
- const params = [];
91
- for (const key of keys) {
92
- /* v8 ignore next 3: undefined value branch in multi-insert */
93
- if (typeof copy[i][key] === 'undefined') {
94
- params.push(this.platform.usesDefaultKeyword() ? raw('default') : null);
95
- }
96
- else {
97
- params.push(copy[i][key]);
98
- }
99
- }
100
- // we need to interpolate to allow proper escaping
101
- const formatted = this.platform.formatQuery(sql, params).replaceAll(`'`, `''`);
102
- /* v8 ignore next 3: returning field type branches */
103
- const using = this.options.returning.map(field => {
104
- const name = Array.isArray(field) ? field[0] : field;
105
- const type = Array.isArray(field) ? field[1] : 'string';
106
- outBindings[`out_${name}__${i}`] = {
107
- dir: this.platform.mapToBindType('out'),
108
- type: this.platform.mapToBindType(type),
109
- };
110
- return `out :out_${name}__${i}`;
111
- });
112
- blockLines.push(` execute immediate '${formatted}' using ${using.join(', ')};`);
113
- block2Lines.push(` execute immediate '${sql}' using ${using.join(', ')};`);
114
- }
115
- const block = `begin\n${blockLines.join('\n')}\n end;`;
116
- const block2 = `begin\n${block2Lines.join('\n')}\n end;`;
117
- // save raw query without interpolation for logging,
118
- Object.defineProperty(outBindings, '__rawQuery', {
119
- value: block2,
120
- writable: true,
121
- configurable: true,
122
- enumerable: false,
23
+ this.parts.length = 0;
24
+ this.params.length = 0;
25
+ /* v8 ignore next 3: query comment branch */
26
+ if (this.options.comment) {
27
+ this.parts.push(...this.options.comment.map(comment => `/* ${comment} */`));
28
+ }
29
+ let copy;
30
+ if (this.options.onConflict && !Utils.isEmpty(Utils.asArray(this.options.data)[0])) {
31
+ this.compileUpsert();
32
+ } else {
33
+ if (this.options.returning && Array.isArray(this.options.data) && this.options.data.length > 1) {
34
+ copy = [...this.options.data];
35
+ this.options.data.length = 1;
36
+ }
37
+ switch (this.type) {
38
+ case QueryType.SELECT:
39
+ case QueryType.COUNT:
40
+ this.compileSelect();
41
+ break;
42
+ case QueryType.INSERT:
43
+ this.compileInsert();
44
+ break;
45
+ case QueryType.UPDATE:
46
+ this.compileUpdate();
47
+ break;
48
+ case QueryType.DELETE:
49
+ this.compileDelete();
50
+ break;
51
+ case QueryType.TRUNCATE:
52
+ this.compileTruncate();
53
+ break;
54
+ }
55
+ this.addOnConflictClause();
56
+ }
57
+ if (this.options.returning) {
58
+ const isUpsert = this.options.onConflict && !Utils.isEmpty(Utils.asArray(this.options.data)[0]);
59
+ const prefix = isUpsert ? `${this.getTableName()}.` : '';
60
+ const fields = this.options.returning.map(field => prefix + this.quote(Array.isArray(field) ? field[0] : field));
61
+ const into = this.options.returning.map(field => ':out_' + (Array.isArray(field) ? field[0] : field));
62
+ const outBindings = this.options.returning.map(field => {
63
+ const name = 'out_' + (Array.isArray(field) ? field[0] : field);
64
+ const type = Array.isArray(field) ? field[1] : 'string';
65
+ return [name, type];
66
+ });
67
+ markOutBindings(outBindings);
68
+ this.parts.push(`returning ${fields.join(', ')}`);
69
+ this.parts.push(`into ${into.join(', ')}`);
70
+ this.params.push(outBindings);
71
+ }
72
+ this.addLockClause();
73
+ if (!copy) {
74
+ return this.combineParts();
75
+ }
76
+ // multi insert with returning
77
+ const sql = this.parts.join(' ');
78
+ const blockLines = [];
79
+ const block2Lines = [];
80
+ const keys = Object.keys(copy[0]);
81
+ const last = this.params[this.params.length - 1];
82
+ /* v8 ignore next 3: defensive check — output bindings are always set by compile() */
83
+ if (!Array.isArray(last) || !('__outBindings' in last) || !last.__outBindings) {
84
+ throw new Error('Output bindings are required for multi insert with returning');
85
+ }
86
+ const outBindings = {};
87
+ markOutBindings(outBindings);
88
+ for (let i = 0; i < copy.length; i++) {
89
+ const params = [];
90
+ for (const key of keys) {
91
+ /* v8 ignore next 3: undefined value branch in multi-insert */
92
+ if (typeof copy[i][key] === 'undefined') {
93
+ params.push(this.platform.usesDefaultKeyword() ? raw('default') : null);
94
+ } else {
95
+ params.push(copy[i][key]);
96
+ }
97
+ }
98
+ // we need to interpolate to allow proper escaping
99
+ const formatted = this.platform.formatQuery(sql, params).replaceAll(`'`, `''`);
100
+ /* v8 ignore next 3: returning field type branches */
101
+ const using = this.options.returning.map(field => {
102
+ const name = Array.isArray(field) ? field[0] : field;
103
+ const type = Array.isArray(field) ? field[1] : 'string';
104
+ outBindings[`out_${name}__${i}`] = {
105
+ dir: this.platform.mapToBindType('out'),
106
+ type: this.platform.mapToBindType(type),
107
+ };
108
+ return `out :out_${name}__${i}`;
109
+ });
110
+ blockLines.push(` execute immediate '${formatted}' using ${using.join(', ')};`);
111
+ block2Lines.push(` execute immediate '${sql}' using ${using.join(', ')};`);
112
+ }
113
+ const block = `begin\n${blockLines.join('\n')}\n end;`;
114
+ const block2 = `begin\n${block2Lines.join('\n')}\n end;`;
115
+ // save raw query without interpolation for logging,
116
+ Object.defineProperty(outBindings, '__rawQuery', {
117
+ value: block2,
118
+ writable: true,
119
+ configurable: true,
120
+ enumerable: false,
121
+ });
122
+ this.options.data = copy;
123
+ return { sql: block, params: [outBindings] };
124
+ }
125
+ compileTruncate() {
126
+ super.compileTruncate();
127
+ this.parts.push('drop all storage cascade');
128
+ }
129
+ combineParts() {
130
+ let sql = this.parts.join(' ');
131
+ const last = this.params[this.params.length - 1];
132
+ if (this.options.wrap) {
133
+ const [a, b] = this.options.wrap;
134
+ sql = `${a}${sql}${b}`;
135
+ }
136
+ if (!(Array.isArray(last) && '__outBindings' in last && last.__outBindings)) {
137
+ return { sql, params: this.params };
138
+ }
139
+ const out = this.params.pop();
140
+ const outBindings = {};
141
+ markOutBindings(outBindings);
142
+ this.params.push(outBindings);
143
+ for (const item of out) {
144
+ outBindings[item[0]] = {
145
+ dir: this.platform.mapToBindType('out'),
146
+ type: this.platform.mapToBindType(item[1]),
147
+ };
148
+ }
149
+ return { sql, params: this.params };
150
+ }
151
+ compileUpsert() {
152
+ const clause = this.options.onConflict;
153
+ const dataAsArray = Utils.asArray(this.options.data);
154
+ const keys = Object.keys(dataAsArray[0]);
155
+ const parts = [];
156
+ for (const data of dataAsArray) {
157
+ for (const key of keys) {
158
+ this.params.push(data[key]);
159
+ }
160
+ parts.push(`select ${keys.map(k => `? as ${this.quote(k)}`).join(', ')} from dual`);
161
+ }
162
+ this.parts.push(`merge into ${this.getTableName()}`);
163
+ this.parts.push(`using (${parts.join(' union all ')}) tsource`);
164
+ /* v8 ignore next 4: RawQueryFragment conflict fields branch */
165
+ if (clause.fields instanceof RawQueryFragment) {
166
+ this.parts.push(clause.fields.sql);
167
+ this.params.push(...clause.fields.params);
168
+ } else if (clause.fields.length > 0) {
169
+ const fields = clause.fields.map(field => {
170
+ const col = this.quote(field);
171
+ return `${this.getTableName()}.${col} = tsource.${col}`;
172
+ });
173
+ this.parts.push(`on (${fields.join(' and ')})`);
174
+ }
175
+ const sourceColumns = keys.map(field => `tsource.${this.quote(field)}`).join(', ');
176
+ const destinationColumns = keys.map(field => this.quote(field)).join(', ');
177
+ this.parts.push(`when not matched then insert (${destinationColumns}) values (${sourceColumns})`);
178
+ if (!clause.ignore) {
179
+ /* v8 ignore next: merge type branch */
180
+ if (!clause.merge || Array.isArray(clause.merge)) {
181
+ const mergeParts = (clause.merge || keys)
182
+ .filter(field => !Array.isArray(clause.fields) || !clause.fields.includes(field))
183
+ .filter(field => keys.includes(field)) // only reference columns present in the source data
184
+ .map(column => `${this.quote(column)} = tsource.${this.quote(column)}`);
185
+ /* v8 ignore next 10: empty mergeParts branch */
186
+ if (mergeParts.length > 0) {
187
+ this.parts.push('when matched');
188
+ if (clause.where) {
189
+ this.parts.push(`and ${clause.where.sql}`);
190
+ this.params.push(...clause.where.params);
191
+ }
192
+ this.parts.push('then update set');
193
+ this.parts.push(mergeParts.join(', '));
194
+ }
195
+ } /* v8 ignore start: object-form merge branch */ else if (typeof clause.merge === 'object') {
196
+ this.parts.push('when matched');
197
+ if (clause.where) {
198
+ this.parts.push(`and ${clause.where.sql}`);
199
+ this.params.push(...clause.where.params);
200
+ }
201
+ this.parts.push('then update set');
202
+ const parts = Object.entries(clause.merge).map(([key, value]) => {
203
+ this.params.push(value);
204
+ return `${this.getTableName()}.${this.quote(key)} = ?`;
123
205
  });
124
- this.options.data = copy;
125
- return { sql: block, params: [outBindings] };
126
- }
127
- compileTruncate() {
128
- super.compileTruncate();
129
- this.parts.push('drop all storage cascade');
130
- }
131
- combineParts() {
132
- let sql = this.parts.join(' ');
133
- const last = this.params[this.params.length - 1];
134
- if (this.options.wrap) {
135
- const [a, b] = this.options.wrap;
136
- sql = `${a}${sql}${b}`;
137
- }
138
- if (!(Array.isArray(last) && '__outBindings' in last && last.__outBindings)) {
139
- return { sql, params: this.params };
140
- }
141
- const out = this.params.pop();
142
- const outBindings = {};
143
- markOutBindings(outBindings);
144
- this.params.push(outBindings);
145
- for (const item of out) {
146
- outBindings[item[0]] = {
147
- dir: this.platform.mapToBindType('out'),
148
- type: this.platform.mapToBindType(item[1]),
149
- };
150
- }
151
- return { sql, params: this.params };
152
- }
153
- compileUpsert() {
154
- const clause = this.options.onConflict;
155
- const dataAsArray = Utils.asArray(this.options.data);
156
- const keys = Object.keys(dataAsArray[0]);
157
- const parts = [];
158
- for (const data of dataAsArray) {
159
- for (const key of keys) {
160
- this.params.push(data[key]);
161
- }
162
- parts.push(`select ${keys.map(k => `? as ${this.quote(k)}`).join(', ')} from dual`);
163
- }
164
- this.parts.push(`merge into ${this.getTableName()}`);
165
- this.parts.push(`using (${parts.join(' union all ')}) tsource`);
166
- /* v8 ignore next 4: RawQueryFragment conflict fields branch */
167
- if (clause.fields instanceof RawQueryFragment) {
168
- this.parts.push(clause.fields.sql);
169
- this.params.push(...clause.fields.params);
170
- }
171
- else if (clause.fields.length > 0) {
172
- const fields = clause.fields.map(field => {
173
- const col = this.quote(field);
174
- return `${this.getTableName()}.${col} = tsource.${col}`;
175
- });
176
- this.parts.push(`on (${fields.join(' and ')})`);
177
- }
178
- const sourceColumns = keys.map(field => `tsource.${this.quote(field)}`).join(', ');
179
- const destinationColumns = keys.map(field => this.quote(field)).join(', ');
180
- this.parts.push(`when not matched then insert (${destinationColumns}) values (${sourceColumns})`);
181
- if (!clause.ignore) {
182
- /* v8 ignore next: merge type branch */
183
- if (!clause.merge || Array.isArray(clause.merge)) {
184
- const mergeParts = (clause.merge || keys)
185
- .filter(field => !Array.isArray(clause.fields) || !clause.fields.includes(field))
186
- .filter((field) => keys.includes(field)) // only reference columns present in the source data
187
- .map((column) => `${this.quote(column)} = tsource.${this.quote(column)}`);
188
- /* v8 ignore next 10: empty mergeParts branch */
189
- if (mergeParts.length > 0) {
190
- this.parts.push('when matched');
191
- if (clause.where) {
192
- this.parts.push(`and ${clause.where.sql}`);
193
- this.params.push(...clause.where.params);
194
- }
195
- this.parts.push('then update set');
196
- this.parts.push(mergeParts.join(', '));
197
- }
198
- } /* v8 ignore start: object-form merge branch */
199
- else if (typeof clause.merge === 'object') {
200
- this.parts.push('when matched');
201
- if (clause.where) {
202
- this.parts.push(`and ${clause.where.sql}`);
203
- this.params.push(...clause.where.params);
204
- }
205
- this.parts.push('then update set');
206
- const parts = Object.entries(clause.merge).map(([key, value]) => {
207
- this.params.push(value);
208
- return `${this.getTableName()}.${this.quote(key)} = ?`;
209
- });
210
- this.parts.push(parts.join(', '));
211
- }
212
- /* v8 ignore stop */
213
- }
206
+ this.parts.push(parts.join(', '));
207
+ }
208
+ /* v8 ignore stop */
214
209
  }
215
- compileSelect() {
216
- this.parts.push('select');
217
- this.addHintComment();
218
- this.parts.push(`${this.getFields()} from ${this.getTableName()}`);
219
- if (this.options.joins) {
220
- for (const join of this.options.joins) {
221
- this.parts.push(join.sql);
222
- this.params.push(...join.params);
223
- }
224
- }
225
- if (this.options.where?.sql.trim()) {
226
- this.parts.push(`where ${this.options.where.sql}`);
227
- this.params.push(...this.options.where.params);
228
- }
229
- if (this.options.groupBy) {
230
- const fields = this.options.groupBy.map(field => this.quote(field));
231
- this.parts.push(`group by ${fields.join(', ')}`);
232
- }
233
- if (this.options.having) {
234
- this.parts.push(`having ${this.options.having.sql}`);
235
- this.params.push(...this.options.having.params);
236
- }
237
- if (this.options.orderBy) {
238
- this.parts.push(`order by ${this.options.orderBy}`);
239
- }
240
- if (this.options.offset != null) {
241
- this.parts.push(`offset ? rows`);
242
- this.params.push(this.options.offset);
243
- }
244
- if (this.options.limit != null) {
245
- this.parts.push(`fetch next ? rows only`);
246
- this.params.push(this.options.limit);
247
- }
210
+ }
211
+ compileSelect() {
212
+ this.parts.push('select');
213
+ this.addHintComment();
214
+ this.parts.push(`${this.getFields()} from ${this.getTableName()}`);
215
+ if (this.options.joins) {
216
+ for (const join of this.options.joins) {
217
+ this.parts.push(join.sql);
218
+ this.params.push(...join.params);
219
+ }
220
+ }
221
+ if (this.options.where?.sql.trim()) {
222
+ this.parts.push(`where ${this.options.where.sql}`);
223
+ this.params.push(...this.options.where.params);
224
+ }
225
+ if (this.options.groupBy) {
226
+ const fields = this.options.groupBy.map(field => this.quote(field));
227
+ this.parts.push(`group by ${fields.join(', ')}`);
228
+ }
229
+ if (this.options.having) {
230
+ this.parts.push(`having ${this.options.having.sql}`);
231
+ this.params.push(...this.options.having.params);
232
+ }
233
+ if (this.options.orderBy) {
234
+ this.parts.push(`order by ${this.options.orderBy}`);
235
+ }
236
+ if (this.options.offset != null) {
237
+ this.parts.push(`offset ? rows`);
238
+ this.params.push(this.options.offset);
239
+ }
240
+ if (this.options.limit != null) {
241
+ this.parts.push(`fetch next ? rows only`);
242
+ this.params.push(this.options.limit);
248
243
  }
244
+ }
249
245
  }