typesql-cli 0.8.0-alpha.4 → 0.8.0-alpha.6
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/mysql-query-analyzer/traverse.d.ts +19 -0
- package/mysql-query-analyzer/traverse.d.ts.map +1 -1
- package/mysql-query-analyzer/traverse.js.map +1 -1
- package/mysql-query-analyzer/unify.d.ts.map +1 -1
- package/mysql-query-analyzer/unify.js +8 -0
- package/mysql-query-analyzer/unify.js.map +1 -1
- package/package.json +1 -1
- package/sqlite-query-analyzer/code-generator.js +69 -24
- package/sqlite-query-analyzer/code-generator.js.map +1 -1
- package/sqlite-query-analyzer/parser.js +139 -28
- package/sqlite-query-analyzer/parser.js.map +1 -1
- package/sqlite-query-analyzer/traverse.d.ts +5 -5
- package/sqlite-query-analyzer/traverse.d.ts.map +1 -1
- package/sqlite-query-analyzer/traverse.js +408 -76
- package/sqlite-query-analyzer/traverse.js.map +1 -1
- package/sqlite-query-analyzer/types.d.ts +1 -1
- package/sqlite-query-analyzer/types.d.ts.map +1 -1
@@ -8,7 +8,26 @@ function traverse_Sql_stmtContext(sql_stmt, traverseContext) {
|
|
8
8
|
const select_stmt = sql_stmt.select_stmt();
|
9
9
|
if (select_stmt) {
|
10
10
|
const queryResult = traverse_select_stmt(select_stmt, traverseContext);
|
11
|
-
return
|
11
|
+
return {
|
12
|
+
queryType: 'Select',
|
13
|
+
columns: queryResult.columns,
|
14
|
+
multipleRowsResult: isMultipleRowResult(select_stmt, queryResult.fromColumns),
|
15
|
+
};
|
16
|
+
}
|
17
|
+
const insert_stmt = sql_stmt.insert_stmt();
|
18
|
+
if (insert_stmt) {
|
19
|
+
const insertResult = traverse_insert_stmt(insert_stmt, traverseContext);
|
20
|
+
return insertResult;
|
21
|
+
}
|
22
|
+
const update_stmt = sql_stmt.update_stmt();
|
23
|
+
if (update_stmt) {
|
24
|
+
const updateResult = traverse_update_stmt(update_stmt, traverseContext);
|
25
|
+
return updateResult;
|
26
|
+
}
|
27
|
+
const delete_stmt = sql_stmt.delete_stmt();
|
28
|
+
if (delete_stmt) {
|
29
|
+
const deleteResult = traverse_delete_stmt(delete_stmt, traverseContext);
|
30
|
+
return deleteResult;
|
12
31
|
}
|
13
32
|
throw Error("traverse_Sql_stmtContext");
|
14
33
|
}
|
@@ -36,7 +55,6 @@ function traverse_select_stmt(select_stmt, traverseContext) {
|
|
36
55
|
const querySpecResult = select_coreList.map(select_core => {
|
37
56
|
const columnsResult = [];
|
38
57
|
const listType = [];
|
39
|
-
const columnNullability = [];
|
40
58
|
const table_or_subquery = select_core.table_or_subquery_list();
|
41
59
|
if (table_or_subquery) {
|
42
60
|
const fields = traverse_table_or_subquery(table_or_subquery, traverseContext);
|
@@ -56,8 +74,12 @@ function traverse_select_stmt(select_stmt, traverseContext) {
|
|
56
74
|
columnsResult.forEach(col => {
|
57
75
|
if (!tableName || (0, select_columns_1.includeColumn)(col, tableName)) {
|
58
76
|
const columnType = (0, collect_constraints_1.createColumnType)(col);
|
59
|
-
listType.push(
|
60
|
-
|
77
|
+
listType.push({
|
78
|
+
name: columnType.name,
|
79
|
+
type: columnType,
|
80
|
+
notNull: col.notNull,
|
81
|
+
table: col.tableAlias || col.table
|
82
|
+
});
|
61
83
|
}
|
62
84
|
});
|
63
85
|
}
|
@@ -65,12 +87,11 @@ function traverse_select_stmt(select_stmt, traverseContext) {
|
|
65
87
|
const alias = (_b = result_column.column_alias()) === null || _b === void 0 ? void 0 : _b.getText();
|
66
88
|
if (expr) {
|
67
89
|
const exprType = traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns: columnsResult }));
|
68
|
-
if (exprType.kind == 'TypeVar') {
|
90
|
+
if (exprType.type.kind == 'TypeVar') {
|
69
91
|
if (alias) {
|
70
92
|
exprType.name = alias;
|
71
93
|
}
|
72
94
|
listType.push(exprType);
|
73
|
-
columnNullability.push(inferNotNull_expr(expr, columnsResult));
|
74
95
|
}
|
75
96
|
}
|
76
97
|
});
|
@@ -78,18 +99,9 @@ function traverse_select_stmt(select_stmt, traverseContext) {
|
|
78
99
|
whereList.forEach(where => {
|
79
100
|
traverse_expr(where, Object.assign(Object.assign({}, traverseContext), { fromColumns: columnsResult }));
|
80
101
|
});
|
81
|
-
const columns = listType.map((t, index) => {
|
82
|
-
const resultType = {
|
83
|
-
name: t.name,
|
84
|
-
type: t,
|
85
|
-
notNull: columnNullability[index],
|
86
|
-
table: t.table || ''
|
87
|
-
};
|
88
|
-
return resultType;
|
89
|
-
});
|
90
102
|
const querySpecification = {
|
91
|
-
columns,
|
92
|
-
fromColumns:
|
103
|
+
columns: listType,
|
104
|
+
fromColumns: columnsResult //TODO - return isMultipleRowResult instead
|
93
105
|
};
|
94
106
|
return querySpecification;
|
95
107
|
});
|
@@ -141,53 +153,189 @@ function traverse_expr(expr, traverseContext) {
|
|
141
153
|
var _a, _b;
|
142
154
|
const function_name = (_a = expr.function_name()) === null || _a === void 0 ? void 0 : _a.getText().toLowerCase();
|
143
155
|
if (function_name == 'sum' || function_name == 'avg') {
|
144
|
-
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'NUMERIC');
|
156
|
+
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), function_name == 'sum' ? 'NUMERIC' : 'REAL');
|
145
157
|
const sumParamExpr = expr.expr(0);
|
146
158
|
const paramType = traverse_expr(sumParamExpr, traverseContext);
|
147
|
-
if (paramType.kind == 'TypeVar') {
|
159
|
+
if (paramType.type.kind == 'TypeVar') {
|
148
160
|
functionType.table = paramType.table;
|
149
161
|
}
|
150
|
-
return
|
162
|
+
return {
|
163
|
+
name: functionType.name,
|
164
|
+
type: functionType,
|
165
|
+
notNull: paramType.notNull,
|
166
|
+
table: functionType.table || ''
|
167
|
+
};
|
168
|
+
}
|
169
|
+
if (function_name == 'min' || function_name == 'max') {
|
170
|
+
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
|
171
|
+
const sumParamExpr = expr.expr(0);
|
172
|
+
const paramType = traverse_expr(sumParamExpr, traverseContext);
|
173
|
+
traverseContext.constraints.push({
|
174
|
+
expression: expr.getText(),
|
175
|
+
type1: functionType,
|
176
|
+
type2: paramType.type
|
177
|
+
});
|
178
|
+
return {
|
179
|
+
name: functionType.name,
|
180
|
+
type: functionType,
|
181
|
+
notNull: paramType.notNull,
|
182
|
+
table: functionType.table || ''
|
183
|
+
};
|
151
184
|
}
|
152
185
|
if (function_name == 'count') {
|
153
186
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER');
|
154
187
|
if (expr.expr_list().length == 1) {
|
155
188
|
const sumParamExpr = expr.expr(0);
|
156
189
|
const paramType = traverse_expr(sumParamExpr, traverseContext);
|
157
|
-
if (paramType.kind == 'TypeVar') {
|
190
|
+
if (paramType.type.kind == 'TypeVar') {
|
158
191
|
functionType.table = paramType.table;
|
159
192
|
}
|
160
193
|
}
|
161
|
-
return
|
194
|
+
return {
|
195
|
+
name: functionType.name,
|
196
|
+
type: functionType,
|
197
|
+
notNull: true,
|
198
|
+
table: functionType.table || ''
|
199
|
+
};
|
200
|
+
}
|
201
|
+
if (function_name == 'concat') {
|
202
|
+
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'TEXT');
|
203
|
+
expr.expr_list().forEach(paramExpr => {
|
204
|
+
const paramType = traverse_expr(paramExpr, traverseContext);
|
205
|
+
traverseContext.constraints.push({
|
206
|
+
expression: expr.getText(),
|
207
|
+
type1: functionType,
|
208
|
+
type2: paramType.type
|
209
|
+
});
|
210
|
+
if (paramType.type.kind == 'TypeVar') {
|
211
|
+
functionType.table = paramType.table;
|
212
|
+
}
|
213
|
+
});
|
214
|
+
return {
|
215
|
+
name: functionType.name,
|
216
|
+
type: functionType,
|
217
|
+
notNull: true,
|
218
|
+
table: functionType.table || ''
|
219
|
+
};
|
220
|
+
}
|
221
|
+
if (function_name == 'strftime') {
|
222
|
+
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'TEXT');
|
223
|
+
const paramExpr = expr.expr(1);
|
224
|
+
const paramType = traverse_expr(paramExpr, traverseContext);
|
225
|
+
traverseContext.constraints.push({
|
226
|
+
expression: paramExpr.getText(),
|
227
|
+
type1: (0, collect_constraints_1.freshVar)(paramExpr.getText(), 'DATE'),
|
228
|
+
type2: paramType.type
|
229
|
+
});
|
230
|
+
return {
|
231
|
+
name: functionType.name,
|
232
|
+
type: functionType,
|
233
|
+
notNull: false,
|
234
|
+
table: functionType.table || ''
|
235
|
+
};
|
236
|
+
}
|
237
|
+
if (function_name == 'date' || function_name == 'time' || function_name == 'datetime') {
|
238
|
+
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'TEXT');
|
239
|
+
const paramExpr = expr.expr(0);
|
240
|
+
const paramType = traverse_expr(paramExpr, traverseContext);
|
241
|
+
traverseContext.constraints.push({
|
242
|
+
expression: paramExpr.getText(),
|
243
|
+
type1: (0, collect_constraints_1.freshVar)(paramExpr.getText(), 'DATE'),
|
244
|
+
type2: paramType.type
|
245
|
+
});
|
246
|
+
return {
|
247
|
+
name: functionType.name,
|
248
|
+
type: functionType,
|
249
|
+
notNull: false,
|
250
|
+
table: functionType.table || ''
|
251
|
+
};
|
252
|
+
}
|
253
|
+
if (function_name == 'ifnull') {
|
254
|
+
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
|
255
|
+
const paramTypes = expr.expr_list().map(paramExpr => {
|
256
|
+
const paramType = traverse_expr(paramExpr, traverseContext);
|
257
|
+
paramType.notNull = false;
|
258
|
+
traverseContext.constraints.push({
|
259
|
+
expression: expr.getText(),
|
260
|
+
type1: functionType,
|
261
|
+
type2: paramType.type
|
262
|
+
});
|
263
|
+
return paramType;
|
264
|
+
});
|
265
|
+
return {
|
266
|
+
name: functionType.name,
|
267
|
+
type: functionType,
|
268
|
+
notNull: paramTypes.every(param => param.notNull),
|
269
|
+
table: functionType.table || ''
|
270
|
+
};
|
271
|
+
}
|
272
|
+
if (function_name) {
|
273
|
+
throw Error('traverse_expr: function not supported:' + function_name);
|
162
274
|
}
|
163
275
|
const column_name = expr.column_name();
|
164
276
|
if (column_name) {
|
165
|
-
const
|
166
|
-
|
167
|
-
const typeVar = (0, collect_constraints_1.freshVar)(column.columnName, column.columnType.type, column.tableAlias || column.table);
|
168
|
-
return typeVar;
|
277
|
+
const type = traverse_column_name(column_name, traverseContext);
|
278
|
+
return type;
|
169
279
|
}
|
170
280
|
const literal = expr.literal_value();
|
171
281
|
if (literal) {
|
172
282
|
if (literal.STRING_LITERAL()) {
|
173
|
-
|
283
|
+
const type = (0, collect_constraints_1.freshVar)(literal.getText(), 'TEXT');
|
284
|
+
return {
|
285
|
+
name: type.name,
|
286
|
+
type: type,
|
287
|
+
notNull: true,
|
288
|
+
table: type.table || ''
|
289
|
+
};
|
174
290
|
}
|
175
291
|
if (literal.NUMERIC_LITERAL()) {
|
176
|
-
|
292
|
+
const type = (0, collect_constraints_1.freshVar)(literal.getText(), 'INTEGER');
|
293
|
+
return {
|
294
|
+
name: type.name,
|
295
|
+
type: type,
|
296
|
+
notNull: true,
|
297
|
+
table: type.table || ''
|
298
|
+
};
|
177
299
|
}
|
178
|
-
|
300
|
+
const type = (0, collect_constraints_1.freshVar)(literal.getText(), '?');
|
301
|
+
return {
|
302
|
+
name: type.name,
|
303
|
+
type: type,
|
304
|
+
notNull: true,
|
305
|
+
table: type.table || ''
|
306
|
+
};
|
179
307
|
}
|
180
308
|
const parameter = expr.BIND_PARAMETER();
|
181
309
|
if (parameter) {
|
182
310
|
const param = (0, collect_constraints_1.freshVar)('?', '?');
|
183
|
-
|
184
|
-
|
311
|
+
const type = {
|
312
|
+
name: param.name,
|
313
|
+
type: param,
|
314
|
+
notNull: true,
|
315
|
+
table: param.table || ''
|
316
|
+
};
|
317
|
+
traverseContext.parameters.push(type.type);
|
318
|
+
return type;
|
185
319
|
}
|
186
320
|
if (expr.STAR() || expr.DIV() || expr.MOD()) {
|
187
321
|
const exprLeft = expr.expr(0);
|
188
322
|
const exprRight = expr.expr(1);
|
189
323
|
const typeLeft = traverse_expr(exprLeft, traverseContext);
|
190
|
-
|
324
|
+
const typeRight = traverse_expr(exprRight, traverseContext);
|
325
|
+
const type = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
|
326
|
+
return {
|
327
|
+
name: type.name,
|
328
|
+
type: type,
|
329
|
+
notNull: typeLeft.notNull && typeRight.notNull,
|
330
|
+
table: type.table || ''
|
331
|
+
};
|
332
|
+
}
|
333
|
+
if (expr.PLUS() || expr.MINUS()) {
|
334
|
+
const exprLeft = expr.expr(0);
|
335
|
+
const exprRight = expr.expr(1);
|
336
|
+
const typeLeft = traverse_expr(exprLeft, traverseContext);
|
337
|
+
const typeRight = traverse_expr(exprRight, traverseContext);
|
338
|
+
return Object.assign(Object.assign({}, typeRight), { notNull: typeLeft.notNull && typeRight.notNull });
|
191
339
|
}
|
192
340
|
if (expr.LT2() || expr.GT2() || expr.AMP() || expr.PIPE() || expr.LT() || expr.LT_EQ() || expr.GT() || expr.GT_EQ()) {
|
193
341
|
const exprLeft = expr.expr(0);
|
@@ -196,10 +344,27 @@ function traverse_expr(expr, traverseContext) {
|
|
196
344
|
const typeRight = traverse_expr(exprRight, traverseContext);
|
197
345
|
traverseContext.constraints.push({
|
198
346
|
expression: expr.getText(),
|
199
|
-
type1: typeLeft,
|
200
|
-
type2: typeRight
|
347
|
+
type1: typeLeft.type,
|
348
|
+
type2: typeRight.type
|
201
349
|
});
|
202
|
-
|
350
|
+
const type = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
|
351
|
+
return {
|
352
|
+
name: type.name,
|
353
|
+
type: type,
|
354
|
+
notNull: true,
|
355
|
+
table: type.table || ''
|
356
|
+
};
|
357
|
+
}
|
358
|
+
if (expr.IS_()) { //is null/is not null
|
359
|
+
const expr_ = expr.expr(0);
|
360
|
+
traverse_expr(expr_, traverseContext);
|
361
|
+
const type = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
|
362
|
+
return {
|
363
|
+
name: type.name,
|
364
|
+
type: type,
|
365
|
+
notNull: true,
|
366
|
+
table: type.table || ''
|
367
|
+
};
|
203
368
|
}
|
204
369
|
if (expr.ASSIGN()) { //=
|
205
370
|
const exprLeft = expr.expr(0);
|
@@ -208,10 +373,16 @@ function traverse_expr(expr, traverseContext) {
|
|
208
373
|
const typeRight = traverse_expr(exprRight, traverseContext);
|
209
374
|
traverseContext.constraints.push({
|
210
375
|
expression: expr.getText(),
|
211
|
-
type1: typeLeft,
|
212
|
-
type2: typeRight
|
376
|
+
type1: typeLeft.type,
|
377
|
+
type2: typeRight.type
|
213
378
|
});
|
214
|
-
|
379
|
+
const type = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
|
380
|
+
return {
|
381
|
+
name: type.name,
|
382
|
+
type: type,
|
383
|
+
notNull: true,
|
384
|
+
table: type.table || ''
|
385
|
+
};
|
215
386
|
}
|
216
387
|
if (expr.BETWEEN_()) {
|
217
388
|
const exprType = traverse_expr(expr.expr(0), traverseContext);
|
@@ -219,18 +390,18 @@ function traverse_expr(expr, traverseContext) {
|
|
219
390
|
const between2 = traverse_expr(expr.expr(2), traverseContext);
|
220
391
|
traverseContext.constraints.push({
|
221
392
|
expression: expr.getText(),
|
222
|
-
type1: exprType,
|
223
|
-
type2: between1
|
393
|
+
type1: exprType.type,
|
394
|
+
type2: between1.type
|
224
395
|
});
|
225
396
|
traverseContext.constraints.push({
|
226
397
|
expression: expr.getText(),
|
227
|
-
type1: exprType,
|
228
|
-
type2: between2
|
398
|
+
type1: exprType.type,
|
399
|
+
type2: between2.type
|
229
400
|
});
|
230
401
|
traverseContext.constraints.push({
|
231
402
|
expression: expr.getText(),
|
232
|
-
type1: between1,
|
233
|
-
type2: between2
|
403
|
+
type1: between1.type,
|
404
|
+
type2: between2.type
|
234
405
|
});
|
235
406
|
return exprType;
|
236
407
|
}
|
@@ -249,23 +420,47 @@ function traverse_expr(expr, traverseContext) {
|
|
249
420
|
const typeRight = traverse_expr(exprRight, traverseContext);
|
250
421
|
traverseContext.constraints.push({
|
251
422
|
expression: expr.getText(),
|
252
|
-
type1: typeLeft,
|
253
|
-
type2: typeRight
|
423
|
+
type1: typeLeft.type,
|
424
|
+
type2: typeRight.type
|
254
425
|
});
|
255
426
|
}
|
256
427
|
});
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
428
|
+
const type = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
|
429
|
+
return {
|
430
|
+
name: type.name,
|
431
|
+
type: type,
|
432
|
+
notNull: true,
|
433
|
+
table: type.table || ''
|
434
|
+
};
|
264
435
|
}
|
265
436
|
const select_stmt = expr.select_stmt();
|
266
437
|
if (select_stmt) {
|
267
438
|
const subQueryType = traverse_select_stmt(select_stmt, traverseContext);
|
268
|
-
|
439
|
+
const type = Object.assign(Object.assign({}, subQueryType.columns[0].type), { table: '' });
|
440
|
+
return {
|
441
|
+
name: type.name,
|
442
|
+
type: type,
|
443
|
+
notNull: subQueryType.columns[0].notNull,
|
444
|
+
table: type.table || ''
|
445
|
+
};
|
446
|
+
}
|
447
|
+
if (expr.OPEN_PAR() && expr.CLOSE_PAR()) {
|
448
|
+
const type = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
|
449
|
+
const exprTypes = expr.expr_list().map(innerExpr => {
|
450
|
+
const exprType = traverse_expr(innerExpr, traverseContext);
|
451
|
+
traverseContext.constraints.push({
|
452
|
+
expression: innerExpr.getText(),
|
453
|
+
type1: exprType.type,
|
454
|
+
type2: type
|
455
|
+
});
|
456
|
+
return exprType;
|
457
|
+
});
|
458
|
+
return {
|
459
|
+
name: type.name,
|
460
|
+
type: type,
|
461
|
+
notNull: exprTypes.every(type => type.notNull),
|
462
|
+
table: type.table || ''
|
463
|
+
};
|
269
464
|
}
|
270
465
|
if (expr.CASE_()) {
|
271
466
|
const resultTypes = []; //then and else
|
@@ -274,14 +469,14 @@ function traverse_expr(expr, traverseContext) {
|
|
274
469
|
const type = traverse_expr(expr_, traverseContext);
|
275
470
|
if (expr_.WHEN__list() || expr_.THEN__list()) {
|
276
471
|
if (index % 2 == 0) {
|
277
|
-
whenTypes.push(type);
|
472
|
+
whenTypes.push(type.type);
|
278
473
|
}
|
279
474
|
else {
|
280
|
-
resultTypes.push(type);
|
475
|
+
resultTypes.push(type.type);
|
281
476
|
}
|
282
477
|
}
|
283
478
|
if (expr_.ELSE_()) {
|
284
|
-
resultTypes.push(type);
|
479
|
+
resultTypes.push(type.type);
|
285
480
|
}
|
286
481
|
});
|
287
482
|
resultTypes.forEach((resultType, index) => {
|
@@ -293,35 +488,43 @@ function traverse_expr(expr, traverseContext) {
|
|
293
488
|
});
|
294
489
|
}
|
295
490
|
});
|
296
|
-
|
491
|
+
const type = resultTypes[0];
|
492
|
+
return {
|
493
|
+
name: type.name,
|
494
|
+
type: type,
|
495
|
+
notNull: false,
|
496
|
+
table: type.table || ''
|
497
|
+
};
|
297
498
|
}
|
298
499
|
throw Error('traverse_expr not supported:' + expr.getText());
|
299
500
|
}
|
300
|
-
function
|
301
|
-
|
302
|
-
const
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
return true;
|
311
|
-
}
|
312
|
-
return false;
|
501
|
+
function traverse_column_name(column_name, traverseContext) {
|
502
|
+
const fieldName = (0, select_columns_1.splitName)(column_name.getText());
|
503
|
+
const column = (0, select_columns_1.findColumn)(fieldName, traverseContext.fromColumns);
|
504
|
+
const typeVar = (0, collect_constraints_1.freshVar)(column.columnName, column.columnType.type, column.tableAlias || column.table);
|
505
|
+
return {
|
506
|
+
name: typeVar.name,
|
507
|
+
type: typeVar,
|
508
|
+
table: column.tableAlias || column.table,
|
509
|
+
notNull: column.notNull
|
510
|
+
};
|
313
511
|
}
|
314
|
-
function isMultipleRowResult(
|
315
|
-
const select_stmt = sql_stmtContext.select_stmt();
|
512
|
+
function isMultipleRowResult(select_stmt, fromColumns) {
|
316
513
|
if (select_stmt.select_core_list().length == 1) { //UNION queries are multipleRowsResult = true
|
317
|
-
const
|
514
|
+
const select_core = select_stmt.select_core(0);
|
515
|
+
const from = select_core.FROM_();
|
318
516
|
if (!from) {
|
319
517
|
return false;
|
320
518
|
}
|
321
|
-
const agreegateFunction =
|
519
|
+
const agreegateFunction = select_core.result_column_list().every(result_column => isAgregateFunction(result_column));
|
322
520
|
if (agreegateFunction) {
|
323
521
|
return false;
|
324
522
|
}
|
523
|
+
const _whereExpr = select_core._whereExpr;
|
524
|
+
const isSingleResult = _whereExpr && where_is_single_result(_whereExpr, fromColumns);
|
525
|
+
if (isSingleResult == true) {
|
526
|
+
return false;
|
527
|
+
}
|
325
528
|
}
|
326
529
|
if (isLimitOne(select_stmt)) {
|
327
530
|
return false;
|
@@ -334,7 +537,9 @@ function isAgregateFunction(result_column) {
|
|
334
537
|
const function_name = (_b = (_a = result_column.expr()) === null || _a === void 0 ? void 0 : _a.function_name()) === null || _b === void 0 ? void 0 : _b.getText().toLowerCase();
|
335
538
|
return function_name == 'count'
|
336
539
|
|| function_name == 'sum'
|
337
|
-
|| function_name == 'avg'
|
540
|
+
|| function_name == 'avg'
|
541
|
+
|| function_name == 'min'
|
542
|
+
|| function_name == 'max';
|
338
543
|
}
|
339
544
|
function isLimitOne(select_stmt) {
|
340
545
|
const limit_stmt = select_stmt.limit_stmt();
|
@@ -343,4 +548,131 @@ function isLimitOne(select_stmt) {
|
|
343
548
|
}
|
344
549
|
return false;
|
345
550
|
}
|
551
|
+
function where_is_single_result(whereExpr, fromColumns) {
|
552
|
+
if (whereExpr.ASSIGN()) {
|
553
|
+
const isSingleResult = is_single_result(whereExpr, fromColumns);
|
554
|
+
return isSingleResult;
|
555
|
+
}
|
556
|
+
const expr_list = whereExpr.expr_list();
|
557
|
+
const onlyAnd = !whereExpr.OR_();
|
558
|
+
const oneSingle = expr_list.some(expr => is_single_result(expr, fromColumns));
|
559
|
+
if (onlyAnd && oneSingle) {
|
560
|
+
return true;
|
561
|
+
}
|
562
|
+
return false;
|
563
|
+
}
|
564
|
+
function is_single_result(expr, fromColumns) {
|
565
|
+
const expr1 = expr.expr(0);
|
566
|
+
const expr2 = expr.expr(1); //TODO: 1 = id
|
567
|
+
const column_name = expr1 === null || expr1 === void 0 ? void 0 : expr1.column_name();
|
568
|
+
if (column_name && expr.ASSIGN()) {
|
569
|
+
const fieldName = (0, select_columns_1.splitName)(column_name.getText());
|
570
|
+
const column = (0, select_columns_1.findColumn)(fieldName, fromColumns);
|
571
|
+
if (column.columnKey == 'PRI') {
|
572
|
+
return true;
|
573
|
+
}
|
574
|
+
}
|
575
|
+
return false;
|
576
|
+
}
|
577
|
+
function traverse_insert_stmt(insert_stmt, traverseContext) {
|
578
|
+
const table_name = insert_stmt.table_name();
|
579
|
+
const fromColumns = (0, select_columns_1.filterColumns)(traverseContext.dbSchema, [], '', (0, select_columns_1.splitName)(table_name.getText()));
|
580
|
+
const columns = insert_stmt.column_name_list().map(column_name => {
|
581
|
+
return traverse_column_name(column_name, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
|
582
|
+
});
|
583
|
+
const insertColumns = [];
|
584
|
+
const value_row_list = insert_stmt.values_clause().value_row_list();
|
585
|
+
value_row_list.forEach((value_row) => {
|
586
|
+
value_row.expr_list().forEach((expr, index) => {
|
587
|
+
const numberParamsBefore = traverseContext.parameters.length;
|
588
|
+
const exprType = traverse_expr(expr, traverseContext);
|
589
|
+
traverseContext.parameters.slice(numberParamsBefore).forEach((param) => {
|
590
|
+
const col = columns[index];
|
591
|
+
traverseContext.constraints.push({
|
592
|
+
expression: expr.getText(),
|
593
|
+
type1: col.type,
|
594
|
+
type2: exprType.type
|
595
|
+
});
|
596
|
+
insertColumns.push({
|
597
|
+
name: param.name,
|
598
|
+
type: param,
|
599
|
+
notNull: exprType.notNull && col.notNull,
|
600
|
+
table: ""
|
601
|
+
});
|
602
|
+
});
|
603
|
+
});
|
604
|
+
});
|
605
|
+
const queryResult = {
|
606
|
+
queryType: 'Insert',
|
607
|
+
columns: insertColumns
|
608
|
+
};
|
609
|
+
return queryResult;
|
610
|
+
}
|
611
|
+
function traverse_update_stmt(update_stmt, traverseContext) {
|
612
|
+
const table_name = update_stmt.qualified_table_name().getText();
|
613
|
+
const fromColumns = (0, select_columns_1.filterColumns)(traverseContext.dbSchema, [], '', (0, select_columns_1.splitName)(table_name));
|
614
|
+
const column_name_list = Array.from({ length: update_stmt.ASSIGN_list().length })
|
615
|
+
.map((_, i) => update_stmt.column_name(i));
|
616
|
+
const columns = column_name_list.map(column_name => {
|
617
|
+
return traverse_column_name(column_name, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
|
618
|
+
});
|
619
|
+
const updateColumns = [];
|
620
|
+
const whereParams = [];
|
621
|
+
const expr_list = update_stmt.expr_list();
|
622
|
+
let paramsBefore = traverseContext.parameters.length;
|
623
|
+
expr_list.forEach((expr, index) => {
|
624
|
+
paramsBefore = traverseContext.parameters.length;
|
625
|
+
const exprType = traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
|
626
|
+
if (index < columns.length) {
|
627
|
+
const col = columns[index];
|
628
|
+
traverseContext.constraints.push({
|
629
|
+
expression: expr.getText(),
|
630
|
+
type1: col.type,
|
631
|
+
type2: exprType.type
|
632
|
+
});
|
633
|
+
updateColumns.push({
|
634
|
+
name: col.name,
|
635
|
+
type: exprType.type,
|
636
|
+
notNull: exprType.notNull && col.notNull,
|
637
|
+
table: ""
|
638
|
+
});
|
639
|
+
}
|
640
|
+
else {
|
641
|
+
traverseContext.parameters.slice(paramsBefore).forEach((param, index) => {
|
642
|
+
whereParams.push({
|
643
|
+
name: param.name,
|
644
|
+
type: param,
|
645
|
+
notNull: true,
|
646
|
+
table: ''
|
647
|
+
});
|
648
|
+
});
|
649
|
+
}
|
650
|
+
});
|
651
|
+
const queryResult = {
|
652
|
+
queryType: 'Update',
|
653
|
+
columns: updateColumns,
|
654
|
+
params: whereParams
|
655
|
+
};
|
656
|
+
return queryResult;
|
657
|
+
}
|
658
|
+
function traverse_delete_stmt(delete_stmt, traverseContext) {
|
659
|
+
const table_name = delete_stmt.qualified_table_name().getText();
|
660
|
+
const fromColumns = (0, select_columns_1.filterColumns)(traverseContext.dbSchema, [], '', (0, select_columns_1.splitName)(table_name));
|
661
|
+
const expr = delete_stmt.expr();
|
662
|
+
traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
|
663
|
+
const params = traverseContext.parameters.map(param => {
|
664
|
+
const paramResult = {
|
665
|
+
name: param.name,
|
666
|
+
type: param,
|
667
|
+
notNull: true,
|
668
|
+
table: param.table || ''
|
669
|
+
};
|
670
|
+
return paramResult;
|
671
|
+
});
|
672
|
+
const queryResult = {
|
673
|
+
queryType: 'Delete',
|
674
|
+
params: params
|
675
|
+
};
|
676
|
+
return queryResult;
|
677
|
+
}
|
346
678
|
//# sourceMappingURL=traverse.js.map
|