rawsql-ts 0.11.42-beta → 0.11.44-beta

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 (278) hide show
  1. package/README.md +12 -13
  2. package/dist/esm/index.js +18 -0
  3. package/dist/esm/index.js.map +1 -1
  4. package/dist/esm/index.min.js +23 -18
  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 +157 -0
  19. package/dist/esm/src/models/DDLStatements.js +141 -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 +33 -0
  40. package/dist/esm/src/models/SqlPrintToken.js +32 -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/CreateIndexParser.d.ts +16 -0
  49. package/dist/esm/src/parsers/CreateIndexParser.js +237 -0
  50. package/dist/esm/src/parsers/CreateIndexParser.js.map +1 -0
  51. package/dist/esm/src/parsers/CreateTableParser.d.ts +41 -0
  52. package/dist/esm/src/parsers/CreateTableParser.js +734 -0
  53. package/dist/esm/src/parsers/CreateTableParser.js.map +1 -0
  54. package/dist/esm/src/parsers/DeleteClauseParser.d.ts +11 -0
  55. package/dist/esm/src/parsers/DeleteClauseParser.js +33 -0
  56. package/dist/esm/src/parsers/DeleteClauseParser.js.map +1 -0
  57. package/dist/esm/src/parsers/DeleteQueryParser.d.ts +16 -0
  58. package/dist/esm/src/parsers/DeleteQueryParser.js +73 -0
  59. package/dist/esm/src/parsers/DeleteQueryParser.js.map +1 -0
  60. package/dist/esm/src/parsers/DropConstraintParser.d.ts +12 -0
  61. package/dist/esm/src/parsers/DropConstraintParser.js +47 -0
  62. package/dist/esm/src/parsers/DropConstraintParser.js.map +1 -0
  63. package/dist/esm/src/parsers/DropIndexParser.d.ts +12 -0
  64. package/dist/esm/src/parsers/DropIndexParser.js +69 -0
  65. package/dist/esm/src/parsers/DropIndexParser.js.map +1 -0
  66. package/dist/esm/src/parsers/DropTableParser.d.ts +12 -0
  67. package/dist/esm/src/parsers/DropTableParser.js +59 -0
  68. package/dist/esm/src/parsers/DropTableParser.js.map +1 -0
  69. package/dist/esm/src/parsers/FunctionExpressionParser.d.ts +4 -0
  70. package/dist/esm/src/parsers/FunctionExpressionParser.js +25 -8
  71. package/dist/esm/src/parsers/FunctionExpressionParser.js.map +1 -1
  72. package/dist/esm/src/parsers/InsertQueryParser.js +103 -31
  73. package/dist/esm/src/parsers/InsertQueryParser.js.map +1 -1
  74. package/dist/esm/src/parsers/MergeQueryParser.d.ts +26 -0
  75. package/dist/esm/src/parsers/MergeQueryParser.js +479 -0
  76. package/dist/esm/src/parsers/MergeQueryParser.js.map +1 -0
  77. package/dist/esm/src/parsers/ReturningClauseParser.js +50 -7
  78. package/dist/esm/src/parsers/ReturningClauseParser.js.map +1 -1
  79. package/dist/esm/src/parsers/SelectClauseParser.js +3 -3
  80. package/dist/esm/src/parsers/SelectClauseParser.js.map +1 -1
  81. package/dist/esm/src/parsers/SelectQueryParser.d.ts +4 -0
  82. package/dist/esm/src/parsers/SelectQueryParser.js +4 -0
  83. package/dist/esm/src/parsers/SelectQueryParser.js.map +1 -1
  84. package/dist/esm/src/parsers/SetClauseParser.js +97 -15
  85. package/dist/esm/src/parsers/SetClauseParser.js.map +1 -1
  86. package/dist/esm/src/parsers/SqlParser.d.ts +38 -0
  87. package/dist/esm/src/parsers/SqlParser.js +344 -0
  88. package/dist/esm/src/parsers/SqlParser.js.map +1 -0
  89. package/dist/esm/src/parsers/SqlPrintTokenParser.d.ts +68 -21
  90. package/dist/esm/src/parsers/SqlPrintTokenParser.js +1145 -254
  91. package/dist/esm/src/parsers/SqlPrintTokenParser.js.map +1 -1
  92. package/dist/esm/src/parsers/SqlTokenizer.d.ts +24 -2
  93. package/dist/esm/src/parsers/SqlTokenizer.js +135 -74
  94. package/dist/esm/src/parsers/SqlTokenizer.js.map +1 -1
  95. package/dist/esm/src/parsers/UpdateQueryParser.js +11 -1
  96. package/dist/esm/src/parsers/UpdateQueryParser.js.map +1 -1
  97. package/dist/esm/src/parsers/UsingClauseParser.d.ts +11 -0
  98. package/dist/esm/src/parsers/UsingClauseParser.js +29 -0
  99. package/dist/esm/src/parsers/UsingClauseParser.js.map +1 -0
  100. package/dist/esm/src/parsers/ValueParser.js +5 -1
  101. package/dist/esm/src/parsers/ValueParser.js.map +1 -1
  102. package/dist/esm/src/parsers/ValuesQueryParser.d.ts +0 -2
  103. package/dist/esm/src/parsers/ValuesQueryParser.js +5 -45
  104. package/dist/esm/src/parsers/ValuesQueryParser.js.map +1 -1
  105. package/dist/esm/src/parsers/utils/LexemeCommentUtils.d.ts +6 -0
  106. package/dist/esm/src/parsers/utils/LexemeCommentUtils.js +26 -0
  107. package/dist/esm/src/parsers/utils/LexemeCommentUtils.js.map +1 -0
  108. package/dist/esm/src/tokenReaders/CommandTokenReader.js +49 -2
  109. package/dist/esm/src/tokenReaders/CommandTokenReader.js.map +1 -1
  110. package/dist/esm/src/tokenReaders/LiteralTokenReader.js +8 -5
  111. package/dist/esm/src/tokenReaders/LiteralTokenReader.js.map +1 -1
  112. package/dist/esm/src/tokenReaders/OperatorTokenReader.js +10 -1
  113. package/dist/esm/src/tokenReaders/OperatorTokenReader.js.map +1 -1
  114. package/dist/esm/src/tokenReaders/TypeTokenReader.js +11 -1
  115. package/dist/esm/src/tokenReaders/TypeTokenReader.js.map +1 -1
  116. package/dist/esm/src/transformers/InsertQuerySelectValuesConverter.d.ts +18 -0
  117. package/dist/esm/src/transformers/InsertQuerySelectValuesConverter.js +118 -0
  118. package/dist/esm/src/transformers/InsertQuerySelectValuesConverter.js.map +1 -0
  119. package/dist/esm/src/transformers/LinePrinter.d.ts +2 -0
  120. package/dist/esm/src/transformers/LinePrinter.js +34 -1
  121. package/dist/esm/src/transformers/LinePrinter.js.map +1 -1
  122. package/dist/esm/src/transformers/OnelineFormattingHelper.d.ts +29 -0
  123. package/dist/esm/src/transformers/OnelineFormattingHelper.js +88 -0
  124. package/dist/esm/src/transformers/OnelineFormattingHelper.js.map +1 -0
  125. package/dist/esm/src/transformers/QueryBuilder.d.ts +47 -13
  126. package/dist/esm/src/transformers/QueryBuilder.js +424 -62
  127. package/dist/esm/src/transformers/QueryBuilder.js.map +1 -1
  128. package/dist/esm/src/transformers/SqlFormatter.d.ts +13 -3
  129. package/dist/esm/src/transformers/SqlFormatter.js +13 -4
  130. package/dist/esm/src/transformers/SqlFormatter.js.map +1 -1
  131. package/dist/esm/src/transformers/SqlPrinter.d.ts +63 -10
  132. package/dist/esm/src/transformers/SqlPrinter.js +954 -64
  133. package/dist/esm/src/transformers/SqlPrinter.js.map +1 -1
  134. package/dist/esm/src/types/Formatting.d.ts +8 -0
  135. package/dist/esm/src/types/Formatting.js +2 -0
  136. package/dist/esm/src/types/Formatting.js.map +1 -0
  137. package/dist/esm/src/utils/ParserStringUtils.d.ts +6 -0
  138. package/dist/esm/src/utils/ParserStringUtils.js +28 -0
  139. package/dist/esm/src/utils/ParserStringUtils.js.map +1 -0
  140. package/dist/esm/tsconfig.browser.tsbuildinfo +1 -1
  141. package/dist/index.min.js +22 -17
  142. package/dist/index.min.js.map +4 -4
  143. package/dist/src/index.d.ts +18 -0
  144. package/dist/src/index.js +18 -0
  145. package/dist/src/index.js.map +1 -1
  146. package/dist/src/models/BinarySelectQuery.d.ts +25 -1
  147. package/dist/src/models/BinarySelectQuery.js +28 -0
  148. package/dist/src/models/BinarySelectQuery.js.map +1 -1
  149. package/dist/src/models/Clause.d.ts +14 -2
  150. package/dist/src/models/Clause.js +29 -2
  151. package/dist/src/models/Clause.js.map +1 -1
  152. package/dist/src/models/CreateTableQuery.d.ts +99 -5
  153. package/dist/src/models/CreateTableQuery.js +90 -11
  154. package/dist/src/models/CreateTableQuery.js.map +1 -1
  155. package/dist/src/models/DDLStatements.d.ts +157 -0
  156. package/dist/src/models/DDLStatements.js +153 -0
  157. package/dist/src/models/DDLStatements.js.map +1 -0
  158. package/dist/src/models/DeleteQuery.d.ts +17 -0
  159. package/dist/src/models/DeleteQuery.js +20 -0
  160. package/dist/src/models/DeleteQuery.js.map +1 -0
  161. package/dist/src/models/InsertQuery.d.ts +7 -1
  162. package/dist/src/models/InsertQuery.js +6 -2
  163. package/dist/src/models/InsertQuery.js.map +1 -1
  164. package/dist/src/models/MergeQuery.d.ts +63 -0
  165. package/dist/src/models/MergeQuery.js +104 -0
  166. package/dist/src/models/MergeQuery.js.map +1 -0
  167. package/dist/src/models/SelectQuery.d.ts +37 -1
  168. package/dist/src/models/SelectQuery.js +7 -1
  169. package/dist/src/models/SelectQuery.js.map +1 -1
  170. package/dist/src/models/SimpleSelectQuery.d.ts +29 -1
  171. package/dist/src/models/SimpleSelectQuery.js +32 -0
  172. package/dist/src/models/SimpleSelectQuery.js.map +1 -1
  173. package/dist/src/models/SqlComponent.d.ts +2 -1
  174. package/dist/src/models/SqlComponent.js +1 -1
  175. package/dist/src/models/SqlComponent.js.map +1 -1
  176. package/dist/src/models/SqlPrintToken.d.ts +33 -0
  177. package/dist/src/models/SqlPrintToken.js +32 -0
  178. package/dist/src/models/SqlPrintToken.js.map +1 -1
  179. package/dist/src/models/ValuesQuery.d.ts +25 -1
  180. package/dist/src/models/ValuesQuery.js +28 -0
  181. package/dist/src/models/ValuesQuery.js.map +1 -1
  182. package/dist/src/parsers/AlterTableParser.d.ts +25 -0
  183. package/dist/src/parsers/AlterTableParser.js +432 -0
  184. package/dist/src/parsers/AlterTableParser.js.map +1 -0
  185. package/dist/src/parsers/CreateIndexParser.d.ts +16 -0
  186. package/dist/src/parsers/CreateIndexParser.js +241 -0
  187. package/dist/src/parsers/CreateIndexParser.js.map +1 -0
  188. package/dist/src/parsers/CreateTableParser.d.ts +41 -0
  189. package/dist/src/parsers/CreateTableParser.js +738 -0
  190. package/dist/src/parsers/CreateTableParser.js.map +1 -0
  191. package/dist/src/parsers/DeleteClauseParser.d.ts +11 -0
  192. package/dist/src/parsers/DeleteClauseParser.js +37 -0
  193. package/dist/src/parsers/DeleteClauseParser.js.map +1 -0
  194. package/dist/src/parsers/DeleteQueryParser.d.ts +16 -0
  195. package/dist/src/parsers/DeleteQueryParser.js +77 -0
  196. package/dist/src/parsers/DeleteQueryParser.js.map +1 -0
  197. package/dist/src/parsers/DropConstraintParser.d.ts +12 -0
  198. package/dist/src/parsers/DropConstraintParser.js +51 -0
  199. package/dist/src/parsers/DropConstraintParser.js.map +1 -0
  200. package/dist/src/parsers/DropIndexParser.d.ts +12 -0
  201. package/dist/src/parsers/DropIndexParser.js +73 -0
  202. package/dist/src/parsers/DropIndexParser.js.map +1 -0
  203. package/dist/src/parsers/DropTableParser.d.ts +12 -0
  204. package/dist/src/parsers/DropTableParser.js +63 -0
  205. package/dist/src/parsers/DropTableParser.js.map +1 -0
  206. package/dist/src/parsers/FunctionExpressionParser.d.ts +4 -0
  207. package/dist/src/parsers/FunctionExpressionParser.js +25 -8
  208. package/dist/src/parsers/FunctionExpressionParser.js.map +1 -1
  209. package/dist/src/parsers/InsertQueryParser.js +103 -31
  210. package/dist/src/parsers/InsertQueryParser.js.map +1 -1
  211. package/dist/src/parsers/MergeQueryParser.d.ts +26 -0
  212. package/dist/src/parsers/MergeQueryParser.js +483 -0
  213. package/dist/src/parsers/MergeQueryParser.js.map +1 -0
  214. package/dist/src/parsers/ReturningClauseParser.js +50 -7
  215. package/dist/src/parsers/ReturningClauseParser.js.map +1 -1
  216. package/dist/src/parsers/SelectClauseParser.js +2 -2
  217. package/dist/src/parsers/SelectClauseParser.js.map +1 -1
  218. package/dist/src/parsers/SelectQueryParser.d.ts +4 -0
  219. package/dist/src/parsers/SelectQueryParser.js +4 -0
  220. package/dist/src/parsers/SelectQueryParser.js.map +1 -1
  221. package/dist/src/parsers/SetClauseParser.js +97 -15
  222. package/dist/src/parsers/SetClauseParser.js.map +1 -1
  223. package/dist/src/parsers/SqlParser.d.ts +38 -0
  224. package/dist/src/parsers/SqlParser.js +353 -0
  225. package/dist/src/parsers/SqlParser.js.map +1 -0
  226. package/dist/src/parsers/SqlPrintTokenParser.d.ts +68 -21
  227. package/dist/src/parsers/SqlPrintTokenParser.js +1143 -252
  228. package/dist/src/parsers/SqlPrintTokenParser.js.map +1 -1
  229. package/dist/src/parsers/SqlTokenizer.d.ts +24 -2
  230. package/dist/src/parsers/SqlTokenizer.js +139 -74
  231. package/dist/src/parsers/SqlTokenizer.js.map +1 -1
  232. package/dist/src/parsers/UpdateQueryParser.js +11 -1
  233. package/dist/src/parsers/UpdateQueryParser.js.map +1 -1
  234. package/dist/src/parsers/UsingClauseParser.d.ts +11 -0
  235. package/dist/src/parsers/UsingClauseParser.js +33 -0
  236. package/dist/src/parsers/UsingClauseParser.js.map +1 -0
  237. package/dist/src/parsers/ValueParser.js +5 -1
  238. package/dist/src/parsers/ValueParser.js.map +1 -1
  239. package/dist/src/parsers/ValuesQueryParser.d.ts +0 -2
  240. package/dist/src/parsers/ValuesQueryParser.js +5 -45
  241. package/dist/src/parsers/ValuesQueryParser.js.map +1 -1
  242. package/dist/src/parsers/utils/LexemeCommentUtils.d.ts +6 -0
  243. package/dist/src/parsers/utils/LexemeCommentUtils.js +29 -0
  244. package/dist/src/parsers/utils/LexemeCommentUtils.js.map +1 -0
  245. package/dist/src/tokenReaders/CommandTokenReader.js +49 -2
  246. package/dist/src/tokenReaders/CommandTokenReader.js.map +1 -1
  247. package/dist/src/tokenReaders/LiteralTokenReader.js +8 -5
  248. package/dist/src/tokenReaders/LiteralTokenReader.js.map +1 -1
  249. package/dist/src/tokenReaders/OperatorTokenReader.js +10 -1
  250. package/dist/src/tokenReaders/OperatorTokenReader.js.map +1 -1
  251. package/dist/src/tokenReaders/TypeTokenReader.js +11 -1
  252. package/dist/src/tokenReaders/TypeTokenReader.js.map +1 -1
  253. package/dist/src/transformers/InsertQuerySelectValuesConverter.d.ts +18 -0
  254. package/dist/src/transformers/InsertQuerySelectValuesConverter.js +122 -0
  255. package/dist/src/transformers/InsertQuerySelectValuesConverter.js.map +1 -0
  256. package/dist/src/transformers/LinePrinter.d.ts +2 -0
  257. package/dist/src/transformers/LinePrinter.js +34 -1
  258. package/dist/src/transformers/LinePrinter.js.map +1 -1
  259. package/dist/src/transformers/OnelineFormattingHelper.d.ts +29 -0
  260. package/dist/src/transformers/OnelineFormattingHelper.js +92 -0
  261. package/dist/src/transformers/OnelineFormattingHelper.js.map +1 -0
  262. package/dist/src/transformers/QueryBuilder.d.ts +47 -13
  263. package/dist/src/transformers/QueryBuilder.js +433 -60
  264. package/dist/src/transformers/QueryBuilder.js.map +1 -1
  265. package/dist/src/transformers/SqlFormatter.d.ts +13 -3
  266. package/dist/src/transformers/SqlFormatter.js +20 -6
  267. package/dist/src/transformers/SqlFormatter.js.map +1 -1
  268. package/dist/src/transformers/SqlPrinter.d.ts +63 -10
  269. package/dist/src/transformers/SqlPrinter.js +954 -64
  270. package/dist/src/transformers/SqlPrinter.js.map +1 -1
  271. package/dist/src/types/Formatting.d.ts +8 -0
  272. package/dist/src/types/Formatting.js +3 -0
  273. package/dist/src/types/Formatting.js.map +1 -0
  274. package/dist/src/utils/ParserStringUtils.d.ts +6 -0
  275. package/dist/src/utils/ParserStringUtils.js +31 -0
  276. package/dist/src/utils/ParserStringUtils.js.map +1 -0
  277. package/dist/tsconfig.tsbuildinfo +1 -1
  278. package/package.json +1 -1
@@ -11,7 +11,10 @@ const IdentifierDecorator_1 = require("./IdentifierDecorator");
11
11
  const ParameterDecorator_1 = require("./ParameterDecorator");
12
12
  const InsertQuery_1 = require("../models/InsertQuery");
13
13
  const UpdateQuery_1 = require("../models/UpdateQuery");
14
+ const DeleteQuery_1 = require("../models/DeleteQuery");
14
15
  const CreateTableQuery_1 = require("../models/CreateTableQuery");
16
+ const MergeQuery_1 = require("../models/MergeQuery");
17
+ const DDLStatements_1 = require("../models/DDLStatements");
15
18
  var ParameterStyle;
16
19
  (function (ParameterStyle) {
17
20
  ParameterStyle["Anonymous"] = "anonymous";
@@ -23,36 +26,45 @@ exports.PRESETS = {
23
26
  identifierEscape: { start: '`', end: '`' },
24
27
  parameterSymbol: '?',
25
28
  parameterStyle: ParameterStyle.Anonymous,
29
+ constraintStyle: 'mysql',
26
30
  },
27
31
  postgres: {
28
32
  identifierEscape: { start: '"', end: '"' },
29
33
  parameterSymbol: '$',
30
34
  parameterStyle: ParameterStyle.Indexed,
35
+ castStyle: 'postgres',
36
+ constraintStyle: 'postgres',
31
37
  },
32
38
  postgresWithNamedParams: {
33
39
  identifierEscape: { start: '"', end: '"' },
34
40
  parameterSymbol: ':',
35
41
  parameterStyle: ParameterStyle.Named,
42
+ castStyle: 'postgres',
43
+ constraintStyle: 'postgres',
36
44
  },
37
45
  sqlserver: {
38
46
  identifierEscape: { start: '[', end: ']' },
39
47
  parameterSymbol: '@',
40
48
  parameterStyle: ParameterStyle.Named,
49
+ constraintStyle: 'postgres',
41
50
  },
42
51
  sqlite: {
43
52
  identifierEscape: { start: '"', end: '"' },
44
53
  parameterSymbol: ':',
45
54
  parameterStyle: ParameterStyle.Named,
55
+ constraintStyle: 'postgres',
46
56
  },
47
57
  oracle: {
48
58
  identifierEscape: { start: '"', end: '"' },
49
59
  parameterSymbol: ':',
50
60
  parameterStyle: ParameterStyle.Named,
61
+ constraintStyle: 'postgres',
51
62
  },
52
63
  clickhouse: {
53
64
  identifierEscape: { start: '`', end: '`' },
54
65
  parameterSymbol: '?',
55
66
  parameterStyle: ParameterStyle.Anonymous,
67
+ constraintStyle: 'postgres',
56
68
  },
57
69
  firebird: {
58
70
  identifierEscape: { start: '"', end: '"' },
@@ -83,6 +95,7 @@ exports.PRESETS = {
83
95
  identifierEscape: { start: '"', end: '"' },
84
96
  parameterSymbol: '$',
85
97
  parameterStyle: ParameterStyle.Indexed,
98
+ castStyle: 'postgres',
86
99
  },
87
100
  athena: {
88
101
  identifierEscape: { start: '"', end: '"' },
@@ -108,6 +121,7 @@ exports.PRESETS = {
108
121
  identifierEscape: { start: '"', end: '"' },
109
122
  parameterSymbol: '$',
110
123
  parameterStyle: ParameterStyle.Indexed,
124
+ castStyle: 'postgres',
111
125
  },
112
126
  flinksql: {
113
127
  identifierEscape: { start: '`', end: '`' },
@@ -142,10 +156,10 @@ class SqlPrintTokenParser {
142
156
  return this._selfHandlingComponentTypes;
143
157
  }
144
158
  constructor(options) {
145
- var _a, _b, _c, _d, _e, _f, _g, _h;
159
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
146
160
  this.handlers = new Map();
147
161
  this.index = 1;
148
- this.commentStyle = 'block';
162
+ this.joinConditionContexts = [];
149
163
  if (options === null || options === void 0 ? void 0 : options.preset) {
150
164
  const preset = options.preset;
151
165
  options = { ...preset, ...options };
@@ -159,7 +173,9 @@ class SqlPrintTokenParser {
159
173
  start: (_e = (_d = options === null || options === void 0 ? void 0 : options.identifierEscape) === null || _d === void 0 ? void 0 : _d.start) !== null && _e !== void 0 ? _e : '"',
160
174
  end: (_g = (_f = options === null || options === void 0 ? void 0 : options.identifierEscape) === null || _f === void 0 ? void 0 : _f.end) !== null && _g !== void 0 ? _g : '"'
161
175
  });
162
- this.commentStyle = (_h = options === null || options === void 0 ? void 0 : options.commentStyle) !== null && _h !== void 0 ? _h : 'block';
176
+ this.castStyle = (_h = options === null || options === void 0 ? void 0 : options.castStyle) !== null && _h !== void 0 ? _h : 'standard';
177
+ this.constraintStyle = (_j = options === null || options === void 0 ? void 0 : options.constraintStyle) !== null && _j !== void 0 ? _j : 'postgres';
178
+ this.normalizeJoinConditionOrder = (_k = options === null || options === void 0 ? void 0 : options.joinConditionOrderByDeclaration) !== null && _k !== void 0 ? _k : false;
163
179
  this.handlers.set(ValueComponent_1.ValueList.kind, (expr) => this.visitValueList(expr));
164
180
  this.handlers.set(ValueComponent_1.ColumnReference.kind, (expr) => this.visitColumnReference(expr));
165
181
  this.handlers.set(ValueComponent_1.QualifiedName.kind, (expr) => this.visitQualifiedName(expr));
@@ -231,10 +247,32 @@ class SqlPrintTokenParser {
231
247
  this.handlers.set(Clause_1.InsertClause.kind, (expr) => this.visitInsertClause(expr));
232
248
  this.handlers.set(UpdateQuery_1.UpdateQuery.kind, (expr) => this.visitUpdateQuery(expr));
233
249
  this.handlers.set(Clause_1.UpdateClause.kind, (expr) => this.visitUpdateClause(expr));
250
+ this.handlers.set(DeleteQuery_1.DeleteQuery.kind, (expr) => this.visitDeleteQuery(expr));
251
+ this.handlers.set(Clause_1.DeleteClause.kind, (expr) => this.visitDeleteClause(expr));
252
+ this.handlers.set(Clause_1.UsingClause.kind, (expr) => this.visitUsingClause(expr));
234
253
  this.handlers.set(Clause_1.SetClause.kind, (expr) => this.visitSetClause(expr));
235
254
  this.handlers.set(Clause_1.SetClauseItem.kind, (expr) => this.visitSetClauseItem(expr));
236
255
  this.handlers.set(Clause_1.ReturningClause.kind, (expr) => this.visitReturningClause(expr));
237
256
  this.handlers.set(CreateTableQuery_1.CreateTableQuery.kind, (expr) => this.visitCreateTableQuery(expr));
257
+ this.handlers.set(CreateTableQuery_1.TableColumnDefinition.kind, (expr) => this.visitTableColumnDefinition(expr));
258
+ this.handlers.set(CreateTableQuery_1.ColumnConstraintDefinition.kind, (expr) => this.visitColumnConstraintDefinition(expr));
259
+ this.handlers.set(CreateTableQuery_1.TableConstraintDefinition.kind, (expr) => this.visitTableConstraintDefinition(expr));
260
+ this.handlers.set(CreateTableQuery_1.ReferenceDefinition.kind, (expr) => this.visitReferenceDefinition(expr));
261
+ this.handlers.set(DDLStatements_1.CreateIndexStatement.kind, (expr) => this.visitCreateIndexStatement(expr));
262
+ this.handlers.set(DDLStatements_1.IndexColumnDefinition.kind, (expr) => this.visitIndexColumnDefinition(expr));
263
+ this.handlers.set(DDLStatements_1.DropTableStatement.kind, (expr) => this.visitDropTableStatement(expr));
264
+ this.handlers.set(DDLStatements_1.DropIndexStatement.kind, (expr) => this.visitDropIndexStatement(expr));
265
+ this.handlers.set(DDLStatements_1.AlterTableStatement.kind, (expr) => this.visitAlterTableStatement(expr));
266
+ this.handlers.set(DDLStatements_1.AlterTableAddConstraint.kind, (expr) => this.visitAlterTableAddConstraint(expr));
267
+ this.handlers.set(DDLStatements_1.AlterTableDropConstraint.kind, (expr) => this.visitAlterTableDropConstraint(expr));
268
+ this.handlers.set(DDLStatements_1.AlterTableDropColumn.kind, (expr) => this.visitAlterTableDropColumn(expr));
269
+ this.handlers.set(DDLStatements_1.DropConstraintStatement.kind, (expr) => this.visitDropConstraintStatement(expr));
270
+ this.handlers.set(MergeQuery_1.MergeQuery.kind, (expr) => this.visitMergeQuery(expr));
271
+ this.handlers.set(MergeQuery_1.MergeWhenClause.kind, (expr) => this.visitMergeWhenClause(expr));
272
+ this.handlers.set(MergeQuery_1.MergeUpdateAction.kind, (expr) => this.visitMergeUpdateAction(expr));
273
+ this.handlers.set(MergeQuery_1.MergeDeleteAction.kind, (expr) => this.visitMergeDeleteAction(expr));
274
+ this.handlers.set(MergeQuery_1.MergeInsertAction.kind, (expr) => this.visitMergeInsertAction(expr));
275
+ this.handlers.set(MergeQuery_1.MergeDoNothingAction.kind, (expr) => this.visitMergeDoNothingAction(expr));
238
276
  }
239
277
  /**
240
278
  * Pretty-prints a BinarySelectQuery (e.g., UNION, INTERSECT, EXCEPT).
@@ -250,14 +288,12 @@ class SqlPrintTokenParser {
250
288
  arg.positionedComments = null;
251
289
  }
252
290
  else if (arg.headerComments && arg.headerComments.length > 0) {
253
- // Fallback to legacy headerComments if no positioned comments
254
- // For smart comment style, treat headerComments as a single multi-line block
255
- if (this.commentStyle === 'smart' && arg.headerComments.length > 1) {
291
+ if (this.shouldMergeHeaderComments(arg.headerComments)) {
256
292
  const mergedHeaderComment = this.createHeaderMultiLineCommentBlock(arg.headerComments);
257
293
  token.innerTokens.push(mergedHeaderComment);
258
294
  }
259
295
  else {
260
- const headerCommentBlocks = this.createCommentBlocks(arg.headerComments);
296
+ const headerCommentBlocks = this.createCommentBlocks(arg.headerComments, true);
261
297
  token.innerTokens.push(...headerCommentBlocks);
262
298
  }
263
299
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
@@ -441,14 +477,9 @@ class SqlPrintTokenParser {
441
477
  const beforeComments = component.getPositionedComments('before');
442
478
  if (beforeComments.length > 0) {
443
479
  const commentBlocks = this.createCommentBlocks(beforeComments);
444
- // Create a single inline sequence: /* comment */ content
445
- const beforeTokens = [];
446
- for (const commentBlock of commentBlocks) {
447
- beforeTokens.push(commentBlock);
448
- beforeTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.space, ' '));
480
+ for (let i = commentBlocks.length - 1; i >= 0; i--) {
481
+ token.innerTokens.unshift(commentBlocks[i]);
449
482
  }
450
- // Insert before the existing content
451
- token.innerTokens.unshift(...beforeTokens);
452
483
  }
453
484
  // Handle 'after' comments - add inline after the main content
454
485
  const afterComments = component.getPositionedComments('after');
@@ -456,7 +487,7 @@ class SqlPrintTokenParser {
456
487
  const commentBlocks = this.createCommentBlocks(afterComments);
457
488
  // Append after comments with spaces for inline formatting
458
489
  for (const commentBlock of commentBlocks) {
459
- token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.space, ' '));
490
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
460
491
  token.innerTokens.push(commentBlock);
461
492
  }
462
493
  }
@@ -485,16 +516,8 @@ class SqlPrintTokenParser {
485
516
  if (!(comments === null || comments === void 0 ? void 0 : comments.length)) {
486
517
  return;
487
518
  }
488
- // For multiple comments, create inline comment sequence instead of separate blocks
489
- if (comments.length > 1) {
490
- const inlineComments = this.createInlineCommentSequence(comments);
491
- this.insertCommentBlocksWithSpacing(token, inlineComments);
492
- }
493
- else {
494
- // Create CommentBlock containers for single comment
495
- const commentBlocks = this.createCommentBlocks(comments);
496
- this.insertCommentBlocksWithSpacing(token, commentBlocks);
497
- }
519
+ const commentBlocks = this.createCommentBlocks(comments);
520
+ this.insertCommentBlocksWithSpacing(token, commentBlocks);
498
521
  }
499
522
  /**
500
523
  * Creates inline comment sequence for multiple comments without newlines
@@ -505,7 +528,7 @@ class SqlPrintTokenParser {
505
528
  const comment = comments[i];
506
529
  if (comment.trim()) {
507
530
  // Add comment token directly
508
- const commentToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.comment, this.formatBlockComment(comment));
531
+ const commentToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.comment, this.formatComment(comment));
509
532
  commentTokens.push(commentToken);
510
533
  // Add space between comments (except after last comment)
511
534
  if (i < comments.length - 1) {
@@ -518,63 +541,21 @@ class SqlPrintTokenParser {
518
541
  }
519
542
  /**
520
543
  * Creates CommentBlock containers for the given comments.
521
- * Each CommentBlock contains: Comment -> CommentNewline -> Space
522
- * This structure supports both oneliner and multiline formatting modes.
544
+ * Each CommentBlock contains: Comment -> CommentNewline -> Space.
545
+ * @param comments Raw comment strings to convert into CommentBlock tokens.
546
+ * @param isHeaderComment Marks the generated blocks as originating from header comments when true.
523
547
  */
524
- createCommentBlocks(comments) {
525
- if (this.commentStyle === 'smart') {
526
- return this.createSmartCommentBlocks(comments);
527
- }
528
- // Block style (default) - each comment gets its own block
548
+ createCommentBlocks(comments, isHeaderComment = false) {
549
+ // Create individual comment blocks for each comment entry
529
550
  const commentBlocks = [];
530
551
  for (const comment of comments) {
531
552
  // Accept comments that have content after trim OR are separator lines OR are empty (for structure preservation)
532
553
  const trimmed = comment.trim();
533
554
  const isSeparatorLine = /^[-=_+*#]+$/.test(trimmed);
534
555
  if (trimmed || isSeparatorLine || comment === '') {
535
- commentBlocks.push(this.createSingleCommentBlock(comment));
536
- }
537
- }
538
- return commentBlocks;
539
- }
540
- /**
541
- * Creates smart comment blocks by merging consecutive block comments into multi-line format
542
- */
543
- createSmartCommentBlocks(comments) {
544
- const commentBlocks = [];
545
- const blockComments = [];
546
- const flushBlockComments = () => {
547
- if (blockComments.length > 0) {
548
- if (blockComments.length === 1) {
549
- // Single comment - keep as-is
550
- commentBlocks.push(this.createSingleCommentBlock(blockComments[0]));
551
- }
552
- else {
553
- // Multiple consecutive comments - create multi-line block
554
- commentBlocks.push(this.createMultiLineCommentBlock(blockComments));
555
- }
556
- blockComments.length = 0;
557
- }
558
- };
559
- for (const comment of comments) {
560
- const trimmed = comment.trim();
561
- const isSeparatorLine = /^[-=_+*#]+$/.test(trimmed);
562
- if (!trimmed && !isSeparatorLine && comment !== '') {
563
- continue;
564
- }
565
- // Check if this is a block comment that should be merged
566
- if (this.shouldMergeComment(trimmed)) {
567
- blockComments.push(comment);
568
- }
569
- else {
570
- // Flush any accumulated block comments first
571
- flushBlockComments();
572
- // Add this comment as-is
573
- commentBlocks.push(this.createSingleCommentBlock(comment));
556
+ commentBlocks.push(this.createSingleCommentBlock(comment, isHeaderComment));
574
557
  }
575
558
  }
576
- // Flush any remaining block comments
577
- flushBlockComments();
578
559
  return commentBlocks;
579
560
  }
580
561
  /**
@@ -587,8 +568,14 @@ class SqlPrintTokenParser {
587
568
  return false;
588
569
  }
589
570
  // Don't merge if it's already a proper multi-line block comment
590
- if (trimmed.startsWith('/*') && trimmed.endsWith('*/') && trimmed.includes('\n')) {
591
- return false;
571
+ if (trimmed.startsWith('/*') && trimmed.endsWith('*/')) {
572
+ const inner = trimmed.slice(2, -2).trim();
573
+ if (!inner) {
574
+ return false;
575
+ }
576
+ if (trimmed.includes('\n')) {
577
+ return false;
578
+ }
592
579
  }
593
580
  // Merge all other content including separator lines, plain text, and single-line block comments
594
581
  // Separator lines within comment blocks should be merged together
@@ -598,98 +585,6 @@ class SqlPrintTokenParser {
598
585
  * Creates a multi-line block comment structure from consecutive comments
599
586
  * Returns a CommentBlock containing multiple comment lines for proper LinePrinter integration
600
587
  */
601
- createMultiLineCommentBlock(comments) {
602
- const lines = [];
603
- for (const comment of comments) {
604
- const trimmed = comment.trim();
605
- // Remove existing /* */ markers if present and extract content
606
- if (trimmed.startsWith('/*') && trimmed.endsWith('*/')) {
607
- const content = trimmed.slice(2, -2);
608
- if (content.trim()) {
609
- // Sanitize the content (only remove /* and */)
610
- const sanitized = content
611
- .replace(/\*\//g, '*') // Remove */ sequences
612
- .replace(/\/\*/g, '*'); // Remove /* sequences
613
- // Split multi-line content and add each line
614
- const contentLines = sanitized.split('\n').map(line => line.trim()).filter(line => line);
615
- lines.push(...contentLines);
616
- }
617
- }
618
- else if (trimmed) {
619
- // Sanitize plain text content
620
- const sanitized = trimmed
621
- .replace(/\*\//g, '*') // Remove */ sequences
622
- .replace(/\/\*/g, '*'); // Remove /* sequences
623
- // Split plain text content by lines
624
- const contentLines = sanitized.split('\n').map(line => line.trim()).filter(line => line);
625
- lines.push(...contentLines);
626
- }
627
- }
628
- // Create multi-line comment block structure
629
- const commentBlock = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.CommentBlock);
630
- if (lines.length === 0) {
631
- // Empty comment
632
- const commentToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.comment, '/* */');
633
- commentBlock.innerTokens.push(commentToken);
634
- }
635
- else {
636
- // Opening /*
637
- const openToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.comment, '/*');
638
- commentBlock.innerTokens.push(openToken);
639
- commentBlock.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.commentNewline, ''));
640
- // Content lines (each as separate comment token)
641
- for (const line of lines) {
642
- const lineToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.comment, ` ${line}`);
643
- commentBlock.innerTokens.push(lineToken);
644
- commentBlock.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.commentNewline, ''));
645
- }
646
- // Closing */
647
- const closeToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.comment, '*/');
648
- commentBlock.innerTokens.push(closeToken);
649
- }
650
- // Add final newline and space for standard structure
651
- commentBlock.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.commentNewline, ''));
652
- commentBlock.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.space, ' '));
653
- return commentBlock;
654
- }
655
- /**
656
- * Creates a multi-line comment block specifically for headerComments
657
- * headerComments come as pre-split lines, so we handle them differently
658
- */
659
- createHeaderMultiLineCommentBlock(headerComments) {
660
- // Keep all lines including empty ones to preserve structure
661
- const lines = headerComments;
662
- // Create multi-line comment block structure
663
- const commentBlock = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.CommentBlock);
664
- if (lines.length === 0) {
665
- // Empty comment
666
- const commentToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.comment, '/* */');
667
- commentBlock.innerTokens.push(commentToken);
668
- }
669
- else {
670
- // Opening /*
671
- const openToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.comment, '/*');
672
- commentBlock.innerTokens.push(openToken);
673
- commentBlock.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.commentNewline, ''));
674
- // Content lines (each as separate comment token)
675
- for (const line of lines) {
676
- // Sanitize the line content
677
- const sanitized = line
678
- .replace(/\*\//g, '*') // Remove */ sequences
679
- .replace(/\/\*/g, '*'); // Remove /* sequences
680
- const lineToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.comment, ` ${sanitized}`);
681
- commentBlock.innerTokens.push(lineToken);
682
- commentBlock.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.commentNewline, ''));
683
- }
684
- // Closing */
685
- const closeToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.comment, '*/');
686
- commentBlock.innerTokens.push(closeToken);
687
- }
688
- // Add final newline and space for standard structure
689
- commentBlock.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.commentNewline, ''));
690
- commentBlock.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.space, ' '));
691
- return commentBlock;
692
- }
693
588
  /**
694
589
  * Creates a single CommentBlock with the standard structure:
695
590
  * Comment -> CommentNewline -> Space
@@ -698,8 +593,11 @@ class SqlPrintTokenParser {
698
593
  * - Multiline mode: Comment + newline (space is filtered as leading space)
699
594
  * - Oneliner mode: Comment + space (commentNewline is skipped)
700
595
  */
701
- createSingleCommentBlock(comment) {
596
+ createSingleCommentBlock(comment, isHeaderComment = false) {
702
597
  const commentBlock = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.CommentBlock);
598
+ if (isHeaderComment) {
599
+ commentBlock.markAsHeaderComment();
600
+ }
703
601
  // Add comment token - preserve original format for line comments
704
602
  const commentToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.comment, this.formatComment(comment));
705
603
  commentBlock.innerTokens.push(commentToken);
@@ -717,42 +615,28 @@ class SqlPrintTokenParser {
717
615
  */
718
616
  formatComment(comment) {
719
617
  const trimmed = comment.trim();
720
- // Smart style processing
721
- if (this.commentStyle === 'smart') {
722
- return this.formatCommentSmart(trimmed);
618
+ if (!trimmed) {
619
+ return '/* */';
723
620
  }
724
- // Default block style processing
725
- // If it's already a line comment, preserve it
726
- // But exclude separator lines (lines with only dashes, equals, etc.)
727
- if (trimmed.startsWith('--') && !/^--[-=_+*#]*$/.test(trimmed)) {
728
- return trimmed;
621
+ const isSeparatorLine = /^[-=_+*#]+$/.test(trimmed);
622
+ if (isSeparatorLine) {
623
+ return this.formatBlockComment(trimmed);
624
+ }
625
+ if (trimmed.startsWith('--')) {
626
+ return this.formatLineComment(trimmed.slice(2));
729
627
  }
730
- // If it's already a block comment, preserve it (but sanitize)
731
628
  if (trimmed.startsWith('/*') && trimmed.endsWith('*/')) {
732
- // Pass the entire comment including /* and */ for proper sanitization
733
629
  return this.formatBlockComment(trimmed);
734
630
  }
735
- // For plain text comments, convert to block format
736
631
  return this.formatBlockComment(trimmed);
737
632
  }
738
- /**
739
- * Formats comments using smart style rules:
740
- * - Only multi-line block comment merging is supported
741
- * - Single-line comments remain as block comments (no dash conversion)
742
- */
743
- formatCommentSmart(comment) {
744
- // Smart style only affects multi-line comment merging at createSmartCommentBlocks level
745
- // Individual comment formatting remains the same as block style
746
- return this.formatBlockComment(comment);
747
- }
748
633
  /**
749
634
  * Inserts comment blocks into a token and handles spacing logic.
750
635
  * Adds separator spaces for clause-level containers and manages duplicate space removal.
751
636
  */
752
637
  insertCommentBlocksWithSpacing(token, commentBlocks) {
753
- // For SelectItem, append comments at the end with proper spacing
638
+ // For SelectItem, append comment blocks after ensuring spacing
754
639
  if (token.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.SelectItem) {
755
- // Add space before comment if not already present
756
640
  if (token.innerTokens.length > 0) {
757
641
  const lastToken = token.innerTokens[token.innerTokens.length - 1];
758
642
  if (lastToken.type !== SqlPrintToken_1.SqlPrintTokenType.space) {
@@ -772,7 +656,6 @@ class SqlPrintTokenParser {
772
656
  }
773
657
  // Special handling for IdentifierString to add space before comment
774
658
  if (token.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.IdentifierString) {
775
- // Add space before comment if not already present
776
659
  if (token.innerTokens.length > 0) {
777
660
  const lastToken = token.innerTokens[token.innerTokens.length - 1];
778
661
  if (lastToken.type !== SqlPrintToken_1.SqlPrintTokenType.space) {
@@ -875,27 +758,95 @@ class SqlPrintTokenParser {
875
758
  * Prevents SQL injection by removing dangerous comment sequences.
876
759
  */
877
760
  formatBlockComment(comment) {
878
- // Sanitize dangerous comment sequences to prevent SQL injection
879
- let sanitizedComment = comment
880
- .replace(/\*\//g, '*') // Remove comment close sequences
881
- .replace(/\/\*/g, '*'); // Remove comment open sequences
882
- // Check if this is a separator line (like ----------) before processing
883
- const trimmed = sanitizedComment.trim();
884
- const isSeparatorLine = /^[-=_+*#]+$/.test(trimmed);
885
- if (isSeparatorLine) {
886
- // For separator lines, preserve as-is (already sanitized above)
887
- return `/* ${trimmed} */`;
888
- }
889
- // For multiline comments: convert newlines to spaces (security requirement)
890
- sanitizedComment = sanitizedComment
891
- .replace(/\r?\n/g, ' ') // Replace newlines with spaces
892
- .replace(/\s+/g, ' ') // Collapse multiple spaces into single space
893
- .trim(); // Remove leading/trailing whitespace
894
- // Return empty string if comment becomes empty after sanitization
895
- if (!sanitizedComment) {
896
- return '';
761
+ const hasDelimiters = comment.startsWith('/*') && comment.endsWith('*/');
762
+ const rawContent = hasDelimiters ? comment.slice(2, -2) : comment;
763
+ const escapedContent = this.escapeCommentDelimiters(rawContent);
764
+ const normalized = escapedContent.replace(/\r?\n/g, '\n');
765
+ const lines = normalized
766
+ .split('\n')
767
+ .map(line => line.replace(/\s+/g, ' ').trim())
768
+ .filter(line => line.length > 0);
769
+ if (lines.length === 0) {
770
+ return '/* */';
771
+ }
772
+ const isSeparatorLine = lines.length === 1 && /^[-=_+*#]+$/.test(lines[0]);
773
+ if (!hasDelimiters) {
774
+ // Flatten free-form comments to a single block to avoid leaking multi-line structures.
775
+ if (isSeparatorLine) {
776
+ return `/* ${lines[0]} */`;
777
+ }
778
+ const flattened = lines.join(' ');
779
+ return `/* ${flattened} */`;
780
+ }
781
+ if (isSeparatorLine || lines.length === 1) {
782
+ return `/* ${lines[0]} */`;
783
+ }
784
+ const body = lines.map(line => ` ${line}`).join('\n');
785
+ return `/*\n${body}\n*/`;
786
+ }
787
+ shouldMergeHeaderComments(comments) {
788
+ if (comments.length <= 1) {
789
+ return false;
790
+ }
791
+ return comments.some(comment => {
792
+ const trimmed = comment.trim();
793
+ return /^[-=_+*#]{3,}$/.test(trimmed) || trimmed.startsWith('- ') || trimmed.startsWith('* ');
794
+ });
795
+ }
796
+ createHeaderMultiLineCommentBlock(headerComments) {
797
+ const commentBlock = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.CommentBlock);
798
+ commentBlock.markAsHeaderComment();
799
+ if (headerComments.length === 0) {
800
+ const commentToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.comment, '/* */');
801
+ commentBlock.innerTokens.push(commentToken);
802
+ }
803
+ else {
804
+ const openToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.comment, '/*');
805
+ commentBlock.innerTokens.push(openToken);
806
+ commentBlock.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.commentNewline, ''));
807
+ for (const line of headerComments) {
808
+ const sanitized = this.escapeCommentDelimiters(line);
809
+ const lineToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.comment, ` ${sanitized}`);
810
+ commentBlock.innerTokens.push(lineToken);
811
+ commentBlock.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.commentNewline, ''));
812
+ }
813
+ const closeToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.comment, '*/');
814
+ commentBlock.innerTokens.push(closeToken);
815
+ }
816
+ commentBlock.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.commentNewline, ''));
817
+ commentBlock.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.space, ' '));
818
+ return commentBlock;
819
+ }
820
+ /**
821
+ * Formats text as a single-line comment while sanitizing unsafe sequences.
822
+ */
823
+ formatLineComment(content) {
824
+ // Normalize content to a single line and remove dangerous sequences
825
+ const sanitized = this.sanitizeLineCommentContent(content);
826
+ if (!sanitized) {
827
+ return '--';
897
828
  }
898
- return `/* ${sanitizedComment} */`;
829
+ return `-- ${sanitized}`;
830
+ }
831
+ /**
832
+ * Sanitizes content intended for a single-line comment.
833
+ */
834
+ sanitizeLineCommentContent(content) {
835
+ // Replace comment delimiters to avoid nested comment injection
836
+ let sanitized = this.escapeCommentDelimiters(content)
837
+ .replace(/\r?\n/g, ' ')
838
+ .replace(/\u2028|\u2029/g, ' ')
839
+ .replace(/\s+/g, ' ')
840
+ .trim();
841
+ if (sanitized.startsWith('--')) {
842
+ sanitized = sanitized.slice(2).trimStart();
843
+ }
844
+ return sanitized;
845
+ }
846
+ escapeCommentDelimiters(content) {
847
+ return content
848
+ .replace(/\/\*/g, '\\/\\*')
849
+ .replace(/\*\//g, '*\\/');
899
850
  }
900
851
  visitValueList(arg) {
901
852
  const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.ValueList);
@@ -915,7 +866,6 @@ class SqlPrintTokenParser {
915
866
  }
916
867
  visitFunctionCall(arg) {
917
868
  const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.FunctionCall);
918
- this.addComponentComments(token, arg);
919
869
  token.innerTokens.push(arg.qualifiedName.accept(this));
920
870
  token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
921
871
  if (arg.argument) {
@@ -953,6 +903,7 @@ class SqlPrintTokenParser {
953
903
  token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
954
904
  }
955
905
  }
906
+ this.addComponentComments(token, arg);
956
907
  return token;
957
908
  }
958
909
  visitUnaryExpression(arg) {
@@ -1157,9 +1108,22 @@ class SqlPrintTokenParser {
1157
1108
  }
1158
1109
  visitCastExpression(arg) {
1159
1110
  const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.CastExpression);
1111
+ // Use PostgreSQL-specific :: casts only when the preset explicitly opts in.
1112
+ if (this.castStyle === 'postgres') {
1113
+ token.innerTokens.push(this.visit(arg.input));
1114
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.operator, '::'));
1115
+ token.innerTokens.push(this.visit(arg.castType));
1116
+ return token;
1117
+ }
1118
+ // Default to ANSI-compliant CAST(expression AS type) syntax for broader compatibility.
1119
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'cast'));
1120
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
1160
1121
  token.innerTokens.push(this.visit(arg.input));
1161
- token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.operator, '::'));
1122
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1123
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'as'));
1124
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1162
1125
  token.innerTokens.push(this.visit(arg.castType));
1126
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
1163
1127
  return token;
1164
1128
  }
1165
1129
  visitCaseExpression(arg) {
@@ -1170,20 +1134,148 @@ class SqlPrintTokenParser {
1170
1134
  // Clear positioned comments to prevent duplicate processing
1171
1135
  arg.positionedComments = null;
1172
1136
  }
1137
+ const promotedComments = [];
1138
+ const trailingSwitchComments = this.extractSwitchAfterComments(arg.switchCase);
1139
+ let conditionToken = null;
1140
+ if (arg.condition) {
1141
+ conditionToken = this.visit(arg.condition);
1142
+ promotedComments.push(...this.collectCaseLeadingCommentBlocks(conditionToken));
1143
+ }
1144
+ const switchToken = this.visit(arg.switchCase);
1145
+ promotedComments.push(...this.collectCaseLeadingCommentsFromSwitch(switchToken));
1146
+ if (promotedComments.length > 0) {
1147
+ token.innerTokens.push(...promotedComments);
1148
+ }
1173
1149
  // Add the CASE keyword
1174
1150
  token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'case'));
1175
1151
  // Add the condition if exists
1176
- if (arg.condition) {
1152
+ if (conditionToken) {
1177
1153
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1178
- token.innerTokens.push(this.visit(arg.condition));
1154
+ token.innerTokens.push(conditionToken);
1179
1155
  }
1180
1156
  // Add the WHEN/THEN pairs and ELSE
1181
- token.innerTokens.push(this.visit(arg.switchCase));
1157
+ token.innerTokens.push(switchToken);
1182
1158
  // Add the END keyword
1183
1159
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1184
1160
  token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'end'));
1161
+ if (trailingSwitchComments.length > 0) {
1162
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.commentNewline, ''));
1163
+ const trailingBlocks = this.createCommentBlocks(trailingSwitchComments);
1164
+ token.innerTokens.push(...trailingBlocks);
1165
+ }
1185
1166
  return token;
1186
1167
  }
1168
+ extractSwitchAfterComments(arg) {
1169
+ if (!arg.positionedComments || arg.positionedComments.length === 0) {
1170
+ return [];
1171
+ }
1172
+ const trailing = [];
1173
+ const retained = [];
1174
+ for (const entry of arg.positionedComments) {
1175
+ if (entry.position === 'after') {
1176
+ trailing.push(...entry.comments);
1177
+ }
1178
+ else {
1179
+ retained.push(entry);
1180
+ }
1181
+ }
1182
+ arg.positionedComments = retained.length > 0 ? retained : null;
1183
+ return trailing;
1184
+ }
1185
+ collectCaseLeadingCommentsFromSwitch(token) {
1186
+ if (!token.innerTokens || token.innerTokens.length === 0) {
1187
+ return [];
1188
+ }
1189
+ const pairToken = token.innerTokens.find(child => child.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.CaseKeyValuePair);
1190
+ if (!pairToken) {
1191
+ return [];
1192
+ }
1193
+ const keyToken = this.findCaseKeyToken(pairToken);
1194
+ if (!keyToken) {
1195
+ return [];
1196
+ }
1197
+ return this.collectCaseLeadingCommentBlocks(keyToken);
1198
+ }
1199
+ findCaseKeyToken(pairToken) {
1200
+ for (const child of pairToken.innerTokens) {
1201
+ if (child.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.CommentBlock) {
1202
+ continue;
1203
+ }
1204
+ if (child.type === SqlPrintToken_1.SqlPrintTokenType.space) {
1205
+ continue;
1206
+ }
1207
+ if (child.type === SqlPrintToken_1.SqlPrintTokenType.keyword) {
1208
+ continue;
1209
+ }
1210
+ if (child.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.CaseThenValue) {
1211
+ continue;
1212
+ }
1213
+ return child;
1214
+ }
1215
+ return undefined;
1216
+ }
1217
+ collectCaseLeadingCommentBlocks(token) {
1218
+ if (!token.innerTokens || token.innerTokens.length === 0) {
1219
+ return [];
1220
+ }
1221
+ const collected = [];
1222
+ this.collectCaseLeadingCommentBlocksRecursive(token, collected, new Set(), 0);
1223
+ return collected;
1224
+ }
1225
+ collectCaseLeadingCommentBlocksRecursive(token, collected, seen, depth) {
1226
+ if (!token.innerTokens || token.innerTokens.length === 0) {
1227
+ return;
1228
+ }
1229
+ let removedAny = false;
1230
+ while (token.innerTokens.length > 0) {
1231
+ const first = token.innerTokens[0];
1232
+ if (first.containerType === SqlPrintToken_1.SqlPrintTokenContainerType.CommentBlock) {
1233
+ token.innerTokens.shift();
1234
+ const signature = this.commentBlockSignature(first);
1235
+ if (!(depth > 0 && seen.has(signature))) {
1236
+ collected.push(first);
1237
+ seen.add(signature);
1238
+ }
1239
+ removedAny = true;
1240
+ continue;
1241
+ }
1242
+ if (!removedAny && first.type === SqlPrintToken_1.SqlPrintTokenType.space) {
1243
+ return;
1244
+ }
1245
+ break;
1246
+ }
1247
+ if (!token.innerTokens || token.innerTokens.length === 0) {
1248
+ return;
1249
+ }
1250
+ const firstChild = token.innerTokens[0];
1251
+ if (this.isTransparentCaseWrapper(firstChild)) {
1252
+ this.collectCaseLeadingCommentBlocksRecursive(firstChild, collected, seen, depth + 1);
1253
+ }
1254
+ }
1255
+ isTransparentCaseWrapper(token) {
1256
+ if (!token) {
1257
+ return false;
1258
+ }
1259
+ const transparentContainers = [
1260
+ SqlPrintToken_1.SqlPrintTokenContainerType.ColumnReference,
1261
+ SqlPrintToken_1.SqlPrintTokenContainerType.QualifiedName,
1262
+ SqlPrintToken_1.SqlPrintTokenContainerType.IdentifierString,
1263
+ SqlPrintToken_1.SqlPrintTokenContainerType.RawString,
1264
+ SqlPrintToken_1.SqlPrintTokenContainerType.LiteralValue,
1265
+ SqlPrintToken_1.SqlPrintTokenContainerType.ParenExpression,
1266
+ SqlPrintToken_1.SqlPrintTokenContainerType.UnaryExpression,
1267
+ ];
1268
+ return transparentContainers.includes(token.containerType);
1269
+ }
1270
+ commentBlockSignature(commentBlock) {
1271
+ if (!commentBlock.innerTokens || commentBlock.innerTokens.length === 0) {
1272
+ return '';
1273
+ }
1274
+ return commentBlock.innerTokens
1275
+ .filter(inner => inner.text !== '')
1276
+ .map(inner => inner.text)
1277
+ .join('|');
1278
+ }
1187
1279
  visitArrayExpression(arg) {
1188
1280
  const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.ArrayExpression);
1189
1281
  token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'array'));
@@ -1269,6 +1361,7 @@ class SqlPrintTokenParser {
1269
1361
  }
1270
1362
  visitTupleExpression(arg) {
1271
1363
  const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.TupleExpression);
1364
+ const requiresMultiline = this.tupleRequiresMultiline(arg);
1272
1365
  token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
1273
1366
  for (let i = 0; i < arg.values.length; i++) {
1274
1367
  if (i > 0) {
@@ -1276,10 +1369,38 @@ class SqlPrintTokenParser {
1276
1369
  }
1277
1370
  token.innerTokens.push(this.visit(arg.values[i]));
1278
1371
  }
1372
+ if (requiresMultiline) {
1373
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.commentNewline, '', SqlPrintToken_1.SqlPrintTokenContainerType.TupleExpression));
1374
+ }
1279
1375
  token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
1280
- this.addComponentComments(token, arg);
1281
1376
  return token;
1282
1377
  }
1378
+ tupleRequiresMultiline(tuple) {
1379
+ for (const value of tuple.values) {
1380
+ if (this.hasInlineComments(value)) {
1381
+ return true;
1382
+ }
1383
+ }
1384
+ return false;
1385
+ }
1386
+ hasInlineComments(component) {
1387
+ if (this.hasLeadingComments(component)) {
1388
+ return true;
1389
+ }
1390
+ if (component instanceof ValueComponent_1.TupleExpression) {
1391
+ return this.tupleRequiresMultiline(component);
1392
+ }
1393
+ return false;
1394
+ }
1395
+ hasLeadingComments(component) {
1396
+ var _a;
1397
+ const positioned = (_a = component.positionedComments) !== null && _a !== void 0 ? _a : [];
1398
+ const before = positioned.find(pc => pc.position === 'before');
1399
+ if (before && before.comments.some(comment => comment.trim().length > 0)) {
1400
+ return true;
1401
+ }
1402
+ return false;
1403
+ }
1283
1404
  visitWindowFrameExpression(arg) {
1284
1405
  // Compose window frame expression: over(partition by ... order by ... rows ...)
1285
1406
  const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.WindowFrameExpression);
@@ -1358,19 +1479,16 @@ class SqlPrintTokenParser {
1358
1479
  const originalValuePositionedComments = arg.value.positionedComments;
1359
1480
  // Clear positioned comments from the value to avoid duplication since SelectItem handles them
1360
1481
  arg.value.positionedComments = null;
1361
- // Add 'before' positioned comments
1482
+ // Add positioned comments in recorded order
1362
1483
  const beforeComments = arg.getPositionedComments('before');
1484
+ const afterComments = arg.getPositionedComments('after');
1485
+ const isParenExpression = arg.value instanceof ValueComponent_1.ParenExpression;
1363
1486
  if (beforeComments.length > 0) {
1364
1487
  const commentTokens = this.createInlineCommentSequence(beforeComments);
1365
1488
  token.innerTokens.push(...commentTokens);
1366
1489
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1367
1490
  }
1368
- // Add the value (column name)
1369
1491
  token.innerTokens.push(this.visit(arg.value));
1370
- // Add 'after' positioned comments for the value
1371
- // Skip after comments if the value is ParenExpression (already handled in ParenExpression processing)
1372
- const afterComments = arg.getPositionedComments('after');
1373
- const isParenExpression = arg.value.constructor.name === 'ParenExpression';
1374
1492
  if (afterComments.length > 0 && !isParenExpression) {
1375
1493
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1376
1494
  const commentTokens = this.createInlineCommentSequence(afterComments);
@@ -1563,16 +1681,32 @@ class SqlPrintTokenParser {
1563
1681
  }
1564
1682
  }
1565
1683
  visitFromClause(arg) {
1566
- const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'from', SqlPrintToken_1.SqlPrintTokenContainerType.FromClause);
1567
- token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1568
- token.innerTokens.push(this.visit(arg.source));
1569
- if (arg.joins) {
1570
- for (let i = 0; i < arg.joins.length; i++) {
1571
- token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1572
- token.innerTokens.push(this.visit(arg.joins[i]));
1684
+ // Build a declaration order map so JOIN ON operands can be normalized later.
1685
+ let contextPushed = false;
1686
+ if (this.normalizeJoinConditionOrder) {
1687
+ const aliasOrder = this.buildJoinAliasOrder(arg);
1688
+ if (aliasOrder.size > 0) {
1689
+ this.joinConditionContexts.push({ aliasOrder });
1690
+ contextPushed = true;
1691
+ }
1692
+ }
1693
+ try {
1694
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'from', SqlPrintToken_1.SqlPrintTokenContainerType.FromClause);
1695
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1696
+ token.innerTokens.push(this.visit(arg.source));
1697
+ if (arg.joins) {
1698
+ for (let i = 0; i < arg.joins.length; i++) {
1699
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1700
+ token.innerTokens.push(this.visit(arg.joins[i]));
1701
+ }
1702
+ }
1703
+ return token;
1704
+ }
1705
+ finally {
1706
+ if (contextPushed) {
1707
+ this.joinConditionContexts.pop();
1573
1708
  }
1574
1709
  }
1575
- return token;
1576
1710
  }
1577
1711
  visitJoinClause(arg) {
1578
1712
  // Print join clause: [joinType] [lateral] [source] [on/using ...]
@@ -1622,12 +1756,123 @@ class SqlPrintTokenParser {
1622
1756
  return token;
1623
1757
  }
1624
1758
  visitJoinOnClause(arg) {
1759
+ // Normalize JOIN ON predicate columns to follow declaration order when enabled.
1760
+ if (this.normalizeJoinConditionOrder) {
1761
+ const aliasOrder = this.getCurrentJoinAliasOrder();
1762
+ if (aliasOrder) {
1763
+ this.normalizeJoinConditionValue(arg.condition, aliasOrder);
1764
+ }
1765
+ }
1625
1766
  const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.JoinOnClause);
1626
1767
  token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'on'));
1627
1768
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1628
1769
  token.innerTokens.push(this.visit(arg.condition));
1629
1770
  return token;
1630
1771
  }
1772
+ getCurrentJoinAliasOrder() {
1773
+ if (this.joinConditionContexts.length === 0) {
1774
+ return null;
1775
+ }
1776
+ return this.joinConditionContexts[this.joinConditionContexts.length - 1].aliasOrder;
1777
+ }
1778
+ buildJoinAliasOrder(fromClause) {
1779
+ const aliasOrder = new Map();
1780
+ let nextIndex = 0;
1781
+ const registerSource = (source) => {
1782
+ const identifiers = this.collectSourceIdentifiers(source);
1783
+ if (identifiers.length === 0) {
1784
+ return;
1785
+ }
1786
+ // Track the earliest declaration index for each identifier found in the FROM clause.
1787
+ for (const identifier of identifiers) {
1788
+ const key = identifier.toLowerCase();
1789
+ if (!aliasOrder.has(key)) {
1790
+ aliasOrder.set(key, nextIndex);
1791
+ }
1792
+ }
1793
+ nextIndex++;
1794
+ };
1795
+ registerSource(fromClause.source);
1796
+ if (fromClause.joins) {
1797
+ for (const joinClause of fromClause.joins) {
1798
+ registerSource(joinClause.source);
1799
+ }
1800
+ }
1801
+ return aliasOrder;
1802
+ }
1803
+ collectSourceIdentifiers(source) {
1804
+ const identifiers = [];
1805
+ const aliasName = source.getAliasName();
1806
+ if (aliasName) {
1807
+ identifiers.push(aliasName);
1808
+ }
1809
+ // Capture table identifiers so unaliased tables can still be matched.
1810
+ if (source.datasource instanceof Clause_1.TableSource) {
1811
+ const tableComponent = source.datasource.table.name;
1812
+ identifiers.push(tableComponent);
1813
+ const fullName = source.datasource.getSourceName();
1814
+ if (fullName && fullName !== tableComponent) {
1815
+ identifiers.push(fullName);
1816
+ }
1817
+ }
1818
+ return identifiers;
1819
+ }
1820
+ normalizeJoinConditionValue(condition, aliasOrder) {
1821
+ // Walk the value tree so every comparison within the JOIN predicate is inspected.
1822
+ const kind = condition.getKind();
1823
+ if (kind === ValueComponent_1.ParenExpression.kind) {
1824
+ const paren = condition;
1825
+ this.normalizeJoinConditionValue(paren.expression, aliasOrder);
1826
+ return;
1827
+ }
1828
+ if (kind === ValueComponent_1.BinaryExpression.kind) {
1829
+ const binary = condition;
1830
+ this.normalizeJoinConditionValue(binary.left, aliasOrder);
1831
+ this.normalizeJoinConditionValue(binary.right, aliasOrder);
1832
+ this.normalizeBinaryEquality(binary, aliasOrder);
1833
+ }
1834
+ }
1835
+ normalizeBinaryEquality(binary, aliasOrder) {
1836
+ // Only normalize simple equality comparisons, leaving other operators untouched.
1837
+ const operatorValue = binary.operator.value.toLowerCase();
1838
+ if (operatorValue !== '=') {
1839
+ return;
1840
+ }
1841
+ const leftOwner = this.resolveColumnOwner(binary.left);
1842
+ const rightOwner = this.resolveColumnOwner(binary.right);
1843
+ if (!leftOwner || !rightOwner || leftOwner === rightOwner) {
1844
+ return;
1845
+ }
1846
+ const leftOrder = aliasOrder.get(leftOwner);
1847
+ const rightOrder = aliasOrder.get(rightOwner);
1848
+ if (leftOrder === undefined || rightOrder === undefined) {
1849
+ return;
1850
+ }
1851
+ if (leftOrder > rightOrder) {
1852
+ // Swap operands so the earlier declared table appears on the left.
1853
+ const originalLeft = binary.left;
1854
+ binary.left = binary.right;
1855
+ binary.right = originalLeft;
1856
+ }
1857
+ }
1858
+ resolveColumnOwner(value) {
1859
+ var _a;
1860
+ const kind = value.getKind();
1861
+ if (kind === ValueComponent_1.ColumnReference.kind) {
1862
+ // Column references expose their qualifier namespace, which we normalize for lookups.
1863
+ const columnRef = value;
1864
+ const namespace = columnRef.getNamespace();
1865
+ if (!namespace) {
1866
+ return null;
1867
+ }
1868
+ const qualifier = namespace.includes('.') ? (_a = namespace.split('.').pop()) !== null && _a !== void 0 ? _a : '' : namespace;
1869
+ return qualifier.toLowerCase();
1870
+ }
1871
+ if (kind === ValueComponent_1.ParenExpression.kind) {
1872
+ return this.resolveColumnOwner(value.expression);
1873
+ }
1874
+ return null;
1875
+ }
1631
1876
  visitJoinUsingClause(arg) {
1632
1877
  const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.JoinUsingClause);
1633
1878
  token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'using'));
@@ -1806,14 +2051,12 @@ class SqlPrintTokenParser {
1806
2051
  const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.SimpleSelectQuery);
1807
2052
  // Handle positioned comments for SimpleSelectQuery (unified spec)
1808
2053
  if (arg.headerComments && arg.headerComments.length > 0) {
1809
- // Fallback to legacy headerComments if no positioned comments
1810
- // For smart comment style, treat headerComments as a single multi-line block
1811
- if (this.commentStyle === 'smart' && arg.headerComments.length > 1) {
2054
+ if (this.shouldMergeHeaderComments(arg.headerComments)) {
1812
2055
  const mergedHeaderComment = this.createHeaderMultiLineCommentBlock(arg.headerComments);
1813
2056
  token.innerTokens.push(mergedHeaderComment);
1814
2057
  }
1815
2058
  else {
1816
- const headerCommentBlocks = this.createCommentBlocks(arg.headerComments);
2059
+ const headerCommentBlocks = this.createCommentBlocks(arg.headerComments, true);
1817
2060
  token.innerTokens.push(...headerCommentBlocks);
1818
2061
  }
1819
2062
  if (arg.withClause) {
@@ -1894,11 +2137,18 @@ class SqlPrintTokenParser {
1894
2137
  const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'values', SqlPrintToken_1.SqlPrintTokenContainerType.ValuesQuery);
1895
2138
  // Add headerComments before VALUES keyword
1896
2139
  if (arg.headerComments && arg.headerComments.length > 0) {
1897
- const headerCommentBlocks = this.createCommentBlocks(arg.headerComments);
1898
- for (const commentBlock of headerCommentBlocks) {
1899
- token.innerTokens.push(commentBlock);
2140
+ if (this.shouldMergeHeaderComments(arg.headerComments)) {
2141
+ const mergedHeaderComment = this.createHeaderMultiLineCommentBlock(arg.headerComments);
2142
+ token.innerTokens.push(mergedHeaderComment);
1900
2143
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1901
2144
  }
2145
+ else {
2146
+ const headerCommentBlocks = this.createCommentBlocks(arg.headerComments, true);
2147
+ for (const commentBlock of headerCommentBlocks) {
2148
+ token.innerTokens.push(commentBlock);
2149
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2150
+ }
2151
+ }
1902
2152
  }
1903
2153
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1904
2154
  const values = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.Values);
@@ -1934,22 +2184,27 @@ class SqlPrintTokenParser {
1934
2184
  }
1935
2185
  visitInsertQuery(arg) {
1936
2186
  const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.InsertQuery);
1937
- // Process the insert clause
2187
+ if (arg.withClause) {
2188
+ token.innerTokens.push(arg.withClause.accept(this));
2189
+ }
1938
2190
  token.innerTokens.push(this.visit(arg.insertClause));
1939
2191
  // Process the select query if present
1940
2192
  if (arg.selectQuery) {
1941
2193
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1942
2194
  token.innerTokens.push(this.visit(arg.selectQuery));
1943
2195
  }
2196
+ if (arg.returningClause) {
2197
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2198
+ token.innerTokens.push(arg.returningClause.accept(this));
2199
+ }
1944
2200
  return token;
1945
2201
  }
1946
2202
  visitInsertClause(arg) {
1947
- const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '');
1948
- token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2203
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.InsertClause);
1949
2204
  token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'insert into'));
1950
2205
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1951
2206
  token.innerTokens.push(arg.source.accept(this));
1952
- if (arg.columns.length > 0) {
2207
+ if (arg.columns && arg.columns.length > 0) {
1953
2208
  token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
1954
2209
  for (let i = 0; i < arg.columns.length; i++) {
1955
2210
  if (i > 0) {
@@ -1961,14 +2216,184 @@ class SqlPrintTokenParser {
1961
2216
  }
1962
2217
  return token;
1963
2218
  }
1964
- visitUpdateQuery(arg) {
1965
- const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.UpdateQuery);
2219
+ visitDeleteQuery(arg) {
2220
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.DeleteQuery);
2221
+ // Attach WITH clause tokens when present before the DELETE command.
1966
2222
  if (arg.withClause) {
1967
2223
  token.innerTokens.push(arg.withClause.accept(this));
1968
2224
  }
1969
- token.innerTokens.push(arg.updateClause.accept(this));
1970
- token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1971
- token.innerTokens.push(arg.setClause.accept(this));
2225
+ token.innerTokens.push(arg.deleteClause.accept(this));
2226
+ // Append USING clause when the DELETE references additional sources.
2227
+ if (arg.usingClause) {
2228
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2229
+ token.innerTokens.push(arg.usingClause.accept(this));
2230
+ }
2231
+ // Append WHERE clause to restrict affected rows.
2232
+ if (arg.whereClause) {
2233
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2234
+ token.innerTokens.push(arg.whereClause.accept(this));
2235
+ }
2236
+ // Append RETURNING clause when the DELETE yields output columns.
2237
+ if (arg.returningClause) {
2238
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2239
+ token.innerTokens.push(arg.returningClause.accept(this));
2240
+ }
2241
+ return token;
2242
+ }
2243
+ visitDeleteClause(arg) {
2244
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'delete from', SqlPrintToken_1.SqlPrintTokenContainerType.DeleteClause);
2245
+ // Render the target relation immediately after the DELETE FROM keyword.
2246
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2247
+ token.innerTokens.push(arg.source.accept(this));
2248
+ return token;
2249
+ }
2250
+ visitUsingClause(arg) {
2251
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'using', SqlPrintToken_1.SqlPrintTokenContainerType.UsingClause);
2252
+ if (arg.sources.length > 0) {
2253
+ // Attach the first USING source directly after the keyword.
2254
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2255
+ for (let i = 0; i < arg.sources.length; i++) {
2256
+ if (i > 0) {
2257
+ // Separate subsequent sources with comma and space for clarity.
2258
+ token.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens());
2259
+ }
2260
+ token.innerTokens.push(this.visit(arg.sources[i]));
2261
+ }
2262
+ }
2263
+ return token;
2264
+ }
2265
+ visitMergeQuery(arg) {
2266
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.MergeQuery);
2267
+ if (arg.withClause) {
2268
+ token.innerTokens.push(arg.withClause.accept(this));
2269
+ }
2270
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'merge into'));
2271
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2272
+ token.innerTokens.push(arg.target.accept(this));
2273
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2274
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'using'));
2275
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2276
+ token.innerTokens.push(arg.source.accept(this));
2277
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2278
+ const onClauseToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.JoinOnClause);
2279
+ onClauseToken.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'on'));
2280
+ onClauseToken.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2281
+ onClauseToken.innerTokens.push(arg.onCondition.accept(this));
2282
+ token.innerTokens.push(onClauseToken);
2283
+ for (const clause of arg.whenClauses) {
2284
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2285
+ token.innerTokens.push(clause.accept(this));
2286
+ }
2287
+ return token;
2288
+ }
2289
+ visitMergeWhenClause(arg) {
2290
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.MergeWhenClause);
2291
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, this.mergeMatchTypeToKeyword(arg.matchType)));
2292
+ if (arg.condition) {
2293
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2294
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'and'));
2295
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2296
+ token.innerTokens.push(arg.condition.accept(this));
2297
+ }
2298
+ const thenLeadingComments = arg.getThenLeadingComments();
2299
+ const thenKeywordToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'then');
2300
+ if (thenLeadingComments.length > 0) {
2301
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2302
+ const commentBlocks = this.createCommentBlocks(thenLeadingComments);
2303
+ token.innerTokens.push(...commentBlocks);
2304
+ token.innerTokens.push(thenKeywordToken);
2305
+ }
2306
+ else {
2307
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2308
+ token.innerTokens.push(thenKeywordToken);
2309
+ }
2310
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2311
+ token.innerTokens.push(arg.action.accept(this));
2312
+ return token;
2313
+ }
2314
+ visitMergeUpdateAction(arg) {
2315
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.MergeUpdateAction);
2316
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'update'));
2317
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2318
+ token.innerTokens.push(arg.setClause.accept(this));
2319
+ if (arg.whereClause) {
2320
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2321
+ token.innerTokens.push(arg.whereClause.accept(this));
2322
+ }
2323
+ return token;
2324
+ }
2325
+ visitMergeDeleteAction(arg) {
2326
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.MergeDeleteAction);
2327
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'delete'));
2328
+ if (arg.whereClause) {
2329
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2330
+ token.innerTokens.push(arg.whereClause.accept(this));
2331
+ }
2332
+ return token;
2333
+ }
2334
+ visitMergeInsertAction(arg) {
2335
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.MergeInsertAction);
2336
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'insert'));
2337
+ if (arg.columns && arg.columns.length > 0) {
2338
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2339
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
2340
+ for (let i = 0; i < arg.columns.length; i++) {
2341
+ if (i > 0) {
2342
+ token.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens());
2343
+ }
2344
+ token.innerTokens.push(arg.columns[i].accept(this));
2345
+ }
2346
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
2347
+ }
2348
+ if (arg.defaultValues) {
2349
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2350
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'default values'));
2351
+ return token;
2352
+ }
2353
+ if (arg.values) {
2354
+ const leadingValuesComments = arg.getValuesLeadingComments();
2355
+ if (leadingValuesComments.length > 0) {
2356
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.commentNewline, ''));
2357
+ const commentBlocks = this.createCommentBlocks(leadingValuesComments);
2358
+ token.innerTokens.push(...commentBlocks);
2359
+ }
2360
+ else {
2361
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2362
+ }
2363
+ const valuesKeywordToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'values');
2364
+ token.innerTokens.push(valuesKeywordToken);
2365
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2366
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
2367
+ token.innerTokens.push(arg.values.accept(this));
2368
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
2369
+ }
2370
+ return token;
2371
+ }
2372
+ visitMergeDoNothingAction(_) {
2373
+ return new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'do nothing', SqlPrintToken_1.SqlPrintTokenContainerType.MergeDoNothingAction);
2374
+ }
2375
+ mergeMatchTypeToKeyword(matchType) {
2376
+ switch (matchType) {
2377
+ case 'matched':
2378
+ return 'when matched';
2379
+ case 'not_matched':
2380
+ return 'when not matched';
2381
+ case 'not_matched_by_source':
2382
+ return 'when not matched by source';
2383
+ case 'not_matched_by_target':
2384
+ return 'when not matched by target';
2385
+ default:
2386
+ return 'when';
2387
+ }
2388
+ }
2389
+ visitUpdateQuery(arg) {
2390
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.UpdateQuery);
2391
+ if (arg.withClause) {
2392
+ token.innerTokens.push(arg.withClause.accept(this));
2393
+ }
2394
+ token.innerTokens.push(arg.updateClause.accept(this));
2395
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2396
+ token.innerTokens.push(arg.setClause.accept(this));
1972
2397
  if (arg.fromClause) {
1973
2398
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1974
2399
  token.innerTokens.push(arg.fromClause.accept(this));
@@ -1990,7 +2415,7 @@ class SqlPrintTokenParser {
1990
2415
  return token;
1991
2416
  }
1992
2417
  visitSetClause(arg) {
1993
- const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'set', SqlPrintToken_1.SqlPrintTokenContainerType.SelectClause);
2418
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'set', SqlPrintToken_1.SqlPrintTokenContainerType.SetClause);
1994
2419
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1995
2420
  for (let i = 0; i < arg.items.length; i++) {
1996
2421
  if (i > 0) {
@@ -2021,15 +2446,481 @@ class SqlPrintTokenParser {
2021
2446
  return token;
2022
2447
  }
2023
2448
  visitCreateTableQuery(arg) {
2024
- const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, arg.isTemporary ? 'create temporary table' : 'create table', SqlPrintToken_1.SqlPrintTokenContainerType.CreateTableQuery);
2449
+ var _a;
2450
+ const baseKeyword = arg.isTemporary ? 'create temporary table' : 'create table';
2451
+ let keywordText = arg.ifNotExists ? `${baseKeyword} if not exists` : baseKeyword;
2452
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, keywordText, SqlPrintToken_1.SqlPrintTokenContainerType.CreateTableQuery);
2025
2453
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2026
- token.innerTokens.push(arg.tableName.accept(this));
2454
+ const qualifiedName = new ValueComponent_1.QualifiedName((_a = arg.namespaces) !== null && _a !== void 0 ? _a : null, arg.tableName);
2455
+ token.innerTokens.push(qualifiedName.accept(this));
2456
+ const definitionEntries = [...arg.columns, ...arg.tableConstraints];
2457
+ if (definitionEntries.length > 0) {
2458
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2459
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
2460
+ const definitionToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.CreateTableDefinition);
2461
+ for (let i = 0; i < definitionEntries.length; i++) {
2462
+ if (i > 0) {
2463
+ definitionToken.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens());
2464
+ }
2465
+ definitionToken.innerTokens.push(definitionEntries[i].accept(this));
2466
+ }
2467
+ token.innerTokens.push(definitionToken);
2468
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
2469
+ }
2470
+ if (arg.tableOptions) {
2471
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2472
+ token.innerTokens.push(arg.tableOptions.accept(this));
2473
+ }
2027
2474
  if (arg.asSelectQuery) {
2028
2475
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2029
2476
  token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'as'));
2030
2477
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2031
2478
  token.innerTokens.push(arg.asSelectQuery.accept(this));
2032
2479
  }
2480
+ if (arg.withDataOption) {
2481
+ // Reconstruct WITH [NO] DATA clause to mirror PostgreSQL CREATE TABLE semantics.
2482
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2483
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'with'));
2484
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2485
+ if (arg.withDataOption === 'with-no-data') {
2486
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'no'));
2487
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2488
+ }
2489
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'data'));
2490
+ }
2491
+ return token;
2492
+ }
2493
+ visitTableColumnDefinition(arg) {
2494
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.TableColumnDefinition);
2495
+ token.innerTokens.push(arg.name.accept(this));
2496
+ if (arg.dataType) {
2497
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2498
+ token.innerTokens.push(arg.dataType.accept(this));
2499
+ }
2500
+ for (const constraint of arg.constraints) {
2501
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2502
+ token.innerTokens.push(constraint.accept(this));
2503
+ }
2504
+ return token;
2505
+ }
2506
+ visitColumnConstraintDefinition(arg) {
2507
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.ColumnConstraintDefinition);
2508
+ if (arg.constraintName) {
2509
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'constraint'));
2510
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2511
+ token.innerTokens.push(arg.constraintName.accept(this));
2512
+ }
2513
+ const appendKeyword = (text) => {
2514
+ if (token.innerTokens.length > 0) {
2515
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2516
+ }
2517
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, text));
2518
+ };
2519
+ const appendComponent = (component) => {
2520
+ if (token.innerTokens.length > 0) {
2521
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2522
+ }
2523
+ token.innerTokens.push(component.accept(this));
2524
+ };
2525
+ switch (arg.kind) {
2526
+ case 'not-null':
2527
+ appendKeyword('not null');
2528
+ break;
2529
+ case 'null':
2530
+ appendKeyword('null');
2531
+ break;
2532
+ case 'default':
2533
+ appendKeyword('default');
2534
+ if (arg.defaultValue) {
2535
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2536
+ token.innerTokens.push(arg.defaultValue.accept(this));
2537
+ }
2538
+ break;
2539
+ case 'primary-key':
2540
+ appendKeyword('primary key');
2541
+ break;
2542
+ case 'unique':
2543
+ appendKeyword('unique');
2544
+ break;
2545
+ case 'references':
2546
+ if (arg.reference) {
2547
+ appendComponent(arg.reference);
2548
+ }
2549
+ break;
2550
+ case 'check':
2551
+ if (arg.checkExpression) {
2552
+ appendKeyword('check');
2553
+ token.innerTokens.push(this.wrapWithParenExpression(arg.checkExpression));
2554
+ }
2555
+ break;
2556
+ case 'generated-always-identity':
2557
+ case 'generated-by-default-identity':
2558
+ case 'raw':
2559
+ if (arg.rawClause) {
2560
+ appendComponent(arg.rawClause);
2561
+ }
2562
+ break;
2563
+ }
2564
+ return token;
2565
+ }
2566
+ visitTableConstraintDefinition(arg) {
2567
+ var _a, _b, _c;
2568
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.TableConstraintDefinition);
2569
+ const appendKeyword = (text) => {
2570
+ if (token.innerTokens.length > 0) {
2571
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2572
+ }
2573
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, text));
2574
+ };
2575
+ const appendComponent = (component) => {
2576
+ if (token.innerTokens.length > 0) {
2577
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2578
+ }
2579
+ token.innerTokens.push(component.accept(this));
2580
+ };
2581
+ const appendColumns = (columns) => {
2582
+ if (!columns || columns.length === 0) {
2583
+ return;
2584
+ }
2585
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2586
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
2587
+ const listToken = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.ValueList);
2588
+ for (let i = 0; i < columns.length; i++) {
2589
+ if (i > 0) {
2590
+ listToken.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens());
2591
+ }
2592
+ listToken.innerTokens.push(columns[i].accept(this));
2593
+ }
2594
+ token.innerTokens.push(listToken);
2595
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
2596
+ };
2597
+ const useMysqlConstraintStyle = this.constraintStyle === 'mysql';
2598
+ const inlineNameKinds = new Set(['primary-key', 'unique', 'foreign-key']);
2599
+ const shouldInlineConstraintName = useMysqlConstraintStyle && !!arg.constraintName && inlineNameKinds.has(arg.kind);
2600
+ if (arg.constraintName && !shouldInlineConstraintName) {
2601
+ appendKeyword('constraint');
2602
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2603
+ token.innerTokens.push(arg.constraintName.accept(this));
2604
+ }
2605
+ switch (arg.kind) {
2606
+ case 'primary-key':
2607
+ appendKeyword('primary key');
2608
+ if (shouldInlineConstraintName && arg.constraintName) {
2609
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2610
+ token.innerTokens.push(arg.constraintName.accept(this));
2611
+ }
2612
+ appendColumns((_a = arg.columns) !== null && _a !== void 0 ? _a : []);
2613
+ break;
2614
+ case 'unique':
2615
+ if (useMysqlConstraintStyle) {
2616
+ appendKeyword('unique key');
2617
+ if (shouldInlineConstraintName && arg.constraintName) {
2618
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2619
+ token.innerTokens.push(arg.constraintName.accept(this));
2620
+ }
2621
+ }
2622
+ else {
2623
+ appendKeyword('unique');
2624
+ }
2625
+ appendColumns((_b = arg.columns) !== null && _b !== void 0 ? _b : []);
2626
+ break;
2627
+ case 'foreign-key':
2628
+ appendKeyword('foreign key');
2629
+ if (shouldInlineConstraintName && arg.constraintName) {
2630
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2631
+ token.innerTokens.push(arg.constraintName.accept(this));
2632
+ }
2633
+ appendColumns((_c = arg.columns) !== null && _c !== void 0 ? _c : []);
2634
+ if (arg.reference) {
2635
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2636
+ token.innerTokens.push(arg.reference.accept(this));
2637
+ }
2638
+ break;
2639
+ case 'check':
2640
+ if (arg.checkExpression) {
2641
+ appendKeyword('check');
2642
+ token.innerTokens.push(this.wrapWithParenExpression(arg.checkExpression));
2643
+ }
2644
+ break;
2645
+ case 'raw':
2646
+ if (arg.rawClause) {
2647
+ appendComponent(arg.rawClause);
2648
+ }
2649
+ break;
2650
+ }
2651
+ return token;
2652
+ }
2653
+ wrapWithParenExpression(expression) {
2654
+ // Reuse existing parentheses groups to avoid double-wrapping when callers already provided them.
2655
+ if (expression instanceof ValueComponent_1.ParenExpression) {
2656
+ return this.visit(expression);
2657
+ }
2658
+ // Synthesize a ParenExpression wrapper so nested boolean groups render with consistent indentation.
2659
+ const synthetic = new ValueComponent_1.ParenExpression(expression);
2660
+ return this.visit(synthetic);
2661
+ }
2662
+ visitReferenceDefinition(arg) {
2663
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.ReferenceDefinition);
2664
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'references'));
2665
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2666
+ token.innerTokens.push(arg.targetTable.accept(this));
2667
+ if (arg.columns && arg.columns.length > 0) {
2668
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
2669
+ const columnList = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.ValueList);
2670
+ for (let i = 0; i < arg.columns.length; i++) {
2671
+ if (i > 0) {
2672
+ columnList.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens());
2673
+ }
2674
+ columnList.innerTokens.push(arg.columns[i].accept(this));
2675
+ }
2676
+ token.innerTokens.push(columnList);
2677
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
2678
+ }
2679
+ if (arg.matchType) {
2680
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2681
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, `match ${arg.matchType}`));
2682
+ }
2683
+ if (arg.onDelete) {
2684
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2685
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'on delete'));
2686
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2687
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, arg.onDelete));
2688
+ }
2689
+ if (arg.onUpdate) {
2690
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2691
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'on update'));
2692
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2693
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, arg.onUpdate));
2694
+ }
2695
+ if (arg.deferrable === 'deferrable') {
2696
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2697
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'deferrable'));
2698
+ }
2699
+ else if (arg.deferrable === 'not deferrable') {
2700
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2701
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'not deferrable'));
2702
+ }
2703
+ if (arg.initially === 'immediate') {
2704
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2705
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'initially immediate'));
2706
+ }
2707
+ else if (arg.initially === 'deferred') {
2708
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2709
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'initially deferred'));
2710
+ }
2711
+ return token;
2712
+ }
2713
+ visitCreateIndexStatement(arg) {
2714
+ const keywordParts = ['create'];
2715
+ if (arg.unique) {
2716
+ keywordParts.push('unique');
2717
+ }
2718
+ keywordParts.push('index');
2719
+ if (arg.concurrently) {
2720
+ keywordParts.push('concurrently');
2721
+ }
2722
+ if (arg.ifNotExists) {
2723
+ keywordParts.push('if not exists');
2724
+ }
2725
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, keywordParts.join(' '), SqlPrintToken_1.SqlPrintTokenContainerType.CreateIndexStatement);
2726
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2727
+ token.innerTokens.push(arg.indexName.accept(this));
2728
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2729
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'on'));
2730
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2731
+ token.innerTokens.push(arg.tableName.accept(this));
2732
+ if (arg.usingMethod) {
2733
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2734
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'using'));
2735
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2736
+ token.innerTokens.push(arg.usingMethod.accept(this));
2737
+ }
2738
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2739
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
2740
+ const columnList = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.IndexColumnList);
2741
+ for (let i = 0; i < arg.columns.length; i++) {
2742
+ if (i > 0) {
2743
+ columnList.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens());
2744
+ }
2745
+ columnList.innerTokens.push(arg.columns[i].accept(this));
2746
+ }
2747
+ token.innerTokens.push(columnList);
2748
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
2749
+ if (arg.include && arg.include.length > 0) {
2750
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2751
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'include'));
2752
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2753
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
2754
+ const includeList = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.ValueList);
2755
+ for (let i = 0; i < arg.include.length; i++) {
2756
+ if (i > 0) {
2757
+ includeList.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens());
2758
+ }
2759
+ includeList.innerTokens.push(arg.include[i].accept(this));
2760
+ }
2761
+ token.innerTokens.push(includeList);
2762
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
2763
+ }
2764
+ if (arg.withOptions) {
2765
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2766
+ token.innerTokens.push(arg.withOptions.accept(this));
2767
+ }
2768
+ if (arg.tablespace) {
2769
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2770
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'tablespace'));
2771
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2772
+ token.innerTokens.push(arg.tablespace.accept(this));
2773
+ }
2774
+ if (arg.where) {
2775
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2776
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'where'));
2777
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2778
+ token.innerTokens.push(this.visit(arg.where));
2779
+ }
2780
+ return token;
2781
+ }
2782
+ visitIndexColumnDefinition(arg) {
2783
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.IndexColumnDefinition);
2784
+ token.innerTokens.push(this.visit(arg.expression));
2785
+ if (arg.collation) {
2786
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2787
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'collate'));
2788
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2789
+ token.innerTokens.push(arg.collation.accept(this));
2790
+ }
2791
+ if (arg.operatorClass) {
2792
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2793
+ token.innerTokens.push(arg.operatorClass.accept(this));
2794
+ }
2795
+ if (arg.sortOrder) {
2796
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2797
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, arg.sortOrder));
2798
+ }
2799
+ if (arg.nullsOrder) {
2800
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2801
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, `nulls ${arg.nullsOrder}`));
2802
+ }
2803
+ return token;
2804
+ }
2805
+ visitDropTableStatement(arg) {
2806
+ const keyword = arg.ifExists ? 'drop table if exists' : 'drop table';
2807
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, keyword, SqlPrintToken_1.SqlPrintTokenContainerType.DropTableStatement);
2808
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2809
+ const tableList = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.ValueList);
2810
+ for (let i = 0; i < arg.tables.length; i++) {
2811
+ if (i > 0) {
2812
+ tableList.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens());
2813
+ }
2814
+ tableList.innerTokens.push(arg.tables[i].accept(this));
2815
+ }
2816
+ token.innerTokens.push(tableList);
2817
+ if (arg.behavior) {
2818
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2819
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, arg.behavior));
2820
+ }
2821
+ return token;
2822
+ }
2823
+ visitDropIndexStatement(arg) {
2824
+ const keywordParts = ['drop', 'index'];
2825
+ if (arg.concurrently) {
2826
+ keywordParts.push('concurrently');
2827
+ }
2828
+ if (arg.ifExists) {
2829
+ keywordParts.push('if exists');
2830
+ }
2831
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, keywordParts.join(' '), SqlPrintToken_1.SqlPrintTokenContainerType.DropIndexStatement);
2832
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2833
+ const indexList = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.ValueList);
2834
+ for (let i = 0; i < arg.indexNames.length; i++) {
2835
+ if (i > 0) {
2836
+ indexList.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens());
2837
+ }
2838
+ indexList.innerTokens.push(arg.indexNames[i].accept(this));
2839
+ }
2840
+ token.innerTokens.push(indexList);
2841
+ if (arg.behavior) {
2842
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2843
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, arg.behavior));
2844
+ }
2845
+ return token;
2846
+ }
2847
+ visitAlterTableStatement(arg) {
2848
+ const keywordParts = ['alter', 'table'];
2849
+ if (arg.ifExists) {
2850
+ keywordParts.push('if exists');
2851
+ }
2852
+ if (arg.only) {
2853
+ keywordParts.push('only');
2854
+ }
2855
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, keywordParts.join(' '), SqlPrintToken_1.SqlPrintTokenContainerType.AlterTableStatement);
2856
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2857
+ token.innerTokens.push(arg.table.accept(this));
2858
+ for (let i = 0; i < arg.actions.length; i++) {
2859
+ if (i === 0) {
2860
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2861
+ }
2862
+ else {
2863
+ token.innerTokens.push(SqlPrintTokenParser.COMMA_TOKEN);
2864
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2865
+ }
2866
+ token.innerTokens.push(arg.actions[i].accept(this));
2867
+ }
2868
+ return token;
2869
+ }
2870
+ visitAlterTableAddConstraint(arg) {
2871
+ const keyword = arg.ifNotExists ? 'add if not exists' : 'add';
2872
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.AlterTableAddConstraint);
2873
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, keyword));
2874
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2875
+ token.innerTokens.push(arg.constraint.accept(this));
2876
+ if (arg.notValid) {
2877
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2878
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, 'not valid'));
2879
+ }
2880
+ return token;
2881
+ }
2882
+ visitAlterTableDropConstraint(arg) {
2883
+ let keyword = 'drop constraint';
2884
+ if (arg.ifExists) {
2885
+ keyword += ' if exists';
2886
+ }
2887
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.AlterTableDropConstraint);
2888
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, keyword));
2889
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2890
+ token.innerTokens.push(arg.constraintName.accept(this));
2891
+ if (arg.behavior) {
2892
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2893
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, arg.behavior));
2894
+ }
2895
+ return token;
2896
+ }
2897
+ visitAlterTableDropColumn(arg) {
2898
+ let keyword = 'drop column';
2899
+ if (arg.ifExists) {
2900
+ keyword += ' if exists';
2901
+ }
2902
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.container, '', SqlPrintToken_1.SqlPrintTokenContainerType.AlterTableDropColumn);
2903
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, keyword));
2904
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2905
+ token.innerTokens.push(arg.columnName.accept(this));
2906
+ if (arg.behavior) {
2907
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2908
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, arg.behavior));
2909
+ }
2910
+ return token;
2911
+ }
2912
+ visitDropConstraintStatement(arg) {
2913
+ let keyword = 'drop constraint';
2914
+ if (arg.ifExists) {
2915
+ keyword += ' if exists';
2916
+ }
2917
+ const token = new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, keyword, SqlPrintToken_1.SqlPrintTokenContainerType.DropConstraintStatement);
2918
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2919
+ token.innerTokens.push(arg.constraintName.accept(this));
2920
+ if (arg.behavior) {
2921
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2922
+ token.innerTokens.push(new SqlPrintToken_1.SqlPrintToken(SqlPrintToken_1.SqlPrintTokenType.keyword, arg.behavior));
2923
+ }
2033
2924
  return token;
2034
2925
  }
2035
2926
  }