@puca/puca-core 10.4.3 → 10.5.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/dist/core/services/bootstrap-commands.js +1 -1
- package/dist/core/services/bootstrap-commands.js.map +1 -1
- package/dist/core/services/crud-base-controller-parse-from.d.ts +5 -0
- package/dist/core/services/crud-base-controller-parse-from.js +21 -0
- package/dist/core/services/crud-base-controller-parse-from.js.map +1 -0
- package/dist/core/services/crud-base.controller.js +3 -2
- package/dist/core/services/crud-base.controller.js.map +1 -1
- package/dist/core/services/eval.service.js +6 -10
- package/dist/core/services/eval.service.js.map +1 -1
- package/dist/core/services/index.js +1 -1
- package/dist/core/services/index.js.map +1 -1
- package/dist/core/services/install.service.d.ts +13 -0
- package/dist/core/services/install.service.js +192 -6
- package/dist/core/services/install.service.js.map +1 -1
- package/dist/core/services/life-cycle.service.js +2 -0
- package/dist/core/services/life-cycle.service.js.map +1 -1
- package/dist/core/services/table-permission.service.js +5 -4
- package/dist/core/services/table-permission.service.js.map +1 -1
- package/dist/core/services/user-config.service.d.ts +4 -1
- package/dist/core/services/user-config.service.js +90 -10
- package/dist/core/services/user-config.service.js.map +1 -1
- package/dist/models/crud-controller-especification-environment.d.ts +1 -0
- package/dist/models/crud-controller-especification-environment.js.map +1 -1
- package/dist/models/user-access-control-mode.d.ts +2 -1
- package/dist/models/user-access-control-mode.js +1 -0
- package/dist/models/user-access-control-mode.js.map +1 -1
- package/dist/orm/cases/vanilla/sql-builder/select.service.js +3 -2
- package/dist/orm/cases/vanilla/sql-builder/select.service.js.map +1 -1
- package/dist/orm/services/index.d.ts +2 -0
- package/dist/orm/services/index.js +3 -1
- package/dist/orm/services/index.js.map +1 -1
- package/dist/orm/services/sql-parser/sql-parser.service.d.ts +3 -8
- package/dist/orm/services/sql-parser/sql-parser.service.js +501 -105
- package/dist/orm/services/sql-parser/sql-parser.service.js.map +1 -1
- package/dist/plugins/nestjs/query-params.middleware.d.ts +5 -0
- package/dist/plugins/nestjs/query-params.middleware.js +16 -0
- package/dist/plugins/nestjs/query-params.middleware.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/dist/orm/services/sql-parser/sql-parser-model.d.ts +0 -78
- package/dist/orm/services/sql-parser/sql-parser-model.js +0 -4
- package/dist/orm/services/sql-parser/sql-parser-model.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/orm/services/index.ts"],"names":[],"mappings":";;;AAAA,2CAA4C;AAC5C,mDAA+C;AAC/C,6DAAyD;AACzD,kFAA6E;AAC7E,4EAAuE;AACvE,oCAAuC;AACvC,mDAAsD;AACtD,+CAAmD;AACnD,+CAAiE;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/orm/services/index.ts"],"names":[],"mappings":";;;AAAA,2CAA4C;AAC5C,mDAA+C;AAC/C,6DAAyD;AACzD,kFAA6E;AAC7E,4EAAuE;AACvE,oCAAuC;AACvC,mDAAsD;AACtD,+CAAmD;AACnD,+CAAiE;AACjE,wEAAmE;AAEtD,QAAA,sBAAsB,GAAG,IAAI,iDAAsB,EAAE,CAAC;AACtD,QAAA,kBAAkB,GAAG,IAAI,gCAAkB,CAAC,8BAAsB,EAAE,wBAAY,CAAC,CAAA;AACjF,QAAA,mBAAmB,GAAG,IAAI,2CAAmB,EAAE,CAAC;AAIhD,QAAA,mBAAmB,GAAG,IAAI,mCAAmB,CAAC,2BAAmB,EAAE,4BAAgB,CAAC,CAAA;AAEpF,QAAA,UAAU,GAAG,IAAI,wBAAU,CACtC,mBAAW,EACX,GAAG,EAAE,CAAC,yBAAiB,EACvB,4BAAgB,CACjB,CAAA;AAGD,yBAAiB,GAAG,IAAI,sCAAiB,CACvC,8BAAsB,EACtB,0BAAkB,EAClB,2BAAmB,EACnB,GAAG,EAAE,CAAC,kBAAU,EAChB,qBAAW,CACZ,CAAC;AACa,QAAA,gBAAgB,GAAG,IAAI,qCAAgB,EAAE,CAAA"}
|
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
parse(sql: string): Select | Update | Delete | Insert;
|
|
5
|
-
private parseColumnAST;
|
|
6
|
-
private parseOperatorAST;
|
|
7
|
-
private parseWhereAST;
|
|
8
|
-
private parseSelectAST;
|
|
1
|
+
import * as puca from '../../cases/vanilla/interfaces';
|
|
2
|
+
export declare class SqlParserService {
|
|
3
|
+
parseToSelectText(sql: string): puca.Select;
|
|
9
4
|
}
|
|
@@ -3,130 +3,526 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SqlParserService = void 0;
|
|
4
4
|
const _ = require("lodash");
|
|
5
5
|
const pgsql_parser_service_1 = require("../../plugins/pgsql-parser/pgsql-parser.service");
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
const puca = require("../../cases/vanilla/interfaces");
|
|
7
|
+
const helperParseExpressionToASTColumn = (expr) => {
|
|
8
|
+
if (expr.type === 'star') {
|
|
9
|
+
throw new Error('* Not supported');
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
expr: expr,
|
|
13
|
+
as: null
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
class SqlParser extends pgsql_parser_service_1.PgsqlNormalizeService {
|
|
17
|
+
constructor() {
|
|
18
|
+
super(...arguments);
|
|
19
|
+
this.args = [];
|
|
20
|
+
}
|
|
21
|
+
parseToSelectText(sql) {
|
|
9
22
|
const ast = this.parseAST(sql);
|
|
10
23
|
if (_.isArray(ast)) {
|
|
11
|
-
throw new Error('
|
|
24
|
+
throw new Error('Not implemented');
|
|
12
25
|
}
|
|
13
26
|
if (ast.type !== 'select') {
|
|
14
|
-
throw new Error('not
|
|
27
|
+
throw new Error('Operation not supported');
|
|
15
28
|
}
|
|
16
|
-
return this.
|
|
29
|
+
return this.parseToSelect(ast, {
|
|
30
|
+
processUnion: true
|
|
31
|
+
});
|
|
17
32
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
33
|
+
parseToSelect(select, opts) {
|
|
34
|
+
const [from, ...astJoin] = select.from;
|
|
35
|
+
const fields = this.parseToSelectFields(select.columns);
|
|
36
|
+
const join = this.parseToSelectJoin(astJoin);
|
|
37
|
+
const where = this.parseToSelectWhere(select.where);
|
|
38
|
+
const orderBy = this.parseToSelectOrderBy(select.orderby);
|
|
39
|
+
const groupBy = this.parseToSelectGroupBy(select.groupby);
|
|
40
|
+
const w = this.parseToSelectWith(select.with);
|
|
41
|
+
const result = {
|
|
42
|
+
from: this.parseToSelectFrom(from),
|
|
43
|
+
args: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, fields.reduce((acc, f) => (Object.assign(Object.assign({}, acc), f.args)), {})), join.reduce((acc, j) => (Object.assign(Object.assign({}, acc), j.args)), {})), where.args), orderBy.reduce((acc, o) => (Object.assign(Object.assign({}, acc), o.args)), {})), groupBy.reduce((acc, g) => (Object.assign(Object.assign({}, acc), g.args)), {})), w.reduce((acc, w) => (Object.assign(Object.assign({}, acc), w.args)), {})),
|
|
44
|
+
fields: fields.map(f => f.field),
|
|
45
|
+
join: join.map(j => j.join),
|
|
46
|
+
where: where.w,
|
|
47
|
+
orderBy: orderBy.map(o => o.field),
|
|
48
|
+
groupBy: groupBy.map(g => g.field),
|
|
49
|
+
with: w.map(w => w.w),
|
|
50
|
+
union: [],
|
|
51
|
+
unionAll: []
|
|
52
|
+
};
|
|
53
|
+
if (opts.processUnion) {
|
|
54
|
+
let current = select;
|
|
55
|
+
let next = current._next;
|
|
56
|
+
while (next) {
|
|
57
|
+
const nextSelect = this.parseToSelect(next, {
|
|
58
|
+
processUnion: false
|
|
59
|
+
});
|
|
60
|
+
if (current.set_op === 'union') {
|
|
61
|
+
result.union.push(_.omit(nextSelect, 'args'));
|
|
62
|
+
}
|
|
63
|
+
else if (current.set_op === 'union all') {
|
|
64
|
+
result.unionAll.push(_.omit(nextSelect, 'args'));
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
throw new Error('Invalid set_op');
|
|
68
|
+
}
|
|
69
|
+
Object.entries(nextSelect.args).forEach(([k, v]) => result.args[k] = v);
|
|
70
|
+
current = next;
|
|
71
|
+
next = next._next;
|
|
72
|
+
}
|
|
31
73
|
}
|
|
32
|
-
return
|
|
74
|
+
return result;
|
|
33
75
|
}
|
|
34
|
-
|
|
35
|
-
if (!
|
|
36
|
-
return
|
|
76
|
+
parseToSelectJoin(joins) {
|
|
77
|
+
if (!joins) {
|
|
78
|
+
return [];
|
|
37
79
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
80
|
+
return joins.map((oJoin) => {
|
|
81
|
+
const join = oJoin;
|
|
82
|
+
const on = this.parseToSelectWhere(join.on);
|
|
83
|
+
const result = {
|
|
84
|
+
tableName: join.table,
|
|
85
|
+
alias: join.as,
|
|
86
|
+
mode: {
|
|
87
|
+
"LEFT JOIN": puca.JoinMode.Left,
|
|
88
|
+
"RIGHT JOIN": puca.JoinMode.Right,
|
|
89
|
+
"INNER JOIN": puca.JoinMode.Inner,
|
|
90
|
+
"FULL JOIN": puca.JoinMode.Full
|
|
91
|
+
}[join.join],
|
|
92
|
+
on: on.w,
|
|
93
|
+
};
|
|
94
|
+
return {
|
|
95
|
+
join: result,
|
|
96
|
+
args: on.args
|
|
97
|
+
};
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
parseToSelectFields(columns) {
|
|
101
|
+
return columns.map((column) => {
|
|
102
|
+
const expr = column.expr;
|
|
103
|
+
const mapOver = (exprAsAny, input) => {
|
|
104
|
+
const astOver = exprAsAny.over;
|
|
105
|
+
if (!astOver) {
|
|
106
|
+
return input;
|
|
55
107
|
}
|
|
108
|
+
const astPartitionBy = astOver.as_window_specification.window_specification.partitionby;
|
|
109
|
+
const astOrderBy = astOver.as_window_specification.window_specification.orderby;
|
|
110
|
+
const partitionBy = astPartitionBy ? this.parseToSelectFields(astPartitionBy) : [];
|
|
111
|
+
const orderBy = astOrderBy ? this.parseToSelectOrderBy(astOrderBy) : [];
|
|
112
|
+
return Object.assign(Object.assign({}, input), { field: Object.assign(Object.assign({}, input.field), { over: {
|
|
113
|
+
partitionBy: partitionBy.map(p => p.field),
|
|
114
|
+
orderBy: orderBy.map(o => o.field)
|
|
115
|
+
} }), args: Object.assign(Object.assign(Object.assign({}, input.args), partitionBy.reduce((acc, p) => (Object.assign(Object.assign({}, acc), p.args)), {})), orderBy.reduce((acc, o) => (Object.assign(Object.assign({}, acc), o.args)), {})) });
|
|
116
|
+
};
|
|
117
|
+
if ('value' in expr) {
|
|
118
|
+
const value = expr.value;
|
|
119
|
+
const argsName = this.getNextArgsName(value);
|
|
56
120
|
return {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
121
|
+
field: {
|
|
122
|
+
field: `$${argsName}`,
|
|
123
|
+
alias: column.as || null
|
|
124
|
+
},
|
|
125
|
+
args: {
|
|
126
|
+
[argsName]: value
|
|
127
|
+
},
|
|
60
128
|
};
|
|
61
|
-
return;
|
|
62
129
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
130
|
+
if (expr.type === 'aggr_func') {
|
|
131
|
+
const args = expr.args || [];
|
|
132
|
+
const fArgs = _.isArray(args) ? args : [args];
|
|
133
|
+
const pArgs = this.parseToSelectFields(fArgs);
|
|
134
|
+
const result = {
|
|
135
|
+
function: expr.name.toLowerCase(),
|
|
136
|
+
args: pArgs.map(p => p.field),
|
|
137
|
+
alias: column.as || expr.name.toLowerCase()
|
|
138
|
+
};
|
|
139
|
+
return mapOver(expr, {
|
|
140
|
+
field: result,
|
|
141
|
+
args: pArgs.reduce((acc, p) => (Object.assign(Object.assign({}, acc), p.args)), {})
|
|
142
|
+
});
|
|
70
143
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
144
|
+
if (expr.type === 'function') {
|
|
145
|
+
const fArgs = expr.args.value;
|
|
146
|
+
const pArgs = this.parseToSelectFields(fArgs.map((f) => helperParseExpressionToASTColumn(f)));
|
|
147
|
+
const result = {
|
|
148
|
+
function: expr.name.toLowerCase(),
|
|
149
|
+
args: pArgs.map(w => w.field),
|
|
150
|
+
alias: column.as || expr.name.toLowerCase()
|
|
151
|
+
};
|
|
152
|
+
return mapOver(expr, {
|
|
153
|
+
field: result,
|
|
154
|
+
args: pArgs.reduce((acc, p) => (Object.assign(Object.assign({}, acc), p.args)), {})
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
if (expr.type === 'column_ref') {
|
|
158
|
+
const result = {
|
|
159
|
+
table: expr.table,
|
|
160
|
+
field: expr.column,
|
|
161
|
+
alias: column.as || null
|
|
162
|
+
};
|
|
163
|
+
return { field: result, args: {} };
|
|
164
|
+
}
|
|
165
|
+
const exprAsAny = expr;
|
|
166
|
+
if (exprAsAny.ast) {
|
|
167
|
+
const select = this.parseToSelect(exprAsAny.ast, { processUnion: true });
|
|
168
|
+
return {
|
|
169
|
+
field: Object.assign(Object.assign({}, _.omit(select, 'args')), { alias: column.as || 'subselect' }),
|
|
170
|
+
args: select.args
|
|
171
|
+
};
|
|
77
172
|
}
|
|
173
|
+
if (exprAsAny.type === 'case') {
|
|
174
|
+
const astElse = exprAsAny.args.find(a => a.type === 'else');
|
|
175
|
+
const pucaElse = astElse ? this.parseToSelectFields([
|
|
176
|
+
helperParseExpressionToASTColumn(astElse.result)
|
|
177
|
+
])[0] : null;
|
|
178
|
+
const args = Object.assign({}, ((pucaElse === null || pucaElse === void 0 ? void 0 : pucaElse.args) || {}));
|
|
179
|
+
return {
|
|
180
|
+
field: {
|
|
181
|
+
conditions: exprAsAny.args
|
|
182
|
+
.filter(a => a.type === 'when')
|
|
183
|
+
.map((a, i) => {
|
|
184
|
+
const when = this.parseToSelectWhere(a.cond);
|
|
185
|
+
Object.entries(when.args).forEach(([k, v]) => args[k] = v);
|
|
186
|
+
const then = this.parseToSelectFields([
|
|
187
|
+
helperParseExpressionToASTColumn(a.result)
|
|
188
|
+
])[0];
|
|
189
|
+
Object.entries(then.args).forEach(([k, v]) => args[k] = v);
|
|
190
|
+
return {
|
|
191
|
+
when: when.w,
|
|
192
|
+
then: then.field
|
|
193
|
+
};
|
|
194
|
+
}),
|
|
195
|
+
else: (pucaElse === null || pucaElse === void 0 ? void 0 : pucaElse.field) || null,
|
|
196
|
+
alias: column.as || 'case'
|
|
197
|
+
},
|
|
198
|
+
args
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
if (exprAsAny.type === 'binary_expr') {
|
|
202
|
+
const v1 = this.parseToSelectWhereBinaryExprV(exprAsAny.left);
|
|
203
|
+
const v2 = this.parseToSelectWhereBinaryExprV(exprAsAny.right);
|
|
204
|
+
const f = this.getFunctionByOperator(exprAsAny.operator);
|
|
205
|
+
const result = {
|
|
206
|
+
function: f,
|
|
207
|
+
args: [v1.field, v2.field],
|
|
208
|
+
alias: column.as || f
|
|
209
|
+
};
|
|
210
|
+
return {
|
|
211
|
+
field: result,
|
|
212
|
+
args: Object.assign(Object.assign({}, v1.args), v2.args)
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
if (exprAsAny.type === 'cast') {
|
|
216
|
+
const f = this.parseToSelectFields([
|
|
217
|
+
exprAsAny
|
|
218
|
+
])[0];
|
|
219
|
+
const field = typeof f.field === 'string' ? {
|
|
220
|
+
field: f.field,
|
|
221
|
+
} : f.field;
|
|
222
|
+
return {
|
|
223
|
+
field: Object.assign(Object.assign({}, field), { alias: exprAsAny.as || column.as || 'cast', cast: exprAsAny.target.dataType.toLowerCase() }),
|
|
224
|
+
args: f.args
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
if (exprAsAny.type === 'window_func') {
|
|
228
|
+
const result = {
|
|
229
|
+
function: exprAsAny.name.toLowerCase(),
|
|
230
|
+
args: [],
|
|
231
|
+
alias: column.as || exprAsAny.name.toLowerCase()
|
|
232
|
+
};
|
|
233
|
+
return mapOver(exprAsAny, {
|
|
234
|
+
field: result,
|
|
235
|
+
args: {}
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
throw new Error("Not implemented (parseToSelectFields - " + exprAsAny.type + ")");
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
getFunctionByOperator(operator) {
|
|
242
|
+
const result = {
|
|
243
|
+
'+': 'add',
|
|
244
|
+
'-': 'subtract',
|
|
245
|
+
'*': 'multiply',
|
|
246
|
+
'/': 'divide',
|
|
247
|
+
'||': 'concat',
|
|
248
|
+
}[operator];
|
|
249
|
+
if (!result) {
|
|
250
|
+
throw new Error('Invalid function operator');
|
|
251
|
+
}
|
|
252
|
+
return result;
|
|
253
|
+
}
|
|
254
|
+
parseToSelectFrom(from) {
|
|
255
|
+
if (!from) {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
const result = {
|
|
259
|
+
table: from.table,
|
|
260
|
+
alias: from.as
|
|
261
|
+
};
|
|
262
|
+
return result;
|
|
263
|
+
}
|
|
264
|
+
parseToSelectOrderBy(orderBy) {
|
|
265
|
+
if (!orderBy) {
|
|
266
|
+
return [];
|
|
267
|
+
}
|
|
268
|
+
return orderBy.map((astField) => {
|
|
269
|
+
const field = this.parseToSelectFields([
|
|
270
|
+
helperParseExpressionToASTColumn(astField.expr)
|
|
271
|
+
])[0];
|
|
272
|
+
const result = {
|
|
273
|
+
field: field.field,
|
|
274
|
+
sort: astField.type === 'DESC' ? puca.AscDesc.DESC : puca.AscDesc.ASC
|
|
275
|
+
};
|
|
276
|
+
return {
|
|
277
|
+
field: result,
|
|
278
|
+
args: field.args
|
|
279
|
+
};
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
parseToSelectGroupBy(groupBy) {
|
|
283
|
+
if (!groupBy) {
|
|
284
|
+
return [];
|
|
285
|
+
}
|
|
286
|
+
return this.parseToSelectFields(groupBy.map(f => helperParseExpressionToASTColumn(f)));
|
|
287
|
+
}
|
|
288
|
+
parseToSelectWith(withs) {
|
|
289
|
+
if (!withs) {
|
|
290
|
+
return [];
|
|
291
|
+
}
|
|
292
|
+
return withs.map((w) => {
|
|
293
|
+
const name = w.name;
|
|
294
|
+
const astSelect = w.stmt;
|
|
295
|
+
const select = this.parseToSelect(astSelect, { processUnion: true });
|
|
296
|
+
const result = {
|
|
297
|
+
alias: name.value,
|
|
298
|
+
select: _.omit(select, 'args')
|
|
299
|
+
};
|
|
300
|
+
return {
|
|
301
|
+
w: result,
|
|
302
|
+
args: select.args
|
|
303
|
+
};
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
getNextArgsName(value) {
|
|
307
|
+
let index = this.args.findIndex(a => _.isEqual(a, value));
|
|
308
|
+
if (index === -1) {
|
|
309
|
+
index = this.args.length;
|
|
310
|
+
this.args.push(value);
|
|
311
|
+
}
|
|
312
|
+
return `arg${index}`;
|
|
313
|
+
}
|
|
314
|
+
parseToSelectWhereBinaryExprV(v) {
|
|
315
|
+
if ('value' in v) {
|
|
316
|
+
const argsName = this.getNextArgsName(v.value);
|
|
317
|
+
return {
|
|
318
|
+
args: {
|
|
319
|
+
[argsName]: v.value
|
|
320
|
+
},
|
|
321
|
+
field: {
|
|
322
|
+
field: `$${argsName}`,
|
|
323
|
+
}
|
|
324
|
+
};
|
|
78
325
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (selectAst.distinct) {
|
|
87
|
-
throw new Error('not implemented');
|
|
88
|
-
}
|
|
89
|
-
if (selectAst.having) {
|
|
90
|
-
throw new Error('not implemented');
|
|
91
|
-
}
|
|
92
|
-
const fromAst = selectAst.from.shift();
|
|
93
|
-
const joinAst = selectAst.from || [];
|
|
94
|
-
const columnsAst = selectAst.columns;
|
|
95
|
-
const where = selectAst.where;
|
|
96
|
-
return {
|
|
97
|
-
fields: columnsAst.map(c => this.parseColumnAST(c)),
|
|
98
|
-
from: {
|
|
99
|
-
alias: fromAst.as,
|
|
100
|
-
table: fromAst.table
|
|
101
|
-
},
|
|
102
|
-
groupBy: (selectAst.groupby || []).map(g => {
|
|
103
|
-
return this.parseColumnAST({ type: 'expr', as: null, expr: g });
|
|
104
|
-
}),
|
|
105
|
-
limit: selectAst.limit && selectAst.limit.value.length === 1 ? selectAst.limit.value[0].value : null,
|
|
106
|
-
offset: null,
|
|
107
|
-
orderBy: (selectAst.orderby || []).map(o => {
|
|
108
|
-
return this.parseColumnAST(o.expr);
|
|
109
|
-
}),
|
|
110
|
-
join: joinAst.map(j => {
|
|
111
|
-
const mode = {
|
|
112
|
-
'INNER JOIN': interfaces_1.JoinMode.Inner,
|
|
113
|
-
'LEFT JOIN': interfaces_1.JoinMode.Left,
|
|
114
|
-
'RIGHT JOIN': interfaces_1.JoinMode.Right,
|
|
115
|
-
'FULL JOIN': interfaces_1.JoinMode.Full,
|
|
116
|
-
}[j.join];
|
|
117
|
-
if (!mode) {
|
|
118
|
-
throw new Error('invalid join mode: ' + mode);
|
|
326
|
+
if (v.type === 'column_ref') {
|
|
327
|
+
return {
|
|
328
|
+
args: null,
|
|
329
|
+
field: {
|
|
330
|
+
table: v.table,
|
|
331
|
+
field: v.column,
|
|
332
|
+
alias: null
|
|
119
333
|
}
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
const vAsAny = v;
|
|
337
|
+
if (vAsAny.type === 'function') {
|
|
338
|
+
const pArgs = this.parseToSelectFields(vAsAny.args.value.map((f) => helperParseExpressionToASTColumn(f)));
|
|
339
|
+
return {
|
|
340
|
+
args: pArgs.reduce((acc, p) => (Object.assign(Object.assign({}, acc), p.args)), {}),
|
|
341
|
+
field: {
|
|
342
|
+
function: vAsAny.name,
|
|
343
|
+
args: pArgs.map(w => w.field),
|
|
344
|
+
alias: null
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
if (vAsAny.type === 'case') {
|
|
349
|
+
const astElse = vAsAny.args.find(a => a.type === 'else');
|
|
350
|
+
const pucaElse = astElse ? this.parseToSelectFields([
|
|
351
|
+
helperParseExpressionToASTColumn(astElse.result)
|
|
352
|
+
])[0] : null;
|
|
353
|
+
const args = Object.assign({}, ((pucaElse === null || pucaElse === void 0 ? void 0 : pucaElse.args) || {}));
|
|
354
|
+
return {
|
|
355
|
+
args,
|
|
356
|
+
field: {
|
|
357
|
+
conditions: vAsAny.args
|
|
358
|
+
.filter(a => a.type === 'when')
|
|
359
|
+
.map((a, i) => {
|
|
360
|
+
const when = this.parseToSelectWhere(a.cond);
|
|
361
|
+
Object.entries(when.args).forEach(([k, v]) => args[k] = v);
|
|
362
|
+
const then = this.parseToSelectFields([
|
|
363
|
+
helperParseExpressionToASTColumn(a.result)
|
|
364
|
+
])[0];
|
|
365
|
+
Object.entries(then.args).forEach(([k, v]) => args[k] = v);
|
|
366
|
+
return {
|
|
367
|
+
when: when.w,
|
|
368
|
+
then: then.field
|
|
369
|
+
};
|
|
370
|
+
}),
|
|
371
|
+
else: (pucaElse === null || pucaElse === void 0 ? void 0 : pucaElse.field) || null,
|
|
372
|
+
alias: null
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
if (vAsAny.type === 'binary_expr') {
|
|
377
|
+
const v1 = this.parseToSelectWhereBinaryExprV(vAsAny.left);
|
|
378
|
+
const v2 = this.parseToSelectWhereBinaryExprV(vAsAny.right);
|
|
379
|
+
const f = this.getFunctionByOperator(vAsAny.operator);
|
|
380
|
+
const result = {
|
|
381
|
+
function: f,
|
|
382
|
+
args: [v1.field, v2.field],
|
|
383
|
+
alias: null
|
|
384
|
+
};
|
|
385
|
+
return {
|
|
386
|
+
field: result,
|
|
387
|
+
args: Object.assign(Object.assign({}, v1.args), v2.args)
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
if (vAsAny.type === 'cast') {
|
|
391
|
+
const f = this.parseToSelectFields([
|
|
392
|
+
vAsAny
|
|
393
|
+
])[0];
|
|
394
|
+
const field = typeof f.field === 'string' ? {
|
|
395
|
+
field: f.field,
|
|
396
|
+
} : f.field;
|
|
397
|
+
return {
|
|
398
|
+
field: Object.assign(Object.assign({}, field), { cast: vAsAny.target.dataType.toLowerCase(), alias: null }),
|
|
399
|
+
args: f.args
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
throw new Error('Invalid where binary expr v type ' + vAsAny.type);
|
|
403
|
+
}
|
|
404
|
+
parseToSelectWhereBinaryExpr(e) {
|
|
405
|
+
if (e.operator === 'AND' || e.operator === 'OR') {
|
|
406
|
+
return this.parseToSelectWhere(e);
|
|
407
|
+
}
|
|
408
|
+
const e2 = e;
|
|
409
|
+
const o = this.parseToSelectWhereBinaryExprOperator(e2.operator);
|
|
410
|
+
const v1 = this.parseToSelectWhereBinaryExprV(e2.left);
|
|
411
|
+
const v2 = o.operator === puca.ConditionalOperator.isNull ? null : this.parseToSelectWhereBinaryExprV(e2.right);
|
|
412
|
+
const result = {
|
|
413
|
+
v1: v1.field,
|
|
414
|
+
v2: (v2 === null || v2 === void 0 ? void 0 : v2.field) || null,
|
|
415
|
+
operator: o.operator
|
|
416
|
+
};
|
|
417
|
+
return { w: o.not ? { not: result } : result, args: Object.assign(Object.assign({}, v1.args), v2 === null || v2 === void 0 ? void 0 : v2.args) };
|
|
418
|
+
}
|
|
419
|
+
parseToSelectWhereBinaryExprOperator(operator) {
|
|
420
|
+
if (operator === '=') {
|
|
421
|
+
return { not: false, operator: puca.ConditionalOperator.equals };
|
|
422
|
+
}
|
|
423
|
+
if (operator === '!=') {
|
|
424
|
+
return { not: true, operator: puca.ConditionalOperator.equals };
|
|
425
|
+
}
|
|
426
|
+
if (operator === '<>') {
|
|
427
|
+
return { not: true, operator: puca.ConditionalOperator.equals };
|
|
428
|
+
}
|
|
429
|
+
if (operator === '>') {
|
|
430
|
+
return { not: false, operator: puca.ConditionalOperator.greater };
|
|
431
|
+
}
|
|
432
|
+
if (operator === '>=') {
|
|
433
|
+
return { not: false, operator: puca.ConditionalOperator.greaterOrEquals };
|
|
434
|
+
}
|
|
435
|
+
if (operator === '<') {
|
|
436
|
+
return { not: false, operator: puca.ConditionalOperator.less };
|
|
437
|
+
}
|
|
438
|
+
if (operator === '<=') {
|
|
439
|
+
return { not: false, operator: puca.ConditionalOperator.lessOrEquals };
|
|
440
|
+
}
|
|
441
|
+
if (operator === 'LIKE') {
|
|
442
|
+
return { not: false, operator: puca.ConditionalOperator.contains };
|
|
443
|
+
}
|
|
444
|
+
if (operator === 'NOT LIKE') {
|
|
445
|
+
return { not: true, operator: puca.ConditionalOperator.contains };
|
|
446
|
+
}
|
|
447
|
+
if (operator === 'ILIKE') {
|
|
448
|
+
return { not: false, operator: puca.ConditionalOperator.iContains };
|
|
449
|
+
}
|
|
450
|
+
if (operator === 'NOT ILIKE') {
|
|
451
|
+
return { not: true, operator: puca.ConditionalOperator.iContains };
|
|
452
|
+
}
|
|
453
|
+
if (operator === 'IN') {
|
|
454
|
+
throw new Error('Not implemented');
|
|
455
|
+
}
|
|
456
|
+
if (operator === 'NOT IN') {
|
|
457
|
+
throw new Error('Not implemented');
|
|
458
|
+
}
|
|
459
|
+
if (operator === 'anyOf') {
|
|
460
|
+
return { not: false, operator: puca.ConditionalOperator.anyOf };
|
|
461
|
+
}
|
|
462
|
+
if (operator === 'IS') {
|
|
463
|
+
return { not: false, operator: puca.ConditionalOperator.isNull };
|
|
464
|
+
}
|
|
465
|
+
if (operator === 'IS NOT') {
|
|
466
|
+
return { not: true, operator: puca.ConditionalOperator.isNull };
|
|
467
|
+
}
|
|
468
|
+
throw new Error('Invalid operator');
|
|
469
|
+
}
|
|
470
|
+
parseToSelectWhere(where) {
|
|
471
|
+
if (!where) {
|
|
472
|
+
return {
|
|
473
|
+
w: null,
|
|
474
|
+
args: {}
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
if (where.type === 'function') {
|
|
478
|
+
throw new Error('Not implemented');
|
|
479
|
+
}
|
|
480
|
+
if (where.operator === 'NOT') {
|
|
481
|
+
const not = this.parseToSelectWhere(where.expr);
|
|
482
|
+
return {
|
|
483
|
+
w: { not: not.w },
|
|
484
|
+
args: not.args
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
else if (where.type === 'binary_expr') {
|
|
488
|
+
if (where.operator === 'AND') {
|
|
489
|
+
const w1 = this.parseToSelectWhere(where.left);
|
|
490
|
+
const w2 = this.parseToSelectWhere(where.right);
|
|
491
|
+
return {
|
|
492
|
+
w: {
|
|
493
|
+
and: [
|
|
494
|
+
w1.w,
|
|
495
|
+
w2.w
|
|
496
|
+
]
|
|
497
|
+
},
|
|
498
|
+
args: Object.assign(Object.assign({}, w1.args), w2.args)
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
else if (where.operator === 'OR') {
|
|
502
|
+
const w1 = this.parseToSelectWhere(where.left);
|
|
503
|
+
const w2 = this.parseToSelectWhere(where.right);
|
|
120
504
|
return {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
505
|
+
w: {
|
|
506
|
+
or: [
|
|
507
|
+
w1.w,
|
|
508
|
+
w2.w
|
|
509
|
+
]
|
|
510
|
+
},
|
|
511
|
+
args: Object.assign(Object.assign({}, w1.args), w2.args)
|
|
125
512
|
};
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
513
|
+
}
|
|
514
|
+
else {
|
|
515
|
+
return this.parseToSelectWhereBinaryExpr(where);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
const whereAsAny = where;
|
|
519
|
+
throw new Error('Invalid where type ' + whereAsAny.type);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
class SqlParserService {
|
|
523
|
+
parseToSelectText(sql) {
|
|
524
|
+
const sqlParser = new SqlParser();
|
|
525
|
+
return sqlParser.parseToSelectText(sql);
|
|
130
526
|
}
|
|
131
527
|
}
|
|
132
528
|
exports.SqlParserService = SqlParserService;
|