Search in sources :

Example 1 with OdpsCreateTableStatement

use of com.alibaba.druid.sql.dialect.odps.ast.OdpsCreateTableStatement in project druid by alibaba.

the class OdpsCreateTableParser method parseCrateTable.

public SQLCreateTableStatement parseCrateTable(boolean acceptCreate) {
    OdpsCreateTableStatement stmt = new OdpsCreateTableStatement();
    if (acceptCreate) {
        accept(Token.CREATE);
    }
    accept(Token.TABLE);
    if (lexer.token() == Token.IF || identifierEquals("IF")) {
        lexer.nextToken();
        accept(Token.NOT);
        accept(Token.EXISTS);
        stmt.setIfNotExiists(true);
    }
    stmt.setName(this.exprParser.name());
    if (identifierEquals("LIFECYCLE")) {
        lexer.nextToken();
        stmt.setLifecycle(this.exprParser.expr());
    }
    if (lexer.token() == Token.LIKE) {
        lexer.nextToken();
        SQLName name = this.exprParser.name();
        stmt.setLike(name);
    } else if (lexer.token() == Token.AS) {
        lexer.nextToken();
        OdpsSelectParser selectParser = new OdpsSelectParser(this.exprParser);
        SQLSelect select = selectParser.select();
        stmt.setSelect(select);
    } else {
        accept(Token.LPAREN);
        if (lexer.isKeepComments() && lexer.hasComment()) {
            stmt.addBodyBeforeComment(lexer.readAndResetComments());
        }
        for (; ; ) {
            if (lexer.token() != Token.IDENTIFIER) {
                throw new ParserException("expect identifier");
            }
            SQLColumnDefinition column = this.exprParser.parseColumn();
            stmt.getTableElementList().add(column);
            if (lexer.isKeepComments() && lexer.hasComment()) {
                column.addAfterComment(lexer.readAndResetComments());
            }
            if (!(lexer.token() == (Token.COMMA))) {
                break;
            } else {
                lexer.nextToken();
                if (lexer.isKeepComments() && lexer.hasComment()) {
                    column.addAfterComment(lexer.readAndResetComments());
                }
            }
        }
        accept(Token.RPAREN);
    }
    if (lexer.token() == Token.COMMENT) {
        lexer.nextToken();
        stmt.setComment(this.exprParser.primary());
    }
    if (lexer.token() == Token.PARTITIONED) {
        lexer.nextToken();
        accept(Token.BY);
        accept(Token.LPAREN);
        for (; ; ) {
            if (lexer.token() != Token.IDENTIFIER) {
                throw new ParserException("expect identifier");
            }
            SQLColumnDefinition column = this.exprParser.parseColumn();
            stmt.addPartitionColumn(column);
            if (lexer.isKeepComments() && lexer.hasComment()) {
                column.addAfterComment(lexer.readAndResetComments());
            }
            if (!(lexer.token() == (Token.COMMA))) {
                break;
            } else {
                lexer.nextToken();
                if (lexer.isKeepComments() && lexer.hasComment()) {
                    column.addAfterComment(lexer.readAndResetComments());
                }
            }
        }
        accept(Token.RPAREN);
    }
    if (identifierEquals("LIFECYCLE")) {
        lexer.nextToken();
        stmt.setLifecycle(this.exprParser.expr());
    }
    return stmt;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) OdpsCreateTableStatement(com.alibaba.druid.sql.dialect.odps.ast.OdpsCreateTableStatement) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)

Aggregations

SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)1 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)1 OdpsCreateTableStatement (com.alibaba.druid.sql.dialect.odps.ast.OdpsCreateTableStatement)1 ParserException (com.alibaba.druid.sql.parser.ParserException)1