rawsql-ts 0.6.0-beta → 0.7.1-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 (210) hide show
  1. package/README.md +141 -121
  2. package/dist/esm/index.js +1 -0
  3. package/dist/esm/index.js.map +1 -1
  4. package/dist/esm/models/Clause.js +111 -43
  5. package/dist/esm/models/Clause.js.map +1 -1
  6. package/dist/esm/models/CreateTableQuery.js +12 -22
  7. package/dist/esm/models/CreateTableQuery.js.map +1 -1
  8. package/dist/esm/models/KeywordTrie.js +2 -0
  9. package/dist/esm/models/KeywordTrie.js.map +1 -1
  10. package/dist/esm/models/SimpleSelectQuery.js +17 -15
  11. package/dist/esm/models/SimpleSelectQuery.js.map +1 -1
  12. package/dist/esm/models/SqlPrintToken.js +94 -0
  13. package/dist/esm/models/SqlPrintToken.js.map +1 -0
  14. package/dist/esm/models/ValueComponent.js +86 -17
  15. package/dist/esm/models/ValueComponent.js.map +1 -1
  16. package/dist/esm/parsers/FetchClauseParser.js +84 -0
  17. package/dist/esm/parsers/FetchClauseParser.js.map +1 -0
  18. package/dist/esm/parsers/FullNameParser.js +6 -1
  19. package/dist/esm/parsers/FullNameParser.js.map +1 -1
  20. package/dist/esm/parsers/IdentifierDecorator.js +13 -0
  21. package/dist/esm/parsers/IdentifierDecorator.js.map +1 -0
  22. package/dist/esm/parsers/InsertQueryParser.js +1 -1
  23. package/dist/esm/parsers/JoinClauseParser.js +13 -35
  24. package/dist/esm/parsers/JoinClauseParser.js.map +1 -1
  25. package/dist/esm/parsers/JoinOnClauseParser.js +17 -0
  26. package/dist/esm/parsers/JoinOnClauseParser.js.map +1 -0
  27. package/dist/esm/parsers/JoinUsingClauseParser.js +19 -0
  28. package/dist/esm/parsers/JoinUsingClauseParser.js.map +1 -0
  29. package/dist/esm/parsers/LimitClauseParser.js +1 -13
  30. package/dist/esm/parsers/LimitClauseParser.js.map +1 -1
  31. package/dist/esm/parsers/OffsetClauseParser.js +38 -0
  32. package/dist/esm/parsers/OffsetClauseParser.js.map +1 -0
  33. package/dist/esm/parsers/ParameterDecorator.js +36 -0
  34. package/dist/esm/parsers/ParameterDecorator.js.map +1 -0
  35. package/dist/esm/parsers/SelectClauseParser.js +25 -2
  36. package/dist/esm/parsers/SelectClauseParser.js.map +1 -1
  37. package/dist/esm/parsers/SelectQueryParser.js +40 -13
  38. package/dist/esm/parsers/SelectQueryParser.js.map +1 -1
  39. package/dist/esm/parsers/SourceParser.js +17 -13
  40. package/dist/esm/parsers/SourceParser.js.map +1 -1
  41. package/dist/esm/parsers/SqlPrintTokenParser.js +1058 -0
  42. package/dist/esm/parsers/SqlPrintTokenParser.js.map +1 -0
  43. package/dist/esm/parsers/SqlTokenizer.js +2 -0
  44. package/dist/esm/parsers/SqlTokenizer.js.map +1 -1
  45. package/dist/esm/parsers/WindowClauseParser.js +27 -15
  46. package/dist/esm/parsers/WindowClauseParser.js.map +1 -1
  47. package/dist/esm/tokenReaders/BaseTokenReader.js +3 -3
  48. package/dist/esm/tokenReaders/BaseTokenReader.js.map +1 -1
  49. package/dist/esm/tokenReaders/CommandTokenReader.js +8 -2
  50. package/dist/esm/tokenReaders/CommandTokenReader.js.map +1 -1
  51. package/dist/esm/tokenReaders/EscapedIdentifierTokenReader.js +54 -0
  52. package/dist/esm/tokenReaders/EscapedIdentifierTokenReader.js.map +1 -0
  53. package/dist/esm/tokenReaders/IdentifierTokenReader.js +0 -36
  54. package/dist/esm/tokenReaders/IdentifierTokenReader.js.map +1 -1
  55. package/dist/esm/tokenReaders/LiteralTokenReader.js +23 -6
  56. package/dist/esm/tokenReaders/LiteralTokenReader.js.map +1 -1
  57. package/dist/esm/transformers/CTECollector.js +9 -10
  58. package/dist/esm/transformers/CTECollector.js.map +1 -1
  59. package/dist/esm/transformers/CTEDisabler.js +10 -9
  60. package/dist/esm/transformers/CTEDisabler.js.map +1 -1
  61. package/dist/esm/transformers/CTEInjector.js +2 -2
  62. package/dist/esm/transformers/Formatter.js +19 -646
  63. package/dist/esm/transformers/Formatter.js.map +1 -1
  64. package/dist/esm/transformers/LinePrinter.js +81 -0
  65. package/dist/esm/transformers/LinePrinter.js.map +1 -0
  66. package/dist/esm/transformers/QueryBuilder.js +8 -18
  67. package/dist/esm/transformers/QueryBuilder.js.map +1 -1
  68. package/dist/esm/transformers/SelectableColumnCollector.js +26 -9
  69. package/dist/esm/transformers/SelectableColumnCollector.js.map +1 -1
  70. package/dist/esm/transformers/SqlFormatter.js +31 -0
  71. package/dist/esm/transformers/SqlFormatter.js.map +1 -0
  72. package/dist/esm/transformers/SqlOutputToken.js +9 -0
  73. package/dist/esm/transformers/SqlOutputToken.js.map +1 -0
  74. package/dist/esm/transformers/SqlPrinter.js +144 -0
  75. package/dist/esm/transformers/SqlPrinter.js.map +1 -0
  76. package/dist/esm/transformers/TableSourceCollector.js +32 -16
  77. package/dist/esm/transformers/TableSourceCollector.js.map +1 -1
  78. package/dist/esm/types/index.d.ts +1 -0
  79. package/dist/esm/types/models/Clause.d.ts +64 -13
  80. package/dist/esm/types/models/SimpleSelectQuery.d.ts +20 -5
  81. package/dist/esm/types/models/SqlPrintToken.d.ts +102 -0
  82. package/dist/esm/types/models/ValueComponent.d.ts +43 -9
  83. package/dist/esm/types/parsers/FetchClauseParser.d.ts +24 -0
  84. package/dist/esm/types/parsers/FullNameParser.d.ts +1 -0
  85. package/dist/esm/types/parsers/IdentifierDecorator.d.ts +9 -0
  86. package/dist/esm/types/parsers/JoinClauseParser.d.ts +0 -2
  87. package/dist/esm/types/parsers/JoinOnClauseParser.d.ts +8 -0
  88. package/dist/esm/types/parsers/JoinUsingClauseParser.d.ts +8 -0
  89. package/dist/esm/types/parsers/OffsetClauseParser.d.ts +9 -0
  90. package/dist/esm/types/parsers/ParameterDecorator.d.ts +20 -0
  91. package/dist/esm/types/parsers/SelectClauseParser.d.ts +19 -2
  92. package/dist/esm/types/parsers/SqlPrintTokenParser.d.ts +144 -0
  93. package/dist/esm/types/parsers/WindowClauseParser.d.ts +3 -3
  94. package/dist/esm/types/tokenReaders/EscapedIdentifierTokenReader.d.ts +15 -0
  95. package/dist/esm/types/tokenReaders/IdentifierTokenReader.d.ts +0 -4
  96. package/dist/esm/types/tokenReaders/LiteralTokenReader.d.ts +15 -2
  97. package/dist/esm/types/transformers/Formatter.d.ts +8 -105
  98. package/dist/esm/types/transformers/LinePrinter.d.ts +41 -0
  99. package/dist/esm/types/transformers/SelectableColumnCollector.d.ts +2 -0
  100. package/dist/esm/types/transformers/SqlFormatter.d.ts +40 -0
  101. package/dist/esm/types/transformers/SqlOutputToken.d.ts +6 -0
  102. package/dist/esm/types/transformers/SqlPrinter.d.ts +54 -0
  103. package/dist/esm/types/transformers/TableSourceCollector.d.ts +2 -0
  104. package/dist/esm/utils/stringUtils.js +17 -0
  105. package/dist/esm/utils/stringUtils.js.map +1 -1
  106. package/dist/index.d.ts +1 -0
  107. package/dist/index.js +1 -0
  108. package/dist/index.js.map +1 -1
  109. package/dist/models/Clause.d.ts +64 -13
  110. package/dist/models/Clause.js +115 -44
  111. package/dist/models/Clause.js.map +1 -1
  112. package/dist/models/CreateTableQuery.js +12 -22
  113. package/dist/models/CreateTableQuery.js.map +1 -1
  114. package/dist/models/KeywordTrie.js +2 -0
  115. package/dist/models/KeywordTrie.js.map +1 -1
  116. package/dist/models/SimpleSelectQuery.d.ts +20 -5
  117. package/dist/models/SimpleSelectQuery.js +17 -15
  118. package/dist/models/SimpleSelectQuery.js.map +1 -1
  119. package/dist/models/SqlPrintToken.d.ts +102 -0
  120. package/dist/models/SqlPrintToken.js +98 -0
  121. package/dist/models/SqlPrintToken.js.map +1 -0
  122. package/dist/models/ValueComponent.d.ts +43 -9
  123. package/dist/models/ValueComponent.js +88 -18
  124. package/dist/models/ValueComponent.js.map +1 -1
  125. package/dist/parsers/FetchClauseParser.d.ts +24 -0
  126. package/dist/parsers/FetchClauseParser.js +89 -0
  127. package/dist/parsers/FetchClauseParser.js.map +1 -0
  128. package/dist/parsers/FullNameParser.d.ts +1 -0
  129. package/dist/parsers/FullNameParser.js +6 -1
  130. package/dist/parsers/FullNameParser.js.map +1 -1
  131. package/dist/parsers/IdentifierDecorator.d.ts +9 -0
  132. package/dist/parsers/IdentifierDecorator.js +17 -0
  133. package/dist/parsers/IdentifierDecorator.js.map +1 -0
  134. package/dist/parsers/InsertQueryParser.js +1 -1
  135. package/dist/parsers/JoinClauseParser.d.ts +0 -2
  136. package/dist/parsers/JoinClauseParser.js +12 -34
  137. package/dist/parsers/JoinClauseParser.js.map +1 -1
  138. package/dist/parsers/JoinOnClauseParser.d.ts +8 -0
  139. package/dist/parsers/JoinOnClauseParser.js +21 -0
  140. package/dist/parsers/JoinOnClauseParser.js.map +1 -0
  141. package/dist/parsers/JoinUsingClauseParser.d.ts +8 -0
  142. package/dist/parsers/JoinUsingClauseParser.js +23 -0
  143. package/dist/parsers/JoinUsingClauseParser.js.map +1 -0
  144. package/dist/parsers/LimitClauseParser.js +1 -13
  145. package/dist/parsers/LimitClauseParser.js.map +1 -1
  146. package/dist/parsers/OffsetClauseParser.d.ts +9 -0
  147. package/dist/parsers/OffsetClauseParser.js +42 -0
  148. package/dist/parsers/OffsetClauseParser.js.map +1 -0
  149. package/dist/parsers/ParameterDecorator.d.ts +20 -0
  150. package/dist/parsers/ParameterDecorator.js +40 -0
  151. package/dist/parsers/ParameterDecorator.js.map +1 -0
  152. package/dist/parsers/SelectClauseParser.d.ts +19 -2
  153. package/dist/parsers/SelectClauseParser.js +28 -4
  154. package/dist/parsers/SelectClauseParser.js.map +1 -1
  155. package/dist/parsers/SelectQueryParser.js +40 -13
  156. package/dist/parsers/SelectQueryParser.js.map +1 -1
  157. package/dist/parsers/SourceParser.js +17 -13
  158. package/dist/parsers/SourceParser.js.map +1 -1
  159. package/dist/parsers/SqlPrintTokenParser.d.ts +144 -0
  160. package/dist/parsers/SqlPrintTokenParser.js +1062 -0
  161. package/dist/parsers/SqlPrintTokenParser.js.map +1 -0
  162. package/dist/parsers/SqlTokenizer.js +2 -0
  163. package/dist/parsers/SqlTokenizer.js.map +1 -1
  164. package/dist/parsers/WindowClauseParser.d.ts +3 -3
  165. package/dist/parsers/WindowClauseParser.js +26 -14
  166. package/dist/parsers/WindowClauseParser.js.map +1 -1
  167. package/dist/tokenReaders/BaseTokenReader.js +3 -3
  168. package/dist/tokenReaders/BaseTokenReader.js.map +1 -1
  169. package/dist/tokenReaders/CommandTokenReader.js +8 -2
  170. package/dist/tokenReaders/CommandTokenReader.js.map +1 -1
  171. package/dist/tokenReaders/EscapedIdentifierTokenReader.d.ts +15 -0
  172. package/dist/tokenReaders/EscapedIdentifierTokenReader.js +58 -0
  173. package/dist/tokenReaders/EscapedIdentifierTokenReader.js.map +1 -0
  174. package/dist/tokenReaders/IdentifierTokenReader.d.ts +0 -4
  175. package/dist/tokenReaders/IdentifierTokenReader.js +0 -36
  176. package/dist/tokenReaders/IdentifierTokenReader.js.map +1 -1
  177. package/dist/tokenReaders/LiteralTokenReader.d.ts +15 -2
  178. package/dist/tokenReaders/LiteralTokenReader.js +23 -6
  179. package/dist/tokenReaders/LiteralTokenReader.js.map +1 -1
  180. package/dist/transformers/CTECollector.js +9 -10
  181. package/dist/transformers/CTECollector.js.map +1 -1
  182. package/dist/transformers/CTEDisabler.js +9 -8
  183. package/dist/transformers/CTEDisabler.js.map +1 -1
  184. package/dist/transformers/CTEInjector.js +2 -2
  185. package/dist/transformers/Formatter.d.ts +8 -105
  186. package/dist/transformers/Formatter.js +20 -647
  187. package/dist/transformers/Formatter.js.map +1 -1
  188. package/dist/transformers/LinePrinter.d.ts +41 -0
  189. package/dist/transformers/LinePrinter.js +86 -0
  190. package/dist/transformers/LinePrinter.js.map +1 -0
  191. package/dist/transformers/QueryBuilder.js +8 -18
  192. package/dist/transformers/QueryBuilder.js.map +1 -1
  193. package/dist/transformers/SelectableColumnCollector.d.ts +2 -0
  194. package/dist/transformers/SelectableColumnCollector.js +25 -8
  195. package/dist/transformers/SelectableColumnCollector.js.map +1 -1
  196. package/dist/transformers/SqlFormatter.d.ts +40 -0
  197. package/dist/transformers/SqlFormatter.js +35 -0
  198. package/dist/transformers/SqlFormatter.js.map +1 -0
  199. package/dist/transformers/SqlOutputToken.d.ts +6 -0
  200. package/dist/transformers/SqlOutputToken.js +13 -0
  201. package/dist/transformers/SqlOutputToken.js.map +1 -0
  202. package/dist/transformers/SqlPrinter.d.ts +54 -0
  203. package/dist/transformers/SqlPrinter.js +148 -0
  204. package/dist/transformers/SqlPrinter.js.map +1 -0
  205. package/dist/transformers/TableSourceCollector.d.ts +2 -0
  206. package/dist/transformers/TableSourceCollector.js +30 -14
  207. package/dist/transformers/TableSourceCollector.js.map +1 -1
  208. package/dist/utils/stringUtils.js +17 -0
  209. package/dist/utils/stringUtils.js.map +1 -1
  210. package/package.json +2 -1
@@ -1,665 +1,38 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Formatter = exports.ParameterStyle = void 0;
4
- const SelectQuery_1 = require("../models/SelectQuery");
5
- const ValueComponent_1 = require("../models/ValueComponent");
6
- const Clause_1 = require("../models/Clause");
7
- const CreateTableQuery_1 = require("../models/CreateTableQuery");
8
- const InsertQuery_1 = require("../models/InsertQuery");
9
- const UpdateQuery_1 = require("../models/UpdateQuery");
10
- const ParameterCollector_1 = require("./ParameterCollector");
11
- var ParameterStyle;
12
- (function (ParameterStyle) {
13
- ParameterStyle["Anonymous"] = "anonymous";
14
- ParameterStyle["Indexed"] = "indexed";
15
- ParameterStyle["Named"] = "named";
16
- })(ParameterStyle || (exports.ParameterStyle = ParameterStyle = {}));
3
+ exports.SqlFormatter = exports.Formatter = void 0;
4
+ const SqlFormatter_1 = require("./SqlFormatter");
5
+ Object.defineProperty(exports, "SqlFormatter", { enumerable: true, get: function () { return SqlFormatter_1.SqlFormatter; } });
6
+ /**
7
+ * @deprecated The Formatter class is deprecated. Use SqlFormatter instead.
8
+ */
17
9
  class Formatter {
18
10
  constructor() {
19
- this.parameterIndex = 0;
20
- this.handlers = new Map();
21
- // Default settings
22
- this.config = {
23
- identifierEscape: {
24
- start: '"',
25
- end: '"'
26
- },
11
+ this.sqlFormatter = new SqlFormatter_1.SqlFormatter({
12
+ identifierEscape: { start: '"', end: '"' },
27
13
  parameterSymbol: ':',
28
- parameterStyle: ParameterStyle.Named,
29
- };
30
- // value
31
- this.handlers.set(ValueComponent_1.LiteralValue.kind, (expr) => this.visitLiteralExpression(expr));
32
- this.handlers.set(ValueComponent_1.RawString.kind, (expr) => this.visitRawString(expr));
33
- this.handlers.set(ValueComponent_1.StringSpecifierExpression.kind, (expr) => this.visitStringSpecifierExpression(expr));
34
- this.handlers.set(ValueComponent_1.IdentifierString.kind, (expr) => this.visitIdentifierString(expr));
35
- this.handlers.set(ValueComponent_1.SwitchCaseArgument.kind, (expr) => this.visitSwitchCaseArgument(expr));
36
- this.handlers.set(ValueComponent_1.ValueList.kind, (expr) => this.visitValueList(expr));
37
- this.handlers.set(ValueComponent_1.ColumnReference.kind, (expr) => this.visitColumnReference(expr));
38
- this.handlers.set(ValueComponent_1.FunctionCall.kind, (expr) => this.visitFunctionCall(expr));
39
- this.handlers.set(ValueComponent_1.UnaryExpression.kind, (expr) => this.visitUnaryExpression(expr));
40
- this.handlers.set(ValueComponent_1.BinaryExpression.kind, (expr) => this.visitBinaryExpression(expr));
41
- this.handlers.set(ValueComponent_1.ParameterExpression.kind, (expr) => this.visitParameterExpression(expr));
42
- this.handlers.set(Clause_1.SelectItem.kind, (expr) => this.visitSelectItemExpression(expr));
43
- this.handlers.set(ValueComponent_1.ArrayExpression.kind, (expr) => this.visitArrayExpression(expr));
44
- this.handlers.set(ValueComponent_1.CaseExpression.kind, (expr) => this.visitCaseExpression(expr));
45
- this.handlers.set(ValueComponent_1.CastExpression.kind, (expr) => this.visitCastExpression(expr));
46
- this.handlers.set(ValueComponent_1.ParenExpression.kind, (expr) => this.visitBracketExpression(expr));
47
- this.handlers.set(ValueComponent_1.BetweenExpression.kind, (expr) => this.visitBetweenExpression(expr));
48
- this.handlers.set(ValueComponent_1.TypeValue.kind, (expr) => this.visitTypeValue(expr));
49
- this.handlers.set(ValueComponent_1.InlineQuery.kind, (expr) => this.visitInlineQuery(expr));
50
- // source alias
51
- this.handlers.set(Clause_1.SourceAliasExpression.kind, (expr) => this.visitSourceAliasExpression(expr));
52
- // from
53
- this.handlers.set(Clause_1.FromClause.kind, (expr) => this.visitFromClause(expr));
54
- this.handlers.set(Clause_1.JoinClause.kind, (expr) => this.visitJoinClause(expr));
55
- this.handlers.set(Clause_1.JoinOnClause.kind, (expr) => this.visitJoinOnClause(expr));
56
- this.handlers.set(Clause_1.JoinUsingClause.kind, (expr) => this.visitJoinUsingClause(expr));
57
- this.handlers.set(Clause_1.SourceExpression.kind, (expr) => this.visitSourceExpression(expr));
58
- this.handlers.set(Clause_1.SubQuerySource.kind, (expr) => this.visitSubQuerySource(expr));
59
- this.handlers.set(Clause_1.FunctionSource.kind, (expr) => this.visitFunctionSource(expr));
60
- this.handlers.set(Clause_1.TableSource.kind, (expr) => this.visitTableSource(expr));
61
- // order by
62
- this.handlers.set(Clause_1.OrderByClause.kind, (expr) => this.visitOrderByClause(expr));
63
- this.handlers.set(Clause_1.OrderByItem.kind, (expr) => this.visitOrderByItem(expr));
64
- // partition by
65
- this.handlers.set(Clause_1.PartitionByClause.kind, (expr) => this.visitPartitionByClause(expr));
66
- // window frame
67
- this.handlers.set(ValueComponent_1.WindowFrameExpression.kind, (expr) => this.visitWindowFrameExpression(expr));
68
- this.handlers.set(ValueComponent_1.WindowFrameSpec.kind, (arg) => this.visitWindowFrameSpec(arg));
69
- this.handlers.set(ValueComponent_1.WindowFrameBoundStatic.kind, (arg) => this.visitWindowFrameBoundStatic(arg));
70
- this.handlers.set(ValueComponent_1.WindowFrameBoundaryValue.kind, (arg) => this.visitWindowFrameBoundaryValue(arg));
71
- this.handlers.set(Clause_1.WindowFrameClause.kind, (arg) => this.visitWindowFrameClause(arg));
72
- // where
73
- this.handlers.set(Clause_1.WhereClause.kind, (expr) => this.visitWhereClause(expr));
74
- // group by
75
- this.handlers.set(Clause_1.GroupByClause.kind, (expr) => this.visitGroupByClause(expr));
76
- this.handlers.set(Clause_1.HavingClause.kind, (expr) => this.visitHavingClause(expr));
77
- // with
78
- this.handlers.set(Clause_1.CommonTable.kind, (expr) => this.visitCommonTable(expr));
79
- this.handlers.set(Clause_1.WithClause.kind, (expr) => this.visitWithClause(expr));
80
- // select
81
- this.handlers.set(Clause_1.SelectClause.kind, (expr) => this.visitSelectClause(expr));
82
- this.handlers.set(Clause_1.Distinct.kind, (expr) => this.visitDistinct(expr));
83
- this.handlers.set(Clause_1.DistinctOn.kind, (expr) => this.visitDistinctOn(expr));
84
- // row limit
85
- this.handlers.set(Clause_1.LimitClause.kind, (expr) => this.visitLimitClause(expr));
86
- this.handlers.set(Clause_1.FetchSpecification.kind, (expr) => this.visitFetchSpecification(expr));
87
- // for clause
88
- this.handlers.set(Clause_1.ForClause.kind, (expr) => this.visitForClause(expr));
89
- // values clause
90
- this.handlers.set(SelectQuery_1.ValuesQuery.kind, (expr) => this.visitValuesQuery(expr));
91
- this.handlers.set(ValueComponent_1.TupleExpression.kind, (expr) => this.visitTupleExpression(expr));
92
- // select query
93
- this.handlers.set(SelectQuery_1.SimpleSelectQuery.kind, (expr) => this.visitSelectQuery(expr));
94
- this.handlers.set(SelectQuery_1.BinarySelectQuery.kind, (expr) => this.visitBinarySelectQuery(expr));
95
- // create table query
96
- this.handlers.set(CreateTableQuery_1.CreateTableQuery.kind, (expr) => this.visitCreateTableQuery(expr));
97
- // update query
98
- this.handlers.set(UpdateQuery_1.UpdateQuery.kind, (expr) => this.visitUpdateQuery(expr));
99
- this.handlers.set(Clause_1.UpdateClause.kind, (expr) => this.visitUpdateClause(expr));
100
- this.handlers.set(Clause_1.SetClause.kind, (expr) => this.visitSetClause(expr));
101
- this.handlers.set(Clause_1.SetClauseItem.kind, (expr) => this.visitSetClauseItem(expr));
102
- this.handlers.set(Clause_1.ReturningClause.kind, (expr) => this.visitReturningClause(expr));
103
- // insert query
104
- this.handlers.set(InsertQuery_1.InsertQuery.kind, (expr) => this.visitInsertQuery(expr));
105
- this.handlers.set(Clause_1.InsertClause.kind, (expr) => this.visitInsertClause(expr));
14
+ parameterStyle: 'named' // Default to 'named' for backward compatibility
15
+ });
106
16
  }
107
- /**
108
- * Formats the given SQL AST node into a SQL string.
109
- * This is the recommended public API for users.
110
- * @param arg The root SQL AST node to format.
111
- * @param config (Optional) Formatter configuration.
112
- * @returns The formatted SQL string.
113
- */
114
17
  format(arg, config = null) {
115
- this.parameterIndex = 0; // Reset counter for each format
18
+ // Use the sqlFormatter instance to format the SQL component
116
19
  if (config) {
117
- // Always reset to default before merging user config
118
- this.config = Object.assign({ identifierEscape: { start: '"', end: '"' }, parameterSymbol: ':' }, config);
20
+ this.sqlFormatter = new SqlFormatter_1.SqlFormatter(config);
119
21
  }
120
- return this.visit(arg);
22
+ const result = this.sqlFormatter.format(arg);
23
+ return result.formattedSql;
121
24
  }
122
25
  formatWithParameters(arg, config = null) {
123
- var _a;
124
- const sql = this.format(arg, config); // Sort parameters by index
125
- const paramsRaw = ParameterCollector_1.ParameterCollector.collect(arg).sort((a, b) => { var _a, _b; return ((_a = a.index) !== null && _a !== void 0 ? _a : 0) - ((_b = b.index) !== null && _b !== void 0 ? _b : 0); });
126
- const style = ((_a = this.config.parameterStyle) !== null && _a !== void 0 ? _a : ParameterStyle.Named);
127
- if (style === ParameterStyle.Named) {
128
- // Named: { name: value, ... }
129
- const paramsObj = {};
130
- for (const p of paramsRaw) {
131
- const key = p.name.value;
132
- if (paramsObj.hasOwnProperty(key)) {
133
- if (paramsObj[key] !== p.value) {
134
- throw new Error(`Duplicate parameter name '${key}' with different values detected during query composition.`);
135
- }
136
- // If value is the same, skip (already set)
137
- continue;
138
- }
139
- paramsObj[key] = p.value;
140
- }
141
- return { sql, params: paramsObj };
142
- }
143
- else if (style === ParameterStyle.Indexed) {
144
- // Indexed: [value1, value2, ...] (sorted by index)
145
- const paramsArr = paramsRaw.map(p => p.value);
146
- return { sql, params: paramsArr };
147
- }
148
- else if (style === ParameterStyle.Anonymous) {
149
- // Anonymous: [value1, value2, ...] (sorted by index, name is empty)
150
- const paramsArr = paramsRaw.map(p => p.value);
151
- return { sql, params: paramsArr };
26
+ // Use the sqlFormatter instance to format the SQL component with parameters
27
+ if (config) {
28
+ this.sqlFormatter = new SqlFormatter_1.SqlFormatter(config);
152
29
  }
153
- // Fallback (just in case)
154
- return { sql, params: [] };
30
+ const result = this.sqlFormatter.format(arg);
31
+ return { sql: result.formattedSql, params: result.params };
155
32
  }
156
- /**
157
- * Visitor entry point for SQL AST nodes.
158
- * Note: This method is public only for interface compatibility.
159
- * Users should call the format() method instead of visit() directly.
160
- * (If you call visit() directly, you are basically breaking the abstraction, so don't do it!)
161
- * @param arg The SQL AST node to visit.
162
- * @returns The formatted SQL string for the node.
163
- */
164
33
  visit(arg) {
165
- var _a, _b;
166
- const handler = this.handlers.get(arg.getKind());
167
- if (handler) {
168
- return handler(arg);
169
- }
170
- // Provide more detailed error message
171
- const kindSymbol = ((_a = arg.getKind()) === null || _a === void 0 ? void 0 : _a.toString()) || 'unknown';
172
- const constructor = ((_b = arg.constructor) === null || _b === void 0 ? void 0 : _b.name) || 'unknown';
173
- throw new Error(`[Formatter] No handler for ${constructor} with kind ${kindSymbol}.`);
174
- }
175
- visitBinarySelectQuery(arg) {
176
- const left = arg.left.accept(this);
177
- const operator = arg.operator.accept(this);
178
- const right = arg.right.accept(this);
179
- return `${left} ${operator} ${right}`;
180
- }
181
- visitWindowFrameBoundaryValue(arg) {
182
- const value = arg.value.accept(this);
183
- const following = arg.isFollowing ? "following" : "preceding";
184
- return `${value} ${following}`;
185
- }
186
- visitWindowFrameBoundStatic(arg) {
187
- switch (arg.bound) {
188
- case ValueComponent_1.WindowFrameBound.UnboundedPreceding:
189
- return "unbounded preceding";
190
- case ValueComponent_1.WindowFrameBound.CurrentRow:
191
- return "current row";
192
- case ValueComponent_1.WindowFrameBound.UnboundedFollowing:
193
- return "unbounded following";
194
- default:
195
- throw new Error(`Unknown WindowFrameBound: ${arg.bound}`);
196
- }
197
- }
198
- visitWindowFrameExpression(arg) {
199
- const partitionBy = arg.partition !== null ? arg.partition.accept(this) : null;
200
- const orderBy = arg.order !== null ? arg.order.accept(this) : null;
201
- const frameSpec = arg.frameSpec !== null ? arg.frameSpec.accept(this) : null;
202
- const parts = [];
203
- if (partitionBy)
204
- parts.push(partitionBy);
205
- if (orderBy)
206
- parts.push(orderBy);
207
- if (frameSpec)
208
- parts.push(frameSpec);
209
- if (parts.length > 0) {
210
- return `(${parts.join(" ")})`;
211
- }
212
- return `()`;
213
- }
214
- visitWindowFrameSpec(arg) {
215
- const frameType = arg.frameType;
216
- const startBound = arg.startBound.accept(this);
217
- if (arg.endBound === null) {
218
- return `${frameType} ${startBound}`;
219
- }
220
- else {
221
- const endBound = arg.endBound.accept(this);
222
- return `${frameType} between ${startBound} and ${endBound}`;
223
- }
224
- }
225
- visitJoinUsingClause(arg) {
226
- return `using (${arg.condition.accept(this)})`;
227
- }
228
- visitJoinOnClause(arg) {
229
- if (arg.condition !== null) {
230
- return `on ${arg.condition.accept(this)}`;
231
- }
232
- return `on`;
233
- }
234
- visitTypeValue(arg) {
235
- let typeStr = '';
236
- if (arg.namespaces && arg.namespaces.length > 0) {
237
- typeStr = arg.namespaces.map(ns => ns.accept(this)).join('.') + '.' + arg.name.accept(this);
238
- }
239
- else {
240
- typeStr = arg.name.accept(this);
241
- }
242
- if (arg.argument !== null) {
243
- return `${typeStr}(${arg.argument.accept(this)})`;
244
- }
245
- return `${typeStr}`;
246
- }
247
- visitStringSpecifierExpression(arg) {
248
- return `${arg.specifier.accept(this)}${arg.value.accept(this)}`;
249
- }
250
- visitWithClause(arg) {
251
- const part = arg.tables.map((e) => e.accept(this)).join(", ");
252
- if (arg.recursive) {
253
- return `with recursive ${part}`;
254
- }
255
- return `with ${part}`;
256
- }
257
- visitCommonTable(arg) {
258
- const alias = arg.aliasExpression.accept(this);
259
- const materil = arg.materialized === null
260
- ? ''
261
- : arg.materialized ? 'materialized' : 'not materialized';
262
- if (alias && materil) {
263
- return `${alias} ${materil} as (${arg.query.accept(this)})`;
264
- }
265
- return `${alias} as (${arg.query.accept(this)})`;
266
- }
267
- visitDistinctOn(arg) {
268
- return `distinct on(${arg.value.accept(this)})`;
269
- }
270
- visitDistinct(arg) {
271
- return `distinct`;
272
- }
273
- visitHavingClause(arg) {
274
- return `having ${arg.condition.accept(this)}`;
275
- }
276
- visitGroupByClause(arg) {
277
- const part = arg.grouping.map((e) => e.accept(this)).join(", ");
278
- return `group by ${part}`;
279
- }
280
- visitFromClause(arg) {
281
- if (arg.joins !== null && arg.joins.length > 0) {
282
- const part = arg.joins.map((e) => e.accept(this)).join(" ");
283
- return `from ${arg.source.accept(this)} ${part}`;
284
- }
285
- return `from ${arg.source.accept(this)}`;
286
- }
287
- visitJoinClause(arg) {
288
- const joinType = `${arg.joinType.accept(this)}`;
289
- const lateral = arg.lateral === true ? ` lateral` : "";
290
- const joinSource = arg.source.accept(this);
291
- const condition = arg.condition !== null ? ` ${arg.condition.accept(this)}` : "";
292
- return `${joinType}${lateral} ${joinSource}${condition}`;
293
- }
294
- visitSourceAliasExpression(arg) {
295
- const columnAlias = arg.columns !== null ? `(${arg.columns.map((e) => e.accept(this)).join(", ")})` : null;
296
- const tableAlias = arg.table !== null ? `${arg.table.accept(this)}` : "";
297
- if (columnAlias && tableAlias) {
298
- return `${tableAlias}${columnAlias}`;
299
- }
300
- if (tableAlias) {
301
- return tableAlias;
302
- }
303
- throw new Error("Invalid SourceAliasExpression: tableAlias is null");
304
- }
305
- visitSourceExpression(arg) {
306
- let alias = arg.aliasExpression !== null ? `${arg.aliasExpression.accept(this)}` : "";
307
- // Avoid duplicate alias if the name is the same as the alias
308
- if (arg.datasource instanceof Clause_1.TableSource) {
309
- if (arg.aliasExpression !== null && arg.datasource.identifier !== null && arg.datasource.identifier.accept(this) === arg.aliasExpression.accept(this)) {
310
- alias = "";
311
- }
312
- }
313
- if (alias) {
314
- return `${arg.datasource.accept(this)} as ${alias}`;
315
- }
316
- return `${arg.datasource.accept(this)}`;
317
- }
318
- visitSubQuerySource(arg) {
319
- return `(${arg.query.accept(this)})`;
320
- }
321
- visitFunctionSource(arg) {
322
- if (arg.argument !== null) {
323
- return `${arg.name.accept(this)}(${arg.argument.accept(this)})`;
324
- }
325
- return `${arg.name.accept(this)}()`;
326
- }
327
- visitTableSource(arg) {
328
- if (arg.namespaces !== null) {
329
- return `${arg.namespaces.map((ns) => `${ns.accept(this)}`).join(".")}.${arg.table.accept(this)}`;
330
- }
331
- return `${arg.table.accept(this)}`;
332
- }
333
- visitValueList(arg) {
334
- return `${arg.values.map((v) => v.accept(this)).join(", ")}`;
335
- }
336
- visitSwitchCaseArgument(arg) {
337
- const casePart = arg.cases.map((kv) => `when ${kv.key.accept(this)} then ${kv.value.accept(this)}`).join(" ");
338
- const elsePart = arg.elseValue ? ` else ${arg.elseValue.accept(this)}` : "";
339
- return `${casePart}${elsePart}`;
340
- }
341
- visitColumnReference(arg) {
342
- if (arg.namespaces != null) {
343
- return `${arg.namespaces.map((ns) => `${ns.accept(this)}`).join(".")}.${arg.column.accept(this)}`;
344
- }
345
- return `${arg.column.accept(this)}`;
346
- }
347
- visitFunctionCall(arg) {
348
- const partArg = arg.argument !== null ? arg.argument.accept(this) : "";
349
- let funcName;
350
- if (arg.namespaces && arg.namespaces.length > 0) {
351
- funcName = arg.namespaces.map(ns => ns.accept(this)).join(".") + "." + arg.name.accept(this);
352
- }
353
- else {
354
- funcName = arg.name.accept(this);
355
- }
356
- if (arg.over === null) {
357
- return `${funcName}(${partArg})`;
358
- }
359
- else {
360
- let partOver = arg.over !== null ? `${arg.over.accept(this)}` : "";
361
- if (partOver) {
362
- if (partOver.startsWith("(")) {
363
- partOver = ` over${partOver}`;
364
- }
365
- else {
366
- partOver = ` over ${partOver}`;
367
- }
368
- }
369
- return `${funcName}(${partArg})${partOver}`;
370
- }
371
- }
372
- visitUnaryExpression(arg) {
373
- return `${arg.operator.accept(this)} ${arg.expression.accept(this)}`;
374
- }
375
- visitBinaryExpression(arg) {
376
- return `${arg.left.accept(this)} ${arg.operator.accept(this)} ${arg.right.accept(this)}`;
377
- }
378
- visitLiteralExpression(arg) {
379
- if (typeof arg.value === "string") {
380
- return `'${arg.value.replace(/'/g, "''")}'`;
381
- }
382
- else if (arg.value === null) {
383
- return "null";
384
- }
385
- return arg.value.toString();
386
- }
387
- visitParameterExpression(arg) {
388
- var _a, _b;
389
- // update index
390
- arg.index = this.parameterIndex;
391
- this.parameterIndex++;
392
- // Decide output style based on config
393
- const style = (_a = this.config.parameterStyle) !== null && _a !== void 0 ? _a : ParameterStyle.Named;
394
- if (style === ParameterStyle.Anonymous) {
395
- return '?';
396
- }
397
- if (style === ParameterStyle.Indexed) {
398
- return `$${arg.index + 1}`; // 0-based to 1-based
399
- }
400
- // Named (default)
401
- if (typeof this.config.parameterSymbol === 'object' && this.config.parameterSymbol !== null) {
402
- return `${this.config.parameterSymbol.start}${arg.name.accept(this)}${this.config.parameterSymbol.end}`;
403
- }
404
- return `${(_b = this.config.parameterSymbol) !== null && _b !== void 0 ? _b : ':'}${arg.name.accept(this)}`;
405
- }
406
- visitSelectItemExpression(arg) {
407
- if (arg.identifier) {
408
- if (arg.value instanceof ValueComponent_1.ColumnReference) {
409
- const c = arg.value;
410
- if (c.column.name === arg.identifier.name) {
411
- return `${arg.value.accept(this)}`;
412
- }
413
- else {
414
- return `${arg.value.accept(this)} as ${arg.identifier.accept(this)}`;
415
- }
416
- }
417
- return `${arg.value.accept(this)} as ${arg.identifier.accept(this)}`;
418
- }
419
- return arg.value.accept(this);
420
- }
421
- visitSelectClause(arg) {
422
- const distinct = arg.distinct !== null ? " " + arg.distinct.accept(this) : "";
423
- const colum = arg.items.map((e) => e.accept(this)).join(", ");
424
- return `select${distinct} ${colum}`;
425
- }
426
- visitSelectQuery(arg) {
427
- const parts = [];
428
- // WITH
429
- if (arg.WithClause !== null) {
430
- parts.push(arg.WithClause.accept(this));
431
- }
432
- parts.push(arg.selectClause.accept(this));
433
- if (arg.fromClause !== null) {
434
- parts.push(arg.fromClause.accept(this));
435
- }
436
- if (arg.whereClause !== null) {
437
- parts.push(arg.whereClause.accept(this));
438
- }
439
- if (arg.groupByClause !== null) {
440
- parts.push(arg.groupByClause.accept(this));
441
- }
442
- if (arg.havingClause !== null) {
443
- parts.push(arg.havingClause.accept(this));
444
- }
445
- if (arg.windowFrameClause !== null) {
446
- parts.push(arg.windowFrameClause.accept(this));
447
- }
448
- if (arg.orderByClause !== null) {
449
- parts.push(arg.orderByClause.accept(this));
450
- }
451
- if (arg.rowLimitClause !== null) {
452
- parts.push(arg.rowLimitClause.accept(this));
453
- }
454
- if (arg.forClause !== null) {
455
- parts.push(arg.forClause.accept(this));
456
- }
457
- return parts.join(" ");
458
- }
459
- visitArrayExpression(arg) {
460
- return `array[${arg.expression.accept(this)}]`;
461
- }
462
- visitCaseExpression(arg) {
463
- if (arg.condition !== null) {
464
- return `case ${arg.condition.accept(this)} ${arg.switchCase.accept(this)} end`;
465
- }
466
- return `case ${arg.switchCase.accept(this)} end`;
467
- }
468
- visitCastExpression(arg) {
469
- return `${arg.input.accept(this)}::${arg.castType.accept(this)}`;
470
- }
471
- visitBracketExpression(arg) {
472
- return `(${arg.expression.accept(this)})`;
473
- }
474
- visitBetweenExpression(arg) {
475
- if (arg.negated) {
476
- return `${arg.expression.accept(this)} not between ${arg.lower.accept(this)} and ${arg.upper.accept(this)}`;
477
- }
478
- return `${arg.expression.accept(this)} between ${arg.lower.accept(this)} and ${arg.upper.accept(this)}`;
479
- }
480
- visitPartitionByClause(arg) {
481
- return `partition by ${arg.value.accept(this)}`;
482
- }
483
- visitOrderByClause(arg) {
484
- const part = arg.order.map((e) => e.accept(this)).join(", ");
485
- return `order by ${part}`;
486
- }
487
- visitOrderByItem(arg) {
488
- const direction = arg.sortDirection === Clause_1.SortDirection.Ascending ? null : "desc";
489
- const nullsOption = arg.nullsPosition !== null ? (arg.nullsPosition === Clause_1.NullsSortDirection.First ? "nulls first" : "nulls last") : null;
490
- if (direction !== null && nullsOption !== null) {
491
- return `${arg.value.accept(this)} ${direction} ${nullsOption}`;
492
- }
493
- else if (direction !== null) {
494
- return `${arg.value.accept(this)} ${direction}`;
495
- }
496
- else if (nullsOption !== null) {
497
- return `${arg.value.accept(this)} ${nullsOption}`;
498
- }
499
- return arg.value.accept(this);
500
- }
501
- visitWindowFrameClause(arg) {
502
- const partExpr = arg.expression.accept(this);
503
- return `window ${arg.name.accept(this)} as ${partExpr}`;
504
- }
505
- visitLimitClause(arg) {
506
- if (arg.offset !== null) {
507
- return `limit ${arg.limit.accept(this)} offset ${arg.offset.accept(this)}`;
508
- }
509
- return `limit ${arg.limit.accept(this)}`;
510
- }
511
- visitFetchSpecification(arg) {
512
- const type = arg.type === Clause_1.FetchType.First ? 'first' : 'next';
513
- const count = arg.count.accept(this);
514
- if (arg.unit !== null) {
515
- return `fetch ${type} ${count} ${arg.unit}`;
516
- }
517
- return `fetch ${type} ${count}`;
518
- }
519
- visitForClause(arg) {
520
- return `for ${arg.lockMode}`;
521
- }
522
- visitWhereClause(arg) {
523
- return `where ${arg.condition.accept(this)}`;
524
- }
525
- visitInlineQuery(arg) {
526
- return `(${arg.selectQuery.accept(this)})`;
527
- }
528
- visitRawString(arg) {
529
- const invalidChars = new Set(["'", '"', ",", ";", ":", ".", "--", "/*"]);
530
- if (invalidChars.has(arg.value)) {
531
- throw new Error(`invalid keyword: ${arg.value} `);
532
- }
533
- else if (arg.value.trim() === "") {
534
- throw new Error("invalid keyword: empty string");
535
- }
536
- return arg.value.trim();
537
- }
538
- visitIdentifierString(arg) {
539
- var _a;
540
- // No need to escape wildcards
541
- if (arg.name === '*') {
542
- return arg.name;
543
- }
544
- const escape = (_a = this.config.identifierEscape) !== null && _a !== void 0 ? _a : { start: '"', end: '"' };
545
- return `${escape.start}${arg.name}${escape.end}`;
546
- }
547
- visitValuesQuery(arg) {
548
- const tuples = arg.tuples.map((tuple) => tuple.accept(this)).join(", ");
549
- return `values ${tuples}`;
550
- }
551
- visitTupleExpression(arg) {
552
- const values = arg.values.map((value) => value.accept(this)).join(", ");
553
- return `(${values})`;
554
- }
555
- /**
556
- * Formats a CreateTableQuery into SQL string.
557
- */
558
- visitCreateTableQuery(arg) {
559
- const temp = arg.isTemporary ? "temporary " : "";
560
- let sql = `create ${temp}table ${arg.tableName.accept(this)}`;
561
- if (arg.asSelectQuery) {
562
- sql += ` as ${this.visit(arg.asSelectQuery)}`;
563
- }
564
- return sql;
565
- }
566
- visitInsertQuery(arg) {
567
- const parts = [];
568
- parts.push(arg.insertClause.accept(this));
569
- if (arg.selectQuery) {
570
- parts.push(arg.selectQuery.accept(this));
571
- }
572
- else {
573
- throw new Error("InsertQuery must have selectQuery (SELECT or VALUES)");
574
- }
575
- return parts.join(" ");
576
- }
577
- visitUpdateQuery(arg) {
578
- // Format: [WITH ...] UPDATE [source] SET ... [FROM ...] [WHERE ...] [RETURNING ...]
579
- const parts = [];
580
- // Add WITH clause if present
581
- if (arg.withClause) {
582
- parts.push(arg.withClause.accept(this));
583
- }
584
- // updateClause (SourceExpression) を使う
585
- parts.push(arg.updateClause.accept(this));
586
- if (arg.setClause.items.length === 0) {
587
- throw new Error("UpdateQuery must have setClause");
588
- }
589
- parts.push(arg.setClause.accept(this));
590
- if (arg.fromClause) {
591
- parts.push(arg.fromClause.accept(this));
592
- }
593
- if (arg.whereClause) {
594
- parts.push(arg.whereClause.accept(this));
595
- }
596
- if (arg.returningClause) {
597
- parts.push(arg.returningClause.accept(this));
598
- }
599
- return parts.join(" ");
600
- }
601
- visitUpdateClause(arg) {
602
- // Format: UPDATE table [AS alias]
603
- const table = arg.source.accept(this);
604
- return `update ${table}`;
605
- }
606
- visitSetClause(arg) {
607
- // Format: SET col1 = val1, col2 = val2, ...
608
- const items = arg.items.map(item => item.accept(this)).join(", ");
609
- return `set ${items}`;
610
- }
611
- visitSetClauseItem(arg) {
612
- // Format: col1 = val1 (with optional namespaces)
613
- let column;
614
- if (arg.namespaces && arg.namespaces.length > 0) {
615
- column = arg.namespaces.map(ns => ns.accept(this)).join(".") + "." + arg.column.accept(this);
616
- }
617
- else {
618
- column = arg.column.accept(this);
619
- }
620
- const value = arg.value.accept(this);
621
- return `${column} = ${value}`;
622
- }
623
- visitReturningClause(arg) {
624
- // Format: RETURNING col1, col2, ...
625
- const columns = arg.columns.map(col => col.accept(this)).join(", ");
626
- return `returning ${columns}`;
627
- }
628
- visitInsertClause(arg) {
629
- const table = arg.source.accept(this);
630
- const columns = arg.columns.map(col => new ValueComponent_1.IdentifierString(col).accept(this)).join(", ");
631
- if (arg.columns.length > 0) {
632
- return `insert into ${table}(${columns})`;
633
- }
634
- else {
635
- return `insert into ${table}`;
636
- }
34
+ return this.format(arg);
637
35
  }
638
36
  }
639
37
  exports.Formatter = Formatter;
640
- /**
641
- * Preset configs for common DB dialects.
642
- */
643
- Formatter.PRESETS = {
644
- mysql: {
645
- identifierEscape: { start: '`', end: '`' },
646
- parameterSymbol: '?',
647
- parameterStyle: ParameterStyle.Anonymous,
648
- },
649
- postgres: {
650
- identifierEscape: { start: '"', end: '"' },
651
- parameterSymbol: ':',
652
- parameterStyle: ParameterStyle.Indexed,
653
- },
654
- sqlserver: {
655
- identifierEscape: { start: '[', end: ']' },
656
- parameterSymbol: '@',
657
- parameterStyle: ParameterStyle.Named,
658
- },
659
- sqlite: {
660
- identifierEscape: { start: '"', end: '"' },
661
- parameterSymbol: ':',
662
- parameterStyle: ParameterStyle.Named,
663
- },
664
- };
665
38
  //# sourceMappingURL=Formatter.js.map