Search in sources :

Example 6 with SQLQueryExpr

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

the class MySqlStatementParser method parseReplicate.

public MySqlReplaceStatement parseReplicate() {
    MySqlReplaceStatement stmt = new MySqlReplaceStatement();
    accept(Token.REPLACE);
    if (lexer.token() == Token.COMMENT) {
        lexer.nextToken();
    }
    if (identifierEquals(LOW_PRIORITY)) {
        stmt.setLowPriority(true);
        lexer.nextToken();
    }
    if (identifierEquals(DELAYED)) {
        stmt.setDelayed(true);
        lexer.nextToken();
    }
    if (lexer.token() == Token.INTO) {
        lexer.nextToken();
    }
    SQLName tableName = exprParser.name();
    stmt.setTableName(tableName);
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        if (lexer.token() == Token.SELECT) {
            SQLQueryExpr queryExpr = (SQLQueryExpr) this.exprParser.expr();
            stmt.setQuery(queryExpr);
        } else {
            this.exprParser.exprList(stmt.getColumns(), stmt);
        }
        accept(Token.RPAREN);
    }
    if (lexer.token() == Token.VALUES || identifierEquals("VALUE")) {
        lexer.nextToken();
        parseValueClause(stmt.getValuesList(), 0);
    } else if (lexer.token() == Token.SELECT) {
        SQLQueryExpr queryExpr = (SQLQueryExpr) this.exprParser.expr();
        stmt.setQuery(queryExpr);
    } else if (lexer.token() == Token.SET) {
        lexer.nextToken();
        SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause();
        stmt.getValuesList().add(values);
        for (; ; ) {
            stmt.addColumn(this.exprParser.name());
            if (lexer.token() == Token.COLONEQ) {
                lexer.nextToken();
            } else {
                accept(Token.EQ);
            }
            values.addValue(this.exprParser.expr());
            if (lexer.token() == (Token.COMMA)) {
                lexer.nextToken();
                continue;
            }
            break;
        }
    } else if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        SQLQueryExpr queryExpr = (SQLQueryExpr) this.exprParser.expr();
        stmt.setQuery(queryExpr);
        accept(Token.RPAREN);
    }
    return stmt;
}
Also used : SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) MySqlReplaceStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement) SQLName(com.alibaba.druid.sql.ast.SQLName) ValuesClause(com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause) ValuesClause(com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)

Example 7 with SQLQueryExpr

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

the class WallVisitorUtils method isFirstSelectTableSource.

private static boolean isFirstSelectTableSource(SQLObject x) {
    for (; ; ) {
        if (x instanceof SQLExpr) {
            x = x.getParent();
        } else {
            break;
        }
    }
    if (!(x instanceof SQLExprTableSource)) {
        return false;
    }
    SQLSelectQueryBlock queryBlock = null;
    SQLObject parent = x.getParent();
    while (parent != null) {
        if (parent instanceof SQLSelectQueryBlock) {
            queryBlock = (SQLSelectQueryBlock) parent;
            break;
        }
        x = parent;
        parent = x.getParent();
    }
    if (queryBlock == null) {
        return false;
    }
    boolean isWhereQueryExpr = false;
    boolean isSelectItem = false;
    do {
        x = parent;
        parent = parent.getParent();
        if (parent instanceof SQLUnionQuery) {
            SQLUnionQuery union = (SQLUnionQuery) parent;
            if (union.getRight() == x && hasTableSource(union.getLeft())) {
                return false;
            }
        } else if (parent instanceof SQLQueryExpr || parent instanceof SQLInSubQueryExpr || parent instanceof SQLExistsExpr) {
            isWhereQueryExpr = isWhereOrHaving(parent);
        } else if (parent instanceof SQLSelectItem) {
            isSelectItem = true;
        } else if ((isWhereQueryExpr || isSelectItem) && parent instanceof SQLSelectQueryBlock) {
            if (hasTableSource((SQLSelectQueryBlock) parent)) {
                return false;
            }
        }
    } while (parent != null);
    return true;
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLExistsExpr(com.alibaba.druid.sql.ast.expr.SQLExistsExpr) SQLInSubQueryExpr(com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 8 with SQLQueryExpr

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

the class EqualTest_query method test_exits.

public void test_exits() throws Exception {
    String sql = "(select id from t)";
    String sql_c = "(select id from t1)";
    SQLQueryExpr exprA, exprB, exprC;
    {
        OracleExprParser parser = new OracleExprParser(sql);
        exprA = (SQLQueryExpr) parser.expr();
    }
    {
        OracleExprParser parser = new OracleExprParser(sql);
        exprB = (SQLQueryExpr) parser.expr();
    }
    {
        OracleExprParser parser = new OracleExprParser(sql_c);
        exprC = (SQLQueryExpr) 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 SQLQueryExpr(), new SQLQueryExpr());
    Assert.assertEquals(new SQLQueryExpr().hashCode(), new SQLQueryExpr().hashCode());
}
Also used : OracleExprParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleExprParser) SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr)

Aggregations

SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)8 SQLName (com.alibaba.druid.sql.ast.SQLName)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3 SQLDataTypeImpl (com.alibaba.druid.sql.ast.SQLDataTypeImpl)2 SQLObject (com.alibaba.druid.sql.ast.SQLObject)2 SQLParameter (com.alibaba.druid.sql.ast.SQLParameter)2 SQLBetweenExpr (com.alibaba.druid.sql.ast.expr.SQLBetweenExpr)1 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)1 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)1 SQLCaseExpr (com.alibaba.druid.sql.ast.expr.SQLCaseExpr)1 Item (com.alibaba.druid.sql.ast.expr.SQLCaseExpr.Item)1 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)1 SQLExistsExpr (com.alibaba.druid.sql.ast.expr.SQLExistsExpr)1 SQLInListExpr (com.alibaba.druid.sql.ast.expr.SQLInListExpr)1 SQLInSubQueryExpr (com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr)1 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1 SQLNCharExpr (com.alibaba.druid.sql.ast.expr.SQLNCharExpr)1 SQLNotExpr (com.alibaba.druid.sql.ast.expr.SQLNotExpr)1 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)1 SQLUnaryExpr (com.alibaba.druid.sql.ast.expr.SQLUnaryExpr)1