rawsql-ts 0.11.43-beta → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (294) hide show
  1. package/README.md +12 -12
  2. package/dist/esm/index.js +18 -0
  3. package/dist/esm/index.js.map +1 -1
  4. package/dist/esm/index.min.js +15 -19
  5. package/dist/esm/index.min.js.map +4 -4
  6. package/dist/esm/src/index.d.ts +18 -0
  7. package/dist/esm/src/index.js +18 -0
  8. package/dist/esm/src/index.js.map +1 -1
  9. package/dist/esm/src/models/BinarySelectQuery.d.ts +25 -1
  10. package/dist/esm/src/models/BinarySelectQuery.js +28 -0
  11. package/dist/esm/src/models/BinarySelectQuery.js.map +1 -1
  12. package/dist/esm/src/models/Clause.d.ts +14 -2
  13. package/dist/esm/src/models/Clause.js +26 -1
  14. package/dist/esm/src/models/Clause.js.map +1 -1
  15. package/dist/esm/src/models/CreateTableQuery.d.ts +99 -5
  16. package/dist/esm/src/models/CreateTableQuery.js +85 -10
  17. package/dist/esm/src/models/CreateTableQuery.js.map +1 -1
  18. package/dist/esm/src/models/DDLStatements.d.ts +195 -0
  19. package/dist/esm/src/models/DDLStatements.js +201 -0
  20. package/dist/esm/src/models/DDLStatements.js.map +1 -0
  21. package/dist/esm/src/models/DeleteQuery.d.ts +17 -0
  22. package/dist/esm/src/models/DeleteQuery.js +16 -0
  23. package/dist/esm/src/models/DeleteQuery.js.map +1 -0
  24. package/dist/esm/src/models/InsertQuery.d.ts +7 -1
  25. package/dist/esm/src/models/InsertQuery.js +6 -2
  26. package/dist/esm/src/models/InsertQuery.js.map +1 -1
  27. package/dist/esm/src/models/MergeQuery.d.ts +63 -0
  28. package/dist/esm/src/models/MergeQuery.js +94 -0
  29. package/dist/esm/src/models/MergeQuery.js.map +1 -0
  30. package/dist/esm/src/models/SelectQuery.d.ts +37 -1
  31. package/dist/esm/src/models/SelectQuery.js +4 -1
  32. package/dist/esm/src/models/SelectQuery.js.map +1 -1
  33. package/dist/esm/src/models/SimpleSelectQuery.d.ts +29 -1
  34. package/dist/esm/src/models/SimpleSelectQuery.js +32 -0
  35. package/dist/esm/src/models/SimpleSelectQuery.js.map +1 -1
  36. package/dist/esm/src/models/SqlComponent.d.ts +2 -1
  37. package/dist/esm/src/models/SqlComponent.js +1 -1
  38. package/dist/esm/src/models/SqlComponent.js.map +1 -1
  39. package/dist/esm/src/models/SqlPrintToken.d.ts +36 -0
  40. package/dist/esm/src/models/SqlPrintToken.js +35 -0
  41. package/dist/esm/src/models/SqlPrintToken.js.map +1 -1
  42. package/dist/esm/src/models/ValuesQuery.d.ts +25 -1
  43. package/dist/esm/src/models/ValuesQuery.js +28 -0
  44. package/dist/esm/src/models/ValuesQuery.js.map +1 -1
  45. package/dist/esm/src/parsers/AlterTableParser.d.ts +25 -0
  46. package/dist/esm/src/parsers/AlterTableParser.js +428 -0
  47. package/dist/esm/src/parsers/AlterTableParser.js.map +1 -0
  48. package/dist/esm/src/parsers/AnalyzeStatementParser.d.ts +13 -0
  49. package/dist/esm/src/parsers/AnalyzeStatementParser.js +90 -0
  50. package/dist/esm/src/parsers/AnalyzeStatementParser.js.map +1 -0
  51. package/dist/esm/src/parsers/CreateIndexParser.d.ts +16 -0
  52. package/dist/esm/src/parsers/CreateIndexParser.js +237 -0
  53. package/dist/esm/src/parsers/CreateIndexParser.js.map +1 -0
  54. package/dist/esm/src/parsers/CreateTableParser.d.ts +41 -0
  55. package/dist/esm/src/parsers/CreateTableParser.js +734 -0
  56. package/dist/esm/src/parsers/CreateTableParser.js.map +1 -0
  57. package/dist/esm/src/parsers/DeleteClauseParser.d.ts +11 -0
  58. package/dist/esm/src/parsers/DeleteClauseParser.js +33 -0
  59. package/dist/esm/src/parsers/DeleteClauseParser.js.map +1 -0
  60. package/dist/esm/src/parsers/DeleteQueryParser.d.ts +16 -0
  61. package/dist/esm/src/parsers/DeleteQueryParser.js +73 -0
  62. package/dist/esm/src/parsers/DeleteQueryParser.js.map +1 -0
  63. package/dist/esm/src/parsers/DropConstraintParser.d.ts +12 -0
  64. package/dist/esm/src/parsers/DropConstraintParser.js +47 -0
  65. package/dist/esm/src/parsers/DropConstraintParser.js.map +1 -0
  66. package/dist/esm/src/parsers/DropIndexParser.d.ts +12 -0
  67. package/dist/esm/src/parsers/DropIndexParser.js +69 -0
  68. package/dist/esm/src/parsers/DropIndexParser.js.map +1 -0
  69. package/dist/esm/src/parsers/DropTableParser.d.ts +12 -0
  70. package/dist/esm/src/parsers/DropTableParser.js +59 -0
  71. package/dist/esm/src/parsers/DropTableParser.js.map +1 -0
  72. package/dist/esm/src/parsers/ExplainStatementParser.d.ts +23 -0
  73. package/dist/esm/src/parsers/ExplainStatementParser.js +185 -0
  74. package/dist/esm/src/parsers/ExplainStatementParser.js.map +1 -0
  75. package/dist/esm/src/parsers/FunctionExpressionParser.d.ts +4 -0
  76. package/dist/esm/src/parsers/FunctionExpressionParser.js +25 -8
  77. package/dist/esm/src/parsers/FunctionExpressionParser.js.map +1 -1
  78. package/dist/esm/src/parsers/InsertQueryParser.js +103 -31
  79. package/dist/esm/src/parsers/InsertQueryParser.js.map +1 -1
  80. package/dist/esm/src/parsers/MergeQueryParser.d.ts +26 -0
  81. package/dist/esm/src/parsers/MergeQueryParser.js +479 -0
  82. package/dist/esm/src/parsers/MergeQueryParser.js.map +1 -0
  83. package/dist/esm/src/parsers/ParenExpressionParser.js +25 -0
  84. package/dist/esm/src/parsers/ParenExpressionParser.js.map +1 -1
  85. package/dist/esm/src/parsers/ReturningClauseParser.js +50 -7
  86. package/dist/esm/src/parsers/ReturningClauseParser.js.map +1 -1
  87. package/dist/esm/src/parsers/SelectClauseParser.js +3 -3
  88. package/dist/esm/src/parsers/SelectClauseParser.js.map +1 -1
  89. package/dist/esm/src/parsers/SelectQueryParser.d.ts +4 -0
  90. package/dist/esm/src/parsers/SelectQueryParser.js +4 -0
  91. package/dist/esm/src/parsers/SelectQueryParser.js.map +1 -1
  92. package/dist/esm/src/parsers/SetClauseParser.js +97 -15
  93. package/dist/esm/src/parsers/SetClauseParser.js.map +1 -1
  94. package/dist/esm/src/parsers/SqlParser.d.ts +40 -0
  95. package/dist/esm/src/parsers/SqlParser.js +400 -0
  96. package/dist/esm/src/parsers/SqlParser.js.map +1 -0
  97. package/dist/esm/src/parsers/SqlPrintTokenParser.d.ts +65 -3
  98. package/dist/esm/src/parsers/SqlPrintTokenParser.js +1145 -38
  99. package/dist/esm/src/parsers/SqlPrintTokenParser.js.map +1 -1
  100. package/dist/esm/src/parsers/SqlTokenizer.d.ts +24 -2
  101. package/dist/esm/src/parsers/SqlTokenizer.js +135 -74
  102. package/dist/esm/src/parsers/SqlTokenizer.js.map +1 -1
  103. package/dist/esm/src/parsers/UpdateQueryParser.js +11 -1
  104. package/dist/esm/src/parsers/UpdateQueryParser.js.map +1 -1
  105. package/dist/esm/src/parsers/UsingClauseParser.d.ts +11 -0
  106. package/dist/esm/src/parsers/UsingClauseParser.js +29 -0
  107. package/dist/esm/src/parsers/UsingClauseParser.js.map +1 -0
  108. package/dist/esm/src/parsers/ValueParser.js +5 -1
  109. package/dist/esm/src/parsers/ValueParser.js.map +1 -1
  110. package/dist/esm/src/parsers/ValuesQueryParser.d.ts +0 -2
  111. package/dist/esm/src/parsers/ValuesQueryParser.js +5 -45
  112. package/dist/esm/src/parsers/ValuesQueryParser.js.map +1 -1
  113. package/dist/esm/src/parsers/utils/LexemeCommentUtils.d.ts +6 -0
  114. package/dist/esm/src/parsers/utils/LexemeCommentUtils.js +26 -0
  115. package/dist/esm/src/parsers/utils/LexemeCommentUtils.js.map +1 -0
  116. package/dist/esm/src/tokenReaders/CommandTokenReader.js +50 -2
  117. package/dist/esm/src/tokenReaders/CommandTokenReader.js.map +1 -1
  118. package/dist/esm/src/tokenReaders/LiteralTokenReader.js +8 -5
  119. package/dist/esm/src/tokenReaders/LiteralTokenReader.js.map +1 -1
  120. package/dist/esm/src/tokenReaders/OperatorTokenReader.js +10 -1
  121. package/dist/esm/src/tokenReaders/OperatorTokenReader.js.map +1 -1
  122. package/dist/esm/src/tokenReaders/TypeTokenReader.js +11 -1
  123. package/dist/esm/src/tokenReaders/TypeTokenReader.js.map +1 -1
  124. package/dist/esm/src/transformers/InsertQuerySelectValuesConverter.d.ts +18 -0
  125. package/dist/esm/src/transformers/InsertQuerySelectValuesConverter.js +118 -0
  126. package/dist/esm/src/transformers/InsertQuerySelectValuesConverter.js.map +1 -0
  127. package/dist/esm/src/transformers/LinePrinter.d.ts +1 -0
  128. package/dist/esm/src/transformers/LinePrinter.js +12 -0
  129. package/dist/esm/src/transformers/LinePrinter.js.map +1 -1
  130. package/dist/esm/src/transformers/OnelineFormattingHelper.d.ts +29 -0
  131. package/dist/esm/src/transformers/OnelineFormattingHelper.js +95 -0
  132. package/dist/esm/src/transformers/OnelineFormattingHelper.js.map +1 -0
  133. package/dist/esm/src/transformers/QueryBuilder.d.ts +47 -13
  134. package/dist/esm/src/transformers/QueryBuilder.js +424 -62
  135. package/dist/esm/src/transformers/QueryBuilder.js.map +1 -1
  136. package/dist/esm/src/transformers/SqlFormatter.d.ts +13 -1
  137. package/dist/esm/src/transformers/SqlFormatter.js +13 -4
  138. package/dist/esm/src/transformers/SqlFormatter.js.map +1 -1
  139. package/dist/esm/src/transformers/SqlPrinter.d.ts +47 -8
  140. package/dist/esm/src/transformers/SqlPrinter.js +625 -72
  141. package/dist/esm/src/transformers/SqlPrinter.js.map +1 -1
  142. package/dist/esm/src/types/Formatting.d.ts +8 -0
  143. package/dist/esm/src/types/Formatting.js +2 -0
  144. package/dist/esm/src/types/Formatting.js.map +1 -0
  145. package/dist/esm/src/utils/ParserStringUtils.d.ts +6 -0
  146. package/dist/esm/src/utils/ParserStringUtils.js +28 -0
  147. package/dist/esm/src/utils/ParserStringUtils.js.map +1 -0
  148. package/dist/esm/tsconfig.browser.tsbuildinfo +1 -1
  149. package/dist/index.min.js +14 -18
  150. package/dist/index.min.js.map +4 -4
  151. package/dist/src/index.d.ts +18 -0
  152. package/dist/src/index.js +18 -0
  153. package/dist/src/index.js.map +1 -1
  154. package/dist/src/models/BinarySelectQuery.d.ts +25 -1
  155. package/dist/src/models/BinarySelectQuery.js +28 -0
  156. package/dist/src/models/BinarySelectQuery.js.map +1 -1
  157. package/dist/src/models/Clause.d.ts +14 -2
  158. package/dist/src/models/Clause.js +29 -2
  159. package/dist/src/models/Clause.js.map +1 -1
  160. package/dist/src/models/CreateTableQuery.d.ts +99 -5
  161. package/dist/src/models/CreateTableQuery.js +90 -11
  162. package/dist/src/models/CreateTableQuery.js.map +1 -1
  163. package/dist/src/models/DDLStatements.d.ts +195 -0
  164. package/dist/src/models/DDLStatements.js +216 -0
  165. package/dist/src/models/DDLStatements.js.map +1 -0
  166. package/dist/src/models/DeleteQuery.d.ts +17 -0
  167. package/dist/src/models/DeleteQuery.js +20 -0
  168. package/dist/src/models/DeleteQuery.js.map +1 -0
  169. package/dist/src/models/InsertQuery.d.ts +7 -1
  170. package/dist/src/models/InsertQuery.js +6 -2
  171. package/dist/src/models/InsertQuery.js.map +1 -1
  172. package/dist/src/models/MergeQuery.d.ts +63 -0
  173. package/dist/src/models/MergeQuery.js +104 -0
  174. package/dist/src/models/MergeQuery.js.map +1 -0
  175. package/dist/src/models/SelectQuery.d.ts +37 -1
  176. package/dist/src/models/SelectQuery.js +7 -1
  177. package/dist/src/models/SelectQuery.js.map +1 -1
  178. package/dist/src/models/SimpleSelectQuery.d.ts +29 -1
  179. package/dist/src/models/SimpleSelectQuery.js +32 -0
  180. package/dist/src/models/SimpleSelectQuery.js.map +1 -1
  181. package/dist/src/models/SqlComponent.d.ts +2 -1
  182. package/dist/src/models/SqlComponent.js +1 -1
  183. package/dist/src/models/SqlComponent.js.map +1 -1
  184. package/dist/src/models/SqlPrintToken.d.ts +36 -0
  185. package/dist/src/models/SqlPrintToken.js +35 -0
  186. package/dist/src/models/SqlPrintToken.js.map +1 -1
  187. package/dist/src/models/ValuesQuery.d.ts +25 -1
  188. package/dist/src/models/ValuesQuery.js +28 -0
  189. package/dist/src/models/ValuesQuery.js.map +1 -1
  190. package/dist/src/parsers/AlterTableParser.d.ts +25 -0
  191. package/dist/src/parsers/AlterTableParser.js +432 -0
  192. package/dist/src/parsers/AlterTableParser.js.map +1 -0
  193. package/dist/src/parsers/AnalyzeStatementParser.d.ts +13 -0
  194. package/dist/src/parsers/AnalyzeStatementParser.js +94 -0
  195. package/dist/src/parsers/AnalyzeStatementParser.js.map +1 -0
  196. package/dist/src/parsers/CreateIndexParser.d.ts +16 -0
  197. package/dist/src/parsers/CreateIndexParser.js +241 -0
  198. package/dist/src/parsers/CreateIndexParser.js.map +1 -0
  199. package/dist/src/parsers/CreateTableParser.d.ts +41 -0
  200. package/dist/src/parsers/CreateTableParser.js +738 -0
  201. package/dist/src/parsers/CreateTableParser.js.map +1 -0
  202. package/dist/src/parsers/DeleteClauseParser.d.ts +11 -0
  203. package/dist/src/parsers/DeleteClauseParser.js +37 -0
  204. package/dist/src/parsers/DeleteClauseParser.js.map +1 -0
  205. package/dist/src/parsers/DeleteQueryParser.d.ts +16 -0
  206. package/dist/src/parsers/DeleteQueryParser.js +77 -0
  207. package/dist/src/parsers/DeleteQueryParser.js.map +1 -0
  208. package/dist/src/parsers/DropConstraintParser.d.ts +12 -0
  209. package/dist/src/parsers/DropConstraintParser.js +51 -0
  210. package/dist/src/parsers/DropConstraintParser.js.map +1 -0
  211. package/dist/src/parsers/DropIndexParser.d.ts +12 -0
  212. package/dist/src/parsers/DropIndexParser.js +73 -0
  213. package/dist/src/parsers/DropIndexParser.js.map +1 -0
  214. package/dist/src/parsers/DropTableParser.d.ts +12 -0
  215. package/dist/src/parsers/DropTableParser.js +63 -0
  216. package/dist/src/parsers/DropTableParser.js.map +1 -0
  217. package/dist/src/parsers/ExplainStatementParser.d.ts +23 -0
  218. package/dist/src/parsers/ExplainStatementParser.js +189 -0
  219. package/dist/src/parsers/ExplainStatementParser.js.map +1 -0
  220. package/dist/src/parsers/FunctionExpressionParser.d.ts +4 -0
  221. package/dist/src/parsers/FunctionExpressionParser.js +25 -8
  222. package/dist/src/parsers/FunctionExpressionParser.js.map +1 -1
  223. package/dist/src/parsers/InsertQueryParser.js +103 -31
  224. package/dist/src/parsers/InsertQueryParser.js.map +1 -1
  225. package/dist/src/parsers/MergeQueryParser.d.ts +26 -0
  226. package/dist/src/parsers/MergeQueryParser.js +483 -0
  227. package/dist/src/parsers/MergeQueryParser.js.map +1 -0
  228. package/dist/src/parsers/ParenExpressionParser.js +25 -0
  229. package/dist/src/parsers/ParenExpressionParser.js.map +1 -1
  230. package/dist/src/parsers/ReturningClauseParser.js +50 -7
  231. package/dist/src/parsers/ReturningClauseParser.js.map +1 -1
  232. package/dist/src/parsers/SelectClauseParser.js +2 -2
  233. package/dist/src/parsers/SelectClauseParser.js.map +1 -1
  234. package/dist/src/parsers/SelectQueryParser.d.ts +4 -0
  235. package/dist/src/parsers/SelectQueryParser.js +4 -0
  236. package/dist/src/parsers/SelectQueryParser.js.map +1 -1
  237. package/dist/src/parsers/SetClauseParser.js +97 -15
  238. package/dist/src/parsers/SetClauseParser.js.map +1 -1
  239. package/dist/src/parsers/SqlParser.d.ts +40 -0
  240. package/dist/src/parsers/SqlParser.js +409 -0
  241. package/dist/src/parsers/SqlParser.js.map +1 -0
  242. package/dist/src/parsers/SqlPrintTokenParser.d.ts +65 -3
  243. package/dist/src/parsers/SqlPrintTokenParser.js +1143 -36
  244. package/dist/src/parsers/SqlPrintTokenParser.js.map +1 -1
  245. package/dist/src/parsers/SqlTokenizer.d.ts +24 -2
  246. package/dist/src/parsers/SqlTokenizer.js +139 -74
  247. package/dist/src/parsers/SqlTokenizer.js.map +1 -1
  248. package/dist/src/parsers/UpdateQueryParser.js +11 -1
  249. package/dist/src/parsers/UpdateQueryParser.js.map +1 -1
  250. package/dist/src/parsers/UsingClauseParser.d.ts +11 -0
  251. package/dist/src/parsers/UsingClauseParser.js +33 -0
  252. package/dist/src/parsers/UsingClauseParser.js.map +1 -0
  253. package/dist/src/parsers/ValueParser.js +5 -1
  254. package/dist/src/parsers/ValueParser.js.map +1 -1
  255. package/dist/src/parsers/ValuesQueryParser.d.ts +0 -2
  256. package/dist/src/parsers/ValuesQueryParser.js +5 -45
  257. package/dist/src/parsers/ValuesQueryParser.js.map +1 -1
  258. package/dist/src/parsers/utils/LexemeCommentUtils.d.ts +6 -0
  259. package/dist/src/parsers/utils/LexemeCommentUtils.js +29 -0
  260. package/dist/src/parsers/utils/LexemeCommentUtils.js.map +1 -0
  261. package/dist/src/tokenReaders/CommandTokenReader.js +50 -2
  262. package/dist/src/tokenReaders/CommandTokenReader.js.map +1 -1
  263. package/dist/src/tokenReaders/LiteralTokenReader.js +8 -5
  264. package/dist/src/tokenReaders/LiteralTokenReader.js.map +1 -1
  265. package/dist/src/tokenReaders/OperatorTokenReader.js +10 -1
  266. package/dist/src/tokenReaders/OperatorTokenReader.js.map +1 -1
  267. package/dist/src/tokenReaders/TypeTokenReader.js +11 -1
  268. package/dist/src/tokenReaders/TypeTokenReader.js.map +1 -1
  269. package/dist/src/transformers/InsertQuerySelectValuesConverter.d.ts +18 -0
  270. package/dist/src/transformers/InsertQuerySelectValuesConverter.js +122 -0
  271. package/dist/src/transformers/InsertQuerySelectValuesConverter.js.map +1 -0
  272. package/dist/src/transformers/LinePrinter.d.ts +1 -0
  273. package/dist/src/transformers/LinePrinter.js +12 -0
  274. package/dist/src/transformers/LinePrinter.js.map +1 -1
  275. package/dist/src/transformers/OnelineFormattingHelper.d.ts +29 -0
  276. package/dist/src/transformers/OnelineFormattingHelper.js +99 -0
  277. package/dist/src/transformers/OnelineFormattingHelper.js.map +1 -0
  278. package/dist/src/transformers/QueryBuilder.d.ts +47 -13
  279. package/dist/src/transformers/QueryBuilder.js +433 -60
  280. package/dist/src/transformers/QueryBuilder.js.map +1 -1
  281. package/dist/src/transformers/SqlFormatter.d.ts +13 -1
  282. package/dist/src/transformers/SqlFormatter.js +20 -5
  283. package/dist/src/transformers/SqlFormatter.js.map +1 -1
  284. package/dist/src/transformers/SqlPrinter.d.ts +47 -8
  285. package/dist/src/transformers/SqlPrinter.js +625 -72
  286. package/dist/src/transformers/SqlPrinter.js.map +1 -1
  287. package/dist/src/types/Formatting.d.ts +8 -0
  288. package/dist/src/types/Formatting.js +3 -0
  289. package/dist/src/types/Formatting.js.map +1 -0
  290. package/dist/src/utils/ParserStringUtils.d.ts +6 -0
  291. package/dist/src/utils/ParserStringUtils.js +31 -0
  292. package/dist/src/utils/ParserStringUtils.js.map +1 -0
  293. package/dist/tsconfig.tsbuildinfo +1 -1
  294. package/package.json +3 -3
@@ -1,4 +1,4 @@
1
- import { PartitionByClause, OrderByClause, OrderByItem, SelectClause, SelectItem, Distinct, DistinctOn, SortDirection, NullsSortDirection, TableSource, SourceExpression, FromClause, JoinClause, JoinOnClause, JoinUsingClause, FunctionSource, SourceAliasExpression, WhereClause, GroupByClause, HavingClause, SubQuerySource, WindowFrameClause, LimitClause, ForClause, OffsetClause, WindowsClause as WindowClause, CommonTable, WithClause, FetchClause, FetchExpression, InsertClause, UpdateClause, SetClause, ReturningClause, SetClauseItem } from "../models/Clause";
1
+ import { PartitionByClause, OrderByClause, OrderByItem, SelectClause, SelectItem, Distinct, DistinctOn, SortDirection, NullsSortDirection, TableSource, SourceExpression, FromClause, JoinClause, JoinOnClause, JoinUsingClause, FunctionSource, SourceAliasExpression, WhereClause, GroupByClause, HavingClause, SubQuerySource, WindowFrameClause, LimitClause, ForClause, OffsetClause, WindowsClause as WindowClause, CommonTable, WithClause, FetchClause, FetchExpression, InsertClause, UpdateClause, DeleteClause, UsingClause, SetClause, ReturningClause, SetClauseItem } from "../models/Clause";
2
2
  import { HintClause } from "../models/HintClause";
3
3
  import { BinarySelectQuery, SimpleSelectQuery, ValuesQuery } from "../models/SelectQuery";
4
4
  import { SqlPrintToken, SqlPrintTokenType, SqlPrintTokenContainerType } from "../models/SqlPrintToken";
@@ -8,7 +8,10 @@ import { IdentifierDecorator } from "./IdentifierDecorator";
8
8
  import { ParameterDecorator } from "./ParameterDecorator";
9
9
  import { InsertQuery } from "../models/InsertQuery";
10
10
  import { UpdateQuery } from "../models/UpdateQuery";
11
- import { CreateTableQuery } from "../models/CreateTableQuery";
11
+ import { DeleteQuery } from "../models/DeleteQuery";
12
+ import { CreateTableQuery, TableColumnDefinition, ColumnConstraintDefinition, TableConstraintDefinition, ReferenceDefinition } from "../models/CreateTableQuery";
13
+ import { MergeQuery, MergeWhenClause, MergeUpdateAction, MergeDeleteAction, MergeInsertAction, MergeDoNothingAction } from "../models/MergeQuery";
14
+ import { DropTableStatement, DropIndexStatement, CreateIndexStatement, IndexColumnDefinition, AlterTableStatement, AlterTableAddConstraint, AlterTableDropConstraint, AlterTableDropColumn, DropConstraintStatement, ExplainStatement, AnalyzeStatement } from "../models/DDLStatements";
12
15
  export var ParameterStyle;
13
16
  (function (ParameterStyle) {
14
17
  ParameterStyle["Anonymous"] = "anonymous";
@@ -20,36 +23,45 @@ export const PRESETS = {
20
23
  identifierEscape: { start: '`', end: '`' },
21
24
  parameterSymbol: '?',
22
25
  parameterStyle: ParameterStyle.Anonymous,
26
+ constraintStyle: 'mysql',
23
27
  },
24
28
  postgres: {
25
29
  identifierEscape: { start: '"', end: '"' },
26
30
  parameterSymbol: '$',
27
31
  parameterStyle: ParameterStyle.Indexed,
32
+ castStyle: 'postgres',
33
+ constraintStyle: 'postgres',
28
34
  },
29
35
  postgresWithNamedParams: {
30
36
  identifierEscape: { start: '"', end: '"' },
31
37
  parameterSymbol: ':',
32
38
  parameterStyle: ParameterStyle.Named,
39
+ castStyle: 'postgres',
40
+ constraintStyle: 'postgres',
33
41
  },
34
42
  sqlserver: {
35
43
  identifierEscape: { start: '[', end: ']' },
36
44
  parameterSymbol: '@',
37
45
  parameterStyle: ParameterStyle.Named,
46
+ constraintStyle: 'postgres',
38
47
  },
39
48
  sqlite: {
40
49
  identifierEscape: { start: '"', end: '"' },
41
50
  parameterSymbol: ':',
42
51
  parameterStyle: ParameterStyle.Named,
52
+ constraintStyle: 'postgres',
43
53
  },
44
54
  oracle: {
45
55
  identifierEscape: { start: '"', end: '"' },
46
56
  parameterSymbol: ':',
47
57
  parameterStyle: ParameterStyle.Named,
58
+ constraintStyle: 'postgres',
48
59
  },
49
60
  clickhouse: {
50
61
  identifierEscape: { start: '`', end: '`' },
51
62
  parameterSymbol: '?',
52
63
  parameterStyle: ParameterStyle.Anonymous,
64
+ constraintStyle: 'postgres',
53
65
  },
54
66
  firebird: {
55
67
  identifierEscape: { start: '"', end: '"' },
@@ -80,6 +92,7 @@ export const PRESETS = {
80
92
  identifierEscape: { start: '"', end: '"' },
81
93
  parameterSymbol: '$',
82
94
  parameterStyle: ParameterStyle.Indexed,
95
+ castStyle: 'postgres',
83
96
  },
84
97
  athena: {
85
98
  identifierEscape: { start: '"', end: '"' },
@@ -105,6 +118,7 @@ export const PRESETS = {
105
118
  identifierEscape: { start: '"', end: '"' },
106
119
  parameterSymbol: '$',
107
120
  parameterStyle: ParameterStyle.Indexed,
121
+ castStyle: 'postgres',
108
122
  },
109
123
  flinksql: {
110
124
  identifierEscape: { start: '`', end: '`' },
@@ -139,9 +153,10 @@ export class SqlPrintTokenParser {
139
153
  return this._selfHandlingComponentTypes;
140
154
  }
141
155
  constructor(options) {
142
- var _a, _b, _c, _d, _e, _f, _g;
156
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
143
157
  this.handlers = new Map();
144
158
  this.index = 1;
159
+ this.joinConditionContexts = [];
145
160
  if (options === null || options === void 0 ? void 0 : options.preset) {
146
161
  const preset = options.preset;
147
162
  options = Object.assign(Object.assign({}, preset), options);
@@ -155,6 +170,9 @@ export class SqlPrintTokenParser {
155
170
  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 : '"',
156
171
  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 : '"'
157
172
  });
173
+ this.castStyle = (_h = options === null || options === void 0 ? void 0 : options.castStyle) !== null && _h !== void 0 ? _h : 'standard';
174
+ this.constraintStyle = (_j = options === null || options === void 0 ? void 0 : options.constraintStyle) !== null && _j !== void 0 ? _j : 'postgres';
175
+ this.normalizeJoinConditionOrder = (_k = options === null || options === void 0 ? void 0 : options.joinConditionOrderByDeclaration) !== null && _k !== void 0 ? _k : false;
158
176
  this.handlers.set(ValueList.kind, (expr) => this.visitValueList(expr));
159
177
  this.handlers.set(ColumnReference.kind, (expr) => this.visitColumnReference(expr));
160
178
  this.handlers.set(QualifiedName.kind, (expr) => this.visitQualifiedName(expr));
@@ -226,10 +244,34 @@ export class SqlPrintTokenParser {
226
244
  this.handlers.set(InsertClause.kind, (expr) => this.visitInsertClause(expr));
227
245
  this.handlers.set(UpdateQuery.kind, (expr) => this.visitUpdateQuery(expr));
228
246
  this.handlers.set(UpdateClause.kind, (expr) => this.visitUpdateClause(expr));
247
+ this.handlers.set(DeleteQuery.kind, (expr) => this.visitDeleteQuery(expr));
248
+ this.handlers.set(DeleteClause.kind, (expr) => this.visitDeleteClause(expr));
249
+ this.handlers.set(UsingClause.kind, (expr) => this.visitUsingClause(expr));
229
250
  this.handlers.set(SetClause.kind, (expr) => this.visitSetClause(expr));
230
251
  this.handlers.set(SetClauseItem.kind, (expr) => this.visitSetClauseItem(expr));
231
252
  this.handlers.set(ReturningClause.kind, (expr) => this.visitReturningClause(expr));
232
253
  this.handlers.set(CreateTableQuery.kind, (expr) => this.visitCreateTableQuery(expr));
254
+ this.handlers.set(TableColumnDefinition.kind, (expr) => this.visitTableColumnDefinition(expr));
255
+ this.handlers.set(ColumnConstraintDefinition.kind, (expr) => this.visitColumnConstraintDefinition(expr));
256
+ this.handlers.set(TableConstraintDefinition.kind, (expr) => this.visitTableConstraintDefinition(expr));
257
+ this.handlers.set(ReferenceDefinition.kind, (expr) => this.visitReferenceDefinition(expr));
258
+ this.handlers.set(CreateIndexStatement.kind, (expr) => this.visitCreateIndexStatement(expr));
259
+ this.handlers.set(IndexColumnDefinition.kind, (expr) => this.visitIndexColumnDefinition(expr));
260
+ this.handlers.set(DropTableStatement.kind, (expr) => this.visitDropTableStatement(expr));
261
+ this.handlers.set(DropIndexStatement.kind, (expr) => this.visitDropIndexStatement(expr));
262
+ this.handlers.set(AlterTableStatement.kind, (expr) => this.visitAlterTableStatement(expr));
263
+ this.handlers.set(AlterTableAddConstraint.kind, (expr) => this.visitAlterTableAddConstraint(expr));
264
+ this.handlers.set(AlterTableDropConstraint.kind, (expr) => this.visitAlterTableDropConstraint(expr));
265
+ this.handlers.set(AlterTableDropColumn.kind, (expr) => this.visitAlterTableDropColumn(expr));
266
+ this.handlers.set(DropConstraintStatement.kind, (expr) => this.visitDropConstraintStatement(expr));
267
+ this.handlers.set(ExplainStatement.kind, (expr) => this.visitExplainStatement(expr));
268
+ this.handlers.set(AnalyzeStatement.kind, (expr) => this.visitAnalyzeStatement(expr));
269
+ this.handlers.set(MergeQuery.kind, (expr) => this.visitMergeQuery(expr));
270
+ this.handlers.set(MergeWhenClause.kind, (expr) => this.visitMergeWhenClause(expr));
271
+ this.handlers.set(MergeUpdateAction.kind, (expr) => this.visitMergeUpdateAction(expr));
272
+ this.handlers.set(MergeDeleteAction.kind, (expr) => this.visitMergeDeleteAction(expr));
273
+ this.handlers.set(MergeInsertAction.kind, (expr) => this.visitMergeInsertAction(expr));
274
+ this.handlers.set(MergeDoNothingAction.kind, (expr) => this.visitMergeDoNothingAction(expr));
233
275
  }
234
276
  /**
235
277
  * Pretty-prints a BinarySelectQuery (e.g., UNION, INTERSECT, EXCEPT).
@@ -250,7 +292,7 @@ export class SqlPrintTokenParser {
250
292
  token.innerTokens.push(mergedHeaderComment);
251
293
  }
252
294
  else {
253
- const headerCommentBlocks = this.createCommentBlocks(arg.headerComments);
295
+ const headerCommentBlocks = this.createCommentBlocks(arg.headerComments, true);
254
296
  token.innerTokens.push(...headerCommentBlocks);
255
297
  }
256
298
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
@@ -498,10 +540,11 @@ export class SqlPrintTokenParser {
498
540
  }
499
541
  /**
500
542
  * Creates CommentBlock containers for the given comments.
501
- * Each CommentBlock contains: Comment -> CommentNewline -> Space
502
- * This structure supports both oneliner and multiline formatting modes.
543
+ * Each CommentBlock contains: Comment -> CommentNewline -> Space.
544
+ * @param comments Raw comment strings to convert into CommentBlock tokens.
545
+ * @param isHeaderComment Marks the generated blocks as originating from header comments when true.
503
546
  */
504
- createCommentBlocks(comments) {
547
+ createCommentBlocks(comments, isHeaderComment = false) {
505
548
  // Create individual comment blocks for each comment entry
506
549
  const commentBlocks = [];
507
550
  for (const comment of comments) {
@@ -509,7 +552,7 @@ export class SqlPrintTokenParser {
509
552
  const trimmed = comment.trim();
510
553
  const isSeparatorLine = /^[-=_+*#]+$/.test(trimmed);
511
554
  if (trimmed || isSeparatorLine || comment === '') {
512
- commentBlocks.push(this.createSingleCommentBlock(comment));
555
+ commentBlocks.push(this.createSingleCommentBlock(comment, isHeaderComment));
513
556
  }
514
557
  }
515
558
  return commentBlocks;
@@ -549,8 +592,11 @@ export class SqlPrintTokenParser {
549
592
  * - Multiline mode: Comment + newline (space is filtered as leading space)
550
593
  * - Oneliner mode: Comment + space (commentNewline is skipped)
551
594
  */
552
- createSingleCommentBlock(comment) {
595
+ createSingleCommentBlock(comment, isHeaderComment = false) {
553
596
  const commentBlock = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.CommentBlock);
597
+ if (isHeaderComment) {
598
+ commentBlock.markAsHeaderComment();
599
+ }
554
600
  // Add comment token - preserve original format for line comments
555
601
  const commentToken = new SqlPrintToken(SqlPrintTokenType.comment, this.formatComment(comment));
556
602
  commentBlock.innerTokens.push(commentToken);
@@ -665,10 +711,11 @@ export class SqlPrintTokenParser {
665
711
  const afterComments = component.getPositionedComments('after');
666
712
  if (afterComments.length > 0) {
667
713
  const commentBlocks = this.createCommentBlocks(afterComments);
668
- // Insert before closing paren (last position) without separator space
669
- const insertIndex = token.innerTokens.length;
714
+ const closingIndex = token.innerTokens.length - 1;
715
+ let insertIndex = closingIndex + 1;
670
716
  for (const commentBlock of commentBlocks) {
671
- token.innerTokens.splice(insertIndex - 1, 0, commentBlock);
717
+ token.innerTokens.splice(insertIndex, 0, SqlPrintTokenParser.SPACE_TOKEN, commentBlock);
718
+ insertIndex += 2;
672
719
  }
673
720
  }
674
721
  }
@@ -748,6 +795,7 @@ export class SqlPrintTokenParser {
748
795
  }
749
796
  createHeaderMultiLineCommentBlock(headerComments) {
750
797
  const commentBlock = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.CommentBlock);
798
+ commentBlock.markAsHeaderComment();
751
799
  if (headerComments.length === 0) {
752
800
  const commentToken = new SqlPrintToken(SqlPrintTokenType.comment, '/* */');
753
801
  commentBlock.innerTokens.push(commentToken);
@@ -818,7 +866,6 @@ export class SqlPrintTokenParser {
818
866
  }
819
867
  visitFunctionCall(arg) {
820
868
  const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.FunctionCall);
821
- this.addComponentComments(token, arg);
822
869
  token.innerTokens.push(arg.qualifiedName.accept(this));
823
870
  token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
824
871
  if (arg.argument) {
@@ -856,6 +903,7 @@ export class SqlPrintTokenParser {
856
903
  token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
857
904
  }
858
905
  }
906
+ this.addComponentComments(token, arg);
859
907
  return token;
860
908
  }
861
909
  visitUnaryExpression(arg) {
@@ -1060,9 +1108,22 @@ export class SqlPrintTokenParser {
1060
1108
  }
1061
1109
  visitCastExpression(arg) {
1062
1110
  const token = new SqlPrintToken(SqlPrintTokenType.container, '', 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(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(SqlPrintTokenType.keyword, 'cast'));
1120
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
1063
1121
  token.innerTokens.push(this.visit(arg.input));
1064
- token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.operator, '::'));
1122
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1123
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'as'));
1124
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1065
1125
  token.innerTokens.push(this.visit(arg.castType));
1126
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
1066
1127
  return token;
1067
1128
  }
1068
1129
  visitCaseExpression(arg) {
@@ -1073,20 +1134,148 @@ export class SqlPrintTokenParser {
1073
1134
  // Clear positioned comments to prevent duplicate processing
1074
1135
  arg.positionedComments = null;
1075
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
+ }
1076
1149
  // Add the CASE keyword
1077
1150
  token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'case'));
1078
1151
  // Add the condition if exists
1079
- if (arg.condition) {
1152
+ if (conditionToken) {
1080
1153
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1081
- token.innerTokens.push(this.visit(arg.condition));
1154
+ token.innerTokens.push(conditionToken);
1082
1155
  }
1083
1156
  // Add the WHEN/THEN pairs and ELSE
1084
- token.innerTokens.push(this.visit(arg.switchCase));
1157
+ token.innerTokens.push(switchToken);
1085
1158
  // Add the END keyword
1086
1159
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1087
1160
  token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'end'));
1161
+ if (trailingSwitchComments.length > 0) {
1162
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.commentNewline, ''));
1163
+ const trailingBlocks = this.createCommentBlocks(trailingSwitchComments);
1164
+ token.innerTokens.push(...trailingBlocks);
1165
+ }
1088
1166
  return token;
1089
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 === 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 === SqlPrintTokenContainerType.CommentBlock) {
1202
+ continue;
1203
+ }
1204
+ if (child.type === SqlPrintTokenType.space) {
1205
+ continue;
1206
+ }
1207
+ if (child.type === SqlPrintTokenType.keyword) {
1208
+ continue;
1209
+ }
1210
+ if (child.containerType === 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 === 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 === 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
+ SqlPrintTokenContainerType.ColumnReference,
1261
+ SqlPrintTokenContainerType.QualifiedName,
1262
+ SqlPrintTokenContainerType.IdentifierString,
1263
+ SqlPrintTokenContainerType.RawString,
1264
+ SqlPrintTokenContainerType.LiteralValue,
1265
+ SqlPrintTokenContainerType.ParenExpression,
1266
+ 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
+ }
1090
1279
  visitArrayExpression(arg) {
1091
1280
  const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.ArrayExpression);
1092
1281
  token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'array'));
@@ -1172,6 +1361,7 @@ export class SqlPrintTokenParser {
1172
1361
  }
1173
1362
  visitTupleExpression(arg) {
1174
1363
  const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.TupleExpression);
1364
+ const requiresMultiline = this.tupleRequiresMultiline(arg);
1175
1365
  token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
1176
1366
  for (let i = 0; i < arg.values.length; i++) {
1177
1367
  if (i > 0) {
@@ -1179,10 +1369,38 @@ export class SqlPrintTokenParser {
1179
1369
  }
1180
1370
  token.innerTokens.push(this.visit(arg.values[i]));
1181
1371
  }
1372
+ if (requiresMultiline) {
1373
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.commentNewline, '', SqlPrintTokenContainerType.TupleExpression));
1374
+ }
1182
1375
  token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
1183
- this.addComponentComments(token, arg);
1184
1376
  return token;
1185
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 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
+ }
1186
1404
  visitWindowFrameExpression(arg) {
1187
1405
  // Compose window frame expression: over(partition by ... order by ... rows ...)
1188
1406
  const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.WindowFrameExpression);
@@ -1259,12 +1477,15 @@ export class SqlPrintTokenParser {
1259
1477
  // Preserve original positioned comments to avoid mutating the source object
1260
1478
  const originalSelectItemPositionedComments = arg.positionedComments;
1261
1479
  const originalValuePositionedComments = arg.value.positionedComments;
1262
- // Clear positioned comments from the value to avoid duplication since SelectItem handles them
1263
- arg.value.positionedComments = null;
1480
+ const isParenExpression = arg.value instanceof ParenExpression;
1481
+ // Clear positioned comments from the value to avoid duplication when SelectItem itself renders them.
1482
+ // ParenExpression handles trailing comments internally, so we must keep its metadata intact.
1483
+ if (!isParenExpression) {
1484
+ arg.value.positionedComments = null;
1485
+ }
1264
1486
  // Add positioned comments in recorded order
1265
1487
  const beforeComments = arg.getPositionedComments('before');
1266
1488
  const afterComments = arg.getPositionedComments('after');
1267
- const isParenExpression = arg.value.constructor.name === 'ParenExpression';
1268
1489
  if (beforeComments.length > 0) {
1269
1490
  const commentTokens = this.createInlineCommentSequence(beforeComments);
1270
1491
  token.innerTokens.push(...commentTokens);
@@ -1463,16 +1684,32 @@ export class SqlPrintTokenParser {
1463
1684
  }
1464
1685
  }
1465
1686
  visitFromClause(arg) {
1466
- const token = new SqlPrintToken(SqlPrintTokenType.keyword, 'from', SqlPrintTokenContainerType.FromClause);
1467
- token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1468
- token.innerTokens.push(this.visit(arg.source));
1469
- if (arg.joins) {
1470
- for (let i = 0; i < arg.joins.length; i++) {
1471
- token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1472
- token.innerTokens.push(this.visit(arg.joins[i]));
1687
+ // Build a declaration order map so JOIN ON operands can be normalized later.
1688
+ let contextPushed = false;
1689
+ if (this.normalizeJoinConditionOrder) {
1690
+ const aliasOrder = this.buildJoinAliasOrder(arg);
1691
+ if (aliasOrder.size > 0) {
1692
+ this.joinConditionContexts.push({ aliasOrder });
1693
+ contextPushed = true;
1694
+ }
1695
+ }
1696
+ try {
1697
+ const token = new SqlPrintToken(SqlPrintTokenType.keyword, 'from', SqlPrintTokenContainerType.FromClause);
1698
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1699
+ token.innerTokens.push(this.visit(arg.source));
1700
+ if (arg.joins) {
1701
+ for (let i = 0; i < arg.joins.length; i++) {
1702
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1703
+ token.innerTokens.push(this.visit(arg.joins[i]));
1704
+ }
1705
+ }
1706
+ return token;
1707
+ }
1708
+ finally {
1709
+ if (contextPushed) {
1710
+ this.joinConditionContexts.pop();
1473
1711
  }
1474
1712
  }
1475
- return token;
1476
1713
  }
1477
1714
  visitJoinClause(arg) {
1478
1715
  // Print join clause: [joinType] [lateral] [source] [on/using ...]
@@ -1522,12 +1759,123 @@ export class SqlPrintTokenParser {
1522
1759
  return token;
1523
1760
  }
1524
1761
  visitJoinOnClause(arg) {
1762
+ // Normalize JOIN ON predicate columns to follow declaration order when enabled.
1763
+ if (this.normalizeJoinConditionOrder) {
1764
+ const aliasOrder = this.getCurrentJoinAliasOrder();
1765
+ if (aliasOrder) {
1766
+ this.normalizeJoinConditionValue(arg.condition, aliasOrder);
1767
+ }
1768
+ }
1525
1769
  const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.JoinOnClause);
1526
1770
  token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'on'));
1527
1771
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1528
1772
  token.innerTokens.push(this.visit(arg.condition));
1529
1773
  return token;
1530
1774
  }
1775
+ getCurrentJoinAliasOrder() {
1776
+ if (this.joinConditionContexts.length === 0) {
1777
+ return null;
1778
+ }
1779
+ return this.joinConditionContexts[this.joinConditionContexts.length - 1].aliasOrder;
1780
+ }
1781
+ buildJoinAliasOrder(fromClause) {
1782
+ const aliasOrder = new Map();
1783
+ let nextIndex = 0;
1784
+ const registerSource = (source) => {
1785
+ const identifiers = this.collectSourceIdentifiers(source);
1786
+ if (identifiers.length === 0) {
1787
+ return;
1788
+ }
1789
+ // Track the earliest declaration index for each identifier found in the FROM clause.
1790
+ for (const identifier of identifiers) {
1791
+ const key = identifier.toLowerCase();
1792
+ if (!aliasOrder.has(key)) {
1793
+ aliasOrder.set(key, nextIndex);
1794
+ }
1795
+ }
1796
+ nextIndex++;
1797
+ };
1798
+ registerSource(fromClause.source);
1799
+ if (fromClause.joins) {
1800
+ for (const joinClause of fromClause.joins) {
1801
+ registerSource(joinClause.source);
1802
+ }
1803
+ }
1804
+ return aliasOrder;
1805
+ }
1806
+ collectSourceIdentifiers(source) {
1807
+ const identifiers = [];
1808
+ const aliasName = source.getAliasName();
1809
+ if (aliasName) {
1810
+ identifiers.push(aliasName);
1811
+ }
1812
+ // Capture table identifiers so unaliased tables can still be matched.
1813
+ if (source.datasource instanceof TableSource) {
1814
+ const tableComponent = source.datasource.table.name;
1815
+ identifiers.push(tableComponent);
1816
+ const fullName = source.datasource.getSourceName();
1817
+ if (fullName && fullName !== tableComponent) {
1818
+ identifiers.push(fullName);
1819
+ }
1820
+ }
1821
+ return identifiers;
1822
+ }
1823
+ normalizeJoinConditionValue(condition, aliasOrder) {
1824
+ // Walk the value tree so every comparison within the JOIN predicate is inspected.
1825
+ const kind = condition.getKind();
1826
+ if (kind === ParenExpression.kind) {
1827
+ const paren = condition;
1828
+ this.normalizeJoinConditionValue(paren.expression, aliasOrder);
1829
+ return;
1830
+ }
1831
+ if (kind === BinaryExpression.kind) {
1832
+ const binary = condition;
1833
+ this.normalizeJoinConditionValue(binary.left, aliasOrder);
1834
+ this.normalizeJoinConditionValue(binary.right, aliasOrder);
1835
+ this.normalizeBinaryEquality(binary, aliasOrder);
1836
+ }
1837
+ }
1838
+ normalizeBinaryEquality(binary, aliasOrder) {
1839
+ // Only normalize simple equality comparisons, leaving other operators untouched.
1840
+ const operatorValue = binary.operator.value.toLowerCase();
1841
+ if (operatorValue !== '=') {
1842
+ return;
1843
+ }
1844
+ const leftOwner = this.resolveColumnOwner(binary.left);
1845
+ const rightOwner = this.resolveColumnOwner(binary.right);
1846
+ if (!leftOwner || !rightOwner || leftOwner === rightOwner) {
1847
+ return;
1848
+ }
1849
+ const leftOrder = aliasOrder.get(leftOwner);
1850
+ const rightOrder = aliasOrder.get(rightOwner);
1851
+ if (leftOrder === undefined || rightOrder === undefined) {
1852
+ return;
1853
+ }
1854
+ if (leftOrder > rightOrder) {
1855
+ // Swap operands so the earlier declared table appears on the left.
1856
+ const originalLeft = binary.left;
1857
+ binary.left = binary.right;
1858
+ binary.right = originalLeft;
1859
+ }
1860
+ }
1861
+ resolveColumnOwner(value) {
1862
+ var _a;
1863
+ const kind = value.getKind();
1864
+ if (kind === ColumnReference.kind) {
1865
+ // Column references expose their qualifier namespace, which we normalize for lookups.
1866
+ const columnRef = value;
1867
+ const namespace = columnRef.getNamespace();
1868
+ if (!namespace) {
1869
+ return null;
1870
+ }
1871
+ const qualifier = namespace.includes('.') ? (_a = namespace.split('.').pop()) !== null && _a !== void 0 ? _a : '' : namespace;
1872
+ return qualifier.toLowerCase();
1873
+ }
1874
+ if (kind === ParenExpression.kind) {
1875
+ return this.resolveColumnOwner(value.expression);
1876
+ }
1877
+ return null;
1878
+ }
1531
1879
  visitJoinUsingClause(arg) {
1532
1880
  const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.JoinUsingClause);
1533
1881
  token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'using'));
@@ -1711,7 +2059,7 @@ export class SqlPrintTokenParser {
1711
2059
  token.innerTokens.push(mergedHeaderComment);
1712
2060
  }
1713
2061
  else {
1714
- const headerCommentBlocks = this.createCommentBlocks(arg.headerComments);
2062
+ const headerCommentBlocks = this.createCommentBlocks(arg.headerComments, true);
1715
2063
  token.innerTokens.push(...headerCommentBlocks);
1716
2064
  }
1717
2065
  if (arg.withClause) {
@@ -1798,7 +2146,7 @@ export class SqlPrintTokenParser {
1798
2146
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1799
2147
  }
1800
2148
  else {
1801
- const headerCommentBlocks = this.createCommentBlocks(arg.headerComments);
2149
+ const headerCommentBlocks = this.createCommentBlocks(arg.headerComments, true);
1802
2150
  for (const commentBlock of headerCommentBlocks) {
1803
2151
  token.innerTokens.push(commentBlock);
1804
2152
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
@@ -1839,22 +2187,27 @@ export class SqlPrintTokenParser {
1839
2187
  }
1840
2188
  visitInsertQuery(arg) {
1841
2189
  const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.InsertQuery);
1842
- // Process the insert clause
2190
+ if (arg.withClause) {
2191
+ token.innerTokens.push(arg.withClause.accept(this));
2192
+ }
1843
2193
  token.innerTokens.push(this.visit(arg.insertClause));
1844
2194
  // Process the select query if present
1845
2195
  if (arg.selectQuery) {
1846
2196
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1847
2197
  token.innerTokens.push(this.visit(arg.selectQuery));
1848
2198
  }
2199
+ if (arg.returningClause) {
2200
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2201
+ token.innerTokens.push(arg.returningClause.accept(this));
2202
+ }
1849
2203
  return token;
1850
2204
  }
1851
2205
  visitInsertClause(arg) {
1852
- const token = new SqlPrintToken(SqlPrintTokenType.container, '');
1853
- token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2206
+ const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.InsertClause);
1854
2207
  token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'insert into'));
1855
2208
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1856
2209
  token.innerTokens.push(arg.source.accept(this));
1857
- if (arg.columns.length > 0) {
2210
+ if (arg.columns && arg.columns.length > 0) {
1858
2211
  token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
1859
2212
  for (let i = 0; i < arg.columns.length; i++) {
1860
2213
  if (i > 0) {
@@ -1866,6 +2219,176 @@ export class SqlPrintTokenParser {
1866
2219
  }
1867
2220
  return token;
1868
2221
  }
2222
+ visitDeleteQuery(arg) {
2223
+ const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.DeleteQuery);
2224
+ // Attach WITH clause tokens when present before the DELETE command.
2225
+ if (arg.withClause) {
2226
+ token.innerTokens.push(arg.withClause.accept(this));
2227
+ }
2228
+ token.innerTokens.push(arg.deleteClause.accept(this));
2229
+ // Append USING clause when the DELETE references additional sources.
2230
+ if (arg.usingClause) {
2231
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2232
+ token.innerTokens.push(arg.usingClause.accept(this));
2233
+ }
2234
+ // Append WHERE clause to restrict affected rows.
2235
+ if (arg.whereClause) {
2236
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2237
+ token.innerTokens.push(arg.whereClause.accept(this));
2238
+ }
2239
+ // Append RETURNING clause when the DELETE yields output columns.
2240
+ if (arg.returningClause) {
2241
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2242
+ token.innerTokens.push(arg.returningClause.accept(this));
2243
+ }
2244
+ return token;
2245
+ }
2246
+ visitDeleteClause(arg) {
2247
+ const token = new SqlPrintToken(SqlPrintTokenType.keyword, 'delete from', SqlPrintTokenContainerType.DeleteClause);
2248
+ // Render the target relation immediately after the DELETE FROM keyword.
2249
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2250
+ token.innerTokens.push(arg.source.accept(this));
2251
+ return token;
2252
+ }
2253
+ visitUsingClause(arg) {
2254
+ const token = new SqlPrintToken(SqlPrintTokenType.keyword, 'using', SqlPrintTokenContainerType.UsingClause);
2255
+ if (arg.sources.length > 0) {
2256
+ // Attach the first USING source directly after the keyword.
2257
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2258
+ for (let i = 0; i < arg.sources.length; i++) {
2259
+ if (i > 0) {
2260
+ // Separate subsequent sources with comma and space for clarity.
2261
+ token.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens());
2262
+ }
2263
+ token.innerTokens.push(this.visit(arg.sources[i]));
2264
+ }
2265
+ }
2266
+ return token;
2267
+ }
2268
+ visitMergeQuery(arg) {
2269
+ const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.MergeQuery);
2270
+ if (arg.withClause) {
2271
+ token.innerTokens.push(arg.withClause.accept(this));
2272
+ }
2273
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'merge into'));
2274
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2275
+ token.innerTokens.push(arg.target.accept(this));
2276
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2277
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'using'));
2278
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2279
+ token.innerTokens.push(arg.source.accept(this));
2280
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2281
+ const onClauseToken = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.JoinOnClause);
2282
+ onClauseToken.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'on'));
2283
+ onClauseToken.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2284
+ onClauseToken.innerTokens.push(arg.onCondition.accept(this));
2285
+ token.innerTokens.push(onClauseToken);
2286
+ for (const clause of arg.whenClauses) {
2287
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2288
+ token.innerTokens.push(clause.accept(this));
2289
+ }
2290
+ return token;
2291
+ }
2292
+ visitMergeWhenClause(arg) {
2293
+ const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.MergeWhenClause);
2294
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, this.mergeMatchTypeToKeyword(arg.matchType)));
2295
+ if (arg.condition) {
2296
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2297
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'and'));
2298
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2299
+ token.innerTokens.push(arg.condition.accept(this));
2300
+ }
2301
+ const thenLeadingComments = arg.getThenLeadingComments();
2302
+ const thenKeywordToken = new SqlPrintToken(SqlPrintTokenType.keyword, 'then');
2303
+ if (thenLeadingComments.length > 0) {
2304
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2305
+ const commentBlocks = this.createCommentBlocks(thenLeadingComments);
2306
+ token.innerTokens.push(...commentBlocks);
2307
+ token.innerTokens.push(thenKeywordToken);
2308
+ }
2309
+ else {
2310
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2311
+ token.innerTokens.push(thenKeywordToken);
2312
+ }
2313
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2314
+ token.innerTokens.push(arg.action.accept(this));
2315
+ return token;
2316
+ }
2317
+ visitMergeUpdateAction(arg) {
2318
+ const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.MergeUpdateAction);
2319
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'update'));
2320
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2321
+ token.innerTokens.push(arg.setClause.accept(this));
2322
+ if (arg.whereClause) {
2323
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2324
+ token.innerTokens.push(arg.whereClause.accept(this));
2325
+ }
2326
+ return token;
2327
+ }
2328
+ visitMergeDeleteAction(arg) {
2329
+ const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.MergeDeleteAction);
2330
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'delete'));
2331
+ if (arg.whereClause) {
2332
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2333
+ token.innerTokens.push(arg.whereClause.accept(this));
2334
+ }
2335
+ return token;
2336
+ }
2337
+ visitMergeInsertAction(arg) {
2338
+ const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.MergeInsertAction);
2339
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'insert'));
2340
+ if (arg.columns && arg.columns.length > 0) {
2341
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2342
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
2343
+ for (let i = 0; i < arg.columns.length; i++) {
2344
+ if (i > 0) {
2345
+ token.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens());
2346
+ }
2347
+ token.innerTokens.push(arg.columns[i].accept(this));
2348
+ }
2349
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
2350
+ }
2351
+ if (arg.defaultValues) {
2352
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2353
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'default values'));
2354
+ return token;
2355
+ }
2356
+ if (arg.values) {
2357
+ const leadingValuesComments = arg.getValuesLeadingComments();
2358
+ if (leadingValuesComments.length > 0) {
2359
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.commentNewline, ''));
2360
+ const commentBlocks = this.createCommentBlocks(leadingValuesComments);
2361
+ token.innerTokens.push(...commentBlocks);
2362
+ }
2363
+ else {
2364
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2365
+ }
2366
+ const valuesKeywordToken = new SqlPrintToken(SqlPrintTokenType.keyword, 'values');
2367
+ token.innerTokens.push(valuesKeywordToken);
2368
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2369
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
2370
+ token.innerTokens.push(arg.values.accept(this));
2371
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
2372
+ }
2373
+ return token;
2374
+ }
2375
+ visitMergeDoNothingAction(_) {
2376
+ return new SqlPrintToken(SqlPrintTokenType.keyword, 'do nothing', SqlPrintTokenContainerType.MergeDoNothingAction);
2377
+ }
2378
+ mergeMatchTypeToKeyword(matchType) {
2379
+ switch (matchType) {
2380
+ case 'matched':
2381
+ return 'when matched';
2382
+ case 'not_matched':
2383
+ return 'when not matched';
2384
+ case 'not_matched_by_source':
2385
+ return 'when not matched by source';
2386
+ case 'not_matched_by_target':
2387
+ return 'when not matched by target';
2388
+ default:
2389
+ return 'when';
2390
+ }
2391
+ }
1869
2392
  visitUpdateQuery(arg) {
1870
2393
  const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.UpdateQuery);
1871
2394
  if (arg.withClause) {
@@ -1895,7 +2418,7 @@ export class SqlPrintTokenParser {
1895
2418
  return token;
1896
2419
  }
1897
2420
  visitSetClause(arg) {
1898
- const token = new SqlPrintToken(SqlPrintTokenType.keyword, 'set', SqlPrintTokenContainerType.SelectClause);
2421
+ const token = new SqlPrintToken(SqlPrintTokenType.keyword, 'set', SqlPrintTokenContainerType.SetClause);
1899
2422
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1900
2423
  for (let i = 0; i < arg.items.length; i++) {
1901
2424
  if (i > 0) {
@@ -1926,17 +2449,601 @@ export class SqlPrintTokenParser {
1926
2449
  return token;
1927
2450
  }
1928
2451
  visitCreateTableQuery(arg) {
1929
- const token = new SqlPrintToken(SqlPrintTokenType.keyword, arg.isTemporary ? 'create temporary table' : 'create table', SqlPrintTokenContainerType.CreateTableQuery);
2452
+ var _a;
2453
+ const baseKeyword = arg.isTemporary ? 'create temporary table' : 'create table';
2454
+ let keywordText = arg.ifNotExists ? `${baseKeyword} if not exists` : baseKeyword;
2455
+ const token = new SqlPrintToken(SqlPrintTokenType.keyword, keywordText, SqlPrintTokenContainerType.CreateTableQuery);
1930
2456
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1931
- token.innerTokens.push(arg.tableName.accept(this));
2457
+ const qualifiedName = new QualifiedName((_a = arg.namespaces) !== null && _a !== void 0 ? _a : null, arg.tableName);
2458
+ token.innerTokens.push(qualifiedName.accept(this));
2459
+ const definitionEntries = [...arg.columns, ...arg.tableConstraints];
2460
+ if (definitionEntries.length > 0) {
2461
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2462
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
2463
+ const definitionToken = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.CreateTableDefinition);
2464
+ for (let i = 0; i < definitionEntries.length; i++) {
2465
+ if (i > 0) {
2466
+ definitionToken.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens());
2467
+ }
2468
+ definitionToken.innerTokens.push(definitionEntries[i].accept(this));
2469
+ }
2470
+ token.innerTokens.push(definitionToken);
2471
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
2472
+ }
2473
+ if (arg.tableOptions) {
2474
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2475
+ token.innerTokens.push(arg.tableOptions.accept(this));
2476
+ }
1932
2477
  if (arg.asSelectQuery) {
1933
2478
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1934
2479
  token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'as'));
1935
2480
  token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
1936
2481
  token.innerTokens.push(arg.asSelectQuery.accept(this));
1937
2482
  }
2483
+ if (arg.withDataOption) {
2484
+ // Reconstruct WITH [NO] DATA clause to mirror PostgreSQL CREATE TABLE semantics.
2485
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2486
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'with'));
2487
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2488
+ if (arg.withDataOption === 'with-no-data') {
2489
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'no'));
2490
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2491
+ }
2492
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'data'));
2493
+ }
2494
+ return token;
2495
+ }
2496
+ visitTableColumnDefinition(arg) {
2497
+ const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.TableColumnDefinition);
2498
+ token.innerTokens.push(arg.name.accept(this));
2499
+ if (arg.dataType) {
2500
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2501
+ token.innerTokens.push(arg.dataType.accept(this));
2502
+ }
2503
+ for (const constraint of arg.constraints) {
2504
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2505
+ token.innerTokens.push(constraint.accept(this));
2506
+ }
2507
+ return token;
2508
+ }
2509
+ visitColumnConstraintDefinition(arg) {
2510
+ const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.ColumnConstraintDefinition);
2511
+ if (arg.constraintName) {
2512
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'constraint'));
2513
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2514
+ token.innerTokens.push(arg.constraintName.accept(this));
2515
+ }
2516
+ const appendKeyword = (text) => {
2517
+ if (token.innerTokens.length > 0) {
2518
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2519
+ }
2520
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, text));
2521
+ };
2522
+ const appendComponent = (component) => {
2523
+ if (token.innerTokens.length > 0) {
2524
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2525
+ }
2526
+ token.innerTokens.push(component.accept(this));
2527
+ };
2528
+ switch (arg.kind) {
2529
+ case 'not-null':
2530
+ appendKeyword('not null');
2531
+ break;
2532
+ case 'null':
2533
+ appendKeyword('null');
2534
+ break;
2535
+ case 'default':
2536
+ appendKeyword('default');
2537
+ if (arg.defaultValue) {
2538
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2539
+ token.innerTokens.push(arg.defaultValue.accept(this));
2540
+ }
2541
+ break;
2542
+ case 'primary-key':
2543
+ appendKeyword('primary key');
2544
+ break;
2545
+ case 'unique':
2546
+ appendKeyword('unique');
2547
+ break;
2548
+ case 'references':
2549
+ if (arg.reference) {
2550
+ appendComponent(arg.reference);
2551
+ }
2552
+ break;
2553
+ case 'check':
2554
+ if (arg.checkExpression) {
2555
+ appendKeyword('check');
2556
+ token.innerTokens.push(this.wrapWithParenExpression(arg.checkExpression));
2557
+ }
2558
+ break;
2559
+ case 'generated-always-identity':
2560
+ case 'generated-by-default-identity':
2561
+ case 'raw':
2562
+ if (arg.rawClause) {
2563
+ appendComponent(arg.rawClause);
2564
+ }
2565
+ break;
2566
+ }
2567
+ return token;
2568
+ }
2569
+ visitTableConstraintDefinition(arg) {
2570
+ var _a, _b, _c;
2571
+ const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.TableConstraintDefinition);
2572
+ const appendKeyword = (text) => {
2573
+ if (token.innerTokens.length > 0) {
2574
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2575
+ }
2576
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, text));
2577
+ };
2578
+ const appendComponent = (component) => {
2579
+ if (token.innerTokens.length > 0) {
2580
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2581
+ }
2582
+ token.innerTokens.push(component.accept(this));
2583
+ };
2584
+ const appendColumns = (columns) => {
2585
+ if (!columns || columns.length === 0) {
2586
+ return;
2587
+ }
2588
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2589
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
2590
+ const listToken = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.ValueList);
2591
+ for (let i = 0; i < columns.length; i++) {
2592
+ if (i > 0) {
2593
+ listToken.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens());
2594
+ }
2595
+ listToken.innerTokens.push(columns[i].accept(this));
2596
+ }
2597
+ token.innerTokens.push(listToken);
2598
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
2599
+ };
2600
+ const useMysqlConstraintStyle = this.constraintStyle === 'mysql';
2601
+ const inlineNameKinds = new Set(['primary-key', 'unique', 'foreign-key']);
2602
+ const shouldInlineConstraintName = useMysqlConstraintStyle && !!arg.constraintName && inlineNameKinds.has(arg.kind);
2603
+ if (arg.constraintName && !shouldInlineConstraintName) {
2604
+ appendKeyword('constraint');
2605
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2606
+ token.innerTokens.push(arg.constraintName.accept(this));
2607
+ }
2608
+ switch (arg.kind) {
2609
+ case 'primary-key':
2610
+ appendKeyword('primary key');
2611
+ if (shouldInlineConstraintName && arg.constraintName) {
2612
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2613
+ token.innerTokens.push(arg.constraintName.accept(this));
2614
+ }
2615
+ appendColumns((_a = arg.columns) !== null && _a !== void 0 ? _a : []);
2616
+ break;
2617
+ case 'unique':
2618
+ if (useMysqlConstraintStyle) {
2619
+ appendKeyword('unique key');
2620
+ if (shouldInlineConstraintName && arg.constraintName) {
2621
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2622
+ token.innerTokens.push(arg.constraintName.accept(this));
2623
+ }
2624
+ }
2625
+ else {
2626
+ appendKeyword('unique');
2627
+ }
2628
+ appendColumns((_b = arg.columns) !== null && _b !== void 0 ? _b : []);
2629
+ break;
2630
+ case 'foreign-key':
2631
+ appendKeyword('foreign key');
2632
+ if (shouldInlineConstraintName && arg.constraintName) {
2633
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2634
+ token.innerTokens.push(arg.constraintName.accept(this));
2635
+ }
2636
+ appendColumns((_c = arg.columns) !== null && _c !== void 0 ? _c : []);
2637
+ if (arg.reference) {
2638
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2639
+ token.innerTokens.push(arg.reference.accept(this));
2640
+ }
2641
+ break;
2642
+ case 'check':
2643
+ if (arg.checkExpression) {
2644
+ appendKeyword('check');
2645
+ token.innerTokens.push(this.wrapWithParenExpression(arg.checkExpression));
2646
+ }
2647
+ break;
2648
+ case 'raw':
2649
+ if (arg.rawClause) {
2650
+ appendComponent(arg.rawClause);
2651
+ }
2652
+ break;
2653
+ }
2654
+ return token;
2655
+ }
2656
+ wrapWithParenExpression(expression) {
2657
+ // Reuse existing parentheses groups to avoid double-wrapping when callers already provided them.
2658
+ if (expression instanceof ParenExpression) {
2659
+ return this.visit(expression);
2660
+ }
2661
+ // Synthesize a ParenExpression wrapper so nested boolean groups render with consistent indentation.
2662
+ const synthetic = new ParenExpression(expression);
2663
+ return this.visit(synthetic);
2664
+ }
2665
+ visitReferenceDefinition(arg) {
2666
+ const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.ReferenceDefinition);
2667
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'references'));
2668
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2669
+ token.innerTokens.push(arg.targetTable.accept(this));
2670
+ if (arg.columns && arg.columns.length > 0) {
2671
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
2672
+ const columnList = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.ValueList);
2673
+ for (let i = 0; i < arg.columns.length; i++) {
2674
+ if (i > 0) {
2675
+ columnList.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens());
2676
+ }
2677
+ columnList.innerTokens.push(arg.columns[i].accept(this));
2678
+ }
2679
+ token.innerTokens.push(columnList);
2680
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
2681
+ }
2682
+ if (arg.matchType) {
2683
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2684
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, `match ${arg.matchType}`));
2685
+ }
2686
+ if (arg.onDelete) {
2687
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2688
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'on delete'));
2689
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2690
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, arg.onDelete));
2691
+ }
2692
+ if (arg.onUpdate) {
2693
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2694
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'on update'));
2695
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2696
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, arg.onUpdate));
2697
+ }
2698
+ if (arg.deferrable === 'deferrable') {
2699
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2700
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'deferrable'));
2701
+ }
2702
+ else if (arg.deferrable === 'not deferrable') {
2703
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2704
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'not deferrable'));
2705
+ }
2706
+ if (arg.initially === 'immediate') {
2707
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2708
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'initially immediate'));
2709
+ }
2710
+ else if (arg.initially === 'deferred') {
2711
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2712
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'initially deferred'));
2713
+ }
2714
+ return token;
2715
+ }
2716
+ visitCreateIndexStatement(arg) {
2717
+ const keywordParts = ['create'];
2718
+ if (arg.unique) {
2719
+ keywordParts.push('unique');
2720
+ }
2721
+ keywordParts.push('index');
2722
+ if (arg.concurrently) {
2723
+ keywordParts.push('concurrently');
2724
+ }
2725
+ if (arg.ifNotExists) {
2726
+ keywordParts.push('if not exists');
2727
+ }
2728
+ const token = new SqlPrintToken(SqlPrintTokenType.keyword, keywordParts.join(' '), SqlPrintTokenContainerType.CreateIndexStatement);
2729
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2730
+ token.innerTokens.push(arg.indexName.accept(this));
2731
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2732
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'on'));
2733
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2734
+ token.innerTokens.push(arg.tableName.accept(this));
2735
+ if (arg.usingMethod) {
2736
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2737
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'using'));
2738
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2739
+ token.innerTokens.push(arg.usingMethod.accept(this));
2740
+ }
2741
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2742
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
2743
+ const columnList = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.IndexColumnList);
2744
+ for (let i = 0; i < arg.columns.length; i++) {
2745
+ if (i > 0) {
2746
+ columnList.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens());
2747
+ }
2748
+ columnList.innerTokens.push(arg.columns[i].accept(this));
2749
+ }
2750
+ token.innerTokens.push(columnList);
2751
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
2752
+ if (arg.include && arg.include.length > 0) {
2753
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2754
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'include'));
2755
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2756
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
2757
+ const includeList = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.ValueList);
2758
+ for (let i = 0; i < arg.include.length; i++) {
2759
+ if (i > 0) {
2760
+ includeList.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens());
2761
+ }
2762
+ includeList.innerTokens.push(arg.include[i].accept(this));
2763
+ }
2764
+ token.innerTokens.push(includeList);
2765
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
2766
+ }
2767
+ if (arg.withOptions) {
2768
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2769
+ token.innerTokens.push(arg.withOptions.accept(this));
2770
+ }
2771
+ if (arg.tablespace) {
2772
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2773
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'tablespace'));
2774
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2775
+ token.innerTokens.push(arg.tablespace.accept(this));
2776
+ }
2777
+ if (arg.where) {
2778
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2779
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'where'));
2780
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2781
+ token.innerTokens.push(this.visit(arg.where));
2782
+ }
1938
2783
  return token;
1939
2784
  }
2785
+ visitIndexColumnDefinition(arg) {
2786
+ const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.IndexColumnDefinition);
2787
+ token.innerTokens.push(this.visit(arg.expression));
2788
+ if (arg.collation) {
2789
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2790
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'collate'));
2791
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2792
+ token.innerTokens.push(arg.collation.accept(this));
2793
+ }
2794
+ if (arg.operatorClass) {
2795
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2796
+ token.innerTokens.push(arg.operatorClass.accept(this));
2797
+ }
2798
+ if (arg.sortOrder) {
2799
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2800
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, arg.sortOrder));
2801
+ }
2802
+ if (arg.nullsOrder) {
2803
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2804
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, `nulls ${arg.nullsOrder}`));
2805
+ }
2806
+ return token;
2807
+ }
2808
+ visitDropTableStatement(arg) {
2809
+ const keyword = arg.ifExists ? 'drop table if exists' : 'drop table';
2810
+ const token = new SqlPrintToken(SqlPrintTokenType.keyword, keyword, SqlPrintTokenContainerType.DropTableStatement);
2811
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2812
+ const tableList = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.ValueList);
2813
+ for (let i = 0; i < arg.tables.length; i++) {
2814
+ if (i > 0) {
2815
+ tableList.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens());
2816
+ }
2817
+ tableList.innerTokens.push(arg.tables[i].accept(this));
2818
+ }
2819
+ token.innerTokens.push(tableList);
2820
+ if (arg.behavior) {
2821
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2822
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, arg.behavior));
2823
+ }
2824
+ return token;
2825
+ }
2826
+ visitDropIndexStatement(arg) {
2827
+ const keywordParts = ['drop', 'index'];
2828
+ if (arg.concurrently) {
2829
+ keywordParts.push('concurrently');
2830
+ }
2831
+ if (arg.ifExists) {
2832
+ keywordParts.push('if exists');
2833
+ }
2834
+ const token = new SqlPrintToken(SqlPrintTokenType.keyword, keywordParts.join(' '), SqlPrintTokenContainerType.DropIndexStatement);
2835
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2836
+ const indexList = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.ValueList);
2837
+ for (let i = 0; i < arg.indexNames.length; i++) {
2838
+ if (i > 0) {
2839
+ indexList.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens());
2840
+ }
2841
+ indexList.innerTokens.push(arg.indexNames[i].accept(this));
2842
+ }
2843
+ token.innerTokens.push(indexList);
2844
+ if (arg.behavior) {
2845
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2846
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, arg.behavior));
2847
+ }
2848
+ return token;
2849
+ }
2850
+ visitAlterTableStatement(arg) {
2851
+ const keywordParts = ['alter', 'table'];
2852
+ if (arg.ifExists) {
2853
+ keywordParts.push('if exists');
2854
+ }
2855
+ if (arg.only) {
2856
+ keywordParts.push('only');
2857
+ }
2858
+ const token = new SqlPrintToken(SqlPrintTokenType.keyword, keywordParts.join(' '), SqlPrintTokenContainerType.AlterTableStatement);
2859
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2860
+ token.innerTokens.push(arg.table.accept(this));
2861
+ for (let i = 0; i < arg.actions.length; i++) {
2862
+ if (i === 0) {
2863
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2864
+ }
2865
+ else {
2866
+ token.innerTokens.push(SqlPrintTokenParser.COMMA_TOKEN);
2867
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2868
+ }
2869
+ token.innerTokens.push(arg.actions[i].accept(this));
2870
+ }
2871
+ return token;
2872
+ }
2873
+ visitAlterTableAddConstraint(arg) {
2874
+ const keyword = arg.ifNotExists ? 'add if not exists' : 'add';
2875
+ const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.AlterTableAddConstraint);
2876
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, keyword));
2877
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2878
+ token.innerTokens.push(arg.constraint.accept(this));
2879
+ if (arg.notValid) {
2880
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2881
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'not valid'));
2882
+ }
2883
+ return token;
2884
+ }
2885
+ visitAlterTableDropConstraint(arg) {
2886
+ let keyword = 'drop constraint';
2887
+ if (arg.ifExists) {
2888
+ keyword += ' if exists';
2889
+ }
2890
+ const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.AlterTableDropConstraint);
2891
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, keyword));
2892
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2893
+ token.innerTokens.push(arg.constraintName.accept(this));
2894
+ if (arg.behavior) {
2895
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2896
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, arg.behavior));
2897
+ }
2898
+ return token;
2899
+ }
2900
+ visitAlterTableDropColumn(arg) {
2901
+ let keyword = 'drop column';
2902
+ if (arg.ifExists) {
2903
+ keyword += ' if exists';
2904
+ }
2905
+ const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.AlterTableDropColumn);
2906
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, keyword));
2907
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2908
+ token.innerTokens.push(arg.columnName.accept(this));
2909
+ if (arg.behavior) {
2910
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2911
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, arg.behavior));
2912
+ }
2913
+ return token;
2914
+ }
2915
+ visitDropConstraintStatement(arg) {
2916
+ let keyword = 'drop constraint';
2917
+ if (arg.ifExists) {
2918
+ keyword += ' if exists';
2919
+ }
2920
+ const token = new SqlPrintToken(SqlPrintTokenType.keyword, keyword, SqlPrintTokenContainerType.DropConstraintStatement);
2921
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2922
+ token.innerTokens.push(arg.constraintName.accept(this));
2923
+ if (arg.behavior) {
2924
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2925
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, arg.behavior));
2926
+ }
2927
+ return token;
2928
+ }
2929
+ visitExplainStatement(arg) {
2930
+ const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.ExplainStatement);
2931
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'explain'));
2932
+ const inlineFlags = [];
2933
+ const optionList = [];
2934
+ if (arg.options) {
2935
+ for (const option of arg.options) {
2936
+ if (this.isExplainLegacyFlag(option) && this.isExplainBooleanTrue(option.value)) {
2937
+ inlineFlags.push(option);
2938
+ }
2939
+ else {
2940
+ optionList.push(option);
2941
+ }
2942
+ }
2943
+ }
2944
+ for (const flag of inlineFlags) {
2945
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2946
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, flag.name.name.toLowerCase()));
2947
+ }
2948
+ if (optionList.length > 0) {
2949
+ // Keep the option list immediately after EXPLAIN without an extra space.
2950
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
2951
+ for (let i = 0; i < optionList.length; i++) {
2952
+ if (i > 0) {
2953
+ token.innerTokens.push(SqlPrintTokenParser.COMMA_TOKEN);
2954
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2955
+ }
2956
+ token.innerTokens.push(this.renderExplainOption(optionList[i]));
2957
+ }
2958
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
2959
+ }
2960
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2961
+ token.innerTokens.push(arg.statement.accept(this));
2962
+ return token;
2963
+ }
2964
+ visitAnalyzeStatement(arg) {
2965
+ const keywordParts = ['analyze'];
2966
+ if (arg.verbose) {
2967
+ keywordParts.push('verbose');
2968
+ }
2969
+ const token = new SqlPrintToken(SqlPrintTokenType.keyword, keywordParts.join(' '), SqlPrintTokenContainerType.AnalyzeStatement);
2970
+ // Render relation target when provided.
2971
+ if (arg.target) {
2972
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2973
+ token.innerTokens.push(this.renderQualifiedNameInline(arg.target));
2974
+ // Render column list inline (comma space) when present.
2975
+ if (arg.columns && arg.columns.length > 0) {
2976
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2977
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN);
2978
+ for (let i = 0; i < arg.columns.length; i++) {
2979
+ if (i > 0) {
2980
+ token.innerTokens.push(SqlPrintTokenParser.COMMA_TOKEN);
2981
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2982
+ }
2983
+ token.innerTokens.push(this.renderIdentifierInline(arg.columns[i]));
2984
+ }
2985
+ token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN);
2986
+ }
2987
+ }
2988
+ return token;
2989
+ }
2990
+ renderExplainOption(option) {
2991
+ const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.ExplainOption);
2992
+ token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, option.name.name.toLowerCase()));
2993
+ if (option.value && !this.isExplainBooleanTrue(option.value)) {
2994
+ token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN);
2995
+ token.innerTokens.push(option.value.accept(this));
2996
+ }
2997
+ return token;
2998
+ }
2999
+ isExplainLegacyFlag(option) {
3000
+ const name = option.name.name.toLowerCase();
3001
+ return name === 'analyze' || name === 'verbose';
3002
+ }
3003
+ isExplainBooleanTrue(value) {
3004
+ if (!value) {
3005
+ return false;
3006
+ }
3007
+ if (value instanceof RawString) {
3008
+ const normalized = value.value.toLowerCase();
3009
+ return normalized === 'true' || normalized === 't' || normalized === 'on' || normalized === 'yes' || normalized === '1';
3010
+ }
3011
+ if (value instanceof LiteralValue) {
3012
+ if (typeof value.value === 'boolean') {
3013
+ return value.value;
3014
+ }
3015
+ if (typeof value.value === 'number') {
3016
+ return value.value !== 0;
3017
+ }
3018
+ if (typeof value.value === 'string') {
3019
+ const normalized = value.value.toLowerCase();
3020
+ return normalized === 'true' || normalized === 't' || normalized === 'on' || normalized === 'yes' || normalized === '1';
3021
+ }
3022
+ }
3023
+ return false;
3024
+ }
3025
+ renderQualifiedNameInline(arg) {
3026
+ const parts = [];
3027
+ if (arg.namespaces && arg.namespaces.length > 0) {
3028
+ for (const ns of arg.namespaces) {
3029
+ parts.push(this.renderIdentifierText(ns));
3030
+ }
3031
+ }
3032
+ parts.push(this.renderIdentifierText(arg.name));
3033
+ return new SqlPrintToken(SqlPrintTokenType.value, parts.join('.'), SqlPrintTokenContainerType.QualifiedName);
3034
+ }
3035
+ renderIdentifierInline(component) {
3036
+ return new SqlPrintToken(SqlPrintTokenType.value, this.renderIdentifierText(component), SqlPrintTokenContainerType.IdentifierString);
3037
+ }
3038
+ renderIdentifierText(component) {
3039
+ if (component instanceof IdentifierString) {
3040
+ if (component.name === '*') {
3041
+ return component.name;
3042
+ }
3043
+ return this.identifierDecorator.decorate(component.name);
3044
+ }
3045
+ return component.value;
3046
+ }
1940
3047
  }
1941
3048
  // Static tokens for common symbols
1942
3049
  SqlPrintTokenParser.SPACE_TOKEN = new SqlPrintToken(SqlPrintTokenType.space, ' ');