rawsql-ts 0.11.43-beta → 0.12.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/README.md +12 -12
- package/dist/esm/index.js +18 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index.min.js +15 -19
- package/dist/esm/index.min.js.map +4 -4
- package/dist/esm/src/index.d.ts +18 -0
- package/dist/esm/src/index.js +18 -0
- package/dist/esm/src/index.js.map +1 -1
- package/dist/esm/src/models/BinarySelectQuery.d.ts +25 -1
- package/dist/esm/src/models/BinarySelectQuery.js +28 -0
- package/dist/esm/src/models/BinarySelectQuery.js.map +1 -1
- package/dist/esm/src/models/Clause.d.ts +14 -2
- package/dist/esm/src/models/Clause.js +26 -1
- package/dist/esm/src/models/Clause.js.map +1 -1
- package/dist/esm/src/models/CreateTableQuery.d.ts +99 -5
- package/dist/esm/src/models/CreateTableQuery.js +85 -10
- package/dist/esm/src/models/CreateTableQuery.js.map +1 -1
- package/dist/esm/src/models/DDLStatements.d.ts +195 -0
- package/dist/esm/src/models/DDLStatements.js +201 -0
- package/dist/esm/src/models/DDLStatements.js.map +1 -0
- package/dist/esm/src/models/DeleteQuery.d.ts +17 -0
- package/dist/esm/src/models/DeleteQuery.js +16 -0
- package/dist/esm/src/models/DeleteQuery.js.map +1 -0
- package/dist/esm/src/models/InsertQuery.d.ts +7 -1
- package/dist/esm/src/models/InsertQuery.js +6 -2
- package/dist/esm/src/models/InsertQuery.js.map +1 -1
- package/dist/esm/src/models/MergeQuery.d.ts +63 -0
- package/dist/esm/src/models/MergeQuery.js +94 -0
- package/dist/esm/src/models/MergeQuery.js.map +1 -0
- package/dist/esm/src/models/SelectQuery.d.ts +37 -1
- package/dist/esm/src/models/SelectQuery.js +4 -1
- package/dist/esm/src/models/SelectQuery.js.map +1 -1
- package/dist/esm/src/models/SimpleSelectQuery.d.ts +29 -1
- package/dist/esm/src/models/SimpleSelectQuery.js +32 -0
- package/dist/esm/src/models/SimpleSelectQuery.js.map +1 -1
- package/dist/esm/src/models/SqlComponent.d.ts +2 -1
- package/dist/esm/src/models/SqlComponent.js +1 -1
- package/dist/esm/src/models/SqlComponent.js.map +1 -1
- package/dist/esm/src/models/SqlPrintToken.d.ts +36 -0
- package/dist/esm/src/models/SqlPrintToken.js +35 -0
- package/dist/esm/src/models/SqlPrintToken.js.map +1 -1
- package/dist/esm/src/models/ValuesQuery.d.ts +25 -1
- package/dist/esm/src/models/ValuesQuery.js +28 -0
- package/dist/esm/src/models/ValuesQuery.js.map +1 -1
- package/dist/esm/src/parsers/AlterTableParser.d.ts +25 -0
- package/dist/esm/src/parsers/AlterTableParser.js +428 -0
- package/dist/esm/src/parsers/AlterTableParser.js.map +1 -0
- package/dist/esm/src/parsers/AnalyzeStatementParser.d.ts +13 -0
- package/dist/esm/src/parsers/AnalyzeStatementParser.js +90 -0
- package/dist/esm/src/parsers/AnalyzeStatementParser.js.map +1 -0
- package/dist/esm/src/parsers/CreateIndexParser.d.ts +16 -0
- package/dist/esm/src/parsers/CreateIndexParser.js +237 -0
- package/dist/esm/src/parsers/CreateIndexParser.js.map +1 -0
- package/dist/esm/src/parsers/CreateTableParser.d.ts +41 -0
- package/dist/esm/src/parsers/CreateTableParser.js +734 -0
- package/dist/esm/src/parsers/CreateTableParser.js.map +1 -0
- package/dist/esm/src/parsers/DeleteClauseParser.d.ts +11 -0
- package/dist/esm/src/parsers/DeleteClauseParser.js +33 -0
- package/dist/esm/src/parsers/DeleteClauseParser.js.map +1 -0
- package/dist/esm/src/parsers/DeleteQueryParser.d.ts +16 -0
- package/dist/esm/src/parsers/DeleteQueryParser.js +73 -0
- package/dist/esm/src/parsers/DeleteQueryParser.js.map +1 -0
- package/dist/esm/src/parsers/DropConstraintParser.d.ts +12 -0
- package/dist/esm/src/parsers/DropConstraintParser.js +47 -0
- package/dist/esm/src/parsers/DropConstraintParser.js.map +1 -0
- package/dist/esm/src/parsers/DropIndexParser.d.ts +12 -0
- package/dist/esm/src/parsers/DropIndexParser.js +69 -0
- package/dist/esm/src/parsers/DropIndexParser.js.map +1 -0
- package/dist/esm/src/parsers/DropTableParser.d.ts +12 -0
- package/dist/esm/src/parsers/DropTableParser.js +59 -0
- package/dist/esm/src/parsers/DropTableParser.js.map +1 -0
- package/dist/esm/src/parsers/ExplainStatementParser.d.ts +23 -0
- package/dist/esm/src/parsers/ExplainStatementParser.js +185 -0
- package/dist/esm/src/parsers/ExplainStatementParser.js.map +1 -0
- package/dist/esm/src/parsers/FunctionExpressionParser.d.ts +4 -0
- package/dist/esm/src/parsers/FunctionExpressionParser.js +25 -8
- package/dist/esm/src/parsers/FunctionExpressionParser.js.map +1 -1
- package/dist/esm/src/parsers/InsertQueryParser.js +103 -31
- package/dist/esm/src/parsers/InsertQueryParser.js.map +1 -1
- package/dist/esm/src/parsers/MergeQueryParser.d.ts +26 -0
- package/dist/esm/src/parsers/MergeQueryParser.js +479 -0
- package/dist/esm/src/parsers/MergeQueryParser.js.map +1 -0
- package/dist/esm/src/parsers/ParenExpressionParser.js +25 -0
- package/dist/esm/src/parsers/ParenExpressionParser.js.map +1 -1
- package/dist/esm/src/parsers/ReturningClauseParser.js +50 -7
- package/dist/esm/src/parsers/ReturningClauseParser.js.map +1 -1
- package/dist/esm/src/parsers/SelectClauseParser.js +3 -3
- package/dist/esm/src/parsers/SelectClauseParser.js.map +1 -1
- package/dist/esm/src/parsers/SelectQueryParser.d.ts +4 -0
- package/dist/esm/src/parsers/SelectQueryParser.js +4 -0
- package/dist/esm/src/parsers/SelectQueryParser.js.map +1 -1
- package/dist/esm/src/parsers/SetClauseParser.js +97 -15
- package/dist/esm/src/parsers/SetClauseParser.js.map +1 -1
- package/dist/esm/src/parsers/SqlParser.d.ts +40 -0
- package/dist/esm/src/parsers/SqlParser.js +400 -0
- package/dist/esm/src/parsers/SqlParser.js.map +1 -0
- package/dist/esm/src/parsers/SqlPrintTokenParser.d.ts +65 -3
- package/dist/esm/src/parsers/SqlPrintTokenParser.js +1145 -38
- package/dist/esm/src/parsers/SqlPrintTokenParser.js.map +1 -1
- package/dist/esm/src/parsers/SqlTokenizer.d.ts +24 -2
- package/dist/esm/src/parsers/SqlTokenizer.js +135 -74
- package/dist/esm/src/parsers/SqlTokenizer.js.map +1 -1
- package/dist/esm/src/parsers/UpdateQueryParser.js +11 -1
- package/dist/esm/src/parsers/UpdateQueryParser.js.map +1 -1
- package/dist/esm/src/parsers/UsingClauseParser.d.ts +11 -0
- package/dist/esm/src/parsers/UsingClauseParser.js +29 -0
- package/dist/esm/src/parsers/UsingClauseParser.js.map +1 -0
- package/dist/esm/src/parsers/ValueParser.js +5 -1
- package/dist/esm/src/parsers/ValueParser.js.map +1 -1
- package/dist/esm/src/parsers/ValuesQueryParser.d.ts +0 -2
- package/dist/esm/src/parsers/ValuesQueryParser.js +5 -45
- package/dist/esm/src/parsers/ValuesQueryParser.js.map +1 -1
- package/dist/esm/src/parsers/utils/LexemeCommentUtils.d.ts +6 -0
- package/dist/esm/src/parsers/utils/LexemeCommentUtils.js +26 -0
- package/dist/esm/src/parsers/utils/LexemeCommentUtils.js.map +1 -0
- package/dist/esm/src/tokenReaders/CommandTokenReader.js +50 -2
- package/dist/esm/src/tokenReaders/CommandTokenReader.js.map +1 -1
- package/dist/esm/src/tokenReaders/LiteralTokenReader.js +8 -5
- package/dist/esm/src/tokenReaders/LiteralTokenReader.js.map +1 -1
- package/dist/esm/src/tokenReaders/OperatorTokenReader.js +10 -1
- package/dist/esm/src/tokenReaders/OperatorTokenReader.js.map +1 -1
- package/dist/esm/src/tokenReaders/TypeTokenReader.js +11 -1
- package/dist/esm/src/tokenReaders/TypeTokenReader.js.map +1 -1
- package/dist/esm/src/transformers/InsertQuerySelectValuesConverter.d.ts +18 -0
- package/dist/esm/src/transformers/InsertQuerySelectValuesConverter.js +118 -0
- package/dist/esm/src/transformers/InsertQuerySelectValuesConverter.js.map +1 -0
- package/dist/esm/src/transformers/LinePrinter.d.ts +1 -0
- package/dist/esm/src/transformers/LinePrinter.js +12 -0
- package/dist/esm/src/transformers/LinePrinter.js.map +1 -1
- package/dist/esm/src/transformers/OnelineFormattingHelper.d.ts +29 -0
- package/dist/esm/src/transformers/OnelineFormattingHelper.js +95 -0
- package/dist/esm/src/transformers/OnelineFormattingHelper.js.map +1 -0
- package/dist/esm/src/transformers/QueryBuilder.d.ts +47 -13
- package/dist/esm/src/transformers/QueryBuilder.js +424 -62
- package/dist/esm/src/transformers/QueryBuilder.js.map +1 -1
- package/dist/esm/src/transformers/SqlFormatter.d.ts +13 -1
- package/dist/esm/src/transformers/SqlFormatter.js +13 -4
- package/dist/esm/src/transformers/SqlFormatter.js.map +1 -1
- package/dist/esm/src/transformers/SqlPrinter.d.ts +47 -8
- package/dist/esm/src/transformers/SqlPrinter.js +625 -72
- package/dist/esm/src/transformers/SqlPrinter.js.map +1 -1
- package/dist/esm/src/types/Formatting.d.ts +8 -0
- package/dist/esm/src/types/Formatting.js +2 -0
- package/dist/esm/src/types/Formatting.js.map +1 -0
- package/dist/esm/src/utils/ParserStringUtils.d.ts +6 -0
- package/dist/esm/src/utils/ParserStringUtils.js +28 -0
- package/dist/esm/src/utils/ParserStringUtils.js.map +1 -0
- package/dist/esm/tsconfig.browser.tsbuildinfo +1 -1
- package/dist/index.min.js +14 -18
- package/dist/index.min.js.map +4 -4
- package/dist/src/index.d.ts +18 -0
- package/dist/src/index.js +18 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/models/BinarySelectQuery.d.ts +25 -1
- package/dist/src/models/BinarySelectQuery.js +28 -0
- package/dist/src/models/BinarySelectQuery.js.map +1 -1
- package/dist/src/models/Clause.d.ts +14 -2
- package/dist/src/models/Clause.js +29 -2
- package/dist/src/models/Clause.js.map +1 -1
- package/dist/src/models/CreateTableQuery.d.ts +99 -5
- package/dist/src/models/CreateTableQuery.js +90 -11
- package/dist/src/models/CreateTableQuery.js.map +1 -1
- package/dist/src/models/DDLStatements.d.ts +195 -0
- package/dist/src/models/DDLStatements.js +216 -0
- package/dist/src/models/DDLStatements.js.map +1 -0
- package/dist/src/models/DeleteQuery.d.ts +17 -0
- package/dist/src/models/DeleteQuery.js +20 -0
- package/dist/src/models/DeleteQuery.js.map +1 -0
- package/dist/src/models/InsertQuery.d.ts +7 -1
- package/dist/src/models/InsertQuery.js +6 -2
- package/dist/src/models/InsertQuery.js.map +1 -1
- package/dist/src/models/MergeQuery.d.ts +63 -0
- package/dist/src/models/MergeQuery.js +104 -0
- package/dist/src/models/MergeQuery.js.map +1 -0
- package/dist/src/models/SelectQuery.d.ts +37 -1
- package/dist/src/models/SelectQuery.js +7 -1
- package/dist/src/models/SelectQuery.js.map +1 -1
- package/dist/src/models/SimpleSelectQuery.d.ts +29 -1
- package/dist/src/models/SimpleSelectQuery.js +32 -0
- package/dist/src/models/SimpleSelectQuery.js.map +1 -1
- package/dist/src/models/SqlComponent.d.ts +2 -1
- package/dist/src/models/SqlComponent.js +1 -1
- package/dist/src/models/SqlComponent.js.map +1 -1
- package/dist/src/models/SqlPrintToken.d.ts +36 -0
- package/dist/src/models/SqlPrintToken.js +35 -0
- package/dist/src/models/SqlPrintToken.js.map +1 -1
- package/dist/src/models/ValuesQuery.d.ts +25 -1
- package/dist/src/models/ValuesQuery.js +28 -0
- package/dist/src/models/ValuesQuery.js.map +1 -1
- package/dist/src/parsers/AlterTableParser.d.ts +25 -0
- package/dist/src/parsers/AlterTableParser.js +432 -0
- package/dist/src/parsers/AlterTableParser.js.map +1 -0
- package/dist/src/parsers/AnalyzeStatementParser.d.ts +13 -0
- package/dist/src/parsers/AnalyzeStatementParser.js +94 -0
- package/dist/src/parsers/AnalyzeStatementParser.js.map +1 -0
- package/dist/src/parsers/CreateIndexParser.d.ts +16 -0
- package/dist/src/parsers/CreateIndexParser.js +241 -0
- package/dist/src/parsers/CreateIndexParser.js.map +1 -0
- package/dist/src/parsers/CreateTableParser.d.ts +41 -0
- package/dist/src/parsers/CreateTableParser.js +738 -0
- package/dist/src/parsers/CreateTableParser.js.map +1 -0
- package/dist/src/parsers/DeleteClauseParser.d.ts +11 -0
- package/dist/src/parsers/DeleteClauseParser.js +37 -0
- package/dist/src/parsers/DeleteClauseParser.js.map +1 -0
- package/dist/src/parsers/DeleteQueryParser.d.ts +16 -0
- package/dist/src/parsers/DeleteQueryParser.js +77 -0
- package/dist/src/parsers/DeleteQueryParser.js.map +1 -0
- package/dist/src/parsers/DropConstraintParser.d.ts +12 -0
- package/dist/src/parsers/DropConstraintParser.js +51 -0
- package/dist/src/parsers/DropConstraintParser.js.map +1 -0
- package/dist/src/parsers/DropIndexParser.d.ts +12 -0
- package/dist/src/parsers/DropIndexParser.js +73 -0
- package/dist/src/parsers/DropIndexParser.js.map +1 -0
- package/dist/src/parsers/DropTableParser.d.ts +12 -0
- package/dist/src/parsers/DropTableParser.js +63 -0
- package/dist/src/parsers/DropTableParser.js.map +1 -0
- package/dist/src/parsers/ExplainStatementParser.d.ts +23 -0
- package/dist/src/parsers/ExplainStatementParser.js +189 -0
- package/dist/src/parsers/ExplainStatementParser.js.map +1 -0
- package/dist/src/parsers/FunctionExpressionParser.d.ts +4 -0
- package/dist/src/parsers/FunctionExpressionParser.js +25 -8
- package/dist/src/parsers/FunctionExpressionParser.js.map +1 -1
- package/dist/src/parsers/InsertQueryParser.js +103 -31
- package/dist/src/parsers/InsertQueryParser.js.map +1 -1
- package/dist/src/parsers/MergeQueryParser.d.ts +26 -0
- package/dist/src/parsers/MergeQueryParser.js +483 -0
- package/dist/src/parsers/MergeQueryParser.js.map +1 -0
- package/dist/src/parsers/ParenExpressionParser.js +25 -0
- package/dist/src/parsers/ParenExpressionParser.js.map +1 -1
- package/dist/src/parsers/ReturningClauseParser.js +50 -7
- package/dist/src/parsers/ReturningClauseParser.js.map +1 -1
- package/dist/src/parsers/SelectClauseParser.js +2 -2
- package/dist/src/parsers/SelectClauseParser.js.map +1 -1
- package/dist/src/parsers/SelectQueryParser.d.ts +4 -0
- package/dist/src/parsers/SelectQueryParser.js +4 -0
- package/dist/src/parsers/SelectQueryParser.js.map +1 -1
- package/dist/src/parsers/SetClauseParser.js +97 -15
- package/dist/src/parsers/SetClauseParser.js.map +1 -1
- package/dist/src/parsers/SqlParser.d.ts +40 -0
- package/dist/src/parsers/SqlParser.js +409 -0
- package/dist/src/parsers/SqlParser.js.map +1 -0
- package/dist/src/parsers/SqlPrintTokenParser.d.ts +65 -3
- package/dist/src/parsers/SqlPrintTokenParser.js +1143 -36
- package/dist/src/parsers/SqlPrintTokenParser.js.map +1 -1
- package/dist/src/parsers/SqlTokenizer.d.ts +24 -2
- package/dist/src/parsers/SqlTokenizer.js +139 -74
- package/dist/src/parsers/SqlTokenizer.js.map +1 -1
- package/dist/src/parsers/UpdateQueryParser.js +11 -1
- package/dist/src/parsers/UpdateQueryParser.js.map +1 -1
- package/dist/src/parsers/UsingClauseParser.d.ts +11 -0
- package/dist/src/parsers/UsingClauseParser.js +33 -0
- package/dist/src/parsers/UsingClauseParser.js.map +1 -0
- package/dist/src/parsers/ValueParser.js +5 -1
- package/dist/src/parsers/ValueParser.js.map +1 -1
- package/dist/src/parsers/ValuesQueryParser.d.ts +0 -2
- package/dist/src/parsers/ValuesQueryParser.js +5 -45
- package/dist/src/parsers/ValuesQueryParser.js.map +1 -1
- package/dist/src/parsers/utils/LexemeCommentUtils.d.ts +6 -0
- package/dist/src/parsers/utils/LexemeCommentUtils.js +29 -0
- package/dist/src/parsers/utils/LexemeCommentUtils.js.map +1 -0
- package/dist/src/tokenReaders/CommandTokenReader.js +50 -2
- package/dist/src/tokenReaders/CommandTokenReader.js.map +1 -1
- package/dist/src/tokenReaders/LiteralTokenReader.js +8 -5
- package/dist/src/tokenReaders/LiteralTokenReader.js.map +1 -1
- package/dist/src/tokenReaders/OperatorTokenReader.js +10 -1
- package/dist/src/tokenReaders/OperatorTokenReader.js.map +1 -1
- package/dist/src/tokenReaders/TypeTokenReader.js +11 -1
- package/dist/src/tokenReaders/TypeTokenReader.js.map +1 -1
- package/dist/src/transformers/InsertQuerySelectValuesConverter.d.ts +18 -0
- package/dist/src/transformers/InsertQuerySelectValuesConverter.js +122 -0
- package/dist/src/transformers/InsertQuerySelectValuesConverter.js.map +1 -0
- package/dist/src/transformers/LinePrinter.d.ts +1 -0
- package/dist/src/transformers/LinePrinter.js +12 -0
- package/dist/src/transformers/LinePrinter.js.map +1 -1
- package/dist/src/transformers/OnelineFormattingHelper.d.ts +29 -0
- package/dist/src/transformers/OnelineFormattingHelper.js +99 -0
- package/dist/src/transformers/OnelineFormattingHelper.js.map +1 -0
- package/dist/src/transformers/QueryBuilder.d.ts +47 -13
- package/dist/src/transformers/QueryBuilder.js +433 -60
- package/dist/src/transformers/QueryBuilder.js.map +1 -1
- package/dist/src/transformers/SqlFormatter.d.ts +13 -1
- package/dist/src/transformers/SqlFormatter.js +20 -5
- package/dist/src/transformers/SqlFormatter.js.map +1 -1
- package/dist/src/transformers/SqlPrinter.d.ts +47 -8
- package/dist/src/transformers/SqlPrinter.js +625 -72
- package/dist/src/transformers/SqlPrinter.js.map +1 -1
- package/dist/src/types/Formatting.d.ts +8 -0
- package/dist/src/types/Formatting.js +3 -0
- package/dist/src/types/Formatting.js.map +1 -0
- package/dist/src/utils/ParserStringUtils.d.ts +6 -0
- package/dist/src/utils/ParserStringUtils.js +31 -0
- package/dist/src/utils/ParserStringUtils.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
|
@@ -4,6 +4,10 @@ exports.SqlPrinter = void 0;
|
|
|
4
4
|
const SqlPrintToken_1 = require("../models/SqlPrintToken");
|
|
5
5
|
const LinePrinter_1 = require("./LinePrinter");
|
|
6
6
|
const FormatOptionResolver_1 = require("./FormatOptionResolver");
|
|
7
|
+
const OnelineFormattingHelper_1 = require("./OnelineFormattingHelper");
|
|
8
|
+
const CREATE_TABLE_SINGLE_PAREN_KEYWORDS = new Set(['unique', 'check', 'key', 'index']);
|
|
9
|
+
const CREATE_TABLE_MULTI_PAREN_KEYWORDS = new Set(['primary key', 'foreign key', 'unique key']);
|
|
10
|
+
const CREATE_TABLE_PAREN_KEYWORDS_WITH_IDENTIFIER = new Set(['references']);
|
|
7
11
|
/**
|
|
8
12
|
* SqlPrinter formats a SqlPrintToken tree into a SQL string with flexible style options.
|
|
9
13
|
*
|
|
@@ -29,9 +33,11 @@ class SqlPrinter {
|
|
|
29
33
|
* @param options Optional style settings for pretty printing
|
|
30
34
|
*/
|
|
31
35
|
constructor(options) {
|
|
32
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
|
|
36
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
|
|
33
37
|
/** Track whether we are currently inside a WITH clause for full-oneline formatting */
|
|
34
38
|
this.insideWithClause = false;
|
|
39
|
+
/** Tracks nesting depth while formatting MERGE WHEN predicate segments */
|
|
40
|
+
this.mergeWhenPredicateDepth = 0;
|
|
35
41
|
/** Pending line comment that needs a forced newline before next token */
|
|
36
42
|
this.pendingLineCommentBreak = null;
|
|
37
43
|
/** Accumulates lines when reconstructing multi-line block comments inside CommentBlocks */
|
|
@@ -50,19 +56,32 @@ class SqlPrinter {
|
|
|
50
56
|
this.andBreak = (_e = options === null || options === void 0 ? void 0 : options.andBreak) !== null && _e !== void 0 ? _e : 'none';
|
|
51
57
|
this.orBreak = (_f = options === null || options === void 0 ? void 0 : options.orBreak) !== null && _f !== void 0 ? _f : 'none';
|
|
52
58
|
this.keywordCase = (_g = options === null || options === void 0 ? void 0 : options.keywordCase) !== null && _g !== void 0 ? _g : 'none';
|
|
53
|
-
this.
|
|
54
|
-
this.withClauseStyle = (
|
|
55
|
-
this.commentStyle = (
|
|
56
|
-
this.parenthesesOneLine = (
|
|
57
|
-
this.betweenOneLine = (
|
|
58
|
-
this.valuesOneLine = (
|
|
59
|
-
this.joinOneLine = (
|
|
60
|
-
this.caseOneLine = (
|
|
61
|
-
this.subqueryOneLine = (
|
|
62
|
-
this.indentNestedParentheses = (
|
|
59
|
+
this.commentExportMode = this.resolveCommentExportMode(options === null || options === void 0 ? void 0 : options.exportComment);
|
|
60
|
+
this.withClauseStyle = (_h = options === null || options === void 0 ? void 0 : options.withClauseStyle) !== null && _h !== void 0 ? _h : 'standard';
|
|
61
|
+
this.commentStyle = (_j = options === null || options === void 0 ? void 0 : options.commentStyle) !== null && _j !== void 0 ? _j : 'block';
|
|
62
|
+
this.parenthesesOneLine = (_k = options === null || options === void 0 ? void 0 : options.parenthesesOneLine) !== null && _k !== void 0 ? _k : false;
|
|
63
|
+
this.betweenOneLine = (_l = options === null || options === void 0 ? void 0 : options.betweenOneLine) !== null && _l !== void 0 ? _l : false;
|
|
64
|
+
this.valuesOneLine = (_m = options === null || options === void 0 ? void 0 : options.valuesOneLine) !== null && _m !== void 0 ? _m : false;
|
|
65
|
+
this.joinOneLine = (_o = options === null || options === void 0 ? void 0 : options.joinOneLine) !== null && _o !== void 0 ? _o : false;
|
|
66
|
+
this.caseOneLine = (_p = options === null || options === void 0 ? void 0 : options.caseOneLine) !== null && _p !== void 0 ? _p : false;
|
|
67
|
+
this.subqueryOneLine = (_q = options === null || options === void 0 ? void 0 : options.subqueryOneLine) !== null && _q !== void 0 ? _q : false;
|
|
68
|
+
this.indentNestedParentheses = (_r = options === null || options === void 0 ? void 0 : options.indentNestedParentheses) !== null && _r !== void 0 ? _r : false;
|
|
69
|
+
this.insertColumnsOneLine = (_s = options === null || options === void 0 ? void 0 : options.insertColumnsOneLine) !== null && _s !== void 0 ? _s : false;
|
|
70
|
+
this.whenOneLine = (_t = options === null || options === void 0 ? void 0 : options.whenOneLine) !== null && _t !== void 0 ? _t : false;
|
|
71
|
+
const onelineOptions = {
|
|
72
|
+
parenthesesOneLine: this.parenthesesOneLine,
|
|
73
|
+
betweenOneLine: this.betweenOneLine,
|
|
74
|
+
valuesOneLine: this.valuesOneLine,
|
|
75
|
+
joinOneLine: this.joinOneLine,
|
|
76
|
+
caseOneLine: this.caseOneLine,
|
|
77
|
+
subqueryOneLine: this.subqueryOneLine,
|
|
78
|
+
insertColumnsOneLine: this.insertColumnsOneLine,
|
|
79
|
+
withClauseStyle: this.withClauseStyle,
|
|
80
|
+
};
|
|
81
|
+
this.onelineHelper = new OnelineFormattingHelper_1.OnelineFormattingHelper(onelineOptions);
|
|
63
82
|
this.linePrinter = new LinePrinter_1.LinePrinter(this.indentChar, this.indentSize, this.newline, this.commaBreak);
|
|
64
83
|
// Initialize
|
|
65
|
-
this.indentIncrementContainers = new Set((
|
|
84
|
+
this.indentIncrementContainers = new Set((_u = options === null || options === void 0 ? void 0 : options.indentIncrementContainerTypes) !== null && _u !== void 0 ? _u : [
|
|
66
85
|
SqlPrintToken_1.SqlPrintTokenContainerType.SelectClause,
|
|
67
86
|
SqlPrintToken_1.SqlPrintTokenContainerType.FromClause,
|
|
68
87
|
SqlPrintToken_1.SqlPrintTokenContainerType.WhereClause,
|
|
@@ -82,7 +101,11 @@ class SqlPrinter {
|
|
|
82
101
|
SqlPrintToken_1.SqlPrintTokenContainerType.CaseThenValue,
|
|
83
102
|
SqlPrintToken_1.SqlPrintTokenContainerType.ElseClause,
|
|
84
103
|
SqlPrintToken_1.SqlPrintTokenContainerType.CaseElseValue,
|
|
85
|
-
SqlPrintToken_1.SqlPrintTokenContainerType.SimpleSelectQuery
|
|
104
|
+
SqlPrintToken_1.SqlPrintTokenContainerType.SimpleSelectQuery,
|
|
105
|
+
SqlPrintToken_1.SqlPrintTokenContainerType.CreateTableDefinition,
|
|
106
|
+
SqlPrintToken_1.SqlPrintTokenContainerType.AlterTableStatement,
|
|
107
|
+
SqlPrintToken_1.SqlPrintTokenContainerType.IndexColumnList,
|
|
108
|
+
SqlPrintToken_1.SqlPrintTokenContainerType.SetClause
|
|
86
109
|
// Note: CommentBlock is intentionally excluded from indentIncrementContainers
|
|
87
110
|
// because it serves as a grouping mechanism without affecting indentation.
|
|
88
111
|
// CaseExpression, SwitchCaseArgument, CaseKeyValuePair, and ElseClause
|
|
@@ -111,7 +134,42 @@ class SqlPrinter {
|
|
|
111
134
|
this.appendToken(token, level, undefined, 0, false);
|
|
112
135
|
return this.linePrinter.print();
|
|
113
136
|
}
|
|
114
|
-
|
|
137
|
+
// Resolve legacy boolean values into explicit comment export modes.
|
|
138
|
+
resolveCommentExportMode(option) {
|
|
139
|
+
if (option === undefined) {
|
|
140
|
+
return 'none';
|
|
141
|
+
}
|
|
142
|
+
if (option === true) {
|
|
143
|
+
return 'full';
|
|
144
|
+
}
|
|
145
|
+
if (option === false) {
|
|
146
|
+
return 'none';
|
|
147
|
+
}
|
|
148
|
+
return option;
|
|
149
|
+
}
|
|
150
|
+
// Determine whether the current mode allows emitting inline comments.
|
|
151
|
+
rendersInlineComments() {
|
|
152
|
+
return this.commentExportMode === 'full';
|
|
153
|
+
}
|
|
154
|
+
// Decide if a comment block or token should be rendered given its context.
|
|
155
|
+
shouldRenderComment(token, context) {
|
|
156
|
+
if (context === null || context === void 0 ? void 0 : context.forceRender) {
|
|
157
|
+
return this.commentExportMode !== 'none';
|
|
158
|
+
}
|
|
159
|
+
switch (this.commentExportMode) {
|
|
160
|
+
case 'full':
|
|
161
|
+
return true;
|
|
162
|
+
case 'none':
|
|
163
|
+
return false;
|
|
164
|
+
case 'header-only':
|
|
165
|
+
return token.isHeaderComment === true;
|
|
166
|
+
case 'top-header-only':
|
|
167
|
+
return token.isHeaderComment === true && Boolean(context === null || context === void 0 ? void 0 : context.isTopLevelContainer);
|
|
168
|
+
default:
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
appendToken(token, level, parentContainerType, caseContextDepth = 0, indentParentActive = false, commentContext) {
|
|
115
173
|
// Track WITH clause context for full-oneline formatting
|
|
116
174
|
const wasInsideWithClause = this.insideWithClause;
|
|
117
175
|
if (token.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.WithClause && this.withClauseStyle === 'full-oneline') {
|
|
@@ -120,6 +178,45 @@ class SqlPrinter {
|
|
|
120
178
|
if (this.shouldSkipToken(token)) {
|
|
121
179
|
return;
|
|
122
180
|
}
|
|
181
|
+
const containerIsTopLevel = parentContainerType === undefined;
|
|
182
|
+
let leadingCommentCount = 0;
|
|
183
|
+
// Collect leading comment blocks with context so we can respect the export mode.
|
|
184
|
+
const leadingCommentContexts = [];
|
|
185
|
+
if (token.innerTokens && token.innerTokens.length > 0) {
|
|
186
|
+
while (leadingCommentCount < token.innerTokens.length) {
|
|
187
|
+
const leadingCandidate = token.innerTokens[leadingCommentCount];
|
|
188
|
+
if (leadingCandidate.containerType !== SqlPrintToken_1.SqlPrintTokenContainerType.CommentBlock) {
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
const context = {
|
|
192
|
+
position: 'leading',
|
|
193
|
+
isTopLevelContainer: containerIsTopLevel,
|
|
194
|
+
};
|
|
195
|
+
const shouldRender = this.shouldRenderComment(leadingCandidate, context);
|
|
196
|
+
leadingCommentContexts.push({ token: leadingCandidate, context, shouldRender });
|
|
197
|
+
leadingCommentCount++;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
const hasRenderableLeadingComment = leadingCommentContexts.some(item => item.shouldRender);
|
|
201
|
+
const leadingCommentIndentLevel = hasRenderableLeadingComment
|
|
202
|
+
? this.getLeadingCommentIndentLevel(parentContainerType, level)
|
|
203
|
+
: null;
|
|
204
|
+
if (hasRenderableLeadingComment
|
|
205
|
+
&& !this.isOnelineMode()
|
|
206
|
+
&& this.shouldAddNewlineBeforeLeadingComments(parentContainerType)) {
|
|
207
|
+
const currentLine = this.linePrinter.getCurrentLine();
|
|
208
|
+
if (currentLine.text.trim().length > 0) {
|
|
209
|
+
// Align the newline before leading comments with the intended comment indentation.
|
|
210
|
+
this.linePrinter.appendNewline(leadingCommentIndentLevel !== null && leadingCommentIndentLevel !== void 0 ? leadingCommentIndentLevel : level);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
for (const leading of leadingCommentContexts) {
|
|
214
|
+
if (!leading.shouldRender) {
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
// Keep leading comment processing aligned with its computed indentation level.
|
|
218
|
+
this.appendToken(leading.token, leadingCommentIndentLevel !== null && leadingCommentIndentLevel !== void 0 ? leadingCommentIndentLevel : level, token.containerType, caseContextDepth, indentParentActive, leading.context);
|
|
219
|
+
}
|
|
123
220
|
if (this.smartCommentBlockBuilder && token.containerType !== SqlPrintToken_1.SqlPrintTokenContainerType.CommentBlock && token.type !== SqlPrintToken_1.SqlPrintTokenType.commentNewline) {
|
|
124
221
|
this.flushSmartCommentBlockBuilder();
|
|
125
222
|
}
|
|
@@ -133,8 +230,17 @@ class SqlPrinter {
|
|
|
133
230
|
return;
|
|
134
231
|
}
|
|
135
232
|
}
|
|
233
|
+
// Fallback context applies when the caller did not provide comment metadata.
|
|
234
|
+
const effectiveCommentContext = commentContext !== null && commentContext !== void 0 ? commentContext : {
|
|
235
|
+
position: 'inline',
|
|
236
|
+
isTopLevelContainer: containerIsTopLevel,
|
|
237
|
+
};
|
|
136
238
|
if (token.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.CommentBlock) {
|
|
137
|
-
this.
|
|
239
|
+
if (!this.shouldRenderComment(token, effectiveCommentContext)) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
const commentLevel = this.getCommentBaseIndentLevel(level, parentContainerType);
|
|
243
|
+
this.handleCommentBlockContainer(token, commentLevel, effectiveCommentContext);
|
|
138
244
|
return;
|
|
139
245
|
}
|
|
140
246
|
const current = this.linePrinter.getCurrentLine();
|
|
@@ -149,7 +255,7 @@ class SqlPrinter {
|
|
|
149
255
|
this.handleCommaToken(token, level, parentContainerType);
|
|
150
256
|
}
|
|
151
257
|
else if (token.type === SqlPrintToken_1.SqlPrintTokenType.parenthesis) {
|
|
152
|
-
this.handleParenthesisToken(token, level, indentParentActive);
|
|
258
|
+
this.handleParenthesisToken(token, level, indentParentActive, parentContainerType);
|
|
153
259
|
}
|
|
154
260
|
else if (token.type === SqlPrintToken_1.SqlPrintTokenType.operator && token.text.toLowerCase() === 'and') {
|
|
155
261
|
this.handleAndOperatorToken(token, level, parentContainerType, caseContextDepth);
|
|
@@ -157,34 +263,34 @@ class SqlPrinter {
|
|
|
157
263
|
else if (token.type === SqlPrintToken_1.SqlPrintTokenType.operator && token.text.toLowerCase() === 'or') {
|
|
158
264
|
this.handleOrOperatorToken(token, level, parentContainerType, caseContextDepth);
|
|
159
265
|
}
|
|
160
|
-
else if (token.containerType ===
|
|
266
|
+
else if (token.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.JoinClause) {
|
|
161
267
|
this.handleJoinClauseToken(token, level);
|
|
162
268
|
}
|
|
163
269
|
else if (token.type === SqlPrintToken_1.SqlPrintTokenType.comment) {
|
|
164
|
-
if (this.
|
|
165
|
-
this.
|
|
270
|
+
if (this.shouldRenderComment(token, effectiveCommentContext)) {
|
|
271
|
+
const commentLevel = this.getCommentBaseIndentLevel(level, parentContainerType);
|
|
272
|
+
this.printCommentToken(token.text, commentLevel, parentContainerType);
|
|
166
273
|
}
|
|
167
274
|
}
|
|
168
275
|
else if (token.type === SqlPrintToken_1.SqlPrintTokenType.space) {
|
|
169
276
|
this.handleSpaceToken(token, parentContainerType);
|
|
170
277
|
}
|
|
171
278
|
else if (token.type === SqlPrintToken_1.SqlPrintTokenType.commentNewline) {
|
|
172
|
-
this.
|
|
279
|
+
if (this.whenOneLine && parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.MergeWhenClause) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
const commentLevel = this.getCommentBaseIndentLevel(level, parentContainerType);
|
|
283
|
+
this.handleCommentNewlineToken(token, commentLevel);
|
|
173
284
|
}
|
|
174
285
|
else if (token.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.CommonTable && this.withClauseStyle === 'cte-oneline') {
|
|
175
286
|
this.handleCteOnelineToken(token, level);
|
|
176
287
|
return; // Return early to avoid processing innerTokens
|
|
177
288
|
}
|
|
178
|
-
else if ((token
|
|
179
|
-
(token.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.BetweenExpression && this.betweenOneLine) ||
|
|
180
|
-
(token.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.Values && this.valuesOneLine) ||
|
|
181
|
-
(token.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.JoinOnClause && this.joinOneLine) ||
|
|
182
|
-
(token.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.CaseExpression && this.caseOneLine) ||
|
|
183
|
-
(token.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.InlineQuery && this.subqueryOneLine)) {
|
|
289
|
+
else if (this.shouldFormatContainerAsOneline(token, shouldIndentNested)) {
|
|
184
290
|
this.handleOnelineToken(token, level);
|
|
185
291
|
return; // Return early to avoid processing innerTokens
|
|
186
292
|
}
|
|
187
|
-
else {
|
|
293
|
+
else if (!this.tryAppendInsertClauseTokenText(token.text, parentContainerType)) {
|
|
188
294
|
this.linePrinter.appendText(token.text);
|
|
189
295
|
}
|
|
190
296
|
// append keyword tokens(not indented)
|
|
@@ -198,7 +304,18 @@ class SqlPrinter {
|
|
|
198
304
|
let increasedIndent = false;
|
|
199
305
|
const shouldIncreaseIndent = this.indentIncrementContainers.has(token.containerType) || shouldIndentNested;
|
|
200
306
|
const delayIndentNewline = shouldIndentNested && token.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.ParenExpression;
|
|
201
|
-
|
|
307
|
+
const isAlterTableStatement = token.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.AlterTableStatement;
|
|
308
|
+
let deferAlterTableIndent = false;
|
|
309
|
+
const alignExplainChild = this.shouldAlignExplainStatementChild(parentContainerType, token.containerType);
|
|
310
|
+
if (alignExplainChild) {
|
|
311
|
+
// Keep EXPLAIN target statements flush left so they render like standalone statements.
|
|
312
|
+
if (!this.isOnelineMode() && current.text !== '') {
|
|
313
|
+
this.linePrinter.appendNewline(level);
|
|
314
|
+
}
|
|
315
|
+
innerLevel = level;
|
|
316
|
+
increasedIndent = false;
|
|
317
|
+
}
|
|
318
|
+
else if (!this.isOnelineMode() && shouldIncreaseIndent) {
|
|
202
319
|
if (this.insideWithClause && this.withClauseStyle === 'full-oneline') {
|
|
203
320
|
// Keep everything on one line for full-oneline WITH clauses.
|
|
204
321
|
}
|
|
@@ -207,15 +324,96 @@ class SqlPrinter {
|
|
|
207
324
|
increasedIndent = true;
|
|
208
325
|
}
|
|
209
326
|
else if (current.text !== '') {
|
|
210
|
-
|
|
327
|
+
if (isAlterTableStatement) {
|
|
328
|
+
// Delay the first line break so ALTER TABLE keeps the table name on the opening line.
|
|
329
|
+
innerLevel = level + 1;
|
|
330
|
+
increasedIndent = true;
|
|
331
|
+
deferAlterTableIndent = true;
|
|
332
|
+
}
|
|
333
|
+
else {
|
|
334
|
+
let targetIndentLevel = level + 1;
|
|
335
|
+
if (token.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.SetClause &&
|
|
336
|
+
parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.MergeUpdateAction) {
|
|
337
|
+
targetIndentLevel = level + 2;
|
|
338
|
+
}
|
|
339
|
+
if (this.shouldAlignCreateTableSelect(token.containerType, parentContainerType)) {
|
|
340
|
+
innerLevel = level;
|
|
341
|
+
increasedIndent = false;
|
|
342
|
+
this.linePrinter.appendNewline(level);
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
innerLevel = targetIndentLevel;
|
|
346
|
+
increasedIndent = true;
|
|
347
|
+
this.linePrinter.appendNewline(innerLevel);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
else if (token.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.SetClause) {
|
|
352
|
+
innerLevel = parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.MergeUpdateAction ? level + 2 : level + 1;
|
|
211
353
|
increasedIndent = true;
|
|
212
|
-
|
|
354
|
+
current.level = innerLevel;
|
|
213
355
|
}
|
|
214
356
|
}
|
|
215
|
-
|
|
357
|
+
const isMergeWhenClause = this.whenOneLine && token.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.MergeWhenClause;
|
|
358
|
+
let mergePredicateActive = isMergeWhenClause;
|
|
359
|
+
let alterTableTableRendered = false;
|
|
360
|
+
let alterTableIndentInserted = false;
|
|
361
|
+
for (let i = leadingCommentCount; i < token.innerTokens.length; i++) {
|
|
216
362
|
const child = token.innerTokens[i];
|
|
363
|
+
const nextChild = token.innerTokens[i + 1];
|
|
364
|
+
const previousEntry = this.findPreviousSignificantToken(token.innerTokens, i);
|
|
365
|
+
const previousChild = previousEntry === null || previousEntry === void 0 ? void 0 : previousEntry.token;
|
|
366
|
+
const priorEntry = previousEntry ? this.findPreviousSignificantToken(token.innerTokens, previousEntry.index) : undefined;
|
|
367
|
+
const priorChild = priorEntry === null || priorEntry === void 0 ? void 0 : priorEntry.token;
|
|
368
|
+
const childIsAction = this.isMergeActionContainer(child);
|
|
369
|
+
const nextIsAction = this.isMergeActionContainer(nextChild);
|
|
370
|
+
const inMergePredicate = mergePredicateActive && !childIsAction;
|
|
371
|
+
if (isAlterTableStatement) {
|
|
372
|
+
if (child.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.QualifiedName) {
|
|
373
|
+
// Track when the table name has been printed so we can defer indentation until after it.
|
|
374
|
+
alterTableTableRendered = true;
|
|
375
|
+
}
|
|
376
|
+
else if (deferAlterTableIndent && alterTableTableRendered && !alterTableIndentInserted) {
|
|
377
|
+
if (!this.isOnelineMode()) {
|
|
378
|
+
this.linePrinter.appendNewline(innerLevel);
|
|
379
|
+
}
|
|
380
|
+
alterTableIndentInserted = true;
|
|
381
|
+
deferAlterTableIndent = false;
|
|
382
|
+
if (!this.isOnelineMode() && child.type === SqlPrintToken_1.SqlPrintTokenType.space) {
|
|
383
|
+
// Drop the space token because we already emitted a newline.
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
if (child.type === SqlPrintToken_1.SqlPrintTokenType.space) {
|
|
389
|
+
if (this.shouldConvertSpaceToClauseBreak(token.containerType, nextChild)) {
|
|
390
|
+
if (!this.isOnelineMode()) {
|
|
391
|
+
// Use a dedicated indent resolver so clause breaks can shift indentation for nested blocks.
|
|
392
|
+
const clauseBreakIndent = this.getClauseBreakIndentLevel(token.containerType, innerLevel);
|
|
393
|
+
this.linePrinter.appendNewline(clauseBreakIndent);
|
|
394
|
+
}
|
|
395
|
+
if (isMergeWhenClause && nextIsAction) {
|
|
396
|
+
mergePredicateActive = false;
|
|
397
|
+
}
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
this.handleSpaceToken(child, token.containerType, nextChild, previousChild, priorChild);
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
217
403
|
const childIndentParentActive = token.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.ParenExpression ? shouldIndentNested : indentParentActive;
|
|
218
|
-
|
|
404
|
+
if (inMergePredicate) {
|
|
405
|
+
this.mergeWhenPredicateDepth++;
|
|
406
|
+
}
|
|
407
|
+
const childCommentContext = child.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.CommentBlock
|
|
408
|
+
? { position: 'inline', isTopLevelContainer: containerIsTopLevel }
|
|
409
|
+
: undefined;
|
|
410
|
+
this.appendToken(child, innerLevel, token.containerType, nextCaseContextDepth, childIndentParentActive, childCommentContext);
|
|
411
|
+
if (inMergePredicate) {
|
|
412
|
+
this.mergeWhenPredicateDepth--;
|
|
413
|
+
}
|
|
414
|
+
if (childIsAction && isMergeWhenClause) {
|
|
415
|
+
mergePredicateActive = false;
|
|
416
|
+
}
|
|
219
417
|
}
|
|
220
418
|
if (this.smartCommentBlockBuilder && this.smartCommentBlockBuilder.mode === 'line') {
|
|
221
419
|
this.flushSmartCommentBlockBuilder();
|
|
@@ -232,6 +430,21 @@ class SqlPrinter {
|
|
|
232
430
|
this.linePrinter.appendNewline(level);
|
|
233
431
|
}
|
|
234
432
|
}
|
|
433
|
+
shouldAlignExplainStatementChild(parentType, childType) {
|
|
434
|
+
if (parentType !== SqlPrintToken_1.SqlPrintTokenContainerType.ExplainStatement) {
|
|
435
|
+
return false;
|
|
436
|
+
}
|
|
437
|
+
switch (childType) {
|
|
438
|
+
case SqlPrintToken_1.SqlPrintTokenContainerType.SimpleSelectQuery:
|
|
439
|
+
case SqlPrintToken_1.SqlPrintTokenContainerType.InsertQuery:
|
|
440
|
+
case SqlPrintToken_1.SqlPrintTokenContainerType.UpdateQuery:
|
|
441
|
+
case SqlPrintToken_1.SqlPrintTokenContainerType.DeleteQuery:
|
|
442
|
+
case SqlPrintToken_1.SqlPrintTokenContainerType.MergeQuery:
|
|
443
|
+
return true;
|
|
444
|
+
default:
|
|
445
|
+
return false;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
235
448
|
isCaseContext(containerType) {
|
|
236
449
|
switch (containerType) {
|
|
237
450
|
case SqlPrintToken_1.SqlPrintTokenContainerType.CaseExpression:
|
|
@@ -268,11 +481,13 @@ class SqlPrinter {
|
|
|
268
481
|
}
|
|
269
482
|
handleKeywordToken(token, level, parentContainerType, caseContextDepth = 0) {
|
|
270
483
|
const lower = token.text.toLowerCase();
|
|
271
|
-
if (lower === 'and' &&
|
|
484
|
+
if (lower === 'and' &&
|
|
485
|
+
(this.andBreak !== 'none' || (this.whenOneLine && parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.MergeWhenClause))) {
|
|
272
486
|
this.handleAndOperatorToken(token, level, parentContainerType, caseContextDepth);
|
|
273
487
|
return;
|
|
274
488
|
}
|
|
275
|
-
else if (lower === 'or' &&
|
|
489
|
+
else if (lower === 'or' &&
|
|
490
|
+
(this.orBreak !== 'none' || (this.whenOneLine && parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.MergeWhenClause))) {
|
|
276
491
|
this.handleOrOperatorToken(token, level, parentContainerType, caseContextDepth);
|
|
277
492
|
return;
|
|
278
493
|
}
|
|
@@ -281,18 +496,50 @@ class SqlPrinter {
|
|
|
281
496
|
this.linePrinter.appendText(text);
|
|
282
497
|
return;
|
|
283
498
|
}
|
|
499
|
+
this.ensureSpaceBeforeKeyword();
|
|
284
500
|
this.linePrinter.appendText(text);
|
|
285
501
|
}
|
|
502
|
+
ensureSpaceBeforeKeyword() {
|
|
503
|
+
const currentLine = this.linePrinter.getCurrentLine();
|
|
504
|
+
if (currentLine.text === '') {
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
const lastChar = currentLine.text[currentLine.text.length - 1];
|
|
508
|
+
if (lastChar === '(') {
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
this.ensureTrailingSpace();
|
|
512
|
+
}
|
|
513
|
+
ensureTrailingSpace() {
|
|
514
|
+
const currentLine = this.linePrinter.getCurrentLine();
|
|
515
|
+
if (currentLine.text === '') {
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
if (!currentLine.text.endsWith(' ')) {
|
|
519
|
+
currentLine.text += ' ';
|
|
520
|
+
}
|
|
521
|
+
currentLine.text = currentLine.text.replace(/\s+$/, ' ');
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Normalizes INSERT column list token text when one-line formatting is active.
|
|
525
|
+
*/
|
|
526
|
+
tryAppendInsertClauseTokenText(text, parentContainerType) {
|
|
527
|
+
const currentLineText = this.linePrinter.getCurrentLine().text;
|
|
528
|
+
const result = this.onelineHelper.formatInsertClauseToken(text, parentContainerType, currentLineText, () => this.ensureTrailingSpace());
|
|
529
|
+
if (!result.handled) {
|
|
530
|
+
return false;
|
|
531
|
+
}
|
|
532
|
+
if (result.text) {
|
|
533
|
+
this.linePrinter.appendText(result.text);
|
|
534
|
+
}
|
|
535
|
+
return true;
|
|
536
|
+
}
|
|
286
537
|
handleCommaToken(token, level, parentContainerType) {
|
|
287
538
|
const text = token.text;
|
|
288
539
|
const isWithinWithClause = parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.WithClause;
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
effectiveCommaBreak = this.cteCommaBreak;
|
|
293
|
-
}
|
|
294
|
-
else if (isWithinValuesClause) {
|
|
295
|
-
effectiveCommaBreak = this.valuesCommaBreak;
|
|
540
|
+
let effectiveCommaBreak = this.onelineHelper.resolveCommaBreak(parentContainerType, this.commaBreak, this.cteCommaBreak, this.valuesCommaBreak);
|
|
541
|
+
if (parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.SetClause) {
|
|
542
|
+
effectiveCommaBreak = 'before';
|
|
296
543
|
}
|
|
297
544
|
// Skip comma newlines when inside WITH clause with full-oneline style
|
|
298
545
|
if (this.insideWithClause && this.withClauseStyle === 'full-oneline') {
|
|
@@ -310,6 +557,14 @@ class SqlPrinter {
|
|
|
310
557
|
}
|
|
311
558
|
if (!(this.insideWithClause && this.withClauseStyle === 'full-oneline')) {
|
|
312
559
|
this.linePrinter.appendNewline(level);
|
|
560
|
+
if (this.newline === ' ') {
|
|
561
|
+
// Remove the spacer introduced by space newlines so commas attach directly to the preceding token.
|
|
562
|
+
this.linePrinter.trimTrailingWhitespaceFromPreviousLine();
|
|
563
|
+
}
|
|
564
|
+
if (parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.InsertClause) {
|
|
565
|
+
// Align comma-prefixed column entries under the INSERT column indentation.
|
|
566
|
+
this.linePrinter.getCurrentLine().level = level + 1;
|
|
567
|
+
}
|
|
313
568
|
}
|
|
314
569
|
this.linePrinter.appendText(text);
|
|
315
570
|
if (previousCommaBreak !== 'before') {
|
|
@@ -338,6 +593,9 @@ class SqlPrinter {
|
|
|
338
593
|
this.linePrinter.commaBreak = 'none';
|
|
339
594
|
}
|
|
340
595
|
this.linePrinter.appendText(text);
|
|
596
|
+
if (this.onelineHelper.isInsertClauseOneline(parentContainerType)) {
|
|
597
|
+
this.ensureTrailingSpace();
|
|
598
|
+
}
|
|
341
599
|
if (previousCommaBreak !== 'none') {
|
|
342
600
|
this.linePrinter.commaBreak = previousCommaBreak;
|
|
343
601
|
}
|
|
@@ -352,6 +610,11 @@ class SqlPrinter {
|
|
|
352
610
|
this.linePrinter.appendText(text);
|
|
353
611
|
return;
|
|
354
612
|
}
|
|
613
|
+
if (this.whenOneLine &&
|
|
614
|
+
(parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.MergeWhenClause || this.mergeWhenPredicateDepth > 0)) {
|
|
615
|
+
this.linePrinter.appendText(text);
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
355
618
|
if (this.andBreak === 'before') {
|
|
356
619
|
// Skip newline when inside WITH clause with full-oneline style
|
|
357
620
|
if (!(this.insideWithClause && this.withClauseStyle === 'full-oneline')) {
|
|
@@ -370,20 +633,32 @@ class SqlPrinter {
|
|
|
370
633
|
this.linePrinter.appendText(text);
|
|
371
634
|
}
|
|
372
635
|
}
|
|
373
|
-
handleParenthesisToken(token, level, indentParentActive) {
|
|
636
|
+
handleParenthesisToken(token, level, indentParentActive, parentContainerType) {
|
|
374
637
|
if (token.text === '(') {
|
|
375
638
|
this.linePrinter.appendText(token.text);
|
|
376
|
-
if (
|
|
377
|
-
|
|
639
|
+
if ((parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.InsertClause ||
|
|
640
|
+
parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.MergeInsertAction) &&
|
|
641
|
+
this.insertColumnsOneLine) {
|
|
642
|
+
return;
|
|
643
|
+
}
|
|
644
|
+
if (!this.isOnelineMode()) {
|
|
645
|
+
if (this.shouldBreakAfterOpeningParen(parentContainerType)) {
|
|
646
|
+
this.linePrinter.appendNewline(level + 1);
|
|
647
|
+
}
|
|
648
|
+
else if (indentParentActive && !(this.insideWithClause && this.withClauseStyle === 'full-oneline')) {
|
|
378
649
|
this.linePrinter.appendNewline(level);
|
|
379
650
|
}
|
|
380
651
|
}
|
|
381
652
|
return;
|
|
382
653
|
}
|
|
383
|
-
if (token.text === ')' &&
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
654
|
+
if (token.text === ')' && !this.isOnelineMode()) {
|
|
655
|
+
if (this.shouldBreakBeforeClosingParen(parentContainerType)) {
|
|
656
|
+
this.linePrinter.appendNewline(Math.max(level, 0));
|
|
657
|
+
this.linePrinter.appendText(token.text);
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
if (indentParentActive && !(this.insideWithClause && this.withClauseStyle === 'full-oneline')) {
|
|
661
|
+
const closingLevel = Math.max(level - 1, 0);
|
|
387
662
|
this.linePrinter.appendNewline(closingLevel);
|
|
388
663
|
}
|
|
389
664
|
}
|
|
@@ -396,6 +671,11 @@ class SqlPrinter {
|
|
|
396
671
|
this.linePrinter.appendText(text);
|
|
397
672
|
return;
|
|
398
673
|
}
|
|
674
|
+
if (this.whenOneLine &&
|
|
675
|
+
(parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.MergeWhenClause || this.mergeWhenPredicateDepth > 0)) {
|
|
676
|
+
this.linePrinter.appendText(text);
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
399
679
|
if (this.orBreak === 'before') {
|
|
400
680
|
// Insert a newline before OR unless WITH full-oneline mode suppresses breaks.
|
|
401
681
|
if (!(this.insideWithClause && this.withClauseStyle === 'full-oneline')) {
|
|
@@ -445,37 +725,140 @@ class SqlPrinter {
|
|
|
445
725
|
}
|
|
446
726
|
handleJoinClauseToken(token, level) {
|
|
447
727
|
const text = this.applyKeywordCase(token.text);
|
|
448
|
-
// before join clause, add newline
|
|
449
|
-
if (
|
|
728
|
+
// before join clause, add newline when multiline formatting is allowed
|
|
729
|
+
if (this.onelineHelper.shouldInsertJoinNewline(this.insideWithClause)) {
|
|
450
730
|
this.linePrinter.appendNewline(level);
|
|
451
731
|
}
|
|
452
732
|
this.linePrinter.appendText(text);
|
|
453
733
|
}
|
|
734
|
+
/**
|
|
735
|
+
* Decides whether the current container should collapse into a single line.
|
|
736
|
+
*/
|
|
737
|
+
shouldFormatContainerAsOneline(token, shouldIndentNested) {
|
|
738
|
+
return this.onelineHelper.shouldFormatContainer(token, shouldIndentNested);
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* Detects an INSERT column list that must stay on a single line.
|
|
742
|
+
*/
|
|
743
|
+
isInsertClauseOneline(parentContainerType) {
|
|
744
|
+
return this.onelineHelper.isInsertClauseOneline(parentContainerType);
|
|
745
|
+
}
|
|
454
746
|
/**
|
|
455
747
|
* Handles space tokens with context-aware filtering.
|
|
456
748
|
* Skips spaces in CommentBlocks when in specific CTE modes to prevent duplication.
|
|
457
749
|
*/
|
|
458
|
-
handleSpaceToken(token, parentContainerType) {
|
|
750
|
+
handleSpaceToken(token, parentContainerType, nextToken, previousToken, priorToken) {
|
|
459
751
|
if (this.smartCommentBlockBuilder && this.smartCommentBlockBuilder.mode === 'line') {
|
|
460
752
|
this.flushSmartCommentBlockBuilder();
|
|
461
753
|
}
|
|
462
|
-
|
|
754
|
+
const currentLineText = this.linePrinter.getCurrentLine().text;
|
|
755
|
+
if (this.onelineHelper.shouldSkipInsertClauseSpace(parentContainerType, nextToken, currentLineText)) {
|
|
756
|
+
return;
|
|
757
|
+
}
|
|
758
|
+
if (this.onelineHelper.shouldSkipCommentBlockSpace(parentContainerType, this.insideWithClause)) {
|
|
463
759
|
const currentLine = this.linePrinter.getCurrentLine();
|
|
464
760
|
if (currentLine.text !== '' && !currentLine.text.endsWith(' ')) {
|
|
465
761
|
this.linePrinter.appendText(' ');
|
|
466
762
|
}
|
|
467
763
|
return;
|
|
468
764
|
}
|
|
765
|
+
// Skip redundant spaces before structural parentheses in CREATE TABLE DDL.
|
|
766
|
+
if (this.shouldSkipSpaceBeforeParenthesis(parentContainerType, nextToken, previousToken, priorToken)) {
|
|
767
|
+
return;
|
|
768
|
+
}
|
|
469
769
|
this.linePrinter.appendText(token.text);
|
|
470
770
|
}
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
this.
|
|
478
|
-
|
|
771
|
+
findPreviousSignificantToken(tokens, index) {
|
|
772
|
+
for (let i = index - 1; i >= 0; i--) {
|
|
773
|
+
const candidate = tokens[i];
|
|
774
|
+
if (candidate.type === SqlPrintToken_1.SqlPrintTokenType.space || candidate.type === SqlPrintToken_1.SqlPrintTokenType.commentNewline) {
|
|
775
|
+
continue;
|
|
776
|
+
}
|
|
777
|
+
if (candidate.type === SqlPrintToken_1.SqlPrintTokenType.comment && !this.rendersInlineComments()) {
|
|
778
|
+
continue;
|
|
779
|
+
}
|
|
780
|
+
return { token: candidate, index: i };
|
|
781
|
+
}
|
|
782
|
+
return undefined;
|
|
783
|
+
}
|
|
784
|
+
shouldSkipSpaceBeforeParenthesis(parentContainerType, nextToken, previousToken, priorToken) {
|
|
785
|
+
if (!nextToken || nextToken.type !== SqlPrintToken_1.SqlPrintTokenType.parenthesis || nextToken.text !== '(') {
|
|
786
|
+
return false;
|
|
787
|
+
}
|
|
788
|
+
if (!parentContainerType || !this.isCreateTableSpacingContext(parentContainerType)) {
|
|
789
|
+
return false;
|
|
790
|
+
}
|
|
791
|
+
if (!previousToken) {
|
|
792
|
+
return false;
|
|
793
|
+
}
|
|
794
|
+
if (this.isCreateTableNameToken(previousToken, parentContainerType)) {
|
|
795
|
+
return true;
|
|
796
|
+
}
|
|
797
|
+
if (this.isCreateTableConstraintKeyword(previousToken, parentContainerType)) {
|
|
798
|
+
return true;
|
|
799
|
+
}
|
|
800
|
+
if (priorToken && this.isCreateTableConstraintKeyword(priorToken, parentContainerType)) {
|
|
801
|
+
if (this.isIdentifierAttachedToConstraint(previousToken, priorToken, parentContainerType)) {
|
|
802
|
+
return true;
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
return false;
|
|
806
|
+
}
|
|
807
|
+
shouldAlignCreateTableSelect(containerType, parentContainerType) {
|
|
808
|
+
return containerType === SqlPrintToken_1.SqlPrintTokenContainerType.SimpleSelectQuery &&
|
|
809
|
+
parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.CreateTableQuery;
|
|
810
|
+
}
|
|
811
|
+
isCreateTableSpacingContext(parentContainerType) {
|
|
812
|
+
switch (parentContainerType) {
|
|
813
|
+
case SqlPrintToken_1.SqlPrintTokenContainerType.CreateTableQuery:
|
|
814
|
+
case SqlPrintToken_1.SqlPrintTokenContainerType.CreateTableDefinition:
|
|
815
|
+
case SqlPrintToken_1.SqlPrintTokenContainerType.TableConstraintDefinition:
|
|
816
|
+
case SqlPrintToken_1.SqlPrintTokenContainerType.ColumnConstraintDefinition:
|
|
817
|
+
case SqlPrintToken_1.SqlPrintTokenContainerType.ReferenceDefinition:
|
|
818
|
+
return true;
|
|
819
|
+
default:
|
|
820
|
+
return false;
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
isCreateTableNameToken(previousToken, parentContainerType) {
|
|
824
|
+
if (parentContainerType !== SqlPrintToken_1.SqlPrintTokenContainerType.CreateTableQuery) {
|
|
825
|
+
return false;
|
|
826
|
+
}
|
|
827
|
+
return previousToken.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.QualifiedName;
|
|
828
|
+
}
|
|
829
|
+
isCreateTableConstraintKeyword(token, parentContainerType) {
|
|
830
|
+
if (token.type !== SqlPrintToken_1.SqlPrintTokenType.keyword) {
|
|
831
|
+
return false;
|
|
832
|
+
}
|
|
833
|
+
const text = token.text.toLowerCase();
|
|
834
|
+
if (parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.ReferenceDefinition) {
|
|
835
|
+
return CREATE_TABLE_PAREN_KEYWORDS_WITH_IDENTIFIER.has(text);
|
|
836
|
+
}
|
|
837
|
+
if (CREATE_TABLE_SINGLE_PAREN_KEYWORDS.has(text)) {
|
|
838
|
+
return true;
|
|
839
|
+
}
|
|
840
|
+
if (CREATE_TABLE_MULTI_PAREN_KEYWORDS.has(text)) {
|
|
841
|
+
return true;
|
|
842
|
+
}
|
|
843
|
+
return false;
|
|
844
|
+
}
|
|
845
|
+
isIdentifierAttachedToConstraint(token, keywordToken, parentContainerType) {
|
|
846
|
+
if (!token) {
|
|
847
|
+
return false;
|
|
848
|
+
}
|
|
849
|
+
if (parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.ReferenceDefinition) {
|
|
850
|
+
return token.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.QualifiedName &&
|
|
851
|
+
CREATE_TABLE_PAREN_KEYWORDS_WITH_IDENTIFIER.has(keywordToken.text.toLowerCase());
|
|
852
|
+
}
|
|
853
|
+
if (parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.TableConstraintDefinition ||
|
|
854
|
+
parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.ColumnConstraintDefinition) {
|
|
855
|
+
const normalized = keywordToken.text.toLowerCase();
|
|
856
|
+
if (CREATE_TABLE_SINGLE_PAREN_KEYWORDS.has(normalized) ||
|
|
857
|
+
CREATE_TABLE_MULTI_PAREN_KEYWORDS.has(normalized)) {
|
|
858
|
+
return token.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.IdentifierString;
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
return false;
|
|
479
862
|
}
|
|
480
863
|
printCommentToken(text, level, parentContainerType) {
|
|
481
864
|
const trimmed = text.trim();
|
|
@@ -498,11 +881,12 @@ class SqlPrinter {
|
|
|
498
881
|
const lineText = content ? `-- ${content}` : '--';
|
|
499
882
|
if (parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.CommentBlock) {
|
|
500
883
|
this.linePrinter.appendText(lineText);
|
|
501
|
-
this.pendingLineCommentBreak = level;
|
|
884
|
+
this.pendingLineCommentBreak = this.resolveCommentIndentLevel(level, parentContainerType);
|
|
502
885
|
}
|
|
503
886
|
else {
|
|
504
887
|
this.linePrinter.appendText(lineText);
|
|
505
|
-
this.
|
|
888
|
+
const effectiveLevel = this.resolveCommentIndentLevel(level, parentContainerType);
|
|
889
|
+
this.linePrinter.appendNewline(effectiveLevel);
|
|
506
890
|
}
|
|
507
891
|
}
|
|
508
892
|
}
|
|
@@ -523,10 +907,11 @@ class SqlPrinter {
|
|
|
523
907
|
}
|
|
524
908
|
if (trimmed.startsWith('--')) {
|
|
525
909
|
if (parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.CommentBlock) {
|
|
526
|
-
this.pendingLineCommentBreak = level;
|
|
910
|
+
this.pendingLineCommentBreak = this.resolveCommentIndentLevel(level, parentContainerType);
|
|
527
911
|
}
|
|
528
912
|
else {
|
|
529
|
-
this.
|
|
913
|
+
const effectiveLevel = this.resolveCommentIndentLevel(level, parentContainerType);
|
|
914
|
+
this.linePrinter.appendNewline(effectiveLevel);
|
|
530
915
|
}
|
|
531
916
|
}
|
|
532
917
|
}
|
|
@@ -569,11 +954,8 @@ class SqlPrinter {
|
|
|
569
954
|
this.flushSmartCommentBlockBuilder();
|
|
570
955
|
return false;
|
|
571
956
|
}
|
|
572
|
-
handleCommentBlockContainer(token, level) {
|
|
957
|
+
handleCommentBlockContainer(token, level, context) {
|
|
573
958
|
var _a;
|
|
574
|
-
if (!this.exportComment) {
|
|
575
|
-
return;
|
|
576
|
-
}
|
|
577
959
|
if (this.commentStyle !== 'smart') {
|
|
578
960
|
const rawLines = this.extractRawCommentBlockLines(token);
|
|
579
961
|
if (rawLines.length > 0) {
|
|
@@ -583,7 +965,13 @@ class SqlPrinter {
|
|
|
583
965
|
return;
|
|
584
966
|
}
|
|
585
967
|
for (const child of token.innerTokens) {
|
|
586
|
-
|
|
968
|
+
// Force inner comment tokens to render once the block is approved.
|
|
969
|
+
const childContext = {
|
|
970
|
+
position: context.position,
|
|
971
|
+
isTopLevelContainer: context.isTopLevelContainer,
|
|
972
|
+
forceRender: true,
|
|
973
|
+
};
|
|
974
|
+
this.appendToken(child, level, token.containerType, 0, false, childContext);
|
|
587
975
|
}
|
|
588
976
|
return;
|
|
589
977
|
}
|
|
@@ -645,7 +1033,8 @@ class SqlPrinter {
|
|
|
645
1033
|
}
|
|
646
1034
|
const { lines, level, mode } = this.smartCommentBlockBuilder;
|
|
647
1035
|
if (mode === 'line') {
|
|
648
|
-
|
|
1036
|
+
const meaningfulLineCount = lines.filter(line => line.trim() !== '').length;
|
|
1037
|
+
if (meaningfulLineCount > 1) {
|
|
649
1038
|
const blockText = this.buildBlockComment(lines, level);
|
|
650
1039
|
this.linePrinter.appendText(blockText);
|
|
651
1040
|
}
|
|
@@ -808,6 +1197,19 @@ class SqlPrinter {
|
|
|
808
1197
|
.replace(/\/\*/g, '\\/\\*')
|
|
809
1198
|
.replace(/\*\//g, '*\\/');
|
|
810
1199
|
}
|
|
1200
|
+
getCommentBaseIndentLevel(level, parentContainerType) {
|
|
1201
|
+
if (!parentContainerType) {
|
|
1202
|
+
return level;
|
|
1203
|
+
}
|
|
1204
|
+
const clauseAlignedLevel = this.getClauseBreakIndentLevel(parentContainerType, level);
|
|
1205
|
+
return Math.max(level, clauseAlignedLevel);
|
|
1206
|
+
}
|
|
1207
|
+
resolveCommentIndentLevel(level, parentContainerType) {
|
|
1208
|
+
var _a;
|
|
1209
|
+
const baseLevel = this.getCommentBaseIndentLevel(level, parentContainerType);
|
|
1210
|
+
const currentLevel = (_a = this.linePrinter.getCurrentLine().level) !== null && _a !== void 0 ? _a : baseLevel;
|
|
1211
|
+
return Math.max(baseLevel, currentLevel);
|
|
1212
|
+
}
|
|
811
1213
|
/**
|
|
812
1214
|
* Handles commentNewline tokens with conditional newline behavior.
|
|
813
1215
|
* In multiline mode (newline !== ' '), adds a newline after comments.
|
|
@@ -838,6 +1240,43 @@ class SqlPrinter {
|
|
|
838
1240
|
return (this.insideWithClause && this.withClauseStyle === 'full-oneline') ||
|
|
839
1241
|
this.withClauseStyle === 'cte-oneline';
|
|
840
1242
|
}
|
|
1243
|
+
shouldAddNewlineBeforeLeadingComments(parentType) {
|
|
1244
|
+
if (!parentType) {
|
|
1245
|
+
return false;
|
|
1246
|
+
}
|
|
1247
|
+
if (parentType === SqlPrintToken_1.SqlPrintTokenContainerType.TupleExpression) {
|
|
1248
|
+
return true;
|
|
1249
|
+
}
|
|
1250
|
+
if (parentType === SqlPrintToken_1.SqlPrintTokenContainerType.InsertClause ||
|
|
1251
|
+
parentType === SqlPrintToken_1.SqlPrintTokenContainerType.MergeInsertAction) {
|
|
1252
|
+
return !this.insertColumnsOneLine;
|
|
1253
|
+
}
|
|
1254
|
+
if (parentType === SqlPrintToken_1.SqlPrintTokenContainerType.SetClause) {
|
|
1255
|
+
return true;
|
|
1256
|
+
}
|
|
1257
|
+
if (parentType === SqlPrintToken_1.SqlPrintTokenContainerType.SelectClause) {
|
|
1258
|
+
return true;
|
|
1259
|
+
}
|
|
1260
|
+
if (parentType === SqlPrintToken_1.SqlPrintTokenContainerType.ExplainStatement) {
|
|
1261
|
+
// Ensure EXPLAIN targets print header comments on a dedicated line.
|
|
1262
|
+
return true;
|
|
1263
|
+
}
|
|
1264
|
+
return false;
|
|
1265
|
+
}
|
|
1266
|
+
getLeadingCommentIndentLevel(parentType, currentLevel) {
|
|
1267
|
+
if (parentType === SqlPrintToken_1.SqlPrintTokenContainerType.TupleExpression) {
|
|
1268
|
+
return currentLevel + 1;
|
|
1269
|
+
}
|
|
1270
|
+
if (parentType === SqlPrintToken_1.SqlPrintTokenContainerType.InsertClause ||
|
|
1271
|
+
parentType === SqlPrintToken_1.SqlPrintTokenContainerType.MergeInsertAction ||
|
|
1272
|
+
parentType === SqlPrintToken_1.SqlPrintTokenContainerType.SelectClause) {
|
|
1273
|
+
return currentLevel + 1;
|
|
1274
|
+
}
|
|
1275
|
+
if (parentType === SqlPrintToken_1.SqlPrintTokenContainerType.SetClause) {
|
|
1276
|
+
return currentLevel + 1;
|
|
1277
|
+
}
|
|
1278
|
+
return currentLevel;
|
|
1279
|
+
}
|
|
841
1280
|
/**
|
|
842
1281
|
* Determines if the printer is in oneliner mode.
|
|
843
1282
|
* Oneliner mode uses single spaces instead of actual newlines.
|
|
@@ -870,9 +1309,10 @@ class SqlPrinter {
|
|
|
870
1309
|
andBreak: this.andBreak,
|
|
871
1310
|
orBreak: this.orBreak,
|
|
872
1311
|
keywordCase: this.keywordCase,
|
|
873
|
-
exportComment:
|
|
1312
|
+
exportComment: 'none',
|
|
874
1313
|
withClauseStyle: 'standard', // Prevent recursive processing
|
|
875
1314
|
indentNestedParentheses: false,
|
|
1315
|
+
insertColumnsOneLine: this.insertColumnsOneLine,
|
|
876
1316
|
});
|
|
877
1317
|
}
|
|
878
1318
|
/**
|
|
@@ -885,6 +1325,117 @@ class SqlPrinter {
|
|
|
885
1325
|
const cleanedResult = this.cleanDuplicateSpaces(onelineResult);
|
|
886
1326
|
this.linePrinter.appendText(cleanedResult);
|
|
887
1327
|
}
|
|
1328
|
+
getClauseBreakIndentLevel(parentType, level) {
|
|
1329
|
+
if (!parentType) {
|
|
1330
|
+
return level;
|
|
1331
|
+
}
|
|
1332
|
+
switch (parentType) {
|
|
1333
|
+
case SqlPrintToken_1.SqlPrintTokenContainerType.MergeWhenClause:
|
|
1334
|
+
// Actions under WHEN clauses should be indented one level deeper than the WHEN line.
|
|
1335
|
+
return level + 1;
|
|
1336
|
+
case SqlPrintToken_1.SqlPrintTokenContainerType.MergeUpdateAction:
|
|
1337
|
+
case SqlPrintToken_1.SqlPrintTokenContainerType.MergeDeleteAction:
|
|
1338
|
+
case SqlPrintToken_1.SqlPrintTokenContainerType.MergeInsertAction:
|
|
1339
|
+
// Keep MERGE actions and their follow-up keywords (e.g., VALUES, WHERE) aligned with the action keyword.
|
|
1340
|
+
return level + 1;
|
|
1341
|
+
default:
|
|
1342
|
+
return level;
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
isMergeActionContainer(token) {
|
|
1346
|
+
if (!token) {
|
|
1347
|
+
return false;
|
|
1348
|
+
}
|
|
1349
|
+
switch (token.containerType) {
|
|
1350
|
+
case SqlPrintToken_1.SqlPrintTokenContainerType.MergeUpdateAction:
|
|
1351
|
+
case SqlPrintToken_1.SqlPrintTokenContainerType.MergeDeleteAction:
|
|
1352
|
+
case SqlPrintToken_1.SqlPrintTokenContainerType.MergeInsertAction:
|
|
1353
|
+
case SqlPrintToken_1.SqlPrintTokenContainerType.MergeDoNothingAction:
|
|
1354
|
+
return true;
|
|
1355
|
+
default:
|
|
1356
|
+
return false;
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
shouldBreakAfterOpeningParen(parentType) {
|
|
1360
|
+
if (!parentType) {
|
|
1361
|
+
return false;
|
|
1362
|
+
}
|
|
1363
|
+
if (parentType === SqlPrintToken_1.SqlPrintTokenContainerType.InsertClause ||
|
|
1364
|
+
parentType === SqlPrintToken_1.SqlPrintTokenContainerType.MergeInsertAction) {
|
|
1365
|
+
return !this.isInsertClauseOneline(parentType);
|
|
1366
|
+
}
|
|
1367
|
+
return false;
|
|
1368
|
+
}
|
|
1369
|
+
shouldBreakBeforeClosingParen(parentType) {
|
|
1370
|
+
if (!parentType) {
|
|
1371
|
+
return false;
|
|
1372
|
+
}
|
|
1373
|
+
if (parentType === SqlPrintToken_1.SqlPrintTokenContainerType.InsertClause ||
|
|
1374
|
+
parentType === SqlPrintToken_1.SqlPrintTokenContainerType.MergeInsertAction) {
|
|
1375
|
+
return !this.isInsertClauseOneline(parentType);
|
|
1376
|
+
}
|
|
1377
|
+
return false;
|
|
1378
|
+
}
|
|
1379
|
+
shouldConvertSpaceToClauseBreak(parentType, nextToken) {
|
|
1380
|
+
if (!parentType || !nextToken) {
|
|
1381
|
+
return false;
|
|
1382
|
+
}
|
|
1383
|
+
const nextKeyword = nextToken.type === SqlPrintToken_1.SqlPrintTokenType.keyword ? nextToken.text.toLowerCase() : null;
|
|
1384
|
+
const nextContainer = nextToken.containerType;
|
|
1385
|
+
if (parentType === SqlPrintToken_1.SqlPrintTokenContainerType.MergeQuery) {
|
|
1386
|
+
// Break before USING blocks and before each WHEN clause to mirror statement structure.
|
|
1387
|
+
if (nextKeyword === 'using') {
|
|
1388
|
+
return true;
|
|
1389
|
+
}
|
|
1390
|
+
if (nextContainer === SqlPrintToken_1.SqlPrintTokenContainerType.MergeWhenClause) {
|
|
1391
|
+
return true;
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
if (parentType === SqlPrintToken_1.SqlPrintTokenContainerType.MergeWhenClause) {
|
|
1395
|
+
// Force the action to start on the next line with additional indentation.
|
|
1396
|
+
if (nextContainer === SqlPrintToken_1.SqlPrintTokenContainerType.MergeUpdateAction ||
|
|
1397
|
+
nextContainer === SqlPrintToken_1.SqlPrintTokenContainerType.MergeDeleteAction ||
|
|
1398
|
+
nextContainer === SqlPrintToken_1.SqlPrintTokenContainerType.MergeInsertAction ||
|
|
1399
|
+
nextContainer === SqlPrintToken_1.SqlPrintTokenContainerType.MergeDoNothingAction) {
|
|
1400
|
+
return true;
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
if (parentType === SqlPrintToken_1.SqlPrintTokenContainerType.UpdateQuery) {
|
|
1404
|
+
if (nextKeyword === 'set' || nextKeyword === 'from' || nextKeyword === 'where' || nextKeyword === 'returning') {
|
|
1405
|
+
return true;
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
if (parentType === SqlPrintToken_1.SqlPrintTokenContainerType.InsertQuery) {
|
|
1409
|
+
if (nextKeyword === 'returning') {
|
|
1410
|
+
return true;
|
|
1411
|
+
}
|
|
1412
|
+
if (nextKeyword && (nextKeyword.startsWith('select') || nextKeyword.startsWith('values'))) {
|
|
1413
|
+
return true;
|
|
1414
|
+
}
|
|
1415
|
+
if (nextContainer === SqlPrintToken_1.SqlPrintTokenContainerType.ValuesQuery || nextContainer === SqlPrintToken_1.SqlPrintTokenContainerType.SimpleSelectQuery) {
|
|
1416
|
+
return true;
|
|
1417
|
+
}
|
|
1418
|
+
if (nextContainer === SqlPrintToken_1.SqlPrintTokenContainerType.InsertClause) {
|
|
1419
|
+
return true;
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
if (parentType === SqlPrintToken_1.SqlPrintTokenContainerType.DeleteQuery) {
|
|
1423
|
+
if (nextKeyword === 'using' || nextKeyword === 'where' || nextKeyword === 'returning') {
|
|
1424
|
+
return true;
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
if (parentType === SqlPrintToken_1.SqlPrintTokenContainerType.MergeUpdateAction || parentType === SqlPrintToken_1.SqlPrintTokenContainerType.MergeDeleteAction) {
|
|
1428
|
+
if (nextKeyword === 'where') {
|
|
1429
|
+
return true;
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
if (parentType === SqlPrintToken_1.SqlPrintTokenContainerType.MergeInsertAction) {
|
|
1433
|
+
if (nextKeyword && (nextKeyword.startsWith('values') || nextKeyword === 'default values')) {
|
|
1434
|
+
return true;
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
return false;
|
|
1438
|
+
}
|
|
888
1439
|
/**
|
|
889
1440
|
* Creates a unified SqlPrinter instance configured for oneline formatting.
|
|
890
1441
|
* Works for all oneline options: parentheses, BETWEEN, VALUES, JOIN, CASE, subqueries.
|
|
@@ -901,7 +1452,8 @@ class SqlPrinter {
|
|
|
901
1452
|
andBreak: 'none', // Disable AND-based line breaks
|
|
902
1453
|
orBreak: 'none', // Disable OR-based line breaks
|
|
903
1454
|
keywordCase: this.keywordCase,
|
|
904
|
-
exportComment: this.
|
|
1455
|
+
exportComment: this.commentExportMode,
|
|
1456
|
+
commentStyle: this.commentStyle,
|
|
905
1457
|
withClauseStyle: 'standard',
|
|
906
1458
|
parenthesesOneLine: false, // Prevent recursive processing (avoid infinite loops)
|
|
907
1459
|
betweenOneLine: false, // Prevent recursive processing (avoid infinite loops)
|
|
@@ -910,6 +1462,7 @@ class SqlPrinter {
|
|
|
910
1462
|
caseOneLine: false, // Prevent recursive processing (avoid infinite loops)
|
|
911
1463
|
subqueryOneLine: false, // Prevent recursive processing (avoid infinite loops)
|
|
912
1464
|
indentNestedParentheses: false,
|
|
1465
|
+
insertColumnsOneLine: this.insertColumnsOneLine,
|
|
913
1466
|
});
|
|
914
1467
|
}
|
|
915
1468
|
/**
|