private void AnalyzeMergeStatement(MergeStatement mergeStatement, ParserResults results)
        {
            mergeStatement.PrintTSqlStatementBlockToDebugConsole();
            List <TableParsingResult> temp = new List <TableParsingResult>();

            if (mergeStatement.MergeSpecification.Target is NamedTableReference mergeNamedTableReference)
            {
                string tableName = ExtraceTableNameFromNamedTableRefernce(mergeNamedTableReference, out string alias);
                temp.AddIfNotExists(tableName, SqlOperationType.INSERT, alias);
                temp.AddIfNotExists(tableName, SqlOperationType.UPDATE, alias);

                if (mergeStatement.MergeSpecification.TableReference is QueryDerivedTable mergeQueryDerivedTable && mergeQueryDerivedTable.QueryExpression is QuerySpecification querySpecification)
                {
                    var result = ExtractTablesFromQuerySpecification(querySpecification); //ExtractTablesUsedInFromClause(mergeQuerySpecification.FromClause);
                    temp.AddIfNotExists(result);
                }
            }

            mergeStatement.PrintTablesToDebugConsole(temp);
            results.AddIfNotExists(temp);
        }
        private void AnalyzeDeleteStatement(DeleteStatement deleteStatement, ParserResults results)
        {
            deleteStatement.PrintTSqlStatementBlockToDebugConsole();

            if (deleteStatement.DeleteSpecification.Target is NamedTableReference deleteNamedTableReference)
            {
                string tableName = ExtraceTableNameFromNamedTableRefernce(deleteNamedTableReference, out string alias);
                if (deleteStatement.DeleteSpecification.FromClause != null)
                {
                    var items = ExtractTablesUsedInFromClause(deleteStatement.DeleteSpecification.FromClause);
                    var match = items.Find(x => x.Alias == tableName);
                    if (match != null)
                    {
                        match.OperationType = SqlOperationType.DELETE;
                    }
                    results.AddIfNotExists(items);
                }
                else
                {
                    results.AddIfNotExists(tableName, SqlOperationType.DELETE, alias);
                }
            }
        }
Ejemplo n.º 3
0
 public static void AddIfNotExists(this ParserResults results, string tableName, SqlOperationType sqlOperationType, string alias)
 {
     results.TableParsingResults.AddIfNotExists(tableName, sqlOperationType, alias);
 }
 private void AnalyzeWhileStatement(WhileStatement whileStatement, ParserResults results)
 {
     AnalyzeTsqlStatement(results, whileStatement.Statement);
 }
 private void AnalyzeDeclareVariableStatement(DeclareVariableStatement declareVariableStatement, ParserResults results)
 {
     foreach (DeclareVariableElement element in declareVariableStatement.Declarations)
     {
         if (element.Value is ScalarSubquery scalarSubquery)
         {
             var items = ExtractTablesFromScalarSubQuery(scalarSubquery);
             results.AddIfNotExists(items);
         }
     }
 }
        private void AnalyzeBeginEndBlockStatement(BeginEndBlockStatement beginEndBlockStatement, ParserResults results)
        {
            if (beginEndBlockStatement.StatementList.Statements.Count == 0)
            {
                return;
            }

            AnalyzeTsqlStatementList(beginEndBlockStatement.StatementList, results);
        }
        private void AnalyzeCreateProcedureStatement(CreateProcedureStatement createProcedureStatement, ParserResults results)
        {
            //((BeginEndBlockStatement)((CreateProcedureStatement)statement).StatementList.Statements[0]).StatementList.Statements
            if (createProcedureStatement.StatementList.Statements.Count == 0)
            {
                return;
            }

            AnalyzeTsqlStatementList(createProcedureStatement.StatementList, results);
        }
        private ParserResults AnalyzeTsqlStatement(ParserResults results, TSqlStatement statement)
        {
            if (statement is null)
            {
                return(results);
            }

            if (statement is CreateProcedureStatement createProcedureStatement)
            {
                AnalyzeCreateProcedureStatement(createProcedureStatement, results);
            }
            if (statement is BeginEndBlockStatement beginEndBlockStatement)
            {
                AnalyzeBeginEndBlockStatement(beginEndBlockStatement, results);
            }
            else if (statement is UpdateStatement updateStatement)
            {
                AnalyzeUpdateStatement(updateStatement, results);
            }
            else if (statement is InsertStatement insertStatement)
            {
                AnalyzeInsertStatement(insertStatement, results);
            }
            else if (statement is SelectStatement selectStatement)
            {
                AnalyzeSelectStatement(selectStatement, results);
            }
            else if (statement is MergeStatement mergeStatement)
            {
                AnalyzeMergeStatement(mergeStatement, results);
            }
            else if (statement is DeleteStatement deleteStatement)
            {
                AnalyzeDeleteStatement(deleteStatement, results);
            }
            else if (statement is TryCatchStatement tryCatchStatement)
            {
                AnalyzeTryCatchStatement(tryCatchStatement, results);
            }
            else if (statement is WhileStatement whileStatement)
            {
                AnalyzeWhileStatement(whileStatement, results);
            }
            else if (statement is ExecuteStatement executeStatement)
            {
                AnalyzeExecuteStatement(executeStatement, results);
            }
            else if (statement is IfStatement ifStatement)
            {
                AnalyzeIfStatement(ifStatement, results);
            }
            else if (statement is DeclareVariableStatement declareVariableStatement)
            {
                AnalyzeDeclareVariableStatement(declareVariableStatement, results);
            }
            else
            {
                //TODO
                Debug.WriteLine($"found statement type (not analyzed): {statement.GetType().FullName}");
            }
            return(results);
        }