typesql-cli 0.5.18 → 0.6.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.
Files changed (41) hide show
  1. package/code-generator.js +2 -2
  2. package/code-generator.js.map +1 -1
  3. package/describe-query.d.ts +1 -1
  4. package/describe-query.d.ts.map +1 -1
  5. package/describe-query.js +2 -3
  6. package/describe-query.js.map +1 -1
  7. package/mysql-query-analyzer/collect-constraints.d.ts +22 -49
  8. package/mysql-query-analyzer/collect-constraints.d.ts.map +1 -1
  9. package/mysql-query-analyzer/collect-constraints.js +39 -1757
  10. package/mysql-query-analyzer/collect-constraints.js.map +1 -1
  11. package/mysql-query-analyzer/infer-column-nullability.d.ts +3 -3
  12. package/mysql-query-analyzer/infer-column-nullability.d.ts.map +1 -1
  13. package/mysql-query-analyzer/infer-column-nullability.js +8 -25
  14. package/mysql-query-analyzer/infer-column-nullability.js.map +1 -1
  15. package/mysql-query-analyzer/infer-param-nullability.d.ts +3 -1
  16. package/mysql-query-analyzer/infer-param-nullability.d.ts.map +1 -1
  17. package/mysql-query-analyzer/infer-param-nullability.js +21 -3
  18. package/mysql-query-analyzer/infer-param-nullability.js.map +1 -1
  19. package/mysql-query-analyzer/parse.d.ts +10 -10
  20. package/mysql-query-analyzer/parse.d.ts.map +1 -1
  21. package/mysql-query-analyzer/parse.js +126 -184
  22. package/mysql-query-analyzer/parse.js.map +1 -1
  23. package/mysql-query-analyzer/select-columns.d.ts +8 -9
  24. package/mysql-query-analyzer/select-columns.d.ts.map +1 -1
  25. package/mysql-query-analyzer/select-columns.js +26 -226
  26. package/mysql-query-analyzer/select-columns.js.map +1 -1
  27. package/mysql-query-analyzer/traverse.d.ts +46 -0
  28. package/mysql-query-analyzer/traverse.d.ts.map +1 -0
  29. package/mysql-query-analyzer/traverse.js +1605 -0
  30. package/mysql-query-analyzer/traverse.js.map +1 -0
  31. package/mysql-query-analyzer/types.d.ts +34 -5
  32. package/mysql-query-analyzer/types.d.ts.map +1 -1
  33. package/mysql-query-analyzer/unify.d.ts +4 -3
  34. package/mysql-query-analyzer/unify.d.ts.map +1 -1
  35. package/mysql-query-analyzer/unify.js +540 -48
  36. package/mysql-query-analyzer/unify.js.map +1 -1
  37. package/mysql-query-analyzer/verify-multiple-result.js +1 -1
  38. package/mysql-query-analyzer/verify-multiple-result.js.map +1 -1
  39. package/package.json +1 -1
  40. package/types.d.ts +2 -2
  41. package/types.d.ts.map +1 -1
@@ -3,20 +3,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.walkSubquery = exports.walkQuerySpecification = exports.generateTypeInfo = exports.analiseQuerySpecification = exports.unionTypeResult = exports.analiseSelectStatement = exports.getDeleteColumns = exports.getUpdateColumns = exports.getInsertColumns = exports.analiseUpdateStatement = exports.analiseDeleteStatement = exports.analiseInsertStatement = exports.analiseTree = exports.freshVar = void 0;
6
+ exports.getFunctionName = exports.isDateLiteral = exports.isDateTimeLiteral = exports.isTimeLiteral = exports.verifyDateTypesCoercion = exports.getVarType = exports.generateTypeInfo = exports.getDeleteColumns = exports.getInsertColumns = exports.getInsertIntoTable = exports.createColumnType = exports.freshVar = void 0;
7
7
  const moment_1 = __importDefault(require("moment"));
8
- const ts_mysql_parser_1 = require("ts-mysql-parser");
9
8
  const select_columns_1 = require("./select-columns");
10
- const parse_1 = require("./parse");
11
9
  const unify_1 = require("./unify");
12
- const infer_param_nullability_1 = require("./infer-param-nullability");
13
- const describe_query_1 = require("../describe-query");
14
- const util_1 = require("./util");
15
10
  let counter = 0;
16
11
  function freshVar(name, typeVar, selectItem, list) {
17
12
  const param = {
18
13
  kind: 'TypeVar',
19
- id: ++counter,
14
+ id: (++counter).toString(),
20
15
  name,
21
16
  type: typeVar
22
17
  };
@@ -29,285 +24,22 @@ function freshVar(name, typeVar, selectItem, list) {
29
24
  return param;
30
25
  }
31
26
  exports.freshVar = freshVar;
32
- function analiseTree(tree, context, namedParameters) {
33
- var _a, _b, _c;
34
- if (tree instanceof ts_mysql_parser_1.QueryContext) {
35
- const selectStatement = (_a = tree.simpleStatement()) === null || _a === void 0 ? void 0 : _a.selectStatement();
36
- if (selectStatement) {
37
- return analiseSelectStatement(selectStatement, context.dbSchema, context.withSchema, namedParameters);
38
- }
39
- const insertStatement = (_b = tree.simpleStatement()) === null || _b === void 0 ? void 0 : _b.insertStatement();
40
- if (insertStatement) {
41
- const insertStmt = analiseInsertStatement(insertStatement, context);
42
- const TypeInfer = {
43
- columns: [],
44
- parameters: insertStmt.parameters.map(param => param.columnType)
45
- };
46
- return TypeInfer;
47
- }
48
- const updateStatement = (_c = tree.simpleStatement()) === null || _c === void 0 ? void 0 : _c.updateStatement();
49
- if (updateStatement) {
50
- const updateStmt = analiseUpdateStatement(updateStatement, context.dbSchema, context.withSchema);
51
- const TypeInfer = {
52
- columns: [],
53
- parameters: updateStmt.data.map(param => param.columnType)
54
- };
55
- return TypeInfer;
56
- }
57
- }
58
- throw Error('invalid type of tree');
59
- }
60
- exports.analiseTree = analiseTree;
61
- function analiseInsertStatement(insertStatement, context) {
62
- var _a, _b;
63
- const insertColumns = getInsertColumns(insertStatement, context.dbSchema);
64
- const allParameters = [];
65
- const paramsNullability = {};
66
- const exprOrDefaultList = [];
67
- const valuesContext = (_a = insertStatement.insertFromConstructor()) === null || _a === void 0 ? void 0 : _a.insertValues().valueList().values()[0];
68
- if (valuesContext) {
69
- valuesContext.DEFAULT_SYMBOL().forEach(terminalNode => {
70
- exprOrDefaultList.push(terminalNode);
71
- });
72
- valuesContext.expr().forEach(expr => {
73
- exprOrDefaultList.push(expr);
74
- });
75
- }
76
- //order the tokens based on sql position
77
- exprOrDefaultList.sort((token1, token2) => token1.sourceInterval.a - token2.sourceInterval.a);
78
- const insertIntoTable = getInsertIntoTable(insertStatement);
79
- const fromColumns = context.dbSchema
80
- .filter(c => c.table == insertIntoTable)
81
- .map(c => {
82
- const col = {
83
- table: c.table,
84
- column: c.column,
85
- columnName: c.column,
86
- columnType: c.column_type,
87
- columnKey: "",
88
- notNull: c.notNull
89
- };
90
- return col;
91
- });
92
- context.fromColumns = fromColumns;
93
- exprOrDefaultList.forEach((expr, index) => {
94
- const column = insertColumns[index];
95
- if (expr instanceof ts_mysql_parser_1.ExprContext) {
96
- const numberParamsBefore = context.parameters.length;
97
- const exprType = walkExpr(context, expr);
98
- const paramNullabilityExpr = (0, infer_param_nullability_1.inferParamNullability)(expr);
99
- context.parameters.slice(numberParamsBefore).forEach(param => {
100
- paramsNullability[param.id] = paramNullabilityExpr.every(n => n) && column.notNull;
101
- });
102
- context.constraints.push({
103
- expression: expr.text,
104
- //TODO - CHANGING ORDER SHOULDN'T AFFECT THE TYPE INFERENCE
105
- type1: exprType.kind == 'TypeOperator' ? exprType.types[0] : exprType,
106
- type2: freshVar(column.column, column.column_type)
107
- });
108
- }
109
- });
110
- const updateList = ((_b = insertStatement.insertUpdateList()) === null || _b === void 0 ? void 0 : _b.updateList().updateElement()) || [];
111
- updateList.forEach(updateElement => {
112
- const columnName = updateElement.columnRef().text;
113
- const field = (0, select_columns_1.splitName)(columnName);
114
- const expr = updateElement.expr();
115
- if (expr) {
116
- const numberParamsBefore = context.parameters.length;
117
- const exprType = walkExpr(context, expr);
118
- const column = (0, select_columns_1.findColumn2)(field, insertIntoTable, context.dbSchema);
119
- context.parameters.slice(numberParamsBefore).forEach(param => {
120
- paramsNullability[param.id] = column.notNull;
121
- });
122
- context.constraints.push({
123
- expression: expr.text,
124
- type1: exprType,
125
- type2: freshVar(column.column, column.column_type)
126
- });
127
- }
128
- });
129
- const insertQueryExpression = insertStatement.insertQueryExpression();
130
- if (insertQueryExpression) {
131
- const numberParamsBefore = context.parameters.length;
132
- const exprTypes = walkInsertQueryExpression(insertQueryExpression, context);
133
- exprTypes.types.forEach((type, index) => {
134
- const column = insertColumns[index];
135
- if (type.kind == 'TypeVar') {
136
- paramsNullability[type.id] = column.notNull;
137
- }
138
- context.constraints.push({
139
- expression: insertQueryExpression.text,
140
- type1: type,
141
- type2: freshVar(column.column, column.column_type)
142
- });
143
- });
144
- const paramNullabilityExpr = (0, infer_param_nullability_1.inferParamNullabilityQuery)(insertQueryExpression);
145
- context.parameters.slice(numberParamsBefore).forEach((param, index) => {
146
- if (paramsNullability[param.id] == null) {
147
- paramsNullability[param.id] = paramNullabilityExpr[index];
148
- }
149
- });
150
- }
151
- const typeInfo = generateTypeInfo(context.parameters, context.constraints);
152
- typeInfo.forEach((param, index) => {
153
- const paramId = context.parameters[index].id;
154
- allParameters.push({
155
- name: 'param' + (allParameters.length + 1),
156
- columnType: (0, describe_query_1.verifyNotInferred)(param),
157
- notNull: paramsNullability[paramId]
158
- });
159
- });
160
- const typeInferenceResult = {
161
- kind: 'Insert',
162
- parameters: allParameters
163
- };
164
- return typeInferenceResult;
165
- }
166
- exports.analiseInsertStatement = analiseInsertStatement;
167
- function walkInsertQueryExpression(insertQueryExpression, context) {
168
- const queryExpressionOrParens = insertQueryExpression.queryExpressionOrParens();
169
- return walkQueryExpressionOrParens(queryExpressionOrParens, context);
170
- }
171
- function walkQueryExpressionOrParens(queryExpressionOrParens, context) {
172
- const queryExpression = queryExpressionOrParens.queryExpression();
173
- if (queryExpression) {
174
- return walkQueryExpression(queryExpression, context);
175
- }
176
- const queryEpressionParens = queryExpressionOrParens.queryExpressionParens();
177
- if (queryEpressionParens) {
178
- return walkQueryExpressionParens(queryEpressionParens);
179
- }
180
- throw Error("walkQueryExpressionOrParens");
181
- }
182
- function walkQueryExpression(queryExpression, context) {
183
- const queryExpressionBody = queryExpression.queryExpressionBody();
184
- if (queryExpressionBody) {
185
- return walkQueryExpressionBody(queryExpressionBody, context);
186
- }
187
- const queryExpressionParens = queryExpression.queryExpressionParens();
188
- if (queryExpressionParens) {
189
- return walkQueryExpressionParens(queryExpressionParens);
190
- }
191
- throw Error("walkQueryExpression");
192
- }
193
- function walkQueryExpressionParens(queryExpressionParens) {
194
- throw Error("walkQueryExpressionParens not implemented:" + queryExpressionParens.text);
195
- }
196
- function walkQueryExpressionBody(queryExpressionBody, context) {
197
- const childQueryExpressionBody = queryExpressionBody.queryExpressionBody();
198
- if (childQueryExpressionBody) {
199
- return walkQueryExpressionBody(childQueryExpressionBody, context);
200
- }
201
- const queryExpressionParensList = queryExpressionBody.queryExpressionParens();
202
- queryExpressionParensList.forEach(queryExpressionParens => {
203
- return walkQueryExpressionParens(queryExpressionParens);
204
- });
205
- const querySpecification = queryExpressionBody.querySpecification();
206
- if (querySpecification) {
207
- return walkQuerySpecification(context, querySpecification);
208
- }
209
- throw Error("walkQueryExpressionBody");
210
- }
211
- function analiseDeleteStatement(deleteStatement, dbSchema, withSchema) {
212
- var _a;
213
- const whereExpr = (_a = deleteStatement.whereClause()) === null || _a === void 0 ? void 0 : _a.expr();
214
- const deleteColumns = getDeleteColumns(deleteStatement, dbSchema);
215
- const allParameters = [];
216
- if (whereExpr) {
217
- const context = {
218
- fromColumns: deleteColumns,
219
- parameters: [],
220
- constraints: [],
221
- dbSchema,
222
- withSchema
223
- };
224
- walkExpr(context, whereExpr);
225
- const typeInfo = generateTypeInfo(context.parameters, context.constraints);
226
- const paramNullability = (0, infer_param_nullability_1.inferParamNullability)(whereExpr);
227
- typeInfo.forEach((param, paramIndex) => {
228
- allParameters.push({
229
- name: 'param' + (allParameters.length + 1),
230
- columnType: (0, describe_query_1.verifyNotInferred)(param),
231
- notNull: paramNullability[paramIndex]
232
- });
233
- });
234
- }
235
- const typeInferenceResult = {
236
- kind: 'Delete',
237
- parameters: allParameters
238
- };
239
- return typeInferenceResult;
240
- }
241
- exports.analiseDeleteStatement = analiseDeleteStatement;
242
- function analiseUpdateStatement(updateStatement, dbSchema, withSchema) {
243
- var _a;
244
- const updateElement = updateStatement.updateList().updateElement();
245
- const updateColumns = getUpdateColumns(updateStatement, dbSchema);
246
- const dataParameters = [];
247
- const whereParameters = [];
248
- updateElement.forEach(updateElement => {
249
- const expr = updateElement.expr();
250
- if (expr) {
251
- const context = {
252
- dbSchema,
253
- withSchema,
254
- parameters: [],
255
- constraints: [],
256
- fromColumns: updateColumns
257
- };
258
- const result = walkExpr(context, expr);
259
- const columnName = updateElement.columnRef().text;
260
- const field = (0, select_columns_1.splitName)(columnName);
261
- const column = (0, select_columns_1.findColumn)(field, updateColumns);
262
- context.constraints.push({
263
- expression: updateStatement.text,
264
- type1: result,
265
- type2: freshVar(column.table, column.columnType)
266
- });
267
- const typeInfo = generateTypeInfo(context.parameters, context.constraints);
268
- const paramNullabilityExpr = (0, infer_param_nullability_1.inferParamNullability)(expr);
269
- typeInfo.forEach(param => {
270
- dataParameters.push({
271
- name: column.columnName,
272
- columnType: (0, describe_query_1.verifyNotInferred)(param),
273
- notNull: paramNullabilityExpr.every(n => n) && column.notNull
274
- });
275
- });
276
- }
277
- });
278
- const whereExpr = (_a = updateStatement.whereClause()) === null || _a === void 0 ? void 0 : _a.expr();
279
- if (whereExpr) {
280
- const context = {
281
- dbSchema,
282
- withSchema,
283
- parameters: [],
284
- constraints: [],
285
- fromColumns: updateColumns
286
- };
287
- walkExpr(context, whereExpr);
288
- const typeInfo = generateTypeInfo(context.parameters, context.constraints);
289
- const paramNullability = (0, infer_param_nullability_1.inferParamNullability)(whereExpr);
290
- typeInfo.forEach((param, paramIndex) => {
291
- whereParameters.push({
292
- name: 'param' + (whereParameters.length + 1),
293
- columnType: (0, describe_query_1.verifyNotInferred)(param),
294
- notNull: paramNullability[paramIndex]
295
- });
296
- });
297
- }
298
- const typeInferenceResult = {
299
- kind: 'Update',
300
- data: dataParameters,
301
- parameters: whereParameters
27
+ function createColumnType(col) {
28
+ const columnType = {
29
+ kind: 'TypeVar',
30
+ id: col.columnType.id,
31
+ name: col.columnName,
32
+ type: col.columnType.type
302
33
  };
303
- return typeInferenceResult;
34
+ return columnType;
304
35
  }
305
- exports.analiseUpdateStatement = analiseUpdateStatement;
36
+ exports.createColumnType = createColumnType;
306
37
  function getInsertIntoTable(insertStatement) {
307
38
  const insertIntoTable = (0, select_columns_1.splitName)(insertStatement.tableRef().text).name;
308
39
  return insertIntoTable;
309
40
  }
310
- function getInsertColumns(insertStatement, dbSchema) {
41
+ exports.getInsertIntoTable = getInsertIntoTable;
42
+ function getInsertColumns(insertStatement, fromColumns) {
311
43
  var _a;
312
44
  const insertIntoTable = getInsertIntoTable(insertStatement);
313
45
  const insertFields = insertStatement.insertFromConstructor() ||
@@ -316,24 +48,18 @@ function getInsertColumns(insertStatement, dbSchema) {
316
48
  const colRef = insertIdentifier.columnRef();
317
49
  if (colRef) {
318
50
  const fieldName = (0, select_columns_1.splitName)(colRef.text);
319
- const column = (0, select_columns_1.findColumn2)(fieldName, insertIntoTable, dbSchema);
51
+ const column = (0, select_columns_1.findColumn)(fieldName, fromColumns);
320
52
  return column;
321
53
  }
322
54
  throw Error('Invalid sql');
323
55
  });
324
56
  //check insert stmt without fields (only values). Ex.: insert into mytable values()
325
57
  if (!fields) {
326
- return dbSchema.filter(column => column.table == insertIntoTable);
58
+ return fromColumns.filter(column => column.table == insertIntoTable);
327
59
  }
328
60
  return fields;
329
61
  }
330
62
  exports.getInsertColumns = getInsertColumns;
331
- function getUpdateColumns(updateStatement, dbSchema) {
332
- const tableReferences = updateStatement.tableReferenceList().tableReference();
333
- const columns = (0, select_columns_1.extractColumnsFromTableReferences)(tableReferences, dbSchema, []);
334
- return columns;
335
- }
336
- exports.getUpdateColumns = getUpdateColumns;
337
63
  function getDeleteColumns(deleteStatement, dbSchema) {
338
64
  var _a, _b;
339
65
  //TODO - Use extractColumnsFromTableReferences
@@ -344,523 +70,18 @@ function getDeleteColumns(deleteStatement, dbSchema) {
344
70
  .filter(col => col.table == tableName)
345
71
  .map(col => {
346
72
  const colDef = {
347
- table: col.table,
348
- column: col.column,
73
+ table: tableNameStr,
74
+ tableAlias: tableAlias,
349
75
  columnName: col.column,
350
- columnType: col.column_type,
76
+ columnType: { kind: "TypeVar", id: col.column, name: col.column, type: col.column_type },
351
77
  columnKey: col.columnKey,
352
- notNull: col.notNull,
353
- tableAlias: tableAlias
78
+ notNull: col.notNull
354
79
  };
355
80
  return colDef;
356
81
  });
357
82
  return columns;
358
83
  }
359
84
  exports.getDeleteColumns = getDeleteColumns;
360
- function analiseSelectStatement(selectStatement, dbSchema, withSchema, namedParameters) {
361
- const querySpec = (0, parse_1.getQuerySpecificationsFromSelectStatement)(selectStatement);
362
- const fromColumns = (0, select_columns_1.getColumnsFrom)(querySpec[0], dbSchema, withSchema);
363
- let result = analiseQuerySpecification(querySpec[0], dbSchema, withSchema, fromColumns, namedParameters);
364
- for (let index = 1; index < querySpec.length; index++) {
365
- const unionQuery = querySpec[index];
366
- const fromColumns2 = (0, select_columns_1.getColumnsFrom)(unionQuery, dbSchema, withSchema);
367
- const result2 = analiseQuerySpecification(unionQuery, dbSchema, withSchema, fromColumns2, namedParameters);
368
- result = unionResult(result, result2);
369
- }
370
- return result;
371
- }
372
- exports.analiseSelectStatement = analiseSelectStatement;
373
- function unionResult(typeInference1, typeInference2) {
374
- const resultColumnTypes = typeInference1.columns.map((col1, index) => {
375
- const col2 = typeInference2.columns[index];
376
- const resultType = unionTypeResult(col1, col2);
377
- return resultType;
378
- });
379
- return {
380
- columns: resultColumnTypes,
381
- parameters: [...typeInference1.parameters, ...typeInference2.parameters] //TODO-INVERSE?
382
- };
383
- }
384
- function unionTypeResult(type1, type2) {
385
- //Gernerated with tests\check-mysql-inference.ts
386
- const typeMapping = {
387
- "decimal_tinyint": "decimal",
388
- "decimal_smallint": "decimal",
389
- "decimal_int": "decimal",
390
- "decimal_float": "double",
391
- "decimal_double": "double",
392
- "decimal_timestamp": "varchar",
393
- "decimal_bigint": "decimal",
394
- "decimal_mediumint": "decimal",
395
- "decimal_date": "varchar",
396
- "decimal_time": "varchar",
397
- "decimal_datetime": "varchar",
398
- "decimal_year": "decimal",
399
- "decimal_varchar": "varchar",
400
- "decimal_bit": "decimal",
401
- "decimal_json": "varbinary",
402
- "decimal_enum": "varchar",
403
- "decimal_set": "varchar",
404
- "decimal_tinyblob": "text",
405
- "decimal_mediumblob": "text",
406
- "decimal_longblob": "longtext",
407
- "decimal_blob": "text",
408
- "decimal_tinytext": "text",
409
- "decimal_mediumtext": "text",
410
- "decimal_longtext": "longtext",
411
- "decimal_text": "text",
412
- "decimal_varbinary": "varbinary",
413
- "decimal_binary": "binary",
414
- "decimal_char": "binary",
415
- "decimal_geometry": "varbinary",
416
- "tinyint_smallint": "smallint",
417
- "tinyint_int": "int",
418
- "tinyint_float": "float",
419
- "tinyint_double": "double",
420
- "tinyint_timestamp": "varchar",
421
- "tinyint_bigint": "bigint",
422
- "tinyint_mediumint": "mediumint",
423
- "tinyint_date": "varchar",
424
- "tinyint_time": "varchar",
425
- "tinyint_datetime": "varchar",
426
- "tinyint_year": "tinyint",
427
- "tinyint_varchar": "varchar",
428
- "tinyint_bit": "decimal",
429
- "tinyint_json": "varbinary",
430
- "tinyint_enum": "varchar",
431
- "tinyint_set": "varchar",
432
- "tinyint_tinyblob": "text",
433
- "tinyint_mediumblob": "text",
434
- "tinyint_longblob": "longtext",
435
- "tinyint_blob": "text",
436
- "tinyint_tinytext": "text",
437
- "tinyint_mediumtext": "text",
438
- "tinyint_longtext": "longtext",
439
- "tinyint_text": "text",
440
- "tinyint_varbinary": "varbinary",
441
- "tinyint_binary": "binary",
442
- "tinyint_char": "binary",
443
- "tinyint_geometry": "varbinary",
444
- "smallint_int": "int",
445
- "smallint_float": "float",
446
- "smallint_double": "double",
447
- "smallint_timestamp": "varchar",
448
- "smallint_bigint": "bigint",
449
- "smallint_mediumint": "mediumint",
450
- "smallint_date": "varchar",
451
- "smallint_time": "varchar",
452
- "smallint_datetime": "varchar",
453
- "smallint_year": "smallint",
454
- "smallint_varchar": "varchar",
455
- "smallint_bit": "decimal",
456
- "smallint_json": "varbinary",
457
- "smallint_enum": "varchar",
458
- "smallint_set": "varchar",
459
- "smallint_tinyblob": "text",
460
- "smallint_mediumblob": "text",
461
- "smallint_longblob": "longtext",
462
- "smallint_blob": "text",
463
- "smallint_tinytext": "text",
464
- "smallint_mediumtext": "text",
465
- "smallint_longtext": "longtext",
466
- "smallint_text": "text",
467
- "smallint_varbinary": "varbinary",
468
- "smallint_binary": "binary",
469
- "smallint_char": "binary",
470
- "smallint_geometry": "varbinary",
471
- "int_float": "double",
472
- "int_double": "double",
473
- "int_timestamp": "varchar",
474
- "int_bigint": "bigint",
475
- "int_mediumint": "int",
476
- "int_date": "varchar",
477
- "int_time": "varchar",
478
- "int_datetime": "varchar",
479
- "int_year": "int",
480
- "int_varchar": "varchar",
481
- "int_bit": "decimal",
482
- "int_json": "varbinary",
483
- "int_enum": "varchar",
484
- "int_set": "varchar",
485
- "int_tinyblob": "text",
486
- "int_mediumblob": "text",
487
- "int_longblob": "longtext",
488
- "int_blob": "text",
489
- "int_tinytext": "text",
490
- "int_mediumtext": "text",
491
- "int_longtext": "longtext",
492
- "int_text": "text",
493
- "int_varbinary": "varbinary",
494
- "int_binary": "binary",
495
- "int_char": "binary",
496
- "int_geometry": "varbinary",
497
- "float_double": "double",
498
- "float_timestamp": "varchar",
499
- "float_bigint": "float",
500
- "float_mediumint": "float",
501
- "float_date": "varchar",
502
- "float_time": "varchar",
503
- "float_datetime": "varchar",
504
- "float_year": "float",
505
- "float_varchar": "varchar",
506
- "float_bit": "double",
507
- "float_json": "varbinary",
508
- "float_enum": "varchar",
509
- "float_set": "varchar",
510
- "float_tinyblob": "text",
511
- "float_mediumblob": "text",
512
- "float_longblob": "longtext",
513
- "float_blob": "text",
514
- "float_tinytext": "text",
515
- "float_mediumtext": "text",
516
- "float_longtext": "longtext",
517
- "float_text": "text",
518
- "float_varbinary": "varbinary",
519
- "float_binary": "binary",
520
- "float_char": "binary",
521
- "float_geometry": "varbinary",
522
- "double_timestamp": "varchar",
523
- "double_bigint": "double",
524
- "double_mediumint": "double",
525
- "double_date": "varchar",
526
- "double_time": "varchar",
527
- "double_datetime": "varchar",
528
- "double_year": "double",
529
- "double_varchar": "varchar",
530
- "double_bit": "double",
531
- "double_json": "varbinary",
532
- "double_enum": "varchar",
533
- "double_set": "varchar",
534
- "double_tinyblob": "text",
535
- "double_mediumblob": "text",
536
- "double_longblob": "longtext",
537
- "double_blob": "text",
538
- "double_tinytext": "text",
539
- "double_mediumtext": "text",
540
- "double_longtext": "longtext",
541
- "double_text": "text",
542
- "double_varbinary": "varbinary",
543
- "double_binary": "binary",
544
- "double_char": "binary",
545
- "double_geometry": "varbinary",
546
- "timestamp_bigint": "varchar",
547
- "timestamp_mediumint": "varchar",
548
- "timestamp_date": "datetime",
549
- "timestamp_time": "datetime",
550
- "timestamp_datetime": "datetime",
551
- "timestamp_year": "varchar",
552
- "timestamp_varchar": "varchar",
553
- "timestamp_bit": "varbinary",
554
- "timestamp_json": "varbinary",
555
- "timestamp_enum": "varchar",
556
- "timestamp_set": "varchar",
557
- "timestamp_tinyblob": "text",
558
- "timestamp_mediumblob": "text",
559
- "timestamp_longblob": "longtext",
560
- "timestamp_blob": "text",
561
- "timestamp_tinytext": "text",
562
- "timestamp_mediumtext": "text",
563
- "timestamp_longtext": "longtext",
564
- "timestamp_text": "text",
565
- "timestamp_varbinary": "varbinary",
566
- "timestamp_binary": "binary",
567
- "timestamp_char": "binary",
568
- "timestamp_geometry": "varbinary",
569
- "bigint_mediumint": "bigint",
570
- "bigint_date": "varchar",
571
- "bigint_time": "varchar",
572
- "bigint_datetime": "varchar",
573
- "bigint_year": "bigint",
574
- "bigint_varchar": "varchar",
575
- "bigint_bit": "decimal",
576
- "bigint_json": "varbinary",
577
- "bigint_enum": "varchar",
578
- "bigint_set": "varchar",
579
- "bigint_tinyblob": "text",
580
- "bigint_mediumblob": "text",
581
- "bigint_longblob": "longtext",
582
- "bigint_blob": "text",
583
- "bigint_tinytext": "text",
584
- "bigint_mediumtext": "text",
585
- "bigint_longtext": "longtext",
586
- "bigint_text": "text",
587
- "bigint_varbinary": "varbinary",
588
- "bigint_binary": "binary",
589
- "bigint_char": "binary",
590
- "bigint_geometry": "varbinary",
591
- "mediumint_date": "varchar",
592
- "mediumint_time": "varchar",
593
- "mediumint_datetime": "varchar",
594
- "mediumint_year": "mediumint",
595
- "mediumint_varchar": "varchar",
596
- "mediumint_bit": "decimal",
597
- "mediumint_json": "varbinary",
598
- "mediumint_enum": "varchar",
599
- "mediumint_set": "varchar",
600
- "mediumint_tinyblob": "text",
601
- "mediumint_mediumblob": "text",
602
- "mediumint_longblob": "longtext",
603
- "mediumint_blob": "text",
604
- "mediumint_tinytext": "text",
605
- "mediumint_mediumtext": "text",
606
- "mediumint_longtext": "longtext",
607
- "mediumint_text": "text",
608
- "mediumint_varbinary": "varbinary",
609
- "mediumint_binary": "binary",
610
- "mediumint_char": "binary",
611
- "mediumint_geometry": "varbinary",
612
- "date_time": "datetime",
613
- "date_datetime": "datetime",
614
- "date_year": "varchar",
615
- "date_varchar": "varchar",
616
- "date_bit": "varbinary",
617
- "date_json": "varbinary",
618
- "date_enum": "varchar",
619
- "date_set": "varchar",
620
- "date_tinyblob": "text",
621
- "date_mediumblob": "text",
622
- "date_longblob": "longtext",
623
- "date_blob": "text",
624
- "date_tinytext": "text",
625
- "date_mediumtext": "text",
626
- "date_longtext": "longtext",
627
- "date_text": "text",
628
- "date_varbinary": "varbinary",
629
- "date_binary": "binary",
630
- "date_char": "binary",
631
- "date_geometry": "varbinary",
632
- "time_datetime": "datetime",
633
- "time_year": "varchar",
634
- "time_varchar": "varchar",
635
- "time_bit": "varbinary",
636
- "time_json": "varbinary",
637
- "time_enum": "varchar",
638
- "time_set": "varchar",
639
- "time_tinyblob": "text",
640
- "time_mediumblob": "text",
641
- "time_longblob": "longtext",
642
- "time_blob": "text",
643
- "time_tinytext": "text",
644
- "time_mediumtext": "text",
645
- "time_longtext": "longtext",
646
- "time_text": "text",
647
- "time_varbinary": "varbinary",
648
- "time_binary": "binary",
649
- "time_char": "binary",
650
- "time_geometry": "varbinary",
651
- "datetime_year": "varchar",
652
- "datetime_varchar": "varchar",
653
- "datetime_bit": "varbinary",
654
- "datetime_json": "varbinary",
655
- "datetime_enum": "varchar",
656
- "datetime_set": "varchar",
657
- "datetime_tinyblob": "text",
658
- "datetime_mediumblob": "text",
659
- "datetime_longblob": "longtext",
660
- "datetime_blob": "text",
661
- "datetime_tinytext": "text",
662
- "datetime_mediumtext": "text",
663
- "datetime_longtext": "longtext",
664
- "datetime_text": "text",
665
- "datetime_varbinary": "varbinary",
666
- "datetime_binary": "binary",
667
- "datetime_char": "binary",
668
- "datetime_geometry": "varbinary",
669
- "year_varchar": "varchar",
670
- "year_bit": "bigint",
671
- "year_json": "varbinary",
672
- "year_enum": "varchar",
673
- "year_set": "varchar",
674
- "year_tinyblob": "text",
675
- "year_mediumblob": "text",
676
- "year_longblob": "longtext",
677
- "year_blob": "text",
678
- "year_tinytext": "text",
679
- "year_mediumtext": "text",
680
- "year_longtext": "longtext",
681
- "year_text": "text",
682
- "year_varbinary": "varbinary",
683
- "year_binary": "binary",
684
- "year_char": "binary",
685
- "year_geometry": "varbinary",
686
- "varchar_bit": "varbinary",
687
- "varchar_json": "varbinary",
688
- "varchar_enum": "varchar",
689
- "varchar_set": "varchar",
690
- "varchar_tinyblob": "text",
691
- "varchar_mediumblob": "text",
692
- "varchar_longblob": "longtext",
693
- "varchar_blob": "text",
694
- "varchar_tinytext": "text",
695
- "varchar_mediumtext": "text",
696
- "varchar_longtext": "longtext",
697
- "varchar_text": "text",
698
- "varchar_varbinary": "varbinary",
699
- "varchar_binary": "varbinary",
700
- "varchar_char": "varchar",
701
- "varchar_geometry": "varbinary",
702
- "bit_json": "varbinary",
703
- "bit_enum": "varbinary",
704
- "bit_set": "varbinary",
705
- "bit_tinyblob": "text",
706
- "bit_mediumblob": "text",
707
- "bit_longblob": "longtext",
708
- "bit_blob": "text",
709
- "bit_tinytext": "tinytext",
710
- "bit_mediumtext": "mediumtext",
711
- "bit_longtext": "longtext",
712
- "bit_text": "text",
713
- "bit_varbinary": "varbinary",
714
- "bit_binary": "binary",
715
- "bit_char": "binary",
716
- "bit_geometry": "varbinary",
717
- "json_enum": "varbinary",
718
- "json_set": "varbinary",
719
- "json_tinyblob": "longtext",
720
- "json_mediumblob": "longtext",
721
- "json_longblob": "longtext",
722
- "json_blob": "longtext",
723
- "json_tinytext": "longtext",
724
- "json_mediumtext": "longtext",
725
- "json_longtext": "longtext",
726
- "json_text": "longtext",
727
- "json_varbinary": "varbinary",
728
- "json_binary": "varbinary",
729
- "json_char": "varbinary",
730
- "json_geometry": "varbinary",
731
- "enum_set": "varchar",
732
- "enum_tinyblob": "text",
733
- "enum_mediumblob": "text",
734
- "enum_longblob": "longtext",
735
- "enum_blob": "text",
736
- "enum_tinytext": "text",
737
- "enum_mediumtext": "text",
738
- "enum_longtext": "longtext",
739
- "enum_text": "text",
740
- "enum_varbinary": "varbinary",
741
- "enum_binary": "binary",
742
- "enum_char": "binary",
743
- "enum_geometry": "varbinary",
744
- "set_tinyblob": "text",
745
- "set_mediumblob": "text",
746
- "set_longblob": "longtext",
747
- "set_blob": "text",
748
- "set_tinytext": "text",
749
- "set_mediumtext": "text",
750
- "set_longtext": "longtext",
751
- "set_text": "text",
752
- "set_varbinary": "varbinary",
753
- "set_binary": "binary",
754
- "set_char": "binary",
755
- "set_geometry": "varbinary",
756
- "tinyblob_mediumblob": "text",
757
- "tinyblob_longblob": "longtext",
758
- "tinyblob_blob": "text",
759
- "tinyblob_tinytext": "tinytext",
760
- "tinyblob_mediumtext": "mediumtext",
761
- "tinyblob_longtext": "longtext",
762
- "tinyblob_text": "text",
763
- "tinyblob_varbinary": "text",
764
- "tinyblob_binary": "text",
765
- "tinyblob_char": "text",
766
- "tinyblob_geometry": "longtext",
767
- "mediumblob_longblob": "longtext",
768
- "mediumblob_blob": "text",
769
- "mediumblob_tinytext": "text",
770
- "mediumblob_mediumtext": "mediumtext",
771
- "mediumblob_longtext": "longtext",
772
- "mediumblob_text": "text",
773
- "mediumblob_varbinary": "text",
774
- "mediumblob_binary": "text",
775
- "mediumblob_char": "text",
776
- "mediumblob_geometry": "longtext",
777
- "longblob_blob": "longtext",
778
- "longblob_tinytext": "longtext",
779
- "longblob_mediumtext": "longtext",
780
- "longblob_longtext": "longtext",
781
- "longblob_text": "longtext",
782
- "longblob_varbinary": "longtext",
783
- "longblob_binary": "longtext",
784
- "longblob_char": "longtext",
785
- "longblob_geometry": "longtext",
786
- "blob_tinytext": "text",
787
- "blob_mediumtext": "mediumtext",
788
- "blob_longtext": "longtext",
789
- "blob_text": "text",
790
- "blob_varbinary": "text",
791
- "blob_binary": "text",
792
- "blob_char": "text",
793
- "blob_geometry": "longtext",
794
- "tinytext_mediumtext": "text",
795
- "tinytext_longtext": "longtext",
796
- "tinytext_text": "text",
797
- "tinytext_varbinary": "tinytext",
798
- "tinytext_binary": "tinytext",
799
- "tinytext_char": "text",
800
- "tinytext_geometry": "longtext",
801
- "mediumtext_longtext": "longtext",
802
- "mediumtext_text": "text",
803
- "mediumtext_varbinary": "mediumtext",
804
- "mediumtext_binary": "mediumtext",
805
- "mediumtext_char": "text",
806
- "mediumtext_geometry": "longtext",
807
- "longtext_text": "longtext",
808
- "longtext_varbinary": "longtext",
809
- "longtext_binary": "longtext",
810
- "longtext_char": "longtext",
811
- "longtext_geometry": "longtext",
812
- "text_varbinary": "text",
813
- "text_binary": "text",
814
- "text_char": "text",
815
- "text_geometry": "longtext",
816
- "varbinary_binary": "varbinary",
817
- "varbinary_char": "varbinary",
818
- "varbinary_geometry": "varbinary",
819
- "binary_char": "binary",
820
- "binary_geometry": "varbinary",
821
- "char_geometry": "varbinary"
822
- };
823
- if (type1 == type2)
824
- return type1;
825
- //@ts-ignore
826
- //ex. tinyint_smallint or smallint_tinyint
827
- return typeMapping[type1 + "_" + type2] || typeMapping[type2 + "_" + type1];
828
- }
829
- exports.unionTypeResult = unionTypeResult;
830
- function analiseQuerySpecification(querySpec, dbSchema, withSchema, fromColumns, namedParameters) {
831
- const context = {
832
- dbSchema,
833
- withSchema,
834
- fromColumns,
835
- parameters: [],
836
- constraints: [],
837
- };
838
- const queryTypes = walkQuerySpecification(context, querySpec);
839
- const paramIndexes = (0, util_1.getParameterIndexes)(namedParameters.slice(0, context.parameters.length)); //for [a, a, b, a] will return a: [0, 1, 3]; b: [2]
840
- paramIndexes.forEach(paramIndex => {
841
- (0, util_1.getPairWise)(paramIndex.indexes, (cur, next) => {
842
- context.constraints.push({
843
- expression: paramIndex.paramName,
844
- type1: context.parameters[cur],
845
- type2: context.parameters[next]
846
- });
847
- });
848
- });
849
- // console.log("namedNodes");
850
- // console.dir(namedNodes, { depth: null });
851
- // console.log("constraints2=");
852
- // console.dir(constraints, { depth: null });
853
- const substitutions = {};
854
- (0, unify_1.unify)(context.constraints, substitutions);
855
- const parameters = context.parameters.map(param => getVarType(substitutions, param));
856
- const columnTypes = queryTypes.types.map(param => getVarType(substitutions, param));
857
- const querySpecResult = {
858
- parameters: parameters,
859
- columns: columnTypes
860
- };
861
- return querySpecResult;
862
- }
863
- exports.analiseQuerySpecification = analiseQuerySpecification;
864
85
  function generateTypeInfo(namedNodes, constraints) {
865
86
  const substitutions = {};
866
87
  (0, unify_1.unify)(constraints, substitutions);
@@ -870,897 +91,26 @@ function generateTypeInfo(namedNodes, constraints) {
870
91
  exports.generateTypeInfo = generateTypeInfo;
871
92
  function getVarType(substitutions, typeVar) {
872
93
  if (typeVar.kind == 'TypeVar') {
873
- const type = substitutions[typeVar.id];
874
- if (!type) {
875
- return typeVar.type;
876
- }
877
- const resultType = typeVar.list ? type.type + '[]' : type.type;
94
+ // if (typeVar.type != '?') {
95
+ // return typeVar.type;
96
+ // }
97
+ const subs = substitutions[typeVar.id];
98
+ if (subs) {
99
+ if (subs.id != typeVar.id) {
100
+ return getVarType(substitutions, subs);
101
+ }
102
+ const resultType = subs.list || typeVar.list ? subs.type + '[]' : subs.type;
103
+ return resultType;
104
+ }
105
+ // if (!subs) {
106
+ // return typeVar.type as MySqlType;
107
+ // }
108
+ const resultType = typeVar.list ? typeVar.type + '[]' : typeVar.type;
878
109
  return resultType;
879
110
  }
880
111
  return '?';
881
112
  }
882
- function walkQuerySpecification(context, querySpec) {
883
- const listType = [];
884
- const fromClause = querySpec.fromClause();
885
- if (fromClause) {
886
- const subqueryColumns = (0, select_columns_1.getColumnsFrom)(querySpec, context.dbSchema, context.withSchema);
887
- const newContext = Object.assign(Object.assign({}, context), { fromColumns: subqueryColumns });
888
- walkFromClause(newContext, fromClause);
889
- }
890
- if (querySpec.selectItemList().MULT_OPERATOR()) {
891
- context.fromColumns.forEach(col => {
892
- const colType = freshVar(col.columnName, col.columnType);
893
- listType.push(colType);
894
- });
895
- }
896
- querySpec.selectItemList().selectItem().forEach(selectItem => {
897
- var _a, _b;
898
- const tableWild = selectItem.tableWild(); //ex. t1.*
899
- if (tableWild) {
900
- tableWild.identifier().forEach(tabWild => {
901
- if (tableWild.MULT_OPERATOR()) {
902
- const prefix = tabWild.text;
903
- const columns = (0, select_columns_1.selectAllColumns)(prefix, context.fromColumns);
904
- columns.forEach(col => {
905
- const colType = freshVar(col.columnName, col.columnType);
906
- listType.push(colType);
907
- });
908
- }
909
- });
910
- }
911
- else {
912
- const expr = selectItem.expr();
913
- if (expr) {
914
- const exprType = walkExpr(context, expr);
915
- if (exprType.kind == 'TypeOperator') {
916
- const subqueryType = exprType.types[0];
917
- listType.push(subqueryType);
918
- }
919
- else {
920
- listType.push(exprType);
921
- const subQueryContext = (0, infer_param_nullability_1.getParentContext)(expr, ts_mysql_parser_1.SubqueryContext);
922
- const alias = ((_b = (_a = selectItem.selectAlias()) === null || _a === void 0 ? void 0 : _a.identifier()) === null || _b === void 0 ? void 0 : _b.text) || undefined;
923
- if (alias && !subQueryContext) {
924
- //IF NOT A SUBQUERY THIS ALIAS CAN BE USED IN THE HAVING CLAUSE
925
- context.constraints.push({
926
- expression: expr.text,
927
- mostGeneralType: true,
928
- aliasConstraint: true,
929
- type1: exprType,
930
- type2: freshVar(alias, exprType.type)
931
- });
932
- }
933
- }
934
- }
935
- }
936
- });
937
- const whereClause = querySpec.whereClause();
938
- //TODO - HAVING, BLAH
939
- if (whereClause) {
940
- const whereExpr = whereClause === null || whereClause === void 0 ? void 0 : whereClause.expr();
941
- walkExpr(context, whereExpr);
942
- }
943
- const havingClause = querySpec.havingClause();
944
- if (havingClause) {
945
- context.havingExpr = true;
946
- walkHavingClause(havingClause, context);
947
- context.havingExpr = false;
948
- }
949
- const typeOperator = {
950
- kind: 'TypeOperator',
951
- selectItem: true,
952
- types: listType
953
- };
954
- return typeOperator;
955
- }
956
- exports.walkQuerySpecification = walkQuerySpecification;
957
- function walkFromClause(context, fromClause) {
958
- var _a;
959
- const tableReferences = (_a = fromClause.tableReferenceList()) === null || _a === void 0 ? void 0 : _a.tableReference();
960
- tableReferences === null || tableReferences === void 0 ? void 0 : tableReferences.forEach(tabeRef => {
961
- tabeRef.joinedTable().forEach(joinedTable => {
962
- const onExpr = joinedTable.expr();
963
- if (onExpr) {
964
- walkExpr(context, onExpr);
965
- }
966
- });
967
- });
968
- }
969
- function walkHavingClause(havingClause, context) {
970
- const havingExpr = havingClause.expr();
971
- walkExpr(context, havingExpr);
972
- }
973
- function walkExpr(context, expr) {
974
- if (expr instanceof ts_mysql_parser_1.ExprIsContext) {
975
- const boolPri = expr.boolPri();
976
- const boolPriType = walkBoolPri(context, boolPri);
977
- return boolPriType;
978
- }
979
- if (expr instanceof ts_mysql_parser_1.ExprNotContext) {
980
- return freshVar(expr.text, 'tinyint');
981
- ;
982
- }
983
- if (expr instanceof ts_mysql_parser_1.ExprAndContext || expr instanceof ts_mysql_parser_1.ExprXorContext || expr instanceof ts_mysql_parser_1.ExprOrContext) {
984
- const exprLeft = expr.expr()[0];
985
- walkExpr(context, exprLeft);
986
- const exprRight = expr.expr()[1];
987
- walkExpr(context, exprRight);
988
- return freshVar(expr.text, 'tinyint');
989
- }
990
- throw Error('invalid type');
991
- }
992
- function walkBoolPri(context, boolPri) {
993
- if (boolPri instanceof ts_mysql_parser_1.PrimaryExprPredicateContext) {
994
- const predicate = boolPri.predicate();
995
- const predicateType = walkPredicate(context, predicate);
996
- return predicateType;
997
- }
998
- if (boolPri instanceof ts_mysql_parser_1.PrimaryExprIsNullContext) {
999
- const boolPri2 = boolPri.boolPri();
1000
- walkBoolPri(context, boolPri2);
1001
- return freshVar(boolPri.text, '?');
1002
- }
1003
- if (boolPri instanceof ts_mysql_parser_1.PrimaryExprCompareContext) {
1004
- const compareLeft = boolPri.boolPri();
1005
- const compareRight = boolPri.predicate();
1006
- const typeLeft = walkBoolPri(context, compareLeft);
1007
- const typeRight = walkPredicate(context, compareRight);
1008
- context.constraints.push({
1009
- expression: boolPri.text,
1010
- type1: typeLeft,
1011
- type2: typeRight
1012
- });
1013
- return freshVar(boolPri.text, 'tinyint');
1014
- }
1015
- if (boolPri instanceof ts_mysql_parser_1.PrimaryExprAllAnyContext) {
1016
- const compareLeft = boolPri.boolPri();
1017
- const compareRight = boolPri.subquery();
1018
- const typeLeft = walkBoolPri(context, compareLeft);
1019
- const typeRight = walkSubquery(context, compareRight);
1020
- context.constraints.push({
1021
- expression: boolPri.text,
1022
- type1: typeLeft,
1023
- type2: typeRight
1024
- });
1025
- return freshVar(boolPri.text, 'tinyint');
1026
- }
1027
- throw Error('invalid sql');
1028
- }
1029
- function walkPredicate(context, predicate) {
1030
- const bitExpr = predicate.bitExpr()[0];
1031
- const bitExprType = walkBitExpr(context, bitExpr);
1032
- const predicateOperations = predicate.predicateOperations();
1033
- if (predicateOperations) {
1034
- const rightType = walkpredicateOperations(context, bitExprType, predicateOperations);
1035
- context.constraints.push({
1036
- expression: predicateOperations.text,
1037
- type1: bitExprType,
1038
- type2: rightType,
1039
- mostGeneralType: true
1040
- });
1041
- return rightType;
1042
- }
1043
- // return freshVar(predicateOperations.text, 'tinyint');
1044
- return bitExprType;
1045
- }
1046
- function walkpredicateOperations(context, parentType, predicateOperations) {
1047
- if (predicateOperations instanceof ts_mysql_parser_1.PredicateExprInContext) {
1048
- const subquery = predicateOperations.subquery();
1049
- if (subquery) {
1050
- const rightType = walkSubquery(context, subquery);
1051
- return rightType;
1052
- }
1053
- const exprList = predicateOperations.exprList();
1054
- if (exprList) {
1055
- const rightType = walkExprList(context, exprList);
1056
- return rightType;
1057
- }
1058
- }
1059
- if (predicateOperations instanceof ts_mysql_parser_1.PredicateExprLikeContext) {
1060
- const simpleExpr = predicateOperations.simpleExpr()[0];
1061
- const rightType = walkSimpleExpr(context, simpleExpr);
1062
- context.constraints.push({
1063
- expression: simpleExpr.text,
1064
- type1: parentType,
1065
- type2: rightType
1066
- });
1067
- return rightType;
1068
- }
1069
- throw Error("Not expected");
1070
- }
1071
- function walkExprList(context, exprList) {
1072
- const listType = exprList.expr().map(item => {
1073
- const exprType = walkExpr(context, item);
1074
- return exprType;
1075
- });
1076
- const type = {
1077
- kind: 'TypeOperator',
1078
- types: listType
1079
- };
1080
- return type;
1081
- }
1082
- function walkBitExpr(context, bitExpr) {
1083
- const simpleExpr = bitExpr.simpleExpr();
1084
- if (simpleExpr) {
1085
- return walkSimpleExpr(context, simpleExpr);
1086
- }
1087
- if (bitExpr.bitExpr().length == 2) {
1088
- const bitExprType = freshVar(bitExpr.text, 'number');
1089
- const bitExprLeft = bitExpr.bitExpr()[0];
1090
- const typeLeftTemp = walkBitExpr(context, bitExprLeft);
1091
- const typeLeft = typeLeftTemp.kind == 'TypeOperator' ? typeLeftTemp.types[0] : typeLeftTemp;
1092
- //const newTypeLeft = typeLeft.name == '?'? freshVar('?', 'bigint') : typeLeft;
1093
- const bitExprRight = bitExpr.bitExpr()[1];
1094
- const typeRightTemp = walkBitExpr(context, bitExprRight);
1095
- //In the expression 'id + (value + 2) + ?' the '(value+2)' is treated as a SimpleExprListContext and return a TypeOperator
1096
- const typeRight = typeRightTemp.kind == 'TypeOperator' ? typeRightTemp.types[0] : typeRightTemp;
1097
- //const newTypeRight = typeRight.name == '?'? freshVar('?', 'bigint') : typeRight;
1098
- context.constraints.push({
1099
- expression: bitExpr.text,
1100
- type1: typeLeft,
1101
- type2: typeRight,
1102
- mostGeneralType: true,
1103
- coercionType: 'Sum'
1104
- });
1105
- context.constraints.push({
1106
- expression: bitExpr.text,
1107
- type1: bitExprType,
1108
- type2: typeLeft,
1109
- mostGeneralType: true,
1110
- coercionType: 'Sum'
1111
- });
1112
- context.constraints.push({
1113
- expression: bitExpr.text,
1114
- type1: bitExprType,
1115
- type2: typeRight,
1116
- mostGeneralType: true,
1117
- coercionType: 'Sum'
1118
- });
1119
- return bitExprType;
1120
- }
1121
- if (bitExpr.INTERVAL_SYMBOL()) {
1122
- const bitExpr2 = bitExpr.bitExpr()[0];
1123
- const leftType = walkBitExpr(context, bitExpr2);
1124
- const expr = bitExpr.expr(); //expr interval
1125
- walkExpr(context, expr);
1126
- context.constraints.push({
1127
- expression: bitExpr.text,
1128
- type1: leftType,
1129
- type2: freshVar('datetime', 'datetime')
1130
- });
1131
- return freshVar('datetime', 'datetime');
1132
- }
1133
- throw Error('Invalid sql');
1134
- }
1135
- function walkSimpleExpr(context, simpleExpr) {
1136
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
1137
- if (simpleExpr instanceof ts_mysql_parser_1.SimpleExprColumnRefContext) {
1138
- const fieldName = (0, select_columns_1.splitName)(simpleExpr.text);
1139
- if (context.havingExpr) {
1140
- let foundType = null;
1141
- context.constraints.forEach(p => {
1142
- if (p.type1.kind == 'TypeVar' && p.aliasConstraint && p.type1.name == simpleExpr.text) {
1143
- foundType = p.type1;
1144
- }
1145
- if (p.type2.kind == 'TypeVar' && p.aliasConstraint && p.type2.name == simpleExpr.text) {
1146
- foundType = p.type2;
1147
- }
1148
- });
1149
- if (foundType) {
1150
- return foundType;
1151
- }
1152
- const type = freshVar(simpleExpr.text, '?');
1153
- return type;
1154
- }
1155
- else {
1156
- const columnType = (0, select_columns_1.findColumn)(fieldName, context.fromColumns).columnType;
1157
- const type = freshVar(simpleExpr.text, columnType);
1158
- return type;
1159
- }
1160
- }
1161
- if (simpleExpr instanceof ts_mysql_parser_1.SimpleExprRuntimeFunctionContext) {
1162
- const runtimeFunctionCall = simpleExpr.runtimeFunctionCall();
1163
- if (runtimeFunctionCall.NOW_SYMBOL()) {
1164
- return freshVar(simpleExpr.text, 'datetime');
1165
- }
1166
- if (runtimeFunctionCall.CURDATE_SYMBOL()) {
1167
- return freshVar(simpleExpr.text, 'date');
1168
- }
1169
- if (runtimeFunctionCall.CURTIME_SYMBOL()) {
1170
- return freshVar(simpleExpr.text, 'time');
1171
- }
1172
- if (runtimeFunctionCall.REPLACE_SYMBOL()) {
1173
- const exprList = runtimeFunctionCall.expr();
1174
- exprList.forEach(expr => {
1175
- const exprType = walkExpr(context, expr);
1176
- context.constraints.push({
1177
- expression: expr.text,
1178
- type1: exprType,
1179
- type2: freshVar('varchar', 'varchar')
1180
- });
1181
- });
1182
- return freshVar('varchar', 'varchar');
1183
- }
1184
- if (runtimeFunctionCall.YEAR_SYMBOL() || runtimeFunctionCall.MONTH_SYMBOL() || runtimeFunctionCall.DAY_SYMBOL()) {
1185
- const expr = (_a = runtimeFunctionCall.exprWithParentheses()) === null || _a === void 0 ? void 0 : _a.expr();
1186
- if (expr) {
1187
- const paramType = walkExpr(context, expr);
1188
- if (paramType.kind == 'TypeVar' && isDateTimeLiteral(paramType.name)) {
1189
- paramType.type = 'datetime';
1190
- }
1191
- if (paramType.kind == 'TypeVar' && isDateLiteral(paramType.name)) {
1192
- paramType.type = 'date';
1193
- }
1194
- context.constraints.push({
1195
- expression: expr.text,
1196
- type1: paramType,
1197
- type2: freshVar(simpleExpr.text, 'date')
1198
- });
1199
- }
1200
- const returnType = runtimeFunctionCall.YEAR_SYMBOL() ? 'year' : 'tinyint';
1201
- return freshVar(simpleExpr.text, returnType);
1202
- }
1203
- if (runtimeFunctionCall.HOUR_SYMBOL() || runtimeFunctionCall.MINUTE_SYMBOL() || runtimeFunctionCall.SECOND_SYMBOL()) {
1204
- const expr = (_b = runtimeFunctionCall.exprWithParentheses()) === null || _b === void 0 ? void 0 : _b.expr();
1205
- if (expr) {
1206
- const paramType = walkExpr(context, expr);
1207
- if (paramType.kind == 'TypeVar' && isTimeLiteral(paramType.name)) {
1208
- paramType.type = 'time';
1209
- }
1210
- if (paramType.kind == 'TypeVar' && isDateLiteral(paramType.name)) {
1211
- paramType.type = 'date';
1212
- }
1213
- if (paramType.kind == 'TypeVar' && isDateTimeLiteral(paramType.name)) {
1214
- paramType.type = 'datetime';
1215
- }
1216
- context.constraints.push({
1217
- expression: expr.text,
1218
- type1: paramType,
1219
- type2: freshVar(simpleExpr.text, 'time')
1220
- });
1221
- }
1222
- //HOUR can return values greater than 23. Ex.: SELECT HOUR('272:59:59');
1223
- //https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_hour
1224
- const returnType = runtimeFunctionCall.HOUR_SYMBOL() ? 'int' : 'tinyint';
1225
- return freshVar(simpleExpr.text, returnType);
1226
- }
1227
- const trimFunction = runtimeFunctionCall.trimFunction();
1228
- if (trimFunction) {
1229
- const exprList = trimFunction.expr();
1230
- if (exprList.length == 1) {
1231
- const exprType = walkExpr(context, exprList[0]);
1232
- context.constraints.push({
1233
- expression: exprList[0].text,
1234
- type1: exprType,
1235
- type2: freshVar('varchar', 'varchar')
1236
- });
1237
- }
1238
- if (exprList.length == 2) {
1239
- const exprType = walkExpr(context, exprList[0]);
1240
- const expr2Type = walkExpr(context, exprList[1]);
1241
- context.constraints.push({
1242
- expression: exprList[0].text,
1243
- type1: exprType,
1244
- type2: freshVar('varchar', 'varchar')
1245
- });
1246
- context.constraints.push({
1247
- expression: exprList[1].text,
1248
- type1: expr2Type,
1249
- type2: freshVar('varchar', 'varchar')
1250
- });
1251
- }
1252
- return freshVar('varchar', 'varchar');
1253
- }
1254
- const substringFunction = runtimeFunctionCall.substringFunction();
1255
- if (substringFunction) {
1256
- const exprList = substringFunction.expr();
1257
- const varcharParam = freshVar('varchar', 'varchar');
1258
- const intParam = freshVar('int', 'int');
1259
- const params = {
1260
- kind: 'FixedLengthParams',
1261
- paramsType: [varcharParam, intParam, intParam]
1262
- };
1263
- walkExprListParameters(context, exprList, params);
1264
- return varcharParam;
1265
- }
1266
- if (runtimeFunctionCall.ADDDATE_SYMBOL()
1267
- || runtimeFunctionCall.DATE_ADD_SYMBOL()
1268
- || runtimeFunctionCall.SUBDATE_SYMBOL()
1269
- || runtimeFunctionCall.DATE_SUB_SYMBOL()) {
1270
- //SELECT ADDDATE('2008-01-02', INTERVAL 31 DAY)
1271
- //SELECT ADDDATE('2008-01-02', 31)
1272
- const expr1 = runtimeFunctionCall.expr()[0];
1273
- const expr2 = runtimeFunctionCall.expr()[1];
1274
- const typeExpr1 = walkExpr(context, expr1);
1275
- const typeExpr2 = walkExpr(context, expr2);
1276
- if (typeExpr1.kind == 'TypeVar' && (isDateLiteral(typeExpr1.name) || isDateTimeLiteral(typeExpr1.name))) {
1277
- typeExpr1.type = 'datetime';
1278
- }
1279
- context.constraints.push({
1280
- expression: expr1.text,
1281
- type1: typeExpr1,
1282
- type2: freshVar('datetime', 'datetime')
1283
- });
1284
- context.constraints.push({
1285
- expression: expr2.text,
1286
- type1: typeExpr2,
1287
- type2: freshVar('bigint', 'bigint')
1288
- });
1289
- return freshVar('datetime', 'datetime');
1290
- }
1291
- if (runtimeFunctionCall.COALESCE_SYMBOL()) {
1292
- const exprList = (_c = runtimeFunctionCall.exprListWithParentheses()) === null || _c === void 0 ? void 0 : _c.exprList().expr();
1293
- if (exprList) {
1294
- const paramType = freshVar('?', '?');
1295
- const params = {
1296
- kind: 'VariableLengthParams',
1297
- paramType: 'any'
1298
- };
1299
- const paramsTypeList = walkExprListParameters(context, exprList, params);
1300
- paramsTypeList.forEach((typeVar, paramIndex) => {
1301
- context.constraints.push({
1302
- expression: runtimeFunctionCall.text + '_param' + (paramIndex + 1),
1303
- type1: paramType,
1304
- type2: typeVar,
1305
- mostGeneralType: true,
1306
- coercionType: 'Coalesce'
1307
- });
1308
- });
1309
- return paramType;
1310
- }
1311
- }
1312
- //MOD (number, number): number
1313
- if (runtimeFunctionCall.MOD_SYMBOL()) {
1314
- const functionType = freshVar('number', 'number');
1315
- const exprList = runtimeFunctionCall.expr();
1316
- const param1 = walkExpr(context, exprList[0]);
1317
- const param2 = walkExpr(context, exprList[1]);
1318
- context.constraints.push({
1319
- expression: simpleExpr.text,
1320
- type1: freshVar('number', 'number'),
1321
- type2: param1,
1322
- mostGeneralType: true
1323
- });
1324
- context.constraints.push({
1325
- expression: simpleExpr.text,
1326
- type1: freshVar('number', 'number'),
1327
- type2: param2,
1328
- mostGeneralType: true
1329
- });
1330
- context.constraints.push({
1331
- expression: simpleExpr.text,
1332
- type1: functionType,
1333
- type2: param1,
1334
- mostGeneralType: true
1335
- });
1336
- context.constraints.push({
1337
- expression: simpleExpr.text,
1338
- type1: functionType,
1339
- type2: param2,
1340
- mostGeneralType: true
1341
- });
1342
- return functionType;
1343
- }
1344
- if (runtimeFunctionCall.IF_SYMBOL()) {
1345
- const exprList = runtimeFunctionCall.expr();
1346
- const expr1 = exprList[0];
1347
- const expr2 = exprList[1];
1348
- const expr3 = exprList[2];
1349
- walkExpr(context, expr1);
1350
- const expr2Type = walkExpr(context, expr2);
1351
- const expr3Type = walkExpr(context, expr3);
1352
- context.constraints.push({
1353
- expression: runtimeFunctionCall.text,
1354
- type1: expr2Type,
1355
- type2: expr3Type,
1356
- mostGeneralType: true
1357
- });
1358
- return expr2Type;
1359
- }
1360
- throw Error('Function not supported: ' + runtimeFunctionCall.text);
1361
- }
1362
- if (simpleExpr instanceof ts_mysql_parser_1.SimpleExprFunctionContext) {
1363
- const functionIdentifier = getFunctionName(simpleExpr);
1364
- if (functionIdentifier === 'concat_ws' || (functionIdentifier === null || functionIdentifier === void 0 ? void 0 : functionIdentifier.toLowerCase()) === 'concat') {
1365
- const varcharType = freshVar(simpleExpr.text, 'varchar');
1366
- const params = {
1367
- kind: 'VariableLengthParams',
1368
- paramType: '?'
1369
- };
1370
- walkFunctionParameters(context, simpleExpr, params);
1371
- return varcharType;
1372
- }
1373
- if (functionIdentifier === 'avg') {
1374
- const functionType = freshVar(simpleExpr.text, '?');
1375
- context.constraints.push({
1376
- expression: simpleExpr.text,
1377
- type1: functionType,
1378
- type2: freshVar('decimal', 'decimal'),
1379
- mostGeneralType: true
1380
- });
1381
- const params = {
1382
- kind: 'FixedLengthParams',
1383
- paramsType: [functionType]
1384
- };
1385
- walkFunctionParameters(context, simpleExpr, params);
1386
- return functionType;
1387
- }
1388
- if (functionIdentifier === 'round') {
1389
- const functionType = freshVar(simpleExpr.text, '?');
1390
- const params = {
1391
- kind: 'FixedLengthParams',
1392
- paramsType: [functionType]
1393
- };
1394
- const paramsType = walkFunctionParameters(context, simpleExpr, params);
1395
- //The return value has the same type as the first argument
1396
- context.constraints.push({
1397
- expression: simpleExpr.text,
1398
- type1: functionType,
1399
- type2: paramsType[0],
1400
- mostGeneralType: true
1401
- });
1402
- return functionType;
1403
- }
1404
- if (functionIdentifier === 'floor') {
1405
- const doubleParam = freshVar('double', 'double');
1406
- const params = {
1407
- kind: 'FixedLengthParams',
1408
- paramsType: [doubleParam, doubleParam]
1409
- };
1410
- walkFunctionParameters(context, simpleExpr, params);
1411
- return freshVar(simpleExpr.text, 'bigint');
1412
- }
1413
- if (functionIdentifier === 'str_to_date') {
1414
- const varcharParam = freshVar('varchar', 'varchar');
1415
- const params = {
1416
- kind: 'FixedLengthParams',
1417
- paramsType: [varcharParam, varcharParam]
1418
- };
1419
- walkFunctionParameters(context, simpleExpr, params);
1420
- return freshVar(simpleExpr.text, 'date');
1421
- }
1422
- if (functionIdentifier === 'datediff') {
1423
- const udfExprList = (_d = simpleExpr.functionCall().udfExprList()) === null || _d === void 0 ? void 0 : _d.udfExpr();
1424
- if (udfExprList) {
1425
- udfExprList.forEach((inExpr) => {
1426
- const expr = inExpr.expr();
1427
- const exprType = walkExpr(context, expr);
1428
- const newType = verifyDateTypesCoercion(exprType);
1429
- context.constraints.push({
1430
- expression: expr.text,
1431
- type1: newType,
1432
- type2: freshVar('date', 'date'),
1433
- mostGeneralType: true
1434
- });
1435
- });
1436
- }
1437
- return freshVar(simpleExpr.text, 'bigint');
1438
- }
1439
- if (functionIdentifier === 'lpad' || functionIdentifier == 'rpad') {
1440
- const varcharParam = freshVar('varchar', 'varchar');
1441
- const intParam = freshVar('int', 'int');
1442
- const params = {
1443
- kind: 'FixedLengthParams',
1444
- paramsType: [varcharParam, intParam, varcharParam]
1445
- };
1446
- walkFunctionParameters(context, simpleExpr, params);
1447
- return varcharParam;
1448
- }
1449
- if (functionIdentifier === 'lower'
1450
- || functionIdentifier === 'lcase'
1451
- || functionIdentifier === 'upper'
1452
- || functionIdentifier === 'ucase'
1453
- || functionIdentifier === 'ltrim'
1454
- || functionIdentifier === 'rtrim') {
1455
- const varcharParam = freshVar('varchar', 'varchar');
1456
- const params = {
1457
- kind: 'FixedLengthParams',
1458
- paramsType: [varcharParam]
1459
- };
1460
- walkFunctionParameters(context, simpleExpr, params);
1461
- return varcharParam;
1462
- }
1463
- if (functionIdentifier === 'length' || functionIdentifier == 'char_length') {
1464
- const varcharParam = freshVar('varchar', 'varchar');
1465
- const params = {
1466
- kind: 'FixedLengthParams',
1467
- paramsType: [varcharParam]
1468
- };
1469
- walkFunctionParameters(context, simpleExpr, params);
1470
- return freshVar('int', 'int');
1471
- }
1472
- if (functionIdentifier === 'abs') {
1473
- const functionType = freshVar('number', 'number');
1474
- const udfExprList = (_e = simpleExpr.functionCall().udfExprList()) === null || _e === void 0 ? void 0 : _e.udfExpr();
1475
- udfExprList === null || udfExprList === void 0 ? void 0 : udfExprList.forEach(expr => {
1476
- const param1 = walkExpr(context, expr.expr());
1477
- context.constraints.push({
1478
- expression: simpleExpr.text,
1479
- type1: functionType,
1480
- type2: param1,
1481
- mostGeneralType: true
1482
- });
1483
- });
1484
- return functionType;
1485
- }
1486
- if (functionIdentifier == 'ceiling' || functionIdentifier == 'ceil') {
1487
- const functionType = freshVar('number', 'number');
1488
- const udfExprList = (_f = simpleExpr.functionCall().udfExprList()) === null || _f === void 0 ? void 0 : _f.udfExpr();
1489
- udfExprList === null || udfExprList === void 0 ? void 0 : udfExprList.forEach(expr => {
1490
- const param1 = walkExpr(context, expr.expr());
1491
- context.constraints.push({
1492
- expression: simpleExpr.text,
1493
- type1: functionType,
1494
- type2: param1,
1495
- mostGeneralType: true,
1496
- coercionType: 'Ceiling'
1497
- });
1498
- });
1499
- return functionType;
1500
- }
1501
- if (functionIdentifier == 'timestampdiff') {
1502
- const udfExprList = (_g = simpleExpr.functionCall().udfExprList()) === null || _g === void 0 ? void 0 : _g.udfExpr();
1503
- if (udfExprList) {
1504
- const [first, ...rest] = udfExprList;
1505
- const unit = first.text.trim().toLowerCase();
1506
- rest.forEach((inExpr, paramIndex) => {
1507
- const expr = inExpr.expr();
1508
- const exprType = walkExpr(context, expr);
1509
- const newType = verifyDateTypesCoercion(exprType);
1510
- //const expectedType = ['hour', 'minute', 'second'].includes(unit)? 'time' : 'datetime'
1511
- context.constraints.push({
1512
- expression: expr.text,
1513
- type1: newType,
1514
- type2: freshVar('datetime', 'datetime'),
1515
- mostGeneralType: true
1516
- });
1517
- });
1518
- }
1519
- return freshVar('int', 'int');
1520
- }
1521
- if (functionIdentifier == 'ifnull' || functionIdentifier == 'nullif') {
1522
- const functionType = freshVar(simpleExpr.text, '?');
1523
- const udfExprList = (_h = simpleExpr.functionCall().udfExprList()) === null || _h === void 0 ? void 0 : _h.udfExpr();
1524
- if (udfExprList) {
1525
- const [expr1, expr2] = udfExprList;
1526
- const expr1Type = walkExpr(context, expr1.expr());
1527
- context.constraints.push({
1528
- expression: expr1.text,
1529
- type1: functionType,
1530
- type2: expr1Type
1531
- });
1532
- const expr2Type = walkExpr(context, expr2.expr());
1533
- context.constraints.push({
1534
- expression: expr2.text,
1535
- type1: functionType,
1536
- type2: expr2Type
1537
- });
1538
- }
1539
- return functionType;
1540
- }
1541
- if (functionIdentifier == 'md5'
1542
- || functionIdentifier == 'hex'
1543
- || functionIdentifier == 'unhex') {
1544
- const functionType = freshVar(simpleExpr.text, 'char');
1545
- const udfExprList = (_j = simpleExpr.functionCall().udfExprList()) === null || _j === void 0 ? void 0 : _j.udfExpr();
1546
- if (udfExprList) {
1547
- const [expr1] = udfExprList;
1548
- walkExpr(context, expr1.expr());
1549
- }
1550
- return functionType;
1551
- }
1552
- throw Error('Function not supported: ' + functionIdentifier);
1553
- }
1554
- if (simpleExpr instanceof ts_mysql_parser_1.SimpleExprParamMarkerContext) {
1555
- const param = freshVar('?', '?');
1556
- context.parameters.push(param);
1557
- return param;
1558
- }
1559
- if (simpleExpr instanceof ts_mysql_parser_1.SimpleExprSumContext) {
1560
- const sumExpr = simpleExpr.sumExpr();
1561
- if (sumExpr.MAX_SYMBOL() || sumExpr.MIN_SYMBOL()) {
1562
- const functionType = freshVar(simpleExpr.text, '?');
1563
- const inSumExpr = (_k = sumExpr.inSumExpr()) === null || _k === void 0 ? void 0 : _k.expr();
1564
- if (inSumExpr) {
1565
- const inSumExprType = walkExpr(context, inSumExpr);
1566
- context.constraints.push({
1567
- expression: simpleExpr.text,
1568
- type1: functionType,
1569
- type2: inSumExprType,
1570
- mostGeneralType: true
1571
- });
1572
- }
1573
- return functionType;
1574
- }
1575
- if (sumExpr.COUNT_SYMBOL()) {
1576
- const functionType = freshVar(simpleExpr.text, 'bigint');
1577
- const inSumExpr = (_l = sumExpr.inSumExpr()) === null || _l === void 0 ? void 0 : _l.expr();
1578
- if (inSumExpr) {
1579
- walkExpr(context, inSumExpr);
1580
- }
1581
- return functionType;
1582
- }
1583
- if (sumExpr.SUM_SYMBOL() || sumExpr.AVG_SYMBOL()) {
1584
- const functionType = freshVar(simpleExpr.text, '?');
1585
- const inSumExpr = (_m = sumExpr.inSumExpr()) === null || _m === void 0 ? void 0 : _m.expr();
1586
- if (inSumExpr) {
1587
- const inSumExprType = walkExpr(context, inSumExpr);
1588
- context.constraints.push({
1589
- expression: simpleExpr.text,
1590
- type1: functionType,
1591
- type2: inSumExprType,
1592
- mostGeneralType: true,
1593
- coercionType: 'SumFunction'
1594
- });
1595
- }
1596
- return functionType;
1597
- }
1598
- if (sumExpr.GROUP_CONCAT_SYMBOL()) {
1599
- const exprList = sumExpr.exprList();
1600
- if (exprList) {
1601
- exprList.expr().map(item => {
1602
- const exprType = walkExpr(context, item);
1603
- return exprType;
1604
- });
1605
- /*
1606
- The result type is TEXT or BLOB unless group_concat_max_len is less than or equal to 512,
1607
- in which case the result type is VARCHAR or VARBINARY.
1608
- */
1609
- //TODO - Infer TEXT/BLOB or VARCHAR/VARBINARY
1610
- return freshVar(sumExpr.text, 'varchar');
1611
- ;
1612
- }
1613
- }
1614
- throw Error('Expression not supported: ' + sumExpr.text);
1615
- }
1616
- if (simpleExpr instanceof ts_mysql_parser_1.SimpleExprLiteralContext) {
1617
- const literal = simpleExpr.literal();
1618
- if (literal.textLiteral()) {
1619
- const text = ((_o = literal.textLiteral()) === null || _o === void 0 ? void 0 : _o.text.slice(1, -1)) || ''; //remove quotes
1620
- return freshVar(text, 'varchar');
1621
- }
1622
- const numLiteral = literal.numLiteral();
1623
- if (numLiteral) {
1624
- return freshVar(numLiteral.text, 'bigint');
1625
- // addNamedNode(simpleExpr, freshVar('bigint', 'bigint'), namedNodes)
1626
- // if(numLiteral.INT_NUMBER()) {
1627
- // const typeInt = freshVar('int', 'int');
1628
- // addNamedNode(simpleExpr, typeInt, namedNodes)
1629
- // }
1630
- // if(numLiteral.DECIMAL_NUMBER()) {
1631
- // const typeDecimal = freshVar('decimal', 'decimal');
1632
- // addNamedNode(simpleExpr, typeDecimal, namedNodes)
1633
- // }
1634
- // if(numLiteral.FLOAT_NUMBER()) {
1635
- // const typeFloat = freshVar('float', 'float');
1636
- // addNamedNode(simpleExpr, typeFloat, namedNodes)
1637
- // }
1638
- ;
1639
- }
1640
- const boolLiteral = literal.boolLiteral();
1641
- if (boolLiteral) {
1642
- return freshVar(boolLiteral.text, 'bit');
1643
- }
1644
- const nullLiteral = literal.nullLiteral();
1645
- if (nullLiteral) {
1646
- return freshVar(nullLiteral.text, '?');
1647
- }
1648
- throw Error('literal not supported:' + literal.text);
1649
- //...
1650
- }
1651
- if (simpleExpr instanceof ts_mysql_parser_1.SimpleExprListContext) {
1652
- const exprList = simpleExpr.exprList();
1653
- const listType = exprList.expr().map(item => {
1654
- const exprType = walkExpr(context, item);
1655
- return exprType;
1656
- });
1657
- const resultType = {
1658
- kind: 'TypeOperator',
1659
- types: listType
1660
- };
1661
- return resultType;
1662
- }
1663
- if (simpleExpr instanceof ts_mysql_parser_1.SimpleExprSubQueryContext) {
1664
- const subquery = simpleExpr.subquery();
1665
- const subqueryType = walkSubquery(context, subquery);
1666
- return subqueryType;
1667
- }
1668
- if (simpleExpr instanceof ts_mysql_parser_1.SimpleExprCaseContext) {
1669
- //case when expr then expr else expr
1670
- const caseType = freshVar(simpleExpr.text, '?');
1671
- simpleExpr.whenExpression().forEach(whenExprCont => {
1672
- const whenExpr = whenExprCont.expr();
1673
- const whenType = walkExpr(context, whenExpr);
1674
- context.constraints.push({
1675
- expression: whenExpr.text,
1676
- type1: whenType.kind == 'TypeOperator' ? whenType.types[0] : whenType,
1677
- type2: freshVar('tinyint', 'tinyint') //bool
1678
- });
1679
- });
1680
- const thenTypes = simpleExpr.thenExpression().map(thenExprCtx => {
1681
- const thenExpr = thenExprCtx.expr();
1682
- const thenType = walkExpr(context, thenExpr);
1683
- context.constraints.push({
1684
- expression: thenExprCtx.text,
1685
- type1: caseType,
1686
- type2: thenType.kind == 'TypeOperator' ? thenType.types[0] : thenType,
1687
- mostGeneralType: true,
1688
- });
1689
- return thenType;
1690
- });
1691
- const elseExpr = (_p = simpleExpr.elseExpression()) === null || _p === void 0 ? void 0 : _p.expr();
1692
- if (elseExpr) {
1693
- const elseType = walkExpr(context, elseExpr);
1694
- context.constraints.push({
1695
- expression: (_q = simpleExpr.elseExpression()) === null || _q === void 0 ? void 0 : _q.text,
1696
- type1: caseType,
1697
- type2: elseType.kind == 'TypeOperator' ? elseType.types[0] : elseType,
1698
- mostGeneralType: true
1699
- });
1700
- thenTypes.forEach(thenType => {
1701
- var _a;
1702
- context.constraints.push({
1703
- expression: (_a = simpleExpr.elseExpression()) === null || _a === void 0 ? void 0 : _a.text,
1704
- type1: thenType,
1705
- type2: elseType.kind == 'TypeOperator' ? elseType.types[0] : elseType,
1706
- mostGeneralType: true
1707
- });
1708
- });
1709
- }
1710
- return caseType;
1711
- }
1712
- if (simpleExpr instanceof ts_mysql_parser_1.SimpleExprIntervalContext) {
1713
- const exprList = simpleExpr.expr();
1714
- const exprLeft = exprList[0];
1715
- const exprRight = exprList[1];
1716
- const typeLeft = walkExpr(context, exprLeft);
1717
- const typeRight = walkExpr(context, exprRight);
1718
- context.constraints.push({
1719
- expression: exprLeft.text,
1720
- type1: typeLeft,
1721
- type2: freshVar('bigint', 'bigint')
1722
- });
1723
- if (typeRight.kind == 'TypeVar' && (isDateLiteral(typeRight.name) || isDateTimeLiteral(typeRight.name))) {
1724
- typeRight.type = 'datetime';
1725
- }
1726
- context.constraints.push({
1727
- expression: exprRight.text,
1728
- type1: typeRight,
1729
- type2: freshVar('datetime', 'datetime')
1730
- });
1731
- return freshVar('datetime', 'datetime');
1732
- }
1733
- if (simpleExpr instanceof ts_mysql_parser_1.SimpleExprWindowingFunctionContext) {
1734
- const windowFunctionCall = simpleExpr.windowFunctionCall();
1735
- return walkWindowFunctionCall(windowFunctionCall, context);
1736
- }
1737
- if (simpleExpr instanceof ts_mysql_parser_1.SimpleExprCastContext) {
1738
- const castType = simpleExpr.castType();
1739
- if (castType.CHAR_SYMBOL()) {
1740
- return freshVar(castType.text, 'char');
1741
- }
1742
- }
1743
- throw Error('Invalid expression: ' + simpleExpr.text);
1744
- }
1745
- function walkWindowFunctionCall(windowFunctionCall, context) {
1746
- if (windowFunctionCall.ROW_NUMBER_SYMBOL()
1747
- || windowFunctionCall.RANK_SYMBOL()
1748
- || windowFunctionCall.DENSE_RANK_SYMBOL()
1749
- || windowFunctionCall.CUME_DIST_SYMBOL()
1750
- || windowFunctionCall.PERCENT_RANK_SYMBOL()) {
1751
- return freshVar(windowFunctionCall.text, 'bigint');
1752
- }
1753
- const expr = windowFunctionCall.expr();
1754
- if (expr) {
1755
- return walkExpr(context, expr);
1756
- }
1757
- const exprWithParentheses = windowFunctionCall.exprWithParentheses();
1758
- if (exprWithParentheses) {
1759
- const expr = exprWithParentheses.expr();
1760
- return walkExpr(context, expr);
1761
- }
1762
- throw Error('No support for expression' + windowFunctionCall.text);
1763
- }
113
+ exports.getVarType = getVarType;
1764
114
  function verifyDateTypesCoercion(type) {
1765
115
  if (type.kind == 'TypeVar' && isDateTimeLiteral(type.name)) {
1766
116
  type.type = 'datetime';
@@ -1773,91 +123,23 @@ function verifyDateTypesCoercion(type) {
1773
123
  }
1774
124
  return type;
1775
125
  }
126
+ exports.verifyDateTypesCoercion = verifyDateTypesCoercion;
1776
127
  function isTimeLiteral(literal) {
1777
128
  return (0, moment_1.default)(literal, 'HH:mm:ss', true).isValid() || (0, moment_1.default)(literal, 'HH:mm', true).isValid();
1778
129
  }
130
+ exports.isTimeLiteral = isTimeLiteral;
1779
131
  function isDateTimeLiteral(literal) {
1780
132
  return (0, moment_1.default)(literal, 'YYYY-MM-DD HH:mm:ss', true).isValid();
1781
133
  }
134
+ exports.isDateTimeLiteral = isDateTimeLiteral;
1782
135
  function isDateLiteral(literal) {
1783
136
  return (0, moment_1.default)(literal, "YYYY-MM-DD", true).isValid();
1784
137
  }
138
+ exports.isDateLiteral = isDateLiteral;
1785
139
  function getFunctionName(simpleExprFunction) {
1786
140
  var _a, _b;
1787
141
  return ((_a = simpleExprFunction.functionCall().pureIdentifier()) === null || _a === void 0 ? void 0 : _a.text.toLowerCase())
1788
142
  || ((_b = simpleExprFunction.functionCall().qualifiedIdentifier()) === null || _b === void 0 ? void 0 : _b.text.toLowerCase());
1789
143
  }
1790
- function walkExprListParameters(context, exprList, params) {
1791
- return exprList.map((expr, paramIndex) => {
1792
- const exprType = walkExpr(context, expr);
1793
- const paramType = params.kind == 'FixedLengthParams' ? params.paramsType[paramIndex] : freshVar(params.paramType, params.paramType);
1794
- context.constraints.push({
1795
- expression: expr.text,
1796
- type1: exprType,
1797
- type2: paramType,
1798
- mostGeneralType: true
1799
- });
1800
- return paramType;
1801
- });
1802
- }
1803
- function walkFunctionParameters(context, simpleExprFunction, params) {
1804
- var _a, _b;
1805
- const functionName = getFunctionName(simpleExprFunction);
1806
- const udfExprList = (_a = simpleExprFunction.functionCall().udfExprList()) === null || _a === void 0 ? void 0 : _a.udfExpr();
1807
- if (udfExprList) {
1808
- const paramTypes = udfExprList
1809
- .filter((undefined, paramIndex) => {
1810
- return functionName == 'timestampdiff' ? paramIndex != 0 : true; //filter the first parameter of timestampdiff function
1811
- })
1812
- .map((inExpr, paramIndex) => {
1813
- const expr = inExpr.expr();
1814
- const exprType = walkExpr(context, expr);
1815
- context.constraints.push({
1816
- expression: expr.text,
1817
- type1: exprType,
1818
- type2: params.kind == 'FixedLengthParams' ? params.paramsType[paramIndex] : freshVar(params.paramType, params.paramType),
1819
- mostGeneralType: true
1820
- });
1821
- return exprType;
1822
- });
1823
- return paramTypes;
1824
- }
1825
- const exprList = (_b = simpleExprFunction.functionCall().exprList()) === null || _b === void 0 ? void 0 : _b.expr();
1826
- if (exprList) {
1827
- const paramTypes = exprList.map((inExpr, paramIndex) => {
1828
- const inSumExprType = walkExpr(context, inExpr);
1829
- context.constraints.push({
1830
- expression: inExpr.text,
1831
- type1: params.kind == 'FixedLengthParams' ? params.paramsType[paramIndex] : freshVar(params.paramType, params.paramType),
1832
- type2: inSumExprType,
1833
- mostGeneralType: true
1834
- });
1835
- return inSumExprType;
1836
- });
1837
- return paramTypes;
1838
- }
1839
- throw Error('Error in walkFunctionParameters');
1840
- }
1841
- function walkSubquery(context, queryExpressionParens) {
1842
- const querySpec = (0, parse_1.getQuerySpecificationsFromSelectStatement)(queryExpressionParens);
1843
- const subqueryColumns = (0, select_columns_1.getColumnsFrom)(querySpec[0], context.dbSchema, context.withSchema);
1844
- const newContext = Object.assign(Object.assign({}, context), { fromColumns: context.fromColumns.concat(subqueryColumns) });
1845
- const typeInferResult = walkQuerySpecification(newContext, querySpec[0]);
1846
- for (let queryIndex = 1; queryIndex < querySpec.length; queryIndex++) { //union (if have any)
1847
- const unionColumns = (0, select_columns_1.getColumnsFrom)(querySpec[queryIndex], context.dbSchema, context.withSchema);
1848
- const unionNewContext = Object.assign(Object.assign({}, context), { fromColumns: context.fromColumns.concat(unionColumns) });
1849
- const unionResult = walkQuerySpecification(unionNewContext, querySpec[queryIndex]);
1850
- typeInferResult.types.forEach((field, fieldIndex) => {
1851
- context.constraints.push({
1852
- expression: querySpec[queryIndex].text,
1853
- type1: typeInferResult.types[fieldIndex],
1854
- type2: unionResult.types[fieldIndex],
1855
- mostGeneralType: true
1856
- });
1857
- });
1858
- }
1859
- //Should retrun the union result type, not the first query from result
1860
- return typeInferResult;
1861
- }
1862
- exports.walkSubquery = walkSubquery;
144
+ exports.getFunctionName = getFunctionName;
1863
145
  //# sourceMappingURL=collect-constraints.js.map