typesql-cli
Advanced tools
Comparing version 0.8.22 to 0.8.23
@@ -40,3 +40,3 @@ "use strict"; | ||
substitutions[ty2.id] = Object.assign(Object.assign({}, ty1), { list: ty2.list, type: newType }); | ||
substitutions[ty1.id] = Object.assign(Object.assign({}, ty1), { list: ty2.list, type: newType }); | ||
substitutions[ty1.id] = Object.assign(Object.assign({}, ty1), { list: ty1.list, type: newType }); | ||
} | ||
@@ -104,3 +104,3 @@ } | ||
// Is possible to convert text to date | ||
const sqliteNumberCoercionOrder = ['INTEGER', 'REAL']; | ||
const sqliteNumberCoercionOrder = ['INTEGER', 'REAL', 'NUMERIC']; | ||
const sqliteIndexNumberType1 = sqliteNumberCoercionOrder.indexOf(type1); | ||
@@ -107,0 +107,0 @@ const sqliteIndexNumberType2 = sqliteNumberCoercionOrder.indexOf(type2); |
{ | ||
"name": "typesql-cli", | ||
"version": "0.8.22", | ||
"version": "0.8.23", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -256,4 +256,4 @@ "use strict"; | ||
functionArguments += tsDescriptor.parameters.length > 0 || generateOrderBy ? ', params: ' + paramsTypeName : ''; | ||
const allParameters = (((_a = tsDescriptor.data) === null || _a === void 0 ? void 0 : _a.map((param) => toParamValue('data', param))) || []) | ||
.concat(tsDescriptor.parameters.map(param => toParamValue('params', param))); | ||
const allParameters = (((_a = tsDescriptor.data) === null || _a === void 0 ? void 0 : _a.map((param) => fromDriver('data', param))) || []) | ||
.concat(tsDescriptor.parameters.map(param => fromDriver('params', param))); | ||
const queryParams = allParameters.length > 0 ? '[' + allParameters.join(', ') + ']' : ''; | ||
@@ -345,3 +345,3 @@ const orNull = queryType == 'Select' ? ' | null' : ''; | ||
const separator = index < tsDescriptor.columns.length - 1 ? ',' : ''; | ||
writer.writeLine(`${col.name}: data[${index}]${separator}`); | ||
writer.writeLine(`${col.name}: ${toDriver(`data[${index}]`, col)}${separator}`); | ||
}); | ||
@@ -531,3 +531,9 @@ }); | ||
} | ||
function toParamValue(variableName, param) { | ||
function toDriver(variableData, param) { | ||
if (param.tsType == 'Date') { | ||
return `new Date(${variableData})`; | ||
} | ||
return variableData; | ||
} | ||
function fromDriver(variableName, param) { | ||
var _a; | ||
@@ -534,0 +540,0 @@ if (param.tsType == 'Date') { |
@@ -46,14 +46,17 @@ "use strict"; | ||
exports.tryTraverse_Sql_stmtContext = tryTraverse_Sql_stmtContext; | ||
function traverse_select_stmt(select_stmt, traverseContext, subQuery = false) { | ||
function traverse_select_stmt(select_stmt, traverseContext, subQuery = false, recursive = false, recursiveNames = []) { | ||
const common_table_stmt = select_stmt.common_table_stmt(); | ||
if (common_table_stmt) { | ||
const recursive = common_table_stmt.RECURSIVE_() != null; | ||
const common_table_expression = common_table_stmt.common_table_expression_list(); | ||
common_table_expression.forEach(common_table_expression => { | ||
const table_name = common_table_expression.table_name(); | ||
const recursiveNames = common_table_expression.column_name_list().map(column_name => column_name.getText()); | ||
const select_stmt = common_table_expression.select_stmt(); | ||
const select_stmt_result = traverse_select_stmt(select_stmt, traverseContext); | ||
select_stmt_result.columns.forEach(col => { | ||
const select_stmt_result = traverse_select_stmt(select_stmt, traverseContext, subQuery, recursive, recursiveNames); | ||
select_stmt_result.columns.forEach((col, index) => { | ||
var _a; | ||
traverseContext.withSchema.push({ | ||
table: table_name.getText(), | ||
columnName: col.name, | ||
columnName: recursive ? (_a = recursiveNames[index]) !== null && _a !== void 0 ? _a : col.name : col.name, | ||
columnType: col.type, | ||
@@ -66,99 +69,20 @@ columnKey: '', | ||
} | ||
const select_coreList = select_stmt.select_core_list(); | ||
const querySpecResult = select_coreList.map(select_core => { | ||
const columnsResult = []; | ||
const listType = []; | ||
const table_or_subquery = select_core.table_or_subquery_list(); | ||
if (table_or_subquery) { | ||
const fields = traverse_table_or_subquery(table_or_subquery, null, null, traverseContext); | ||
columnsResult.push(...fields); | ||
} | ||
const join_clause = select_core.join_clause(); | ||
if (join_clause) { | ||
const join_table_or_subquery = join_clause.table_or_subquery_list(); | ||
const join_constraint_list = join_clause.join_constraint_list(); | ||
const join_operator_list = join_clause.join_operator_list(); | ||
const fields = traverse_table_or_subquery(join_table_or_subquery, join_constraint_list, join_operator_list, traverseContext); | ||
columnsResult.push(...fields); | ||
} | ||
const result_column = select_core.result_column_list(); | ||
const fromColumns = subQuery ? traverseContext.fromColumns.concat(columnsResult) : columnsResult; | ||
result_column.forEach(result_column => { | ||
var _a, _b; | ||
if (result_column.STAR()) { | ||
const tableName = (_a = result_column.table_name()) === null || _a === void 0 ? void 0 : _a.getText(); | ||
columnsResult.forEach(col => { | ||
if (!tableName || (0, select_columns_1.includeColumn)(col, tableName)) { | ||
const columnType = (0, collect_constraints_1.createColumnType)(col); | ||
listType.push({ | ||
name: columnType.name, | ||
type: columnType, | ||
notNull: col.notNull, | ||
table: col.tableAlias || col.table | ||
}); | ||
} | ||
}); | ||
} | ||
const expr = result_column.expr(); | ||
const alias = (_b = result_column.column_alias()) === null || _b === void 0 ? void 0 : _b.getText(); | ||
if (expr) { | ||
const exprType = traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns: fromColumns })); | ||
if (alias) { | ||
traverseContext.relations.filter(relation => relation.joinColumn == exprType.name && (relation.name == exprType.table || relation.alias == exprType.table)).forEach(relation => { | ||
relation.joinColumn = alias; | ||
}); | ||
} | ||
if (exprType.type.kind == 'TypeVar') { | ||
if (alias) { | ||
exprType.name = alias; | ||
} | ||
listType.push(exprType); | ||
} | ||
} | ||
}); | ||
const whereExpr = select_core._whereExpr; | ||
if (whereExpr) { | ||
traverse_expr(whereExpr, Object.assign(Object.assign({}, traverseContext), { fromColumns: fromColumns })); | ||
} | ||
const groupByExprList = select_core._groupByExpr || []; | ||
groupByExprList.forEach(groupByExpr => { | ||
traverse_expr(groupByExpr, Object.assign(Object.assign({}, traverseContext), { fromColumns: fromColumns })); | ||
}); | ||
const havingExpr = select_core._havingExpr; | ||
if (havingExpr) { | ||
const newColumns = listType.map(selectField => { | ||
const col = { | ||
columnName: selectField.name, | ||
table: selectField.table, | ||
columnType: selectField.type, | ||
notNull: selectField.notNull, | ||
columnKey: "" | ||
}; | ||
return col; | ||
}); | ||
//select have precedence: newColumns.concat(fromColumns) | ||
traverse_expr(havingExpr, Object.assign(Object.assign({}, traverseContext), { fromColumns: newColumns.concat(fromColumns) })); | ||
} | ||
const querySpecification = { | ||
columns: listType.map(col => (Object.assign(Object.assign({}, col), { notNull: col.notNull || isNotNull(col.name, whereExpr) || isNotNull(col.name, havingExpr) }))), | ||
fromColumns: columnsResult //TODO - return isMultipleRowResult instead | ||
}; | ||
return querySpecification; | ||
}); | ||
const mainQuery = querySpecResult[0]; | ||
for (let queryIndex = 1; queryIndex < querySpecResult.length; queryIndex++) { //UNION | ||
const unionQuery = querySpecResult[queryIndex]; | ||
unionQuery.columns.forEach((col, colIndex) => { | ||
mainQuery.columns[colIndex].table = ''; | ||
const [mainSelect, ...unionSelect] = select_stmt.select_core_list(); | ||
const mainQueryResult = traverse_select_core(mainSelect, traverseContext, subQuery, recursive, recursiveNames); | ||
unionSelect.forEach(select_core => { | ||
const fromColumns = recursive ? mainQueryResult.columns.map((col, index) => mapTypeAndNullInferToColumnDef(col, recursiveNames[index])) : traverseContext.fromColumns; | ||
const unionResult = traverse_select_core(select_core, Object.assign(Object.assign({}, traverseContext), { fromColumns }), subQuery, recursive); | ||
unionResult.columns.forEach((col, colIndex) => { | ||
mainQueryResult.columns[colIndex].table = ''; | ||
traverseContext.constraints.push({ | ||
expression: 'UNION', | ||
type1: mainQuery.columns[colIndex].type, | ||
type1: mainQueryResult.columns[colIndex].type, | ||
type2: col.type | ||
}); | ||
}); | ||
} | ||
}); | ||
const selectResult = { | ||
queryType: 'Select', | ||
columns: mainQuery.columns, | ||
multipleRowsResult: isMultipleRowResult(select_stmt, mainQuery.fromColumns), | ||
columns: mainQueryResult.columns, | ||
multipleRowsResult: isMultipleRowResult(select_stmt, mainQueryResult.fromColumns), | ||
relations: traverseContext.relations | ||
@@ -176,7 +100,7 @@ }; | ||
else { | ||
traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns: mainQuery.fromColumns })); | ||
traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns: mainQueryResult.fromColumns })); | ||
} | ||
}); | ||
if (hasOrderByParameter) { | ||
const orderByColumns = (0, traverse_1.getOrderByColumns)(mainQuery.fromColumns, mainQuery.columns); | ||
const orderByColumns = (0, traverse_1.getOrderByColumns)(mainQueryResult.fromColumns, mainQueryResult.columns); | ||
selectResult.orderByColumns = orderByColumns; | ||
@@ -209,2 +133,91 @@ } | ||
} | ||
function mapTypeAndNullInferToColumnDef(col, name) { | ||
return { | ||
columnName: name !== null && name !== void 0 ? name : col.name, | ||
columnType: col.type, | ||
notNull: col.notNull, | ||
columnKey: '', | ||
table: col.table, | ||
tableAlias: col.table | ||
}; | ||
} | ||
function traverse_select_core(select_core, traverseContext, subQuery = false, recursive = false, recursiveName) { | ||
const columnsResult = []; | ||
const listType = []; | ||
const table_or_subquery = select_core.table_or_subquery_list(); | ||
if (table_or_subquery) { | ||
const fields = traverse_table_or_subquery(table_or_subquery, null, null, traverseContext); | ||
columnsResult.push(...fields); | ||
} | ||
const join_clause = select_core.join_clause(); | ||
if (join_clause) { | ||
const join_table_or_subquery = join_clause.table_or_subquery_list(); | ||
const join_constraint_list = join_clause.join_constraint_list(); | ||
const join_operator_list = join_clause.join_operator_list(); | ||
const fields = traverse_table_or_subquery(join_table_or_subquery, join_constraint_list, join_operator_list, traverseContext); | ||
columnsResult.push(...fields); | ||
} | ||
const result_column = select_core.result_column_list(); | ||
const fromColumns = subQuery || recursive ? traverseContext.fromColumns.concat(columnsResult) : columnsResult; | ||
result_column.forEach(result_column => { | ||
var _a, _b; | ||
if (result_column.STAR()) { | ||
const tableName = (_a = result_column.table_name()) === null || _a === void 0 ? void 0 : _a.getText(); | ||
columnsResult.forEach(col => { | ||
if (!tableName || (0, select_columns_1.includeColumn)(col, tableName)) { | ||
listType.push({ | ||
name: col.columnName, | ||
type: col.columnType, | ||
notNull: col.notNull, | ||
table: col.tableAlias || col.table | ||
}); | ||
} | ||
}); | ||
} | ||
const expr = result_column.expr(); | ||
const alias = (_b = result_column.column_alias()) === null || _b === void 0 ? void 0 : _b.getText(); | ||
if (expr) { | ||
const exprType = traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns: fromColumns })); | ||
if (alias) { | ||
traverseContext.relations.filter(relation => relation.joinColumn == exprType.name && (relation.name == exprType.table || relation.alias == exprType.table)).forEach(relation => { | ||
relation.joinColumn = alias; | ||
}); | ||
} | ||
if (exprType.type.kind == 'TypeVar') { | ||
if (alias) { | ||
exprType.name = alias; | ||
} | ||
listType.push(exprType); | ||
} | ||
} | ||
}); | ||
const whereExpr = select_core._whereExpr; | ||
if (whereExpr) { | ||
traverse_expr(whereExpr, Object.assign(Object.assign({}, traverseContext), { fromColumns: fromColumns })); | ||
} | ||
const groupByExprList = select_core._groupByExpr || []; | ||
groupByExprList.forEach(groupByExpr => { | ||
traverse_expr(groupByExpr, Object.assign(Object.assign({}, traverseContext), { fromColumns: fromColumns })); | ||
}); | ||
const havingExpr = select_core._havingExpr; | ||
if (havingExpr) { | ||
const newColumns = listType.map(selectField => { | ||
const col = { | ||
columnName: selectField.name, | ||
table: selectField.table, | ||
columnType: selectField.type, | ||
notNull: selectField.notNull, | ||
columnKey: "" | ||
}; | ||
return col; | ||
}); | ||
//select have precedence: newColumns.concat(fromColumns) | ||
traverse_expr(havingExpr, Object.assign(Object.assign({}, traverseContext), { fromColumns: newColumns.concat(fromColumns) })); | ||
} | ||
const querySpecification = { | ||
columns: listType.map(col => (Object.assign(Object.assign({}, col), { notNull: col.notNull || isNotNull(col.name, whereExpr) || isNotNull(col.name, havingExpr) }))), | ||
fromColumns: columnsResult //TODO - return isMultipleRowResult instead | ||
}; | ||
return querySpecification; | ||
} | ||
function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list, join_operator_list, traverseContext) { | ||
@@ -299,3 +312,3 @@ const allFields = []; | ||
function traverse_expr(expr, traverseContext) { | ||
var _a; | ||
var _a, _b; | ||
const function_name = (_a = expr.function_name()) === null || _a === void 0 ? void 0 : _a.getText().toLowerCase(); | ||
@@ -378,2 +391,21 @@ if (function_name == 'avg') { | ||
} | ||
if (function_name == 'length') { | ||
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER'); | ||
const paramExpr = expr.expr(0); | ||
const paramType = traverse_expr(paramExpr, traverseContext); | ||
traverseContext.constraints.push({ | ||
expression: expr.getText(), | ||
type1: (0, collect_constraints_1.freshVar)(expr.getText(), '?'), //str or blob | ||
type2: paramType.type | ||
}); | ||
if (paramType.type.kind == 'TypeVar') { | ||
functionType.table = paramType.table; | ||
} | ||
return { | ||
name: functionType.name, | ||
type: functionType, | ||
notNull: false, | ||
table: functionType.table || '' | ||
}; | ||
} | ||
if (function_name == 'group_concat') { | ||
@@ -456,3 +488,3 @@ expr.expr_list().forEach(paramExpr => { | ||
if (function_name == 'date' || function_name == 'time' || function_name == 'datetime') { | ||
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'TEXT'); | ||
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'DATE'); | ||
const paramExpr = expr.expr(0); | ||
@@ -473,2 +505,36 @@ const paramType = traverse_expr(paramExpr, traverseContext); | ||
} | ||
if (function_name == 'julianday') { | ||
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'REAL'); | ||
const paramExpr = expr.expr(0); | ||
const notNull = paramExpr.getText().toLowerCase() == `'now'` ? true : false; | ||
const paramType = traverse_expr(paramExpr, traverseContext); | ||
traverseContext.constraints.push({ | ||
expression: paramExpr.getText(), | ||
type1: (0, collect_constraints_1.freshVar)(paramExpr.getText(), 'DATE'), | ||
type2: paramType.type | ||
}); | ||
return { | ||
name: functionType.name, | ||
type: functionType, | ||
notNull, | ||
table: functionType.table || '' | ||
}; | ||
} | ||
if (function_name == 'unixepoch') { | ||
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER'); | ||
const paramExpr = expr.expr(0); | ||
const notNull = paramExpr.getText().toLowerCase() == `'now'` ? true : false; | ||
const paramType = traverse_expr(paramExpr, traverseContext); | ||
traverseContext.constraints.push({ | ||
expression: paramExpr.getText(), | ||
type1: (0, collect_constraints_1.freshVar)(paramExpr.getText(), 'DATE'), | ||
type2: paramType.type | ||
}); | ||
return { | ||
name: functionType.name, | ||
type: functionType, | ||
notNull, | ||
table: functionType.table || '' | ||
}; | ||
} | ||
if (function_name == 'ifnull') { | ||
@@ -673,3 +739,3 @@ const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), '?'); | ||
if (expr.PLUS() || expr.MINUS()) { | ||
const returnType = (0, collect_constraints_1.freshVar)(expr.getText(), 'REAL'); | ||
const returnType = (0, collect_constraints_1.freshVar)(expr.getText(), 'REAL'); //NUMERIC | ||
const exprLeft = expr.expr(0); | ||
@@ -679,2 +745,4 @@ const exprRight = expr.expr(1); | ||
const typeRight = traverse_expr(exprRight, traverseContext); | ||
typeLeft.table = ''; | ||
typeRight.table = ''; | ||
traverseContext.constraints.push({ | ||
@@ -685,8 +753,12 @@ expression: exprLeft.getText(), | ||
}); | ||
traverseContext.constraints.push({ | ||
expression: exprRight.getText(), | ||
type1: returnType, | ||
type2: typeRight.type | ||
}); | ||
return Object.assign(Object.assign({}, typeRight), { notNull: typeLeft.notNull && typeRight.notNull }); | ||
const isDateFunctionContext = (expr.parentCtx instanceof sqlite_1.ExprContext) | ||
&& ((_b = expr.parentCtx.function_name()) === null || _b === void 0 ? void 0 : _b.getText().toLowerCase()) == 'date'; | ||
if (!isDateFunctionContext) { | ||
traverseContext.constraints.push({ | ||
expression: exprRight.getText(), | ||
type1: returnType, | ||
type2: typeRight.type | ||
}); | ||
} | ||
return Object.assign(Object.assign({}, typeLeft), { notNull: typeLeft.notNull && typeRight.notNull }); | ||
} | ||
@@ -693,0 +765,0 @@ if (expr.LT2() || expr.GT2() || expr.AMP() || expr.PIPE() || expr.LT() || expr.LT_EQ() || expr.GT() || expr.GT_EQ() || expr.NOT_EQ1() || expr.NOT_EQ2()) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
721435
8852