Search in sources :

Example 41 with SQLBinaryOpExpr

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

the class OracleSelectTest36 method test_0.

public void test_0() throws Exception {
    //
    String sql = //
    "select ID,name from druid_test where (name>=? or name is null) and card_id<?";
    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    print(statementList);
    String result = SQLUtils.toOracleString(stmt);
    String result_lcase = SQLUtils.toOracleString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
    Assert.assertEquals("SELECT ID, name" + "\nFROM druid_test" + "\nWHERE (name >= ?" + "\n\t\tOR name IS NULL)" + "\n\tAND card_id < ?", result);
    Assert.assertEquals("select ID, name" + "\nfrom druid_test" + "\nwhere (name >= ?" + "\n\t\tor name is null)" + "\n\tand card_id < ?", result_lcase);
    {
        SQLSelect select = ((SQLSelectStatement) stmt).getSelect();
        SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) select.getQuery();
        SQLBinaryOpExpr where = (SQLBinaryOpExpr) queryBlock.getWhere();
        Assert.assertEquals(SQLBinaryOperator.BooleanAnd, where.getOperator());
        SQLBinaryOpExpr left = (SQLBinaryOpExpr) where.getLeft();
        Assert.assertEquals(SQLBinaryOperator.BooleanOr, left.getOperator());
        SQLBinaryOpExpr nameGTEQ = (SQLBinaryOpExpr) left.getLeft();
        Assert.assertEquals(SQLBinaryOperator.GreaterThanOrEqual, nameGTEQ.getOperator());
        SQLBinaryOpExpr nameIS = (SQLBinaryOpExpr) left.getRight();
        Assert.assertEquals(SQLBinaryOperator.Is, nameIS.getOperator());
    }
    Assert.assertEquals(1, statementList.size());
    OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
    stmt.accept(visitor);
    System.out.println("Tables : " + visitor.getTables());
    System.out.println("fields : " + visitor.getColumns());
    System.out.println("coditions : " + visitor.getConditions());
    System.out.println("relationships : " + visitor.getRelationships());
    System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("druid_test")));
    Assert.assertEquals(3, visitor.getColumns().size());
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("druid_test", "ID")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("druid_test", "name")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("druid_test", "card_id")));
// Assert.assertTrue(visitor.getOrderByColumns().contains(new TableStat.Column("employees", "last_name")));
}
Also used : SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) OracleSchemaStatVisitor(com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) OracleStatementParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)

Example 42 with SQLBinaryOpExpr

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

the class OdpsExprParser method equalityRest.

public SQLExpr equalityRest(SQLExpr expr) {
    if (lexer.token() == Token.EQEQ) {
        SQLExpr rightExp;
        lexer.nextToken();
        try {
            rightExp = bitOr();
        } catch (EOFParserException e) {
            throw new ParserException("EOF, " + expr + "=", e);
        }
        rightExp = equalityRest(rightExp);
        expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Equality, rightExp, getDbType());
        return expr;
    }
    return super.equalityRest(expr);
}
Also used : SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 43 with SQLBinaryOpExpr

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

the class OracleExprParser method equalityRest.

public SQLExpr equalityRest(SQLExpr expr) {
    SQLExpr rightExp;
    if (lexer.token() == Token.EQ) {
        lexer.nextToken();
        if (lexer.token() == Token.GT) {
            lexer.nextToken();
            rightExp = expr();
            String argumentName = ((SQLIdentifierExpr) expr).getName();
            return new OracleArgumentExpr(argumentName, rightExp);
        }
        rightExp = shift();
        rightExp = equalityRest(rightExp);
        expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Equality, rightExp, getDbType());
    } else if (lexer.token() == Token.BANGEQ) {
        lexer.nextToken();
        rightExp = shift();
        rightExp = equalityRest(rightExp);
        expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotEqual, rightExp, getDbType());
    }
    return expr;
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) OracleArgumentExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleArgumentExpr)

Example 44 with SQLBinaryOpExpr

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

the class WallVisitorUtils method createTenantCondition.

@Deprecated
private static SQLBinaryOpExpr createTenantCondition(WallVisitor visitor, String alias, StatementType statementType, String tableName) {
    SQLExpr left, right;
    if (alias != null) {
        left = new SQLPropertyExpr(new SQLIdentifierExpr(alias), visitor.getConfig().getTenantColumn());
    } else {
        left = new SQLIdentifierExpr(visitor.getConfig().getTenantColumn());
    }
    right = generateTenantValue(visitor, alias, statementType, tableName);
    SQLBinaryOpExpr tenantCondition = new SQLBinaryOpExpr(left, SQLBinaryOperator.Equality, right);
    return tenantCondition;
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 45 with SQLBinaryOpExpr

use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr 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)

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