Search in sources :

Example 1 with BlinkCreateTableStatement

use of com.alibaba.druid.sql.dialect.blink.ast.BlinkCreateTableStatement in project druid by alibaba.

the class BlinkOutputVisitor method printTableElements.

protected void printTableElements(List<SQLTableElement> tableElementList) {
    int size = tableElementList.size();
    if (size == 0) {
        return;
    }
    BlinkCreateTableStatement stmt = (BlinkCreateTableStatement) tableElementList.get(0).getParent();
    print0(" (");
    this.indentCount++;
    println();
    for (int i = 0; i < size; ++i) {
        SQLTableElement element = tableElementList.get(i);
        element.accept(this);
        if (i != size - 1) {
            print(',');
        }
        if (this.isPrettyFormat() && element.hasAfterComment()) {
            print(' ');
            printlnComment(element.getAfterCommentsDirect());
        }
        if (i != size - 1) {
            println();
        }
    }
    if (stmt.getPeriodFor() != null) {
        print(',');
        println();
        print0(ucase ? "PERIODĀ FOR " : "periodĀ for ");
        stmt.getPeriodFor().accept(this);
    }
    this.indentCount--;
    println();
    print(')');
}
Also used : BlinkCreateTableStatement(com.alibaba.druid.sql.dialect.blink.ast.BlinkCreateTableStatement) SQLTableElement(com.alibaba.druid.sql.ast.statement.SQLTableElement)

Example 2 with BlinkCreateTableStatement

use of com.alibaba.druid.sql.dialect.blink.ast.BlinkCreateTableStatement in project druid by alibaba.

the class BlinkCreateTableParser method parseCreateTable.

public SQLCreateTableStatement parseCreateTable(boolean acceptCreate) {
    BlinkCreateTableStatement stmt = new BlinkCreateTableStatement();
    if (acceptCreate) {
        accept(Token.CREATE);
    }
    if (lexer.identifierEquals(FnvHash.Constants.EXTERNAL)) {
        lexer.nextToken();
        stmt.setExternal(true);
    }
    accept(Token.TABLE);
    if (lexer.token() == Token.IF || lexer.identifierEquals("IF")) {
        lexer.nextToken();
        accept(Token.NOT);
        accept(Token.EXISTS);
        stmt.setIfNotExiists(true);
    }
    stmt.setName(this.exprParser.name());
    accept(Token.LPAREN);
    if (lexer.isKeepComments() && lexer.hasComment()) {
        stmt.addBodyBeforeComment(lexer.readAndResetComments());
    }
    for_: for (; ; ) {
        SQLColumnDefinition column = null;
        switch(lexer.token()) {
            case IDENTIFIER:
            case KEY:
                column = this.exprParser.parseColumn();
                column.setParent(stmt);
                stmt.getTableElementList().add(column);
                break;
            case PRIMARY:
                SQLTableConstraint constraint = this.parseConstraint();
                constraint.setParent(stmt);
                stmt.getTableElementList().add(constraint);
                break;
            case PERIOD:
                lexer.nextToken();
                accept(Token.FOR);
                SQLExpr periodFor = this.exprParser.primary();
                stmt.setPeriodFor(periodFor);
                break for_;
            default:
                throw new ParserException("expect identifier. " + lexer.info());
        }
        if (lexer.isKeepComments() && lexer.hasComment() && column != null) {
            column.addAfterComment(lexer.readAndResetComments());
        }
        if (!(lexer.token() == (Token.COMMA))) {
            break;
        } else {
            lexer.nextToken();
            if (lexer.isKeepComments() && lexer.hasComment() && column != null) {
                column.addAfterComment(lexer.readAndResetComments());
            }
        }
    }
    accept(Token.RPAREN);
    if (lexer.token() == Token.COMMENT) {
        lexer.nextToken();
        stmt.setComment(this.exprParser.primary());
    }
    if (stmt.getClusteredBy().size() > 0 || stmt.getSortedBy().size() > 0) {
        accept(Token.INTO);
        if (lexer.token() == Token.LITERAL_INT) {
            stmt.setBuckets(lexer.integerValue().intValue());
            lexer.nextToken();
        } else {
            throw new ParserException("into buckets must be integer. " + lexer.info());
        }
        acceptIdentifier("BUCKETS");
    }
    if (lexer.token() == Token.WITH) {
        lexer.nextToken();
        accept(Token.LPAREN);
        parseAssignItems(stmt.getTableOptions(), stmt, true);
        accept(Token.RPAREN);
    }
    return stmt;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) BlinkCreateTableStatement(com.alibaba.druid.sql.dialect.blink.ast.BlinkCreateTableStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

BlinkCreateTableStatement (com.alibaba.druid.sql.dialect.blink.ast.BlinkCreateTableStatement)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLTableElement (com.alibaba.druid.sql.ast.statement.SQLTableElement)1 ParserException (com.alibaba.druid.sql.parser.ParserException)1