@memberjunction/sql-dialect 6.2.0-edge.5 → 6.2.0-edge.7
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/README.md +280 -91
- package/dist/dialectFactory.d.ts +37 -5
- package/dist/dialectFactory.d.ts.map +1 -1
- package/dist/dialectFactory.js +106 -14
- package/dist/dialectFactory.js.map +1 -1
- package/dist/dialects/ansi.d.ts +55 -0
- package/dist/dialects/ansi.d.ts.map +1 -0
- package/dist/dialects/ansi.js +136 -0
- package/dist/dialects/ansi.js.map +1 -0
- package/dist/dialects/baseSchemaDDL.d.ts +78 -0
- package/dist/dialects/baseSchemaDDL.d.ts.map +1 -0
- package/dist/dialects/baseSchemaDDL.js +63 -0
- package/dist/dialects/baseSchemaDDL.js.map +1 -0
- package/dist/dialects/databricksQueryDialect.d.ts +41 -0
- package/dist/dialects/databricksQueryDialect.d.ts.map +1 -0
- package/dist/dialects/databricksQueryDialect.js +103 -0
- package/dist/dialects/databricksQueryDialect.js.map +1 -0
- package/dist/dialects/mysqlQueryDialect.d.ts +46 -0
- package/dist/dialects/mysqlQueryDialect.d.ts.map +1 -0
- package/dist/dialects/mysqlQueryDialect.js +117 -0
- package/dist/dialects/mysqlQueryDialect.js.map +1 -0
- package/dist/dialects/oracleQueryDialect.d.ts +42 -0
- package/dist/dialects/oracleQueryDialect.d.ts.map +1 -0
- package/dist/dialects/oracleQueryDialect.js +92 -0
- package/dist/dialects/oracleQueryDialect.js.map +1 -0
- package/dist/dialects/postgresqlSchemaDDL.d.ts +112 -0
- package/dist/dialects/postgresqlSchemaDDL.d.ts.map +1 -0
- package/dist/dialects/postgresqlSchemaDDL.js +590 -0
- package/dist/dialects/postgresqlSchemaDDL.js.map +1 -0
- package/dist/dialects/snowflakeQueryDialect.d.ts +41 -0
- package/dist/dialects/snowflakeQueryDialect.d.ts.map +1 -0
- package/dist/dialects/snowflakeQueryDialect.js +94 -0
- package/dist/dialects/snowflakeQueryDialect.js.map +1 -0
- package/dist/dialects/sqlServerSchemaDDL.d.ts +83 -0
- package/dist/dialects/sqlServerSchemaDDL.d.ts.map +1 -0
- package/dist/dialects/sqlServerSchemaDDL.js +507 -0
- package/dist/dialects/sqlServerSchemaDDL.js.map +1 -0
- package/dist/facets/dialectBinding.d.ts +26 -0
- package/dist/facets/dialectBinding.d.ts.map +1 -0
- package/dist/facets/dialectBinding.js +2 -0
- package/dist/facets/dialectBinding.js.map +1 -0
- package/dist/facets/dialectGrammar.d.ts +82 -0
- package/dist/facets/dialectGrammar.d.ts.map +1 -0
- package/dist/facets/dialectGrammar.js +2 -0
- package/dist/facets/dialectGrammar.js.map +1 -0
- package/dist/facets/dialectKey.d.ts +9 -0
- package/dist/facets/dialectKey.d.ts.map +1 -0
- package/dist/facets/dialectKey.js +2 -0
- package/dist/facets/dialectKey.js.map +1 -0
- package/dist/facets/dialectLexicalRules.d.ts +102 -0
- package/dist/facets/dialectLexicalRules.d.ts.map +1 -0
- package/dist/facets/dialectLexicalRules.js +2 -0
- package/dist/facets/dialectLexicalRules.js.map +1 -0
- package/dist/facets/dialectSchemaDDL.d.ts +213 -0
- package/dist/facets/dialectSchemaDDL.d.ts.map +1 -0
- package/dist/facets/dialectSchemaDDL.js +2 -0
- package/dist/facets/dialectSchemaDDL.js.map +1 -0
- package/dist/facets/dialectTypes.d.ts +31 -0
- package/dist/facets/dialectTypes.d.ts.map +1 -0
- package/dist/facets/dialectTypes.js +2 -0
- package/dist/facets/dialectTypes.js.map +1 -0
- package/dist/facets/dialectWriter.d.ts +59 -0
- package/dist/facets/dialectWriter.d.ts.map +1 -0
- package/dist/facets/dialectWriter.js +2 -0
- package/dist/facets/dialectWriter.js.map +1 -0
- package/dist/identifierReferences.d.ts +8 -1
- package/dist/identifierReferences.d.ts.map +1 -1
- package/dist/identifierReferences.js +8 -1
- package/dist/identifierReferences.js.map +1 -1
- package/dist/index.d.ts +22 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -5
- package/dist/index.js.map +1 -1
- package/dist/postgresqlAutoQuote.d.ts +10 -0
- package/dist/postgresqlAutoQuote.d.ts.map +1 -1
- package/dist/postgresqlAutoQuote.js +12 -0
- package/dist/postgresqlAutoQuote.js.map +1 -1
- package/dist/postgresqlDialect.d.ts +57 -149
- package/dist/postgresqlDialect.d.ts.map +1 -1
- package/dist/postgresqlDialect.js +110 -676
- package/dist/postgresqlDialect.js.map +1 -1
- package/dist/postgresqlRestarLayeredOuter.d.ts.map +1 -1
- package/dist/postgresqlRestarLayeredOuter.js +45 -15
- package/dist/postgresqlRestarLayeredOuter.js.map +1 -1
- package/dist/queryDialect.d.ts +42 -0
- package/dist/queryDialect.d.ts.map +1 -0
- package/dist/queryDialect.js +25 -0
- package/dist/queryDialect.js.map +1 -0
- package/dist/sqlDialect.d.ts +148 -212
- package/dist/sqlDialect.d.ts.map +1 -1
- package/dist/sqlDialect.js +751 -29
- package/dist/sqlDialect.js.map +1 -1
- package/dist/sqlServerDialect.d.ts +54 -117
- package/dist/sqlServerDialect.d.ts.map +1 -1
- package/dist/sqlServerDialect.js +124 -587
- package/dist/sqlServerDialect.js.map +1 -1
- package/package.json +4 -2
package/dist/sqlServerDialect.js
CHANGED
|
@@ -1,105 +1,24 @@
|
|
|
1
|
-
import { SQLDialect
|
|
1
|
+
import { SQLDialect } from './sqlDialect.js';
|
|
2
|
+
import { ANSI_GRAMMAR, ANSI_LEXICAL, ANSI_WRITE_STATEMENT_KEYWORDS, AnsiWriter } from './dialects/ansi.js';
|
|
3
|
+
import { SQLServerSchemaDDL } from './dialects/sqlServerSchemaDDL.js';
|
|
2
4
|
/**
|
|
3
|
-
* SQL Server
|
|
4
|
-
*
|
|
5
|
-
*
|
|
5
|
+
* SQL Server lexical rules: `[…]` and `"…"` identifiers, `'…'` and `N'…'` strings with doubled
|
|
6
|
+
* quotes, `--` line comments and nested block comments, `@name` parameters.
|
|
7
|
+
*
|
|
8
|
+
* - Identifiers: https://learn.microsoft.com/en-us/sql/relational-databases/databases/database-identifiers
|
|
9
|
+
* - Nested block comments: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/slash-star-comment-transact-sql
|
|
10
|
+
* - `--` comments: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/comment-transact-sql
|
|
6
11
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
case 'NCHAR':
|
|
18
|
-
case 'CHAR':
|
|
19
|
-
return { typeName: normalized, supportsLength: true, supportsPrecisionScale: false, defaultLength: sourceLength ?? 1 };
|
|
20
|
-
case 'INT':
|
|
21
|
-
case 'INTEGER':
|
|
22
|
-
return { typeName: 'INT', supportsLength: false, supportsPrecisionScale: false };
|
|
23
|
-
case 'BIGINT':
|
|
24
|
-
return { typeName: 'BIGINT', supportsLength: false, supportsPrecisionScale: false };
|
|
25
|
-
case 'SMALLINT':
|
|
26
|
-
return { typeName: 'SMALLINT', supportsLength: false, supportsPrecisionScale: false };
|
|
27
|
-
case 'TINYINT':
|
|
28
|
-
return { typeName: 'TINYINT', supportsLength: false, supportsPrecisionScale: false };
|
|
29
|
-
case 'BIT':
|
|
30
|
-
return { typeName: 'BIT', supportsLength: false, supportsPrecisionScale: false };
|
|
31
|
-
case 'DECIMAL':
|
|
32
|
-
case 'NUMERIC':
|
|
33
|
-
return { typeName: normalized, supportsLength: false, supportsPrecisionScale: true };
|
|
34
|
-
case 'FLOAT':
|
|
35
|
-
return { typeName: 'FLOAT', supportsLength: false, supportsPrecisionScale: false };
|
|
36
|
-
case 'REAL':
|
|
37
|
-
return { typeName: 'REAL', supportsLength: false, supportsPrecisionScale: false };
|
|
38
|
-
case 'MONEY':
|
|
39
|
-
return { typeName: 'MONEY', supportsLength: false, supportsPrecisionScale: false };
|
|
40
|
-
case 'SMALLMONEY':
|
|
41
|
-
return { typeName: 'SMALLMONEY', supportsLength: false, supportsPrecisionScale: false };
|
|
42
|
-
case 'DATE':
|
|
43
|
-
return { typeName: 'DATE', supportsLength: false, supportsPrecisionScale: false };
|
|
44
|
-
case 'DATETIME':
|
|
45
|
-
return { typeName: 'DATETIME', supportsLength: false, supportsPrecisionScale: false };
|
|
46
|
-
case 'DATETIME2':
|
|
47
|
-
return { typeName: 'DATETIME2', supportsLength: false, supportsPrecisionScale: false };
|
|
48
|
-
case 'DATETIMEOFFSET':
|
|
49
|
-
return { typeName: 'DATETIMEOFFSET', supportsLength: false, supportsPrecisionScale: false };
|
|
50
|
-
case 'SMALLDATETIME':
|
|
51
|
-
return { typeName: 'SMALLDATETIME', supportsLength: false, supportsPrecisionScale: false };
|
|
52
|
-
case 'TIME':
|
|
53
|
-
return { typeName: 'TIME', supportsLength: false, supportsPrecisionScale: false };
|
|
54
|
-
case 'TEXT':
|
|
55
|
-
case 'NTEXT':
|
|
56
|
-
return { typeName: normalized, supportsLength: false, supportsPrecisionScale: false };
|
|
57
|
-
case 'IMAGE':
|
|
58
|
-
return { typeName: 'IMAGE', supportsLength: false, supportsPrecisionScale: false };
|
|
59
|
-
case 'VARBINARY':
|
|
60
|
-
return { typeName: 'VARBINARY', supportsLength: true, supportsPrecisionScale: false, defaultLength: sourceLength };
|
|
61
|
-
case 'BINARY':
|
|
62
|
-
return { typeName: 'BINARY', supportsLength: true, supportsPrecisionScale: false, defaultLength: sourceLength ?? 1 };
|
|
63
|
-
case 'XML':
|
|
64
|
-
return { typeName: 'XML', supportsLength: false, supportsPrecisionScale: false };
|
|
65
|
-
default:
|
|
66
|
-
return { typeName: normalized, supportsLength: false, supportsPrecisionScale: false };
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
MapTypeToString(sourceType, sourceLength, sourcePrecision, sourceScale) {
|
|
70
|
-
const mapped = this.MapType(sourceType, sourceLength, sourcePrecision, sourceScale);
|
|
71
|
-
return formatTypeString(mapped, sourceLength, sourcePrecision, sourceScale);
|
|
72
|
-
}
|
|
73
|
-
buildNVarcharType(length) {
|
|
74
|
-
if (length === -1 || length === undefined) {
|
|
75
|
-
return { typeName: 'NVARCHAR(MAX)', supportsLength: false, supportsPrecisionScale: false };
|
|
76
|
-
}
|
|
77
|
-
return { typeName: 'NVARCHAR', supportsLength: true, supportsPrecisionScale: false, defaultLength: length };
|
|
78
|
-
}
|
|
79
|
-
buildVarcharType(length) {
|
|
80
|
-
if (length === -1 || length === undefined) {
|
|
81
|
-
return { typeName: 'VARCHAR(MAX)', supportsLength: false, supportsPrecisionScale: false };
|
|
82
|
-
}
|
|
83
|
-
return { typeName: 'VARCHAR', supportsLength: true, supportsPrecisionScale: false, defaultLength: length };
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Helper to format a MappedType into a full SQL type string.
|
|
88
|
-
*/
|
|
89
|
-
function formatTypeString(mapped, length, precision, scale) {
|
|
90
|
-
if (mapped.supportsPrecisionScale && precision != null) {
|
|
91
|
-
return scale != null
|
|
92
|
-
? `${mapped.typeName}(${precision},${scale})`
|
|
93
|
-
: `${mapped.typeName}(${precision})`;
|
|
94
|
-
}
|
|
95
|
-
if (mapped.supportsLength) {
|
|
96
|
-
const len = length ?? mapped.defaultLength;
|
|
97
|
-
if (len != null) {
|
|
98
|
-
return len === -1 ? `${mapped.typeName}(MAX)` : `${mapped.typeName}(${len})`;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return mapped.typeName;
|
|
102
|
-
}
|
|
12
|
+
export const SQL_SERVER_LEXICAL = Object.freeze({
|
|
13
|
+
...ANSI_LEXICAL,
|
|
14
|
+
IdentifierQuotes: Object.freeze([
|
|
15
|
+
Object.freeze({ Open: '"', Close: '"', DoubledCloseEscape: true }),
|
|
16
|
+
Object.freeze({ Open: '[', Close: ']', DoubledCloseEscape: true }),
|
|
17
|
+
]),
|
|
18
|
+
BlockComments: Object.freeze({ Nested: true, ExecutableOpeners: Object.freeze([]) }),
|
|
19
|
+
ParameterPlaceholders: Object.freeze(['at-name']),
|
|
20
|
+
UnquotedIdentifierCase: 'none',
|
|
21
|
+
});
|
|
103
22
|
/**
|
|
104
23
|
* SQL Server functions caller-supplied SQL may not call: the rowset functions that reach other
|
|
105
24
|
* servers, databases or files, `OPENXML`, and the functions that read trace, audit and
|
|
@@ -109,18 +28,65 @@ const SQL_SERVER_CALLER_SQL_FORBIDDEN_FUNCTIONS = [
|
|
|
109
28
|
'openrowset', 'openquery', 'opendatasource', 'openxml',
|
|
110
29
|
'fn_get_audit_file', 'fn_xe_file_target_read_file', 'fn_trace_gettable'
|
|
111
30
|
];
|
|
31
|
+
/** Keywords that begin a T-SQL statement that writes data, changes the schema or runs code. */
|
|
32
|
+
const SQL_SERVER_WRITE_STATEMENT_KEYWORDS = Object.freeze([
|
|
33
|
+
...ANSI_WRITE_STATEMENT_KEYWORDS.filter(keyword => keyword !== 'CALL'),
|
|
34
|
+
'EXEC', 'EXECUTE', 'DENY', 'BULK', 'BACKUP', 'RESTORE', 'DBCC', 'RECONFIGURE', 'SHUTDOWN', 'KILL',
|
|
35
|
+
]);
|
|
112
36
|
/**
|
|
113
|
-
* SQL
|
|
114
|
-
*
|
|
37
|
+
* Reserved T-SQL keywords that only ever begin a statement. T-SQL needs no separator between
|
|
38
|
+
* statements, so one of these as a bare word after a statement's first word starts another
|
|
39
|
+
* statement in the same batch.
|
|
115
40
|
*/
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
41
|
+
const SQL_SERVER_STATEMENT_KEYWORDS = [
|
|
42
|
+
'SET', 'DECLARE', 'WAITFOR', 'USE', 'PRINT', 'RAISERROR', 'BEGIN', 'COMMIT', 'ROLLBACK', 'SAVE', 'GOTO', 'RETURN',
|
|
43
|
+
'IF', 'WHILE', 'BREAK', 'CONTINUE', 'OPEN', 'CLOSE', 'DEALLOCATE', 'CHECKPOINT', 'READTEXT', 'WRITETEXT', 'UPDATETEXT',
|
|
44
|
+
];
|
|
45
|
+
/**
|
|
46
|
+
* SQL Server grammar facts: `TOP` and `OFFSET … FETCH` row caps, no ORDER BY in a CTE or derived
|
|
47
|
+
* table without TOP/OFFSET/FOR XML, no nested WITH, plain `WITH` for recursion, the `OPTION (…)`
|
|
48
|
+
* hint clause, `FOR JSON` / `FOR XML`, `CROSS/OUTER APPLY`.
|
|
49
|
+
*/
|
|
50
|
+
export const SQL_SERVER_GRAMMAR = Object.freeze({
|
|
51
|
+
...ANSI_GRAMMAR,
|
|
52
|
+
ParserGrammar: 'TransactSQL',
|
|
53
|
+
DefaultSchema: 'dbo',
|
|
54
|
+
RowCapForms: Object.freeze(['top', 'offset-fetch']),
|
|
55
|
+
AllowsOrderByInCTE: false,
|
|
56
|
+
AllowsOrderByInDerivedTable: false,
|
|
57
|
+
SupportsNestedWith: false,
|
|
58
|
+
RecursiveKeyword: null,
|
|
59
|
+
QueryHintKeyword: 'OPTION',
|
|
60
|
+
DocumentTailClauses: Object.freeze(['FOR JSON', 'FOR XML']),
|
|
61
|
+
CorrelatedJoin: 'apply',
|
|
62
|
+
WriteStatementKeywords: SQL_SERVER_WRITE_STATEMENT_KEYWORDS,
|
|
63
|
+
CallerSQLForbiddenFunctions: SQL_SERVER_CALLER_SQL_FORBIDDEN_FUNCTIONS,
|
|
64
|
+
UnseparatedStatementKeywords: Object.freeze([...SQL_SERVER_WRITE_STATEMENT_KEYWORDS, ...SQL_SERVER_STATEMENT_KEYWORDS]),
|
|
65
|
+
DefaultPagingOrderBy: '(SELECT NULL)',
|
|
66
|
+
SelectListPagingOrderBy: '1',
|
|
67
|
+
PagingRequiresOrderBy: true,
|
|
68
|
+
ExtraColumnAliasForms: Object.freeze(['equals', 'string']),
|
|
69
|
+
});
|
|
70
|
+
/**
|
|
71
|
+
* SQL Server's column-type names as they appear in `sys.columns.name` / `EntityField.Type` for
|
|
72
|
+
* entities backed by a SQL Server schema. `char` and `nchar` right-pad stored values with spaces.
|
|
73
|
+
*/
|
|
74
|
+
export const SQL_SERVER_TYPES = Object.freeze({
|
|
75
|
+
BooleanTypeNames: Object.freeze(['bit']),
|
|
76
|
+
StringTypeNames: Object.freeze(['text', 'ntext', 'varchar', 'nvarchar', 'char', 'nchar']),
|
|
77
|
+
FixedWidthStringTypeNames: Object.freeze(['char', 'nchar']),
|
|
78
|
+
DateTypeNames: Object.freeze(['date', 'time', 'datetime', 'datetime2', 'datetimeoffset', 'smalldatetime']),
|
|
79
|
+
IntegerTypeNames: Object.freeze(['int', 'integer', 'bigint', 'smallint', 'tinyint', 'rowversion', 'timestamp']),
|
|
80
|
+
FloatTypeNames: Object.freeze(['decimal', 'numeric', 'float', 'real']),
|
|
81
|
+
UuidTypeNames: Object.freeze(['uniqueidentifier']),
|
|
82
|
+
BinaryTypeNames: Object.freeze(['binary', 'varbinary', 'image']),
|
|
83
|
+
JsonTypeNames: Object.freeze(['xml']),
|
|
84
|
+
CurrencyTypeNames: Object.freeze(['money', 'smallmoney']),
|
|
85
|
+
IntervalTypeNames: Object.freeze([]),
|
|
86
|
+
NetworkTypeNames: Object.freeze([]),
|
|
87
|
+
});
|
|
88
|
+
/** Writes T-SQL: bracket quoting, `TOP`, `BIT` booleans, `ISNULL`, `+` concatenation. */
|
|
89
|
+
export class SQLServerWriter extends AnsiWriter {
|
|
124
90
|
QuoteIdentifier(name) {
|
|
125
91
|
// Double embedded closing brackets so a name containing `]` cannot terminate the
|
|
126
92
|
// quoting early (mirrors the PostgreSQL dialect's doubling of embedded `"`).
|
|
@@ -137,30 +103,15 @@ export class SQLServerDialect extends SQLDialect {
|
|
|
137
103
|
QuoteColumnAlias(aliasName) {
|
|
138
104
|
return aliasName;
|
|
139
105
|
}
|
|
140
|
-
/** SQL Server is case-insensitive for identifiers; the schema name is stored as-given. */
|
|
141
|
-
CanonicalSchemaName(name) {
|
|
142
|
-
return name;
|
|
143
|
-
}
|
|
144
|
-
// ─── Pagination ──────────────────────────────────────────────────
|
|
145
106
|
LimitClause(limit, offset) {
|
|
146
107
|
if (offset != null) {
|
|
147
108
|
return { prefix: '', suffix: `OFFSET ${offset} ROWS FETCH NEXT ${limit} ROWS ONLY` };
|
|
148
109
|
}
|
|
149
110
|
return { prefix: `TOP ${limit}`, suffix: '' };
|
|
150
111
|
}
|
|
151
|
-
// ─── Literals & Expressions ──────────────────────────────────────
|
|
152
112
|
BooleanLiteral(value) {
|
|
153
113
|
return value ? '1' : '0';
|
|
154
114
|
}
|
|
155
|
-
BooleanParameterType() {
|
|
156
|
-
return 'bit';
|
|
157
|
-
}
|
|
158
|
-
ParameterRef(name) {
|
|
159
|
-
return `@${name}`;
|
|
160
|
-
}
|
|
161
|
-
ParameterDefault(value) {
|
|
162
|
-
return ` = ${value}`;
|
|
163
|
-
}
|
|
164
115
|
/**
|
|
165
116
|
* SQL Server has both `ISNULL` (T-SQL native, two-arg only) and `COALESCE`
|
|
166
117
|
* (ANSI, n-ary). For two-argument null-coalescing we emit the native
|
|
@@ -183,500 +134,86 @@ export class SQLServerDialect extends SQLDialect {
|
|
|
183
134
|
CurrentTimestampUTC() {
|
|
184
135
|
return 'GETUTCDATE()';
|
|
185
136
|
}
|
|
186
|
-
AffectedRowCountSQL(dmlStatement, alias) {
|
|
187
|
-
return `${dmlStatement};\nSELECT @@ROWCOUNT AS ${this.QuoteColumnAlias(alias)}`;
|
|
188
|
-
}
|
|
189
|
-
// ─── Type-Name Sets ──────────────────────────────────────────────
|
|
190
|
-
// SQL Server's column-type names as they appear in `sys.columns.name`
|
|
191
|
-
// / `EntityField.Type` for entities backed by a SQL Server schema.
|
|
192
|
-
static { this._booleanTypeNames = ['bit']; }
|
|
193
|
-
static { this._stringTypeNames = ['text', 'ntext', 'varchar', 'nvarchar', 'char', 'nchar']; }
|
|
194
|
-
/** `char` and `nchar` right-pad stored values with spaces up to declared length. */
|
|
195
|
-
static { this._fixedWidthStringTypeNames = ['char', 'nchar']; }
|
|
196
|
-
static { this._dateTypeNames = ['date', 'time', 'datetime', 'datetime2', 'datetimeoffset', 'smalldatetime']; }
|
|
197
|
-
static { this._integerTypeNames = ['int', 'integer', 'bigint', 'smallint', 'tinyint', 'rowversion', 'timestamp']; }
|
|
198
|
-
static { this._floatTypeNames = ['decimal', 'numeric', 'float', 'real']; }
|
|
199
|
-
static { this._uuidTypeNames = ['uniqueidentifier']; }
|
|
200
|
-
static { this._binaryTypeNames = ['binary', 'varbinary', 'image']; }
|
|
201
|
-
static { this._jsonTypeNames = ['xml']; }
|
|
202
|
-
static { this._currencyTypeNames = ['money', 'smallmoney']; }
|
|
203
|
-
static { this._intervalTypeNames = []; }
|
|
204
|
-
static { this._networkTypeNames = []; }
|
|
205
|
-
get BooleanTypeNames() { return SQLServerDialect._booleanTypeNames; }
|
|
206
|
-
get StringTypeNames() { return SQLServerDialect._stringTypeNames; }
|
|
207
|
-
get FixedWidthStringTypeNames() { return SQLServerDialect._fixedWidthStringTypeNames; }
|
|
208
|
-
/** SQL Server index keys are limited to 900 bytes → 450 NVARCHAR (2-byte) chars. */
|
|
209
|
-
get MaxKeyStringLength() { return 450; }
|
|
210
|
-
/** SQL Server enforces a hard ~8060-byte in-row row size; only variable-length values go off-row. */
|
|
211
|
-
get MaxInRowSizeBytes() { return 8060; }
|
|
212
|
-
/** SQL Server's hard per-table column cap. */
|
|
213
|
-
get MaxColumnCount() { return 1024; }
|
|
214
|
-
/**
|
|
215
|
-
* Minimum in-row byte footprint of a column. Off-row-capable variable-length / LOB types
|
|
216
|
-
* (incl. `(N)VARCHAR(MAX)`) contribute only a 24-byte in-row pointer; fixed-length types
|
|
217
|
-
* contribute their full size. Unknown types are treated as off-row pointers (conservative).
|
|
218
|
-
*/
|
|
219
|
-
EstimateInRowBytes(rawSqlType) {
|
|
220
|
-
const t = (rawSqlType ?? '').toUpperCase().trim();
|
|
221
|
-
// Off-row-capable variable-length + LOB types → 24-byte in-row pointer floor.
|
|
222
|
-
if (/^(N?VARCHAR|VARBINARY)\s*\(/.test(t))
|
|
223
|
-
return 24; // covers (N)VARCHAR(MAX) too
|
|
224
|
-
if (/^(TEXT|NTEXT|IMAGE|XML|SQL_VARIANT)\b/.test(t))
|
|
225
|
-
return 24;
|
|
226
|
-
// Fixed-length character/binary types stay fully in-row.
|
|
227
|
-
const nchar = t.match(/^NCHAR\s*\(\s*(\d+)\s*\)/);
|
|
228
|
-
if (nchar)
|
|
229
|
-
return parseInt(nchar[1], 10) * 2;
|
|
230
|
-
const chr = t.match(/^(?:CHAR|BINARY)\s*\(\s*(\d+)\s*\)/);
|
|
231
|
-
if (chr)
|
|
232
|
-
return parseInt(chr[1], 10);
|
|
233
|
-
if (/^UNIQUEIDENTIFIER\b/.test(t))
|
|
234
|
-
return 16;
|
|
235
|
-
if (/^BIGINT\b/.test(t))
|
|
236
|
-
return 8;
|
|
237
|
-
if (/^SMALLINT\b/.test(t))
|
|
238
|
-
return 2;
|
|
239
|
-
if (/^TINYINT\b/.test(t))
|
|
240
|
-
return 1;
|
|
241
|
-
if (/^INT\b/.test(t))
|
|
242
|
-
return 4;
|
|
243
|
-
if (/^BIT\b/.test(t))
|
|
244
|
-
return 1;
|
|
245
|
-
if (/^(DECIMAL|NUMERIC|MONEY|SMALLMONEY)\b/.test(t))
|
|
246
|
-
return 17;
|
|
247
|
-
if (/^(FLOAT|REAL)\b/.test(t))
|
|
248
|
-
return 8;
|
|
249
|
-
if (/^DATETIMEOFFSET\b/.test(t))
|
|
250
|
-
return 10;
|
|
251
|
-
if (/^(DATETIME2|DATETIME|SMALLDATETIME)\b/.test(t))
|
|
252
|
-
return 8;
|
|
253
|
-
if (/^DATE\b/.test(t))
|
|
254
|
-
return 3;
|
|
255
|
-
if (/^TIME\b/.test(t))
|
|
256
|
-
return 5;
|
|
257
|
-
return 24; // unknown → treat as off-row variable-length pointer (conservative)
|
|
258
|
-
}
|
|
259
|
-
get DateTypeNames() { return SQLServerDialect._dateTypeNames; }
|
|
260
|
-
get IntegerTypeNames() { return SQLServerDialect._integerTypeNames; }
|
|
261
|
-
get FloatTypeNames() { return SQLServerDialect._floatTypeNames; }
|
|
262
|
-
get UuidTypeNames() { return SQLServerDialect._uuidTypeNames; }
|
|
263
|
-
get BinaryTypeNames() { return SQLServerDialect._binaryTypeNames; }
|
|
264
|
-
get JsonTypeNames() { return SQLServerDialect._jsonTypeNames; }
|
|
265
|
-
get CurrencyTypeNames() { return SQLServerDialect._currencyTypeNames; }
|
|
266
|
-
get IntervalTypeNames() { return SQLServerDialect._intervalTypeNames; }
|
|
267
|
-
get NetworkTypeNames() { return SQLServerDialect._networkTypeNames; }
|
|
268
|
-
NewUUID() {
|
|
269
|
-
return 'NEWID()';
|
|
270
|
-
}
|
|
271
137
|
CastToText(expr) {
|
|
272
138
|
return `CAST(${expr} AS NVARCHAR(MAX))`;
|
|
273
139
|
}
|
|
274
|
-
/**
|
|
275
|
-
* SQL Server-specific Flyway escape. Interleaves a `CAST(N'' AS NVARCHAR(MAX))`
|
|
276
|
-
* between the split halves so the running T-SQL concat chain inherits
|
|
277
|
-
* NVARCHAR(MAX) precedence. Without the cast, `N'a' + N'b'` produces
|
|
278
|
-
* NVARCHAR(a+b) capped at NVARCHAR(4000) and silently truncates anything
|
|
279
|
-
* past 4,000 characters.
|
|
280
|
-
*/
|
|
281
|
-
EscapeFlywayStringInterpolation(sql) {
|
|
282
|
-
return sql.replaceAll(/\$\{/g, "$$'+CAST(N'' AS NVARCHAR(MAX))+N'{");
|
|
283
|
-
}
|
|
284
|
-
CastToUUID(expr) {
|
|
285
|
-
return `CAST(${expr} AS UNIQUEIDENTIFIER)`;
|
|
286
|
-
}
|
|
287
|
-
// ─── INSERT/UPDATE Return Patterns ───────────────────────────────
|
|
288
|
-
ReturnInsertedClause(columns) {
|
|
289
|
-
if (columns && columns.length > 0) {
|
|
290
|
-
const cols = columns.map(c => `INSERTED.[${c}]`).join(', ');
|
|
291
|
-
return `OUTPUT ${cols}`;
|
|
292
|
-
}
|
|
293
|
-
return 'OUTPUT INSERTED.*';
|
|
294
|
-
}
|
|
295
|
-
AutoIncrementPKExpression() {
|
|
296
|
-
return 'IDENTITY(1,1)';
|
|
297
|
-
}
|
|
298
|
-
UUIDPKDefault() {
|
|
299
|
-
return 'NEWSEQUENTIALID()';
|
|
300
|
-
}
|
|
301
|
-
ScopeIdentityExpression() {
|
|
302
|
-
return 'SCOPE_IDENTITY()';
|
|
303
|
-
}
|
|
304
|
-
RowCountExpression() {
|
|
305
|
-
return '@@ROWCOUNT';
|
|
306
|
-
}
|
|
307
|
-
// ─── Batch & DDL Control ─────────────────────────────────────────
|
|
308
|
-
BatchSeparator() {
|
|
309
|
-
return 'GO';
|
|
310
|
-
}
|
|
311
|
-
CreateSavepointSQL(name) {
|
|
312
|
-
return `SAVE TRANSACTION ${name}`;
|
|
313
|
-
}
|
|
314
|
-
ReleaseSavepointSQL(_name) {
|
|
315
|
-
return null;
|
|
316
|
-
}
|
|
317
|
-
RollbackToSavepointSQL(name) {
|
|
318
|
-
return `ROLLBACK TRANSACTION ${name}`;
|
|
319
|
-
}
|
|
320
|
-
ExistenceCheckSQL(objectType, schema, name) {
|
|
321
|
-
const normalizedType = objectType.toUpperCase();
|
|
322
|
-
switch (normalizedType) {
|
|
323
|
-
case 'TABLE':
|
|
324
|
-
return `IF OBJECT_ID('[${schema}].[${name}]', 'U') IS NOT NULL`;
|
|
325
|
-
case 'VIEW':
|
|
326
|
-
return `IF OBJECT_ID('[${schema}].[${name}]', 'V') IS NOT NULL`;
|
|
327
|
-
case 'PROCEDURE':
|
|
328
|
-
return `IF OBJECT_ID('[${schema}].[${name}]', 'P') IS NOT NULL`;
|
|
329
|
-
case 'FUNCTION':
|
|
330
|
-
return `IF OBJECT_ID('[${schema}].[${name}]', 'FN') IS NOT NULL`;
|
|
331
|
-
case 'TRIGGER':
|
|
332
|
-
return `IF OBJECT_ID('[${schema}].[${name}]', 'TR') IS NOT NULL`;
|
|
333
|
-
default:
|
|
334
|
-
return `IF OBJECT_ID('[${schema}].[${name}]') IS NOT NULL`;
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
CreateOrReplaceSupported(_objectType) {
|
|
338
|
-
return false; // SQL Server does not support CREATE OR REPLACE
|
|
339
|
-
}
|
|
340
|
-
// ─── Full-Text Search ────────────────────────────────────────────
|
|
341
|
-
FullTextSearchPredicate(column, searchTerm) {
|
|
342
|
-
return `CONTAINS(${this.QuoteIdentifier(column)}, ${searchTerm})`;
|
|
343
|
-
}
|
|
344
|
-
FullTextIndexDDL(table, columns, catalog) {
|
|
345
|
-
const catName = catalog ?? 'MJ_FTS';
|
|
346
|
-
const cols = columns.map(c => this.QuoteIdentifier(c)).join(', ');
|
|
347
|
-
const lines = [];
|
|
348
|
-
lines.push(`IF NOT EXISTS (SELECT 1 FROM sys.fulltext_catalogs WHERE name = '${catName}')`);
|
|
349
|
-
lines.push(` CREATE FULLTEXT CATALOG [${catName}];`);
|
|
350
|
-
lines.push('');
|
|
351
|
-
lines.push(`CREATE FULLTEXT INDEX ON ${table}(${cols})`);
|
|
352
|
-
lines.push(` KEY INDEX PK_${table.replace(/[\[\].]/g, '')} ON [${catName}];`);
|
|
353
|
-
return lines.join('\n');
|
|
354
|
-
}
|
|
355
|
-
// ─── CTE / Recursion ─────────────────────────────────────────────
|
|
356
|
-
RecursiveCTESyntax() {
|
|
357
|
-
return 'WITH';
|
|
358
|
-
}
|
|
359
|
-
get AllowsOrderByInCTE() {
|
|
360
|
-
return false;
|
|
361
|
-
}
|
|
362
|
-
get DefaultPagingOrderBy() {
|
|
363
|
-
return '(SELECT NULL)';
|
|
364
|
-
}
|
|
365
|
-
get SelectListPagingOrderBy() {
|
|
366
|
-
return '1';
|
|
367
|
-
}
|
|
368
|
-
get PagingRequiresOrderBy() {
|
|
369
|
-
return true;
|
|
370
|
-
}
|
|
371
|
-
get SupportsEscapeStringLiterals() {
|
|
372
|
-
return false;
|
|
373
|
-
}
|
|
374
|
-
get SupportsDollarQuotedStrings() {
|
|
375
|
-
return false;
|
|
376
|
-
}
|
|
377
|
-
get QueryHintKeyword() {
|
|
378
|
-
return 'OPTION';
|
|
379
|
-
}
|
|
380
|
-
get CallerSQLForbiddenFunctions() {
|
|
381
|
-
return SQL_SERVER_CALLER_SQL_FORBIDDEN_FUNCTIONS;
|
|
382
|
-
}
|
|
383
140
|
StringLiteralPrefix(text) {
|
|
384
141
|
return /[^\x00-\x7F]/.test(text) ? 'N' : '';
|
|
385
142
|
}
|
|
386
143
|
EscapeLikePattern(text) {
|
|
387
144
|
return text.replace(/\[/g, '[[]').replace(/%/g, '[%]').replace(/_/g, '[_]');
|
|
388
145
|
}
|
|
389
|
-
BooleanParameterValue(value) {
|
|
390
|
-
return value ? 1 : 0;
|
|
391
|
-
}
|
|
392
|
-
// ─── Data Types ──────────────────────────────────────────────────
|
|
393
|
-
get TypeMap() {
|
|
394
|
-
return new SQLServerDataTypeMap();
|
|
395
|
-
}
|
|
396
|
-
// ─── Parameters ──────────────────────────────────────────────────
|
|
397
|
-
ParameterPlaceholder(index) {
|
|
398
|
-
return `@p${index}`;
|
|
399
|
-
}
|
|
400
146
|
ConcatOperator() {
|
|
401
147
|
return '+';
|
|
402
148
|
}
|
|
403
|
-
// ─── String Functions ────────────────────────────────────────────
|
|
404
|
-
StringSplitFunction(value, delimiter) {
|
|
405
|
-
return `STRING_SPLIT(${value}, ${delimiter})`;
|
|
406
|
-
}
|
|
407
149
|
JsonExtract(column, path) {
|
|
408
150
|
return `JSON_VALUE(${column}, '${path}')`;
|
|
409
151
|
}
|
|
410
|
-
// ─── Procedure / Function Calls ──────────────────────────────────
|
|
411
|
-
ProcedureCallSyntax(schema, name, params) {
|
|
412
|
-
const paramList = params.join(', ');
|
|
413
|
-
return `EXEC [${schema}].[${name}] ${paramList}`;
|
|
414
|
-
}
|
|
415
|
-
// ─── DDL Generation (Schema/Table) ──────────────────────────────
|
|
416
|
-
CreateSchemaDDL(schemaName) {
|
|
417
|
-
return `IF NOT EXISTS (SELECT 1 FROM sys.schemas WHERE name = '${schemaName}')\n EXEC('CREATE SCHEMA [${schemaName}]');\nGO`;
|
|
418
|
-
}
|
|
419
|
-
/** SQL Server cannot index NVARCHAR(MAX) columns — cap to NVARCHAR(450). */
|
|
420
|
-
CapIndexableType(rawSqlType) {
|
|
421
|
-
return rawSqlType.toUpperCase() === 'NVARCHAR(MAX)' ? 'NVARCHAR(450)' : rawSqlType;
|
|
422
|
-
}
|
|
423
|
-
// ─── DDL Generation (Conditional/Procedural) ────────────────────
|
|
424
152
|
DateAddExpression(unit, amount, baseExpr) {
|
|
425
153
|
return `DATEADD(${unit}, ${amount}, ${baseExpr})`;
|
|
426
154
|
}
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
return [
|
|
430
|
-
`IF NOT EXISTS (SELECT * FROM sys.tables t JOIN sys.schemas s ON t.schema_id = s.schema_id WHERE s.name='${schema}' AND t.name='${tableName}')`,
|
|
431
|
-
`BEGIN`,
|
|
432
|
-
` CREATE TABLE ${quotedTable} (`,
|
|
433
|
-
columnsDDL,
|
|
434
|
-
` );`,
|
|
435
|
-
`END;`,
|
|
436
|
-
].join('\n');
|
|
437
|
-
}
|
|
438
|
-
ConditionalBlock(condition, thenSQL, elseSQL) {
|
|
439
|
-
const lines = [
|
|
440
|
-
`IF ${condition}`,
|
|
441
|
-
`BEGIN`,
|
|
442
|
-
` ${thenSQL}`,
|
|
443
|
-
`END`,
|
|
444
|
-
];
|
|
445
|
-
if (elseSQL) {
|
|
446
|
-
lines.push(`ELSE`);
|
|
447
|
-
lines.push(`BEGIN`);
|
|
448
|
-
lines.push(` ${elseSQL}`);
|
|
449
|
-
lines.push(`END`);
|
|
450
|
-
}
|
|
451
|
-
return lines.join('\n');
|
|
452
|
-
}
|
|
453
|
-
RaiseSignalSQL(message) {
|
|
454
|
-
return `RAISERROR('${message}', 16, 1)`;
|
|
455
|
-
}
|
|
456
|
-
// ─── DDL Generation (Schema/Table continued) ────────────────────
|
|
457
|
-
AddColumnClause(col) {
|
|
458
|
-
const nullable = col.nullable ? 'NULL' : 'NOT NULL';
|
|
459
|
-
const defaultExpr = col.defaultValue != null ? ` DEFAULT ${col.defaultValue}` : '';
|
|
460
|
-
return `ADD [${col.name}] ${col.sqlType} ${nullable}${defaultExpr}`;
|
|
461
|
-
}
|
|
462
|
-
AlterColumnDDL(quotedTable, options) {
|
|
463
|
-
const nullable = options.newNullable ? 'NULL' : 'NOT NULL';
|
|
464
|
-
return `ALTER TABLE ${quotedTable}\n ALTER COLUMN [${options.columnName}] ${options.newType} ${nullable};`;
|
|
465
|
-
}
|
|
466
|
-
CommentOnColumn(schema, table, column, comment) {
|
|
467
|
-
const escaped = comment.replace(/'/g, "''");
|
|
468
|
-
return [
|
|
469
|
-
`EXEC sp_addextendedproperty`,
|
|
470
|
-
` @name = N'MS_Description',`,
|
|
471
|
-
` @value = N'${escaped}',`,
|
|
472
|
-
` @level0type = N'SCHEMA', @level0name = '${schema}',`,
|
|
473
|
-
` @level1type = N'TABLE', @level1name = '${table}',`,
|
|
474
|
-
` @level2type = N'COLUMN', @level2name = '${column}';`,
|
|
475
|
-
].join('\n');
|
|
476
|
-
}
|
|
477
|
-
FallbackType() {
|
|
478
|
-
return 'NVARCHAR(MAX)';
|
|
479
|
-
}
|
|
480
|
-
// ─── DDL Generation (Triggers/Indexes) ──────────────────────────
|
|
481
|
-
TriggerDDL(options) {
|
|
482
|
-
const events = options.events.join(', ');
|
|
483
|
-
const lines = [];
|
|
484
|
-
lines.push(`CREATE TRIGGER [${options.schema}].[${options.triggerName}]`);
|
|
485
|
-
lines.push(`ON [${options.schema}].[${options.tableName}]`);
|
|
486
|
-
lines.push(`${options.timing} ${events}`);
|
|
487
|
-
lines.push('AS');
|
|
488
|
-
lines.push('BEGIN');
|
|
489
|
-
lines.push(' SET NOCOUNT ON;');
|
|
490
|
-
lines.push(` ${options.body}`);
|
|
491
|
-
lines.push('END');
|
|
492
|
-
return lines.join('\n');
|
|
155
|
+
IIF(condition, trueVal, falseVal) {
|
|
156
|
+
return `IIF(${condition}, ${trueVal}, ${falseVal})`;
|
|
493
157
|
}
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
const inc = options.includeColumns.map(c => `[${c}]`).join(', ');
|
|
500
|
-
ddl += ` INCLUDE (${inc})`;
|
|
501
|
-
}
|
|
502
|
-
return ddl;
|
|
158
|
+
}
|
|
159
|
+
/** SQL Server binding through the `mssql` driver: `@p0` placeholders, `1`/`0` booleans. */
|
|
160
|
+
export class SQLServerBinding {
|
|
161
|
+
BooleanParameterValue(value) {
|
|
162
|
+
return value ? 1 : 0;
|
|
503
163
|
}
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
return `GRANT ${permission} ON [${schema}].[${object}] TO [${role}]`;
|
|
164
|
+
ParameterPlaceholder(index) {
|
|
165
|
+
return `@p${index}`;
|
|
507
166
|
}
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
return [
|
|
512
|
-
`EXEC sp_addextendedproperty`,
|
|
513
|
-
` @name = N'MS_Description',`,
|
|
514
|
-
` @value = N'${escapedComment}',`,
|
|
515
|
-
` @level0type = N'SCHEMA', @level0name = N'${schema}',`,
|
|
516
|
-
` @level1type = N'${level1Type}', @level1name = N'${name}'`,
|
|
517
|
-
].join('\n');
|
|
167
|
+
/** A named `@mj_p<n>` parameter; the driver declares its type, so no cast is written. */
|
|
168
|
+
TypedParameter(index, _sqlType) {
|
|
169
|
+
return { Placeholder: `@mj_p${index}`, Name: `mj_p${index}` };
|
|
518
170
|
}
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
//
|
|
523
|
-
|
|
171
|
+
IsConnectionError(e) {
|
|
172
|
+
if (!(e instanceof Error))
|
|
173
|
+
return false;
|
|
174
|
+
// mssql driver sets error.name to 'ConnectionError' for all connectivity
|
|
175
|
+
// failures (timeout, refused, reset, TLS handshake, etc.)
|
|
176
|
+
return e.name === 'ConnectionError';
|
|
524
177
|
}
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* SQL Server dialect implementation.
|
|
181
|
+
* Uses [bracket] quoting, TOP for pagination, BIT for booleans, T-SQL functions.
|
|
182
|
+
*/
|
|
183
|
+
export class SQLServerDialect extends SQLDialect {
|
|
184
|
+
constructor() {
|
|
185
|
+
super(...arguments);
|
|
186
|
+
this.writer = Object.freeze(new SQLServerWriter());
|
|
187
|
+
this.binding = Object.freeze(new SQLServerBinding());
|
|
188
|
+
this.ddl = Object.freeze(new SQLServerSchemaDDL(this.writer));
|
|
532
189
|
}
|
|
533
|
-
|
|
534
|
-
return
|
|
535
|
-
`IF NOT EXISTS (SELECT 1 FROM sys.fn_listextendedproperty(N'MS_Description', N'SCHEMA', N'${schema}', N'TABLE', N'${table}', N'COLUMN', N'${column}'))`,
|
|
536
|
-
this.CommentOnColumn(schema, table, column, comment),
|
|
537
|
-
].join('\n');
|
|
190
|
+
get PlatformKey() {
|
|
191
|
+
return 'sqlserver';
|
|
538
192
|
}
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
return {
|
|
542
|
-
listTables: `
|
|
543
|
-
SELECT s.name AS schema_name, t.name AS table_name
|
|
544
|
-
FROM sys.tables t
|
|
545
|
-
INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
|
|
546
|
-
WHERE s.name = @schema
|
|
547
|
-
ORDER BY t.name`,
|
|
548
|
-
listColumns: `
|
|
549
|
-
SELECT c.name AS column_name, ty.name AS data_type,
|
|
550
|
-
c.max_length, c.precision, c.scale, c.is_nullable,
|
|
551
|
-
c.is_identity, dc.definition AS default_value
|
|
552
|
-
FROM sys.columns c
|
|
553
|
-
INNER JOIN sys.types ty ON c.user_type_id = ty.user_type_id
|
|
554
|
-
LEFT JOIN sys.default_constraints dc ON c.default_object_id = dc.object_id
|
|
555
|
-
WHERE c.object_id = OBJECT_ID(@table)
|
|
556
|
-
ORDER BY c.column_id`,
|
|
557
|
-
listConstraints: `
|
|
558
|
-
SELECT tc.CONSTRAINT_NAME, tc.CONSTRAINT_TYPE, kcu.COLUMN_NAME
|
|
559
|
-
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
|
|
560
|
-
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
|
|
561
|
-
ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
|
|
562
|
-
WHERE tc.TABLE_SCHEMA = @schema AND tc.TABLE_NAME = @table
|
|
563
|
-
ORDER BY tc.CONSTRAINT_TYPE, kcu.ORDINAL_POSITION`,
|
|
564
|
-
listForeignKeys: `
|
|
565
|
-
SELECT fk.name AS fk_name,
|
|
566
|
-
OBJECT_SCHEMA_NAME(fk.parent_object_id) AS source_schema,
|
|
567
|
-
OBJECT_NAME(fk.parent_object_id) AS source_table,
|
|
568
|
-
COL_NAME(fkc.parent_object_id, fkc.parent_column_id) AS source_column,
|
|
569
|
-
OBJECT_SCHEMA_NAME(fk.referenced_object_id) AS target_schema,
|
|
570
|
-
OBJECT_NAME(fk.referenced_object_id) AS target_table,
|
|
571
|
-
COL_NAME(fkc.referenced_object_id, fkc.referenced_column_id) AS target_column
|
|
572
|
-
FROM sys.foreign_keys fk
|
|
573
|
-
INNER JOIN sys.foreign_key_columns fkc ON fk.object_id = fkc.constraint_object_id
|
|
574
|
-
WHERE OBJECT_SCHEMA_NAME(fk.parent_object_id) = @schema
|
|
575
|
-
ORDER BY fk.name`,
|
|
576
|
-
listIndexes: `
|
|
577
|
-
SELECT i.name AS index_name, i.is_unique, i.type_desc,
|
|
578
|
-
COL_NAME(ic.object_id, ic.column_id) AS column_name,
|
|
579
|
-
ic.is_included_column
|
|
580
|
-
FROM sys.indexes i
|
|
581
|
-
INNER JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
|
|
582
|
-
WHERE i.object_id = OBJECT_ID(@table) AND i.name IS NOT NULL
|
|
583
|
-
ORDER BY i.name, ic.key_ordinal`,
|
|
584
|
-
objectExists: `SELECT OBJECT_ID(@objectName) AS object_id`,
|
|
585
|
-
};
|
|
193
|
+
get DisplayName() {
|
|
194
|
+
return 'Microsoft SQL Server (T-SQL)';
|
|
586
195
|
}
|
|
587
|
-
/**
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
* `colCount` (columns in the constraint, for composite exclusion). Both parent + child are
|
|
591
|
-
* filtered to `schema`. Schema is embedded as a literal (no bind params) so it runs via
|
|
592
|
-
* `ExecuteSQL(sql)`.
|
|
593
|
-
*/
|
|
594
|
-
ForeignKeyGraphSQL(schema) {
|
|
595
|
-
const s = this.QuoteStringLiteral(schema);
|
|
596
|
-
// Disabled FKs (fk.is_disabled = 1) are intentionally NOT filtered: a disabled FK can't block
|
|
597
|
-
// the Entity delete, so including it only yields an extra, conservative-safe child clear.
|
|
598
|
-
// ORDER BY fk.name makes edge order — and therefore statement + dry-run order — deterministic.
|
|
599
|
-
return ('SELECT rt.name AS parentTable, rc.name AS parentRefCol, pt.name AS childTable, ' +
|
|
600
|
-
'pc.name AS childCol, pc.is_nullable AS childNullable, fk.name AS fkName, ' +
|
|
601
|
-
'(SELECT COUNT(*) FROM sys.foreign_key_columns x WHERE x.constraint_object_id = fk.object_id) AS colCount ' +
|
|
602
|
-
'FROM sys.foreign_keys fk ' +
|
|
603
|
-
'JOIN sys.foreign_key_columns fkc ON fkc.constraint_object_id = fk.object_id ' +
|
|
604
|
-
'JOIN sys.objects rt ON rt.object_id = fk.referenced_object_id ' +
|
|
605
|
-
'JOIN sys.schemas rs ON rs.schema_id = rt.schema_id ' +
|
|
606
|
-
'JOIN sys.columns rc ON rc.object_id = fk.referenced_object_id AND rc.column_id = fkc.referenced_column_id ' +
|
|
607
|
-
'JOIN sys.objects pt ON pt.object_id = fk.parent_object_id ' +
|
|
608
|
-
'JOIN sys.schemas ps ON ps.schema_id = pt.schema_id ' +
|
|
609
|
-
'JOIN sys.columns pc ON pc.object_id = fk.parent_object_id AND pc.column_id = fkc.parent_column_id ' +
|
|
610
|
-
`WHERE rs.name = ${s} AND ps.name = ${s} ` +
|
|
611
|
-
'ORDER BY fk.name');
|
|
196
|
+
/** `sql-formatter` ships T-SQL under the `tsql` language key. */
|
|
197
|
+
get FormatterLanguage() {
|
|
198
|
+
return 'tsql';
|
|
612
199
|
}
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
return '';
|
|
616
|
-
const body = statements.join(';\n');
|
|
617
|
-
return `SET QUOTED_IDENTIFIER ON;\nSET ANSI_NULLS ON;\nSET XACT_ABORT ON;\nBEGIN TRANSACTION;\n${body};\nCOMMIT TRANSACTION;`;
|
|
200
|
+
get Lexical() {
|
|
201
|
+
return SQL_SERVER_LEXICAL;
|
|
618
202
|
}
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
return `IIF(${condition}, ${trueVal}, ${falseVal})`;
|
|
203
|
+
get Grammar() {
|
|
204
|
+
return SQL_SERVER_GRAMMAR;
|
|
622
205
|
}
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
switch (options.type) {
|
|
626
|
-
case 'string':
|
|
627
|
-
return this.resolveStringType(options.maxLength);
|
|
628
|
-
case 'text':
|
|
629
|
-
return 'NVARCHAR(MAX)';
|
|
630
|
-
case 'integer':
|
|
631
|
-
return 'INT';
|
|
632
|
-
case 'bigint':
|
|
633
|
-
return 'BIGINT';
|
|
634
|
-
case 'decimal':
|
|
635
|
-
return `DECIMAL(${options.precision ?? 18},${options.scale ?? 2})`;
|
|
636
|
-
case 'boolean':
|
|
637
|
-
return 'BIT';
|
|
638
|
-
case 'datetime':
|
|
639
|
-
return 'DATETIMEOFFSET';
|
|
640
|
-
case 'date':
|
|
641
|
-
return 'DATE';
|
|
642
|
-
case 'uuid':
|
|
643
|
-
return 'UNIQUEIDENTIFIER';
|
|
644
|
-
case 'json':
|
|
645
|
-
return 'NVARCHAR(MAX)';
|
|
646
|
-
case 'float':
|
|
647
|
-
return 'FLOAT';
|
|
648
|
-
case 'time':
|
|
649
|
-
return 'TIME';
|
|
650
|
-
default:
|
|
651
|
-
return this.FallbackType();
|
|
652
|
-
}
|
|
206
|
+
get Writer() {
|
|
207
|
+
return this.writer;
|
|
653
208
|
}
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
if (!(e instanceof Error))
|
|
657
|
-
return false;
|
|
658
|
-
// mssql driver sets error.name to 'ConnectionError' for all connectivity
|
|
659
|
-
// failures (timeout, refused, reset, TLS handshake, etc.)
|
|
660
|
-
return e.name === 'ConnectionError';
|
|
209
|
+
get Types() {
|
|
210
|
+
return SQL_SERVER_TYPES;
|
|
661
211
|
}
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
if (maxLength != null && maxLength > 0) {
|
|
665
|
-
if (maxLength > 4000)
|
|
666
|
-
return 'NVARCHAR(MAX)';
|
|
667
|
-
return `NVARCHAR(${maxLength})`;
|
|
668
|
-
}
|
|
669
|
-
return 'NVARCHAR(255)';
|
|
212
|
+
get Binding() {
|
|
213
|
+
return this.binding;
|
|
670
214
|
}
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
switch (objectType.toUpperCase()) {
|
|
674
|
-
case 'TABLE': return 'TABLE';
|
|
675
|
-
case 'VIEW': return 'VIEW';
|
|
676
|
-
case 'PROCEDURE': return 'PROCEDURE';
|
|
677
|
-
case 'FUNCTION': return 'FUNCTION';
|
|
678
|
-
default: return objectType.toUpperCase();
|
|
679
|
-
}
|
|
215
|
+
get DDL() {
|
|
216
|
+
return this.ddl;
|
|
680
217
|
}
|
|
681
218
|
}
|
|
682
219
|
//# sourceMappingURL=sqlServerDialect.js.map
|