@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,137 +1,140 @@
1
- import { Utils, QueryOrder, DecimalType, DoubleType, } from '@mikro-orm/core';
1
+ import { Utils, QueryOrder, DecimalType, DoubleType } from '@mikro-orm/core';
2
2
  import { MySqlSchemaHelper } from './MySqlSchemaHelper.js';
3
3
  import { MySqlExceptionConverter } from './MySqlExceptionConverter.js';
4
4
  import { AbstractSqlPlatform } from '../../AbstractSqlPlatform.js';
5
5
  import { MySqlNativeQueryBuilder } from './MySqlNativeQueryBuilder.js';
6
6
  export class BaseMySqlPlatform extends AbstractSqlPlatform {
7
- schemaHelper = new MySqlSchemaHelper(this);
8
- exceptionConverter = new MySqlExceptionConverter();
9
- #jsonTypeCasts = {
10
- string: 'text',
11
- number: 'double',
12
- bigint: 'bigint',
13
- boolean: 'unsigned',
14
- };
15
- ORDER_BY_NULLS_TRANSLATE = {
16
- [QueryOrder.asc_nulls_first]: 'is not null',
17
- [QueryOrder.asc_nulls_last]: 'is null',
18
- [QueryOrder.desc_nulls_first]: 'is not null',
19
- [QueryOrder.desc_nulls_last]: 'is null',
20
- };
21
- /** @internal */
22
- createNativeQueryBuilder() {
23
- return new MySqlNativeQueryBuilder(this);
24
- }
25
- getDefaultCharset() {
26
- return 'utf8mb4';
27
- }
28
- init(orm) {
29
- super.init(orm);
30
- orm.config.get('schemaGenerator').disableForeignKeysForClear ??= true;
31
- }
32
- getBeginTransactionSQL(options) {
33
- if (options?.isolationLevel || options?.readOnly) {
34
- const parts = [];
35
- if (options.isolationLevel) {
36
- parts.push(`isolation level ${options.isolationLevel}`);
37
- }
38
- if (options.readOnly) {
39
- parts.push('read only');
40
- }
41
- const sql = `set transaction ${parts.join(', ')}`;
42
- return [sql, 'begin'];
43
- }
44
- return ['begin'];
45
- }
46
- convertJsonToDatabaseValue(value, context) {
47
- if (context?.mode === 'query') {
48
- return value;
49
- }
50
- return JSON.stringify(value);
51
- }
52
- getJsonIndexDefinition(index) {
53
- return index.columnNames.map(column => {
54
- if (!column.includes('.')) {
55
- return column;
56
- }
57
- const [root, ...path] = column.split('.');
58
- return `(json_value(${this.quoteIdentifier(root)}, '$.${path.join('.')}' returning ${index.options?.returning ?? 'char(255)'}))`;
59
- });
60
- }
61
- getBooleanTypeDeclarationSQL() {
62
- return 'tinyint(1)';
63
- }
64
- normalizeColumnType(type, options) {
65
- const simpleType = this.extractSimpleType(type);
66
- if (['decimal', 'numeric'].includes(simpleType)) {
67
- return this.getDecimalTypeDeclarationSQL(options);
68
- }
69
- return type;
70
- }
71
- getDefaultMappedType(type) {
72
- if (type === 'tinyint(1)') {
73
- return super.getDefaultMappedType('boolean');
74
- }
75
- return super.getDefaultMappedType(type);
76
- }
77
- isNumericColumn(mappedType) {
78
- return super.isNumericColumn(mappedType) || [DecimalType, DoubleType].some(t => mappedType instanceof t);
79
- }
80
- supportsUnsigned() {
81
- return true;
82
- }
83
- /**
84
- * Returns the default name of index for the given columns
85
- * cannot go past 64 character length for identifiers in MySQL
86
- */
87
- getIndexName(tableName, columns, type) {
88
- if (type === 'primary') {
89
- return this.getDefaultPrimaryName(tableName, columns);
90
- }
91
- const indexName = super.getIndexName(tableName, columns, type);
92
- if (indexName.length > 64) {
93
- return `${indexName.substring(0, 56 - type.length)}_${Utils.hash(indexName, 5)}_${type}`;
94
- }
95
- return indexName;
96
- }
97
- getDefaultPrimaryName(tableName, columns) {
98
- return 'PRIMARY'; // https://dev.mysql.com/doc/refman/8.0/en/create-table.html#create-table-indexes-keys
99
- }
100
- supportsCreatingFullTextIndex() {
101
- return true;
102
- }
103
- getFullTextWhereClause() {
104
- return `match(:column:) against (:query in boolean mode)`;
105
- }
106
- getFullTextIndexExpression(indexName, schemaName, tableName, columns) {
107
- /* v8 ignore next */
108
- const quotedTableName = this.quoteIdentifier(schemaName ? `${schemaName}.${tableName}` : tableName);
109
- const quotedColumnNames = columns.map(c => this.quoteIdentifier(c.name));
110
- const quotedIndexName = this.quoteIdentifier(indexName);
111
- return `alter table ${quotedTableName} add fulltext index ${quotedIndexName}(${quotedColumnNames.join(',')})`;
112
- }
113
- getOrderByExpression(column, direction, collation) {
114
- const ret = [];
115
- const dir = direction.toLowerCase();
116
- const col = collation ? `${column} collate ${this.quoteCollation(collation)}` : column;
117
- if (dir in this.ORDER_BY_NULLS_TRANSLATE) {
118
- ret.push(`${col} ${this.ORDER_BY_NULLS_TRANSLATE[dir]}`);
119
- }
120
- ret.push(`${col} ${dir.replace(/(\s|nulls|first|last)*/gi, '')}`);
121
- return ret;
122
- }
123
- getJsonArrayFromSQL(column, alias, properties) {
124
- const columns = properties
125
- .map(p => `${this.quoteIdentifier(p.name)} ${this.#jsonTypeCasts[p.type] ?? 'text'} path '$.${this.quoteJsonKey(p.name)}'`)
126
- .join(', ');
127
- return `json_table(${column}, '$[*]' columns (${columns})) as ${this.quoteIdentifier(alias)}`;
128
- }
129
- // MySQL does not support correlated json_table inside EXISTS subqueries,
130
- // so we use a semi-join via the comma-join pattern instead.
131
- getJsonArrayExistsSQL(from, where) {
132
- return `(select 1 from ${from} where ${where} limit 1) is not null`;
133
- }
134
- getDefaultClientUrl() {
135
- return 'mysql://root@127.0.0.1:3306';
136
- }
7
+ schemaHelper = new MySqlSchemaHelper(this);
8
+ exceptionConverter = new MySqlExceptionConverter();
9
+ #jsonTypeCasts = {
10
+ string: 'text',
11
+ number: 'double',
12
+ bigint: 'bigint',
13
+ boolean: 'unsigned',
14
+ };
15
+ ORDER_BY_NULLS_TRANSLATE = {
16
+ [QueryOrder.asc_nulls_first]: 'is not null',
17
+ [QueryOrder.asc_nulls_last]: 'is null',
18
+ [QueryOrder.desc_nulls_first]: 'is not null',
19
+ [QueryOrder.desc_nulls_last]: 'is null',
20
+ };
21
+ /** @internal */
22
+ createNativeQueryBuilder() {
23
+ return new MySqlNativeQueryBuilder(this);
24
+ }
25
+ getDefaultCharset() {
26
+ return 'utf8mb4';
27
+ }
28
+ init(orm) {
29
+ super.init(orm);
30
+ orm.config.get('schemaGenerator').disableForeignKeysForClear ??= true;
31
+ }
32
+ getBeginTransactionSQL(options) {
33
+ if (options?.isolationLevel || options?.readOnly) {
34
+ const parts = [];
35
+ if (options.isolationLevel) {
36
+ parts.push(`isolation level ${options.isolationLevel}`);
37
+ }
38
+ if (options.readOnly) {
39
+ parts.push('read only');
40
+ }
41
+ const sql = `set transaction ${parts.join(', ')}`;
42
+ return [sql, 'begin'];
43
+ }
44
+ return ['begin'];
45
+ }
46
+ convertJsonToDatabaseValue(value, context) {
47
+ if (context?.mode === 'query') {
48
+ return value;
49
+ }
50
+ return JSON.stringify(value);
51
+ }
52
+ getJsonIndexDefinition(index) {
53
+ return index.columnNames.map(column => {
54
+ if (!column.includes('.')) {
55
+ return column;
56
+ }
57
+ const [root, ...path] = column.split('.');
58
+ return `(json_value(${this.quoteIdentifier(root)}, '$.${path.join('.')}' returning ${index.options?.returning ?? 'char(255)'}))`;
59
+ });
60
+ }
61
+ getBooleanTypeDeclarationSQL() {
62
+ return 'tinyint(1)';
63
+ }
64
+ normalizeColumnType(type, options) {
65
+ const simpleType = this.extractSimpleType(type);
66
+ if (['decimal', 'numeric'].includes(simpleType)) {
67
+ return this.getDecimalTypeDeclarationSQL(options);
68
+ }
69
+ return type;
70
+ }
71
+ getDefaultMappedType(type) {
72
+ if (type === 'tinyint(1)') {
73
+ return super.getDefaultMappedType('boolean');
74
+ }
75
+ return super.getDefaultMappedType(type);
76
+ }
77
+ isNumericColumn(mappedType) {
78
+ return super.isNumericColumn(mappedType) || [DecimalType, DoubleType].some(t => mappedType instanceof t);
79
+ }
80
+ supportsUnsigned() {
81
+ return true;
82
+ }
83
+ /**
84
+ * Returns the default name of index for the given columns
85
+ * cannot go past 64 character length for identifiers in MySQL
86
+ */
87
+ getIndexName(tableName, columns, type) {
88
+ if (type === 'primary') {
89
+ return this.getDefaultPrimaryName(tableName, columns);
90
+ }
91
+ const indexName = super.getIndexName(tableName, columns, type);
92
+ if (indexName.length > 64) {
93
+ return `${indexName.substring(0, 56 - type.length)}_${Utils.hash(indexName, 5)}_${type}`;
94
+ }
95
+ return indexName;
96
+ }
97
+ getDefaultPrimaryName(tableName, columns) {
98
+ return 'PRIMARY'; // https://dev.mysql.com/doc/refman/8.0/en/create-table.html#create-table-indexes-keys
99
+ }
100
+ supportsCreatingFullTextIndex() {
101
+ return true;
102
+ }
103
+ getFullTextWhereClause() {
104
+ return `match(:column:) against (:query in boolean mode)`;
105
+ }
106
+ getFullTextIndexExpression(indexName, schemaName, tableName, columns) {
107
+ /* v8 ignore next */
108
+ const quotedTableName = this.quoteIdentifier(schemaName ? `${schemaName}.${tableName}` : tableName);
109
+ const quotedColumnNames = columns.map(c => this.quoteIdentifier(c.name));
110
+ const quotedIndexName = this.quoteIdentifier(indexName);
111
+ return `alter table ${quotedTableName} add fulltext index ${quotedIndexName}(${quotedColumnNames.join(',')})`;
112
+ }
113
+ getOrderByExpression(column, direction, collation) {
114
+ const ret = [];
115
+ const dir = direction.toLowerCase();
116
+ const col = collation ? `${column} collate ${this.quoteCollation(collation)}` : column;
117
+ if (dir in this.ORDER_BY_NULLS_TRANSLATE) {
118
+ ret.push(`${col} ${this.ORDER_BY_NULLS_TRANSLATE[dir]}`);
119
+ }
120
+ ret.push(`${col} ${dir.replace(/(\s|nulls|first|last)*/gi, '')}`);
121
+ return ret;
122
+ }
123
+ getJsonArrayFromSQL(column, alias, properties) {
124
+ const columns = properties
125
+ .map(
126
+ p =>
127
+ `${this.quoteIdentifier(p.name)} ${this.#jsonTypeCasts[p.type] ?? 'text'} path '$.${this.quoteJsonKey(p.name)}'`,
128
+ )
129
+ .join(', ');
130
+ return `json_table(${column}, '$[*]' columns (${columns})) as ${this.quoteIdentifier(alias)}`;
131
+ }
132
+ // MySQL does not support correlated json_table inside EXISTS subqueries,
133
+ // so we use a semi-join via the comma-join pattern instead.
134
+ getJsonArrayExistsSQL(from, where) {
135
+ return `(select 1 from ${from} where ${where} limit 1) is not null`;
136
+ }
137
+ getDefaultClientUrl() {
138
+ return 'mysql://root@127.0.0.1:3306';
139
+ }
137
140
  }
@@ -1,9 +1,9 @@
1
1
  import { ExceptionConverter, type Dictionary, type DriverException } from '@mikro-orm/core';
2
2
  export declare class MySqlExceptionConverter extends ExceptionConverter {
3
- /**
4
- * @see http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html
5
- * @see http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
6
- * @see https://github.com/doctrine/dbal/blob/master/src/Driver/AbstractMySQLDriver.php
7
- */
8
- convertException(exception: Error & Dictionary): DriverException;
3
+ /**
4
+ * @see http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html
5
+ * @see http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
6
+ * @see https://github.com/doctrine/dbal/blob/master/src/Driver/AbstractMySQLDriver.php
7
+ */
8
+ convertException(exception: Error & Dictionary): DriverException;
9
9
  }
@@ -1,80 +1,94 @@
1
- import { DeadlockException, LockWaitTimeoutException, TableExistsException, TableNotFoundException, ForeignKeyConstraintViolationException, UniqueConstraintViolationException, InvalidFieldNameException, NonUniqueFieldNameException, SyntaxErrorException, ConnectionException, NotNullConstraintViolationException, ExceptionConverter, CheckConstraintViolationException, } from '@mikro-orm/core';
1
+ import {
2
+ DeadlockException,
3
+ LockWaitTimeoutException,
4
+ TableExistsException,
5
+ TableNotFoundException,
6
+ ForeignKeyConstraintViolationException,
7
+ UniqueConstraintViolationException,
8
+ InvalidFieldNameException,
9
+ NonUniqueFieldNameException,
10
+ SyntaxErrorException,
11
+ ConnectionException,
12
+ NotNullConstraintViolationException,
13
+ ExceptionConverter,
14
+ CheckConstraintViolationException,
15
+ } from '@mikro-orm/core';
2
16
  export class MySqlExceptionConverter extends ExceptionConverter {
3
- /**
4
- * @see http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html
5
- * @see http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
6
- * @see https://github.com/doctrine/dbal/blob/master/src/Driver/AbstractMySQLDriver.php
7
- */
8
- convertException(exception) {
9
- /* v8 ignore next */
10
- switch (exception.errno) {
11
- case 1213:
12
- return new DeadlockException(exception);
13
- case 1205:
14
- return new LockWaitTimeoutException(exception);
15
- case 1050:
16
- return new TableExistsException(exception);
17
- case 1051:
18
- case 1146:
19
- return new TableNotFoundException(exception);
20
- case 1216:
21
- case 1217:
22
- case 1451:
23
- case 1452:
24
- case 1701:
25
- return new ForeignKeyConstraintViolationException(exception);
26
- case 3819:
27
- case 4025:
28
- return new CheckConstraintViolationException(exception);
29
- case 1062:
30
- case 1557:
31
- case 1569:
32
- case 1586:
33
- return new UniqueConstraintViolationException(exception);
34
- case 1054:
35
- case 1166:
36
- case 1611:
37
- return new InvalidFieldNameException(exception);
38
- case 1052:
39
- case 1060:
40
- case 1110:
41
- return new NonUniqueFieldNameException(exception);
42
- case 1064:
43
- case 1149:
44
- case 1287:
45
- case 1341:
46
- case 1342:
47
- case 1343:
48
- case 1344:
49
- case 1382:
50
- case 1479:
51
- case 1541:
52
- case 1554:
53
- case 1626:
54
- return new SyntaxErrorException(exception);
55
- case 1044:
56
- case 1045:
57
- case 1046:
58
- case 1049:
59
- case 1095:
60
- case 1142:
61
- case 1143:
62
- case 1227:
63
- case 1370:
64
- case 1429:
65
- case 2002:
66
- case 2005:
67
- return new ConnectionException(exception);
68
- case 1048:
69
- case 1121:
70
- case 1138:
71
- case 1171:
72
- case 1252:
73
- case 1263:
74
- case 1364:
75
- case 1566:
76
- return new NotNullConstraintViolationException(exception);
77
- }
78
- return super.convertException(exception);
17
+ /**
18
+ * @see http://dev.mysql.com/doc/refman/5.7/en/error-messages-client.html
19
+ * @see http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
20
+ * @see https://github.com/doctrine/dbal/blob/master/src/Driver/AbstractMySQLDriver.php
21
+ */
22
+ convertException(exception) {
23
+ /* v8 ignore next */
24
+ switch (exception.errno) {
25
+ case 1213:
26
+ return new DeadlockException(exception);
27
+ case 1205:
28
+ return new LockWaitTimeoutException(exception);
29
+ case 1050:
30
+ return new TableExistsException(exception);
31
+ case 1051:
32
+ case 1146:
33
+ return new TableNotFoundException(exception);
34
+ case 1216:
35
+ case 1217:
36
+ case 1451:
37
+ case 1452:
38
+ case 1701:
39
+ return new ForeignKeyConstraintViolationException(exception);
40
+ case 3819:
41
+ case 4025:
42
+ return new CheckConstraintViolationException(exception);
43
+ case 1062:
44
+ case 1557:
45
+ case 1569:
46
+ case 1586:
47
+ return new UniqueConstraintViolationException(exception);
48
+ case 1054:
49
+ case 1166:
50
+ case 1611:
51
+ return new InvalidFieldNameException(exception);
52
+ case 1052:
53
+ case 1060:
54
+ case 1110:
55
+ return new NonUniqueFieldNameException(exception);
56
+ case 1064:
57
+ case 1149:
58
+ case 1287:
59
+ case 1341:
60
+ case 1342:
61
+ case 1343:
62
+ case 1344:
63
+ case 1382:
64
+ case 1479:
65
+ case 1541:
66
+ case 1554:
67
+ case 1626:
68
+ return new SyntaxErrorException(exception);
69
+ case 1044:
70
+ case 1045:
71
+ case 1046:
72
+ case 1049:
73
+ case 1095:
74
+ case 1142:
75
+ case 1143:
76
+ case 1227:
77
+ case 1370:
78
+ case 1429:
79
+ case 2002:
80
+ case 2005:
81
+ return new ConnectionException(exception);
82
+ case 1048:
83
+ case 1121:
84
+ case 1138:
85
+ case 1171:
86
+ case 1252:
87
+ case 1263:
88
+ case 1364:
89
+ case 1566:
90
+ return new NotNullConstraintViolationException(exception);
79
91
  }
92
+ return super.convertException(exception);
93
+ }
80
94
  }
@@ -1,7 +1,7 @@
1
1
  import { NativeQueryBuilder } from '../../query/NativeQueryBuilder.js';
2
2
  /** @internal */
3
3
  export declare class MySqlNativeQueryBuilder extends NativeQueryBuilder {
4
- protected compileInsert(): void;
5
- protected addLockClause(): void;
6
- protected addOnConflictClause(): void;
4
+ protected compileInsert(): void;
5
+ protected addLockClause(): void;
6
+ protected addOnConflictClause(): void;
7
7
  }
@@ -2,76 +2,73 @@ import { LockMode, RawQueryFragment, Utils } from '@mikro-orm/core';
2
2
  import { NativeQueryBuilder } from '../../query/NativeQueryBuilder.js';
3
3
  /** @internal */
4
4
  export class MySqlNativeQueryBuilder extends NativeQueryBuilder {
5
- compileInsert() {
6
- if (!this.options.data) {
7
- throw new Error('No data provided');
8
- }
9
- this.parts.push('insert');
10
- if (this.options.onConflict?.ignore) {
11
- this.parts.push('ignore');
12
- }
13
- this.addHintComment();
14
- this.parts.push(`into ${this.getTableName()}`);
15
- if (Object.keys(this.options.data).length === 0) {
16
- this.parts.push('default values');
17
- return;
18
- }
19
- const parts = this.processInsertData();
20
- this.parts.push(parts.join(', '));
5
+ compileInsert() {
6
+ if (!this.options.data) {
7
+ throw new Error('No data provided');
21
8
  }
22
- addLockClause() {
23
- if (!this.options.lockMode) {
24
- return;
25
- }
26
- const map = {
27
- [LockMode.PESSIMISTIC_READ]: 'lock in share mode',
28
- [LockMode.PESSIMISTIC_WRITE]: 'for update',
29
- [LockMode.PESSIMISTIC_PARTIAL_WRITE]: 'for update skip locked',
30
- [LockMode.PESSIMISTIC_WRITE_OR_FAIL]: 'for update nowait',
31
- [LockMode.PESSIMISTIC_PARTIAL_READ]: 'lock in share mode skip locked',
32
- [LockMode.PESSIMISTIC_READ_OR_FAIL]: 'lock in share mode nowait',
33
- };
34
- if (this.options.lockMode !== LockMode.OPTIMISTIC) {
35
- this.parts.push(map[this.options.lockMode]);
36
- }
9
+ this.parts.push('insert');
10
+ if (this.options.onConflict?.ignore) {
11
+ this.parts.push('ignore');
37
12
  }
38
- addOnConflictClause() {
39
- const clause = this.options.onConflict;
40
- if (!clause || clause.ignore) {
41
- return;
42
- }
43
- if (clause.merge) {
44
- this.parts.push('on duplicate key update');
45
- if (Utils.isObject(clause.merge)) {
46
- const fields = Object.keys(clause.merge).map(field => {
47
- this.params.push(clause.merge[field]);
48
- return `${this.quote(field)} = ?`;
49
- });
50
- this.parts.push(fields.join(', '));
51
- }
52
- else if (clause.merge.length === 0) {
53
- const dataAsArray = Utils.asArray(this.options.data);
54
- const keys = Object.keys(dataAsArray[0]);
55
- this.parts.push(keys.map(key => `${this.quote(key)} = values(${this.quote(key)})`).join(', '));
56
- }
57
- else {
58
- const fields = clause.merge.map(key => `${this.quote(key)} = values(${this.quote(key)})`);
59
- this.parts.push(fields.join(', '));
60
- }
61
- if (clause.where) {
62
- this.parts.push(`where ${clause.where.sql}`);
63
- this.params.push(...clause.where.params);
64
- }
65
- return;
66
- }
67
- this.parts.push('on conflict');
68
- if (clause.fields instanceof RawQueryFragment) {
69
- this.parts.push(clause.fields.sql);
70
- this.params.push(...clause.fields.params);
71
- }
72
- else if (clause.fields.length > 0) {
73
- const fields = clause.fields.map(field => this.quote(field));
74
- this.parts.push(`(${fields.join(', ')})`);
75
- }
13
+ this.addHintComment();
14
+ this.parts.push(`into ${this.getTableName()}`);
15
+ if (Object.keys(this.options.data).length === 0) {
16
+ this.parts.push('default values');
17
+ return;
76
18
  }
19
+ const parts = this.processInsertData();
20
+ this.parts.push(parts.join(', '));
21
+ }
22
+ addLockClause() {
23
+ if (!this.options.lockMode) {
24
+ return;
25
+ }
26
+ const map = {
27
+ [LockMode.PESSIMISTIC_READ]: 'lock in share mode',
28
+ [LockMode.PESSIMISTIC_WRITE]: 'for update',
29
+ [LockMode.PESSIMISTIC_PARTIAL_WRITE]: 'for update skip locked',
30
+ [LockMode.PESSIMISTIC_WRITE_OR_FAIL]: 'for update nowait',
31
+ [LockMode.PESSIMISTIC_PARTIAL_READ]: 'lock in share mode skip locked',
32
+ [LockMode.PESSIMISTIC_READ_OR_FAIL]: 'lock in share mode nowait',
33
+ };
34
+ if (this.options.lockMode !== LockMode.OPTIMISTIC) {
35
+ this.parts.push(map[this.options.lockMode]);
36
+ }
37
+ }
38
+ addOnConflictClause() {
39
+ const clause = this.options.onConflict;
40
+ if (!clause || clause.ignore) {
41
+ return;
42
+ }
43
+ if (clause.merge) {
44
+ this.parts.push('on duplicate key update');
45
+ if (Utils.isObject(clause.merge)) {
46
+ const fields = Object.keys(clause.merge).map(field => {
47
+ this.params.push(clause.merge[field]);
48
+ return `${this.quote(field)} = ?`;
49
+ });
50
+ this.parts.push(fields.join(', '));
51
+ } else if (clause.merge.length === 0) {
52
+ const dataAsArray = Utils.asArray(this.options.data);
53
+ const keys = Object.keys(dataAsArray[0]);
54
+ this.parts.push(keys.map(key => `${this.quote(key)} = values(${this.quote(key)})`).join(', '));
55
+ } else {
56
+ const fields = clause.merge.map(key => `${this.quote(key)} = values(${this.quote(key)})`);
57
+ this.parts.push(fields.join(', '));
58
+ }
59
+ if (clause.where) {
60
+ this.parts.push(`where ${clause.where.sql}`);
61
+ this.params.push(...clause.where.params);
62
+ }
63
+ return;
64
+ }
65
+ this.parts.push('on conflict');
66
+ if (clause.fields instanceof RawQueryFragment) {
67
+ this.parts.push(clause.fields.sql);
68
+ this.params.push(...clause.fields.params);
69
+ } else if (clause.fields.length > 0) {
70
+ const fields = clause.fields.map(field => this.quote(field));
71
+ this.parts.push(`(${fields.join(', ')})`);
72
+ }
73
+ }
77
74
  }