public static CodeExpression BuildFromCheckConstraintDefinition(string schemaName, DataTable table, string check_name, string check_definition, out bool hasError, out string errorText) { string select_statement = "SELECT colx = IIF((" + check_definition + "), 1, 0) FROM " + schemaName + ".[" + table.TableName + "] WHERE " + check_definition; TSqlFragment sqlF = ScriptDomFacade.Parse(select_statement); IIFVisitor iif_visitor = new IIFVisitor(); sqlF.Accept(iif_visitor); if (iif_visitor.result == null) { throw new NotSupportedException("Expression 'IIF' could not be found:" + sqlF.AsText()); } BooleanExpression iif_predicate = iif_visitor.result.Predicate; //if (iif_visitor != null) //{ // //return null; // comment out if it needed //} //Console.WriteLine(""); //Console.WriteLine("==== " + check_name + " ============"); //Console.WriteLine(ScriptDomFacade.GenerateScript(iif_predicate)); //Console.WriteLine("-------------------------------------------"); //Console.WriteLine(""); var databaseMetadata = new DatabaseMetadataOnTable(table); hasError = false; errorText = null; return(BooleanExpressionGeneratorVisitor.TryBuildFromFragment(databaseMetadata, iif_predicate, ref hasError, ref errorText)); }
public static void LoadViewOutputColumns(ISchemaMetadataProvider schemaMetadata, string ddl, Action <OutputColumnDescriptor> collector) { TSqlFragment sqlF = ScriptDomFacade.Parse(ddl); //CreateViewStatement stmt_CreateView = (CreateViewStatement)((TSqlScript)sqlF).Batches[0].Statements[0]; SelectStatement stmt_sel = (SelectStatement)((TSqlScript)sqlF).Batches[0].Statements[0]; BatchOutputColumnTypeResolver batchResolver = new BatchOutputColumnTypeResolver(schemaMetadata, stmt_sel, new BatchWithoutParameters()); StatementOutputColumnTypeResolverV2 resolver = new StatementOutputColumnTypeResolverV2(batchResolver, stmt_sel); QuerySpecification first = TopQuerySpecification(stmt_sel.QueryExpression); foreach (SelectElement se in first.SelectElements) { if (se is SelectScalarExpression scalarExpr) { OutputColumnDescriptor col = resolver.ResolveSelectScalarExpression(scalarExpr); collector(col); } else { throw new NotImplementedException(se.WhatIsThis()); } } }
public static ProcedureMetadata ParseProcedureBody(string procedure_schema, string procedure_name, string procedure_body, Dictionary <string, ProcedureCodeParameter> procedureParameters) { string fullName = "[" + procedure_schema + "].[" + procedure_name + "]"; TSqlFragment sqlF = ScriptDomFacade.Parse(procedure_body); return(new ProcedureMetadata(fullName, procedure_name, sqlF, procedureParameters)); }
public static void LoadFunctionOutputColumns(ISchemaMetadataProvider schemaMetadata, IBatchParameterMetadata parameterMetadata, string functionBodyScript, Action <OutputColumnDescriptor> collector) { functionBodyScript = RemoveTrailingBlockComment(functionBodyScript); if (functionBodyScript.Trim()[0] == '(') { int idx = functionBodyScript.IndexOf('('); int lastix = functionBodyScript.LastIndexOf(')'); functionBodyScript = functionBodyScript.Substring(idx + 1, lastix - idx - 2); functionBodyScript = functionBodyScript.Trim(); } TSqlFragment sqlF = ScriptDomFacade.Parse(functionBodyScript); SelectStatement stmt_sel = (SelectStatement)((TSqlScript)sqlF).Batches[0].Statements[0]; BatchOutputColumnTypeResolver batchResolver = new BatchOutputColumnTypeResolver(schemaMetadata, sqlF, parameterMetadata); StatementOutputColumnTypeResolverV2 resolver = new StatementOutputColumnTypeResolverV2(batchResolver, stmt_sel); QuerySpecification first = TopQuerySpecification(stmt_sel.QueryExpression); foreach (SelectElement se in first.SelectElements) { if (se is SelectScalarExpression scalarExpr) { OutputColumnDescriptor col = resolver.ResolveSelectScalarExpression(scalarExpr); collector(col); } else { throw new NotImplementedException(se.WhatIsThis()); } } }