prettier-plugin-tsql 0.1.0
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/LICENSE +21 -0
- package/README.md +199 -0
- package/bin/dotnet/Microsoft.SqlServer.TransactSql.ScriptDom.dll +0 -0
- package/bin/dotnet/SqlScriptDom.deps.json +41 -0
- package/bin/dotnet/SqlScriptDom.dll +0 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/language.d.ts +3 -0
- package/dist/language.d.ts.map +1 -0
- package/dist/language.js +9 -0
- package/dist/language.js.map +1 -0
- package/dist/options.d.ts +3 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/options.js +35 -0
- package/dist/options.js.map +1 -0
- package/dist/parser/index.d.ts +5 -0
- package/dist/parser/index.d.ts.map +1 -0
- package/dist/parser/index.js +263 -0
- package/dist/parser/index.js.map +1 -0
- package/dist/parser/types.d.ts +21 -0
- package/dist/parser/types.d.ts.map +1 -0
- package/dist/parser/types.js +2 -0
- package/dist/parser/types.js.map +1 -0
- package/dist/printer/admin.d.ts +22 -0
- package/dist/printer/admin.d.ts.map +1 -0
- package/dist/printer/admin.js +250 -0
- package/dist/printer/admin.js.map +1 -0
- package/dist/printer/ddl.d.ts +36 -0
- package/dist/printer/ddl.d.ts.map +1 -0
- package/dist/printer/ddl.js +836 -0
- package/dist/printer/ddl.js.map +1 -0
- package/dist/printer/expressions.d.ts +11 -0
- package/dist/printer/expressions.d.ts.map +1 -0
- package/dist/printer/expressions.js +1475 -0
- package/dist/printer/expressions.js.map +1 -0
- package/dist/printer/helpers.d.ts +25 -0
- package/dist/printer/helpers.d.ts.map +1 -0
- package/dist/printer/helpers.js +61 -0
- package/dist/printer/helpers.js.map +1 -0
- package/dist/printer/index.d.ts +4 -0
- package/dist/printer/index.d.ts.map +1 -0
- package/dist/printer/index.js +30 -0
- package/dist/printer/index.js.map +1 -0
- package/dist/printer/procedural.d.ts +41 -0
- package/dist/printer/procedural.d.ts.map +1 -0
- package/dist/printer/procedural.js +364 -0
- package/dist/printer/procedural.js.map +1 -0
- package/dist/printer/security.d.ts +15 -0
- package/dist/printer/security.d.ts.map +1 -0
- package/dist/printer/security.js +309 -0
- package/dist/printer/security.js.map +1 -0
- package/dist/printer/statements.d.ts +18 -0
- package/dist/printer/statements.d.ts.map +1 -0
- package/dist/printer/statements.js +689 -0
- package/dist/printer/statements.js.map +1 -0
- package/dist/printer/utils.d.ts +34 -0
- package/dist/printer/utils.d.ts.map +1 -0
- package/dist/printer/utils.js +61 -0
- package/dist/printer/utils.js.map +1 -0
- package/package.json +63 -0
|
@@ -0,0 +1,689 @@
|
|
|
1
|
+
import { keyword, getDensity, getCommaStyle, hardSep, softSep, hardline, join, indent, group, line, softline, lineSuffix, appendTrailingLines, } from './utils.js';
|
|
2
|
+
import { prop, propArr, propStr, assignmentOp } from './helpers.js';
|
|
3
|
+
import { printExpression, printBoolExpr, printTableRef, printOrderByClause, printQueryExpression, } from './expressions.js';
|
|
4
|
+
import { printCreateTable, printAlterTable, printCreateIndex, printCreateProcedure, printCreateFunction, printCreateView, printCreateTrigger, printAlterIndex, printCreateSequence, printAlterSequence, printBulkInsert, printCreateTypeUddt, printCreateTypeTable, printDropObjects, printDropIndex, printCreateSynonym, printCreateSchema, printAlterSchema, printDropSchema, printCreatePartitionFunction, printAlterPartitionFunction, printDropPartitionFunction, printCreatePartitionScheme, printAlterPartitionScheme, printDropPartitionScheme, printEnableDisableTrigger, printCreateColumnStoreIndex, printCreateStatistics, printUpdateStatistics, printDropStatistics, } from './ddl.js';
|
|
5
|
+
import { printBeginTransaction, printCommitTransaction, printRollbackTransaction, printSaveTransaction, printCheckpoint, printKill, printReconfigure, printDeclareVariable, printDeclareTableVariable, printSetVariable, printSetRowCount, printUse, printPredicateSet, printSetStatistics, printSetIdentityInsert, printSetIsolationLevel, printWaitFor, printPrint, printReturn, printIf, printWhile, printExecute, printTruncateTable, printGoto, printLabel, printThrow, printRaiseError, printTryCatch, printDeclareCursor, printOpenCursor, printFetchCursor, printCloseCursor, printDeallocateCursor, printExecuteAsStatement, printRevert, } from './procedural.js';
|
|
6
|
+
import { printGrantDenyRevoke, printAlterAuthorization, printCreateUser, printAlterUser, printDropUser, printCreateLogin, printAlterLogin, printDropLogin, printCreateRole, printAlterRole, printDropRole, } from './security.js';
|
|
7
|
+
import { printDropDatabase, printDbcc, printBackupDatabase, printBackupLog, printRestore, printCreateDatabase, printAlterDatabaseSet, printAlterDatabaseCollate, printAlterDatabaseModifyName, printAlterDatabaseScopedConfigSet, printAlterDatabaseScopedConfigClear, printAlterDatabaseAddFile, printAlterDatabaseAddFileGroup, printAlterDatabaseRemoveFile, printAlterDatabaseRemoveFileGroup, printAlterDatabaseModifyFile, printAlterDatabaseModifyFileGroup, printAlterDatabaseRebuildLog, } from './admin.js';
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// Shared helpers — exported for use in ddl.ts, procedural.ts, and admin.ts
|
|
10
|
+
// (those files import printStatementWithComments from here; circular but safe in ESM)
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
/** Print a node via the expression dispatcher (no path needed for inner nodes). */
|
|
13
|
+
export function printNode(node, opts) {
|
|
14
|
+
return printExpression(node, opts, (n) => printNode(n, opts));
|
|
15
|
+
}
|
|
16
|
+
/** Print a boolean expression node via the expression dispatcher. */
|
|
17
|
+
export function printBool(node, opts) {
|
|
18
|
+
return printBoolExpr(node, opts, (n) => printNode(n, opts));
|
|
19
|
+
}
|
|
20
|
+
/** Print a query expression node via the expression dispatcher. */
|
|
21
|
+
export function qexpr(node, opts) {
|
|
22
|
+
return printQueryExpression(node, opts, (n) => printNode(n, opts));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Append a trailing comment to a doc.
|
|
26
|
+
* Line comments (--) stay on the same line via lineSuffix.
|
|
27
|
+
* Block comments go on their own line(s) after the doc.
|
|
28
|
+
*/
|
|
29
|
+
function appendTrailingComment(doc, comment) {
|
|
30
|
+
if (!comment)
|
|
31
|
+
return doc;
|
|
32
|
+
if (comment.startsWith('--'))
|
|
33
|
+
return [doc, lineSuffix([' ', comment])];
|
|
34
|
+
return appendTrailingLines(doc, comment);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Print a statement with its leading and trailing comments.
|
|
38
|
+
* Used for top-level statements and for bodies in procs/functions/triggers/IF/WHILE/etc.
|
|
39
|
+
* Exported so that ddl.ts and procedural.ts can call it (circular import — safe in ESM).
|
|
40
|
+
*/
|
|
41
|
+
export function printStatementWithComments(s, opts) {
|
|
42
|
+
const stmtDoc = printStatement(s, opts);
|
|
43
|
+
const withTrailing = appendTrailingComment(stmtDoc, s.trailingComment);
|
|
44
|
+
if (s.leadingComments?.length) {
|
|
45
|
+
return [...s.leadingComments.flatMap((c) => [c, hardline]), withTrailing];
|
|
46
|
+
}
|
|
47
|
+
return withTrailing;
|
|
48
|
+
}
|
|
49
|
+
// Walk to the rightmost non-BooleanBinary leaf (mirrors the one in expressions.ts).
|
|
50
|
+
function rightmostBoolLeaf(node) {
|
|
51
|
+
if (!node)
|
|
52
|
+
return null;
|
|
53
|
+
if (node.type === 'BooleanBinary')
|
|
54
|
+
return rightmostBoolLeaf(prop(node, 'right'));
|
|
55
|
+
return node;
|
|
56
|
+
}
|
|
57
|
+
// Append any trailing comment on the rightmost predicate leaf — covers single-predicate
|
|
58
|
+
// WHERE with a comment below it, and comments after the last predicate in a multi-predicate WHERE.
|
|
59
|
+
function printBoolDoc(where, opts) {
|
|
60
|
+
const base = printBool(where, opts);
|
|
61
|
+
const trailing = rightmostBoolLeaf(where)?.trailingComment;
|
|
62
|
+
return appendTrailingLines(base, trailing);
|
|
63
|
+
}
|
|
64
|
+
function printTable(node, opts) {
|
|
65
|
+
return printTableRef(node, opts, (n) => printNode(n, opts));
|
|
66
|
+
}
|
|
67
|
+
// ---------------------------------------------------------------------------
|
|
68
|
+
// Script / Batch
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
/** Statement types that must be isolated in their own batch. */
|
|
71
|
+
const BATCH_ISOLATING = new Set([
|
|
72
|
+
'CreateViewStatement',
|
|
73
|
+
'AlterViewStatement',
|
|
74
|
+
'CreateOrAlterViewStatement',
|
|
75
|
+
'CreateProcedureStatement',
|
|
76
|
+
'CreateOrAlterProcedureStatement',
|
|
77
|
+
'AlterProcedureStatement',
|
|
78
|
+
'CreateFunctionStatement',
|
|
79
|
+
'AlterFunctionStatement',
|
|
80
|
+
'CreateOrAlterFunctionStatement',
|
|
81
|
+
'CreateTriggerStatement',
|
|
82
|
+
'AlterTriggerStatement',
|
|
83
|
+
]);
|
|
84
|
+
export function printScript(node, opts) {
|
|
85
|
+
const batches = propArr(node, 'batches');
|
|
86
|
+
if (batches.length === 0)
|
|
87
|
+
return '';
|
|
88
|
+
const go = keyword('go', opts);
|
|
89
|
+
const parts = [];
|
|
90
|
+
for (let i = 0; i < batches.length; i++) {
|
|
91
|
+
if (i > 0)
|
|
92
|
+
parts.push(hardline, hardline);
|
|
93
|
+
parts.push(printBatch(batches[i], opts));
|
|
94
|
+
const stmts = propArr(batches[i], 'statements');
|
|
95
|
+
const needsGo = batches.length > 1 || stmts.some((s) => BATCH_ISOLATING.has(s.type));
|
|
96
|
+
if (needsGo)
|
|
97
|
+
parts.push(hardline, go);
|
|
98
|
+
}
|
|
99
|
+
return parts;
|
|
100
|
+
}
|
|
101
|
+
function printBatch(node, opts) {
|
|
102
|
+
const stmts = propArr(node, 'statements');
|
|
103
|
+
if (stmts.length === 0)
|
|
104
|
+
return '';
|
|
105
|
+
return join([hardline, hardline], stmts.map((s) => printStatementWithComments(s, opts)));
|
|
106
|
+
}
|
|
107
|
+
// ---------------------------------------------------------------------------
|
|
108
|
+
// Statement dispatcher
|
|
109
|
+
// ---------------------------------------------------------------------------
|
|
110
|
+
export function printStatement(node, opts) {
|
|
111
|
+
switch (node.type) {
|
|
112
|
+
// DML
|
|
113
|
+
case 'SelectStatement':
|
|
114
|
+
return printSelect(node, opts);
|
|
115
|
+
case 'InsertStatement':
|
|
116
|
+
return printInsert(node, opts);
|
|
117
|
+
case 'UpdateStatement':
|
|
118
|
+
return printUpdate(node, opts);
|
|
119
|
+
case 'DeleteStatement':
|
|
120
|
+
return printDelete(node, opts);
|
|
121
|
+
case 'MergeStatement':
|
|
122
|
+
return printMerge(node, opts);
|
|
123
|
+
// DDL — tables & indexes
|
|
124
|
+
case 'CreateTableStatement':
|
|
125
|
+
return printCreateTable(node, opts);
|
|
126
|
+
case 'AlterTableStatement':
|
|
127
|
+
return printAlterTable(node, opts);
|
|
128
|
+
case 'CreateIndexStatement':
|
|
129
|
+
return printCreateIndex(node, opts);
|
|
130
|
+
case 'AlterIndexStatement':
|
|
131
|
+
return printAlterIndex(node, opts);
|
|
132
|
+
case 'DropIndexStatement':
|
|
133
|
+
return printDropIndex(node, opts);
|
|
134
|
+
// DDL — procedures & functions
|
|
135
|
+
case 'CreateProcedureStatement':
|
|
136
|
+
case 'AlterProcedureStatement':
|
|
137
|
+
case 'CreateOrAlterProcedureStatement':
|
|
138
|
+
return printCreateProcedure(node, opts);
|
|
139
|
+
case 'CreateFunctionStatement':
|
|
140
|
+
case 'AlterFunctionStatement':
|
|
141
|
+
case 'CreateOrAlterFunctionStatement':
|
|
142
|
+
return printCreateFunction(node, opts);
|
|
143
|
+
// DDL — views
|
|
144
|
+
case 'CreateViewStatement':
|
|
145
|
+
case 'AlterViewStatement':
|
|
146
|
+
case 'CreateOrAlterViewStatement':
|
|
147
|
+
return printCreateView(node, opts);
|
|
148
|
+
// DDL — triggers
|
|
149
|
+
case 'CreateTriggerStatement':
|
|
150
|
+
case 'AlterTriggerStatement':
|
|
151
|
+
return printCreateTrigger(node, opts);
|
|
152
|
+
case 'EnableDisableTriggerStatement':
|
|
153
|
+
return printEnableDisableTrigger(node, opts);
|
|
154
|
+
// DDL — columnstore index
|
|
155
|
+
case 'CreateColumnStoreIndexStatement':
|
|
156
|
+
return printCreateColumnStoreIndex(node, opts);
|
|
157
|
+
// DDL — sequences
|
|
158
|
+
case 'CreateSequenceStatement':
|
|
159
|
+
return printCreateSequence(node, opts);
|
|
160
|
+
case 'AlterSequenceStatement':
|
|
161
|
+
return printAlterSequence(node, opts);
|
|
162
|
+
// DDL — types & bulk insert
|
|
163
|
+
case 'BulkInsertStatement':
|
|
164
|
+
return printBulkInsert(node, opts);
|
|
165
|
+
case 'CreateTypeUddtStatement':
|
|
166
|
+
return printCreateTypeUddt(node, opts);
|
|
167
|
+
case 'CreateTypeTableStatement':
|
|
168
|
+
return printCreateTypeTable(node, opts);
|
|
169
|
+
// DDL — DROP (shared helper)
|
|
170
|
+
case 'DropTableStatement':
|
|
171
|
+
return printDropObjects('TABLE', node, opts);
|
|
172
|
+
case 'DropProcedureStatement':
|
|
173
|
+
return printDropObjects('PROCEDURE', node, opts);
|
|
174
|
+
case 'DropViewStatement':
|
|
175
|
+
return printDropObjects('VIEW', node, opts);
|
|
176
|
+
case 'DropFunctionStatement':
|
|
177
|
+
return printDropObjects('FUNCTION', node, opts);
|
|
178
|
+
case 'DropTriggerStatement':
|
|
179
|
+
return printDropObjects('TRIGGER', node, opts);
|
|
180
|
+
case 'DropSequenceStatement':
|
|
181
|
+
return printDropObjects('SEQUENCE', node, opts);
|
|
182
|
+
case 'DropSynonymStatement':
|
|
183
|
+
return printDropObjects('SYNONYM', node, opts);
|
|
184
|
+
// DDL — synonyms
|
|
185
|
+
case 'CreateSynonymStatement':
|
|
186
|
+
return printCreateSynonym(node, opts);
|
|
187
|
+
// DDL — schemas
|
|
188
|
+
case 'CreateSchemaStatement':
|
|
189
|
+
return printCreateSchema(node, opts);
|
|
190
|
+
case 'AlterSchemaStatement':
|
|
191
|
+
return printAlterSchema(node, opts);
|
|
192
|
+
case 'DropSchemaStatement':
|
|
193
|
+
return printDropSchema(node, opts);
|
|
194
|
+
// DDL — partition functions & schemes
|
|
195
|
+
case 'CreatePartitionFunctionStatement':
|
|
196
|
+
return printCreatePartitionFunction(node, opts);
|
|
197
|
+
case 'AlterPartitionFunctionStatement':
|
|
198
|
+
return printAlterPartitionFunction(node, opts);
|
|
199
|
+
case 'DropPartitionFunctionStatement':
|
|
200
|
+
return printDropPartitionFunction(node, opts);
|
|
201
|
+
case 'CreatePartitionSchemeStatement':
|
|
202
|
+
return printCreatePartitionScheme(node, opts);
|
|
203
|
+
case 'AlterPartitionSchemeStatement':
|
|
204
|
+
return printAlterPartitionScheme(node, opts);
|
|
205
|
+
case 'DropPartitionSchemeStatement':
|
|
206
|
+
return printDropPartitionScheme(node, opts);
|
|
207
|
+
// BEGIN/END block (proc bodies, inline blocks)
|
|
208
|
+
case 'BeginEndBlock': {
|
|
209
|
+
const stmts = propArr(node, 'statements');
|
|
210
|
+
return join([hardline, hardline], stmts.map((s) => printStatementWithComments(s, opts)));
|
|
211
|
+
}
|
|
212
|
+
// Transactions
|
|
213
|
+
case 'BeginTransactionStatement':
|
|
214
|
+
return printBeginTransaction(node, opts);
|
|
215
|
+
case 'CommitTransactionStatement':
|
|
216
|
+
return printCommitTransaction(node, opts);
|
|
217
|
+
case 'RollbackTransactionStatement':
|
|
218
|
+
return printRollbackTransaction(node, opts);
|
|
219
|
+
case 'SaveTransactionStatement':
|
|
220
|
+
return printSaveTransaction(node, opts);
|
|
221
|
+
// Variable management
|
|
222
|
+
case 'DeclareVariableStatement':
|
|
223
|
+
return printDeclareVariable(node, opts);
|
|
224
|
+
case 'DeclareTableVariableStatement':
|
|
225
|
+
return printDeclareTableVariable(node, opts);
|
|
226
|
+
case 'SetVariableStatement':
|
|
227
|
+
return printSetVariable(node, opts);
|
|
228
|
+
case 'SetRowCountStatement':
|
|
229
|
+
return printSetRowCount(node, opts);
|
|
230
|
+
// Operational
|
|
231
|
+
case 'CheckpointStatement':
|
|
232
|
+
return printCheckpoint(node, opts);
|
|
233
|
+
case 'KillStatement':
|
|
234
|
+
return printKill(node, opts);
|
|
235
|
+
case 'ReconfigureStatement':
|
|
236
|
+
return printReconfigure(node, opts);
|
|
237
|
+
// DDL — statistics
|
|
238
|
+
case 'CreateStatisticsStatement':
|
|
239
|
+
return printCreateStatistics(node, opts);
|
|
240
|
+
case 'UpdateStatisticsStatement':
|
|
241
|
+
return printUpdateStatistics(node, opts);
|
|
242
|
+
case 'DropStatisticsStatement':
|
|
243
|
+
return printDropStatistics(node, opts);
|
|
244
|
+
// SET / USE / WAITFOR
|
|
245
|
+
case 'UseStatement':
|
|
246
|
+
return printUse(node, opts);
|
|
247
|
+
case 'PredicateSetStatement':
|
|
248
|
+
return printPredicateSet(node, opts);
|
|
249
|
+
case 'SetStatisticsStatement':
|
|
250
|
+
return printSetStatistics(node, opts);
|
|
251
|
+
case 'SetIdentityInsertStatement':
|
|
252
|
+
return printSetIdentityInsert(node, opts);
|
|
253
|
+
case 'SetTransactionIsolationLevelStatement':
|
|
254
|
+
return printSetIsolationLevel(node, opts);
|
|
255
|
+
case 'WaitForStatement':
|
|
256
|
+
return printWaitFor(node, opts);
|
|
257
|
+
// Output / flow
|
|
258
|
+
case 'PrintStatement':
|
|
259
|
+
return printPrint(node, opts);
|
|
260
|
+
case 'ReturnStatement':
|
|
261
|
+
return printReturn(node, opts);
|
|
262
|
+
case 'IfStatement':
|
|
263
|
+
return printIf(node, opts);
|
|
264
|
+
case 'WhileStatement':
|
|
265
|
+
return printWhile(node, opts);
|
|
266
|
+
case 'ExecuteStatement':
|
|
267
|
+
return printExecute(node, opts);
|
|
268
|
+
case 'TruncateTableStatement':
|
|
269
|
+
return printTruncateTable(node, opts);
|
|
270
|
+
case 'BreakStatement':
|
|
271
|
+
return [keyword('BREAK', opts), ';'];
|
|
272
|
+
case 'ContinueStatement':
|
|
273
|
+
return [keyword('CONTINUE', opts), ';'];
|
|
274
|
+
case 'GotoStatement':
|
|
275
|
+
return printGoto(node, opts);
|
|
276
|
+
case 'LabelStatement':
|
|
277
|
+
return printLabel(node, opts);
|
|
278
|
+
case 'ThrowStatement':
|
|
279
|
+
return printThrow(node, opts);
|
|
280
|
+
case 'RaiseErrorStatement':
|
|
281
|
+
return printRaiseError(node, opts);
|
|
282
|
+
case 'TryCatchStatement':
|
|
283
|
+
return printTryCatch(node, opts);
|
|
284
|
+
// Cursors
|
|
285
|
+
case 'DeclareCursorStatement':
|
|
286
|
+
return printDeclareCursor(node, opts);
|
|
287
|
+
case 'OpenCursorStatement':
|
|
288
|
+
return printOpenCursor(node, opts);
|
|
289
|
+
case 'FetchCursorStatement':
|
|
290
|
+
return printFetchCursor(node, opts);
|
|
291
|
+
case 'CloseCursorStatement':
|
|
292
|
+
return printCloseCursor(node, opts);
|
|
293
|
+
case 'DeallocateCursorStatement':
|
|
294
|
+
return printDeallocateCursor(node, opts);
|
|
295
|
+
// Session context
|
|
296
|
+
case 'ExecuteAsStatement':
|
|
297
|
+
return printExecuteAsStatement(node, opts);
|
|
298
|
+
case 'RevertStatement':
|
|
299
|
+
return printRevert(node, opts);
|
|
300
|
+
// Security — GRANT / DENY / REVOKE / ALTER AUTHORIZATION
|
|
301
|
+
case 'GrantStatement':
|
|
302
|
+
return printGrantDenyRevoke(node, 'GRANT', opts);
|
|
303
|
+
case 'DenyStatement':
|
|
304
|
+
return printGrantDenyRevoke(node, 'DENY', opts);
|
|
305
|
+
case 'RevokeStatement':
|
|
306
|
+
return printGrantDenyRevoke(node, 'REVOKE', opts);
|
|
307
|
+
case 'AlterAuthorizationStatement':
|
|
308
|
+
return printAlterAuthorization(node, opts);
|
|
309
|
+
// Security — USER / LOGIN / ROLE
|
|
310
|
+
case 'CreateUserStatement':
|
|
311
|
+
return printCreateUser(node, opts);
|
|
312
|
+
case 'AlterUserStatement':
|
|
313
|
+
return printAlterUser(node, opts);
|
|
314
|
+
case 'DropUserStatement':
|
|
315
|
+
return printDropUser(node, opts);
|
|
316
|
+
case 'CreateLoginStatement':
|
|
317
|
+
return printCreateLogin(node, opts);
|
|
318
|
+
case 'AlterLoginStatement':
|
|
319
|
+
return printAlterLogin(node, opts);
|
|
320
|
+
case 'DropLoginStatement':
|
|
321
|
+
return printDropLogin(node, opts);
|
|
322
|
+
case 'CreateRoleStatement':
|
|
323
|
+
return printCreateRole(node, opts);
|
|
324
|
+
case 'AlterRoleStatement':
|
|
325
|
+
return printAlterRole(node, opts);
|
|
326
|
+
case 'DropRoleStatement':
|
|
327
|
+
return printDropRole(node, opts);
|
|
328
|
+
// Database admin — DROP DATABASE, DBCC, BACKUP, RESTORE, CREATE DATABASE
|
|
329
|
+
case 'DropDatabaseStatement':
|
|
330
|
+
return printDropDatabase(node, opts);
|
|
331
|
+
case 'DbccStatement':
|
|
332
|
+
return printDbcc(node, opts);
|
|
333
|
+
case 'BackupDatabaseStatement':
|
|
334
|
+
return printBackupDatabase(node, opts);
|
|
335
|
+
case 'BackupTransactionLogStatement':
|
|
336
|
+
return printBackupLog(node, opts);
|
|
337
|
+
case 'RestoreStatement':
|
|
338
|
+
return printRestore(node, opts);
|
|
339
|
+
case 'CreateDatabaseStatement':
|
|
340
|
+
return printCreateDatabase(node, opts);
|
|
341
|
+
// ALTER DATABASE variants
|
|
342
|
+
case 'AlterDatabaseSetStatement':
|
|
343
|
+
return printAlterDatabaseSet(node, opts);
|
|
344
|
+
case 'AlterDatabaseCollateStatement':
|
|
345
|
+
return printAlterDatabaseCollate(node, opts);
|
|
346
|
+
case 'AlterDatabaseModifyNameStatement':
|
|
347
|
+
return printAlterDatabaseModifyName(node, opts);
|
|
348
|
+
case 'AlterDatabaseScopedConfigurationSetStatement':
|
|
349
|
+
return printAlterDatabaseScopedConfigSet(node, opts);
|
|
350
|
+
case 'AlterDatabaseScopedConfigurationClearStatement':
|
|
351
|
+
return printAlterDatabaseScopedConfigClear(node, opts);
|
|
352
|
+
case 'AlterDatabaseAddFileStatement':
|
|
353
|
+
return printAlterDatabaseAddFile(node, opts);
|
|
354
|
+
case 'AlterDatabaseAddFileGroupStatement':
|
|
355
|
+
return printAlterDatabaseAddFileGroup(node, opts);
|
|
356
|
+
case 'AlterDatabaseRemoveFileStatement':
|
|
357
|
+
return printAlterDatabaseRemoveFile(node, opts);
|
|
358
|
+
case 'AlterDatabaseRemoveFileGroupStatement':
|
|
359
|
+
return printAlterDatabaseRemoveFileGroup(node, opts);
|
|
360
|
+
case 'AlterDatabaseModifyFileStatement':
|
|
361
|
+
return printAlterDatabaseModifyFile(node, opts);
|
|
362
|
+
case 'AlterDatabaseModifyFileGroupStatement':
|
|
363
|
+
return printAlterDatabaseModifyFileGroup(node, opts);
|
|
364
|
+
case 'AlterDatabaseRebuildLogStatement':
|
|
365
|
+
return printAlterDatabaseRebuildLog(node, opts);
|
|
366
|
+
default:
|
|
367
|
+
return node.text ?? `/* unhandled statement: ${node.type} */`;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
// ---------------------------------------------------------------------------
|
|
371
|
+
// SELECT
|
|
372
|
+
// ---------------------------------------------------------------------------
|
|
373
|
+
function printCtes(node, opts) {
|
|
374
|
+
const ctes = propArr(node, 'ctes');
|
|
375
|
+
if (ctes.length === 0)
|
|
376
|
+
return [];
|
|
377
|
+
const leading = getCommaStyle(opts) === 'leading';
|
|
378
|
+
const cteDocs = ctes.map((cte, i) => {
|
|
379
|
+
const name = propStr(cte, 'name') ?? 'cte';
|
|
380
|
+
const cols = cte.props?.['columns'];
|
|
381
|
+
const query = prop(cte, 'query');
|
|
382
|
+
const colsPart = cols?.length
|
|
383
|
+
? [' ', group(['(', indent([softline, join([',', line], cols)]), softline, ')'])]
|
|
384
|
+
: '';
|
|
385
|
+
// trailing: align subsequent CTEs under "with " with 4 spaces
|
|
386
|
+
// leading: no prefix — the separator provides ", " before the CTE name
|
|
387
|
+
return [
|
|
388
|
+
i === 0 ? keyword('WITH', opts) + ' ' : leading ? '' : ' ',
|
|
389
|
+
name,
|
|
390
|
+
colsPart,
|
|
391
|
+
' ',
|
|
392
|
+
keyword('AS', opts),
|
|
393
|
+
' (',
|
|
394
|
+
indent([hardline, query ? qexpr(query, opts) : '']),
|
|
395
|
+
hardline,
|
|
396
|
+
')',
|
|
397
|
+
];
|
|
398
|
+
});
|
|
399
|
+
const sep = leading ? [hardline, ', '] : [',', hardline];
|
|
400
|
+
return [join(sep, cteDocs), hardline];
|
|
401
|
+
}
|
|
402
|
+
function printSelect(node, opts) {
|
|
403
|
+
const ctesDocs = printCtes(node, opts);
|
|
404
|
+
const queryExpr = prop(node, 'queryExpression');
|
|
405
|
+
const orderBy = prop(node, 'orderBy');
|
|
406
|
+
const optimizerHints = node.props?.['optimizerHints'];
|
|
407
|
+
const parts = [...ctesDocs, queryExpr ? qexpr(queryExpr, opts) : ''];
|
|
408
|
+
if (orderBy) {
|
|
409
|
+
parts.push(hardline, printOrderByClause(orderBy, opts, (n) => printNode(n, opts)));
|
|
410
|
+
}
|
|
411
|
+
if (optimizerHints?.length) {
|
|
412
|
+
parts.push(hardline, keyword('OPTION', opts), ' (', join(', ', optimizerHints.map((h) => keyword(h, opts))), ')');
|
|
413
|
+
}
|
|
414
|
+
parts.push(';');
|
|
415
|
+
return group(parts);
|
|
416
|
+
}
|
|
417
|
+
// ---------------------------------------------------------------------------
|
|
418
|
+
// INSERT
|
|
419
|
+
// ---------------------------------------------------------------------------
|
|
420
|
+
function printInsert(node, opts) {
|
|
421
|
+
const ctesDocs = printCtes(node, opts);
|
|
422
|
+
const target = prop(node, 'target');
|
|
423
|
+
const columns = propArr(node, 'columns');
|
|
424
|
+
const source = prop(node, 'source');
|
|
425
|
+
const output = prop(node, 'output');
|
|
426
|
+
const outputInto = prop(node, 'outputInto');
|
|
427
|
+
const colsPart = columns.length
|
|
428
|
+
? group([
|
|
429
|
+
' (',
|
|
430
|
+
indent([
|
|
431
|
+
softline,
|
|
432
|
+
join(softSep(opts), columns.map((c) => printNode(c, opts))),
|
|
433
|
+
]),
|
|
434
|
+
softline,
|
|
435
|
+
')',
|
|
436
|
+
])
|
|
437
|
+
: '';
|
|
438
|
+
const sourcePart = source?.type === 'ValuesSource'
|
|
439
|
+
? printValuesSource(source, opts)
|
|
440
|
+
: source
|
|
441
|
+
? [hardline, qexpr(source, opts)]
|
|
442
|
+
: '';
|
|
443
|
+
const parts = [
|
|
444
|
+
...ctesDocs,
|
|
445
|
+
keyword('INSERT INTO', opts),
|
|
446
|
+
' ',
|
|
447
|
+
target ? printTable(target, opts) : '',
|
|
448
|
+
colsPart,
|
|
449
|
+
];
|
|
450
|
+
if (outputInto)
|
|
451
|
+
parts.push(hardline, printOutputIntoClause(outputInto, opts));
|
|
452
|
+
else if (output)
|
|
453
|
+
parts.push(hardline, printOutputClause(output, opts));
|
|
454
|
+
parts.push(sourcePart, ';');
|
|
455
|
+
return group(parts);
|
|
456
|
+
}
|
|
457
|
+
function printValuesSource(node, opts) {
|
|
458
|
+
const rows = node.props?.['rows'];
|
|
459
|
+
if (!Array.isArray(rows))
|
|
460
|
+
return [hardline, keyword('VALUES', opts), ' ()'];
|
|
461
|
+
const rowDocs = rows.map((row) => {
|
|
462
|
+
const rowNode = row;
|
|
463
|
+
const vals = propArr(rowNode, 'values').map((v) => printNode(v, opts));
|
|
464
|
+
const rowDoc = group(['(', indent([softline, join(softSep(opts), vals)]), softline, ')']);
|
|
465
|
+
return rowNode.trailingComment ? [rowDoc, lineSuffix([' ', rowNode.trailingComment])] : rowDoc;
|
|
466
|
+
});
|
|
467
|
+
return [hardline, keyword('VALUES', opts), indent([hardline, join(hardSep(opts), rowDocs)])];
|
|
468
|
+
}
|
|
469
|
+
// ---------------------------------------------------------------------------
|
|
470
|
+
// UPDATE
|
|
471
|
+
// ---------------------------------------------------------------------------
|
|
472
|
+
function printUpdate(node, opts) {
|
|
473
|
+
const ctesDocs = printCtes(node, opts);
|
|
474
|
+
const density = getDensity(opts);
|
|
475
|
+
const target = prop(node, 'target');
|
|
476
|
+
const setClauses = propArr(node, 'set');
|
|
477
|
+
const from = prop(node, 'from');
|
|
478
|
+
const where = prop(node, 'where');
|
|
479
|
+
const output = prop(node, 'output');
|
|
480
|
+
const outputInto = prop(node, 'outputInto');
|
|
481
|
+
const setParts = setClauses.map((sc) => {
|
|
482
|
+
const col = prop(sc, 'column');
|
|
483
|
+
const val = prop(sc, 'value');
|
|
484
|
+
const opStr = assignmentOp(propStr(sc, 'operator') ?? 'Equals');
|
|
485
|
+
return [col ? printNode(col, opts) : '', ' ', opStr, ' ', val ? printNode(val, opts) : ''];
|
|
486
|
+
});
|
|
487
|
+
const parts = [
|
|
488
|
+
...ctesDocs,
|
|
489
|
+
keyword('UPDATE', opts),
|
|
490
|
+
' ',
|
|
491
|
+
target ? printTable(target, opts) : '',
|
|
492
|
+
hardline,
|
|
493
|
+
keyword('SET', opts),
|
|
494
|
+
density !== 'spacious' && setParts.length === 1
|
|
495
|
+
? [' ', setParts[0]]
|
|
496
|
+
: indent([hardline, join(hardSep(opts), setParts)]),
|
|
497
|
+
];
|
|
498
|
+
if (from) {
|
|
499
|
+
const tableRefs = propArr(from, 'tableReferences');
|
|
500
|
+
parts.push(hardline, keyword('FROM', opts), indent([
|
|
501
|
+
hardline,
|
|
502
|
+
join(hardSep(opts), tableRefs.map((tr) => printTable(tr, opts))),
|
|
503
|
+
]));
|
|
504
|
+
}
|
|
505
|
+
if (outputInto)
|
|
506
|
+
parts.push(hardline, printOutputIntoClause(outputInto, opts));
|
|
507
|
+
else if (output)
|
|
508
|
+
parts.push(hardline, printOutputClause(output, opts));
|
|
509
|
+
if (where) {
|
|
510
|
+
const inline = density !== 'spacious' && where.type !== 'BooleanBinary';
|
|
511
|
+
parts.push(hardline, keyword('WHERE', opts), inline ? [' ', printBoolDoc(where, opts)] : indent([hardline, printBoolDoc(where, opts)]));
|
|
512
|
+
}
|
|
513
|
+
parts.push(';');
|
|
514
|
+
return group(parts);
|
|
515
|
+
}
|
|
516
|
+
// ---------------------------------------------------------------------------
|
|
517
|
+
// DELETE
|
|
518
|
+
// ---------------------------------------------------------------------------
|
|
519
|
+
function printDelete(node, opts) {
|
|
520
|
+
const ctesDocs = printCtes(node, opts);
|
|
521
|
+
const density = getDensity(opts);
|
|
522
|
+
const target = prop(node, 'target');
|
|
523
|
+
const from = prop(node, 'from');
|
|
524
|
+
const where = prop(node, 'where');
|
|
525
|
+
const output = prop(node, 'output');
|
|
526
|
+
const outputInto = prop(node, 'outputInto');
|
|
527
|
+
const parts = [...ctesDocs, keyword('DELETE FROM', opts), ' ', target ? printTable(target, opts) : ''];
|
|
528
|
+
if (from) {
|
|
529
|
+
const tableRefs = propArr(from, 'tableReferences');
|
|
530
|
+
parts.push(hardline, keyword('FROM', opts), indent([
|
|
531
|
+
hardline,
|
|
532
|
+
join(hardSep(opts), tableRefs.map((tr) => printTable(tr, opts))),
|
|
533
|
+
]));
|
|
534
|
+
}
|
|
535
|
+
if (outputInto)
|
|
536
|
+
parts.push(hardline, printOutputIntoClause(outputInto, opts));
|
|
537
|
+
else if (output)
|
|
538
|
+
parts.push(hardline, printOutputClause(output, opts));
|
|
539
|
+
if (where) {
|
|
540
|
+
const inline = density !== 'spacious' && where.type !== 'BooleanBinary';
|
|
541
|
+
parts.push(hardline, keyword('WHERE', opts), inline ? [' ', printBoolDoc(where, opts)] : indent([hardline, printBoolDoc(where, opts)]));
|
|
542
|
+
}
|
|
543
|
+
parts.push(';');
|
|
544
|
+
return group(parts);
|
|
545
|
+
}
|
|
546
|
+
// ---------------------------------------------------------------------------
|
|
547
|
+
// OUTPUT clause (shared by INSERT / UPDATE / DELETE / MERGE)
|
|
548
|
+
// ---------------------------------------------------------------------------
|
|
549
|
+
function printOutputColumns(columns, opts) {
|
|
550
|
+
return join([',', line], columns.map((c) => printNode(c, opts)));
|
|
551
|
+
}
|
|
552
|
+
function printOutputClause(node, opts) {
|
|
553
|
+
const columns = propArr(node, 'columns');
|
|
554
|
+
return group([keyword('OUTPUT', opts), indent([line, printOutputColumns(columns, opts)])]);
|
|
555
|
+
}
|
|
556
|
+
function printOutputIntoClause(node, opts) {
|
|
557
|
+
const columns = propArr(node, 'columns');
|
|
558
|
+
const into = prop(node, 'into');
|
|
559
|
+
const intoColumns = propArr(node, 'intoColumns');
|
|
560
|
+
const intoColsPart = intoColumns.length
|
|
561
|
+
? [
|
|
562
|
+
' (',
|
|
563
|
+
join(', ', intoColumns.map((c) => printNode(c, opts))),
|
|
564
|
+
')',
|
|
565
|
+
]
|
|
566
|
+
: '';
|
|
567
|
+
return group([
|
|
568
|
+
keyword('OUTPUT', opts),
|
|
569
|
+
indent([line, printOutputColumns(columns, opts)]),
|
|
570
|
+
hardline,
|
|
571
|
+
keyword('INTO', opts),
|
|
572
|
+
' ',
|
|
573
|
+
into ? printTable(into, opts) : '',
|
|
574
|
+
intoColsPart,
|
|
575
|
+
]);
|
|
576
|
+
}
|
|
577
|
+
// ---------------------------------------------------------------------------
|
|
578
|
+
// MERGE
|
|
579
|
+
// ---------------------------------------------------------------------------
|
|
580
|
+
function printMerge(node, opts) {
|
|
581
|
+
const ctesDocs = printCtes(node, opts);
|
|
582
|
+
const target = prop(node, 'target');
|
|
583
|
+
const targetAlias = propStr(node, 'targetAlias');
|
|
584
|
+
const source = prop(node, 'source');
|
|
585
|
+
const on = prop(node, 'on');
|
|
586
|
+
const clauses = propArr(node, 'clauses');
|
|
587
|
+
const output = prop(node, 'output');
|
|
588
|
+
const outputInto = prop(node, 'outputInto');
|
|
589
|
+
const targetDoc = target
|
|
590
|
+
? targetAlias
|
|
591
|
+
? [printTable(target, opts), ' ', keyword('AS', opts), ' ', targetAlias]
|
|
592
|
+
: printTable(target, opts)
|
|
593
|
+
: '';
|
|
594
|
+
const density = getDensity(opts);
|
|
595
|
+
let onDoc = '';
|
|
596
|
+
if (on) {
|
|
597
|
+
const isMultiple = on.type === 'BooleanBinary';
|
|
598
|
+
if (density === 'compact') {
|
|
599
|
+
onDoc = [' ', keyword('ON', opts), ' ', printBool(on, opts)];
|
|
600
|
+
}
|
|
601
|
+
else if (density === 'standard' && !isMultiple) {
|
|
602
|
+
onDoc = [' ', keyword('ON', opts), group([indent([line, printBool(on, opts)])])];
|
|
603
|
+
}
|
|
604
|
+
else {
|
|
605
|
+
onDoc = [' ', keyword('ON', opts), indent([hardline, printBool(on, opts)])];
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
const parts = [
|
|
609
|
+
...ctesDocs,
|
|
610
|
+
keyword('MERGE INTO', opts),
|
|
611
|
+
' ',
|
|
612
|
+
targetDoc,
|
|
613
|
+
hardline,
|
|
614
|
+
keyword('USING', opts),
|
|
615
|
+
' ',
|
|
616
|
+
source ? printTable(source, opts) : '',
|
|
617
|
+
onDoc,
|
|
618
|
+
];
|
|
619
|
+
for (const clause of clauses) {
|
|
620
|
+
parts.push(hardline, printMergeClause(clause, opts));
|
|
621
|
+
}
|
|
622
|
+
if (outputInto)
|
|
623
|
+
parts.push(hardline, printOutputIntoClause(outputInto, opts));
|
|
624
|
+
else if (output)
|
|
625
|
+
parts.push(hardline, printOutputClause(output, opts));
|
|
626
|
+
parts.push(';');
|
|
627
|
+
return group(parts);
|
|
628
|
+
}
|
|
629
|
+
function printMergeClause(node, opts) {
|
|
630
|
+
const condition = propStr(node, 'condition') ?? 'Matched';
|
|
631
|
+
const predicate = prop(node, 'predicate');
|
|
632
|
+
const action = prop(node, 'action');
|
|
633
|
+
const condKw = condition === 'Matched'
|
|
634
|
+
? keyword('WHEN MATCHED', opts)
|
|
635
|
+
: condition === 'NotMatchedByTarget'
|
|
636
|
+
? keyword('WHEN NOT MATCHED BY TARGET', opts)
|
|
637
|
+
: condition === 'NotMatched'
|
|
638
|
+
? keyword('WHEN NOT MATCHED', opts)
|
|
639
|
+
: keyword('WHEN NOT MATCHED BY SOURCE', opts);
|
|
640
|
+
const predPart = predicate ? [' ', keyword('AND', opts), ' ', printBool(predicate, opts)] : '';
|
|
641
|
+
return [
|
|
642
|
+
condKw,
|
|
643
|
+
predPart,
|
|
644
|
+
' ',
|
|
645
|
+
keyword('THEN', opts),
|
|
646
|
+
indent([hardline, action ? printMergeAction(action, opts) : '']),
|
|
647
|
+
];
|
|
648
|
+
}
|
|
649
|
+
function printMergeAction(node, opts) {
|
|
650
|
+
switch (node.type) {
|
|
651
|
+
case 'MergeUpdateAction': {
|
|
652
|
+
const setParts = propArr(node, 'set').map((sc) => {
|
|
653
|
+
const col = prop(sc, 'column');
|
|
654
|
+
const val = prop(sc, 'value');
|
|
655
|
+
const opStr = assignmentOp(propStr(sc, 'operator') ?? 'Equals');
|
|
656
|
+
return [col ? printNode(col, opts) : '', ' ', opStr, ' ', val ? printNode(val, opts) : ''];
|
|
657
|
+
});
|
|
658
|
+
return [keyword('UPDATE SET', opts), indent([hardline, join([',', hardline], setParts)])];
|
|
659
|
+
}
|
|
660
|
+
case 'MergeInsertAction': {
|
|
661
|
+
const columns = propArr(node, 'columns');
|
|
662
|
+
const source = prop(node, 'source');
|
|
663
|
+
const colsPart = columns.length
|
|
664
|
+
? [
|
|
665
|
+
'(',
|
|
666
|
+
join(', ', columns.map((c) => printNode(c, opts))),
|
|
667
|
+
')',
|
|
668
|
+
]
|
|
669
|
+
: '';
|
|
670
|
+
return [keyword('INSERT', opts), ' ', colsPart, source ? printMergeValues(source, opts) : ''];
|
|
671
|
+
}
|
|
672
|
+
case 'MergeDeleteAction':
|
|
673
|
+
return keyword('DELETE', opts);
|
|
674
|
+
default:
|
|
675
|
+
return node.text ?? `/* ${node.type} */`;
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
function printMergeValues(source, opts) {
|
|
679
|
+
if (source.type !== 'ValuesSource')
|
|
680
|
+
return source.text ? [hardline, source.text] : '';
|
|
681
|
+
const rows = source.props?.['rows'];
|
|
682
|
+
if (!Array.isArray(rows) || rows.length === 0)
|
|
683
|
+
return [hardline, keyword('VALUES', opts), ' ()'];
|
|
684
|
+
// MERGE INSERT has exactly one VALUES row
|
|
685
|
+
const row = rows[0];
|
|
686
|
+
const vals = propArr(row, 'values').map((v) => printNode(v, opts));
|
|
687
|
+
return [hardline, keyword('VALUES', opts), ' (', join(', ', vals), ')'];
|
|
688
|
+
}
|
|
689
|
+
//# sourceMappingURL=statements.js.map
|