typesql-cli 0.9.6 → 0.10.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/README.md +2 -3
- package/cli.js +76 -78
- package/cli.js.map +1 -1
- package/code-generator.d.ts +7 -7
- package/code-generator.d.ts.map +1 -1
- package/code-generator.js +193 -194
- package/code-generator.js.map +1 -1
- package/describe-dynamic-query.d.ts +1 -1
- package/describe-dynamic-query.d.ts.map +1 -1
- package/describe-dynamic-query.js +47 -42
- package/describe-dynamic-query.js.map +1 -1
- package/describe-nested-query.d.ts +2 -2
- package/describe-nested-query.d.ts.map +1 -1
- package/describe-nested-query.js +28 -25
- package/describe-nested-query.js.map +1 -1
- package/describe-query.d.ts +4 -4
- package/describe-query.d.ts.map +1 -1
- package/describe-query.js +24 -25
- package/describe-query.js.map +1 -1
- package/drivers/libsql.d.ts +2 -2
- package/drivers/libsql.d.ts.map +1 -1
- package/drivers/libsql.js +4 -5
- package/drivers/libsql.js.map +1 -1
- package/mysql-mapping.d.ts +1 -1
- package/mysql-mapping.d.ts.map +1 -1
- package/mysql-mapping.js +13 -13
- package/mysql-mapping.js.map +1 -1
- package/mysql-query-analyzer/collect-constraints.d.ts +4 -4
- package/mysql-query-analyzer/collect-constraints.d.ts.map +1 -1
- package/mysql-query-analyzer/collect-constraints.js +29 -31
- package/mysql-query-analyzer/collect-constraints.js.map +1 -1
- package/mysql-query-analyzer/infer-column-nullability.d.ts +2 -2
- package/mysql-query-analyzer/infer-column-nullability.d.ts.map +1 -1
- package/mysql-query-analyzer/infer-column-nullability.js +61 -63
- package/mysql-query-analyzer/infer-column-nullability.js.map +1 -1
- package/mysql-query-analyzer/infer-param-nullability.d.ts +2 -2
- package/mysql-query-analyzer/infer-param-nullability.d.ts.map +1 -1
- package/mysql-query-analyzer/infer-param-nullability.js +13 -14
- package/mysql-query-analyzer/infer-param-nullability.js.map +1 -1
- package/mysql-query-analyzer/parse.d.ts +2 -2
- package/mysql-query-analyzer/parse.d.ts.map +1 -1
- package/mysql-query-analyzer/parse.js +42 -40
- package/mysql-query-analyzer/parse.js.map +1 -1
- package/mysql-query-analyzer/select-columns.d.ts +4 -4
- package/mysql-query-analyzer/select-columns.d.ts.map +1 -1
- package/mysql-query-analyzer/select-columns.js +70 -65
- package/mysql-query-analyzer/select-columns.js.map +1 -1
- package/mysql-query-analyzer/traverse.d.ts +4 -4
- package/mysql-query-analyzer/traverse.d.ts.map +1 -1
- package/mysql-query-analyzer/traverse.js +211 -188
- package/mysql-query-analyzer/traverse.js.map +1 -1
- package/mysql-query-analyzer/types.d.ts +5 -5
- package/mysql-query-analyzer/types.d.ts.map +1 -1
- package/mysql-query-analyzer/unify.d.ts +2 -2
- package/mysql-query-analyzer/unify.d.ts.map +1 -1
- package/mysql-query-analyzer/unify.js +485 -487
- package/mysql-query-analyzer/unify.js.map +1 -1
- package/mysql-query-analyzer/util.d.ts.map +1 -1
- package/mysql-query-analyzer/util.js +4 -6
- package/mysql-query-analyzer/util.js.map +1 -1
- package/mysql-query-analyzer/verify-multiple-result.d.ts +2 -2
- package/mysql-query-analyzer/verify-multiple-result.d.ts.map +1 -1
- package/mysql-query-analyzer/verify-multiple-result.js +9 -9
- package/mysql-query-analyzer/verify-multiple-result.js.map +1 -1
- package/package.json +46 -44
- package/queryExectutor.d.ts +4 -4
- package/queryExectutor.d.ts.map +1 -1
- package/queryExectutor.js +48 -51
- package/queryExectutor.js.map +1 -1
- package/sql-generator.d.ts +2 -2
- package/sql-generator.d.ts.map +1 -1
- package/sql-generator.js +30 -29
- package/sql-generator.js.map +1 -1
- package/sqlite-query-analyzer/code-generator.d.ts +6 -6
- package/sqlite-query-analyzer/code-generator.d.ts.map +1 -1
- package/sqlite-query-analyzer/code-generator.js +226 -177
- package/sqlite-query-analyzer/code-generator.js.map +1 -1
- package/sqlite-query-analyzer/parser.d.ts +4 -4
- package/sqlite-query-analyzer/parser.d.ts.map +1 -1
- package/sqlite-query-analyzer/parser.js +17 -18
- package/sqlite-query-analyzer/parser.js.map +1 -1
- package/sqlite-query-analyzer/query-executor.d.ts +6 -6
- package/sqlite-query-analyzer/query-executor.d.ts.map +1 -1
- package/sqlite-query-analyzer/query-executor.js +42 -33
- package/sqlite-query-analyzer/query-executor.js.map +1 -1
- package/sqlite-query-analyzer/replace-list-params.d.ts +1 -1
- package/sqlite-query-analyzer/replace-list-params.d.ts.map +1 -1
- package/sqlite-query-analyzer/replace-list-params.js +5 -5
- package/sqlite-query-analyzer/replace-list-params.js.map +1 -1
- package/sqlite-query-analyzer/sqlite-describe-nested-query.d.ts +4 -4
- package/sqlite-query-analyzer/sqlite-describe-nested-query.d.ts.map +1 -1
- package/sqlite-query-analyzer/sqlite-describe-nested-query.js +19 -17
- package/sqlite-query-analyzer/sqlite-describe-nested-query.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 +185 -155
- package/sqlite-query-analyzer/traverse.js.map +1 -1
- package/ts-dynamic-query-descriptor.d.ts +1 -1
- package/ts-dynamic-query-descriptor.d.ts.map +1 -1
- package/ts-dynamic-query-descriptor.js +7 -8
- package/ts-dynamic-query-descriptor.js.map +1 -1
- package/ts-nested-descriptor.d.ts +4 -4
- package/ts-nested-descriptor.d.ts.map +1 -1
- package/ts-nested-descriptor.js +13 -14
- package/ts-nested-descriptor.js.map +1 -1
- package/types.d.ts +18 -15
- package/types.d.ts.map +1 -1
- package/util.d.ts.map +1 -1
- package/util.js.map +1 -1
- package/utility-types.d.ts.map +1 -1
@@ -1,6 +1,16 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.traverseQueryContext = traverseQueryContext;
|
4
|
+
exports.traverseInsertStatement = traverseInsertStatement;
|
5
|
+
exports.traverseDeleteStatement = traverseDeleteStatement;
|
6
|
+
exports.getUpdateColumns = getUpdateColumns;
|
7
|
+
exports.traverseQuerySpecification = traverseQuerySpecification;
|
8
|
+
exports.traverseWithClause = traverseWithClause;
|
9
|
+
exports.filterColumns = filterColumns;
|
10
|
+
exports.selectAllColumns = selectAllColumns;
|
11
|
+
exports.isMultipleRowResult = isMultipleRowResult;
|
12
|
+
exports.verifyMultipleResult2 = verifyMultipleResult2;
|
13
|
+
exports.getOrderByColumns = getOrderByColumns;
|
4
14
|
const ts_mysql_parser_1 = require("@wsporto/ts-mysql-parser");
|
5
15
|
const describe_query_1 = require("../describe-query");
|
6
16
|
const parse_1 = require("./parse");
|
@@ -55,9 +65,8 @@ function traverseQueryContext(queryContext, dbSchema, namedParameters) {
|
|
55
65
|
const typeInfer = traverseDeleteStatement(deleteStatement, traverseContext);
|
56
66
|
return typeInfer;
|
57
67
|
}
|
58
|
-
throw Error(
|
68
|
+
throw Error(`traverseSql - not supported: ${queryContext.constructor.name}`);
|
59
69
|
}
|
60
|
-
exports.traverseQueryContext = traverseQueryContext;
|
61
70
|
function traverseSelectStatement(selectStatement, traverseContext, namedParameters) {
|
62
71
|
const queryExpression = selectStatement.queryExpression();
|
63
72
|
if (queryExpression) {
|
@@ -65,14 +74,14 @@ function traverseSelectStatement(selectStatement, traverseContext, namedParamete
|
|
65
74
|
const orderByParameters = (0, parse_1.extractOrderByParameters)(selectStatement);
|
66
75
|
const limitParameters = (0, parse_1.extractLimitParameters)(selectStatement);
|
67
76
|
const paramInference = (0, infer_param_nullability_1.inferParamNullabilityQueryExpression)(queryExpression);
|
68
|
-
const allParameters = traverseContext.parameters
|
69
|
-
.map((param, index) => {
|
77
|
+
const allParameters = traverseContext.parameters.map((param, index) => {
|
70
78
|
const param2 = Object.assign(Object.assign({}, param), { notNull: paramInference[index] });
|
71
79
|
return param2;
|
72
80
|
});
|
73
81
|
const paramIndexes = (0, util_1.getParameterIndexes)(namedParameters.slice(0, allParameters.length)); //for [a, a, b, a] will return a: [0, 1, 3]; b: [2]
|
74
|
-
paramIndexes.forEach(paramIndex => {
|
82
|
+
paramIndexes.forEach((paramIndex) => {
|
75
83
|
(0, util_1.getPairWise)(paramIndex.indexes, (cur, next) => {
|
84
|
+
//for [0, 1, 3] will return [0, 1], [1, 3]
|
76
85
|
traverseContext.constraints.push({
|
77
86
|
expression: paramIndex.paramName,
|
78
87
|
type1: allParameters[cur].type,
|
@@ -96,7 +105,7 @@ function traverseSelectStatement(selectStatement, traverseContext, namedParamete
|
|
96
105
|
}
|
97
106
|
return traverseResult;
|
98
107
|
}
|
99
|
-
throw Error(
|
108
|
+
throw Error(`traverseSelectStatement - not supported: ${selectStatement.getText()}`);
|
100
109
|
}
|
101
110
|
function traverseInsertStatement(insertStatement, traverseContext) {
|
102
111
|
var _a, _b;
|
@@ -105,12 +114,15 @@ function traverseInsertStatement(insertStatement, traverseContext) {
|
|
105
114
|
let exprOrDefaultList = [];
|
106
115
|
const valuesContext = (_a = insertStatement.insertFromConstructor()) === null || _a === void 0 ? void 0 : _a.insertValues().valueList().values_list();
|
107
116
|
if (valuesContext) {
|
108
|
-
exprOrDefaultList = valuesContext.map(
|
117
|
+
exprOrDefaultList = valuesContext.map((valueContext) => {
|
118
|
+
var _a;
|
119
|
+
return ((_a = valueContext.children) === null || _a === void 0 ? void 0 : _a.filter((valueContext) => valueContext instanceof ts_mysql_parser_1.ExprIsContext || valueContext.getText() === 'DEFAULT')) || [];
|
120
|
+
});
|
109
121
|
}
|
110
122
|
const insertIntoTable = (0, collect_constraints_1.getInsertIntoTable)(insertStatement);
|
111
123
|
const fromColumns = traverseContext.dbSchema
|
112
|
-
.filter(c => c.table
|
113
|
-
.map(c => {
|
124
|
+
.filter((c) => c.table === insertIntoTable)
|
125
|
+
.map((c) => {
|
114
126
|
const col = {
|
115
127
|
table: c.table,
|
116
128
|
columnName: c.column,
|
@@ -122,20 +134,20 @@ function traverseInsertStatement(insertStatement, traverseContext) {
|
|
122
134
|
});
|
123
135
|
const insertColumns = (0, collect_constraints_1.getInsertColumns)(insertStatement, fromColumns);
|
124
136
|
traverseContext.fromColumns = insertColumns;
|
125
|
-
exprOrDefaultList.forEach(exprOrDefault => {
|
137
|
+
exprOrDefaultList.forEach((exprOrDefault) => {
|
126
138
|
exprOrDefault.forEach((expr, index) => {
|
127
139
|
const column = insertColumns[index];
|
128
140
|
if (expr instanceof ts_mysql_parser_1.ExprContext) {
|
129
141
|
const numberParamsBefore = traverseContext.parameters.length;
|
130
142
|
const exprType = traverseExpr(expr, traverseContext);
|
131
143
|
const paramNullabilityExpr = (0, infer_param_nullability_1.inferParamNullability)(expr);
|
132
|
-
traverseContext.parameters.slice(numberParamsBefore).forEach(param => {
|
133
|
-
paramsNullability[param.type.id] = paramNullabilityExpr.every(n => n) && column.notNull;
|
144
|
+
traverseContext.parameters.slice(numberParamsBefore).forEach((param) => {
|
145
|
+
paramsNullability[param.type.id] = paramNullabilityExpr.every((n) => n) && column.notNull;
|
134
146
|
});
|
135
147
|
traverseContext.constraints.push({
|
136
148
|
expression: expr.getText(),
|
137
149
|
//TODO - CHANGING ORDER SHOULDN'T AFFECT THE TYPE INFERENCE
|
138
|
-
type1: exprType.kind
|
150
|
+
type1: exprType.kind === 'TypeOperator' ? exprType.types[0] : exprType,
|
139
151
|
type2: (0, collect_constraints_1.freshVar)(column.columnName, column.columnType.type)
|
140
152
|
});
|
141
153
|
}
|
@@ -144,7 +156,7 @@ function traverseInsertStatement(insertStatement, traverseContext) {
|
|
144
156
|
});
|
145
157
|
});
|
146
158
|
const updateList = ((_b = insertStatement.insertUpdateList()) === null || _b === void 0 ? void 0 : _b.updateList().updateElement_list()) || [];
|
147
|
-
updateList.forEach(updateElement => {
|
159
|
+
updateList.forEach((updateElement) => {
|
148
160
|
const columnName = updateElement.columnRef().getText();
|
149
161
|
const field = (0, select_columns_1.splitName)(columnName);
|
150
162
|
const expr = updateElement.expr();
|
@@ -152,7 +164,7 @@ function traverseInsertStatement(insertStatement, traverseContext) {
|
|
152
164
|
const numberParamsBefore = traverseContext.parameters.length;
|
153
165
|
const exprType = traverseExpr(expr, traverseContext);
|
154
166
|
const column = (0, select_columns_1.findColumn)(field, fromColumns);
|
155
|
-
traverseContext.parameters.slice(numberParamsBefore).forEach(param => {
|
167
|
+
traverseContext.parameters.slice(numberParamsBefore).forEach((param) => {
|
156
168
|
paramsNullability[param.type.id] = column.notNull;
|
157
169
|
});
|
158
170
|
traverseContext.constraints.push({
|
@@ -169,7 +181,7 @@ function traverseInsertStatement(insertStatement, traverseContext) {
|
|
169
181
|
const exprTypes = traverseInsertQueryExpression(insertQueryExpression, traverseContext);
|
170
182
|
exprTypes.columns.forEach((type, index) => {
|
171
183
|
const column = insertColumns[index];
|
172
|
-
if (type.type.kind
|
184
|
+
if (type.type.kind === 'TypeVar') {
|
173
185
|
paramsNullability[type.type.id] = column.notNull;
|
174
186
|
}
|
175
187
|
traverseContext.constraints.push({
|
@@ -189,7 +201,7 @@ function traverseInsertStatement(insertStatement, traverseContext) {
|
|
189
201
|
typeInfo.forEach((param, index) => {
|
190
202
|
const paramId = traverseContext.parameters[index].type.id;
|
191
203
|
allParameters.push({
|
192
|
-
name:
|
204
|
+
name: `param${allParameters.length + 1}`,
|
193
205
|
columnType: (0, describe_query_1.verifyNotInferred)(param),
|
194
206
|
notNull: paramsNullability[paramId]
|
195
207
|
});
|
@@ -201,7 +213,6 @@ function traverseInsertStatement(insertStatement, traverseContext) {
|
|
201
213
|
};
|
202
214
|
return typeInferenceResult;
|
203
215
|
}
|
204
|
-
exports.traverseInsertStatement = traverseInsertStatement;
|
205
216
|
function traverseUpdateStatement(updateStatement, traverseContext, namedParamters) {
|
206
217
|
var _a;
|
207
218
|
const updateElement = updateStatement.updateList().updateElement_list();
|
@@ -217,7 +228,7 @@ function traverseUpdateStatement(updateStatement, traverseContext, namedParamter
|
|
217
228
|
const paramsBefore = traverseContext.parameters.length;
|
218
229
|
const whereExpr = (_a = updateStatement.whereClause()) === null || _a === void 0 ? void 0 : _a.expr();
|
219
230
|
const paramNullability = (0, infer_param_nullability_1.inferParamNullability)(updateStatement);
|
220
|
-
updateElement.forEach(updateElement => {
|
231
|
+
updateElement.forEach((updateElement) => {
|
221
232
|
const expr = updateElement.expr();
|
222
233
|
if (expr) {
|
223
234
|
const paramBeforeExpr = traverseContext.parameters.length;
|
@@ -246,7 +257,7 @@ function traverseUpdateStatement(updateStatement, traverseContext, namedParamter
|
|
246
257
|
}
|
247
258
|
traverseContext.parameters.slice(0, paramsBefore).forEach((param, index) => {
|
248
259
|
whereParameters.push({
|
249
|
-
name: namedParamters[index] ||
|
260
|
+
name: namedParamters[index] || `param${whereParameters.length + 1}`,
|
250
261
|
type: param.type,
|
251
262
|
notNull: paramNullability[index],
|
252
263
|
table: ''
|
@@ -254,7 +265,7 @@ function traverseUpdateStatement(updateStatement, traverseContext, namedParamter
|
|
254
265
|
});
|
255
266
|
traverseContext.parameters.slice(paramsAfter).forEach((param, index) => {
|
256
267
|
whereParameters.push({
|
257
|
-
name: namedParamters[paramsAfter + index] ||
|
268
|
+
name: namedParamters[paramsAfter + index] || `param${whereParameters.length + 1}`,
|
258
269
|
type: param.type,
|
259
270
|
notNull: paramNullability[paramsAfter + index],
|
260
271
|
table: ''
|
@@ -280,7 +291,7 @@ function traverseDeleteStatement(deleteStatement, traverseContext) {
|
|
280
291
|
const paramNullability = (0, infer_param_nullability_1.inferParamNullability)(whereExpr);
|
281
292
|
typeInfo.forEach((param, paramIndex) => {
|
282
293
|
allParameters.push({
|
283
|
-
name:
|
294
|
+
name: `param${allParameters.length + 1}`,
|
284
295
|
columnType: (0, describe_query_1.verifyNotInferred)(param),
|
285
296
|
notNull: paramNullability[paramIndex]
|
286
297
|
});
|
@@ -293,13 +304,11 @@ function traverseDeleteStatement(deleteStatement, traverseContext) {
|
|
293
304
|
};
|
294
305
|
return typeInferenceResult;
|
295
306
|
}
|
296
|
-
exports.traverseDeleteStatement = traverseDeleteStatement;
|
297
307
|
function getUpdateColumns(updateStatement, traverseContext) {
|
298
308
|
const tableReferences = updateStatement.tableReferenceList().tableReference_list();
|
299
309
|
const columns = traverseTableReferenceList(tableReferences, traverseContext, null);
|
300
310
|
return columns;
|
301
311
|
}
|
302
|
-
exports.getUpdateColumns = getUpdateColumns;
|
303
312
|
function traverseInsertQueryExpression(insertQueryExpression, traverseContext) {
|
304
313
|
const queryExpressionOrParens = insertQueryExpression.queryExpressionOrParens();
|
305
314
|
return traverseQueryExpressionOrParens(queryExpressionOrParens, traverseContext);
|
@@ -313,7 +322,7 @@ function traverseQueryExpressionOrParens(queryExpressionOrParens, traverseContex
|
|
313
322
|
if (queryEpressionParens) {
|
314
323
|
return traverseQueryExpressionParens(queryEpressionParens, traverseContext);
|
315
324
|
}
|
316
|
-
throw Error(
|
325
|
+
throw Error('walkQueryExpressionOrParens');
|
317
326
|
}
|
318
327
|
function traverseQueryExpression(queryExpression, traverseContext, cte, recursiveNames) {
|
319
328
|
const withClause = queryExpression.withClause();
|
@@ -332,7 +341,10 @@ function traverseQueryExpression(queryExpression, traverseContext, cte, recursiv
|
|
332
341
|
const orderByClause = queryExpression.orderClause();
|
333
342
|
if (orderByClause) {
|
334
343
|
if (orderByClause.getText().toLowerCase() !== 'orderby?') {
|
335
|
-
orderByClause
|
344
|
+
orderByClause
|
345
|
+
.orderList()
|
346
|
+
.orderExpression_list()
|
347
|
+
.forEach((orderByExpr) => {
|
336
348
|
traverseExpr(orderByExpr.expr(), Object.assign(Object.assign({}, traverseContext), { fromColumns: exprResult.fromColumns || [] }));
|
337
349
|
});
|
338
350
|
}
|
@@ -348,7 +360,7 @@ function traverseQueryExpressionParens(queryExpressionParens, traverseContext, c
|
|
348
360
|
if (queryExpressionParens2) {
|
349
361
|
return traverseQueryExpressionParens(queryExpressionParens, traverseContext, cte, recursiveNames);
|
350
362
|
}
|
351
|
-
throw Error(
|
363
|
+
throw Error('walkQueryExpressionParens');
|
352
364
|
}
|
353
365
|
function createUnionVar(type, name) {
|
354
366
|
const newVar = Object.assign(Object.assign({}, type), { name: name, table: '' });
|
@@ -358,21 +370,23 @@ function traverseQueryExpressionBody(queryExpressionBody, traverseContext, cte,
|
|
358
370
|
const allQueries = (0, parse_1.getAllQuerySpecificationsFromSelectStatement)(queryExpressionBody);
|
359
371
|
const [first, ...unionQuerySpec] = allQueries;
|
360
372
|
const mainQueryResult = traverseQuerySpecification(first, traverseContext);
|
361
|
-
const resultTypes = mainQueryResult.columns.map((t, index) => unionQuerySpec.length
|
373
|
+
const resultTypes = mainQueryResult.columns.map((t, index) => unionQuerySpec.length === 0
|
374
|
+
? t.type
|
375
|
+
: createUnionVar(t.type, recursiveNames && recursiveNames.length > 0 ? recursiveNames[index] : t.name)); //TODO mover para traversequeryspecificat?
|
362
376
|
if (cte) {
|
363
377
|
resultTypes.forEach((col, index) => {
|
364
378
|
const withCol = {
|
365
379
|
table: cte,
|
366
380
|
columnName: col.name,
|
367
381
|
columnType: col,
|
368
|
-
columnKey:
|
382
|
+
columnKey: '',
|
369
383
|
notNull: mainQueryResult.columns[index].notNull
|
370
384
|
};
|
371
385
|
traverseContext.withSchema.push(withCol);
|
372
386
|
});
|
373
387
|
}
|
374
388
|
for (let queryIndex = 0; queryIndex < unionQuerySpec.length; queryIndex++) {
|
375
|
-
const columnNames = recursiveNames && recursiveNames.length > 0 ? recursiveNames : mainQueryResult.columns.map(col => col.name);
|
389
|
+
const columnNames = recursiveNames && recursiveNames.length > 0 ? recursiveNames : mainQueryResult.columns.map((col) => col.name);
|
376
390
|
const newFromColumns = recursiveNames ? renameFromColumns(mainQueryResult.columns, columnNames) : [];
|
377
391
|
const unionQuery = unionQuerySpec[queryIndex];
|
378
392
|
const unionResult = traverseQuerySpecification(unionQuery, Object.assign(Object.assign({}, traverseContext), { fromColumns: newFromColumns }));
|
@@ -405,10 +419,10 @@ function traverseQueryExpressionBody(queryExpressionBody, traverseContext, cte,
|
|
405
419
|
function renameFromColumns(fromColumns, recursiveNames) {
|
406
420
|
const newFromColumns = fromColumns.map((col, index) => {
|
407
421
|
const newCol = {
|
408
|
-
table:
|
422
|
+
table: '',
|
409
423
|
columnName: recursiveNames[index],
|
410
424
|
columnType: col.type,
|
411
|
-
columnKey:
|
425
|
+
columnKey: '',
|
412
426
|
notNull: col.notNull
|
413
427
|
};
|
414
428
|
return newCol;
|
@@ -438,12 +452,12 @@ function traverseQuerySpecification(querySpec, traverseContext) {
|
|
438
452
|
});
|
439
453
|
const havingClause = querySpec.havingClause();
|
440
454
|
if (havingClause) {
|
441
|
-
const selectColumns = columns.map(c => {
|
455
|
+
const selectColumns = columns.map((c) => {
|
442
456
|
const col = {
|
443
|
-
table:
|
457
|
+
table: '',
|
444
458
|
columnName: c.name,
|
445
459
|
columnType: c.type,
|
446
|
-
columnKey:
|
460
|
+
columnKey: '',
|
447
461
|
notNull: c.notNull
|
448
462
|
};
|
449
463
|
return col;
|
@@ -455,17 +469,19 @@ function traverseQuerySpecification(querySpec, traverseContext) {
|
|
455
469
|
fromColumns: fromColumnsFrom
|
456
470
|
};
|
457
471
|
}
|
458
|
-
exports.traverseQuerySpecification = traverseQuerySpecification;
|
459
472
|
function traverseWithClause(withClause, traverseContext) {
|
460
473
|
//result1, result2
|
461
|
-
withClause.commonTableExpression_list().forEach(commonTableExpression => {
|
474
|
+
withClause.commonTableExpression_list().forEach((commonTableExpression) => {
|
462
475
|
var _a, _b;
|
463
476
|
const cte = commonTableExpression.identifier().getText();
|
464
|
-
const recursiveNames = withClause.RECURSIVE_SYMBOL()
|
477
|
+
const recursiveNames = withClause.RECURSIVE_SYMBOL()
|
478
|
+
? ((_a = commonTableExpression
|
479
|
+
.columnInternalRefList()) === null || _a === void 0 ? void 0 : _a.columnInternalRef_list().map((t) => t.getText())) || []
|
480
|
+
: undefined;
|
465
481
|
const subQuery = commonTableExpression.subquery();
|
466
482
|
traverseSubquery(subQuery, traverseContext, cte, recursiveNames); //recursive= true??
|
467
483
|
(_b = traverseContext.dynamicSqlInfo.with) === null || _b === void 0 ? void 0 : _b.push({
|
468
|
-
fragment: (0, select_columns_1.extractOriginalSql)(commonTableExpression)
|
484
|
+
fragment: `${(0, select_columns_1.extractOriginalSql)(commonTableExpression)}`,
|
469
485
|
relation: cte,
|
470
486
|
fields: [],
|
471
487
|
dependOnFields: [],
|
@@ -475,7 +491,6 @@ function traverseWithClause(withClause, traverseContext) {
|
|
475
491
|
});
|
476
492
|
});
|
477
493
|
}
|
478
|
-
exports.traverseWithClause = traverseWithClause;
|
479
494
|
function traverseFromClause(fromClause, traverseContext) {
|
480
495
|
var _a;
|
481
496
|
const tableReferenceList = (_a = fromClause.tableReferenceList()) === null || _a === void 0 ? void 0 : _a.tableReference_list();
|
@@ -485,15 +500,15 @@ function traverseFromClause(fromClause, traverseContext) {
|
|
485
500
|
function traverseTableReferenceList(tableReferenceList, traverseContext, currentFragment) {
|
486
501
|
const result = [];
|
487
502
|
const fragements = [];
|
488
|
-
tableReferenceList.forEach(tab => {
|
503
|
+
tableReferenceList.forEach((tab) => {
|
489
504
|
const tableFactor = tab.tableFactor();
|
490
505
|
if (tableFactor) {
|
491
|
-
|
506
|
+
const paramBefore = traverseContext.parameters.length;
|
492
507
|
const fields = traverseTableFactor(tableFactor, traverseContext, currentFragment);
|
493
|
-
|
508
|
+
const paramsAfter = traverseContext.parameters.length;
|
494
509
|
result.push(...fields);
|
495
510
|
fragements.push({
|
496
|
-
fragment:
|
511
|
+
fragment: `FROM ${(0, select_columns_1.extractOriginalSql)(tableFactor)}`,
|
497
512
|
fields: [], //fields.map(field => ({ field: field.columnName, name: field.columnName, table: field.table })),
|
498
513
|
dependOnFields: [],
|
499
514
|
dependOnParams: [],
|
@@ -508,13 +523,13 @@ function traverseTableReferenceList(tableReferenceList, traverseContext, current
|
|
508
523
|
if (((_a = joined.innerJoinType()) === null || _a === void 0 ? void 0 : _a.INNER_SYMBOL()) || ((_b = joined.innerJoinType()) === null || _b === void 0 ? void 0 : _b.JOIN_SYMBOL())) {
|
509
524
|
firstLeftJoinIndex = -1; //dont need to add notNull = false to joins
|
510
525
|
}
|
511
|
-
else if (firstLeftJoinIndex
|
526
|
+
else if (firstLeftJoinIndex === -1) {
|
512
527
|
firstLeftJoinIndex = index; //add notNull = false to all joins after the first left join
|
513
528
|
}
|
514
529
|
const tableReferences = joined.tableReference();
|
515
530
|
if (tableReferences) {
|
516
531
|
const innerJoinFragment = {
|
517
|
-
fragment: (0, select_columns_1.extractOriginalSql)(joined)
|
532
|
+
fragment: `${(0, select_columns_1.extractOriginalSql)(joined)}`,
|
518
533
|
fields: [],
|
519
534
|
dependOnFields: [],
|
520
535
|
dependOnParams: [],
|
@@ -528,16 +543,18 @@ function traverseTableReferenceList(tableReferenceList, traverseContext, current
|
|
528
543
|
//doesn't duplicate the fields of the USING clause. Ex. INNER JOIN mytable2 USING(id);
|
529
544
|
const joinedFieldsFiltered = usingFields.length > 0 ? filterUsingFields(joinedFields, usingFields) : joinedFields;
|
530
545
|
allJoinedColumns.push(joinedFieldsFiltered);
|
531
|
-
innerJoinFragment.fields = [
|
546
|
+
innerJoinFragment.fields = [
|
547
|
+
...joinedFieldsFiltered.map((f) => ({
|
532
548
|
field: f.columnName,
|
533
549
|
table: f.tableAlias || f.table,
|
534
550
|
name: f.columnName
|
535
|
-
}))
|
551
|
+
}))
|
552
|
+
];
|
536
553
|
innerJoinFragment.parameters = Array.from({ length: paramsAfter - paramsBefore }, (x, i) => i + paramsAfter - 1);
|
537
554
|
fragements.push(innerJoinFragment);
|
538
555
|
const onClause = joined.expr(); //ON expr
|
539
556
|
if (onClause) {
|
540
|
-
joinedFieldsFiltered.forEach(field => {
|
557
|
+
joinedFieldsFiltered.forEach((field) => {
|
541
558
|
const fieldName = {
|
542
559
|
name: field.columnName,
|
543
560
|
prefix: field.tableAlias || ''
|
@@ -545,18 +562,18 @@ function traverseTableReferenceList(tableReferenceList, traverseContext, current
|
|
545
562
|
field.notNull = field.notNull || !(0, infer_column_nullability_1.possibleNull)(fieldName, onClause);
|
546
563
|
});
|
547
564
|
//apply inference to the parent join too
|
548
|
-
result.forEach(field => {
|
565
|
+
result.forEach((field) => {
|
549
566
|
const fieldName = {
|
550
567
|
name: field.columnName,
|
551
568
|
prefix: field.tableAlias || ''
|
552
569
|
};
|
553
570
|
field.notNull = field.notNull || !(0, infer_column_nullability_1.possibleNull)(fieldName, onClause);
|
554
571
|
});
|
555
|
-
traverseExpr(onClause, Object.assign(Object.assign({}, traverseContext), { fromColumns: allJoinedColumns.
|
572
|
+
traverseExpr(onClause, Object.assign(Object.assign({}, traverseContext), { fromColumns: allJoinedColumns.flat().concat(result) }));
|
556
573
|
const columns = (0, select_columns_1.getExpressions)(onClause, ts_mysql_parser_1.SimpleExprColumnRefContext);
|
557
|
-
columns.forEach(columnRef => {
|
574
|
+
columns.forEach((columnRef) => {
|
558
575
|
const fieldName = (0, select_columns_1.splitName)(columnRef.expr.getText());
|
559
|
-
if ((innerJoinFragment === null || innerJoinFragment === void 0 ? void 0 : innerJoinFragment.relation)
|
576
|
+
if ((innerJoinFragment === null || innerJoinFragment === void 0 ? void 0 : innerJoinFragment.relation) !== fieldName.prefix) {
|
560
577
|
innerJoinFragment.parentRelation = fieldName.prefix;
|
561
578
|
}
|
562
579
|
});
|
@@ -564,8 +581,8 @@ function traverseTableReferenceList(tableReferenceList, traverseContext, current
|
|
564
581
|
}
|
565
582
|
});
|
566
583
|
allJoinedColumns.forEach((joinedColumns, index) => {
|
567
|
-
joinedColumns.forEach(field => {
|
568
|
-
if (firstLeftJoinIndex
|
584
|
+
joinedColumns.forEach((field) => {
|
585
|
+
if (firstLeftJoinIndex !== -1 && index >= firstLeftJoinIndex) {
|
569
586
|
const newField = Object.assign(Object.assign({}, field), { notNull: false });
|
570
587
|
result.push(newField);
|
571
588
|
}
|
@@ -595,12 +612,12 @@ function traverseTableFactor(tableFactor, traverseContext, currentFragment) {
|
|
595
612
|
const subQuery = derivadTable.subquery();
|
596
613
|
if (subQuery) {
|
597
614
|
const subQueryResult = traverseSubquery(subQuery, traverseContext);
|
598
|
-
const result = subQueryResult.columns.map(t => {
|
615
|
+
const result = subQueryResult.columns.map((t) => {
|
599
616
|
const colDef = {
|
600
617
|
table: t.table ? tableAlias || '' : '',
|
601
618
|
columnName: t.name,
|
602
619
|
columnType: t.type,
|
603
|
-
columnKey:
|
620
|
+
columnKey: '',
|
604
621
|
notNull: t.notNull,
|
605
622
|
tableAlias: tableAlias
|
606
623
|
};
|
@@ -614,7 +631,7 @@ function traverseTableFactor(tableFactor, traverseContext, currentFragment) {
|
|
614
631
|
const listParens = traverseTableReferenceListParens(tableReferenceListParens, traverseContext);
|
615
632
|
return listParens;
|
616
633
|
}
|
617
|
-
throw Error(
|
634
|
+
throw Error(`traverseTableFactor - not supported: ${tableFactor.constructor.name}`);
|
618
635
|
}
|
619
636
|
//tableReferenceList | tableReferenceListParens
|
620
637
|
function traverseTableReferenceListParens(ctx, traverseContext) {
|
@@ -626,7 +643,7 @@ function traverseTableReferenceListParens(ctx, traverseContext) {
|
|
626
643
|
if (tableReferenceListParens) {
|
627
644
|
return traverseTableReferenceListParens(tableReferenceListParens, traverseContext);
|
628
645
|
}
|
629
|
-
throw Error(
|
646
|
+
throw Error(`traverseTableReferenceListParens - not supported: ${ctx.constructor.name}`);
|
630
647
|
}
|
631
648
|
function traverseSingleTable(singleTable, dbSchema, withSchema, currentFragment, withFragments) {
|
632
649
|
var _a;
|
@@ -636,8 +653,8 @@ function traverseSingleTable(singleTable, dbSchema, withSchema, currentFragment,
|
|
636
653
|
if (currentFragment) {
|
637
654
|
currentFragment.relation = tableAlias || tableName.name;
|
638
655
|
}
|
639
|
-
withFragments === null || withFragments === void 0 ? void 0 : withFragments.forEach(withFragment => {
|
640
|
-
if (withFragment.relation
|
656
|
+
withFragments === null || withFragments === void 0 ? void 0 : withFragments.forEach((withFragment) => {
|
657
|
+
if (withFragment.relation === table) {
|
641
658
|
withFragment.parentRelation = tableAlias || tableName.name;
|
642
659
|
}
|
643
660
|
});
|
@@ -654,23 +671,25 @@ function traverseSubquery(subQuery, traverseContext, cte, recursiveNames) {
|
|
654
671
|
if (queryExpressionParens2) {
|
655
672
|
return traverseQueryExpressionParens(queryExpressionParens2, traverseContext);
|
656
673
|
}
|
657
|
-
throw Error(
|
674
|
+
throw Error(`traverseSubquery - not expected: ${subQuery.constructor.name}`);
|
658
675
|
}
|
659
676
|
function traverseSelectItemList(selectItemList, traverseContext) {
|
660
677
|
const listType = [];
|
661
678
|
if (selectItemList.MULT_OPERATOR()) {
|
662
|
-
traverseContext.fromColumns.forEach(col => {
|
679
|
+
traverseContext.fromColumns.forEach((col) => {
|
663
680
|
// const columnType = createColumnType(col);
|
664
681
|
const tableName = col.tableAlias || col.table;
|
665
682
|
listType.push(Object.assign(Object.assign({}, col.columnType), { table: tableName }));
|
666
683
|
const fieldFragment = {
|
667
684
|
fragment: `${tableName}.${col.columnName}`,
|
668
685
|
fragementWithoutAlias: `${tableName}.${col.columnName}`,
|
669
|
-
fields: [
|
686
|
+
fields: [
|
687
|
+
{
|
670
688
|
field: col.columnName,
|
671
689
|
name: col.columnName,
|
672
690
|
table: tableName
|
673
|
-
}
|
691
|
+
}
|
692
|
+
],
|
674
693
|
dependOnFields: [],
|
675
694
|
dependOnParams: [],
|
676
695
|
parameters: [],
|
@@ -681,14 +700,14 @@ function traverseSelectItemList(selectItemList, traverseContext) {
|
|
681
700
|
}
|
682
701
|
});
|
683
702
|
}
|
684
|
-
selectItemList.selectItem_list().forEach(selectItem => {
|
703
|
+
selectItemList.selectItem_list().forEach((selectItem) => {
|
685
704
|
var _a;
|
686
705
|
const tableWild = selectItem.tableWild();
|
687
706
|
if (tableWild) {
|
688
707
|
if (tableWild.MULT_OPERATOR()) {
|
689
708
|
const itemName = (0, select_columns_1.splitName)(selectItem.getText());
|
690
709
|
const allColumns = selectAllColumns(itemName.prefix, traverseContext.fromColumns);
|
691
|
-
allColumns.forEach(col => {
|
710
|
+
allColumns.forEach((col) => {
|
692
711
|
const columnType = (0, collect_constraints_1.createColumnType)(col);
|
693
712
|
listType.push(columnType);
|
694
713
|
});
|
@@ -697,7 +716,7 @@ function traverseSelectItemList(selectItemList, traverseContext) {
|
|
697
716
|
const expr = selectItem.expr();
|
698
717
|
if (expr) {
|
699
718
|
const selectFragment = {
|
700
|
-
fragment: (0, select_columns_1.extractOriginalSql)(selectItem)
|
719
|
+
fragment: `${(0, select_columns_1.extractOriginalSql)(selectItem)}`,
|
701
720
|
fragementWithoutAlias: (0, select_columns_1.extractOriginalSql)(expr),
|
702
721
|
fields: [],
|
703
722
|
dependOnFields: [],
|
@@ -710,12 +729,12 @@ function traverseSelectItemList(selectItemList, traverseContext) {
|
|
710
729
|
(_a = traverseContext.dynamicSqlInfo.select) === null || _a === void 0 ? void 0 : _a.push(selectFragment);
|
711
730
|
}
|
712
731
|
// const fields = exprType.kind == 'TypeVar' ? [{ field: exprType.name, table: exprType.table + '', name: getColumnName(selectItem) }] : []
|
713
|
-
if (exprType.kind
|
732
|
+
if (exprType.kind === 'TypeOperator') {
|
714
733
|
const subqueryType = exprType.types[0];
|
715
734
|
subqueryType.name = (0, select_columns_1.getColumnName)(selectItem);
|
716
735
|
listType.push(subqueryType);
|
717
736
|
}
|
718
|
-
else if (exprType.kind
|
737
|
+
else if (exprType.kind === 'TypeVar') {
|
719
738
|
exprType.name = (0, select_columns_1.getColumnName)(selectItem);
|
720
739
|
listType.push(Object.assign({}, exprType)); //clone
|
721
740
|
}
|
@@ -738,7 +757,7 @@ function traverseExpr(expr, traverseContext) {
|
|
738
757
|
const boolPriType = traverseBoolPri(boolPri, traverseContext);
|
739
758
|
if (traverseContext.where) {
|
740
759
|
const currentFragment = {
|
741
|
-
fragment:
|
760
|
+
fragment: `AND ${(0, select_columns_1.extractOriginalSql)(expr)}`,
|
742
761
|
fields: [],
|
743
762
|
dependOnFields: [],
|
744
763
|
dependOnParams: [],
|
@@ -746,12 +765,12 @@ function traverseExpr(expr, traverseContext) {
|
|
746
765
|
dependOn: []
|
747
766
|
};
|
748
767
|
const paramsRight = (0, select_columns_1.getExpressions)(expr, ts_mysql_parser_1.SimpleExprParamMarkerContext);
|
749
|
-
paramsRight.forEach(_ => {
|
768
|
+
paramsRight.forEach((_) => {
|
750
769
|
currentFragment.dependOnParams.push(paramsCount);
|
751
770
|
paramsCount++;
|
752
771
|
});
|
753
772
|
const columnsRef = (0, select_columns_1.getExpressions)(expr, ts_mysql_parser_1.ColumnRefContext);
|
754
|
-
columnsRef.forEach(colRef => {
|
773
|
+
columnsRef.forEach((colRef) => {
|
755
774
|
const fileName = (0, select_columns_1.splitName)(colRef.expr.getText());
|
756
775
|
currentFragment.fields.push({
|
757
776
|
field: fileName.name,
|
@@ -769,17 +788,16 @@ function traverseExpr(expr, traverseContext) {
|
|
769
788
|
return traverseExpr(expr2, traverseContext);
|
770
789
|
}
|
771
790
|
return (0, collect_constraints_1.freshVar)(expr.getText(), 'tinyint');
|
772
|
-
;
|
773
791
|
}
|
774
792
|
if (expr instanceof ts_mysql_parser_1.ExprAndContext || expr instanceof ts_mysql_parser_1.ExprXorContext || expr instanceof ts_mysql_parser_1.ExprOrContext) {
|
775
793
|
const all = [];
|
776
794
|
(0, select_columns_1.getTopLevelAndExpr)(expr, all);
|
777
|
-
all.forEach(andExpression => {
|
795
|
+
all.forEach((andExpression) => {
|
778
796
|
let paramsCount = traverseContext.parameters.length;
|
779
797
|
traverseExpr(andExpression.expr, Object.assign(Object.assign({}, traverseContext), { where: false }));
|
780
798
|
if (traverseContext.where) {
|
781
799
|
const currentFragment = {
|
782
|
-
fragment: andExpression.operator
|
800
|
+
fragment: `${andExpression.operator} ${(0, select_columns_1.extractOriginalSql)(andExpression.expr)}`,
|
783
801
|
fields: [],
|
784
802
|
dependOnFields: [],
|
785
803
|
dependOnParams: [],
|
@@ -787,12 +805,12 @@ function traverseExpr(expr, traverseContext) {
|
|
787
805
|
dependOn: []
|
788
806
|
};
|
789
807
|
const paramsRight = (0, select_columns_1.getExpressions)(andExpression.expr, ts_mysql_parser_1.SimpleExprParamMarkerContext);
|
790
|
-
paramsRight.forEach(_ => {
|
808
|
+
paramsRight.forEach((_) => {
|
791
809
|
currentFragment.dependOnParams.push(paramsCount);
|
792
810
|
paramsCount++;
|
793
811
|
});
|
794
812
|
const columnsRef = (0, select_columns_1.getExpressions)(andExpression.expr, ts_mysql_parser_1.ColumnRefContext);
|
795
|
-
columnsRef.forEach(colRef => {
|
813
|
+
columnsRef.forEach((colRef) => {
|
796
814
|
const fileName = (0, select_columns_1.splitName)(colRef.expr.getText());
|
797
815
|
currentFragment.fields.push({
|
798
816
|
field: fileName.name,
|
@@ -805,7 +823,7 @@ function traverseExpr(expr, traverseContext) {
|
|
805
823
|
});
|
806
824
|
return (0, collect_constraints_1.freshVar)(expr.getText(), 'tinyint');
|
807
825
|
}
|
808
|
-
throw Error(
|
826
|
+
throw Error(`traverseExpr - not supported: ${expr.getText()}`);
|
809
827
|
}
|
810
828
|
function traverseBoolPri(boolPri, traverseContext) {
|
811
829
|
if (boolPri instanceof ts_mysql_parser_1.PrimaryExprPredicateContext) {
|
@@ -841,12 +859,12 @@ function traverseBoolPri(boolPri, traverseContext) {
|
|
841
859
|
type1: typeLeft,
|
842
860
|
type2: {
|
843
861
|
kind: 'TypeOperator',
|
844
|
-
types: subQueryResult.columns.map(t => t.type)
|
862
|
+
types: subQueryResult.columns.map((t) => t.type)
|
845
863
|
}
|
846
864
|
});
|
847
865
|
return (0, collect_constraints_1.freshVar)(boolPri.getText(), 'tinyint');
|
848
866
|
}
|
849
|
-
throw Error(
|
867
|
+
throw Error(`traverseExpr - not supported: ${boolPri.constructor.name}`);
|
850
868
|
}
|
851
869
|
function traversePredicate(predicate, traverseContext) {
|
852
870
|
const bitExpr = predicate.bitExpr(0); //TODO - predicate length = 2? [1] == predicateOperations
|
@@ -854,7 +872,7 @@ function traversePredicate(predicate, traverseContext) {
|
|
854
872
|
const predicateOperations = predicate.predicateOperations();
|
855
873
|
if (predicateOperations) {
|
856
874
|
const rightType = traversePredicateOperations(predicateOperations, bitExprType, traverseContext);
|
857
|
-
if (bitExprType.kind
|
875
|
+
if (bitExprType.kind === 'TypeOperator' && rightType.kind === 'TypeOperator') {
|
858
876
|
rightType.types.forEach((t, i) => {
|
859
877
|
traverseContext.constraints.push({
|
860
878
|
expression: predicateOperations.getText(),
|
@@ -864,7 +882,7 @@ function traversePredicate(predicate, traverseContext) {
|
|
864
882
|
});
|
865
883
|
});
|
866
884
|
}
|
867
|
-
if (bitExprType.kind
|
885
|
+
if (bitExprType.kind === 'TypeVar' && rightType.kind === 'TypeOperator') {
|
868
886
|
rightType.types.forEach((t, i) => {
|
869
887
|
traverseContext.constraints.push({
|
870
888
|
expression: predicateOperations.getText(),
|
@@ -880,7 +898,7 @@ function traversePredicate(predicate, traverseContext) {
|
|
880
898
|
return bitExprType;
|
881
899
|
}
|
882
900
|
function traverseExprList(exprList, traverseContext) {
|
883
|
-
const listType = exprList.expr_list().map(item => {
|
901
|
+
const listType = exprList.expr_list().map((item) => {
|
884
902
|
const exprType = traverseExpr(item, traverseContext);
|
885
903
|
return exprType;
|
886
904
|
});
|
@@ -895,18 +913,18 @@ function traverseBitExpr(bitExpr, traverseContext) {
|
|
895
913
|
if (simpleExpr) {
|
896
914
|
return traverseSimpleExpr(simpleExpr, Object.assign(Object.assign({}, traverseContext), { where: false }));
|
897
915
|
}
|
898
|
-
if (bitExpr.bitExpr_list().length
|
916
|
+
if (bitExpr.bitExpr_list().length === 2) {
|
899
917
|
const bitExprLeft = bitExpr.bitExpr(0);
|
900
918
|
const typeLeftTemp = traverseBitExpr(bitExprLeft, traverseContext);
|
901
|
-
const typeLeft = typeLeftTemp.kind
|
919
|
+
const typeLeft = typeLeftTemp.kind === 'TypeOperator' ? typeLeftTemp.types[0] : typeLeftTemp;
|
902
920
|
//const newTypeLeft = typeLeft.name == '?'? freshVar('?', 'bigint') : typeLeft;
|
903
921
|
const bitExprRight = bitExpr.bitExpr(1);
|
904
922
|
const typeRightTemp = traverseBitExpr(bitExprRight, traverseContext);
|
905
923
|
//In the expression 'id + (value + 2) + ?' the '(value+2)' is treated as a SimpleExprListContext and return a TypeOperator
|
906
|
-
const typeRight = typeRightTemp.kind
|
924
|
+
const typeRight = typeRightTemp.kind === 'TypeOperator' ? typeRightTemp.types[0] : typeRightTemp;
|
907
925
|
//const newTypeRight = typeRight.name == '?'? freshVar('?', 'bigint') : typeRight;
|
908
926
|
const bitExprType = (0, collect_constraints_1.freshVar)(bitExpr.getText(), '?');
|
909
|
-
if (typeLeftTemp.kind
|
927
|
+
if (typeLeftTemp.kind === 'TypeVar' && typeRightTemp.kind === 'TypeVar' && typeLeftTemp.table === typeRightTemp.table) {
|
910
928
|
bitExprType.table = typeLeftTemp.table;
|
911
929
|
}
|
912
930
|
//PRECISA?
|
@@ -967,14 +985,17 @@ function traverseBitExpr(bitExpr, traverseContext) {
|
|
967
985
|
});
|
968
986
|
return (0, collect_constraints_1.freshVar)('datetime', 'datetime');
|
969
987
|
}
|
970
|
-
throw Error(
|
988
|
+
throw Error(`traverseBitExpr - not supported: ${bitExpr.constructor.name}`);
|
971
989
|
}
|
972
990
|
function traversePredicateOperations(predicateOperations, parentType, traverseContext) {
|
973
991
|
if (predicateOperations instanceof ts_mysql_parser_1.PredicateExprInContext) {
|
974
992
|
const subquery = predicateOperations.subquery();
|
975
993
|
if (subquery) {
|
976
994
|
const subQueryResult = traverseSubquery(subquery, traverseContext);
|
977
|
-
return {
|
995
|
+
return {
|
996
|
+
kind: 'TypeOperator',
|
997
|
+
types: subQueryResult.columns.map((t) => t.type)
|
998
|
+
};
|
978
999
|
}
|
979
1000
|
const exprList = predicateOperations.exprList();
|
980
1001
|
if (exprList) {
|
@@ -1014,7 +1035,7 @@ function traversePredicateOperations(predicateOperations, parentType, traverseCo
|
|
1014
1035
|
});
|
1015
1036
|
return bitExprType;
|
1016
1037
|
}
|
1017
|
-
throw Error(
|
1038
|
+
throw Error(`Not supported: ${predicateOperations.constructor.name}`);
|
1018
1039
|
}
|
1019
1040
|
function traverseSimpleExpr(simpleExpr, traverseContext) {
|
1020
1041
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
|
@@ -1074,7 +1095,6 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1074
1095
|
// const typeFloat = freshVar('float', 'float');
|
1075
1096
|
// addNamedNode(simpleExpr, typeFloat, namedNodes)
|
1076
1097
|
// }
|
1077
|
-
;
|
1078
1098
|
}
|
1079
1099
|
const boolLiteral = literal.boolLiteral();
|
1080
1100
|
if (boolLiteral) {
|
@@ -1084,12 +1104,12 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1084
1104
|
if (nullLiteral) {
|
1085
1105
|
return (0, collect_constraints_1.freshVar)(nullLiteral.getText(), '?');
|
1086
1106
|
}
|
1087
|
-
throw Error(
|
1107
|
+
throw Error(`literal not supported:${literal.getText()}`);
|
1088
1108
|
//...
|
1089
1109
|
}
|
1090
1110
|
if (simpleExpr instanceof ts_mysql_parser_1.SimpleExprListContext) {
|
1091
1111
|
const exprList = simpleExpr.exprList();
|
1092
|
-
const listType = exprList.expr_list().map(item => {
|
1112
|
+
const listType = exprList.expr_list().map((item) => {
|
1093
1113
|
const exprType = traverseExpr(item, Object.assign(Object.assign({}, traverseContext), { where: false }));
|
1094
1114
|
return exprType;
|
1095
1115
|
});
|
@@ -1104,29 +1124,29 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1104
1124
|
const subqueryResult = traverseSubquery(subquery, traverseContext);
|
1105
1125
|
return {
|
1106
1126
|
kind: 'TypeOperator',
|
1107
|
-
types: subqueryResult.columns.map(t => (Object.assign(Object.assign({}, t.type), { table: '' })))
|
1127
|
+
types: subqueryResult.columns.map((t) => (Object.assign(Object.assign({}, t.type), { table: '' })))
|
1108
1128
|
};
|
1109
1129
|
}
|
1110
1130
|
if (simpleExpr instanceof ts_mysql_parser_1.SimpleExprCaseContext) {
|
1111
1131
|
//case when expr then expr else expr
|
1112
1132
|
const caseType = (0, collect_constraints_1.freshVar)(simpleExpr.getText(), '?');
|
1113
|
-
simpleExpr.whenExpression_list().forEach(whenExprCont => {
|
1133
|
+
simpleExpr.whenExpression_list().forEach((whenExprCont) => {
|
1114
1134
|
const whenExpr = whenExprCont.expr();
|
1115
1135
|
const whenType = traverseExpr(whenExpr, traverseContext);
|
1116
1136
|
traverseContext.constraints.push({
|
1117
1137
|
expression: whenExpr.getText(),
|
1118
|
-
type1: whenType.kind
|
1138
|
+
type1: whenType.kind === 'TypeOperator' ? whenType.types[0] : whenType,
|
1119
1139
|
type2: (0, collect_constraints_1.freshVar)('tinyint', 'tinyint') //bool
|
1120
1140
|
});
|
1121
1141
|
});
|
1122
|
-
const thenTypes = simpleExpr.thenExpression_list().map(thenExprCtx => {
|
1142
|
+
const thenTypes = simpleExpr.thenExpression_list().map((thenExprCtx) => {
|
1123
1143
|
const thenExpr = thenExprCtx.expr();
|
1124
1144
|
const thenType = traverseExpr(thenExpr, traverseContext);
|
1125
1145
|
traverseContext.constraints.push({
|
1126
1146
|
expression: thenExprCtx.getText(),
|
1127
1147
|
type1: caseType,
|
1128
|
-
type2: thenType.kind
|
1129
|
-
mostGeneralType: true
|
1148
|
+
type2: thenType.kind === 'TypeOperator' ? thenType.types[0] : thenType,
|
1149
|
+
mostGeneralType: true
|
1130
1150
|
});
|
1131
1151
|
return thenType;
|
1132
1152
|
});
|
@@ -1136,15 +1156,15 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1136
1156
|
traverseContext.constraints.push({
|
1137
1157
|
expression: (_c = simpleExpr.elseExpression()) === null || _c === void 0 ? void 0 : _c.getText(),
|
1138
1158
|
type1: caseType,
|
1139
|
-
type2: elseType.kind
|
1159
|
+
type2: elseType.kind === 'TypeOperator' ? elseType.types[0] : elseType,
|
1140
1160
|
mostGeneralType: true
|
1141
1161
|
});
|
1142
|
-
thenTypes.forEach(thenType => {
|
1162
|
+
thenTypes.forEach((thenType) => {
|
1143
1163
|
var _a;
|
1144
1164
|
traverseContext.constraints.push({
|
1145
1165
|
expression: (_a = simpleExpr.elseExpression()) === null || _a === void 0 ? void 0 : _a.getText(),
|
1146
1166
|
type1: thenType,
|
1147
|
-
type2: elseType.kind
|
1167
|
+
type2: elseType.kind === 'TypeOperator' ? elseType.types[0] : elseType,
|
1148
1168
|
mostGeneralType: true
|
1149
1169
|
});
|
1150
1170
|
});
|
@@ -1162,7 +1182,7 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1162
1182
|
type1: typeLeft,
|
1163
1183
|
type2: (0, collect_constraints_1.freshVar)('bigint', 'bigint')
|
1164
1184
|
});
|
1165
|
-
if (typeRight.kind
|
1185
|
+
if (typeRight.kind === 'TypeVar' && ((0, collect_constraints_1.isDateLiteral)(typeRight.name) || (0, collect_constraints_1.isDateTimeLiteral)(typeRight.name))) {
|
1166
1186
|
typeRight.type = 'datetime';
|
1167
1187
|
}
|
1168
1188
|
traverseContext.constraints.push({
|
@@ -1208,7 +1228,7 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1208
1228
|
mostGeneralType: true,
|
1209
1229
|
coercionType: 'SumFunction'
|
1210
1230
|
});
|
1211
|
-
if (inSumExprType.kind
|
1231
|
+
if (inSumExprType.kind === 'TypeVar') {
|
1212
1232
|
functionType.table = inSumExprType.table;
|
1213
1233
|
}
|
1214
1234
|
}
|
@@ -1217,7 +1237,7 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1217
1237
|
if (sumExpr.GROUP_CONCAT_SYMBOL()) {
|
1218
1238
|
const exprList = sumExpr.exprList();
|
1219
1239
|
if (exprList) {
|
1220
|
-
exprList.expr_list().map(item => {
|
1240
|
+
exprList.expr_list().map((item) => {
|
1221
1241
|
const exprType = traverseExpr(item, traverseContext);
|
1222
1242
|
return exprType;
|
1223
1243
|
});
|
@@ -1227,10 +1247,9 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1227
1247
|
*/
|
1228
1248
|
//TODO - Infer TEXT/BLOB or VARCHAR/VARBINARY
|
1229
1249
|
return (0, collect_constraints_1.freshVar)(sumExpr.getText(), 'varchar');
|
1230
|
-
;
|
1231
1250
|
}
|
1232
1251
|
}
|
1233
|
-
throw Error(
|
1252
|
+
throw Error(`Expression not supported: ${sumExpr.constructor.name}`);
|
1234
1253
|
}
|
1235
1254
|
if (simpleExpr instanceof ts_mysql_parser_1.SimpleExprRuntimeFunctionContext) {
|
1236
1255
|
const runtimeFunctionCall = simpleExpr.runtimeFunctionCall();
|
@@ -1245,7 +1264,7 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1245
1264
|
}
|
1246
1265
|
if (runtimeFunctionCall.REPLACE_SYMBOL()) {
|
1247
1266
|
const exprList = runtimeFunctionCall.expr_list();
|
1248
|
-
exprList.forEach(expr => {
|
1267
|
+
exprList.forEach((expr) => {
|
1249
1268
|
const exprType = traverseExpr(expr, traverseContext);
|
1250
1269
|
traverseContext.constraints.push({
|
1251
1270
|
expression: expr.getText(),
|
@@ -1259,10 +1278,10 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1259
1278
|
const expr = (_g = runtimeFunctionCall.exprWithParentheses()) === null || _g === void 0 ? void 0 : _g.expr();
|
1260
1279
|
if (expr) {
|
1261
1280
|
const paramType = traverseExpr(expr, traverseContext);
|
1262
|
-
if (paramType.kind
|
1281
|
+
if (paramType.kind === 'TypeVar' && (0, collect_constraints_1.isDateTimeLiteral)(paramType.name)) {
|
1263
1282
|
paramType.type = 'datetime';
|
1264
1283
|
}
|
1265
|
-
if (paramType.kind
|
1284
|
+
if (paramType.kind === 'TypeVar' && (0, collect_constraints_1.isDateLiteral)(paramType.name)) {
|
1266
1285
|
paramType.type = 'date';
|
1267
1286
|
}
|
1268
1287
|
traverseContext.constraints.push({
|
@@ -1278,10 +1297,10 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1278
1297
|
const expr = (_h = runtimeFunctionCall.exprWithParentheses()) === null || _h === void 0 ? void 0 : _h.expr();
|
1279
1298
|
if (expr) {
|
1280
1299
|
const paramType = traverseExpr(expr, traverseContext);
|
1281
|
-
if (paramType.kind
|
1300
|
+
if (paramType.kind === 'TypeVar' && (0, collect_constraints_1.isDateTimeLiteral)(paramType.name)) {
|
1282
1301
|
paramType.type = 'datetime';
|
1283
1302
|
}
|
1284
|
-
if (paramType.kind
|
1303
|
+
if (paramType.kind === 'TypeVar' && (0, collect_constraints_1.isDateLiteral)(paramType.name)) {
|
1285
1304
|
paramType.type = 'date';
|
1286
1305
|
}
|
1287
1306
|
traverseContext.constraints.push({
|
@@ -1296,13 +1315,13 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1296
1315
|
const expr = (_j = runtimeFunctionCall.exprWithParentheses()) === null || _j === void 0 ? void 0 : _j.expr();
|
1297
1316
|
if (expr) {
|
1298
1317
|
const paramType = traverseExpr(expr, traverseContext);
|
1299
|
-
if (paramType.kind
|
1318
|
+
if (paramType.kind === 'TypeVar' && (0, collect_constraints_1.isTimeLiteral)(paramType.name)) {
|
1300
1319
|
paramType.type = 'time';
|
1301
1320
|
}
|
1302
|
-
if (paramType.kind
|
1321
|
+
if (paramType.kind === 'TypeVar' && (0, collect_constraints_1.isDateLiteral)(paramType.name)) {
|
1303
1322
|
paramType.type = 'date';
|
1304
1323
|
}
|
1305
|
-
if (paramType.kind
|
1324
|
+
if (paramType.kind === 'TypeVar' && (0, collect_constraints_1.isDateTimeLiteral)(paramType.name)) {
|
1306
1325
|
paramType.type = 'datetime';
|
1307
1326
|
}
|
1308
1327
|
traverseContext.constraints.push({
|
@@ -1319,7 +1338,7 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1319
1338
|
const trimFunction = runtimeFunctionCall.trimFunction();
|
1320
1339
|
if (trimFunction) {
|
1321
1340
|
const exprList = trimFunction.expr_list();
|
1322
|
-
if (exprList.length
|
1341
|
+
if (exprList.length === 1) {
|
1323
1342
|
const exprType = traverseExpr(exprList[0], traverseContext);
|
1324
1343
|
traverseContext.constraints.push({
|
1325
1344
|
expression: exprList[0].getText(),
|
@@ -1327,7 +1346,7 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1327
1346
|
type2: (0, collect_constraints_1.freshVar)('varchar', 'varchar')
|
1328
1347
|
});
|
1329
1348
|
}
|
1330
|
-
if (exprList.length
|
1349
|
+
if (exprList.length === 2) {
|
1331
1350
|
const exprType = traverseExpr(exprList[0], traverseContext);
|
1332
1351
|
const expr2Type = traverseExpr(exprList[1], traverseContext);
|
1333
1352
|
traverseContext.constraints.push({
|
@@ -1355,17 +1374,17 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1355
1374
|
traverseExprListParameters(exprList, params, traverseContext);
|
1356
1375
|
return varcharParam;
|
1357
1376
|
}
|
1358
|
-
if (runtimeFunctionCall.ADDDATE_SYMBOL()
|
1359
|
-
|
1360
|
-
|
1361
|
-
|
1377
|
+
if (runtimeFunctionCall.ADDDATE_SYMBOL() ||
|
1378
|
+
runtimeFunctionCall.DATE_ADD_SYMBOL() ||
|
1379
|
+
runtimeFunctionCall.SUBDATE_SYMBOL() ||
|
1380
|
+
runtimeFunctionCall.DATE_SUB_SYMBOL()) {
|
1362
1381
|
//SELECT ADDDATE('2008-01-02', INTERVAL 31 DAY)
|
1363
1382
|
//SELECT ADDDATE('2008-01-02', 31)
|
1364
1383
|
const expr1 = runtimeFunctionCall.expr(0);
|
1365
1384
|
const expr2 = runtimeFunctionCall.expr(1);
|
1366
1385
|
const typeExpr1 = traverseExpr(expr1, traverseContext);
|
1367
1386
|
const typeExpr2 = traverseExpr(expr2, traverseContext);
|
1368
|
-
if (typeExpr1.kind
|
1387
|
+
if (typeExpr1.kind === 'TypeVar' && ((0, collect_constraints_1.isDateLiteral)(typeExpr1.name) || (0, collect_constraints_1.isDateTimeLiteral)(typeExpr1.name))) {
|
1369
1388
|
typeExpr1.type = 'datetime';
|
1370
1389
|
}
|
1371
1390
|
traverseContext.constraints.push({
|
@@ -1391,7 +1410,7 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1391
1410
|
const paramsTypeList = traverseExprListParameters(exprList, params, traverseContext);
|
1392
1411
|
paramsTypeList.forEach((typeVar, paramIndex) => {
|
1393
1412
|
traverseContext.constraints.push({
|
1394
|
-
expression: runtimeFunctionCall.getText()
|
1413
|
+
expression: `${runtimeFunctionCall.getText()}_param${paramIndex + 1}`,
|
1395
1414
|
type1: paramType,
|
1396
1415
|
type2: typeVar,
|
1397
1416
|
mostGeneralType: true,
|
@@ -1451,7 +1470,7 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1451
1470
|
});
|
1452
1471
|
return expr2Type;
|
1453
1472
|
}
|
1454
|
-
throw Error(
|
1473
|
+
throw Error(`Function not supported: ${runtimeFunctionCall.getText()}`);
|
1455
1474
|
}
|
1456
1475
|
if (simpleExpr instanceof ts_mysql_parser_1.SimpleExprFunctionContext) {
|
1457
1476
|
const functionIdentifier = (0, collect_constraints_1.getFunctionName)(simpleExpr);
|
@@ -1530,7 +1549,7 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1530
1549
|
}
|
1531
1550
|
return (0, collect_constraints_1.freshVar)(simpleExpr.getText(), 'bigint');
|
1532
1551
|
}
|
1533
|
-
if (functionIdentifier === 'period_add' || functionIdentifier
|
1552
|
+
if (functionIdentifier === 'period_add' || functionIdentifier === 'period_diff') {
|
1534
1553
|
const udfExprList = (_m = simpleExpr.functionCall().udfExprList()) === null || _m === void 0 ? void 0 : _m.udfExpr_list();
|
1535
1554
|
if (udfExprList) {
|
1536
1555
|
udfExprList.forEach((inExpr) => {
|
@@ -1546,7 +1565,7 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1546
1565
|
}
|
1547
1566
|
return (0, collect_constraints_1.freshVar)(simpleExpr.getText(), 'bigint');
|
1548
1567
|
}
|
1549
|
-
if (functionIdentifier === 'lpad' || functionIdentifier
|
1568
|
+
if (functionIdentifier === 'lpad' || functionIdentifier === 'rpad') {
|
1550
1569
|
const varcharParam = (0, collect_constraints_1.freshVar)('varchar', 'varchar');
|
1551
1570
|
const intParam = (0, collect_constraints_1.freshVar)('int', 'int');
|
1552
1571
|
const params = {
|
@@ -1556,13 +1575,13 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1556
1575
|
walkFunctionParameters(simpleExpr, params, traverseContext);
|
1557
1576
|
return varcharParam;
|
1558
1577
|
}
|
1559
|
-
if (functionIdentifier === 'lower'
|
1560
|
-
|
1561
|
-
|
1562
|
-
|
1563
|
-
|
1564
|
-
|
1565
|
-
|
1578
|
+
if (functionIdentifier === 'lower' ||
|
1579
|
+
functionIdentifier === 'lcase' ||
|
1580
|
+
functionIdentifier === 'upper' ||
|
1581
|
+
functionIdentifier === 'ucase' ||
|
1582
|
+
functionIdentifier === 'trim' ||
|
1583
|
+
functionIdentifier === 'ltrim' ||
|
1584
|
+
functionIdentifier === 'rtrim') {
|
1566
1585
|
const varcharParam = (0, collect_constraints_1.freshVar)('varchar', 'varchar');
|
1567
1586
|
const params = {
|
1568
1587
|
kind: 'FixedLengthParams',
|
@@ -1571,7 +1590,7 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1571
1590
|
walkFunctionParameters(simpleExpr, params, traverseContext);
|
1572
1591
|
return varcharParam;
|
1573
1592
|
}
|
1574
|
-
if (functionIdentifier === 'length' || functionIdentifier
|
1593
|
+
if (functionIdentifier === 'length' || functionIdentifier === 'char_length') {
|
1575
1594
|
const varcharParam = (0, collect_constraints_1.freshVar)('varchar', 'varchar');
|
1576
1595
|
const params = {
|
1577
1596
|
kind: 'FixedLengthParams',
|
@@ -1583,7 +1602,7 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1583
1602
|
if (functionIdentifier === 'abs') {
|
1584
1603
|
const functionType = (0, collect_constraints_1.freshVar)('number', 'number');
|
1585
1604
|
const udfExprList = (_o = simpleExpr.functionCall().udfExprList()) === null || _o === void 0 ? void 0 : _o.udfExpr_list();
|
1586
|
-
udfExprList === null || udfExprList === void 0 ? void 0 : udfExprList.forEach(expr => {
|
1605
|
+
udfExprList === null || udfExprList === void 0 ? void 0 : udfExprList.forEach((expr) => {
|
1587
1606
|
const param1 = traverseExpr(expr.expr(), traverseContext);
|
1588
1607
|
traverseContext.constraints.push({
|
1589
1608
|
expression: simpleExpr.getText(),
|
@@ -1595,10 +1614,10 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1595
1614
|
});
|
1596
1615
|
return functionType;
|
1597
1616
|
}
|
1598
|
-
if (functionIdentifier
|
1617
|
+
if (functionIdentifier === 'ceiling' || functionIdentifier === 'ceil') {
|
1599
1618
|
const functionType = (0, collect_constraints_1.freshVar)('number', 'number');
|
1600
1619
|
const udfExprList = (_p = simpleExpr.functionCall().udfExprList()) === null || _p === void 0 ? void 0 : _p.udfExpr_list();
|
1601
|
-
udfExprList === null || udfExprList === void 0 ? void 0 : udfExprList.forEach(expr => {
|
1620
|
+
udfExprList === null || udfExprList === void 0 ? void 0 : udfExprList.forEach((expr) => {
|
1602
1621
|
const param1 = traverseExpr(expr.expr(), traverseContext);
|
1603
1622
|
traverseContext.constraints.push({
|
1604
1623
|
expression: simpleExpr.getText(),
|
@@ -1610,12 +1629,12 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1610
1629
|
});
|
1611
1630
|
return functionType;
|
1612
1631
|
}
|
1613
|
-
if (functionIdentifier
|
1632
|
+
if (functionIdentifier === 'timestampdiff') {
|
1614
1633
|
const udfExprList = (_q = simpleExpr.functionCall().udfExprList()) === null || _q === void 0 ? void 0 : _q.udfExpr_list();
|
1615
1634
|
if (udfExprList) {
|
1616
1635
|
const [first, ...rest] = udfExprList;
|
1617
1636
|
const unit = first.getText().trim().toLowerCase();
|
1618
|
-
rest.forEach((inExpr
|
1637
|
+
rest.forEach((inExpr) => {
|
1619
1638
|
const expr = inExpr.expr();
|
1620
1639
|
const exprType = traverseExpr(expr, traverseContext);
|
1621
1640
|
const newType = (0, collect_constraints_1.verifyDateTypesCoercion)(exprType);
|
@@ -1630,7 +1649,7 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1630
1649
|
}
|
1631
1650
|
return (0, collect_constraints_1.freshVar)('int', 'int');
|
1632
1651
|
}
|
1633
|
-
if (functionIdentifier
|
1652
|
+
if (functionIdentifier === 'ifnull' || functionIdentifier === 'nullif') {
|
1634
1653
|
const functionType = (0, collect_constraints_1.freshVar)(simpleExpr.getText(), '?');
|
1635
1654
|
const udfExprList = (_r = simpleExpr.functionCall().udfExprList()) === null || _r === void 0 ? void 0 : _r.udfExpr_list();
|
1636
1655
|
if (udfExprList) {
|
@@ -1650,9 +1669,10 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1650
1669
|
}
|
1651
1670
|
return functionType;
|
1652
1671
|
}
|
1653
|
-
if (functionIdentifier
|
1654
|
-
|
1655
|
-
|
1672
|
+
if (functionIdentifier === 'md5' || //md5(str) - TODO - have input constraint = string
|
1673
|
+
functionIdentifier === 'hex' || //md5(n or str)
|
1674
|
+
functionIdentifier === 'unhex') {
|
1675
|
+
//unhex (str) - TODO - have input constraint = string
|
1656
1676
|
const functionType = (0, collect_constraints_1.freshVar)(simpleExpr.getText(), 'char');
|
1657
1677
|
const udfExprList = (_s = simpleExpr.functionCall().udfExprList()) === null || _s === void 0 ? void 0 : _s.udfExpr_list();
|
1658
1678
|
if (udfExprList) {
|
@@ -1666,7 +1686,7 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1666
1686
|
}
|
1667
1687
|
return functionType;
|
1668
1688
|
}
|
1669
|
-
throw Error(
|
1689
|
+
throw Error(`Function not supported: ${functionIdentifier}`);
|
1670
1690
|
}
|
1671
1691
|
if (simpleExpr instanceof ts_mysql_parser_1.SimpleExprWindowingFunctionContext) {
|
1672
1692
|
const windowFunctionCall = simpleExpr.windowFunctionCall();
|
@@ -1678,14 +1698,14 @@ function traverseSimpleExpr(simpleExpr, traverseContext) {
|
|
1678
1698
|
return (0, collect_constraints_1.freshVar)(castType.getText(), 'char');
|
1679
1699
|
}
|
1680
1700
|
}
|
1681
|
-
throw Error(
|
1701
|
+
throw Error(`traverseSimpleExpr - not supported: ${simpleExpr.constructor.name}`);
|
1682
1702
|
}
|
1683
1703
|
function traverseWindowFunctionCall(windowFunctionCall, traverseContext) {
|
1684
|
-
if (windowFunctionCall.ROW_NUMBER_SYMBOL()
|
1685
|
-
|
1686
|
-
|
1687
|
-
|
1688
|
-
|
1704
|
+
if (windowFunctionCall.ROW_NUMBER_SYMBOL() ||
|
1705
|
+
windowFunctionCall.RANK_SYMBOL() ||
|
1706
|
+
windowFunctionCall.DENSE_RANK_SYMBOL() ||
|
1707
|
+
windowFunctionCall.CUME_DIST_SYMBOL() ||
|
1708
|
+
windowFunctionCall.PERCENT_RANK_SYMBOL()) {
|
1689
1709
|
return (0, collect_constraints_1.freshVar)(windowFunctionCall.getText(), 'bigint');
|
1690
1710
|
}
|
1691
1711
|
const expr = windowFunctionCall.expr();
|
@@ -1697,12 +1717,12 @@ function traverseWindowFunctionCall(windowFunctionCall, traverseContext) {
|
|
1697
1717
|
const expr = exprWithParentheses.expr();
|
1698
1718
|
return traverseExpr(expr, traverseContext);
|
1699
1719
|
}
|
1700
|
-
throw Error(
|
1720
|
+
throw Error(`No support for expression${windowFunctionCall.constructor.name}`);
|
1701
1721
|
}
|
1702
1722
|
function traverseExprListParameters(exprList, params, traverseContext) {
|
1703
1723
|
return exprList.map((expr, paramIndex) => {
|
1704
1724
|
const exprType = traverseExpr(expr, traverseContext);
|
1705
|
-
const paramType = params.kind
|
1725
|
+
const paramType = params.kind === 'FixedLengthParams' ? params.paramsType[paramIndex] : (0, collect_constraints_1.freshVar)(params.paramType, params.paramType);
|
1706
1726
|
traverseContext.constraints.push({
|
1707
1727
|
expression: expr.getText(),
|
1708
1728
|
type1: exprType,
|
@@ -1718,8 +1738,8 @@ function walkFunctionParameters(simpleExprFunction, params, traverseContext) {
|
|
1718
1738
|
const udfExprList = (_a = simpleExprFunction.functionCall().udfExprList()) === null || _a === void 0 ? void 0 : _a.udfExpr_list();
|
1719
1739
|
if (udfExprList) {
|
1720
1740
|
const paramTypes = udfExprList
|
1721
|
-
.filter((
|
1722
|
-
return functionName
|
1741
|
+
.filter((_, paramIndex) => {
|
1742
|
+
return functionName === 'timestampdiff' ? paramIndex !== 0 : true; //filter the first parameter of timestampdiff function
|
1723
1743
|
})
|
1724
1744
|
.map((inExpr, paramIndex) => {
|
1725
1745
|
const expr = inExpr.expr();
|
@@ -1727,7 +1747,7 @@ function walkFunctionParameters(simpleExprFunction, params, traverseContext) {
|
|
1727
1747
|
traverseContext.constraints.push({
|
1728
1748
|
expression: expr.getText(),
|
1729
1749
|
type1: exprType,
|
1730
|
-
type2: params.kind
|
1750
|
+
type2: params.kind === 'FixedLengthParams' ? params.paramsType[paramIndex] : (0, collect_constraints_1.freshVar)(params.paramType, params.paramType)
|
1731
1751
|
});
|
1732
1752
|
return exprType;
|
1733
1753
|
});
|
@@ -1739,7 +1759,7 @@ function walkFunctionParameters(simpleExprFunction, params, traverseContext) {
|
|
1739
1759
|
const inSumExprType = traverseExpr(inExpr, traverseContext);
|
1740
1760
|
traverseContext.constraints.push({
|
1741
1761
|
expression: inExpr.getText(),
|
1742
|
-
type1: params.kind
|
1762
|
+
type1: params.kind === 'FixedLengthParams' ? params.paramsType[paramIndex] : (0, collect_constraints_1.freshVar)(params.paramType, params.paramType),
|
1743
1763
|
type2: inSumExprType,
|
1744
1764
|
mostGeneralType: true
|
1745
1765
|
});
|
@@ -1750,32 +1770,36 @@ function walkFunctionParameters(simpleExprFunction, params, traverseContext) {
|
|
1750
1770
|
throw Error('Error in walkFunctionParameters');
|
1751
1771
|
}
|
1752
1772
|
function filterColumns(dbSchema, withSchema, tableAlias, table) {
|
1753
|
-
const withResult = withSchema
|
1773
|
+
const withResult = withSchema
|
1774
|
+
.filter((t) => t.table.toLowerCase() === table.name.toLowerCase())
|
1775
|
+
.map((s) => (Object.assign(Object.assign({}, s), { tableAlias: tableAlias })));
|
1754
1776
|
const tableColumns1 = dbSchema
|
1755
|
-
.filter(schema => schema.table.toLowerCase()
|
1756
|
-
.map(tableColumn => {
|
1777
|
+
.filter((schema) => schema.table.toLowerCase() === table.name.toLowerCase() && (schema.schema === table.prefix || table.prefix === ''))
|
1778
|
+
.map((tableColumn) => {
|
1757
1779
|
//name and colum are the same on the leaf table
|
1758
1780
|
const r = {
|
1759
|
-
columnName: tableColumn.column,
|
1760
|
-
|
1781
|
+
columnName: tableColumn.column,
|
1782
|
+
columnType: (0, collect_constraints_1.createColumnTypeFomColumnSchema)(tableColumn),
|
1783
|
+
notNull: tableColumn.notNull,
|
1784
|
+
table: table.name,
|
1785
|
+
tableAlias: tableAlias || '',
|
1786
|
+
columnKey: tableColumn.columnKey
|
1761
1787
|
};
|
1762
1788
|
return r;
|
1763
1789
|
});
|
1764
1790
|
const result = tableColumns1.concat(withResult);
|
1765
1791
|
return result;
|
1766
1792
|
}
|
1767
|
-
exports.filterColumns = filterColumns;
|
1768
1793
|
function selectAllColumns(tablePrefix, fromColumns) {
|
1769
|
-
return fromColumns.filter(column => {
|
1770
|
-
if (tablePrefix
|
1794
|
+
return fromColumns.filter((column) => {
|
1795
|
+
if (tablePrefix === '' || tablePrefix === column.tableAlias || tablePrefix === column.table) {
|
1771
1796
|
return true;
|
1772
1797
|
}
|
1773
1798
|
return false;
|
1774
1799
|
});
|
1775
1800
|
}
|
1776
|
-
exports.selectAllColumns = selectAllColumns;
|
1777
1801
|
function filterUsingFields(joinedFields, usingFields) {
|
1778
|
-
return joinedFields.filter(joinedField => {
|
1802
|
+
return joinedFields.filter((joinedField) => {
|
1779
1803
|
const isUsing = usingFields.includes(joinedField.columnName);
|
1780
1804
|
if (!isUsing) {
|
1781
1805
|
return true;
|
@@ -1789,12 +1813,13 @@ function isMultipleRowResult(selectStatement, fromColumns) {
|
|
1789
1813
|
return false;
|
1790
1814
|
}
|
1791
1815
|
const querySpecs = (0, parse_1.getAllQuerySpecificationsFromSelectStatement)(selectStatement);
|
1792
|
-
if (querySpecs.length
|
1816
|
+
if (querySpecs.length === 1) {
|
1817
|
+
//UNION queries are multipleRowsResult = true
|
1793
1818
|
const fromClause = querySpecs[0].fromClause();
|
1794
1819
|
if (!fromClause) {
|
1795
1820
|
return false;
|
1796
1821
|
}
|
1797
|
-
if (querySpecs[0].selectItemList().getChildCount()
|
1822
|
+
if (querySpecs[0].selectItemList().getChildCount() === 1) {
|
1798
1823
|
const selectItem = querySpecs[0].selectItemList().getChild(0);
|
1799
1824
|
//if selectItem = * (TerminalNode) childCount = 0; selectItem.expr() throws exception
|
1800
1825
|
const expr = selectItem.getChildCount() > 0 ? selectItem.expr() : null;
|
@@ -1812,19 +1837,18 @@ function isMultipleRowResult(selectStatement, fromColumns) {
|
|
1812
1837
|
}
|
1813
1838
|
const whereClauseExpr = (_b = querySpecs[0].whereClause()) === null || _b === void 0 ? void 0 : _b.expr();
|
1814
1839
|
const isMultipleRowResult = whereClauseExpr && verifyMultipleResult2(whereClauseExpr, fromColumns);
|
1815
|
-
if (isMultipleRowResult
|
1840
|
+
if (isMultipleRowResult === false) {
|
1816
1841
|
return false;
|
1817
1842
|
}
|
1818
1843
|
}
|
1819
1844
|
return true;
|
1820
1845
|
}
|
1821
|
-
exports.isMultipleRowResult = isMultipleRowResult;
|
1822
1846
|
function isLimitOne(selectStatement) {
|
1823
1847
|
const limitOptions = (0, parse_1.getLimitOptions)(selectStatement);
|
1824
|
-
if (limitOptions.length
|
1848
|
+
if (limitOptions.length === 1 && limitOptions[0].getText() === '1') {
|
1825
1849
|
return true;
|
1826
1850
|
}
|
1827
|
-
if (limitOptions.length
|
1851
|
+
if (limitOptions.length === 2 && limitOptions[1].getText() === '1') {
|
1828
1852
|
return true;
|
1829
1853
|
}
|
1830
1854
|
return false;
|
@@ -1834,8 +1858,8 @@ function verifyMultipleResult2(exprContext, fromColumns) {
|
|
1834
1858
|
const boolPri = exprContext.boolPri();
|
1835
1859
|
if (boolPri instanceof ts_mysql_parser_1.PrimaryExprCompareContext) {
|
1836
1860
|
if (boolPri.compOp().EQUAL_OPERATOR()) {
|
1837
|
-
|
1838
|
-
|
1861
|
+
const compareLeft = boolPri.boolPri();
|
1862
|
+
const compareRight = boolPri.predicate();
|
1839
1863
|
if (isUniqueKeyComparation(compareLeft, fromColumns) || isUniqueKeyComparation(compareRight, fromColumns)) {
|
1840
1864
|
return false; //multipleRow = false
|
1841
1865
|
}
|
@@ -1848,8 +1872,8 @@ function verifyMultipleResult2(exprContext, fromColumns) {
|
|
1848
1872
|
return true;
|
1849
1873
|
}
|
1850
1874
|
if (exprContext instanceof ts_mysql_parser_1.ExprAndContext) {
|
1851
|
-
const oneIsSingleResult = exprContext.expr_list().some(expr => verifyMultipleResult2(expr, fromColumns)
|
1852
|
-
return oneIsSingleResult
|
1875
|
+
const oneIsSingleResult = exprContext.expr_list().some((expr) => verifyMultipleResult2(expr, fromColumns) === false);
|
1876
|
+
return oneIsSingleResult === false;
|
1853
1877
|
}
|
1854
1878
|
// if (exprContext instanceof ExprXorContext) {
|
1855
1879
|
// const expressions = exprContext.expr();
|
@@ -1857,15 +1881,15 @@ function verifyMultipleResult2(exprContext, fromColumns) {
|
|
1857
1881
|
if (exprContext instanceof ts_mysql_parser_1.ExprOrContext) {
|
1858
1882
|
return true; //multipleRow = true
|
1859
1883
|
}
|
1860
|
-
throw Error(
|
1884
|
+
throw Error(`Unknow type:${exprContext.constructor.name}`);
|
1861
1885
|
}
|
1862
|
-
exports.verifyMultipleResult2 = verifyMultipleResult2;
|
1863
1886
|
function isUniqueKeyComparation(compare, fromColumns) {
|
1864
1887
|
const tokens = (0, select_columns_1.getSimpleExpressions)(compare);
|
1865
|
-
if (tokens.length
|
1888
|
+
if (tokens.length === 1 && tokens[0] instanceof ts_mysql_parser_1.SimpleExprColumnRefContext) {
|
1866
1889
|
const fieldName = (0, select_columns_1.splitName)(tokens[0].getText());
|
1867
1890
|
const col = (0, select_columns_1.findColumn)(fieldName, fromColumns);
|
1868
|
-
if (col.columnKey
|
1891
|
+
if (col.columnKey === 'PRI' || col.columnKey === 'UNI') {
|
1892
|
+
//TODO - UNIQUE
|
1869
1893
|
return true; //isUniqueKeyComparation = true
|
1870
1894
|
}
|
1871
1895
|
}
|
@@ -1876,7 +1900,7 @@ function getOrderByColumns(fromColumns, selectColumns) {
|
|
1876
1900
|
fromColumns.forEach((col) => {
|
1877
1901
|
const ambiguous = isAmbiguous(fromColumns, col.columnName);
|
1878
1902
|
if (!ambiguous) {
|
1879
|
-
const exists = orderByColumns.find(orderBy => orderBy
|
1903
|
+
const exists = orderByColumns.find((orderBy) => orderBy === col.columnName);
|
1880
1904
|
if (!exists) {
|
1881
1905
|
orderByColumns.push(col.columnName);
|
1882
1906
|
}
|
@@ -1888,10 +1912,10 @@ function getOrderByColumns(fromColumns, selectColumns) {
|
|
1888
1912
|
orderByColumns.push(`${col.table}.${col.columnName}`);
|
1889
1913
|
}
|
1890
1914
|
});
|
1891
|
-
selectColumns.forEach(col => {
|
1892
|
-
const duplicated = selectColumns.filter(orderBy => orderBy.name
|
1915
|
+
selectColumns.forEach((col) => {
|
1916
|
+
const duplicated = selectColumns.filter((orderBy) => orderBy.name === col.name);
|
1893
1917
|
if (duplicated.length <= 1) {
|
1894
|
-
const exists = orderByColumns.find(orderBy => orderBy
|
1918
|
+
const exists = orderByColumns.find((orderBy) => orderBy === col.name);
|
1895
1919
|
if (!exists) {
|
1896
1920
|
orderByColumns.push(col.name);
|
1897
1921
|
}
|
@@ -1899,9 +1923,8 @@ function getOrderByColumns(fromColumns, selectColumns) {
|
|
1899
1923
|
});
|
1900
1924
|
return orderByColumns;
|
1901
1925
|
}
|
1902
|
-
exports.getOrderByColumns = getOrderByColumns;
|
1903
1926
|
function isAmbiguous(columns, columnName) {
|
1904
|
-
const filterByName = columns.filter(col => col.columnName
|
1927
|
+
const filterByName = columns.filter((col) => col.columnName === columnName);
|
1905
1928
|
return filterByName.length > 1;
|
1906
1929
|
}
|
1907
1930
|
//# sourceMappingURL=traverse.js.map
|