Search in sources :

Example 1 with SQLBinaryOpExprGroup

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

the class LargeOrTest method test_largeOr.

public void test_largeOr() throws Exception {
    StringBuffer buf = new StringBuffer();
    buf.append("SELECT 1 FROM T WHERE ID = ?");
    for (int i = 0; i < 10000; ++i) {
        buf.append(" OR ID = ?");
    }
    String sql = buf.toString();
    OracleStatementParser parser = new OracleStatementParser(sql, SQLParserFeature.EnableSQLBinaryOpExprGroup);
    SQLSelectStatement stmt = (SQLSelectStatement) parser.parseStatementList().get(0);
    SQLSelectQueryBlock select = (SQLSelectQueryBlock) stmt.getSelect().getQuery();
    SQLBinaryOpExprGroup where = (SQLBinaryOpExprGroup) select.getWhere();
    SQLBinaryOpExpr last = (SQLBinaryOpExpr) where.getItems().get(0);
    Assert.assertEquals(SQLBinaryOperator.Equality, last.getOperator());
}
Also used : SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLBinaryOpExprGroup(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExprGroup) OracleStatementParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)

Example 2 with SQLBinaryOpExprGroup

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

the class SQLDeleteStatement method removeCondition.

public boolean removeCondition(SQLExpr condition) {
    if (condition == null) {
        return false;
    }
    if (where instanceof SQLBinaryOpExprGroup) {
        SQLBinaryOpExprGroup group = (SQLBinaryOpExprGroup) where;
        int removedCount = 0;
        List<SQLExpr> items = group.getItems();
        for (int i = items.size() - 1; i >= 0; i--) {
            if (items.get(i).equals(condition)) {
                items.remove(i);
                removedCount++;
            }
        }
        if (items.size() == 0) {
            where = null;
        }
        return removedCount > 0;
    }
    if (where instanceof SQLBinaryOpExpr) {
        SQLBinaryOpExpr binaryOpWhere = (SQLBinaryOpExpr) where;
        SQLBinaryOperator operator = binaryOpWhere.getOperator();
        if (operator == SQLBinaryOperator.BooleanAnd || operator == SQLBinaryOperator.BooleanOr) {
            List<SQLExpr> items = SQLBinaryOpExpr.split(binaryOpWhere);
            int removedCount = 0;
            for (int i = items.size() - 1; i >= 0; i--) {
                SQLExpr item = items.get(i);
                if (item.equals(condition)) {
                    if (SQLUtils.replaceInParent(item, null)) {
                        removedCount++;
                    }
                }
            }
            return removedCount > 0;
        }
    }
    if (condition.equals(where)) {
        where = null;
        return true;
    }
    return false;
}
Also used : SQLBinaryOperator(com.alibaba.druid.sql.ast.expr.SQLBinaryOperator) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLBinaryOpExprGroup(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExprGroup)

Example 3 with SQLBinaryOpExprGroup

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

the class SQLUpdateStatement method removeCondition.

public boolean removeCondition(SQLExpr condition) {
    if (condition == null) {
        return false;
    }
    if (where instanceof SQLBinaryOpExprGroup) {
        SQLBinaryOpExprGroup group = (SQLBinaryOpExprGroup) where;
        int removedCount = 0;
        List<SQLExpr> items = group.getItems();
        for (int i = items.size() - 1; i >= 0; i--) {
            if (items.get(i).equals(condition)) {
                items.remove(i);
                removedCount++;
            }
        }
        if (items.size() == 0) {
            where = null;
        }
        return removedCount > 0;
    }
    if (where instanceof SQLBinaryOpExpr) {
        SQLBinaryOpExpr binaryOpWhere = (SQLBinaryOpExpr) where;
        SQLBinaryOperator operator = binaryOpWhere.getOperator();
        if (operator == SQLBinaryOperator.BooleanAnd || operator == SQLBinaryOperator.BooleanOr) {
            List<SQLExpr> items = SQLBinaryOpExpr.split(binaryOpWhere);
            int removedCount = 0;
            for (int i = items.size() - 1; i >= 0; i--) {
                SQLExpr item = items.get(i);
                if (item.equals(condition)) {
                    if (SQLUtils.replaceInParent(item, null)) {
                        removedCount++;
                    }
                }
            }
            return removedCount > 0;
        }
    }
    if (condition.equals(where)) {
        where = null;
        return true;
    }
    return false;
}
Also used : SQLBinaryOperator(com.alibaba.druid.sql.ast.expr.SQLBinaryOperator) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLBinaryOpExprGroup(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExprGroup)

Example 4 with SQLBinaryOpExprGroup

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

the class LargeOrTest method test_largeAnd.

public void test_largeAnd() throws Exception {
    StringBuffer buf = new StringBuffer();
    buf.append("SELECT 1 FROM T WHERE ID = ?");
    for (int i = 0; i < 10000; ++i) {
        buf.append(" AND ID = ?");
    }
    String sql = buf.toString();
    OracleStatementParser parser = new OracleStatementParser(sql, SQLParserFeature.EnableSQLBinaryOpExprGroup);
    SQLSelectStatement stmt = (SQLSelectStatement) parser.parseStatementList().get(0);
    SQLSelectQueryBlock select = (SQLSelectQueryBlock) stmt.getSelect().getQuery();
    SQLBinaryOpExprGroup where = (SQLBinaryOpExprGroup) select.getWhere();
    SQLBinaryOpExpr last = (SQLBinaryOpExpr) where.getItems().get(0);
    Assert.assertEquals(SQLBinaryOperator.Equality, last.getOperator());
}
Also used : SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLBinaryOpExprGroup(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExprGroup) OracleStatementParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)

Aggregations

SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)4 SQLBinaryOpExprGroup (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExprGroup)4 SQLBinaryOperator (com.alibaba.druid.sql.ast.expr.SQLBinaryOperator)2 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)2 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)2 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)2