Search in sources :

Example 6 with SQLInListExpr

use of com.alibaba.druid.sql.ast.expr.SQLInListExpr in project Mycat_plus by coderczp.

the class JoinParser method parserWhere.

private void parserWhere(SQLExpr aexpr, String Operator) {
    if (aexpr == null) {
        return;
    }
    if (aexpr instanceof SQLBinaryOpExpr) {
        SQLBinaryOpExpr expr = (SQLBinaryOpExpr) aexpr;
        SQLExpr exprL = expr.getLeft();
        if (!(exprL instanceof SQLBinaryOpExpr)) {
            opSQLExpr((SQLBinaryOpExpr) aexpr, Operator);
        } else {
            // if (expr.getOperator().getName().equals("AND")) {
            if (expr.getOperator() == SQLBinaryOperator.BooleanAnd) {
                // parserWhere(exprL);
                // parserWhere(expr.getRight());
                andorWhere(exprL, expr.getOperator().getName(), expr.getRight());
            } else if (expr.getOperator() == SQLBinaryOperator.BooleanOr) {
                // .getName().equals("OR")) {
                andorWhere(exprL, expr.getOperator().getName(), expr.getRight());
            } else {
                throw new RuntimeException("Can't identify the operation of  of where");
            }
        }
    } else if (aexpr instanceof SQLInListExpr) {
        SQLInListExpr expr = (SQLInListExpr) aexpr;
        SQLExpr exprL = expr.getExpr();
        String field = exprL.toString();
        tableFilter.addWhere(field, SQLUtils.toMySqlString(expr), Operator);
    }
}
Also used : SQLInListExpr(com.alibaba.druid.sql.ast.expr.SQLInListExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 7 with SQLInListExpr

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

the class SQLMergeTest method merge2.

private String merge2() {
    String sql = "INSERT INTO T (F1, F2, F3, F4, F5) VALUES (?, ?, ?, ?, ?), (?, ?, ?, ?, ?), (?, ?, ?, ?, ?)";
    StringBuilder out = new StringBuilder();
    MySqlOutputVisitor visitor = new MySqlOutputVisitor(out) {

        public boolean visit(SQLInListExpr x) {
            x.getExpr().accept(this);
            if (x.isNot()) {
                print(" NOT IN (##)");
            } else {
                print(" IN (##)");
            }
            return false;
        }

        @Override
        public boolean visit(MySqlInsertStatement x) {
            print("INSERT ");
            if (x.isLowPriority()) {
                print("LOW_PRIORITY ");
            }
            if (x.isDelayed()) {
                print("DELAYED ");
            }
            if (x.isHighPriority()) {
                print("HIGH_PRIORITY ");
            }
            if (x.isIgnore()) {
                print("IGNORE ");
            }
            print("INTO ");
            x.getTableSource().accept(this);
            if (x.getColumns().size() > 0) {
                print(" (");
                for (int i = 0, size = x.getColumns().size(); i < size; ++i) {
                    if (i != 0) {
                        print(", ");
                    }
                    x.getColumns().get(i).accept(this);
                }
                print(")");
            }
            if (x.getValuesList().size() != 0) {
                print(" VALUES ");
                int size = x.getValuesList().size();
                if (size == 0) {
                    print("()");
                } else {
                    for (int i = 0; i < 1; ++i) {
                        if (i != 0) {
                            print(", ");
                        }
                        x.getValuesList().get(i).accept(this);
                    }
                }
            }
            if (x.getQuery() != null) {
                print(" ");
                x.getQuery().accept(this);
            }
            if (x.getDuplicateKeyUpdate().size() != 0) {
                print(" ON DUPLICATE KEY UPDATE ");
                printAndAccept(x.getDuplicateKeyUpdate(), ", ");
            }
            return false;
        }
    };
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    for (SQLStatement statement : statementList) {
        statement.accept(visitor);
        visitor.println();
    }
    return out.toString();
}
Also used : SQLInListExpr(com.alibaba.druid.sql.ast.expr.SQLInListExpr) MySqlOutputVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 8 with SQLInListExpr

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

the class SQLMergeTest method merge.

private String merge() {
    StringBuilder out = new StringBuilder();
    OracleOutputVisitor visitor = new OracleOutputVisitor(out) {

        public boolean visit(SQLInListExpr x) {
            x.getExpr().accept(this);
            if (x.isNot()) {
                print(" NOT IN (##)");
            } else {
                print(" IN (##)");
            }
            return false;
        }
    };
    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    for (SQLStatement statement : statementList) {
        statement.accept(visitor);
        visitor.println();
    }
    return out.toString();
}
Also used : OracleOutputVisitor(com.alibaba.druid.sql.dialect.oracle.visitor.OracleOutputVisitor) SQLInListExpr(com.alibaba.druid.sql.ast.expr.SQLInListExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) OracleStatementParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)

Example 9 with SQLInListExpr

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

the class SQLASTVisitorAdapterTest method test_adapter.

public void test_adapter() throws Exception {
    SQLASTVisitorAdapter adapter = new SQLASTVisitorAdapter();
    new SQLBinaryOpExpr().accept(adapter);
    new SQLInListExpr().accept(adapter);
    new SQLSelectQueryBlock().accept(adapter);
    new SQLDropTableStatement().accept(adapter);
    new SQLCreateTableStatement().accept(adapter);
    new SQLDeleteStatement().accept(adapter);
    new SQLCurrentOfCursorExpr().accept(adapter);
    new SQLInsertStatement().accept(adapter);
    new SQLUpdateStatement().accept(adapter);
    new SQLNotNullConstraint().accept(adapter);
    new SQLMethodInvokeExpr().accept(adapter);
    new SQLCallStatement().accept(adapter);
    new SQLSomeExpr().accept(adapter);
    new SQLAnyExpr().accept(adapter);
    new SQLAllExpr().accept(adapter);
    new SQLDefaultExpr().accept(adapter);
    new SQLCommentStatement().accept(adapter);
    new SQLDropViewStatement().accept(adapter);
    new SQLSavePointStatement().accept(adapter);
    new SQLReleaseSavePointStatement().accept(adapter);
    new SQLCreateDatabaseStatement().accept(adapter);
    new SQLAlterTableDropIndex().accept(adapter);
    new SQLOver().accept(adapter);
    new SQLWithSubqueryClause().accept(adapter);
    new SQLAlterTableAlterColumn().accept(adapter);
    new SQLAlterTableStatement().accept(adapter);
    new SQLAlterTableDisableConstraint().accept(adapter);
    new SQLAlterTableEnableConstraint().accept(adapter);
    new SQLColumnCheck().accept(adapter);
    new SQLExprHint().accept(adapter);
    new SQLAlterTableDropConstraint().accept(adapter);
}
Also used : SQLASTVisitorAdapter(com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLAlterTableDropIndex(com.alibaba.druid.sql.ast.statement.SQLAlterTableDropIndex) SQLAlterTableStatement(com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement) SQLAllExpr(com.alibaba.druid.sql.ast.expr.SQLAllExpr) SQLReleaseSavePointStatement(com.alibaba.druid.sql.ast.statement.SQLReleaseSavePointStatement) SQLNotNullConstraint(com.alibaba.druid.sql.ast.statement.SQLNotNullConstraint) SQLOver(com.alibaba.druid.sql.ast.SQLOver) SQLDropTableStatement(com.alibaba.druid.sql.ast.statement.SQLDropTableStatement) SQLCreateDatabaseStatement(com.alibaba.druid.sql.ast.statement.SQLCreateDatabaseStatement) SQLDropViewStatement(com.alibaba.druid.sql.ast.statement.SQLDropViewStatement) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLAlterTableDisableConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableConstraint) SQLSavePointStatement(com.alibaba.druid.sql.ast.statement.SQLSavePointStatement) SQLAlterTableDropConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint) SQLUpdateStatement(com.alibaba.druid.sql.ast.statement.SQLUpdateStatement) SQLCallStatement(com.alibaba.druid.sql.ast.statement.SQLCallStatement) SQLSomeExpr(com.alibaba.druid.sql.ast.expr.SQLSomeExpr) SQLInListExpr(com.alibaba.druid.sql.ast.expr.SQLInListExpr) SQLExprHint(com.alibaba.druid.sql.ast.statement.SQLExprHint) SQLWithSubqueryClause(com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause) SQLDeleteStatement(com.alibaba.druid.sql.ast.statement.SQLDeleteStatement) SQLCurrentOfCursorExpr(com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr) SQLCommentStatement(com.alibaba.druid.sql.ast.statement.SQLCommentStatement) SQLColumnCheck(com.alibaba.druid.sql.ast.statement.SQLColumnCheck) SQLAnyExpr(com.alibaba.druid.sql.ast.expr.SQLAnyExpr) SQLAlterTableAlterColumn(com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLDefaultExpr(com.alibaba.druid.sql.ast.expr.SQLDefaultExpr) SQLAlterTableEnableConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableConstraint)

Example 10 with SQLInListExpr

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

the class EqualTest_inList method test_exits.

public void test_exits() throws Exception {
    String sql = "x in (1, 2, 3)";
    String sql_c = "x not in (1, 2, 3)";
    SQLInListExpr exprA, exprB, exprC;
    {
        OracleExprParser parser = new OracleExprParser(sql);
        exprA = (SQLInListExpr) parser.expr();
    }
    {
        OracleExprParser parser = new OracleExprParser(sql);
        exprB = (SQLInListExpr) parser.expr();
    }
    {
        OracleExprParser parser = new OracleExprParser(sql_c);
        exprC = (SQLInListExpr) 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 SQLInListExpr(), new SQLInListExpr());
    Assert.assertEquals(new SQLInListExpr().hashCode(), new SQLInListExpr().hashCode());
}
Also used : SQLInListExpr(com.alibaba.druid.sql.ast.expr.SQLInListExpr) OracleExprParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleExprParser)

Aggregations

SQLInListExpr (com.alibaba.druid.sql.ast.expr.SQLInListExpr)11 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)7 SQLExprImpl (com.alibaba.druid.sql.ast.SQLExprImpl)4 SQLNullExpr (com.alibaba.druid.sql.ast.expr.SQLNullExpr)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)2 SQLInSubQueryExpr (com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr)2 SQLListExpr (com.alibaba.druid.sql.ast.expr.SQLListExpr)2 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)2 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)2 Item (com.actiontech.dble.plan.common.item.Item)1 SQLOver (com.alibaba.druid.sql.ast.SQLOver)1 SQLAllExpr (com.alibaba.druid.sql.ast.expr.SQLAllExpr)1 SQLAnyExpr (com.alibaba.druid.sql.ast.expr.SQLAnyExpr)1 SQLCurrentOfCursorExpr (com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr)1 SQLDefaultExpr (com.alibaba.druid.sql.ast.expr.SQLDefaultExpr)1 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1 SQLSomeExpr (com.alibaba.druid.sql.ast.expr.SQLSomeExpr)1 SQLAlterTableAlterColumn (com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn)1 SQLAlterTableDisableConstraint (com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableConstraint)1