dsl-to-sql 1.0.0-fabric-1p-development.1
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.
- package/.turbo/turbo-build.log +9 -0
- package/.turbo/turbo-check-types.log +4 -0
- package/.turbo/turbo-lint.log +126 -0
- package/README.md +45 -0
- package/coverage/base.css +224 -0
- package/coverage/block-navigation.js +87 -0
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +236 -0
- package/coverage/prettify.css +1 -0
- package/coverage/prettify.js +2 -0
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +210 -0
- package/coverage/src/constants.ts.html +826 -0
- package/coverage/src/database_types.ts.html +136 -0
- package/coverage/src/epm-query-builder/EpmQueryBuilder.ts.html +166 -0
- package/coverage/src/epm-query-builder/base/BaseAdvancedAggregations.ts.html +568 -0
- package/coverage/src/epm-query-builder/base/BaseAnalyticalFunctions.ts.html +694 -0
- package/coverage/src/epm-query-builder/base/BaseCTEGenerator.ts.html +1459 -0
- package/coverage/src/epm-query-builder/base/BaseCountQueryBuilder.ts.html +400 -0
- package/coverage/src/epm-query-builder/base/BaseMeasureBuilder.ts.html +295 -0
- package/coverage/src/epm-query-builder/base/BaseOrderBuilder.ts.html +670 -0
- package/coverage/src/epm-query-builder/base/BasePaginationBuilder.ts.html +364 -0
- package/coverage/src/epm-query-builder/base/BaseQueryBuilder.ts.html +238 -0
- package/coverage/src/epm-query-builder/base/BaseRollupBuilder.ts.html +532 -0
- package/coverage/src/epm-query-builder/base/BaseSqlBuilder.ts.html +601 -0
- package/coverage/src/epm-query-builder/base/BaseSuperFilterBuilder.ts.html +1966 -0
- package/coverage/src/epm-query-builder/base/BaseUtilities.ts.html +1798 -0
- package/coverage/src/epm-query-builder/base/ColumnRefUtils.ts.html +211 -0
- package/coverage/src/epm-query-builder/base/RelationshipResolver.ts.html +706 -0
- package/coverage/src/epm-query-builder/base/SharedFilterBuilder.ts.html +1717 -0
- package/coverage/src/epm-query-builder/base/index.html +326 -0
- package/coverage/src/epm-query-builder/constants/Aggregations.ts.html +133 -0
- package/coverage/src/epm-query-builder/constants/Database.ts.html +103 -0
- package/coverage/src/epm-query-builder/constants/Source.ts.html +106 -0
- package/coverage/src/epm-query-builder/constants/index.html +146 -0
- package/coverage/src/epm-query-builder/dialects/duckdb/DuckDbAdvancedAggregations.ts.html +286 -0
- package/coverage/src/epm-query-builder/dialects/duckdb/DuckDbJoinBuilder.ts.html +280 -0
- package/coverage/src/epm-query-builder/dialects/duckdb/DuckDbMeasureBuilder.ts.html +1924 -0
- package/coverage/src/epm-query-builder/dialects/duckdb/DuckDbOrderBuilder.ts.html +769 -0
- package/coverage/src/epm-query-builder/dialects/duckdb/DuckDbPaginationBuilder.ts.html +643 -0
- package/coverage/src/epm-query-builder/dialects/duckdb/DuckDbQueryBuilder.ts.html +2644 -0
- package/coverage/src/epm-query-builder/dialects/duckdb/DuckDbRollupBuilder.ts.html +478 -0
- package/coverage/src/epm-query-builder/dialects/duckdb/DuckDbSuperFilterBuilder.ts.html +1195 -0
- package/coverage/src/epm-query-builder/dialects/duckdb/index.html +221 -0
- package/coverage/src/epm-query-builder/errors/QueryBuilderErrors.ts.html +280 -0
- package/coverage/src/epm-query-builder/errors/index.html +116 -0
- package/coverage/src/epm-query-builder/index.html +116 -0
- package/coverage/src/epm-query-builder/interfaces/IDatabaseQueryBuilder.ts.html +100 -0
- package/coverage/src/epm-query-builder/interfaces/index.html +131 -0
- package/coverage/src/epm-query-builder/interfaces/index.ts.html +88 -0
- package/coverage/src/epm-query-builder/utils/format.ts.html +151 -0
- package/coverage/src/epm-query-builder/utils/index.html +146 -0
- package/coverage/src/epm-query-builder/utils/sql.ts.html +247 -0
- package/coverage/src/epm-query-builder/utils/validation.ts.html +124 -0
- package/coverage/src/epm-query-builder/validation/QueryOptionsValidator.ts.html +631 -0
- package/coverage/src/epm-query-builder/validation/SqlQueryValidator.ts.html +475 -0
- package/coverage/src/epm-query-builder/validation/index.html +131 -0
- package/coverage/src/filters/FilterConditionBuilder.ts.html +427 -0
- package/coverage/src/filters/filter-types.ts.html +484 -0
- package/coverage/src/filters/index.html +131 -0
- package/coverage/src/index.html +176 -0
- package/coverage/src/index.ts.html +103 -0
- package/coverage/src/js-lib/JsToSqlParser.ts.html +736 -0
- package/coverage/src/js-lib/ParseContext.ts.html +532 -0
- package/coverage/src/js-lib/db/azuresql/AzureSqlCallExpressionVisitor.ts.html +196 -0
- package/coverage/src/js-lib/db/azuresql/index.html +116 -0
- package/coverage/src/js-lib/db/base/ArrayExpressionVisitor.ts.html +133 -0
- package/coverage/src/js-lib/db/base/AssignmentExpressionVisitor.ts.html +187 -0
- package/coverage/src/js-lib/db/base/BinaryExpressionVisitor.ts.html +223 -0
- package/coverage/src/js-lib/db/base/CallExpressionVisitor.ts.html +5479 -0
- package/coverage/src/js-lib/db/base/IdentifierVisitor.ts.html +283 -0
- package/coverage/src/js-lib/db/base/LiteralVisitor.ts.html +199 -0
- package/coverage/src/js-lib/db/base/MemberExpressionVisitor.ts.html +193 -0
- package/coverage/src/js-lib/db/base/ProgramVisitor.ts.html +139 -0
- package/coverage/src/js-lib/db/base/UnaryExpressionVisitor.ts.html +181 -0
- package/coverage/src/js-lib/db/base/VisitorInterface.ts.html +103 -0
- package/coverage/src/js-lib/db/base/index.html +251 -0
- package/coverage/src/js-lib/db/bigquery/BigQueryCallExpressionVisitor.ts.html +1747 -0
- package/coverage/src/js-lib/db/bigquery/index.html +116 -0
- package/coverage/src/js-lib/db/commonTransforms.ts.html +2074 -0
- package/coverage/src/js-lib/db/databricks/DatabricksCallExpressionVisitor.ts.html +1303 -0
- package/coverage/src/js-lib/db/databricks/index.html +116 -0
- package/coverage/src/js-lib/db/fabricsql/FabricSqlCallExpressionVisitor.ts.html +196 -0
- package/coverage/src/js-lib/db/fabricsql/index.html +116 -0
- package/coverage/src/js-lib/db/fabricwarehouse/FabricWarehouseCallExpressionVisitor.ts.html +292 -0
- package/coverage/src/js-lib/db/fabricwarehouse/index.html +116 -0
- package/coverage/src/js-lib/db/index.html +116 -0
- package/coverage/src/js-lib/db/postgresql/PostgreSqlCallExpressionVisitor.ts.html +985 -0
- package/coverage/src/js-lib/db/postgresql/index.html +116 -0
- package/coverage/src/js-lib/db/redshift/RedshiftCallExpressionVisitor.ts.html +685 -0
- package/coverage/src/js-lib/db/redshift/index.html +116 -0
- package/coverage/src/js-lib/db/sample/SampleCallExpressionVisitor.ts.html +196 -0
- package/coverage/src/js-lib/db/sample/index.html +116 -0
- package/coverage/src/js-lib/db/snowflake/SnowflakeCallExpressionVisitor.ts.html +1447 -0
- package/coverage/src/js-lib/db/snowflake/index.html +116 -0
- package/coverage/src/js-lib/db/validator/FormulaValidator.ts.html +4162 -0
- package/coverage/src/js-lib/db/validator/index.html +116 -0
- package/coverage/src/js-lib/index.html +131 -0
- package/coverage/src/js-lib/objects/BaseObject.ts.html +169 -0
- package/coverage/src/js-lib/objects/DateObject.ts.html +169 -0
- package/coverage/src/js-lib/objects/PctObject.ts.html +178 -0
- package/coverage/src/js-lib/objects/index.html +146 -0
- package/coverage/src/query-builder/PaginationBuilder.ts.html +142 -0
- package/coverage/src/query-builder/QueryBuilder.ts.html +3118 -0
- package/coverage/src/query-builder/SuperFilterBuilder.ts.html +1969 -0
- package/coverage/src/query-builder/index.html +146 -0
- package/coverage/src/runtime_var.ts.html +109 -0
- package/coverage/src/sql-lib/binary_expr.ts.html +133 -0
- package/coverage/src/sql-lib/case.ts.html +133 -0
- package/coverage/src/sql-lib/column.ts.html +139 -0
- package/coverage/src/sql-lib/else.ts.html +124 -0
- package/coverage/src/sql-lib/function.ts.html +112 -0
- package/coverage/src/sql-lib/index.html +251 -0
- package/coverage/src/sql-lib/join.ts.html +127 -0
- package/coverage/src/sql-lib/literal.ts.html +130 -0
- package/coverage/src/sql-lib/select.ts.html +547 -0
- package/coverage/src/sql-lib/unary_expr.ts.html +112 -0
- package/coverage/src/sql-lib/when.ts.html +130 -0
- package/coverage/src/sql_query_gen.ts.html +535 -0
- package/coverage/src/superFilter/DateFilterFactory.ts.html +625 -0
- package/coverage/src/superFilter/dateFunction.ts.html +193 -0
- package/coverage/src/superFilter/index.html +131 -0
- package/coverage/src/utils.ts.html +571 -0
- package/dist/index.cjs +8440 -0
- package/dist/index.d.cts +927 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +927 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8387 -0
- package/dist/index.js.map +1 -0
- package/eslint.config.js +4 -0
- package/jest.config.ts +44 -0
- package/package.json +45 -0
- package/src/constants.ts +247 -0
- package/src/epm-query-builder/EpmQueryBuilder.ts +27 -0
- package/src/epm-query-builder/base/BaseAdvancedAggregations.ts +161 -0
- package/src/epm-query-builder/base/BaseAnalyticalFunctions.ts +203 -0
- package/src/epm-query-builder/base/BaseCTEGenerator.ts +458 -0
- package/src/epm-query-builder/base/BaseCountQueryBuilder.ts +105 -0
- package/src/epm-query-builder/base/BaseMeasureBuilder.ts +87 -0
- package/src/epm-query-builder/base/BaseOrderBuilder.ts +195 -0
- package/src/epm-query-builder/base/BasePaginationBuilder.ts +93 -0
- package/src/epm-query-builder/base/BaseQueryBuilder.ts +51 -0
- package/src/epm-query-builder/base/BaseRollupBuilder.ts +149 -0
- package/src/epm-query-builder/base/BaseSqlBuilder.ts +172 -0
- package/src/epm-query-builder/base/BaseSuperFilterBuilder.ts +627 -0
- package/src/epm-query-builder/base/BaseUtilities.ts +571 -0
- package/src/epm-query-builder/base/ColumnRefUtils.ts +42 -0
- package/src/epm-query-builder/base/RelationshipResolver.ts +207 -0
- package/src/epm-query-builder/base/SharedFilterBuilder.ts +544 -0
- package/src/epm-query-builder/constants/Aggregations.ts +16 -0
- package/src/epm-query-builder/constants/Database.ts +6 -0
- package/src/epm-query-builder/constants/Source.ts +7 -0
- package/src/epm-query-builder/dialects/duckdb/DuckDbAdvancedAggregations.ts +67 -0
- package/src/epm-query-builder/dialects/duckdb/DuckDbJoinBuilder.ts +65 -0
- package/src/epm-query-builder/dialects/duckdb/DuckDbMeasureBuilder.ts +626 -0
- package/src/epm-query-builder/dialects/duckdb/DuckDbOrderBuilder.ts +228 -0
- package/src/epm-query-builder/dialects/duckdb/DuckDbPaginationBuilder.ts +186 -0
- package/src/epm-query-builder/dialects/duckdb/DuckDbQueryBuilder.ts +853 -0
- package/src/epm-query-builder/dialects/duckdb/DuckDbRollupBuilder.ts +131 -0
- package/src/epm-query-builder/dialects/duckdb/DuckDbSuperFilterBuilder.ts +370 -0
- package/src/epm-query-builder/errors/QueryBuilderErrors.ts +65 -0
- package/src/epm-query-builder/interfaces/IDatabaseQueryBuilder.ts +5 -0
- package/src/epm-query-builder/interfaces/index.ts +1 -0
- package/src/epm-query-builder/types/query-builder-types.d.ts +289 -0
- package/src/epm-query-builder/utils/format.ts +22 -0
- package/src/epm-query-builder/utils/sql.ts +54 -0
- package/src/epm-query-builder/utils/validation.ts +13 -0
- package/src/epm-query-builder/validation/QueryOptionsValidator.ts +182 -0
- package/src/epm-query-builder/validation/SqlQueryValidator.ts +130 -0
- package/src/filters/FilterConditionBuilder.ts +114 -0
- package/src/filters/filter-types.ts +133 -0
- package/src/index.ts +10 -0
- package/src/js-lib/JsToSqlParser.ts +217 -0
- package/src/js-lib/ParseContext.ts +149 -0
- package/src/js-lib/db/base/ArrayExpressionVisitor.ts +16 -0
- package/src/js-lib/db/base/AssignmentExpressionVisitor.ts +34 -0
- package/src/js-lib/db/base/BinaryExpressionVisitor.ts +46 -0
- package/src/js-lib/db/base/CallExpressionVisitor.ts +1798 -0
- package/src/js-lib/db/base/IdentifierVisitor.ts +66 -0
- package/src/js-lib/db/base/LiteralVisitor.ts +38 -0
- package/src/js-lib/db/base/MemberExpressionVisitor.ts +36 -0
- package/src/js-lib/db/base/ProgramVisitor.ts +18 -0
- package/src/js-lib/db/base/UnaryExpressionVisitor.ts +32 -0
- package/src/js-lib/db/base/VisitorInterface.ts +6 -0
- package/src/js-lib/db/validator/FormulaValidator.ts +1235 -0
- package/src/js-lib/objects/BaseObject.ts +28 -0
- package/src/js-lib/objects/DateObject.ts +28 -0
- package/src/js-lib/objects/PctObject.ts +31 -0
- package/src/query-builder/PaginationBuilder.ts +19 -0
- package/src/query-builder/QueryBuilder.ts +1035 -0
- package/src/query-builder/SuperFilterBuilder.ts +628 -0
- package/src/runtime_var.ts +8 -0
- package/src/sql-lib/binary_expr.ts +16 -0
- package/src/sql-lib/case.ts +16 -0
- package/src/sql-lib/column.ts +18 -0
- package/src/sql-lib/else.ts +13 -0
- package/src/sql-lib/function.ts +9 -0
- package/src/sql-lib/join.ts +14 -0
- package/src/sql-lib/literal.ts +15 -0
- package/src/sql-lib/select.ts +154 -0
- package/src/sql-lib/unary_expr.ts +9 -0
- package/src/sql-lib/when.ts +15 -0
- package/src/sql-types.d.ts +565 -0
- package/src/sql_query_gen.ts +150 -0
- package/src/superFilter/DateFilterFactory.ts +180 -0
- package/src/superFilter/dateFunction.ts +36 -0
- package/src/utils.ts +354 -0
- package/test-output/report/junit.xml +329 -0
- package/tests/JsToSqlParser.test.ts +163 -0
- package/tests/QueryBuilder.test.ts +1320 -0
- package/tests/js-lib/CallExpressionVisitor.test.ts +820 -0
- package/tests/mocks/MockQueryResolver.ts +14 -0
- package/tests/sanity.test.ts +146 -0
- package/tests/sql-lib/binary_expr.test.ts +75 -0
- package/tests/sql-lib/case.test.ts +117 -0
- package/tests/sql-lib/column.test.ts +87 -0
- package/tests/sql-lib/else.test.ts +56 -0
- package/tests/sql-lib/function.test.ts +96 -0
- package/tests/sql-lib/literal.test.ts +75 -0
- package/tests/sql-lib/select.test.ts +245 -0
- package/tests/sql-lib/unary_expr.test.ts +32 -0
- package/tests/utils.test.ts +13 -0
- package/tsconfig.json +24 -0
- package/tsdown.config.ts +23 -0
|
@@ -0,0 +1,565 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
|
|
3
|
+
// Type definitions for node-sql-parser 1.0
|
|
4
|
+
// Project: https://github.com/taozhi8833998/node-sql-parser#readme
|
|
5
|
+
// Definitions by: taozhi8833998 <https://github.com/taozhi8833998>
|
|
6
|
+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
|
7
|
+
// TypeScript Version: 2.4
|
|
8
|
+
|
|
9
|
+
export interface With {
|
|
10
|
+
name: { value: string };
|
|
11
|
+
stmt: {
|
|
12
|
+
_parentheses?: boolean;
|
|
13
|
+
tableList: string[];
|
|
14
|
+
columnList: string[];
|
|
15
|
+
ast: Select;
|
|
16
|
+
};
|
|
17
|
+
columns?: any[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type WhilteListCheckMode = 'table' | 'column';
|
|
21
|
+
export interface ParseOptions {
|
|
22
|
+
includeLocations?: boolean;
|
|
23
|
+
}
|
|
24
|
+
export interface Option {
|
|
25
|
+
database?: string;
|
|
26
|
+
type?: string;
|
|
27
|
+
trimQuery?: boolean;
|
|
28
|
+
parseOptions?: ParseOptions;
|
|
29
|
+
}
|
|
30
|
+
export interface TableColumnAst {
|
|
31
|
+
tableList: string[];
|
|
32
|
+
columnList: string[];
|
|
33
|
+
ast: AST[] | AST;
|
|
34
|
+
loc?: LocationRange;
|
|
35
|
+
}
|
|
36
|
+
export interface BaseFrom {
|
|
37
|
+
type: 'from';
|
|
38
|
+
db: string | null;
|
|
39
|
+
table: string;
|
|
40
|
+
as: string | null;
|
|
41
|
+
schema?: string;
|
|
42
|
+
loc?: LocationRange;
|
|
43
|
+
}
|
|
44
|
+
export interface Join extends BaseFrom {
|
|
45
|
+
join: 'INNER JOIN' | 'LEFT JOIN' | 'RIGHT JOIN';
|
|
46
|
+
using?: string[];
|
|
47
|
+
on?: Binary;
|
|
48
|
+
}
|
|
49
|
+
export interface TableExpr {
|
|
50
|
+
type: 'table_expr';
|
|
51
|
+
expr: {
|
|
52
|
+
ast: Select;
|
|
53
|
+
};
|
|
54
|
+
as?: string | null;
|
|
55
|
+
parentheses: boolean | { length: number };
|
|
56
|
+
}
|
|
57
|
+
export interface Dual {
|
|
58
|
+
type: 'dual';
|
|
59
|
+
loc?: LocationRange;
|
|
60
|
+
}
|
|
61
|
+
export type From = BaseFrom | Join | TableExpr | Dual;
|
|
62
|
+
export interface LimitValue {
|
|
63
|
+
type: string;
|
|
64
|
+
value: number;
|
|
65
|
+
loc?: LocationRange;
|
|
66
|
+
}
|
|
67
|
+
export interface Limit {
|
|
68
|
+
seperator: string;
|
|
69
|
+
value: LimitValue[];
|
|
70
|
+
loc?: LocationRange;
|
|
71
|
+
}
|
|
72
|
+
export interface OrderBy {
|
|
73
|
+
type: 'ASC' | 'DESC';
|
|
74
|
+
expr: any;
|
|
75
|
+
loc?: LocationRange;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export type RawSqlExpression = {
|
|
79
|
+
type: 'sql_expr';
|
|
80
|
+
value: string;
|
|
81
|
+
meta?: Record<string, any>;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export interface ValueExpr<T = string | number | boolean> {
|
|
85
|
+
type:
|
|
86
|
+
| 'backticks_quote_string'
|
|
87
|
+
| 'string'
|
|
88
|
+
| 'regex_string'
|
|
89
|
+
| 'hex_string'
|
|
90
|
+
| 'full_hex_string'
|
|
91
|
+
| 'natural_string'
|
|
92
|
+
| 'bit_string'
|
|
93
|
+
| 'double_quote_string'
|
|
94
|
+
| 'single_quote_string'
|
|
95
|
+
| 'boolean'
|
|
96
|
+
| 'bool'
|
|
97
|
+
| 'null'
|
|
98
|
+
| 'star'
|
|
99
|
+
| 'param'
|
|
100
|
+
| 'origin'
|
|
101
|
+
| 'date'
|
|
102
|
+
| 'datetime'
|
|
103
|
+
| 'default'
|
|
104
|
+
| 'time'
|
|
105
|
+
| 'timestamp'
|
|
106
|
+
| 'var_string';
|
|
107
|
+
value: T;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export interface ColumnRefItem {
|
|
111
|
+
type: 'column_ref';
|
|
112
|
+
table: string | null;
|
|
113
|
+
column: string | { expr: ValueExpr };
|
|
114
|
+
options?: ExprList;
|
|
115
|
+
loc?: LocationRange;
|
|
116
|
+
collate?: { collate: CollateExpr };
|
|
117
|
+
}
|
|
118
|
+
export interface ColumnRefExpr {
|
|
119
|
+
type: 'expr';
|
|
120
|
+
expr: ColumnRefItem;
|
|
121
|
+
as: string | null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export type ColumnRef = ColumnRefItem | ColumnRefExpr;
|
|
125
|
+
export interface SetList {
|
|
126
|
+
column: string;
|
|
127
|
+
value: any;
|
|
128
|
+
table: string | null;
|
|
129
|
+
loc?: LocationRange;
|
|
130
|
+
}
|
|
131
|
+
export interface InsertReplaceValue {
|
|
132
|
+
type: 'expr_list';
|
|
133
|
+
value: any[];
|
|
134
|
+
loc?: LocationRange;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface Star {
|
|
138
|
+
type: 'star';
|
|
139
|
+
value: '*' | '';
|
|
140
|
+
loc?: LocationRange;
|
|
141
|
+
}
|
|
142
|
+
export interface Case {
|
|
143
|
+
type: 'case';
|
|
144
|
+
expr: null;
|
|
145
|
+
args: Array<
|
|
146
|
+
| {
|
|
147
|
+
cond: Binary;
|
|
148
|
+
result: ExpressionValue;
|
|
149
|
+
type: 'when';
|
|
150
|
+
}
|
|
151
|
+
| {
|
|
152
|
+
result: ExpressionValue;
|
|
153
|
+
type: 'else';
|
|
154
|
+
}
|
|
155
|
+
>;
|
|
156
|
+
}
|
|
157
|
+
export interface Cast {
|
|
158
|
+
type: 'cast';
|
|
159
|
+
keyword: 'cast';
|
|
160
|
+
expr: ExpressionValue;
|
|
161
|
+
symbol: 'as';
|
|
162
|
+
target: {
|
|
163
|
+
dataType: string;
|
|
164
|
+
suffix: unknown[];
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
export interface AggrFunc {
|
|
168
|
+
type: 'aggr_func';
|
|
169
|
+
name: string;
|
|
170
|
+
args: {
|
|
171
|
+
expr: ExpressionValue;
|
|
172
|
+
distinct: 'DISTINCT' | null;
|
|
173
|
+
orderby: OrderBy[] | null;
|
|
174
|
+
parentheses?: boolean;
|
|
175
|
+
};
|
|
176
|
+
loc?: LocationRange;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export type FunctionName = {
|
|
180
|
+
schema?: { value: string; type: string };
|
|
181
|
+
name: ValueExpr<string>[];
|
|
182
|
+
};
|
|
183
|
+
export interface SqlFunction {
|
|
184
|
+
type: 'function';
|
|
185
|
+
name: FunctionName;
|
|
186
|
+
args?: ExprList;
|
|
187
|
+
suffix?: any;
|
|
188
|
+
loc?: LocationRange;
|
|
189
|
+
}
|
|
190
|
+
export interface Column {
|
|
191
|
+
type: 'column';
|
|
192
|
+
expr: ExpressionValue;
|
|
193
|
+
as: ValueExpr<string> | string | null;
|
|
194
|
+
type?: string;
|
|
195
|
+
loc?: LocationRange;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export interface Interval {
|
|
199
|
+
type: 'interval';
|
|
200
|
+
unit: string;
|
|
201
|
+
expr: ValueExpr & { loc?: LocationRange };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export type Param = { type: 'param'; value: string; loc?: LocationRange };
|
|
205
|
+
|
|
206
|
+
export type Value = { type: string; value: any; loc?: LocationRange };
|
|
207
|
+
|
|
208
|
+
export type Binary = {
|
|
209
|
+
type: 'binary_expr';
|
|
210
|
+
operator: string;
|
|
211
|
+
left: ExpressionValue | ExprList;
|
|
212
|
+
right: ExpressionValue | ExprList;
|
|
213
|
+
loc?: LocationRange;
|
|
214
|
+
parentheses?: boolean;
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
export type Expr = Binary;
|
|
218
|
+
|
|
219
|
+
export type UnaryExpression = {
|
|
220
|
+
type: 'unary_expr';
|
|
221
|
+
operator: string;
|
|
222
|
+
expr: ExpressionValue;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
export type ExpressionValue =
|
|
226
|
+
| ColumnRef
|
|
227
|
+
| Column
|
|
228
|
+
| Param
|
|
229
|
+
| SqlFunction
|
|
230
|
+
| Case
|
|
231
|
+
| AggrFunc
|
|
232
|
+
| Value
|
|
233
|
+
| UnaryExpression
|
|
234
|
+
| Binary
|
|
235
|
+
| Cast
|
|
236
|
+
| Interval
|
|
237
|
+
| RawSqlExpression;
|
|
238
|
+
|
|
239
|
+
export type ExprList = {
|
|
240
|
+
type: 'expr_list';
|
|
241
|
+
value: ExpressionValue[];
|
|
242
|
+
loc?: LocationRange;
|
|
243
|
+
parentheses?: boolean;
|
|
244
|
+
separator?: string;
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
export type PartitionBy = {
|
|
248
|
+
type: 'expr';
|
|
249
|
+
expr: ColumnRef[];
|
|
250
|
+
}[];
|
|
251
|
+
|
|
252
|
+
export type WindowSpec = {
|
|
253
|
+
name: null;
|
|
254
|
+
partitionby: PartitionBy;
|
|
255
|
+
orderby: OrderBy[] | null;
|
|
256
|
+
window_frame_clause: string | null;
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
export type AsWindowSpec = string | { window_specification: WindowSpec; parentheses: boolean };
|
|
260
|
+
|
|
261
|
+
export type NamedWindowExpr = {
|
|
262
|
+
name: string;
|
|
263
|
+
as_window_specification: AsWindowSpec;
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
export type WindowExpr = {
|
|
267
|
+
keyword: 'window';
|
|
268
|
+
type: 'window';
|
|
269
|
+
expr: NamedWindowExpr[];
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
export interface Select {
|
|
273
|
+
with: With[] | null;
|
|
274
|
+
type: 'select';
|
|
275
|
+
options: any[] | null;
|
|
276
|
+
distinct: 'DISTINCT' | null;
|
|
277
|
+
columns: any[] | Column[];
|
|
278
|
+
from: From[] | TableExpr | null;
|
|
279
|
+
where: Binary | SqlFunction | RawSqlExpression | null;
|
|
280
|
+
groupby: { columns: ColumnRef[] | null; modifiers: ValueExpr<string>[] };
|
|
281
|
+
having: any[] | null;
|
|
282
|
+
orderby: OrderBy[] | RawSqlExpression[] | null;
|
|
283
|
+
limit: Limit | RawSqlExpression | null;
|
|
284
|
+
window?: WindowExpr;
|
|
285
|
+
qualify?: RawSqlExpression | null;
|
|
286
|
+
_orderby?: OrderBy[] | null;
|
|
287
|
+
_limit?: Limit | null;
|
|
288
|
+
parentheses_symbol?: boolean;
|
|
289
|
+
_parentheses?: boolean;
|
|
290
|
+
loc?: LocationRange;
|
|
291
|
+
_next?: Select;
|
|
292
|
+
set_op?: string;
|
|
293
|
+
}
|
|
294
|
+
export interface Insert_Replace {
|
|
295
|
+
type: 'replace' | 'insert';
|
|
296
|
+
table: any;
|
|
297
|
+
columns: string[] | null;
|
|
298
|
+
values: InsertReplaceValue[] | Select;
|
|
299
|
+
partition: any[];
|
|
300
|
+
prefix: string;
|
|
301
|
+
on_duplicate_update: {
|
|
302
|
+
keyword: 'on duplicate key update';
|
|
303
|
+
set: SetList[];
|
|
304
|
+
};
|
|
305
|
+
loc?: LocationRange;
|
|
306
|
+
}
|
|
307
|
+
export interface Update {
|
|
308
|
+
type: 'update';
|
|
309
|
+
db: string | null;
|
|
310
|
+
table: Array<From | Dual> | null;
|
|
311
|
+
set: SetList[];
|
|
312
|
+
where: Binary | SqlFunction | null;
|
|
313
|
+
loc?: LocationRange;
|
|
314
|
+
}
|
|
315
|
+
export interface Delete {
|
|
316
|
+
type: 'delete';
|
|
317
|
+
table: any;
|
|
318
|
+
from: Array<From | Dual>;
|
|
319
|
+
where: Binary | SqlFunction | null;
|
|
320
|
+
loc?: LocationRange;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
export interface Alter {
|
|
324
|
+
type: 'alter';
|
|
325
|
+
table: From[];
|
|
326
|
+
expr: any;
|
|
327
|
+
loc?: LocationRange;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export interface Use {
|
|
331
|
+
type: 'use';
|
|
332
|
+
db: string;
|
|
333
|
+
loc?: LocationRange;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
type KW_UNSIGNED = 'UNSIGNED';
|
|
337
|
+
type KW_ZEROFILL = 'ZEROFILL';
|
|
338
|
+
|
|
339
|
+
type Timezone = ['WITHOUT' | 'WITH', 'TIME', 'ZONE'];
|
|
340
|
+
|
|
341
|
+
type KeywordComment = {
|
|
342
|
+
type: 'comment';
|
|
343
|
+
keyword: 'comment';
|
|
344
|
+
symbol?: '=';
|
|
345
|
+
value: string;
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
type CollateExpr = {
|
|
349
|
+
type: 'collate';
|
|
350
|
+
symbol?: '=';
|
|
351
|
+
value: string;
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
type DataType = {
|
|
355
|
+
dataType: string;
|
|
356
|
+
length?: number;
|
|
357
|
+
parentheses?: true;
|
|
358
|
+
suffix?: Timezone | (KW_UNSIGNED | KW_ZEROFILL)[];
|
|
359
|
+
array?: 'one' | 'two';
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
type LiteralNotNull = {
|
|
363
|
+
type: 'not null';
|
|
364
|
+
value: 'not null';
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
type LiteralNull = { type: 'null'; value: null };
|
|
368
|
+
|
|
369
|
+
type LiteralNumeric = number | { type: 'bigint'; value: string };
|
|
370
|
+
|
|
371
|
+
type ColumnConstraint = {
|
|
372
|
+
default_val: {
|
|
373
|
+
type: 'default';
|
|
374
|
+
value: any;
|
|
375
|
+
};
|
|
376
|
+
nullable: LiteralNotNull | LiteralNull;
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
type ColumnDefinitionOptList = {
|
|
380
|
+
nullable?: ColumnConstraint['nullable'];
|
|
381
|
+
default_val?: ColumnConstraint['default_val'];
|
|
382
|
+
auto_increment?: 'auto_increment';
|
|
383
|
+
unique?: 'unique' | 'unique key';
|
|
384
|
+
primary?: 'key' | 'primary key';
|
|
385
|
+
comment?: KeywordComment;
|
|
386
|
+
collate?: { collate: CollateExpr };
|
|
387
|
+
column_format?: { column_format: any };
|
|
388
|
+
storage?: { storage: any };
|
|
389
|
+
reference_definition?: { reference_definition: any };
|
|
390
|
+
character_set?: { type: 'CHARACTER SET'; value: string; symbol?: '=' };
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
type CreateColumnDefinition = {
|
|
394
|
+
column: ColumnRef;
|
|
395
|
+
definition: DataType;
|
|
396
|
+
resource: 'column';
|
|
397
|
+
} & ColumnDefinitionOptList;
|
|
398
|
+
|
|
399
|
+
type IndexType = {
|
|
400
|
+
keyword: 'using';
|
|
401
|
+
type: 'btree' | 'hash' | 'gist' | 'gin';
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
type IndexOption = {
|
|
405
|
+
type: 'key_block_size';
|
|
406
|
+
symbol?: '=';
|
|
407
|
+
expr: LiteralNumeric;
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
type CreateIndexDefinition = {
|
|
411
|
+
index?: string;
|
|
412
|
+
definition: ColumnRef[];
|
|
413
|
+
keyword: 'index' | 'key';
|
|
414
|
+
index_type?: IndexType;
|
|
415
|
+
resource: 'index';
|
|
416
|
+
index_options?: IndexOption[];
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
type CreateFulltextSpatialIndexDefinition = {
|
|
420
|
+
index?: string;
|
|
421
|
+
definition: ColumnRef[];
|
|
422
|
+
keyword?:
|
|
423
|
+
| 'fulltext'
|
|
424
|
+
| 'spatial'
|
|
425
|
+
| 'fulltext key'
|
|
426
|
+
| 'spatial key'
|
|
427
|
+
| 'fulltext index'
|
|
428
|
+
| 'spatial index';
|
|
429
|
+
index_options?: IndexOption[];
|
|
430
|
+
resource: 'index';
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
type ConstraintName = { keyword: 'constraint'; constraint: string };
|
|
434
|
+
|
|
435
|
+
type CreateConstraintPrimary = {
|
|
436
|
+
constraint?: ConstraintName['constraint'];
|
|
437
|
+
definition: ColumnRef[];
|
|
438
|
+
constraint_type: 'primary key';
|
|
439
|
+
keyword?: ConstraintName['keyword'];
|
|
440
|
+
index_type?: IndexType;
|
|
441
|
+
resource: 'constraint';
|
|
442
|
+
index_options?: IndexOption[];
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
type CreateConstraintUnique = {
|
|
446
|
+
constraint?: ConstraintName['constraint'];
|
|
447
|
+
definition: ColumnRef[];
|
|
448
|
+
constraint_type: 'unique key' | 'unique' | 'unique index';
|
|
449
|
+
keyword?: ConstraintName['keyword'];
|
|
450
|
+
index_type?: IndexType;
|
|
451
|
+
index?: string;
|
|
452
|
+
resource: 'constraint';
|
|
453
|
+
index_options?: IndexOption[];
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
type CreateConstraintForeign = {
|
|
457
|
+
constraint?: ConstraintName['constraint'];
|
|
458
|
+
definition: ColumnRef[];
|
|
459
|
+
constraint_type: 'FOREIGN KEY';
|
|
460
|
+
keyword?: ConstraintName['keyword'];
|
|
461
|
+
index?: string;
|
|
462
|
+
resource: 'constraint';
|
|
463
|
+
reference_definition?: any;
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
type CreateConstraintCheck = {
|
|
467
|
+
constraint?: ConstraintName['constraint'];
|
|
468
|
+
definition: any[];
|
|
469
|
+
constraint_type: 'check';
|
|
470
|
+
keyword?: ConstraintName['keyword'];
|
|
471
|
+
resource: 'constraint';
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
type CreateConstraintDefinition =
|
|
475
|
+
| CreateConstraintPrimary
|
|
476
|
+
| CreateConstraintUnique
|
|
477
|
+
| CreateConstraintForeign
|
|
478
|
+
| CreateConstraintCheck;
|
|
479
|
+
|
|
480
|
+
type CreateDefinition =
|
|
481
|
+
| CreateColumnDefinition
|
|
482
|
+
| CreateIndexDefinition
|
|
483
|
+
| CreateFulltextSpatialIndexDefinition
|
|
484
|
+
| CreateConstraintDefinition;
|
|
485
|
+
|
|
486
|
+
export interface Create {
|
|
487
|
+
type: 'create';
|
|
488
|
+
keyword:
|
|
489
|
+
| 'aggregate'
|
|
490
|
+
| 'table'
|
|
491
|
+
| 'trigger'
|
|
492
|
+
| 'extension'
|
|
493
|
+
| 'function'
|
|
494
|
+
| 'index'
|
|
495
|
+
| 'database'
|
|
496
|
+
| 'schema'
|
|
497
|
+
| 'view'
|
|
498
|
+
| 'domain'
|
|
499
|
+
| 'type'
|
|
500
|
+
| 'user';
|
|
501
|
+
temporary?: 'temporary' | null;
|
|
502
|
+
table?: { db: string; table: string }[];
|
|
503
|
+
if_not_exists?: 'if not exists' | null;
|
|
504
|
+
like?: {
|
|
505
|
+
type: 'like';
|
|
506
|
+
table: string;
|
|
507
|
+
parentheses?: boolean;
|
|
508
|
+
} | null;
|
|
509
|
+
ignore_replace?: 'ignore' | 'replace' | null;
|
|
510
|
+
as?: string | null;
|
|
511
|
+
query_expr?: any | null;
|
|
512
|
+
create_definitions?: CreateDefinition[] | null;
|
|
513
|
+
table_options?: any[] | null;
|
|
514
|
+
index_using?: {
|
|
515
|
+
keyword: 'using';
|
|
516
|
+
type: 'btree' | 'hash';
|
|
517
|
+
} | null;
|
|
518
|
+
index?: string | null;
|
|
519
|
+
on_kw?: 'on' | null;
|
|
520
|
+
index_columns?: any[] | null;
|
|
521
|
+
index_type?: 'unique' | 'fulltext' | 'spatial' | null;
|
|
522
|
+
index_options?: any[] | null;
|
|
523
|
+
algorithm_option?: {
|
|
524
|
+
type: 'alter';
|
|
525
|
+
keyword: 'algorithm';
|
|
526
|
+
resource: 'algorithm';
|
|
527
|
+
symbol: '=' | null;
|
|
528
|
+
algorithm: 'default' | 'instant' | 'inplace' | 'copy';
|
|
529
|
+
} | null;
|
|
530
|
+
lock_option?: {
|
|
531
|
+
type: 'alter';
|
|
532
|
+
keyword: 'lock';
|
|
533
|
+
resource: 'lock';
|
|
534
|
+
symbol: '=' | null;
|
|
535
|
+
lock: 'default' | 'none' | 'shared' | 'exclusive';
|
|
536
|
+
} | null;
|
|
537
|
+
database?: string;
|
|
538
|
+
loc?: LocationRange;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
export interface Drop {
|
|
542
|
+
type: 'drop';
|
|
543
|
+
keyword: string;
|
|
544
|
+
name: any[];
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
export type AST = Use | Select | Insert_Replace | Update | Delete | Alter | Create | Drop;
|
|
548
|
+
|
|
549
|
+
export class Parser {
|
|
550
|
+
constructor();
|
|
551
|
+
|
|
552
|
+
parse(sql: string, opt?: Option): TableColumnAst;
|
|
553
|
+
|
|
554
|
+
astify(sql: string, opt?: Option): AST[] | AST;
|
|
555
|
+
|
|
556
|
+
sqlify(ast: AST[] | AST, opt?: Option): string;
|
|
557
|
+
|
|
558
|
+
exprToSQL(ast: any, opt?: Option): string;
|
|
559
|
+
|
|
560
|
+
whiteListCheck(sql: string, whiteList: string[], opt?: Option): Error | undefined;
|
|
561
|
+
|
|
562
|
+
tableList(sql: string, opt?: Option): string[];
|
|
563
|
+
|
|
564
|
+
columnList(sql: string, opt?: Option): string[];
|
|
565
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ExpressionValue,
|
|
3
|
+
ColumnRefItem,
|
|
4
|
+
SqlFunction,
|
|
5
|
+
Case,
|
|
6
|
+
Binary,
|
|
7
|
+
UnaryExpression,
|
|
8
|
+
Select,
|
|
9
|
+
With,
|
|
10
|
+
TableExpr,
|
|
11
|
+
From,
|
|
12
|
+
Join,
|
|
13
|
+
Column,
|
|
14
|
+
ExprList,
|
|
15
|
+
RawSqlExpression,
|
|
16
|
+
OrderBy,
|
|
17
|
+
} from './sql-types';
|
|
18
|
+
import { WhenType } from './sql-lib/when';
|
|
19
|
+
import { ElseType } from './sql-lib/else';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Transforms an AST to a SQL query string
|
|
23
|
+
*/
|
|
24
|
+
export function generateSqlQuery(ast: ExpressionValue | Select | TableExpr | From): string {
|
|
25
|
+
switch (ast.type) {
|
|
26
|
+
case 'column_ref':
|
|
27
|
+
const columnRef = ast as ColumnRefItem;
|
|
28
|
+
return `${columnRef.table ? `${columnRef.table}.` : ''}${columnRef.column}`;
|
|
29
|
+
case 'column':
|
|
30
|
+
const column = ast as Column;
|
|
31
|
+
return `${generateSqlQuery(column.expr)}${column.as ? ` AS ${column.as}` : ''}`;
|
|
32
|
+
case 'function':
|
|
33
|
+
const functionAst = ast as SqlFunction;
|
|
34
|
+
return `${functionAst.name.name[0].value}(${functionAst.args?.value.map((arg) => generateSqlQuery(arg)).join(', ')})`;
|
|
35
|
+
case 'unary_expr':
|
|
36
|
+
const unaryExpr = ast as UnaryExpression;
|
|
37
|
+
return `${unaryExpr.operator} ${generateSqlQuery(unaryExpr.expr)}`;
|
|
38
|
+
case 'binary_expr':
|
|
39
|
+
const binaryExpr = ast as Binary;
|
|
40
|
+
if (binaryExpr.parentheses) {
|
|
41
|
+
return `(${generateSqlQuery(binaryExpr.left)} ${binaryExpr.operator} ${generateSqlQuery(binaryExpr.right)})`;
|
|
42
|
+
}
|
|
43
|
+
return `${generateSqlQuery(binaryExpr.left)} ${binaryExpr.operator} ${generateSqlQuery(binaryExpr.right)}`;
|
|
44
|
+
case 'expr_list':
|
|
45
|
+
const exprList = ast as ExprList;
|
|
46
|
+
return `(${exprList.value.map((arg) => generateSqlQuery(arg)).join(', ')})`;
|
|
47
|
+
case 'default':
|
|
48
|
+
case 'sql_expr':
|
|
49
|
+
return ast.value;
|
|
50
|
+
case 'number':
|
|
51
|
+
return ast.value.toString();
|
|
52
|
+
case 'string':
|
|
53
|
+
return `'${ast.value}'`;
|
|
54
|
+
case 'bit':
|
|
55
|
+
return `'${ast.value}'`;
|
|
56
|
+
case 'double_quote_string':
|
|
57
|
+
return `"${ast.value}"`;
|
|
58
|
+
case 'boolean':
|
|
59
|
+
return ast.value.toString().toUpperCase();
|
|
60
|
+
case 'null':
|
|
61
|
+
return 'NULL';
|
|
62
|
+
case 'select':
|
|
63
|
+
return processSelect(ast as Select);
|
|
64
|
+
case 'from':
|
|
65
|
+
const fromAst = ast as Join;
|
|
66
|
+
const tableName = `${fromAst.db ? `${fromAst.db}.` : ''}${fromAst.table}`;
|
|
67
|
+
if (fromAst.join) {
|
|
68
|
+
return `${fromAst.join} ${tableName} ON ${generateSqlQuery(fromAst.on!)}`;
|
|
69
|
+
}
|
|
70
|
+
return tableName;
|
|
71
|
+
case 'table_expr':
|
|
72
|
+
const tableExpr = ast as TableExpr;
|
|
73
|
+
return generateSqlQuery(tableExpr.expr.ast);
|
|
74
|
+
case 'case':
|
|
75
|
+
const caseAst = ast as Case;
|
|
76
|
+
const whenLength = caseAst.args.length - 1;
|
|
77
|
+
const elseArg = caseAst.args[whenLength] as ElseType;
|
|
78
|
+
const whenArgs = caseAst.args.slice(0, whenLength) as WhenType[];
|
|
79
|
+
return `CASE ${whenArgs.map((arg) => `WHEN ${generateSqlQuery(arg.cond)} THEN ${generateSqlQuery(arg.result)}`).join(' ')} ELSE ${generateSqlQuery(elseArg.result)} END`;
|
|
80
|
+
default:
|
|
81
|
+
throw new Error(`Unsupported type: ${ast.type}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function processWith(ast: With[]): string {
|
|
86
|
+
return ast
|
|
87
|
+
.map((withStmt) => `${withStmt.name.value} AS (${generateSqlQuery(withStmt.stmt.ast)})`)
|
|
88
|
+
.join(', ');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Processes a Select AST and returns the corresponding SQL query string
|
|
93
|
+
*/
|
|
94
|
+
export function processSelect(ast: Select): string {
|
|
95
|
+
const parts: string[] = [];
|
|
96
|
+
if (ast.with) {
|
|
97
|
+
parts.push(`${ast.from ? 'WITH ' : ''}${processWith(ast.with)}`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (ast.from) {
|
|
101
|
+
parts.push('SELECT');
|
|
102
|
+
if (ast.distinct) {
|
|
103
|
+
parts.push(ast.distinct);
|
|
104
|
+
}
|
|
105
|
+
parts.push(ast.columns.map((col) => generateSqlQuery(col)).join(', '));
|
|
106
|
+
const from = Array.isArray(ast.from)
|
|
107
|
+
? ast.from.map((arg) => generateSqlQuery(arg)).join(' ')
|
|
108
|
+
: generateSqlQuery(ast.from);
|
|
109
|
+
parts.push(`FROM ${from}`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (ast.from && ast.where) {
|
|
113
|
+
parts.push(`WHERE ${generateSqlQuery(ast.where)}`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (ast.qualify) {
|
|
117
|
+
parts.push(`QUALIFY ${generateSqlQuery(ast.qualify)}`);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (ast.from && ast.groupby?.columns) {
|
|
121
|
+
parts.push(`GROUP BY ${ast.groupby.columns.map((col) => generateSqlQuery(col)).join(', ')}`);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (ast.from && ast.orderby) {
|
|
125
|
+
parts.push(
|
|
126
|
+
`ORDER BY ${ast.orderby
|
|
127
|
+
.map((order) => {
|
|
128
|
+
if ((order as RawSqlExpression).type === 'sql_expr') {
|
|
129
|
+
return (order as RawSqlExpression).value;
|
|
130
|
+
}
|
|
131
|
+
return `${generateSqlQuery((order as OrderBy).expr)} ${order.type}`;
|
|
132
|
+
})
|
|
133
|
+
.join(', ')}`,
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (ast.from && ast.limit) {
|
|
138
|
+
if ((ast.limit as RawSqlExpression).type === 'sql_expr') {
|
|
139
|
+
parts.push((ast.limit as RawSqlExpression).value);
|
|
140
|
+
} else {
|
|
141
|
+
throw new Error('Unsupported limit type. Limit must be a RawSqlExpression');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (ast._next) {
|
|
146
|
+
parts.push(`${ast.set_op} ${generateSqlQuery(ast._next)}`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return parts.join(' ');
|
|
150
|
+
}
|