public virtual bool exprVisit(TParseTreeNode pNode, bool isLeafNode)
 {
     if (pNode is TExpression)
     {
         TExpression expr = (TExpression)pNode;
         if ((Regex.Match(expr.ToString(), "'\\s*'").Success || Regex.Match(expr.ToString(), "\"\\s*\"").Success) && (expr.ParentExpr != null && isLogicExpression(expr.ParentExpr)))
         {
             piggybacked = true;
         }
     }
     return(true);
 }
示例#2
0
        public virtual void testColumnInAndOr()
        {
            TExpression     expression = parser.parseExpression("columnA+(columnB*2)>columnC and columnD=columnE-9");
            TExpressionList resultList = expression.searchColumn("columnA");

            Assert.IsTrue(resultList.size() == 1);
            TExpression columnAExpr = resultList.getExpression(0);

            Assert.IsTrue(columnAExpr.ExpressionType == EExpressionType.simple_object_name_t);
            Assert.IsTrue(columnAExpr.ToString().Equals("columnA", StringComparison.CurrentCultureIgnoreCase));

            Assert.IsTrue(expression.ToScript().Equals("columnA + (columnB * 2) > columnC and  columnD = columnE - 9", StringComparison.OrdinalIgnoreCase));
            columnAExpr.remove();
            Assert.IsTrue(expression.ToScript().Equals("columnD = columnE - 9", StringComparison.OrdinalIgnoreCase));
        }
示例#3
0
        public virtual void testColumnInAndOr1()
        {
            parser.sqltext = "select *\n" + "from table1 pal, table2 pualr, table3 pu\n" + "WHERE  (pal.application_location_id = pualr.application_location_id \n" + "         AND pu.jbp_uid = pualr.jbp_uid \n" + "         AND pu.username = '******')";
            int ret = parser.parse();

            Assert.IsTrue(ret == 0);
            TSelectSqlStatement selectSqlStatement = (TSelectSqlStatement)parser.sqlstatements.get(0);

            TExpression expression = selectSqlStatement.WhereClause.Condition;

            TExpressionList resultList = expression.searchColumn("application_location_id");

            Assert.IsTrue(resultList.size() == 2);
            TExpression expression1 = resultList.getExpression(0);

            Assert.IsTrue(expression1.ExpressionType == EExpressionType.simple_object_name_t);
            Assert.IsTrue(expression1.ToString().Equals("pal.application_location_id", StringComparison.CurrentCultureIgnoreCase));
            expression1.remove();
            Assert.IsTrue(expression.ToScript().Equals("(pu.jbp_uid = pualr.jbp_uid and  pu.username = '******')", StringComparison.OrdinalIgnoreCase));
        }
示例#4
0
        public void testXmlAgg()
        {
            TGSqlParser sqlparser = new TGSqlParser(EDbVendor.dbvoracle);

            sqlparser.sqltext = "SELECT\n" + "   O.OBJECT_ID,\n" + "   '|' || RTRIM (XMLAGG (XMLELEMENT (K, O.KEY_1 || '|')).EXTRACT ('//text()'),\n" + "'|') || '|' AS TEXT_KEY\n" + "FROM DAG_OBJECT_FACT O";
            Assert.IsTrue(sqlparser.parse() == 0);

            TResultColumn rc           = ((TSelectSqlStatement)sqlparser.sqlstatements.get(0)).ResultColumnList.getResultColumn(1);
            TExpression   expression   = rc.Expr.LeftOperand.RightOperand;
            TFunctionCall functionCall = expression.FunctionCall;
            TExpression   xmlaggExpr   = functionCall.Args.getExpression(0);

            Assert.IsTrue(xmlaggExpr.ExpressionType == EExpressionType.object_access_t);
            TObjectAccess objectAccess = xmlaggExpr.ObjectAccess;
            TFunctionCall xmlelement   = objectAccess.ObjectExpr.FunctionCall.Args.getExpression(0).FunctionCall;

            //Assert.IsTrue(xmlelement.getArgs().size() == 1);
            Assert.IsTrue(xmlelement.XMLElementNameExpr.ToString().Equals("K", StringComparison.CurrentCultureIgnoreCase));
            TResultColumn resultColumn = xmlelement.XMLElementValueExprList.getResultColumn(0);
            TExpression   expression1  = resultColumn.Expr;

            Assert.IsTrue(expression1.ToString().Equals("O.KEY_1 || '|'", StringComparison.CurrentCultureIgnoreCase));
        }
示例#5
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());
        }