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