Search in sources :

Example 16 with SQLBinaryOpExpr

use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr in project druid by alibaba.

the class WallVisitorUtils method checkConditionForMultiTenant.

@Deprecated
public static void checkConditionForMultiTenant(WallVisitor visitor, SQLExpr x, SQLObject parent) {
    String tenantTablePattern = visitor.getConfig().getTenantTablePattern();
    if (tenantTablePattern == null || tenantTablePattern.length() == 0) {
        return;
    }
    if (parent == null) {
        throw new IllegalStateException("parent is null");
    }
    String alias = null;
    SQLTableSource tableSource;
    StatementType statementType = null;
    if (parent instanceof SQLDeleteStatement) {
        tableSource = ((SQLDeleteStatement) parent).getTableSource();
        statementType = StatementType.DELETE;
    } else if (parent instanceof SQLUpdateStatement) {
        tableSource = ((SQLUpdateStatement) parent).getTableSource();
        statementType = StatementType.UPDATE;
    } else if (parent instanceof SQLSelectQueryBlock) {
        tableSource = ((SQLSelectQueryBlock) parent).getFrom();
        statementType = StatementType.SELECT;
    } else {
        throw new IllegalStateException("not support parent : " + parent.getClass());
    }
    String matchTableName = null;
    if (tableSource instanceof SQLExprTableSource) {
        SQLExpr tableExpr = ((SQLExprTableSource) tableSource).getExpr();
        if (tableExpr instanceof SQLIdentifierExpr) {
            String tableName = ((SQLIdentifierExpr) tableExpr).getName();
            if (ServletPathMatcher.getInstance().matches(tenantTablePattern, tableName)) {
                matchTableName = tableName;
                alias = tableSource.getAlias();
            }
        }
    } else if (tableSource instanceof SQLJoinTableSource) {
        SQLJoinTableSource join = (SQLJoinTableSource) tableSource;
        if (join.getLeft() instanceof SQLExprTableSource) {
            SQLExpr tableExpr = ((SQLExprTableSource) join.getLeft()).getExpr();
            if (tableExpr instanceof SQLIdentifierExpr) {
                String tableName = ((SQLIdentifierExpr) tableExpr).getName();
                if (ServletPathMatcher.getInstance().matches(tenantTablePattern, tableName)) {
                    matchTableName = tableName;
                    alias = join.getLeft().getAlias();
                }
            }
            checkJoinConditionForMultiTenant(visitor, join, false, statementType);
        } else {
            checkJoinConditionForMultiTenant(visitor, join, true, statementType);
        }
    }
    if (matchTableName == null) {
        return;
    }
    SQLBinaryOpExpr tenantCondition = createTenantCondition(visitor, alias, statementType, matchTableName);
    SQLExpr condition;
    if (x == null) {
        condition = tenantCondition;
    } else {
        condition = new SQLBinaryOpExpr(tenantCondition, SQLBinaryOperator.BooleanAnd, x);
    }
    if (parent instanceof SQLDeleteStatement) {
        SQLDeleteStatement deleteStmt = (SQLDeleteStatement) parent;
        deleteStmt.setWhere(condition);
        visitor.setSqlModified(true);
    } else if (parent instanceof SQLUpdateStatement) {
        SQLUpdateStatement updateStmt = (SQLUpdateStatement) parent;
        updateStmt.setWhere(condition);
        visitor.setSqlModified(true);
    } else if (parent instanceof SQLSelectQueryBlock) {
        SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) parent;
        queryBlock.setWhere(condition);
        visitor.setSqlModified(true);
    }
}
Also used : StatementType(com.alibaba.druid.wall.WallConfig.TenantCallBack.StatementType) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 17 with SQLBinaryOpExpr

use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr in project druid by alibaba.

the class WallVisitorUtils method partExpr.

public static List<SQLExpr> partExpr(List<SQLExpr> exprs) {
    List<SQLExpr> partList = new ArrayList<SQLExpr>();
    for (SQLExpr x : exprs) {
        if (x instanceof SQLBinaryOpExpr) {
            SQLBinaryOpExpr binary = (SQLBinaryOpExpr) x;
            if (binary.getOperator() == SQLBinaryOperator.BooleanAnd || binary.getOperator() == SQLBinaryOperator.BooleanOr) {
                partList.add(((SQLBinaryOpExpr) x).getLeft());
                partList.add(((SQLBinaryOpExpr) x).getRight());
                continue;
            }
        }
        partList.add(x);
    }
    return partList;
}
Also used : ArrayList(java.util.ArrayList) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 18 with SQLBinaryOpExpr

use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr in project druid by alibaba.

the class WallVisitorUtils method isSimpleConstExpr.

private static boolean isSimpleConstExpr(SQLExpr sqlExpr) {
    List<SQLExpr> parts = getParts(sqlExpr);
    if (parts.isEmpty()) {
        return false;
    }
    for (SQLExpr part : parts) {
        if (isFirst(part)) {
            Object evalValue = part.getAttribute(EVAL_VALUE);
            if (evalValue == null) {
                if (part instanceof SQLBooleanExpr) {
                    evalValue = ((SQLBooleanExpr) part).getValue();
                } else if (part instanceof SQLNumericLiteralExpr) {
                    evalValue = ((SQLNumericLiteralExpr) part).getNumber();
                } else if (part instanceof SQLCharExpr) {
                    evalValue = ((SQLCharExpr) part).getText();
                } else if (part instanceof SQLNCharExpr) {
                    evalValue = ((SQLNCharExpr) part).getText();
                }
            }
            Boolean result = SQLEvalVisitorUtils.castToBoolean(evalValue);
            if (result != null && result) {
                return true;
            }
        }
        boolean isSimpleConstExpr = false;
        if (part == sqlExpr || part instanceof SQLLiteralExpr) {
            isSimpleConstExpr = true;
        } else if (part instanceof SQLBinaryOpExpr) {
            SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) part;
            if (binaryOpExpr.getOperator() == SQLBinaryOperator.Equality || binaryOpExpr.getOperator() == SQLBinaryOperator.NotEqual || binaryOpExpr.getOperator() == SQLBinaryOperator.GreaterThan) {
                if (binaryOpExpr.getLeft() instanceof SQLIntegerExpr && binaryOpExpr.getRight() instanceof SQLIntegerExpr) {
                    isSimpleConstExpr = true;
                }
            }
        }
        if (!isSimpleConstExpr) {
            return false;
        }
    }
    return true;
}
Also used : SQLBooleanExpr(com.alibaba.druid.sql.ast.expr.SQLBooleanExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLNCharExpr(com.alibaba.druid.sql.ast.expr.SQLNCharExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 19 with SQLBinaryOpExpr

use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr in project druid by alibaba.

the class WallVisitorUtils method isFirst.

public static boolean isFirst(SQLObject x) {
    if (x == null) {
        return true;
    }
    for (; ; ) {
        SQLObject parent = x.getParent();
        if (!(parent instanceof SQLExpr)) {
            return true;
        }
        if (parent instanceof SQLBinaryOpExpr) {
            SQLBinaryOpExpr binaryExpr = (SQLBinaryOpExpr) parent;
            if (x == binaryExpr.getRight()) {
                return false;
            }
        }
        x = parent;
    }
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 20 with SQLBinaryOpExpr

use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr in project druid by alibaba.

the class EqualTest_binaryOp method test_exits.

public void test_exits() throws Exception {
    String sql = "a > b";
    String sql_c = "a > 2";
    SQLBinaryOpExpr exprA, exprB, exprC;
    {
        OracleExprParser parser = new OracleExprParser(sql);
        exprA = (SQLBinaryOpExpr) parser.expr();
    }
    {
        OracleExprParser parser = new OracleExprParser(sql);
        exprB = (SQLBinaryOpExpr) parser.expr();
    }
    {
        OracleExprParser parser = new OracleExprParser(sql_c);
        exprC = (SQLBinaryOpExpr) parser.expr();
    }
    Assert.assertEquals(exprA, exprB);
    Assert.assertNotEquals(exprA, exprC);
    Assert.assertTrue(exprA.equals(exprA));
    Assert.assertFalse(exprA.equals(new Object()));
    Assert.assertEquals(exprA.hashCode(), exprB.hashCode());
    Assert.assertEquals(new SQLBinaryOpExpr(), new SQLBinaryOpExpr());
    Assert.assertEquals(new SQLBinaryOpExpr().hashCode(), new SQLBinaryOpExpr().hashCode());
}
Also used : OracleExprParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleExprParser) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)

Aggregations

SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)46 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)30 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)10 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)9 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)8 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)8 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)7 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)7 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)6 SQLObject (com.alibaba.druid.sql.ast.SQLObject)5 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)5 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)5 SQLBinaryOperator (com.alibaba.druid.sql.ast.expr.SQLBinaryOperator)4 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)4 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)4 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)4 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)4 ParserException (com.alibaba.druid.sql.parser.ParserException)4 SQLOver (com.alibaba.druid.sql.ast.SQLOver)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3