Search in sources :

Example 1 with MySqlSubPartitionByKey

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSubPartitionByKey in project druid by alibaba.

the class MySqlCreateTableParser method partitionClauseRest.

protected void partitionClauseRest(SQLPartitionBy clause) {
    if (identifierEquals("PARTITIONS")) {
        lexer.nextToken();
        SQLIntegerExpr countExpr = this.exprParser.integerExpr();
        clause.setPartitionsCount(countExpr);
    }
    if (lexer.token() == Token.PARTITION) {
        lexer.nextToken();
        if (identifierEquals("NUM")) {
            lexer.nextToken();
        }
        clause.setPartitionsCount(this.exprParser.expr());
        clause.putAttribute("ads.partition", Boolean.TRUE);
    }
    if (identifierEquals("SUBPARTITION")) {
        lexer.nextToken();
        accept(Token.BY);
        SQLSubPartitionBy subPartitionByClause = null;
        boolean linear = false;
        if (identifierEquals("LINEAR")) {
            lexer.nextToken();
            linear = true;
        }
        if (lexer.token() == Token.KEY) {
            MySqlSubPartitionByKey subPartitionKey = new MySqlSubPartitionByKey();
            lexer.nextToken();
            if (linear) {
                clause.setLinear(true);
            }
            accept(Token.LPAREN);
            for (; ; ) {
                subPartitionKey.addColumn(this.exprParser.name());
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                }
                break;
            }
            accept(Token.RPAREN);
            subPartitionByClause = subPartitionKey;
        } else if (identifierEquals("HASH")) {
            lexer.nextToken();
            SQLSubPartitionByHash subPartitionHash = new SQLSubPartitionByHash();
            if (linear) {
                clause.setLinear(true);
            }
            if (lexer.token() == Token.KEY) {
                lexer.nextToken();
                subPartitionHash.setKey(true);
            }
            accept(Token.LPAREN);
            subPartitionHash.setExpr(this.exprParser.expr());
            accept(Token.RPAREN);
            subPartitionByClause = subPartitionHash;
        } else if (identifierEquals("LIST")) {
            lexer.nextToken();
            MySqlSubPartitionByList subPartitionList = new MySqlSubPartitionByList();
            if (lexer.token() == Token.LPAREN) {
                lexer.nextToken();
                SQLExpr expr = this.exprParser.expr();
                if (expr instanceof SQLIdentifierExpr && (identifierEquals("bigint") || identifierEquals("long"))) {
                    String dataType = lexer.stringVal();
                    lexer.nextToken();
                    SQLColumnDefinition column = this.exprParser.createColumnDefinition();
                    column.setName((SQLIdentifierExpr) expr);
                    column.setDataType(new SQLDataTypeImpl(dataType));
                    subPartitionList.addColumn(column);
                    subPartitionList.putAttribute("ads.subPartitionList", Boolean.TRUE);
                } else {
                    subPartitionList.setExpr(expr);
                }
                accept(Token.RPAREN);
            } else {
                acceptIdentifier("COLUMNS");
                accept(Token.LPAREN);
                for (; ; ) {
                    subPartitionList.addColumn(this.exprParser.parseColumn());
                    if (lexer.token() == Token.COMMA) {
                        lexer.nextToken();
                        continue;
                    }
                    break;
                }
                accept(Token.RPAREN);
            }
            subPartitionByClause = subPartitionList;
        }
        if (identifierEquals("SUBPARTITION")) {
            lexer.nextToken();
            acceptIdentifier("OPTIONS");
            accept(Token.LPAREN);
            SQLAssignItem option = this.exprParser.parseAssignItem();
            accept(Token.RPAREN);
            option.setParent(subPartitionByClause);
            subPartitionByClause.getOptions().add(option);
        }
        if (identifierEquals("SUBPARTITIONS")) {
            lexer.nextToken();
            Number intValue = lexer.integerValue();
            SQLNumberExpr numExpr = new SQLNumberExpr(intValue);
            subPartitionByClause.setSubPartitionsCount(numExpr);
            lexer.nextToken();
        }
        if (subPartitionByClause != null) {
            subPartitionByClause.setLinear(linear);
            clause.setSubPartitionBy(subPartitionByClause);
        }
    }
}
Also used : MySqlSubPartitionByList(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSubPartitionByList) SQLSubPartitionByHash(com.alibaba.druid.sql.ast.SQLSubPartitionByHash) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLAssignItem(com.alibaba.druid.sql.ast.statement.SQLAssignItem) MySqlSubPartitionByKey(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSubPartitionByKey) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLDataTypeImpl(com.alibaba.druid.sql.ast.SQLDataTypeImpl) SQLSubPartitionBy(com.alibaba.druid.sql.ast.SQLSubPartitionBy)

Aggregations

SQLDataTypeImpl (com.alibaba.druid.sql.ast.SQLDataTypeImpl)1 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLSubPartitionBy (com.alibaba.druid.sql.ast.SQLSubPartitionBy)1 SQLSubPartitionByHash (com.alibaba.druid.sql.ast.SQLSubPartitionByHash)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)1 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)1 SQLAssignItem (com.alibaba.druid.sql.ast.statement.SQLAssignItem)1 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)1 MySqlSubPartitionByKey (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSubPartitionByKey)1 MySqlSubPartitionByList (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSubPartitionByList)1