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.
Files changed (294) hide show
  1. package/README.md +12 -12
  2. package/dist/esm/index.js +18 -0
  3. package/dist/esm/index.js.map +1 -1
  4. package/dist/esm/index.min.js +15 -19
  5. package/dist/esm/index.min.js.map +4 -4
  6. package/dist/esm/src/index.d.ts +18 -0
  7. package/dist/esm/src/index.js +18 -0
  8. package/dist/esm/src/index.js.map +1 -1
  9. package/dist/esm/src/models/BinarySelectQuery.d.ts +25 -1
  10. package/dist/esm/src/models/BinarySelectQuery.js +28 -0
  11. package/dist/esm/src/models/BinarySelectQuery.js.map +1 -1
  12. package/dist/esm/src/models/Clause.d.ts +14 -2
  13. package/dist/esm/src/models/Clause.js +26 -1
  14. package/dist/esm/src/models/Clause.js.map +1 -1
  15. package/dist/esm/src/models/CreateTableQuery.d.ts +99 -5
  16. package/dist/esm/src/models/CreateTableQuery.js +85 -10
  17. package/dist/esm/src/models/CreateTableQuery.js.map +1 -1
  18. package/dist/esm/src/models/DDLStatements.d.ts +195 -0
  19. package/dist/esm/src/models/DDLStatements.js +201 -0
  20. package/dist/esm/src/models/DDLStatements.js.map +1 -0
  21. package/dist/esm/src/models/DeleteQuery.d.ts +17 -0
  22. package/dist/esm/src/models/DeleteQuery.js +16 -0
  23. package/dist/esm/src/models/DeleteQuery.js.map +1 -0
  24. package/dist/esm/src/models/InsertQuery.d.ts +7 -1
  25. package/dist/esm/src/models/InsertQuery.js +6 -2
  26. package/dist/esm/src/models/InsertQuery.js.map +1 -1
  27. package/dist/esm/src/models/MergeQuery.d.ts +63 -0
  28. package/dist/esm/src/models/MergeQuery.js +94 -0
  29. package/dist/esm/src/models/MergeQuery.js.map +1 -0
  30. package/dist/esm/src/models/SelectQuery.d.ts +37 -1
  31. package/dist/esm/src/models/SelectQuery.js +4 -1
  32. package/dist/esm/src/models/SelectQuery.js.map +1 -1
  33. package/dist/esm/src/models/SimpleSelectQuery.d.ts +29 -1
  34. package/dist/esm/src/models/SimpleSelectQuery.js +32 -0
  35. package/dist/esm/src/models/SimpleSelectQuery.js.map +1 -1
  36. package/dist/esm/src/models/SqlComponent.d.ts +2 -1
  37. package/dist/esm/src/models/SqlComponent.js +1 -1
  38. package/dist/esm/src/models/SqlComponent.js.map +1 -1
  39. package/dist/esm/src/models/SqlPrintToken.d.ts +36 -0
  40. package/dist/esm/src/models/SqlPrintToken.js +35 -0
  41. package/dist/esm/src/models/SqlPrintToken.js.map +1 -1
  42. package/dist/esm/src/models/ValuesQuery.d.ts +25 -1
  43. package/dist/esm/src/models/ValuesQuery.js +28 -0
  44. package/dist/esm/src/models/ValuesQuery.js.map +1 -1
  45. package/dist/esm/src/parsers/AlterTableParser.d.ts +25 -0
  46. package/dist/esm/src/parsers/AlterTableParser.js +428 -0
  47. package/dist/esm/src/parsers/AlterTableParser.js.map +1 -0
  48. package/dist/esm/src/parsers/AnalyzeStatementParser.d.ts +13 -0
  49. package/dist/esm/src/parsers/AnalyzeStatementParser.js +90 -0
  50. package/dist/esm/src/parsers/AnalyzeStatementParser.js.map +1 -0
  51. package/dist/esm/src/parsers/CreateIndexParser.d.ts +16 -0
  52. package/dist/esm/src/parsers/CreateIndexParser.js +237 -0
  53. package/dist/esm/src/parsers/CreateIndexParser.js.map +1 -0
  54. package/dist/esm/src/parsers/CreateTableParser.d.ts +41 -0
  55. package/dist/esm/src/parsers/CreateTableParser.js +734 -0
  56. package/dist/esm/src/parsers/CreateTableParser.js.map +1 -0
  57. package/dist/esm/src/parsers/DeleteClauseParser.d.ts +11 -0
  58. package/dist/esm/src/parsers/DeleteClauseParser.js +33 -0
  59. package/dist/esm/src/parsers/DeleteClauseParser.js.map +1 -0
  60. package/dist/esm/src/parsers/DeleteQueryParser.d.ts +16 -0
  61. package/dist/esm/src/parsers/DeleteQueryParser.js +73 -0
  62. package/dist/esm/src/parsers/DeleteQueryParser.js.map +1 -0
  63. package/dist/esm/src/parsers/DropConstraintParser.d.ts +12 -0
  64. package/dist/esm/src/parsers/DropConstraintParser.js +47 -0
  65. package/dist/esm/src/parsers/DropConstraintParser.js.map +1 -0
  66. package/dist/esm/src/parsers/DropIndexParser.d.ts +12 -0
  67. package/dist/esm/src/parsers/DropIndexParser.js +69 -0
  68. package/dist/esm/src/parsers/DropIndexParser.js.map +1 -0
  69. package/dist/esm/src/parsers/DropTableParser.d.ts +12 -0
  70. package/dist/esm/src/parsers/DropTableParser.js +59 -0
  71. package/dist/esm/src/parsers/DropTableParser.js.map +1 -0
  72. package/dist/esm/src/parsers/ExplainStatementParser.d.ts +23 -0
  73. package/dist/esm/src/parsers/ExplainStatementParser.js +185 -0
  74. package/dist/esm/src/parsers/ExplainStatementParser.js.map +1 -0
  75. package/dist/esm/src/parsers/FunctionExpressionParser.d.ts +4 -0
  76. package/dist/esm/src/parsers/FunctionExpressionParser.js +25 -8
  77. package/dist/esm/src/parsers/FunctionExpressionParser.js.map +1 -1
  78. package/dist/esm/src/parsers/InsertQueryParser.js +103 -31
  79. package/dist/esm/src/parsers/InsertQueryParser.js.map +1 -1
  80. package/dist/esm/src/parsers/MergeQueryParser.d.ts +26 -0
  81. package/dist/esm/src/parsers/MergeQueryParser.js +479 -0
  82. package/dist/esm/src/parsers/MergeQueryParser.js.map +1 -0
  83. package/dist/esm/src/parsers/ParenExpressionParser.js +25 -0
  84. package/dist/esm/src/parsers/ParenExpressionParser.js.map +1 -1
  85. package/dist/esm/src/parsers/ReturningClauseParser.js +50 -7
  86. package/dist/esm/src/parsers/ReturningClauseParser.js.map +1 -1
  87. package/dist/esm/src/parsers/SelectClauseParser.js +3 -3
  88. package/dist/esm/src/parsers/SelectClauseParser.js.map +1 -1
  89. package/dist/esm/src/parsers/SelectQueryParser.d.ts +4 -0
  90. package/dist/esm/src/parsers/SelectQueryParser.js +4 -0
  91. package/dist/esm/src/parsers/SelectQueryParser.js.map +1 -1
  92. package/dist/esm/src/parsers/SetClauseParser.js +97 -15
  93. package/dist/esm/src/parsers/SetClauseParser.js.map +1 -1
  94. package/dist/esm/src/parsers/SqlParser.d.ts +40 -0
  95. package/dist/esm/src/parsers/SqlParser.js +400 -0
  96. package/dist/esm/src/parsers/SqlParser.js.map +1 -0
  97. package/dist/esm/src/parsers/SqlPrintTokenParser.d.ts +65 -3
  98. package/dist/esm/src/parsers/SqlPrintTokenParser.js +1145 -38
  99. package/dist/esm/src/parsers/SqlPrintTokenParser.js.map +1 -1
  100. package/dist/esm/src/parsers/SqlTokenizer.d.ts +24 -2
  101. package/dist/esm/src/parsers/SqlTokenizer.js +135 -74
  102. package/dist/esm/src/parsers/SqlTokenizer.js.map +1 -1
  103. package/dist/esm/src/parsers/UpdateQueryParser.js +11 -1
  104. package/dist/esm/src/parsers/UpdateQueryParser.js.map +1 -1
  105. package/dist/esm/src/parsers/UsingClauseParser.d.ts +11 -0
  106. package/dist/esm/src/parsers/UsingClauseParser.js +29 -0
  107. package/dist/esm/src/parsers/UsingClauseParser.js.map +1 -0
  108. package/dist/esm/src/parsers/ValueParser.js +5 -1
  109. package/dist/esm/src/parsers/ValueParser.js.map +1 -1
  110. package/dist/esm/src/parsers/ValuesQueryParser.d.ts +0 -2
  111. package/dist/esm/src/parsers/ValuesQueryParser.js +5 -45
  112. package/dist/esm/src/parsers/ValuesQueryParser.js.map +1 -1
  113. package/dist/esm/src/parsers/utils/LexemeCommentUtils.d.ts +6 -0
  114. package/dist/esm/src/parsers/utils/LexemeCommentUtils.js +26 -0
  115. package/dist/esm/src/parsers/utils/LexemeCommentUtils.js.map +1 -0
  116. package/dist/esm/src/tokenReaders/CommandTokenReader.js +50 -2
  117. package/dist/esm/src/tokenReaders/CommandTokenReader.js.map +1 -1
  118. package/dist/esm/src/tokenReaders/LiteralTokenReader.js +8 -5
  119. package/dist/esm/src/tokenReaders/LiteralTokenReader.js.map +1 -1
  120. package/dist/esm/src/tokenReaders/OperatorTokenReader.js +10 -1
  121. package/dist/esm/src/tokenReaders/OperatorTokenReader.js.map +1 -1
  122. package/dist/esm/src/tokenReaders/TypeTokenReader.js +11 -1
  123. package/dist/esm/src/tokenReaders/TypeTokenReader.js.map +1 -1
  124. package/dist/esm/src/transformers/InsertQuerySelectValuesConverter.d.ts +18 -0
  125. package/dist/esm/src/transformers/InsertQuerySelectValuesConverter.js +118 -0
  126. package/dist/esm/src/transformers/InsertQuerySelectValuesConverter.js.map +1 -0
  127. package/dist/esm/src/transformers/LinePrinter.d.ts +1 -0
  128. package/dist/esm/src/transformers/LinePrinter.js +12 -0
  129. package/dist/esm/src/transformers/LinePrinter.js.map +1 -1
  130. package/dist/esm/src/transformers/OnelineFormattingHelper.d.ts +29 -0
  131. package/dist/esm/src/transformers/OnelineFormattingHelper.js +95 -0
  132. package/dist/esm/src/transformers/OnelineFormattingHelper.js.map +1 -0
  133. package/dist/esm/src/transformers/QueryBuilder.d.ts +47 -13
  134. package/dist/esm/src/transformers/QueryBuilder.js +424 -62
  135. package/dist/esm/src/transformers/QueryBuilder.js.map +1 -1
  136. package/dist/esm/src/transformers/SqlFormatter.d.ts +13 -1
  137. package/dist/esm/src/transformers/SqlFormatter.js +13 -4
  138. package/dist/esm/src/transformers/SqlFormatter.js.map +1 -1
  139. package/dist/esm/src/transformers/SqlPrinter.d.ts +47 -8
  140. package/dist/esm/src/transformers/SqlPrinter.js +625 -72
  141. package/dist/esm/src/transformers/SqlPrinter.js.map +1 -1
  142. package/dist/esm/src/types/Formatting.d.ts +8 -0
  143. package/dist/esm/src/types/Formatting.js +2 -0
  144. package/dist/esm/src/types/Formatting.js.map +1 -0
  145. package/dist/esm/src/utils/ParserStringUtils.d.ts +6 -0
  146. package/dist/esm/src/utils/ParserStringUtils.js +28 -0
  147. package/dist/esm/src/utils/ParserStringUtils.js.map +1 -0
  148. package/dist/esm/tsconfig.browser.tsbuildinfo +1 -1
  149. package/dist/index.min.js +14 -18
  150. package/dist/index.min.js.map +4 -4
  151. package/dist/src/index.d.ts +18 -0
  152. package/dist/src/index.js +18 -0
  153. package/dist/src/index.js.map +1 -1
  154. package/dist/src/models/BinarySelectQuery.d.ts +25 -1
  155. package/dist/src/models/BinarySelectQuery.js +28 -0
  156. package/dist/src/models/BinarySelectQuery.js.map +1 -1
  157. package/dist/src/models/Clause.d.ts +14 -2
  158. package/dist/src/models/Clause.js +29 -2
  159. package/dist/src/models/Clause.js.map +1 -1
  160. package/dist/src/models/CreateTableQuery.d.ts +99 -5
  161. package/dist/src/models/CreateTableQuery.js +90 -11
  162. package/dist/src/models/CreateTableQuery.js.map +1 -1
  163. package/dist/src/models/DDLStatements.d.ts +195 -0
  164. package/dist/src/models/DDLStatements.js +216 -0
  165. package/dist/src/models/DDLStatements.js.map +1 -0
  166. package/dist/src/models/DeleteQuery.d.ts +17 -0
  167. package/dist/src/models/DeleteQuery.js +20 -0
  168. package/dist/src/models/DeleteQuery.js.map +1 -0
  169. package/dist/src/models/InsertQuery.d.ts +7 -1
  170. package/dist/src/models/InsertQuery.js +6 -2
  171. package/dist/src/models/InsertQuery.js.map +1 -1
  172. package/dist/src/models/MergeQuery.d.ts +63 -0
  173. package/dist/src/models/MergeQuery.js +104 -0
  174. package/dist/src/models/MergeQuery.js.map +1 -0
  175. package/dist/src/models/SelectQuery.d.ts +37 -1
  176. package/dist/src/models/SelectQuery.js +7 -1
  177. package/dist/src/models/SelectQuery.js.map +1 -1
  178. package/dist/src/models/SimpleSelectQuery.d.ts +29 -1
  179. package/dist/src/models/SimpleSelectQuery.js +32 -0
  180. package/dist/src/models/SimpleSelectQuery.js.map +1 -1
  181. package/dist/src/models/SqlComponent.d.ts +2 -1
  182. package/dist/src/models/SqlComponent.js +1 -1
  183. package/dist/src/models/SqlComponent.js.map +1 -1
  184. package/dist/src/models/SqlPrintToken.d.ts +36 -0
  185. package/dist/src/models/SqlPrintToken.js +35 -0
  186. package/dist/src/models/SqlPrintToken.js.map +1 -1
  187. package/dist/src/models/ValuesQuery.d.ts +25 -1
  188. package/dist/src/models/ValuesQuery.js +28 -0
  189. package/dist/src/models/ValuesQuery.js.map +1 -1
  190. package/dist/src/parsers/AlterTableParser.d.ts +25 -0
  191. package/dist/src/parsers/AlterTableParser.js +432 -0
  192. package/dist/src/parsers/AlterTableParser.js.map +1 -0
  193. package/dist/src/parsers/AnalyzeStatementParser.d.ts +13 -0
  194. package/dist/src/parsers/AnalyzeStatementParser.js +94 -0
  195. package/dist/src/parsers/AnalyzeStatementParser.js.map +1 -0
  196. package/dist/src/parsers/CreateIndexParser.d.ts +16 -0
  197. package/dist/src/parsers/CreateIndexParser.js +241 -0
  198. package/dist/src/parsers/CreateIndexParser.js.map +1 -0
  199. package/dist/src/parsers/CreateTableParser.d.ts +41 -0
  200. package/dist/src/parsers/CreateTableParser.js +738 -0
  201. package/dist/src/parsers/CreateTableParser.js.map +1 -0
  202. package/dist/src/parsers/DeleteClauseParser.d.ts +11 -0
  203. package/dist/src/parsers/DeleteClauseParser.js +37 -0
  204. package/dist/src/parsers/DeleteClauseParser.js.map +1 -0
  205. package/dist/src/parsers/DeleteQueryParser.d.ts +16 -0
  206. package/dist/src/parsers/DeleteQueryParser.js +77 -0
  207. package/dist/src/parsers/DeleteQueryParser.js.map +1 -0
  208. package/dist/src/parsers/DropConstraintParser.d.ts +12 -0
  209. package/dist/src/parsers/DropConstraintParser.js +51 -0
  210. package/dist/src/parsers/DropConstraintParser.js.map +1 -0
  211. package/dist/src/parsers/DropIndexParser.d.ts +12 -0
  212. package/dist/src/parsers/DropIndexParser.js +73 -0
  213. package/dist/src/parsers/DropIndexParser.js.map +1 -0
  214. package/dist/src/parsers/DropTableParser.d.ts +12 -0
  215. package/dist/src/parsers/DropTableParser.js +63 -0
  216. package/dist/src/parsers/DropTableParser.js.map +1 -0
  217. package/dist/src/parsers/ExplainStatementParser.d.ts +23 -0
  218. package/dist/src/parsers/ExplainStatementParser.js +189 -0
  219. package/dist/src/parsers/ExplainStatementParser.js.map +1 -0
  220. package/dist/src/parsers/FunctionExpressionParser.d.ts +4 -0
  221. package/dist/src/parsers/FunctionExpressionParser.js +25 -8
  222. package/dist/src/parsers/FunctionExpressionParser.js.map +1 -1
  223. package/dist/src/parsers/InsertQueryParser.js +103 -31
  224. package/dist/src/parsers/InsertQueryParser.js.map +1 -1
  225. package/dist/src/parsers/MergeQueryParser.d.ts +26 -0
  226. package/dist/src/parsers/MergeQueryParser.js +483 -0
  227. package/dist/src/parsers/MergeQueryParser.js.map +1 -0
  228. package/dist/src/parsers/ParenExpressionParser.js +25 -0
  229. package/dist/src/parsers/ParenExpressionParser.js.map +1 -1
  230. package/dist/src/parsers/ReturningClauseParser.js +50 -7
  231. package/dist/src/parsers/ReturningClauseParser.js.map +1 -1
  232. package/dist/src/parsers/SelectClauseParser.js +2 -2
  233. package/dist/src/parsers/SelectClauseParser.js.map +1 -1
  234. package/dist/src/parsers/SelectQueryParser.d.ts +4 -0
  235. package/dist/src/parsers/SelectQueryParser.js +4 -0
  236. package/dist/src/parsers/SelectQueryParser.js.map +1 -1
  237. package/dist/src/parsers/SetClauseParser.js +97 -15
  238. package/dist/src/parsers/SetClauseParser.js.map +1 -1
  239. package/dist/src/parsers/SqlParser.d.ts +40 -0
  240. package/dist/src/parsers/SqlParser.js +409 -0
  241. package/dist/src/parsers/SqlParser.js.map +1 -0
  242. package/dist/src/parsers/SqlPrintTokenParser.d.ts +65 -3
  243. package/dist/src/parsers/SqlPrintTokenParser.js +1143 -36
  244. package/dist/src/parsers/SqlPrintTokenParser.js.map +1 -1
  245. package/dist/src/parsers/SqlTokenizer.d.ts +24 -2
  246. package/dist/src/parsers/SqlTokenizer.js +139 -74
  247. package/dist/src/parsers/SqlTokenizer.js.map +1 -1
  248. package/dist/src/parsers/UpdateQueryParser.js +11 -1
  249. package/dist/src/parsers/UpdateQueryParser.js.map +1 -1
  250. package/dist/src/parsers/UsingClauseParser.d.ts +11 -0
  251. package/dist/src/parsers/UsingClauseParser.js +33 -0
  252. package/dist/src/parsers/UsingClauseParser.js.map +1 -0
  253. package/dist/src/parsers/ValueParser.js +5 -1
  254. package/dist/src/parsers/ValueParser.js.map +1 -1
  255. package/dist/src/parsers/ValuesQueryParser.d.ts +0 -2
  256. package/dist/src/parsers/ValuesQueryParser.js +5 -45
  257. package/dist/src/parsers/ValuesQueryParser.js.map +1 -1
  258. package/dist/src/parsers/utils/LexemeCommentUtils.d.ts +6 -0
  259. package/dist/src/parsers/utils/LexemeCommentUtils.js +29 -0
  260. package/dist/src/parsers/utils/LexemeCommentUtils.js.map +1 -0
  261. package/dist/src/tokenReaders/CommandTokenReader.js +50 -2
  262. package/dist/src/tokenReaders/CommandTokenReader.js.map +1 -1
  263. package/dist/src/tokenReaders/LiteralTokenReader.js +8 -5
  264. package/dist/src/tokenReaders/LiteralTokenReader.js.map +1 -1
  265. package/dist/src/tokenReaders/OperatorTokenReader.js +10 -1
  266. package/dist/src/tokenReaders/OperatorTokenReader.js.map +1 -1
  267. package/dist/src/tokenReaders/TypeTokenReader.js +11 -1
  268. package/dist/src/tokenReaders/TypeTokenReader.js.map +1 -1
  269. package/dist/src/transformers/InsertQuerySelectValuesConverter.d.ts +18 -0
  270. package/dist/src/transformers/InsertQuerySelectValuesConverter.js +122 -0
  271. package/dist/src/transformers/InsertQuerySelectValuesConverter.js.map +1 -0
  272. package/dist/src/transformers/LinePrinter.d.ts +1 -0
  273. package/dist/src/transformers/LinePrinter.js +12 -0
  274. package/dist/src/transformers/LinePrinter.js.map +1 -1
  275. package/dist/src/transformers/OnelineFormattingHelper.d.ts +29 -0
  276. package/dist/src/transformers/OnelineFormattingHelper.js +99 -0
  277. package/dist/src/transformers/OnelineFormattingHelper.js.map +1 -0
  278. package/dist/src/transformers/QueryBuilder.d.ts +47 -13
  279. package/dist/src/transformers/QueryBuilder.js +433 -60
  280. package/dist/src/transformers/QueryBuilder.js.map +1 -1
  281. package/dist/src/transformers/SqlFormatter.d.ts +13 -1
  282. package/dist/src/transformers/SqlFormatter.js +20 -5
  283. package/dist/src/transformers/SqlFormatter.js.map +1 -1
  284. package/dist/src/transformers/SqlPrinter.d.ts +47 -8
  285. package/dist/src/transformers/SqlPrinter.js +625 -72
  286. package/dist/src/transformers/SqlPrinter.js.map +1 -1
  287. package/dist/src/types/Formatting.d.ts +8 -0
  288. package/dist/src/types/Formatting.js +3 -0
  289. package/dist/src/types/Formatting.js.map +1 -0
  290. package/dist/src/utils/ParserStringUtils.d.ts +6 -0
  291. package/dist/src/utils/ParserStringUtils.js +31 -0
  292. package/dist/src/utils/ParserStringUtils.js.map +1 -0
  293. package/dist/tsconfig.tsbuildinfo +1 -1
  294. 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.exportComment = (_h = options === null || options === void 0 ? void 0 : options.exportComment) !== null && _h !== void 0 ? _h : false;
54
- this.withClauseStyle = (_j = options === null || options === void 0 ? void 0 : options.withClauseStyle) !== null && _j !== void 0 ? _j : 'standard';
55
- this.commentStyle = (_k = options === null || options === void 0 ? void 0 : options.commentStyle) !== null && _k !== void 0 ? _k : 'block';
56
- this.parenthesesOneLine = (_l = options === null || options === void 0 ? void 0 : options.parenthesesOneLine) !== null && _l !== void 0 ? _l : false;
57
- this.betweenOneLine = (_m = options === null || options === void 0 ? void 0 : options.betweenOneLine) !== null && _m !== void 0 ? _m : false;
58
- this.valuesOneLine = (_o = options === null || options === void 0 ? void 0 : options.valuesOneLine) !== null && _o !== void 0 ? _o : false;
59
- this.joinOneLine = (_p = options === null || options === void 0 ? void 0 : options.joinOneLine) !== null && _p !== void 0 ? _p : false;
60
- this.caseOneLine = (_q = options === null || options === void 0 ? void 0 : options.caseOneLine) !== null && _q !== void 0 ? _q : false;
61
- this.subqueryOneLine = (_r = options === null || options === void 0 ? void 0 : options.subqueryOneLine) !== null && _r !== void 0 ? _r : false;
62
- this.indentNestedParentheses = (_s = options === null || options === void 0 ? void 0 : options.indentNestedParentheses) !== null && _s !== void 0 ? _s : false;
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((_t = options === null || options === void 0 ? void 0 : options.indentIncrementContainerTypes) !== null && _t !== void 0 ? _t : [
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
- appendToken(token, level, parentContainerType, caseContextDepth = 0, indentParentActive = false) {
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.handleCommentBlockContainer(token, level);
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 === "JoinClause") {
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.exportComment) {
165
- this.printCommentToken(token.text, level, parentContainerType);
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.handleCommentNewlineToken(token, level);
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.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.ParenExpression && this.parenthesesOneLine && !shouldIndentNested) ||
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
- if (!this.isOnelineMode() && shouldIncreaseIndent) {
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
- innerLevel = level + 1;
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
- this.linePrinter.appendNewline(innerLevel);
354
+ current.level = innerLevel;
213
355
  }
214
356
  }
215
- for (let i = 0; i < token.innerTokens.length; i++) {
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
- this.appendToken(child, innerLevel, token.containerType, nextCaseContextDepth, childIndentParentActive);
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' && this.andBreak !== 'none') {
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' && this.orBreak !== 'none') {
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
- const isWithinValuesClause = parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.Values;
290
- let effectiveCommaBreak = this.commaBreak;
291
- if (isWithinWithClause) {
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 (indentParentActive && !this.isOnelineMode()) {
377
- if (!(this.insideWithClause && this.withClauseStyle === 'full-oneline')) {
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 === ')' && indentParentActive && !this.isOnelineMode()) {
384
- // Align closing parenthesis with the outer indentation level when nested groups expand.
385
- const closingLevel = Math.max(level - 1, 0);
386
- if (!(this.insideWithClause && this.withClauseStyle === 'full-oneline')) {
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 (skip when inside WITH clause with full-oneline style)
449
- if (!(this.insideWithClause && this.withClauseStyle === 'full-oneline')) {
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
- if (this.shouldSkipCommentBlockSpace(parentContainerType)) {
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
- * Determines whether to skip space tokens in CommentBlocks.
473
- * Prevents duplicate spacing in CTE full-oneline mode only.
474
- */
475
- shouldSkipCommentBlockSpace(parentContainerType) {
476
- return parentContainerType === SqlPrintToken_1.SqlPrintTokenContainerType.CommentBlock &&
477
- this.insideWithClause &&
478
- this.withClauseStyle === 'full-oneline';
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.linePrinter.appendNewline(level);
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.linePrinter.appendNewline(level);
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
- this.appendToken(child, level, token.containerType, 0, false);
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
- if (lines.length > 1) {
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: false,
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.exportComment,
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
  /**