Search in sources :

Example 16 with SQLStatement

use of com.alibaba.druid.sql.ast.SQLStatement in project druid by alibaba.

the class OdpsStatementParser method parseStatementListDialect.

public boolean parseStatementListDialect(List<SQLStatement> statementList) {
    if (lexer.token() == Token.FROM) {
        SQLStatement stmt = this.parseInsert();
        statementList.add(stmt);
        return true;
    }
    if (identifierEquals("ANALYZE")) {
        lexer.nextToken();
        accept(Token.TABLE);
        OdpsAnalyzeTableStatement stmt = new OdpsAnalyzeTableStatement();
        SQLName table = this.exprParser.name();
        stmt.setTable(table);
        if (lexer.token() == Token.PARTITION) {
            lexer.nextToken();
            accept(Token.LPAREN);
            parseAssignItems(stmt.getPartition(), stmt);
            accept(Token.RPAREN);
        }
        accept(Token.COMPUTE);
        acceptIdentifier("STATISTICS");
        statementList.add(stmt);
        return true;
    }
    if (identifierEquals("ADD")) {
        lexer.nextToken();
        if (identifierEquals("STATISTIC")) {
            lexer.nextToken();
            OdpsAddStatisticStatement stmt = new OdpsAddStatisticStatement();
            stmt.setTable(this.exprParser.name());
            stmt.setStatisticClause(parseStaticClause());
            statementList.add(stmt);
            return true;
        }
        throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
    }
    if (identifierEquals("REMOVE")) {
        lexer.nextToken();
        if (identifierEquals("STATISTIC")) {
            lexer.nextToken();
            OdpsRemoveStatisticStatement stmt = new OdpsRemoveStatisticStatement();
            stmt.setTable(this.exprParser.name());
            stmt.setStatisticClause(parseStaticClause());
            statementList.add(stmt);
            return true;
        }
        throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
    }
    if (identifierEquals("READ")) {
        lexer.nextToken();
        OdpsReadStatement stmt = new OdpsReadStatement();
        stmt.setTable(this.exprParser.name());
        if (lexer.token() == Token.LPAREN) {
            lexer.nextToken();
            this.exprParser.names(stmt.getColumns(), stmt);
            accept(Token.RPAREN);
        }
        if (lexer.token() == Token.PARTITION) {
            lexer.nextToken();
            accept(Token.LPAREN);
            parseAssignItems(stmt.getPartition(), stmt);
            accept(Token.RPAREN);
        }
        if (lexer.token() == Token.LITERAL_INT) {
            stmt.setRowCount(this.exprParser.primary());
        }
        statementList.add(stmt);
        return true;
    }
    if (identifierEquals("LIST")) {
        OdpsListStmt stmt = new OdpsListStmt();
        lexer.nextToken();
        stmt.setObject(this.exprParser.expr());
        statementList.add(stmt);
        return true;
    }
    if (lexer.token() == Token.DESC || identifierEquals("DESCRIBE")) {
        OdpsDescStmt stmt = parseDescribe();
        statementList.add(stmt);
        return true;
    }
    return false;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) OdpsAddStatisticStatement(com.alibaba.druid.sql.dialect.odps.ast.OdpsAddStatisticStatement) OdpsReadStatement(com.alibaba.druid.sql.dialect.odps.ast.OdpsReadStatement) OdpsRemoveStatisticStatement(com.alibaba.druid.sql.dialect.odps.ast.OdpsRemoveStatisticStatement) OdpsDescStmt(com.alibaba.druid.sql.dialect.odps.ast.OdpsDescStmt) SQLName(com.alibaba.druid.sql.ast.SQLName) OdpsListStmt(com.alibaba.druid.sql.dialect.odps.ast.OdpsListStmt) OdpsAnalyzeTableStatement(com.alibaba.druid.sql.dialect.odps.ast.OdpsAnalyzeTableStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 17 with SQLStatement

use of com.alibaba.druid.sql.ast.SQLStatement in project druid by alibaba.

the class MySqlOutputVisitor method visit.

@Override
public boolean visit(SQLIfStatement.ElseIf x) {
    print0(ucase ? "ELSE IF " : "else if ");
    x.getCondition().accept(this);
    print0(ucase ? " THEN" : " then");
    println();
    for (int i = 0, size = x.getStatements().size(); i < size; ++i) {
        SQLStatement item = x.getStatements().get(i);
        item.setParent(x);
        item.accept(this);
        if (i != size - 1) {
            println();
        }
    }
    println();
    return false;
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlForceIndexHint(com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) MySqlUseIndexHint(com.alibaba.druid.sql.dialect.mysql.ast.MySqlUseIndexHint) MySqlIgnoreIndexHint(com.alibaba.druid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint)

Example 18 with SQLStatement

use of com.alibaba.druid.sql.ast.SQLStatement in project druid by alibaba.

the class MySqlOutputVisitor method visit.

@Override
public boolean visit(MySqlRepeatStatement x) {
    // TODO Auto-generated method stub
    if (x.getLabelName() != null && !x.getLabelName().equals("")) {
        print0(x.getLabelName());
        print0(": ");
    }
    print0(ucase ? "REPEAT " : "repeat ");
    println();
    for (int i = 0, size = x.getStatements().size(); i < size; ++i) {
        SQLStatement item = x.getStatements().get(i);
        item.setParent(x);
        item.accept(this);
        if (i != size - 1) {
            println();
        }
    }
    println();
    print0(ucase ? "UNTIL " : "until ");
    x.getCondition().accept(this);
    println();
    print0(ucase ? "END REPEAT" : "end repeat");
    if (x.getLabelName() != null && !x.getLabelName().equals("")) {
        print(' ');
        print0(x.getLabelName());
    }
    return false;
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlForceIndexHint(com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) MySqlUseIndexHint(com.alibaba.druid.sql.dialect.mysql.ast.MySqlUseIndexHint) MySqlIgnoreIndexHint(com.alibaba.druid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint)

Example 19 with SQLStatement

use of com.alibaba.druid.sql.ast.SQLStatement in project druid by alibaba.

the class PGSQLStatementParser method parseStatementListDialect.

public boolean parseStatementListDialect(List<SQLStatement> statementList) {
    if (lexer.token() == Token.WITH) {
        SQLStatement stmt = parseWith();
        statementList.add(stmt);
        return true;
    }
    return false;
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 20 with SQLStatement

use of com.alibaba.druid.sql.ast.SQLStatement in project druid by alibaba.

the class ExportColumns method evaluate.

public String evaluate(String sql, String dbType) {
    try {
        List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
        SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
        for (SQLStatement stmt : statementList) {
            stmt.accept(visitor);
        }
        StringBuffer buf = new StringBuffer();
        for (TableStat.Column column : visitor.getColumns()) {
            if (buf.length() != 0) {
                buf.append(',');
            }
            buf.append(column.toString());
        }
        return buf.toString();
    } catch (Throwable ex) {
        System.err.println("error sql : " + sql);
        ex.printStackTrace();
        return null;
    }
}
Also used : TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Aggregations

SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1652 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)915 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)431 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)346 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)273 OracleSchemaStatVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor)254 Column (com.alibaba.druid.stat.TableStat.Column)122 PGSQLStatementParser (com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)109 PGSchemaStatVisitor (com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor)107 SQLServerStatementParser (com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser)100 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)67 MySqlOutputVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor)60 SQLServerSchemaStatVisitor (com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor)46 OdpsStatementParser (com.alibaba.druid.sql.dialect.odps.parser.OdpsStatementParser)44 DB2SchemaStatVisitor (com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor)34 DB2StatementParser (com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser)33 Test (org.junit.Test)31 MysqlTest (com.alibaba.druid.sql.MysqlTest)29 ArrayList (java.util.ArrayList)29 SQLASTOutputVisitor (com.alibaba.druid.sql.visitor.SQLASTOutputVisitor)28