typesql-cli
Advanced tools
Comparing version 0.5.9 to 0.5.10
import { RuleContext } from "antlr4ts"; | ||
import { QuerySpecificationContext, SelectStatementContext, SubqueryContext, InsertStatementContext, UpdateStatementContext, DeleteStatementContext } from "ts-mysql-parser"; | ||
import { ColumnSchema, ColumnDef, TypeInferenceResult, InsertInfoResult, UpdateInfoResult, DeleteInfoResult } from "./types"; | ||
import { ColumnSchema, ColumnDef, TypeInferenceResult, InsertInfoResult, UpdateInfoResult, DeleteInfoResult, TypeAndNullInfer } from "./types"; | ||
import { InferType } from "../mysql-mapping"; | ||
@@ -30,3 +30,3 @@ export type TypeVar = { | ||
dbSchema: ColumnSchema[]; | ||
withSchema: ColumnSchema[]; | ||
withSchema: TypeAndNullInfer[]; | ||
parameters: TypeVar[]; | ||
@@ -43,10 +43,10 @@ constraints: Constraint[]; | ||
export declare function analiseInsertStatement(insertStatement: InsertStatementContext, context: InferenceContext): InsertInfoResult; | ||
export declare function analiseDeleteStatement(deleteStatement: DeleteStatementContext, dbSchema: ColumnSchema[], withSchema: ColumnSchema[]): DeleteInfoResult; | ||
export declare function analiseUpdateStatement(updateStatement: UpdateStatementContext, dbSchema: ColumnSchema[], withSchema: ColumnSchema[]): UpdateInfoResult; | ||
export declare function analiseDeleteStatement(deleteStatement: DeleteStatementContext, dbSchema: ColumnSchema[], withSchema: TypeAndNullInfer[]): DeleteInfoResult; | ||
export declare function analiseUpdateStatement(updateStatement: UpdateStatementContext, dbSchema: ColumnSchema[], withSchema: TypeAndNullInfer[]): UpdateInfoResult; | ||
export declare function getInsertColumns(insertStatement: InsertStatementContext, dbSchema: ColumnSchema[]): ColumnSchema[]; | ||
export declare function getUpdateColumns(updateStatement: UpdateStatementContext, dbSchema: ColumnSchema[]): ColumnDef[]; | ||
export declare function getDeleteColumns(deleteStatement: DeleteStatementContext, dbSchema: ColumnSchema[]): ColumnDef[]; | ||
export declare function analiseSelectStatement(selectStatement: SelectStatementContext | SubqueryContext, dbSchema: ColumnSchema[], withSchema: ColumnSchema[], namedParameters: string[]): TypeInferenceResult; | ||
export declare function analiseSelectStatement(selectStatement: SelectStatementContext | SubqueryContext, dbSchema: ColumnSchema[], withSchema: TypeAndNullInfer[], namedParameters: string[]): TypeInferenceResult; | ||
export declare function unionTypeResult(type1: InferType, type2: InferType): any; | ||
export declare function analiseQuerySpecification(querySpec: QuerySpecificationContext, dbSchema: ColumnSchema[], withSchema: ColumnSchema[], fromColumns: ColumnDef[], namedParameters: string[]): TypeInferenceResult; | ||
export declare function analiseQuerySpecification(querySpec: QuerySpecificationContext, dbSchema: ColumnSchema[], withSchema: TypeAndNullInfer[], fromColumns: ColumnDef[], namedParameters: string[]): TypeInferenceResult; | ||
export declare function generateTypeInfo(namedNodes: TypeVar[], constraints: Constraint[]): InferType[]; | ||
@@ -53,0 +53,0 @@ export declare function walkQuerySpecification(context: InferenceContext, querySpec: QuerySpecificationContext): TypeOperator; |
import { QuerySpecificationContext, ExprContext } from "ts-mysql-parser"; | ||
import { ColumnSchema, FieldName } from "./types"; | ||
export declare function parseAndInferNotNull(sql: string, dbSchema: ColumnSchema[], withSchema?: ColumnSchema[]): boolean[] | undefined; | ||
export declare function inferNotNull(querySpec: QuerySpecificationContext, dbSchema: ColumnSchema[], withSchema: ColumnSchema[]): boolean[]; | ||
import { ColumnSchema, FieldName, TypeAndNullInfer } from "./types"; | ||
export declare function parseAndInferNotNull(sql: string, dbSchema: ColumnSchema[], withSchema?: TypeAndNullInfer[]): boolean[] | undefined; | ||
export declare function inferNotNull(querySpec: QuerySpecificationContext, dbSchema: ColumnSchema[], withSchema: TypeAndNullInfer[]): boolean[]; | ||
export declare function possibleNull(field: FieldName, exprContext: ExprContext): boolean; | ||
//# sourceMappingURL=infer-column-nullability.d.ts.map |
@@ -109,6 +109,4 @@ "use strict"; | ||
const bitExpr2 = bitExpr.bitExpr(); | ||
if (bitExpr2.length == 2) { | ||
return inferNotNullBitExpr(bitExpr2[0], dbSchema, fromColumns) && inferNotNullBitExpr(bitExpr2[1], dbSchema, fromColumns); | ||
} | ||
throw Error('Error during column null inference'); | ||
const notNull = bitExpr2.every(bitExprItem => inferNotNullBitExpr(bitExprItem, dbSchema, fromColumns)); | ||
return notNull; | ||
} | ||
@@ -186,3 +184,7 @@ function inferNotNullSimpleExpr(simpleExpr, dbSchema, fromColumns) { | ||
} | ||
throw Error('Error during column null inference'); | ||
if (simpleExpr instanceof ts_mysql_parser_1.SimpleExprCastContext) { | ||
const expr = simpleExpr.expr(); | ||
return inferNotNullExpr(expr, dbSchema, fromColumns); | ||
} | ||
throw Error('Error during column null inference. Expr: ' + simpleExpr.text); | ||
} | ||
@@ -189,0 +191,0 @@ function inferNotNullWindowFunctionCall(windowFunctionCall, dbSchema, fromColumns) { |
import { QueryContext, QuerySpecificationContext, SelectStatementContext, SubqueryContext, QueryExpressionParensContext, QueryExpressionBodyContext, InsertQueryExpressionContext } from 'ts-mysql-parser'; | ||
import { TypeVar } from './collect-constraints'; | ||
import { ColumnSchema, TypeInferenceResult, QueryInfoResult, InsertInfoResult, UpdateInfoResult, DeleteInfoResult, TypeAndNullInferResult } from './types'; | ||
import { ColumnSchema, TypeInferenceResult, QueryInfoResult, InsertInfoResult, UpdateInfoResult, DeleteInfoResult, TypeAndNullInferResult, TypeAndNullInfer } from './types'; | ||
export declare function parse(sql: string): QueryContext; | ||
@@ -8,9 +8,9 @@ export type SubstitutionHash = { | ||
}; | ||
export declare function infer(queryContext: QueryContext, dbSchema: ColumnSchema[], withSchema: ColumnSchema[], namedParameters: string[]): TypeInferenceResult; | ||
export declare function parseAndInfer(sql: string, dbSchema: ColumnSchema[], withSchema?: ColumnSchema[]): TypeInferenceResult; | ||
export declare function infer(queryContext: QueryContext, dbSchema: ColumnSchema[], withSchema: TypeAndNullInfer[], namedParameters: string[]): TypeInferenceResult; | ||
export declare function parseAndInfer(sql: string, dbSchema: ColumnSchema[], withSchema?: TypeAndNullInfer[]): TypeInferenceResult; | ||
export declare function parseAndInferParamNullability(sql: string): boolean[]; | ||
export declare function extractQueryInfoFromQuerySpecification(querySpec: QuerySpecificationContext, dbSchema: ColumnSchema[], withSchema: ColumnSchema[], namedParameters: string[]): TypeAndNullInferResult; | ||
export declare function extractQueryInfoFromQuerySpecification(querySpec: QuerySpecificationContext, dbSchema: ColumnSchema[], withSchema: TypeAndNullInfer[], namedParameters: string[]): TypeAndNullInferResult; | ||
export declare function extractQueryInfo(sql: string, dbSchema: ColumnSchema[]): QueryInfoResult | InsertInfoResult | UpdateInfoResult | DeleteInfoResult; | ||
export declare function analiseQuery(querySpec: QuerySpecificationContext[], dbSchema: ColumnSchema[], withSchema: ColumnSchema[], namedParameters: string[]): TypeAndNullInferResult; | ||
export declare function analiseQuery(querySpec: QuerySpecificationContext[], dbSchema: ColumnSchema[], withSchema: TypeAndNullInfer[], namedParameters: string[], recursive?: boolean): TypeAndNullInferResult; | ||
export declare function getQuerySpecificationsFromSelectStatement(selectStatement: SelectStatementContext | InsertQueryExpressionContext | QueryExpressionBodyContext | QueryExpressionParensContext | SubqueryContext): QuerySpecificationContext[]; | ||
//# sourceMappingURL=parse.d.ts.map |
@@ -28,2 +28,3 @@ "use strict"; | ||
const ts_mysql_parser_1 = __importStar(require("ts-mysql-parser")); | ||
const tree_1 = require("antlr4ts/tree"); | ||
const collect_constraints_1 = require("./collect-constraints"); | ||
@@ -127,2 +128,13 @@ const select_columns_1 = require("./select-columns"); | ||
if (querySpecs.length == 1) { //UNION queries are multipleRowsResult = true | ||
if (querySpecs[0].selectItemList().childCount == 1) { | ||
const selectItem = querySpecs[0].selectItemList().getChild(0); | ||
//if selectItem = * (TerminalNode) childCount = 0; selectItem.expr() throws exception | ||
const expr = selectItem.childCount > 0 ? selectItem.expr() : null; | ||
if (expr) { | ||
const isSumExpress = isSumExpressContext(expr); //SUM, MAX... are multipleRowsResult = false | ||
if (isSumExpress) { | ||
return false; | ||
} | ||
} | ||
} | ||
const whereClauseExpr = (_a = querySpecs[0].whereClause()) === null || _a === void 0 ? void 0 : _a.expr(); | ||
@@ -143,2 +155,30 @@ const isMultipleRowResult = whereClauseExpr && (0, verify_multiple_result_1.verifyMultipleResult)(whereClauseExpr, fromColumns); | ||
} | ||
function isSumExpressContext(selectItem) { | ||
var _a; | ||
if (selectItem instanceof ts_mysql_parser_1.SimpleExprWindowingFunctionContext | ||
|| selectItem instanceof tree_1.TerminalNode) { | ||
return false; | ||
} | ||
if (selectItem instanceof ts_mysql_parser_1.SumExprContext) { | ||
if (selectItem.children) { | ||
//any of the children is WindowingClauseContext OVER() | ||
for (const child of selectItem.children) { | ||
if (child instanceof ts_mysql_parser_1.WindowingClauseContext) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
//https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html | ||
if (selectItem instanceof ts_mysql_parser_1.FunctionCallContext) { | ||
if (((_a = selectItem.qualifiedIdentifier()) === null || _a === void 0 ? void 0 : _a.text.toLowerCase()) == 'avg') { | ||
return true; | ||
} | ||
} | ||
if (selectItem.childCount == 1) { | ||
return isSumExpressContext(selectItem.getChild(0)); | ||
} | ||
return false; | ||
} | ||
function getLimitOptions(selectStatement) { | ||
@@ -175,11 +215,3 @@ var _a, _b; | ||
withClauseResults.forEach(withClauseResult => { | ||
const withSchema = withClauseResult.queryResult.columns.map(colDef => ({ | ||
schema: '', | ||
table: withClauseResult.identifier, | ||
column: colDef.name, | ||
column_type: colDef.type, | ||
columnKey: '', | ||
notNull: colDef.notNull | ||
})); | ||
allWithSchema.push(...withSchema); | ||
allWithSchema.push(...withClauseResult.queryResult.columns); | ||
withClauseParameters.push(...withClauseResult.queryResult.parameters); | ||
@@ -240,5 +272,8 @@ }); | ||
} | ||
function analiseQuery(querySpec, dbSchema, withSchema, namedParameters) { | ||
function analiseQuery(querySpec, dbSchema, withSchema, namedParameters, recursive = false) { | ||
const mainQueryResult = extractQueryInfoFromQuerySpecification(querySpec[0], dbSchema, withSchema, namedParameters); | ||
for (let queryIndex = 1; queryIndex < querySpec.length; queryIndex++) { //union (if have any) | ||
if (recursive) { | ||
withSchema.push(...mainQueryResult.columns); | ||
} | ||
const unionResult = extractQueryInfoFromQuerySpecification(querySpec[queryIndex], dbSchema, withSchema, namedParameters); | ||
@@ -274,6 +309,31 @@ mainQueryResult.columns.forEach((field, fieldIndex) => { | ||
function analyseWithClause(withClause, dbSchema, withSchema) { | ||
return withClause.commonTableExpression().map(commonTableExpression => { | ||
const result = withClause.commonTableExpression().map(commonTableExpression => { | ||
var _a; | ||
const identifier = commonTableExpression.identifier().text; | ||
const columnInternalRefList = ((_a = commonTableExpression.columnInternalRefList()) === null || _a === void 0 ? void 0 : _a.columnInternalRef()) || []; | ||
columnInternalRefList.forEach(colRef => { | ||
const identifier = colRef.identifier().text; | ||
withSchema.push({ | ||
name: identifier, | ||
notNull: true, | ||
type: '?' | ||
}); | ||
}); | ||
const subQuery = commonTableExpression.subquery(); | ||
const queryResult = (0, select_columns_1.analyzeSubQuery)(subQuery, dbSchema, withSchema); | ||
const recursive = withClause.RECURSIVE_SYMBOL() ? true : false; | ||
const queryResult = (0, select_columns_1.analyzeSubQuery)(subQuery, dbSchema, withSchema, recursive); | ||
if (recursive) { | ||
const newColumnNames = queryResult.columns.map((col, index) => { | ||
const newR = Object.assign(Object.assign({}, col), { name: withSchema[index].name }); | ||
return newR; | ||
}); | ||
const newQueryResult = { | ||
columns: newColumnNames, | ||
parameters: queryResult.parameters | ||
}; | ||
return { | ||
identifier, | ||
queryResult: newQueryResult | ||
}; | ||
} | ||
return { | ||
@@ -284,3 +344,4 @@ identifier, | ||
}); | ||
return result; | ||
} | ||
//# sourceMappingURL=parse.js.map |
import { RuleContext } from "antlr4ts"; | ||
import { ParseTree } from "antlr4ts/tree"; | ||
import { QuerySpecificationContext, SubqueryContext } from "ts-mysql-parser"; | ||
import { ColumnSchema, ColumnDef, FieldName } from "./types"; | ||
export declare function filterColumns(dbSchema: ColumnSchema[], withSchema: ColumnSchema[], tableAlias: string | undefined, table: FieldName): ColumnDef[]; | ||
import { ColumnSchema, ColumnDef, FieldName, TypeAndNullInfer } from "./types"; | ||
export declare function filterColumns(dbSchema: ColumnSchema[], withSchema: TypeAndNullInfer[], tableAlias: string | undefined, table: FieldName): ColumnDef[]; | ||
export declare function selectAllColumns(tablePrefix: string, fromColumns: ColumnDef[]): ColumnDef[]; | ||
export declare function getColumnNames(querySpec: QuerySpecificationContext, fromColumns: ColumnDef[]): string[]; | ||
export declare function getColumnsFrom(ctx: QuerySpecificationContext, dbSchema: ColumnSchema[], withSchema: ColumnSchema[]): ColumnDef[]; | ||
export declare function analyzeSubQuery(subQuery: SubqueryContext, dbSchema: ColumnSchema[], withSchema: ColumnSchema[]): import("./types").TypeAndNullInferResult; | ||
export declare function getColumnsFrom(ctx: QuerySpecificationContext, dbSchema: ColumnSchema[], withSchema: TypeAndNullInfer[]): ColumnDef[]; | ||
export declare function analyzeSubQuery(subQuery: SubqueryContext, dbSchema: ColumnSchema[], withSchema: TypeAndNullInfer[], recursive?: boolean): import("./types").TypeAndNullInferResult; | ||
export declare function splitName(fieldName: string): FieldName; | ||
@@ -11,0 +11,0 @@ export declare function findColumn(fieldName: FieldName, columns: ColumnDef[]): ColumnDef; |
@@ -10,5 +10,5 @@ "use strict"; | ||
function filterColumns(dbSchema, withSchema, tableAlias, table) { | ||
const tableColumns1 = dbSchema.filter(schema => schema.table.toLowerCase() == table.name.toLowerCase() && (schema.schema == table.prefix || table.prefix == '')); | ||
const tableColumns = [...tableColumns1, ...withSchema]; | ||
return tableColumns.map(tableColumn => { | ||
const tableColumns1 = dbSchema | ||
.filter(schema => schema.table.toLowerCase() == table.name.toLowerCase() && (schema.schema == table.prefix || table.prefix == '')) | ||
.map(tableColumn => { | ||
//name and colum are the same on the leaf table | ||
@@ -21,2 +21,15 @@ const r = { | ||
}); | ||
const withColumns = withSchema.map(col => { | ||
const r = { | ||
table: table.name, | ||
tableAlias: tableAlias || '', | ||
column: col.name, | ||
columnName: col.name, | ||
columnType: col.type, | ||
columnKey: "", | ||
notNull: col.notNull | ||
}; | ||
return r; | ||
}); | ||
return [...tableColumns1, ...withColumns]; | ||
} | ||
@@ -62,3 +75,3 @@ exports.filterColumns = filterColumns; | ||
exports.getColumnNames = getColumnNames; | ||
// TODO - withSchema: ColumnSchema[] DEFAULT VALUE [] | ||
// TODO - withSchema: TypeAndNullInfer[] DEFAULT VALUE [] | ||
function getColumnsFrom(ctx, dbSchema, withSchema) { | ||
@@ -205,5 +218,5 @@ var _a, _b; | ||
} | ||
function analyzeSubQuery(subQuery, dbSchema, withSchema) { | ||
function analyzeSubQuery(subQuery, dbSchema, withSchema, recursive = false) { | ||
const queries = (0, parse_1.getQuerySpecificationsFromSelectStatement)(subQuery); | ||
const queryResult = (0, parse_1.analiseQuery)(queries, dbSchema, withSchema, []); //TODO - WHY []? | ||
const queryResult = (0, parse_1.analiseQuery)(queries, dbSchema, withSchema, [], recursive); //TODO - WHY []? | ||
return queryResult; | ||
@@ -210,0 +223,0 @@ } |
{ | ||
"name": "typesql-cli", | ||
"version": "0.5.9", | ||
"version": "0.5.10", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
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
355780
4628