Ejemplo n.º 1
0
 public static string generateVirtualTableName(TCustomSqlStatement stmt)
 {
     lock (typeof(SQLUtil))
     {
         if (virtualTableNames.ContainsKey(stmt.ToString()))
         {
             return(virtualTableNames[stmt.ToString()]);
         }
         else
         {
             string tableName = null;
             virtualTableIndex++;
             if (virtualTableIndex == 0)
             {
                 tableName = "RESULT SET COLUMNS";
             }
             else
             {
                 tableName = "RESULT SET COLUMNS " + virtualTableIndex;
             }
             virtualTableNames[stmt.ToString()] = tableName;
             return(tableName);
         }
     }
 }
Ejemplo n.º 2
0
 private void parseStatement(TCustomSqlStatement stmt)
 {
     if (!stmtList.Contains(stmt.ToString()))
     {
         stmtList.Add(stmt.ToString());
     }
     else
     {
         return;
     }
     if (stmt is TCreateViewSqlStatement)
     {
         TCreateViewSqlStatement createView = ((TCreateViewSqlStatement)stmt);
         parseCreateView(createView);
     }
     else if (stmt is TCreateTableSqlStatement && ((TCreateTableSqlStatement)stmt).SubQuery != null)
     {
         TCreateTableSqlStatement createTable = ((TCreateTableSqlStatement)stmt);
         parseCreateTable(createTable);
     }
     else if (stmt is TInsertSqlStatement && ((TInsertSqlStatement)stmt).SubQuery != null)
     {
         TInsertSqlStatement insert = ((TInsertSqlStatement)stmt);
         parseInsertStmt(insert);
     }
     if (stmt is TUseDatabase)
     {
         TUseDatabase use = (TUseDatabase)stmt;
         database = use.DatabaseName.ToString();
     }
 }
Ejemplo n.º 3
0
        internal virtual bool rewriteQuery(TCustomSqlStatement sqlStatement, EDbVendor dbVendor)
        {
            String sourceSql = sqlStatement.ToString();
            String targetSql = sqlStatement.ToScript();

            return(testScriptGenerator.verifyScript(dbVendor, sourceSql, targetSql));
        }
Ejemplo n.º 4
0
        internal virtual string remove(TCustomSqlStatement stat, LinkedHashMap <string, string> conditionMap)
        {
            if (stat.ResultColumnList != null)
            {
                for (int j = 0; j < stat.ResultColumnList.size(); j++)
                {
                    TResultColumn column = stat.ResultColumnList.getResultColumn(j);
                    if (column.Expr != null && column.Expr.SubQuery is TCustomSqlStatement)
                    {
                        TCustomSqlStatement query = (TCustomSqlStatement)column.Expr.SubQuery;
                        getParserString(query, conditionMap);
                    }
                }
            }
            if (stat.CteList != null)
            {
                for (int i = 0; i < stat.CteList.size(); i++)
                {
                    TCTE cte = stat.CteList.getCTE(i);
                    if (cte.Subquery != null)
                    {
                        getParserString(cte.Subquery, conditionMap);
                    }
                    if (cte.InsertStmt != null)
                    {
                        getParserString(cte.InsertStmt, conditionMap);
                    }
                    if (cte.UpdateStmt != null)
                    {
                        getParserString(cte.UpdateStmt, conditionMap);
                    }
                    if (cte.PreparableStmt != null)
                    {
                        getParserString(cte.PreparableStmt, conditionMap);
                    }
                    if (cte.DeleteStmt != null)
                    {
                        getParserString(cte.DeleteStmt, conditionMap);
                    }
                }
            }

            if (stat is TSelectSqlStatement && ((TSelectSqlStatement)stat).SetOperator != TSelectSqlStatement.setOperator_none)
            {
                TSelectSqlStatement select = ((TSelectSqlStatement)stat);
                getParserString(select.LeftStmt, conditionMap);
                getParserString(select.RightStmt, conditionMap);
                return(select.ToScript());
            }

            if (stat.Statements != null && stat.Statements.size() > 0)
            {
                for (int i = 0; i < stat.Statements.size(); i++)
                {
                    getParserString(stat.Statements.get(i), conditionMap);
                }
            }
            if (stat.ReturningClause != null)
            {
                if (stat.ReturningClause.ColumnValueList != null)
                {
                    for (int i = 0; i < stat.ReturningClause.ColumnValueList.size(); i++)
                    {
                        if (stat.ReturningClause.ColumnValueList.getExpression(i).SubQuery != null)
                        {
                            getParserString(stat.ReturningClause.ColumnValueList.getExpression(i).SubQuery, conditionMap);
                        }
                    }
                }
                if (stat.ReturningClause.VariableList != null)
                {
                    for (int i = 0; i < stat.ReturningClause.VariableList.size(); i++)
                    {
                        if (stat.ReturningClause.VariableList.getExpression(i).SubQuery != null)
                        {
                            getParserString(stat.ReturningClause.VariableList.getExpression(i).SubQuery, conditionMap);
                        }
                    }
                }
            }
            if (stat is TSelectSqlStatement)
            {
                TTableList list = ((TSelectSqlStatement)stat).tables;
                for (int i = 0; i < list.size(); i++)
                {
                    TTable table = list.getTable(i);
                    if (table.Subquery != null)
                    {
                        getParserString(table.Subquery, conditionMap);
                    }
                    if (table.FuncCall != null)
                    {
                        ExpressionChecker w = new ExpressionChecker(this);
                        w.checkFunctionCall(table.FuncCall, conditionMap);
                    }
                }
            }

            if (stat is TSelectSqlStatement)
            {
                TJoinList list = ((TSelectSqlStatement)stat).joins;
                for (int i = 0; i < list.size(); i++)
                {
                    TJoin join = list.getJoin(i);
                    for (int j = 0; j < join.JoinItems.size(); j++)
                    {
                        TJoinItem joinItem = join.JoinItems.getJoinItem(j);
                        if (joinItem.Table != null)
                        {
                            if (joinItem.Table.Subquery != null)
                            {
                                getParserString(joinItem.Table.Subquery, conditionMap);
                            }
                            if (joinItem.Table.FuncCall != null)
                            {
                                ExpressionChecker w = new ExpressionChecker(this);
                                w.checkFunctionCall(joinItem.Table.FuncCall, conditionMap);
                            }
                            if (joinItem.OnCondition != null)
                            {
                                ExpressionChecker w = new ExpressionChecker(this);
                                w.checkExpression(joinItem.OnCondition, conditionMap);
                            }
                        }
                    }
                }
            }

            if (stat is TSelectSqlStatement)
            {
                TSelectSqlStatement select = (TSelectSqlStatement)stat;
                for (int i = 0; i < select.ResultColumnList.size(); i++)
                {
                    TResultColumn field = select.ResultColumnList.getResultColumn(i);
                    TExpression   expr  = field.Expr;
                    if (expr != null && expr.ExpressionType == EExpressionType.subquery_t)
                    {
                        getParserString(expr.SubQuery, conditionMap);
                    }
                }
            }

            if (stat.WhereClause != null && stat.WhereClause.Condition != null && stat.WhereClause.Condition.ToScript().Trim().Length > 0)
            {
                TExpression whereExpression = stat.Gsqlparser.parseExpression(stat.WhereClause.Condition.ToScript());
                if (string.ReferenceEquals(whereExpression.ToString(), null))
                {
                    removeCondition removeCondition = new removeCondition(stat.ToString(), stat.dbvendor, conditionMap);
                    return(removeCondition.result);
                }
                else
                {
                    string oldString = stat.ToScript();
                    conditionBuffer.Remove(0, conditionBuffer.Length);
                    ExpressionChecker w = new ExpressionChecker(this);
                    w.checkExpression(whereExpression, conditionMap);
                    stat.WhereClause.Condition = stat.Gsqlparser.parseExpression(whereExpression.ToScript());
                }
            }
            if ((stat is TSelectSqlStatement) && ((TSelectSqlStatement)stat).GroupByClause != null && ((TSelectSqlStatement)stat).GroupByClause.HavingClause != null)
            {
                TExpression havingExpression = ((TSelectSqlStatement)stat).GroupByClause.HavingClause;

                if (havingExpression == null)
                {
                    removeCondition removeCondition = new removeCondition(stat.ToScript(), stat.dbvendor, conditionMap);
                    return(removeCondition.result);
                }
                else
                {
                    string oldString = stat.ToScript();
                    conditionBuffer.Remove(0, conditionBuffer.Length);
                    ExpressionChecker w = new ExpressionChecker(this);
                    w.checkExpression(havingExpression, conditionMap);
                    string newString = stat.ToScript();
                    if (!oldString.Equals(newString))
                    {
                        if (havingExpression != null && havingExpression.ToScript().Trim().Length == 0)
                        {
                            ((TSelectSqlStatement)stat).GroupByClause = null;
                        }
                    }
                }
            }
            return(stat.ToScript());
        }