@mikro-orm/sql 7.0.4-dev.9 → 7.0.4

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 +94 -58
  2. package/AbstractSqlConnection.js +235 -238
  3. package/AbstractSqlDriver.d.ts +410 -155
  4. package/AbstractSqlDriver.js +2064 -1941
  5. package/AbstractSqlPlatform.d.ts +83 -73
  6. package/AbstractSqlPlatform.js +162 -158
  7. package/PivotCollectionPersister.d.ts +33 -15
  8. package/PivotCollectionPersister.js +158 -160
  9. package/README.md +1 -1
  10. package/SqlEntityManager.d.ts +67 -22
  11. package/SqlEntityManager.js +54 -38
  12. package/SqlEntityRepository.d.ts +14 -14
  13. package/SqlEntityRepository.js +23 -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 +109 -105
  29. package/dialects/postgresql/BasePostgreSqlPlatform.js +354 -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 +733 -705
  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 +3 -3
  53. package/plugin/index.d.ts +42 -35
  54. package/plugin/index.js +43 -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 +118 -118
  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 +1557 -905
  68. package/query/QueryBuilder.js +2322 -2192
  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 +14 -14
  74. package/query/enums.js +14 -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 +70 -66
  82. package/schema/SchemaComparator.js +766 -740
  83. package/schema/SchemaHelper.d.ts +109 -95
  84. package/schema/SchemaHelper.js +675 -659
  85. package/schema/SqlSchemaGenerator.d.ts +78 -58
  86. package/schema/SqlSchemaGenerator.js +535 -501
  87. package/typings.d.ts +380 -266
@@ -1,362 +1,366 @@
1
- import { ALIAS_REPLACEMENT, ARRAY_OPERATORS, raw, RawQueryFragment, Type, Utils, } from '@mikro-orm/core';
1
+ import { ALIAS_REPLACEMENT, ARRAY_OPERATORS, raw, RawQueryFragment, Type, Utils } from '@mikro-orm/core';
2
2
  import { AbstractSqlPlatform } from '../../AbstractSqlPlatform.js';
3
3
  import { PostgreSqlNativeQueryBuilder } from './PostgreSqlNativeQueryBuilder.js';
4
4
  import { PostgreSqlSchemaHelper } from './PostgreSqlSchemaHelper.js';
5
5
  import { PostgreSqlExceptionConverter } from './PostgreSqlExceptionConverter.js';
6
6
  import { FullTextType } from './FullTextType.js';
7
7
  export class BasePostgreSqlPlatform extends AbstractSqlPlatform {
8
- schemaHelper = new PostgreSqlSchemaHelper(this);
9
- exceptionConverter = new PostgreSqlExceptionConverter();
10
- /** Maps JS runtime type names to PostgreSQL cast types for JSON property access. @internal */
11
- #jsonTypeCasts = { number: 'float8', bigint: 'int8', boolean: 'bool' };
12
- createNativeQueryBuilder() {
13
- return new PostgreSqlNativeQueryBuilder(this);
14
- }
15
- usesReturningStatement() {
16
- return true;
17
- }
18
- usesCascadeStatement() {
19
- return true;
20
- }
21
- supportsNativeEnums() {
22
- return true;
23
- }
24
- usesEnumCheckConstraints() {
25
- return true;
26
- }
27
- supportsMaterializedViews() {
28
- return true;
29
- }
30
- supportsCustomPrimaryKeyNames() {
31
- return true;
32
- }
33
- getCurrentTimestampSQL(length) {
34
- return `current_timestamp(${length})`;
35
- }
36
- getDateTimeTypeDeclarationSQL(column) {
37
- /* v8 ignore next */
38
- return 'timestamptz' + (column.length != null ? `(${column.length})` : '');
39
- }
40
- getDefaultDateTimeLength() {
41
- return 6;
42
- }
43
- getTimeTypeDeclarationSQL() {
44
- return 'time(0)';
45
- }
46
- getIntegerTypeDeclarationSQL(column) {
47
- if (column.autoincrement && !column.generated) {
48
- return 'serial';
49
- }
50
- return 'int';
51
- }
52
- getBigIntTypeDeclarationSQL(column) {
53
- /* v8 ignore next */
54
- if (column.autoincrement) {
55
- return `bigserial`;
56
- }
57
- return 'bigint';
58
- }
59
- getTinyIntTypeDeclarationSQL(column) {
60
- return 'smallint';
61
- }
62
- getUuidTypeDeclarationSQL(column) {
63
- return `uuid`;
64
- }
65
- getFullTextWhereClause(prop) {
66
- if (prop.customType instanceof FullTextType) {
67
- return `:column: @@ plainto_tsquery('${prop.customType.regconfig}', :query)`;
68
- }
69
- /* v8 ignore next */
70
- if (prop.columnTypes[0] === 'tsvector') {
71
- return `:column: @@ plainto_tsquery('simple', :query)`;
72
- }
73
- return `to_tsvector('simple', :column:) @@ plainto_tsquery('simple', :query)`;
74
- }
75
- supportsCreatingFullTextIndex() {
76
- return true;
77
- }
78
- getFullTextIndexExpression(indexName, schemaName, tableName, columns) {
79
- /* v8 ignore next */
80
- const quotedTableName = this.quoteIdentifier(schemaName ? `${schemaName}.${tableName}` : tableName);
81
- const quotedColumnNames = columns.map(c => this.quoteIdentifier(c.name));
82
- const quotedIndexName = this.quoteIdentifier(indexName);
83
- if (columns.length === 1 && columns[0].type === 'tsvector') {
84
- return `create index ${quotedIndexName} on ${quotedTableName} using gin(${quotedColumnNames[0]})`;
85
- }
86
- return `create index ${quotedIndexName} on ${quotedTableName} using gin(to_tsvector('simple', ${quotedColumnNames.join(` || ' ' || `)}))`;
87
- }
88
- normalizeColumnType(type, options) {
89
- const simpleType = this.extractSimpleType(type);
90
- if (['int', 'int4', 'integer'].includes(simpleType)) {
91
- return this.getIntegerTypeDeclarationSQL({});
92
- }
93
- if (['bigint', 'int8'].includes(simpleType)) {
94
- return this.getBigIntTypeDeclarationSQL({});
95
- }
96
- if (['smallint', 'int2'].includes(simpleType)) {
97
- return this.getSmallIntTypeDeclarationSQL({});
98
- }
99
- if (['boolean', 'bool'].includes(simpleType)) {
100
- return this.getBooleanTypeDeclarationSQL();
101
- }
102
- if (['varchar', 'character varying'].includes(simpleType)) {
103
- return this.getVarcharTypeDeclarationSQL(options);
104
- }
105
- if (['char', 'bpchar'].includes(simpleType)) {
106
- return this.getCharTypeDeclarationSQL(options);
107
- }
108
- if (['decimal', 'numeric'].includes(simpleType)) {
109
- return this.getDecimalTypeDeclarationSQL(options);
110
- }
111
- if (['interval'].includes(simpleType)) {
112
- return this.getIntervalTypeDeclarationSQL(options);
113
- }
114
- return super.normalizeColumnType(type, options);
115
- }
116
- getMappedType(type) {
117
- switch (this.extractSimpleType(type)) {
118
- case 'tsvector':
119
- return Type.getType(FullTextType);
120
- default:
121
- return super.getMappedType(type);
122
- }
123
- }
124
- getRegExpOperator(val, flags) {
125
- /* v8 ignore next */
126
- if ((val instanceof RegExp && val.flags.includes('i')) || flags?.includes('i')) {
127
- return '~*';
128
- }
129
- return '~';
130
- }
8
+ schemaHelper = new PostgreSqlSchemaHelper(this);
9
+ exceptionConverter = new PostgreSqlExceptionConverter();
10
+ /** Maps JS runtime type names to PostgreSQL cast types for JSON property access. @internal */
11
+ #jsonTypeCasts = { number: 'float8', bigint: 'int8', boolean: 'bool' };
12
+ createNativeQueryBuilder() {
13
+ return new PostgreSqlNativeQueryBuilder(this);
14
+ }
15
+ usesReturningStatement() {
16
+ return true;
17
+ }
18
+ usesCascadeStatement() {
19
+ return true;
20
+ }
21
+ supportsNativeEnums() {
22
+ return true;
23
+ }
24
+ usesEnumCheckConstraints() {
25
+ return true;
26
+ }
27
+ getEnumArrayCheckConstraintExpression(column, items) {
28
+ return `${this.quoteIdentifier(column)} <@ array[${items.map(item => `'${item}'::text`).join(', ')}]`;
29
+ }
30
+ supportsMaterializedViews() {
31
+ return true;
32
+ }
33
+ supportsCustomPrimaryKeyNames() {
34
+ return true;
35
+ }
36
+ getCurrentTimestampSQL(length) {
37
+ return `current_timestamp(${length})`;
38
+ }
39
+ getDateTimeTypeDeclarationSQL(column) {
131
40
  /* v8 ignore next */
132
- getRegExpValue(val) {
133
- if (val.flags.includes('i')) {
134
- return { $re: val.source, $flags: val.flags };
135
- }
136
- return { $re: val.source };
137
- }
138
- isBigIntProperty(prop) {
139
- return super.isBigIntProperty(prop) || ['bigserial', 'int8'].includes(prop.columnTypes?.[0]);
140
- }
141
- getArrayDeclarationSQL() {
142
- return 'text[]';
143
- }
144
- getFloatDeclarationSQL() {
145
- return 'real';
146
- }
147
- getDoubleDeclarationSQL() {
148
- return 'double precision';
149
- }
150
- getEnumTypeDeclarationSQL(column) {
151
- /* v8 ignore next */
152
- if (column.nativeEnumName) {
153
- return column.nativeEnumName;
154
- }
155
- if (column.items?.every(item => typeof item === 'string')) {
156
- return 'text';
157
- }
158
- return `smallint`;
159
- }
160
- supportsMultipleStatements() {
161
- return true;
162
- }
163
- getBeginTransactionSQL(options) {
164
- if (options?.isolationLevel || options?.readOnly) {
165
- let sql = 'start transaction';
166
- sql += options.isolationLevel ? ` isolation level ${options.isolationLevel}` : '';
167
- sql += options.readOnly ? ` read only` : '';
168
- return [sql];
169
- }
170
- return ['begin'];
171
- }
172
- marshallArray(values) {
173
- const quote = (v) => (v === '' || /["{},\\]/.exec(v) ? JSON.stringify(v) : v);
174
- return `{${values.map(v => quote('' + v)).join(',')}}`;
175
- }
41
+ return 'timestamptz' + (column.length != null ? `(${column.length})` : '');
42
+ }
43
+ getDefaultDateTimeLength() {
44
+ return 6;
45
+ }
46
+ getTimeTypeDeclarationSQL() {
47
+ return 'time(0)';
48
+ }
49
+ getIntegerTypeDeclarationSQL(column) {
50
+ if (column.autoincrement && !column.generated) {
51
+ return 'serial';
52
+ }
53
+ return 'int';
54
+ }
55
+ getBigIntTypeDeclarationSQL(column) {
176
56
  /* v8 ignore next */
177
- unmarshallArray(value) {
178
- if (value === '{}') {
179
- return [];
180
- }
181
- return value
182
- .substring(1, value.length - 1)
183
- .split(',')
184
- .map(v => {
185
- if (v === `""`) {
186
- return '';
187
- }
188
- if (/"(.*)"/.exec(v)) {
189
- return v.substring(1, v.length - 1).replaceAll('\\"', '"');
190
- }
191
- return v;
192
- });
193
- }
194
- getVarcharTypeDeclarationSQL(column) {
195
- if (column.length === -1) {
196
- return 'varchar';
197
- }
198
- return super.getVarcharTypeDeclarationSQL(column);
199
- }
200
- getCharTypeDeclarationSQL(column) {
201
- if (column.length === -1) {
202
- return 'char';
203
- }
204
- return super.getCharTypeDeclarationSQL(column);
205
- }
206
- getIntervalTypeDeclarationSQL(column) {
207
- return 'interval' + (column.length != null ? `(${column.length})` : '');
208
- }
209
- getBlobDeclarationSQL() {
210
- return 'bytea';
211
- }
212
- getJsonDeclarationSQL() {
213
- return 'jsonb';
214
- }
215
- getSearchJsonPropertyKey(path, type, aliased, value) {
216
- const first = path.shift();
217
- const last = path.pop();
218
- const root = this.quoteIdentifier(aliased ? `${ALIAS_REPLACEMENT}.${first}` : first);
219
- type = typeof type === 'string' ? this.getMappedType(type).runtimeType : String(type);
220
- const cast = (key) => raw(type in this.#jsonTypeCasts ? `(${key})::${this.#jsonTypeCasts[type]}` : key);
221
- let lastOperator = '->>';
222
- // force `->` for operator payloads with array values
223
- if (Utils.isPlainObject(value) &&
224
- Object.keys(value).every(key => ARRAY_OPERATORS.includes(key) && Array.isArray(value[key]))) {
225
- lastOperator = '->';
226
- }
227
- if (path.length === 0) {
228
- return cast(`${root}${lastOperator}'${last}'`);
229
- }
230
- return cast(`${root}->${path.map(a => this.quoteValue(a)).join('->')}${lastOperator}'${last}'`);
231
- }
232
- getJsonIndexDefinition(index) {
233
- return index.columnNames.map(column => {
234
- if (!column.includes('.')) {
235
- return column;
236
- }
237
- const path = column.split('.');
238
- const first = path.shift();
239
- const last = path.pop();
240
- if (path.length === 0) {
241
- return `(${this.quoteIdentifier(first)}->>${this.quoteValue(last)})`;
242
- }
243
- return `(${this.quoteIdentifier(first)}->${path.map(c => this.quoteValue(c)).join('->')}->>${this.quoteValue(last)})`;
244
- });
245
- }
246
- quoteIdentifier(id, quote = '"') {
247
- if (RawQueryFragment.isKnownFragment(id)) {
248
- return super.quoteIdentifier(id);
249
- }
250
- return `${quote}${id.toString().replace('.', `${quote}.${quote}`)}${quote}`;
251
- }
252
- pad(number, digits) {
253
- return String(number).padStart(digits, '0');
57
+ if (column.autoincrement) {
58
+ return `bigserial`;
59
+ }
60
+ return 'bigint';
61
+ }
62
+ getTinyIntTypeDeclarationSQL(column) {
63
+ return 'smallint';
64
+ }
65
+ getUuidTypeDeclarationSQL(column) {
66
+ return `uuid`;
67
+ }
68
+ getFullTextWhereClause(prop) {
69
+ if (prop.customType instanceof FullTextType) {
70
+ return `:column: @@ plainto_tsquery('${prop.customType.regconfig}', :query)`;
254
71
  }
255
- /** @internal */
256
- formatDate(date) {
257
- if (this.timezone === 'Z') {
258
- return date.toISOString();
259
- }
260
- let offset = -date.getTimezoneOffset();
261
- let year = date.getFullYear();
262
- const isBCYear = year < 1;
263
- /* v8 ignore next */
264
- if (isBCYear) {
265
- year = Math.abs(year) + 1;
266
- }
267
- const datePart = `${this.pad(year, 4)}-${this.pad(date.getMonth() + 1, 2)}-${this.pad(date.getDate(), 2)}`;
268
- const timePart = `${this.pad(date.getHours(), 2)}:${this.pad(date.getMinutes(), 2)}:${this.pad(date.getSeconds(), 2)}.${this.pad(date.getMilliseconds(), 3)}`;
269
- let ret = `${datePart}T${timePart}`;
270
- /* v8 ignore next */
271
- if (offset < 0) {
272
- ret += '-';
273
- offset *= -1;
274
- }
275
- else {
276
- ret += '+';
277
- }
278
- ret += this.pad(Math.floor(offset / 60), 2) + ':' + this.pad(offset % 60, 2);
279
- /* v8 ignore next */
280
- if (isBCYear) {
281
- ret += ' BC';
282
- }
283
- return ret;
284
- }
285
- indexForeignKeys() {
286
- return false;
287
- }
288
- getDefaultMappedType(type) {
289
- const normalizedType = this.extractSimpleType(type);
290
- const map = {
291
- int2: 'smallint',
292
- smallserial: 'smallint',
293
- int: 'integer',
294
- int4: 'integer',
295
- serial: 'integer',
296
- serial4: 'integer',
297
- int8: 'bigint',
298
- bigserial: 'bigint',
299
- serial8: 'bigint',
300
- numeric: 'decimal',
301
- bool: 'boolean',
302
- real: 'float',
303
- float4: 'float',
304
- float8: 'double',
305
- timestamp: 'datetime',
306
- timestamptz: 'datetime',
307
- bytea: 'blob',
308
- jsonb: 'json',
309
- 'character varying': 'varchar',
310
- bpchar: 'character',
311
- };
312
- return super.getDefaultMappedType(map[normalizedType] ?? type);
313
- }
314
- supportsSchemas() {
315
- return true;
316
- }
317
- getDefaultSchemaName() {
318
- return 'public';
319
- }
320
- /**
321
- * Returns the default name of index for the given columns
322
- * cannot go past 63 character length for identifiers in MySQL
323
- */
324
- getIndexName(tableName, columns, type) {
325
- const indexName = super.getIndexName(tableName, columns, type);
326
- if (indexName.length > 63) {
327
- const suffix = type === 'primary' ? 'pkey' : type;
328
- return `${indexName.substring(0, 55 - type.length)}_${Utils.hash(indexName, 5)}_${suffix}`;
329
- }
330
- return indexName;
331
- }
332
- getDefaultPrimaryName(tableName, columns) {
333
- const indexName = `${tableName}_pkey`;
334
- if (indexName.length > 63) {
335
- return `${indexName.substring(0, 55 - 'pkey'.length)}_${Utils.hash(indexName, 5)}_pkey`;
336
- }
337
- return indexName;
338
- }
339
- /**
340
- * @inheritDoc
341
- */
342
- castColumn(prop) {
343
- switch (prop?.columnTypes?.[0]) {
344
- case this.getUuidTypeDeclarationSQL({}):
345
- return '::text';
346
- case this.getBooleanTypeDeclarationSQL():
347
- return '::int';
348
- default:
349
- return '';
350
- }
351
- }
352
- getJsonArrayFromSQL(column, alias, _properties) {
353
- return `jsonb_array_elements(${column}) as ${this.quoteIdentifier(alias)}`;
354
- }
355
- getJsonArrayElementPropertySQL(alias, property, type) {
356
- const expr = `${this.quoteIdentifier(alias)}->>${this.quoteValue(property)}`;
357
- return type in this.#jsonTypeCasts ? `(${expr})::${this.#jsonTypeCasts[type]}` : expr;
72
+ /* v8 ignore next */
73
+ if (prop.columnTypes[0] === 'tsvector') {
74
+ return `:column: @@ plainto_tsquery('simple', :query)`;
75
+ }
76
+ return `to_tsvector('simple', :column:) @@ plainto_tsquery('simple', :query)`;
77
+ }
78
+ supportsCreatingFullTextIndex() {
79
+ return true;
80
+ }
81
+ getFullTextIndexExpression(indexName, schemaName, tableName, columns) {
82
+ /* v8 ignore next */
83
+ const quotedTableName = this.quoteIdentifier(schemaName ? `${schemaName}.${tableName}` : tableName);
84
+ const quotedColumnNames = columns.map(c => this.quoteIdentifier(c.name));
85
+ const quotedIndexName = this.quoteIdentifier(indexName);
86
+ if (columns.length === 1 && columns[0].type === 'tsvector') {
87
+ return `create index ${quotedIndexName} on ${quotedTableName} using gin(${quotedColumnNames[0]})`;
88
+ }
89
+ return `create index ${quotedIndexName} on ${quotedTableName} using gin(to_tsvector('simple', ${quotedColumnNames.join(` || ' ' || `)}))`;
90
+ }
91
+ normalizeColumnType(type, options) {
92
+ const simpleType = this.extractSimpleType(type);
93
+ if (['int', 'int4', 'integer'].includes(simpleType)) {
94
+ return this.getIntegerTypeDeclarationSQL({});
95
+ }
96
+ if (['bigint', 'int8'].includes(simpleType)) {
97
+ return this.getBigIntTypeDeclarationSQL({});
98
+ }
99
+ if (['smallint', 'int2'].includes(simpleType)) {
100
+ return this.getSmallIntTypeDeclarationSQL({});
101
+ }
102
+ if (['boolean', 'bool'].includes(simpleType)) {
103
+ return this.getBooleanTypeDeclarationSQL();
104
+ }
105
+ if (['varchar', 'character varying'].includes(simpleType)) {
106
+ return this.getVarcharTypeDeclarationSQL(options);
107
+ }
108
+ if (['char', 'bpchar'].includes(simpleType)) {
109
+ return this.getCharTypeDeclarationSQL(options);
110
+ }
111
+ if (['decimal', 'numeric'].includes(simpleType)) {
112
+ return this.getDecimalTypeDeclarationSQL(options);
113
+ }
114
+ if (['interval'].includes(simpleType)) {
115
+ return this.getIntervalTypeDeclarationSQL(options);
116
+ }
117
+ return super.normalizeColumnType(type, options);
118
+ }
119
+ getMappedType(type) {
120
+ switch (this.extractSimpleType(type)) {
121
+ case 'tsvector':
122
+ return Type.getType(FullTextType);
123
+ default:
124
+ return super.getMappedType(type);
125
+ }
126
+ }
127
+ getRegExpOperator(val, flags) {
128
+ /* v8 ignore next */
129
+ if ((val instanceof RegExp && val.flags.includes('i')) || flags?.includes('i')) {
130
+ return '~*';
131
+ }
132
+ return '~';
133
+ }
134
+ /* v8 ignore next */
135
+ getRegExpValue(val) {
136
+ if (val.flags.includes('i')) {
137
+ return { $re: val.source, $flags: val.flags };
138
+ }
139
+ return { $re: val.source };
140
+ }
141
+ isBigIntProperty(prop) {
142
+ return super.isBigIntProperty(prop) || ['bigserial', 'int8'].includes(prop.columnTypes?.[0]);
143
+ }
144
+ getArrayDeclarationSQL() {
145
+ return 'text[]';
146
+ }
147
+ getFloatDeclarationSQL() {
148
+ return 'real';
149
+ }
150
+ getDoubleDeclarationSQL() {
151
+ return 'double precision';
152
+ }
153
+ getEnumTypeDeclarationSQL(column) {
154
+ /* v8 ignore next */
155
+ if (column.nativeEnumName) {
156
+ return column.nativeEnumName;
157
+ }
158
+ if (column.items?.every(item => typeof item === 'string')) {
159
+ return 'text';
160
+ }
161
+ return `smallint`;
162
+ }
163
+ supportsMultipleStatements() {
164
+ return true;
165
+ }
166
+ getBeginTransactionSQL(options) {
167
+ if (options?.isolationLevel || options?.readOnly) {
168
+ let sql = 'start transaction';
169
+ sql += options.isolationLevel ? ` isolation level ${options.isolationLevel}` : '';
170
+ sql += options.readOnly ? ` read only` : '';
171
+ return [sql];
172
+ }
173
+ return ['begin'];
174
+ }
175
+ marshallArray(values) {
176
+ const quote = v => (v === '' || /["{},\\]/.exec(v) ? JSON.stringify(v) : v);
177
+ return `{${values.map(v => quote('' + v)).join(',')}}`;
178
+ }
179
+ /* v8 ignore next */
180
+ unmarshallArray(value) {
181
+ if (value === '{}') {
182
+ return [];
183
+ }
184
+ return value
185
+ .substring(1, value.length - 1)
186
+ .split(',')
187
+ .map(v => {
188
+ if (v === `""`) {
189
+ return '';
190
+ }
191
+ if (/"(.*)"/.exec(v)) {
192
+ return v.substring(1, v.length - 1).replaceAll('\\"', '"');
193
+ }
194
+ return v;
195
+ });
196
+ }
197
+ getVarcharTypeDeclarationSQL(column) {
198
+ if (column.length === -1) {
199
+ return 'varchar';
200
+ }
201
+ return super.getVarcharTypeDeclarationSQL(column);
202
+ }
203
+ getCharTypeDeclarationSQL(column) {
204
+ if (column.length === -1) {
205
+ return 'char';
206
+ }
207
+ return super.getCharTypeDeclarationSQL(column);
208
+ }
209
+ getIntervalTypeDeclarationSQL(column) {
210
+ return 'interval' + (column.length != null ? `(${column.length})` : '');
211
+ }
212
+ getBlobDeclarationSQL() {
213
+ return 'bytea';
214
+ }
215
+ getJsonDeclarationSQL() {
216
+ return 'jsonb';
217
+ }
218
+ getSearchJsonPropertyKey(path, type, aliased, value) {
219
+ const first = path.shift();
220
+ const last = path.pop();
221
+ const root = this.quoteIdentifier(aliased ? `${ALIAS_REPLACEMENT}.${first}` : first);
222
+ type = typeof type === 'string' ? this.getMappedType(type).runtimeType : String(type);
223
+ const cast = key => raw(type in this.#jsonTypeCasts ? `(${key})::${this.#jsonTypeCasts[type]}` : key);
224
+ let lastOperator = '->>';
225
+ // force `->` for operator payloads with array values
226
+ if (
227
+ Utils.isPlainObject(value) &&
228
+ Object.keys(value).every(key => ARRAY_OPERATORS.includes(key) && Array.isArray(value[key]))
229
+ ) {
230
+ lastOperator = '->';
231
+ }
232
+ if (path.length === 0) {
233
+ return cast(`${root}${lastOperator}'${last}'`);
234
+ }
235
+ return cast(`${root}->${path.map(a => this.quoteValue(a)).join('->')}${lastOperator}'${last}'`);
236
+ }
237
+ getJsonIndexDefinition(index) {
238
+ return index.columnNames.map(column => {
239
+ if (!column.includes('.')) {
240
+ return column;
241
+ }
242
+ const path = column.split('.');
243
+ const first = path.shift();
244
+ const last = path.pop();
245
+ if (path.length === 0) {
246
+ return `(${this.quoteIdentifier(first)}->>${this.quoteValue(last)})`;
247
+ }
248
+ return `(${this.quoteIdentifier(first)}->${path.map(c => this.quoteValue(c)).join('->')}->>${this.quoteValue(last)})`;
249
+ });
250
+ }
251
+ quoteIdentifier(id, quote = '"') {
252
+ if (RawQueryFragment.isKnownFragment(id)) {
253
+ return super.quoteIdentifier(id);
254
+ }
255
+ return `${quote}${id.toString().replace('.', `${quote}.${quote}`)}${quote}`;
256
+ }
257
+ pad(number, digits) {
258
+ return String(number).padStart(digits, '0');
259
+ }
260
+ /** @internal */
261
+ formatDate(date) {
262
+ if (this.timezone === 'Z') {
263
+ return date.toISOString();
264
+ }
265
+ let offset = -date.getTimezoneOffset();
266
+ let year = date.getFullYear();
267
+ const isBCYear = year < 1;
268
+ /* v8 ignore next */
269
+ if (isBCYear) {
270
+ year = Math.abs(year) + 1;
358
271
  }
359
- getDefaultClientUrl() {
360
- return 'postgresql://postgres@127.0.0.1:5432';
272
+ const datePart = `${this.pad(year, 4)}-${this.pad(date.getMonth() + 1, 2)}-${this.pad(date.getDate(), 2)}`;
273
+ const timePart = `${this.pad(date.getHours(), 2)}:${this.pad(date.getMinutes(), 2)}:${this.pad(date.getSeconds(), 2)}.${this.pad(date.getMilliseconds(), 3)}`;
274
+ let ret = `${datePart}T${timePart}`;
275
+ /* v8 ignore next */
276
+ if (offset < 0) {
277
+ ret += '-';
278
+ offset *= -1;
279
+ } else {
280
+ ret += '+';
361
281
  }
282
+ ret += this.pad(Math.floor(offset / 60), 2) + ':' + this.pad(offset % 60, 2);
283
+ /* v8 ignore next */
284
+ if (isBCYear) {
285
+ ret += ' BC';
286
+ }
287
+ return ret;
288
+ }
289
+ indexForeignKeys() {
290
+ return false;
291
+ }
292
+ getDefaultMappedType(type) {
293
+ const normalizedType = this.extractSimpleType(type);
294
+ const map = {
295
+ int2: 'smallint',
296
+ smallserial: 'smallint',
297
+ int: 'integer',
298
+ int4: 'integer',
299
+ serial: 'integer',
300
+ serial4: 'integer',
301
+ int8: 'bigint',
302
+ bigserial: 'bigint',
303
+ serial8: 'bigint',
304
+ numeric: 'decimal',
305
+ bool: 'boolean',
306
+ real: 'float',
307
+ float4: 'float',
308
+ float8: 'double',
309
+ timestamp: 'datetime',
310
+ timestamptz: 'datetime',
311
+ bytea: 'blob',
312
+ jsonb: 'json',
313
+ 'character varying': 'varchar',
314
+ bpchar: 'character',
315
+ };
316
+ return super.getDefaultMappedType(map[normalizedType] ?? type);
317
+ }
318
+ supportsSchemas() {
319
+ return true;
320
+ }
321
+ getDefaultSchemaName() {
322
+ return 'public';
323
+ }
324
+ /**
325
+ * Returns the default name of index for the given columns
326
+ * cannot go past 63 character length for identifiers in MySQL
327
+ */
328
+ getIndexName(tableName, columns, type) {
329
+ const indexName = super.getIndexName(tableName, columns, type);
330
+ if (indexName.length > 63) {
331
+ const suffix = type === 'primary' ? 'pkey' : type;
332
+ return `${indexName.substring(0, 55 - type.length)}_${Utils.hash(indexName, 5)}_${suffix}`;
333
+ }
334
+ return indexName;
335
+ }
336
+ getDefaultPrimaryName(tableName, columns) {
337
+ const indexName = `${tableName}_pkey`;
338
+ if (indexName.length > 63) {
339
+ return `${indexName.substring(0, 55 - 'pkey'.length)}_${Utils.hash(indexName, 5)}_pkey`;
340
+ }
341
+ return indexName;
342
+ }
343
+ /**
344
+ * @inheritDoc
345
+ */
346
+ castColumn(prop) {
347
+ switch (prop?.columnTypes?.[0]) {
348
+ case this.getUuidTypeDeclarationSQL({}):
349
+ return '::text';
350
+ case this.getBooleanTypeDeclarationSQL():
351
+ return '::int';
352
+ default:
353
+ return '';
354
+ }
355
+ }
356
+ getJsonArrayFromSQL(column, alias, _properties) {
357
+ return `jsonb_array_elements(${column}) as ${this.quoteIdentifier(alias)}`;
358
+ }
359
+ getJsonArrayElementPropertySQL(alias, property, type) {
360
+ const expr = `${this.quoteIdentifier(alias)}->>${this.quoteValue(property)}`;
361
+ return type in this.#jsonTypeCasts ? `(${expr})::${this.#jsonTypeCasts[type]}` : expr;
362
+ }
363
+ getDefaultClientUrl() {
364
+ return 'postgresql://postgres@127.0.0.1:5432';
365
+ }
362
366
  }