@mikro-orm/mssql 7.0.5-dev.8 → 7.0.5
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.
- package/MsSqlConnection.d.ts +4 -4
- package/MsSqlConnection.js +72 -69
- package/MsSqlDriver.d.ts +30 -7
- package/MsSqlDriver.js +74 -71
- package/MsSqlExceptionConverter.d.ts +5 -5
- package/MsSqlExceptionConverter.js +44 -33
- package/MsSqlMikroORM.d.ts +50 -12
- package/MsSqlMikroORM.js +14 -14
- package/MsSqlPlatform.d.ts +85 -68
- package/MsSqlPlatform.js +238 -223
- package/MsSqlQueryBuilder.d.ts +10 -3
- package/MsSqlQueryBuilder.js +16 -16
- package/MsSqlSchemaGenerator.d.ts +3 -3
- package/MsSqlSchemaGenerator.js +22 -22
- package/MsSqlSchemaHelper.d.ts +82 -52
- package/MsSqlSchemaHelper.js +529 -514
- package/README.md +1 -1
- package/UnicodeCharacterType.d.ts +2 -2
- package/UnicodeCharacterType.js +7 -7
- package/UnicodeStringType.d.ts +17 -14
- package/UnicodeStringType.js +42 -42
- package/index.d.ts +5 -1
- package/index.js +1 -1
- package/package.json +4 -4
package/MsSqlPlatform.d.ts
CHANGED
|
@@ -1,75 +1,92 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
AbstractSqlPlatform,
|
|
3
|
+
type EntityManager,
|
|
4
|
+
type EntityMetadata,
|
|
5
|
+
type IDatabaseDriver,
|
|
6
|
+
type IPrimaryKey,
|
|
7
|
+
type MikroORM,
|
|
8
|
+
MsSqlNativeQueryBuilder,
|
|
9
|
+
type Primary,
|
|
10
|
+
QueryOrder,
|
|
11
|
+
RawQueryFragment,
|
|
12
|
+
Type,
|
|
13
|
+
} from '@mikro-orm/sql';
|
|
2
14
|
import { MsSqlSchemaHelper } from './MsSqlSchemaHelper.js';
|
|
3
15
|
import { MsSqlExceptionConverter } from './MsSqlExceptionConverter.js';
|
|
4
16
|
import { MsSqlSchemaGenerator } from './MsSqlSchemaGenerator.js';
|
|
5
17
|
import type { MsSqlDriver } from './MsSqlDriver.js';
|
|
6
18
|
/** Platform implementation for Microsoft SQL Server. */
|
|
7
19
|
export declare class MsSqlPlatform extends AbstractSqlPlatform {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}[]
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
20
|
+
#private;
|
|
21
|
+
protected readonly schemaHelper: MsSqlSchemaHelper;
|
|
22
|
+
protected readonly exceptionConverter: MsSqlExceptionConverter;
|
|
23
|
+
/** @inheritDoc */
|
|
24
|
+
lookupExtensions(orm: MikroORM<MsSqlDriver>): void;
|
|
25
|
+
/** @inheritDoc */
|
|
26
|
+
init(orm: MikroORM): void;
|
|
27
|
+
getRollbackToSavepointSQL(savepointName: string): string;
|
|
28
|
+
getSavepointSQL(savepointName: string): string;
|
|
29
|
+
/** @internal */
|
|
30
|
+
createNativeQueryBuilder(): MsSqlNativeQueryBuilder;
|
|
31
|
+
usesOutputStatement(): boolean;
|
|
32
|
+
convertDateToJSValue(value: string | Date): string;
|
|
33
|
+
convertsJsonAutomatically(): boolean;
|
|
34
|
+
indexForeignKeys(): boolean;
|
|
35
|
+
supportsSchemas(): boolean;
|
|
36
|
+
getCurrentTimestampSQL(length: number): string;
|
|
37
|
+
getDateTimeTypeDeclarationSQL(column: { length?: number }): string;
|
|
38
|
+
getDefaultDateTimeLength(): number;
|
|
39
|
+
getFloatDeclarationSQL(): string;
|
|
40
|
+
getDoubleDeclarationSQL(): string;
|
|
41
|
+
getBooleanTypeDeclarationSQL(): string;
|
|
42
|
+
getRegExpOperator(): string;
|
|
43
|
+
getBlobDeclarationSQL(): string;
|
|
44
|
+
getJsonDeclarationSQL(): string;
|
|
45
|
+
getVarcharTypeDeclarationSQL(column: { length?: number }): string;
|
|
46
|
+
getEnumTypeDeclarationSQL(column: {
|
|
47
|
+
items?: unknown[];
|
|
48
|
+
fieldNames: string[];
|
|
49
|
+
length?: number;
|
|
50
|
+
unsigned?: boolean;
|
|
51
|
+
autoincrement?: boolean;
|
|
52
|
+
}): string;
|
|
53
|
+
normalizeColumnType(
|
|
54
|
+
type: string,
|
|
55
|
+
options: {
|
|
56
|
+
length?: number;
|
|
57
|
+
precision?: number;
|
|
58
|
+
scale?: number;
|
|
59
|
+
},
|
|
60
|
+
): string;
|
|
61
|
+
getDefaultMappedType(type: string): Type<unknown>;
|
|
62
|
+
getDefaultSchemaName(): string | undefined;
|
|
63
|
+
getUuidTypeDeclarationSQL(column: { length?: number }): string;
|
|
64
|
+
validateMetadata(meta: EntityMetadata): void;
|
|
65
|
+
getSearchJsonPropertyKey(path: string[], type: string, aliased: boolean, value?: unknown): string | RawQueryFragment;
|
|
66
|
+
getJsonArrayFromSQL(
|
|
67
|
+
column: string,
|
|
68
|
+
alias: string,
|
|
69
|
+
properties: {
|
|
70
|
+
name: string;
|
|
71
|
+
type: string;
|
|
72
|
+
}[],
|
|
73
|
+
): string;
|
|
74
|
+
normalizePrimaryKey<T extends number | string = number | string>(data: Primary<T> | IPrimaryKey | string): T;
|
|
75
|
+
usesEnumCheckConstraints(): boolean;
|
|
76
|
+
supportsMultipleCascadePaths(): boolean;
|
|
77
|
+
supportsMultipleStatements(): boolean;
|
|
78
|
+
quoteIdentifier(
|
|
79
|
+
id:
|
|
80
|
+
| string
|
|
81
|
+
| {
|
|
82
|
+
toString: () => string;
|
|
83
|
+
},
|
|
84
|
+
): string;
|
|
85
|
+
escape(value: any): string;
|
|
86
|
+
getSchemaGenerator(driver: IDatabaseDriver, em?: EntityManager): MsSqlSchemaGenerator;
|
|
87
|
+
allowsComparingTuples(): boolean;
|
|
88
|
+
/** @internal MSSQL collation names are not quoted. */
|
|
89
|
+
quoteCollation(collation: string): string;
|
|
90
|
+
getOrderByExpression(column: string, direction: QueryOrder, collation?: string): string[];
|
|
91
|
+
getDefaultClientUrl(): string;
|
|
75
92
|
}
|
package/MsSqlPlatform.js
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
AbstractSqlPlatform,
|
|
3
|
+
ALIAS_REPLACEMENT,
|
|
4
|
+
DoubleType,
|
|
5
|
+
FloatType,
|
|
6
|
+
MsSqlNativeQueryBuilder,
|
|
7
|
+
QueryOrder,
|
|
8
|
+
raw,
|
|
9
|
+
RawQueryFragment,
|
|
10
|
+
Type,
|
|
11
|
+
} from '@mikro-orm/sql';
|
|
2
12
|
// @ts-expect-error no types available
|
|
3
13
|
import SqlString from 'tsqlstring';
|
|
4
14
|
import { MsSqlSchemaHelper } from './MsSqlSchemaHelper.js';
|
|
@@ -8,227 +18,232 @@ import { UnicodeCharacterType } from './UnicodeCharacterType.js';
|
|
|
8
18
|
import { UnicodeString, UnicodeStringType } from './UnicodeStringType.js';
|
|
9
19
|
/** Platform implementation for Microsoft SQL Server. */
|
|
10
20
|
export class MsSqlPlatform extends AbstractSqlPlatform {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
21
|
+
schemaHelper = new MsSqlSchemaHelper(this);
|
|
22
|
+
exceptionConverter = new MsSqlExceptionConverter();
|
|
23
|
+
#jsonTypeCasts = {
|
|
24
|
+
string: 'nvarchar(max)',
|
|
25
|
+
number: 'float',
|
|
26
|
+
bigint: 'bigint',
|
|
27
|
+
boolean: 'bit',
|
|
28
|
+
};
|
|
29
|
+
/** @inheritDoc */
|
|
30
|
+
lookupExtensions(orm) {
|
|
31
|
+
MsSqlSchemaGenerator.register(orm);
|
|
32
|
+
}
|
|
33
|
+
/** @inheritDoc */
|
|
34
|
+
init(orm) {
|
|
35
|
+
super.init(orm);
|
|
36
|
+
// do not double escape backslash inside strings
|
|
37
|
+
SqlString.CHARS_GLOBAL_REGEXP = /'/g;
|
|
38
|
+
}
|
|
39
|
+
getRollbackToSavepointSQL(savepointName) {
|
|
40
|
+
return `rollback transaction ${this.quoteIdentifier(savepointName)}`;
|
|
41
|
+
}
|
|
42
|
+
getSavepointSQL(savepointName) {
|
|
43
|
+
return `save transaction ${this.quoteIdentifier(savepointName)}`;
|
|
44
|
+
}
|
|
45
|
+
/** @internal */
|
|
46
|
+
createNativeQueryBuilder() {
|
|
47
|
+
return new MsSqlNativeQueryBuilder(this);
|
|
48
|
+
}
|
|
49
|
+
usesOutputStatement() {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
convertDateToJSValue(value) {
|
|
53
|
+
/* v8 ignore next */
|
|
54
|
+
if (typeof value === 'string') {
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
return SqlString.dateToString(value.toISOString(), this.timezone ?? 'local').substring(1, 11);
|
|
58
|
+
}
|
|
59
|
+
convertsJsonAutomatically() {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
indexForeignKeys() {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
supportsSchemas() {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
getCurrentTimestampSQL(length) {
|
|
69
|
+
return `current_timestamp`;
|
|
70
|
+
}
|
|
71
|
+
getDateTimeTypeDeclarationSQL(column) {
|
|
72
|
+
/* v8 ignore next */
|
|
73
|
+
return 'datetime2' + (column.length != null ? `(${column.length})` : '');
|
|
74
|
+
}
|
|
75
|
+
getDefaultDateTimeLength() {
|
|
76
|
+
return 7;
|
|
77
|
+
}
|
|
78
|
+
getFloatDeclarationSQL() {
|
|
79
|
+
return 'float(24)';
|
|
80
|
+
}
|
|
81
|
+
getDoubleDeclarationSQL() {
|
|
82
|
+
return 'float(53)';
|
|
83
|
+
}
|
|
84
|
+
getBooleanTypeDeclarationSQL() {
|
|
85
|
+
return 'bit';
|
|
86
|
+
}
|
|
87
|
+
getRegExpOperator() {
|
|
88
|
+
throw new Error('Not supported');
|
|
89
|
+
}
|
|
90
|
+
getBlobDeclarationSQL() {
|
|
91
|
+
return 'varbinary(max)';
|
|
92
|
+
}
|
|
93
|
+
getJsonDeclarationSQL() {
|
|
94
|
+
return 'nvarchar(max)';
|
|
95
|
+
}
|
|
96
|
+
getVarcharTypeDeclarationSQL(column) {
|
|
97
|
+
if (column.length === -1) {
|
|
98
|
+
return 'varchar(max)';
|
|
99
|
+
}
|
|
100
|
+
return super.getVarcharTypeDeclarationSQL(column);
|
|
101
|
+
}
|
|
102
|
+
getEnumTypeDeclarationSQL(column) {
|
|
103
|
+
if (column.items?.every(item => typeof item === 'string')) {
|
|
104
|
+
return Type.getType(UnicodeStringType).getColumnType({ length: 100, ...column }, this);
|
|
105
|
+
}
|
|
106
|
+
/* v8 ignore next */
|
|
107
|
+
return this.getSmallIntTypeDeclarationSQL(column);
|
|
108
|
+
}
|
|
109
|
+
normalizeColumnType(type, options) {
|
|
110
|
+
const simpleType = this.extractSimpleType(type);
|
|
111
|
+
if (['decimal', 'numeric'].includes(simpleType)) {
|
|
112
|
+
return this.getDecimalTypeDeclarationSQL(options);
|
|
113
|
+
}
|
|
114
|
+
if (['real'].includes(simpleType)) {
|
|
115
|
+
return this.getFloatDeclarationSQL();
|
|
116
|
+
}
|
|
117
|
+
return super.normalizeColumnType(type, options);
|
|
118
|
+
}
|
|
119
|
+
getDefaultMappedType(type) {
|
|
120
|
+
if (type.startsWith('float')) {
|
|
121
|
+
const len = /float\((\d+)\)/.exec(type)?.[1] ?? 24;
|
|
122
|
+
return +len > 24 ? Type.getType(DoubleType) : Type.getType(FloatType);
|
|
123
|
+
}
|
|
124
|
+
const normalizedType = this.extractSimpleType(type);
|
|
125
|
+
if (normalizedType !== 'uuid' && ['string', 'nvarchar'].includes(normalizedType)) {
|
|
126
|
+
return Type.getType(UnicodeStringType);
|
|
127
|
+
}
|
|
128
|
+
if (['character', 'nchar'].includes(normalizedType)) {
|
|
129
|
+
return Type.getType(UnicodeCharacterType);
|
|
130
|
+
}
|
|
131
|
+
const map = {
|
|
132
|
+
int: 'integer',
|
|
133
|
+
bit: 'boolean',
|
|
134
|
+
real: 'float',
|
|
135
|
+
uniqueidentifier: 'uuid',
|
|
136
|
+
varbinary: 'blob',
|
|
137
|
+
datetime2: 'datetime',
|
|
138
|
+
smalldatetime: 'datetime',
|
|
18
139
|
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
smalldatetime: 'datetime',
|
|
129
|
-
};
|
|
130
|
-
return super.getDefaultMappedType(map[normalizedType] ?? type);
|
|
131
|
-
}
|
|
132
|
-
getDefaultSchemaName() {
|
|
133
|
-
return 'dbo';
|
|
134
|
-
}
|
|
135
|
-
getUuidTypeDeclarationSQL(column) {
|
|
136
|
-
return 'uniqueidentifier';
|
|
137
|
-
}
|
|
138
|
-
validateMetadata(meta) {
|
|
139
|
-
for (const prop of meta.props) {
|
|
140
|
-
if ((prop.runtimeType === 'string' || ['string', 'nvarchar'].includes(prop.type)) &&
|
|
141
|
-
!['uuid'].includes(prop.type) &&
|
|
142
|
-
!prop.columnTypes[0].startsWith('varchar')) {
|
|
143
|
-
prop.customType ??= new UnicodeStringType();
|
|
144
|
-
prop.customType.prop = prop;
|
|
145
|
-
prop.customType.platform = this;
|
|
146
|
-
prop.customType.meta = meta;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
getSearchJsonPropertyKey(path, type, aliased, value) {
|
|
151
|
-
const [a, ...b] = path;
|
|
152
|
-
/* v8 ignore next */
|
|
153
|
-
const root = this.quoteIdentifier(aliased ? `${ALIAS_REPLACEMENT}.${a}` : a);
|
|
154
|
-
const types = {
|
|
155
|
-
boolean: 'bit',
|
|
156
|
-
};
|
|
157
|
-
const cast = (key) => raw(type in types ? `cast(${key} as ${types[type]})` : key);
|
|
158
|
-
/* v8 ignore next */
|
|
159
|
-
if (path.length === 0) {
|
|
160
|
-
return cast(`json_value(${root}, '$.${b.map(this.quoteJsonKey).join('.')}')`);
|
|
161
|
-
}
|
|
162
|
-
return cast(`json_value(${root}, '$.${b.map(this.quoteJsonKey).join('.')}')`);
|
|
163
|
-
}
|
|
164
|
-
getJsonArrayFromSQL(column, alias, properties) {
|
|
165
|
-
const columns = properties
|
|
166
|
-
.map(p => `${this.quoteIdentifier(p.name)} ${this.#jsonTypeCasts[p.type] ?? 'nvarchar(max)'} '$.${this.quoteJsonKey(p.name)}'`)
|
|
167
|
-
.join(', ');
|
|
168
|
-
return `openjson(${column}) with (${columns}) as ${this.quoteIdentifier(alias)}`;
|
|
169
|
-
}
|
|
170
|
-
normalizePrimaryKey(data) {
|
|
171
|
-
/* v8 ignore next */
|
|
172
|
-
if (data instanceof UnicodeString) {
|
|
173
|
-
return data.value;
|
|
174
|
-
}
|
|
175
|
-
return data;
|
|
176
|
-
}
|
|
177
|
-
usesEnumCheckConstraints() {
|
|
178
|
-
return true;
|
|
179
|
-
}
|
|
180
|
-
supportsMultipleCascadePaths() {
|
|
181
|
-
return false;
|
|
182
|
-
}
|
|
183
|
-
supportsMultipleStatements() {
|
|
184
|
-
return true;
|
|
185
|
-
}
|
|
186
|
-
quoteIdentifier(id) {
|
|
187
|
-
if (RawQueryFragment.isKnownFragment(id)) {
|
|
188
|
-
return super.quoteIdentifier(id);
|
|
189
|
-
}
|
|
190
|
-
return `[${id.toString().replace('.', `].[`)}]`;
|
|
191
|
-
}
|
|
192
|
-
escape(value) {
|
|
193
|
-
if (value instanceof UnicodeString) {
|
|
194
|
-
return `N${SqlString.escape(value.value)}`;
|
|
195
|
-
}
|
|
196
|
-
if (value instanceof Buffer) {
|
|
197
|
-
return `0x${value.toString('hex')}`;
|
|
198
|
-
}
|
|
199
|
-
if (value instanceof Date) {
|
|
200
|
-
return SqlString.dateToString(value.toISOString(), this.timezone ?? 'local');
|
|
201
|
-
}
|
|
202
|
-
return SqlString.escape(value);
|
|
203
|
-
}
|
|
204
|
-
/* v8 ignore next: kept for type inference only */
|
|
205
|
-
getSchemaGenerator(driver, em) {
|
|
206
|
-
return new MsSqlSchemaGenerator(em ?? driver);
|
|
207
|
-
}
|
|
208
|
-
allowsComparingTuples() {
|
|
209
|
-
return false;
|
|
210
|
-
}
|
|
211
|
-
/** @internal MSSQL collation names are not quoted. */
|
|
212
|
-
quoteCollation(collation) {
|
|
213
|
-
this.validateCollationName(collation);
|
|
214
|
-
return collation;
|
|
215
|
-
}
|
|
216
|
-
getOrderByExpression(column, direction, collation) {
|
|
217
|
-
const col = collation ? `${column} collate ${this.quoteCollation(collation)}` : column;
|
|
218
|
-
switch (direction.toUpperCase()) {
|
|
219
|
-
case QueryOrder.ASC_NULLS_FIRST:
|
|
220
|
-
return [`case when ${column} is null then 0 else 1 end, ${col} asc`];
|
|
221
|
-
case QueryOrder.ASC_NULLS_LAST:
|
|
222
|
-
return [`case when ${column} is null then 1 else 0 end, ${col} asc`];
|
|
223
|
-
case QueryOrder.DESC_NULLS_FIRST:
|
|
224
|
-
return [`case when ${column} is null then 0 else 1 end, ${col} desc`];
|
|
225
|
-
case QueryOrder.DESC_NULLS_LAST:
|
|
226
|
-
return [`case when ${column} is null then 1 else 0 end, ${col} desc`];
|
|
227
|
-
default:
|
|
228
|
-
return [`${col} ${direction.toLowerCase()}`];
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
getDefaultClientUrl() {
|
|
232
|
-
return 'mssql://sa@localhost:1433';
|
|
233
|
-
}
|
|
140
|
+
return super.getDefaultMappedType(map[normalizedType] ?? type);
|
|
141
|
+
}
|
|
142
|
+
getDefaultSchemaName() {
|
|
143
|
+
return 'dbo';
|
|
144
|
+
}
|
|
145
|
+
getUuidTypeDeclarationSQL(column) {
|
|
146
|
+
return 'uniqueidentifier';
|
|
147
|
+
}
|
|
148
|
+
validateMetadata(meta) {
|
|
149
|
+
for (const prop of meta.props) {
|
|
150
|
+
if (
|
|
151
|
+
(prop.runtimeType === 'string' || ['string', 'nvarchar'].includes(prop.type)) &&
|
|
152
|
+
!['uuid'].includes(prop.type) &&
|
|
153
|
+
!prop.columnTypes[0].startsWith('varchar')
|
|
154
|
+
) {
|
|
155
|
+
prop.customType ??= new UnicodeStringType();
|
|
156
|
+
prop.customType.prop = prop;
|
|
157
|
+
prop.customType.platform = this;
|
|
158
|
+
prop.customType.meta = meta;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
getSearchJsonPropertyKey(path, type, aliased, value) {
|
|
163
|
+
const [a, ...b] = path;
|
|
164
|
+
/* v8 ignore next */
|
|
165
|
+
const root = this.quoteIdentifier(aliased ? `${ALIAS_REPLACEMENT}.${a}` : a);
|
|
166
|
+
const types = {
|
|
167
|
+
boolean: 'bit',
|
|
168
|
+
};
|
|
169
|
+
const cast = key => raw(type in types ? `cast(${key} as ${types[type]})` : key);
|
|
170
|
+
/* v8 ignore next */
|
|
171
|
+
if (path.length === 0) {
|
|
172
|
+
return cast(`json_value(${root}, '$.${b.map(this.quoteJsonKey).join('.')}')`);
|
|
173
|
+
}
|
|
174
|
+
return cast(`json_value(${root}, '$.${b.map(this.quoteJsonKey).join('.')}')`);
|
|
175
|
+
}
|
|
176
|
+
getJsonArrayFromSQL(column, alias, properties) {
|
|
177
|
+
const columns = properties
|
|
178
|
+
.map(
|
|
179
|
+
p =>
|
|
180
|
+
`${this.quoteIdentifier(p.name)} ${this.#jsonTypeCasts[p.type] ?? 'nvarchar(max)'} '$.${this.quoteJsonKey(p.name)}'`,
|
|
181
|
+
)
|
|
182
|
+
.join(', ');
|
|
183
|
+
return `openjson(${column}) with (${columns}) as ${this.quoteIdentifier(alias)}`;
|
|
184
|
+
}
|
|
185
|
+
normalizePrimaryKey(data) {
|
|
186
|
+
/* v8 ignore next */
|
|
187
|
+
if (data instanceof UnicodeString) {
|
|
188
|
+
return data.value;
|
|
189
|
+
}
|
|
190
|
+
return data;
|
|
191
|
+
}
|
|
192
|
+
usesEnumCheckConstraints() {
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
supportsMultipleCascadePaths() {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
supportsMultipleStatements() {
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
quoteIdentifier(id) {
|
|
202
|
+
if (RawQueryFragment.isKnownFragment(id)) {
|
|
203
|
+
return super.quoteIdentifier(id);
|
|
204
|
+
}
|
|
205
|
+
return `[${id.toString().replace('.', `].[`)}]`;
|
|
206
|
+
}
|
|
207
|
+
escape(value) {
|
|
208
|
+
if (value instanceof UnicodeString) {
|
|
209
|
+
return `N${SqlString.escape(value.value)}`;
|
|
210
|
+
}
|
|
211
|
+
if (value instanceof Buffer) {
|
|
212
|
+
return `0x${value.toString('hex')}`;
|
|
213
|
+
}
|
|
214
|
+
if (value instanceof Date) {
|
|
215
|
+
return SqlString.dateToString(value.toISOString(), this.timezone ?? 'local');
|
|
216
|
+
}
|
|
217
|
+
return SqlString.escape(value);
|
|
218
|
+
}
|
|
219
|
+
/* v8 ignore next: kept for type inference only */
|
|
220
|
+
getSchemaGenerator(driver, em) {
|
|
221
|
+
return new MsSqlSchemaGenerator(em ?? driver);
|
|
222
|
+
}
|
|
223
|
+
allowsComparingTuples() {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
/** @internal MSSQL collation names are not quoted. */
|
|
227
|
+
quoteCollation(collation) {
|
|
228
|
+
this.validateCollationName(collation);
|
|
229
|
+
return collation;
|
|
230
|
+
}
|
|
231
|
+
getOrderByExpression(column, direction, collation) {
|
|
232
|
+
const col = collation ? `${column} collate ${this.quoteCollation(collation)}` : column;
|
|
233
|
+
switch (direction.toUpperCase()) {
|
|
234
|
+
case QueryOrder.ASC_NULLS_FIRST:
|
|
235
|
+
return [`case when ${column} is null then 0 else 1 end, ${col} asc`];
|
|
236
|
+
case QueryOrder.ASC_NULLS_LAST:
|
|
237
|
+
return [`case when ${column} is null then 1 else 0 end, ${col} asc`];
|
|
238
|
+
case QueryOrder.DESC_NULLS_FIRST:
|
|
239
|
+
return [`case when ${column} is null then 0 else 1 end, ${col} desc`];
|
|
240
|
+
case QueryOrder.DESC_NULLS_LAST:
|
|
241
|
+
return [`case when ${column} is null then 1 else 0 end, ${col} desc`];
|
|
242
|
+
default:
|
|
243
|
+
return [`${col} ${direction.toLowerCase()}`];
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
getDefaultClientUrl() {
|
|
247
|
+
return 'mssql://sa@localhost:1433';
|
|
248
|
+
}
|
|
234
249
|
}
|
package/MsSqlQueryBuilder.d.ts
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import { type AnyEntity, type RequiredEntityData } from '@mikro-orm/core';
|
|
2
2
|
import { type InsertQueryBuilder, QueryBuilder } from '@mikro-orm/sql';
|
|
3
3
|
/** Query builder with MSSQL-specific behavior such as identity insert handling. */
|
|
4
|
-
export declare class MsSqlQueryBuilder<
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
export declare class MsSqlQueryBuilder<
|
|
5
|
+
Entity extends object = AnyEntity,
|
|
6
|
+
RootAlias extends string = never,
|
|
7
|
+
Hint extends string = never,
|
|
8
|
+
Context extends object = never,
|
|
9
|
+
> extends QueryBuilder<Entity, RootAlias, Hint, Context> {
|
|
10
|
+
insert(
|
|
11
|
+
data: RequiredEntityData<Entity> | RequiredEntityData<Entity>[],
|
|
12
|
+
): InsertQueryBuilder<Entity, RootAlias, Context>;
|
|
13
|
+
private checkIdentityInsert;
|
|
7
14
|
}
|