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