Search in sources :

Example 1 with Expression

use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.

the class MySQLDDLParser method columnDefinition.

// column_definition:
// data_type [NOT NULL | NULL] [DEFAULT default_value]
// [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
// [COMMENT 'string']
// [COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}]
// [reference_definition]
private ColumnDefinition columnDefinition() throws SQLSyntaxErrorException {
    DataType dataType = dataType();
    boolean notNull = false;
    Expression defaultVal = null;
    boolean autoIncrement = false;
    ColumnDefinition.SpecialIndex sindex = null;
    ColumnDefinition.ColumnFormat format = null;
    LiteralString comment = null;
    if (lexer.token() == KW_NOT) {
        lexer.nextToken();
        match(LITERAL_NULL);
        notNull = true;
    } else if (lexer.token() == LITERAL_NULL) {
        lexer.nextToken();
    }
    if (lexer.token() == KW_DEFAULT) {
        lexer.nextToken();
        defaultVal = exprParser.expression();
        if (!(defaultVal instanceof Literal)) {
            throw new SQLSyntaxErrorException("default value of column must be a literal: " + defaultVal);
        }
    }
    if (lexer.token() == IDENTIFIER && "AUTO_INCREMENT".equals(lexer.stringValueUppercase())) {
        lexer.nextToken();
        autoIncrement = true;
    }
    switch(lexer.token()) {
        case KW_UNIQUE:
            if (lexer.nextToken() == KW_KEY) {
                lexer.nextToken();
            }
            sindex = ColumnDefinition.SpecialIndex.UNIQUE;
            break;
        case KW_PRIMARY:
            lexer.nextToken();
        case KW_KEY:
            match(KW_KEY);
            sindex = ColumnDefinition.SpecialIndex.PRIMARY;
            break;
    }
    if (lexer.token() == IDENTIFIER && "COMMENT".equals(lexer.stringValueUppercase())) {
        lexer.nextToken();
        comment = (LiteralString) exprParser.expression();
    }
    if (lexer.token() == IDENTIFIER && "COLUMN_FORMAT".equals(lexer.stringValueUppercase())) {
        switch(lexer.nextToken()) {
            case KW_DEFAULT:
                lexer.nextToken();
                format = ColumnDefinition.ColumnFormat.DEFAULT;
                break;
            case IDENTIFIER:
                SpecialIdentifier si = specialIdentifiers.get(lexer.stringValueUppercase());
                if (si != null) {
                    switch(si) {
                        case FIXED:
                            lexer.nextToken();
                            format = ColumnDefinition.ColumnFormat.FIXED;
                            break;
                        case DYNAMIC:
                            lexer.nextToken();
                            format = ColumnDefinition.ColumnFormat.DYNAMIC;
                            break;
                    }
                }
        }
    }
    return new ColumnDefinition(dataType, notNull, defaultVal, autoIncrement, sindex, comment, format);
}
Also used : LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString) Expression(com.alibaba.cobar.parser.ast.expression.Expression) Literal(com.alibaba.cobar.parser.ast.expression.primary.literal.Literal) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) DataType(com.alibaba.cobar.parser.ast.fragment.ddl.datatype.DataType) ColumnDefinition(com.alibaba.cobar.parser.ast.fragment.ddl.ColumnDefinition)

Example 2 with Expression

use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.

the class MySQLDDLParser method createTableDefs.

private void createTableDefs(DDLCreateTableStatement stmt) throws SQLSyntaxErrorException {
    if (lexer.token() != PUNC_LEFT_PAREN) {
        return;
    }
    match(PUNC_LEFT_PAREN);
    IndexDefinition indexDef;
    Identifier id;
    for (int i = 0; lexer.token() != PUNC_RIGHT_PAREN; ++i) {
        if (i > 0) {
            match(PUNC_COMMA);
        }
        switch(lexer.token()) {
            case KW_PRIMARY:
                lexer.nextToken();
                match(KW_KEY);
                indexDef = indexDefinition();
                stmt.setPrimaryKey(indexDef);
                break;
            case KW_INDEX:
            case KW_KEY:
                lexer.nextToken();
                if (lexer.token() == IDENTIFIER) {
                    id = identifier();
                } else {
                    id = null;
                }
                indexDef = indexDefinition();
                stmt.addIndex(id, indexDef);
                break;
            case KW_UNIQUE:
                switch(lexer.nextToken()) {
                    case KW_INDEX:
                    case KW_KEY:
                        lexer.nextToken();
                        break;
                }
                if (lexer.token() == IDENTIFIER) {
                    id = identifier();
                } else {
                    id = null;
                }
                indexDef = indexDefinition();
                stmt.addUniqueIndex(id, indexDef);
                break;
            case KW_FULLTEXT:
                switch(lexer.nextToken()) {
                    case KW_INDEX:
                    case KW_KEY:
                        lexer.nextToken();
                        break;
                }
                if (lexer.token() == IDENTIFIER) {
                    id = identifier();
                } else {
                    id = null;
                }
                indexDef = indexDefinition();
                if (indexDef.getIndexType() != null) {
                    throw new SQLSyntaxErrorException("FULLTEXT INDEX can specify no index_type");
                }
                stmt.addFullTextIndex(id, indexDef);
                break;
            case KW_SPATIAL:
                switch(lexer.nextToken()) {
                    case KW_INDEX:
                    case KW_KEY:
                        lexer.nextToken();
                        break;
                }
                if (lexer.token() == IDENTIFIER) {
                    id = identifier();
                } else {
                    id = null;
                }
                indexDef = indexDefinition();
                if (indexDef.getIndexType() != null) {
                    throw new SQLSyntaxErrorException("SPATIAL INDEX can specify no index_type");
                }
                stmt.addSpatialIndex(id, indexDef);
                break;
            case KW_CHECK:
                lexer.nextToken();
                match(PUNC_LEFT_PAREN);
                Expression expr = exprParser.expression();
                match(PUNC_RIGHT_PAREN);
                stmt.addCheck(expr);
                break;
            case IDENTIFIER:
                Identifier columnName = identifier();
                ColumnDefinition columnDef = columnDefinition();
                stmt.addColumnDefinition(columnName, columnDef);
                break;
            default:
                throw new SQLSyntaxErrorException("unsupportted column definition");
        }
    }
    match(PUNC_RIGHT_PAREN);
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) IndexDefinition(com.alibaba.cobar.parser.ast.fragment.ddl.index.IndexDefinition) Expression(com.alibaba.cobar.parser.ast.expression.Expression) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) ColumnDefinition(com.alibaba.cobar.parser.ast.fragment.ddl.ColumnDefinition)

Example 3 with Expression

use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.

the class MySQLDDLParser method indexColumnName.

private IndexColumnName indexColumnName() throws SQLSyntaxErrorException {
    // col_name [(length)] [ASC | DESC]
    Identifier colName = identifier();
    Expression len = null;
    if (lexer.token() == PUNC_LEFT_PAREN) {
        lexer.nextToken();
        len = exprParser.expression();
        match(PUNC_RIGHT_PAREN);
    }
    switch(lexer.token()) {
        case KW_ASC:
            lexer.nextToken();
            return new IndexColumnName(colName, len, true);
        case KW_DESC:
            lexer.nextToken();
            return new IndexColumnName(colName, len, false);
        default:
            return new IndexColumnName(colName, len, true);
    }
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) Expression(com.alibaba.cobar.parser.ast.expression.Expression) IndexColumnName(com.alibaba.cobar.parser.ast.fragment.ddl.index.IndexColumnName)

Example 4 with Expression

use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.

the class MySQLDDLParser method tableOption.

private boolean tableOption(TableOptions options) throws SQLSyntaxErrorException {
    Identifier id = null;
    Expression expr = null;
    os: switch(lexer.token()) {
        case KW_CHARACTER:
            lexer.nextToken();
            match(KW_SET);
            if (lexer.token() == OP_EQUALS) {
                lexer.nextToken();
            }
            id = identifier();
            options.setCharSet(id);
            break;
        case KW_COLLATE:
            lexer.nextToken();
            if (lexer.token() == OP_EQUALS) {
                lexer.nextToken();
            }
            id = identifier();
            options.setCollation(id);
            break;
        case KW_DEFAULT:
            // | [DEFAULT] COLLATE [=] collation_name
            switch(lexer.nextToken()) {
                case KW_CHARACTER:
                    lexer.nextToken();
                    match(KW_SET);
                    if (lexer.token() == OP_EQUALS) {
                        lexer.nextToken();
                    }
                    id = identifier();
                    options.setCharSet(id);
                    break os;
                case KW_COLLATE:
                    lexer.nextToken();
                    if (lexer.token() == OP_EQUALS) {
                        lexer.nextToken();
                    }
                    id = identifier();
                    options.setCollation(id);
                    break os;
                case IDENTIFIER:
                    SpecialIdentifier si = specialIdentifiers.get(lexer.stringValueUppercase());
                    if (si != null) {
                        switch(si) {
                            case CHARSET:
                                lexer.nextToken();
                                if (lexer.token() == OP_EQUALS) {
                                    lexer.nextToken();
                                }
                                id = identifier();
                                options.setCharSet(id);
                                break os;
                        }
                    }
                default:
                    lexer.addCacheToke(KW_DEFAULT);
                    return false;
            }
        case KW_INDEX:
            // | INDEX DIRECTORY [=] 'absolute path to directory'
            lexer.nextToken();
            if (lexer.token() == IDENTIFIER && "DIRECTORY".equals(lexer.stringValueUppercase())) {
                if (lexer.nextToken() == OP_EQUALS) {
                    lexer.nextToken();
                }
                options.setIndexDir((LiteralString) exprParser.expression());
                break;
            }
            lexer.addCacheToke(KW_INDEX);
            return true;
        case KW_UNION:
            // | UNION [=] (tbl_name[,tbl_name]...)
            if (lexer.nextToken() == OP_EQUALS) {
                lexer.nextToken();
            }
            match(PUNC_LEFT_PAREN);
            List<Identifier> union = new ArrayList<Identifier>(2);
            for (int j = 0; lexer.token() != PUNC_RIGHT_PAREN; ++j) {
                if (j > 0)
                    match(PUNC_COMMA);
                id = identifier();
                union.add(id);
            }
            match(PUNC_RIGHT_PAREN);
            options.setUnion(union);
            break os;
        case IDENTIFIER:
            SpecialIdentifier si = specialIdentifiers.get(lexer.stringValueUppercase());
            if (si != null) {
                switch(si) {
                    case CHARSET:
                        // CHARSET [=] charset_name
                        lexer.nextToken();
                        if (lexer.token() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        id = identifier();
                        options.setCharSet(id);
                        break os;
                    case ENGINE:
                        // ENGINE [=] engine_name
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        id = identifier();
                        options.setEngine(id);
                        break os;
                    case AUTO_INCREMENT:
                        // | AUTO_INCREMENT [=] value
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        expr = exprParser.expression();
                        options.setAutoIncrement(expr);
                        break os;
                    case AVG_ROW_LENGTH:
                        // | AVG_ROW_LENGTH [=] value
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        expr = exprParser.expression();
                        options.setAvgRowLength(expr);
                        break os;
                    case CHECKSUM:
                        // | CHECKSUM [=] {0 | 1}
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        switch(lexer.token()) {
                            case LITERAL_BOOL_FALSE:
                                lexer.nextToken();
                                options.setCheckSum(false);
                            case LITERAL_BOOL_TRUE:
                                lexer.nextToken();
                                options.setCheckSum(true);
                                break;
                            case LITERAL_NUM_PURE_DIGIT:
                                int intVal = lexer.integerValue().intValue();
                                lexer.nextToken();
                                if (intVal == 0) {
                                    options.setCheckSum(false);
                                } else {
                                    options.setCheckSum(true);
                                }
                                break;
                            default:
                                throw new SQLSyntaxErrorException("table option of CHECKSUM error");
                        }
                        break os;
                    case DELAY_KEY_WRITE:
                        // | DELAY_KEY_WRITE [=] {0 | 1}
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        switch(lexer.token()) {
                            case LITERAL_BOOL_FALSE:
                                lexer.nextToken();
                                options.setDelayKeyWrite(false);
                            case LITERAL_BOOL_TRUE:
                                lexer.nextToken();
                                options.setDelayKeyWrite(true);
                                break;
                            case LITERAL_NUM_PURE_DIGIT:
                                int intVal = lexer.integerValue().intValue();
                                lexer.nextToken();
                                if (intVal == 0) {
                                    options.setDelayKeyWrite(false);
                                } else {
                                    options.setDelayKeyWrite(true);
                                }
                                break;
                            default:
                                throw new SQLSyntaxErrorException("table option of DELAY_KEY_WRITE error");
                        }
                        break os;
                    case COMMENT:
                        // | COMMENT [=] 'string'
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        options.setComment((LiteralString) exprParser.expression());
                        break os;
                    case CONNECTION:
                        // | CONNECTION [=] 'connect_string'
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        options.setConnection((LiteralString) exprParser.expression());
                        break os;
                    case DATA:
                        // | DATA DIRECTORY [=] 'absolute path to directory'
                        lexer.nextToken();
                        matchIdentifier("DIRECTORY");
                        if (lexer.token() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        options.setDataDir((LiteralString) exprParser.expression());
                        break os;
                    case INSERT_METHOD:
                        // | INSERT_METHOD [=] { NO | FIRST | LAST }
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        switch(matchIdentifier("NO", "FIRST", "LAST")) {
                            case 0:
                                options.setInsertMethod(TableOptions.InsertMethod.NO);
                                break;
                            case 1:
                                options.setInsertMethod(TableOptions.InsertMethod.FIRST);
                                break;
                            case 2:
                                options.setInsertMethod(TableOptions.InsertMethod.LAST);
                                break;
                        }
                        break os;
                    case KEY_BLOCK_SIZE:
                        // | KEY_BLOCK_SIZE [=] value
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        options.setKeyBlockSize(exprParser.expression());
                        break os;
                    case MAX_ROWS:
                        // | MAX_ROWS [=] value
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        options.setMaxRows(exprParser.expression());
                        break os;
                    case MIN_ROWS:
                        // | MIN_ROWS [=] value
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        options.setMinRows(exprParser.expression());
                        break os;
                    case PACK_KEYS:
                        // | PACK_KEYS [=] {0 | 1 | DEFAULT}
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        switch(lexer.token()) {
                            case LITERAL_BOOL_FALSE:
                                lexer.nextToken();
                                options.setPackKeys(TableOptions.PackKeys.FALSE);
                                break;
                            case LITERAL_BOOL_TRUE:
                                lexer.nextToken();
                                options.setPackKeys(TableOptions.PackKeys.TRUE);
                                break;
                            case LITERAL_NUM_PURE_DIGIT:
                                int intVal = lexer.integerValue().intValue();
                                lexer.nextToken();
                                if (intVal == 0) {
                                    options.setPackKeys(TableOptions.PackKeys.FALSE);
                                } else {
                                    options.setPackKeys(TableOptions.PackKeys.TRUE);
                                }
                                break;
                            case KW_DEFAULT:
                                lexer.nextToken();
                                options.setPackKeys(TableOptions.PackKeys.DEFAULT);
                                break;
                            default:
                                throw new SQLSyntaxErrorException("table option of PACK_KEYS error");
                        }
                        break os;
                    case PASSWORD:
                        // | PASSWORD [=] 'string'
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        options.setPassword((LiteralString) exprParser.expression());
                        break os;
                    case ROW_FORMAT:
                        // {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT}
                        if (lexer.nextToken() == OP_EQUALS) {
                            lexer.nextToken();
                        }
                        switch(lexer.token()) {
                            case KW_DEFAULT:
                                lexer.nextToken();
                                options.setRowFormat(TableOptions.RowFormat.DEFAULT);
                                break os;
                            case IDENTIFIER:
                                SpecialIdentifier sid = specialIdentifiers.get(lexer.stringValueUppercase());
                                if (sid != null) {
                                    switch(sid) {
                                        case DYNAMIC:
                                            lexer.nextToken();
                                            options.setRowFormat(TableOptions.RowFormat.DYNAMIC);
                                            break os;
                                        case FIXED:
                                            lexer.nextToken();
                                            options.setRowFormat(TableOptions.RowFormat.FIXED);
                                            break os;
                                        case COMPRESSED:
                                            lexer.nextToken();
                                            options.setRowFormat(TableOptions.RowFormat.COMPRESSED);
                                            break os;
                                        case REDUNDANT:
                                            lexer.nextToken();
                                            options.setRowFormat(TableOptions.RowFormat.REDUNDANT);
                                            break os;
                                        case COMPACT:
                                            lexer.nextToken();
                                            options.setRowFormat(TableOptions.RowFormat.COMPACT);
                                            break os;
                                    }
                                }
                            default:
                                throw new SQLSyntaxErrorException("table option of ROW_FORMAT error");
                        }
                }
            }
        default:
            return false;
    }
    return true;
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString) Expression(com.alibaba.cobar.parser.ast.expression.Expression) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 5 with Expression

use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.

the class MySQLDDLParser method dataType.

// data_type:
// | DATE
// | TIME
// | TIMESTAMP
// | DATETIME
// | YEAR
// | spatial_type
private DataType dataType() throws SQLSyntaxErrorException {
    DataType.DataTypeName typeName = null;
    boolean unsigned = false;
    boolean zerofill = false;
    /**
     * for text only
     */
    boolean binary = false;
    Expression length = null;
    Expression decimals = null;
    Identifier charSet = null;
    Identifier collation = null;
    List<Expression> collectionVals = null;
    typeName: switch(lexer.token()) {
        case KW_TINYINT:
            // | TINYINT[(length)] [UNSIGNED] [ZEROFILL]
            typeName = DataType.DataTypeName.TINYINT;
            if (lexer.nextToken() == PUNC_LEFT_PAREN) {
                lexer.nextToken();
                length = exprParser.expression();
                match(PUNC_RIGHT_PAREN);
            }
            if (lexer.token() == KW_UNSIGNED) {
                unsigned = true;
                lexer.nextToken();
            }
            if (lexer.token() == KW_ZEROFILL) {
                zerofill = true;
                lexer.nextToken();
            }
            break typeName;
        case KW_SMALLINT:
            // | SMALLINT[(length)] [UNSIGNED] [ZEROFILL]
            typeName = DataType.DataTypeName.SMALLINT;
            if (lexer.nextToken() == PUNC_LEFT_PAREN) {
                lexer.nextToken();
                length = exprParser.expression();
                match(PUNC_RIGHT_PAREN);
            }
            if (lexer.token() == KW_UNSIGNED) {
                unsigned = true;
                lexer.nextToken();
            }
            if (lexer.token() == KW_ZEROFILL) {
                zerofill = true;
                lexer.nextToken();
            }
            break typeName;
        case KW_MEDIUMINT:
            // | MEDIUMINT[(length)] [UNSIGNED] [ZEROFILL]
            typeName = DataType.DataTypeName.MEDIUMINT;
            if (lexer.nextToken() == PUNC_LEFT_PAREN) {
                lexer.nextToken();
                length = exprParser.expression();
                match(PUNC_RIGHT_PAREN);
            }
            if (lexer.token() == KW_UNSIGNED) {
                unsigned = true;
                lexer.nextToken();
            }
            if (lexer.token() == KW_ZEROFILL) {
                zerofill = true;
                lexer.nextToken();
            }
            break typeName;
        case KW_INTEGER:
        case KW_INT:
            // | INT[(length)] [UNSIGNED] [ZEROFILL]
            // | INTEGER[(length)] [UNSIGNED] [ZEROFILL]
            typeName = DataType.DataTypeName.INT;
            if (lexer.nextToken() == PUNC_LEFT_PAREN) {
                lexer.nextToken();
                length = exprParser.expression();
                match(PUNC_RIGHT_PAREN);
            }
            if (lexer.token() == KW_UNSIGNED) {
                unsigned = true;
                lexer.nextToken();
            }
            if (lexer.token() == KW_ZEROFILL) {
                zerofill = true;
                lexer.nextToken();
            }
            break typeName;
        case KW_BIGINT:
            // | BIGINT[(length)] [UNSIGNED] [ZEROFILL]
            typeName = DataType.DataTypeName.BIGINT;
            if (lexer.nextToken() == PUNC_LEFT_PAREN) {
                lexer.nextToken();
                length = exprParser.expression();
                match(PUNC_RIGHT_PAREN);
            }
            if (lexer.token() == KW_UNSIGNED) {
                unsigned = true;
                lexer.nextToken();
            }
            if (lexer.token() == KW_ZEROFILL) {
                zerofill = true;
                lexer.nextToken();
            }
            break typeName;
        case KW_REAL:
            // | REAL[(length,decimals)] [UNSIGNED] [ZEROFILL]
            typeName = DataType.DataTypeName.REAL;
            if (lexer.nextToken() == PUNC_LEFT_PAREN) {
                lexer.nextToken();
                length = exprParser.expression();
                match(PUNC_COMMA);
                decimals = exprParser.expression();
                match(PUNC_RIGHT_PAREN);
            }
            if (lexer.token() == KW_UNSIGNED) {
                unsigned = true;
                lexer.nextToken();
            }
            if (lexer.token() == KW_ZEROFILL) {
                zerofill = true;
                lexer.nextToken();
            }
            break typeName;
        case KW_DOUBLE:
            // | DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL]
            typeName = DataType.DataTypeName.DOUBLE;
            if (lexer.nextToken() == PUNC_LEFT_PAREN) {
                lexer.nextToken();
                length = exprParser.expression();
                match(PUNC_COMMA);
                decimals = exprParser.expression();
                match(PUNC_RIGHT_PAREN);
            }
            if (lexer.token() == KW_UNSIGNED) {
                unsigned = true;
                lexer.nextToken();
            }
            if (lexer.token() == KW_ZEROFILL) {
                zerofill = true;
                lexer.nextToken();
            }
            break typeName;
        case KW_FLOAT:
            // | FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL]
            typeName = DataType.DataTypeName.FLOAT;
            if (lexer.nextToken() == PUNC_LEFT_PAREN) {
                lexer.nextToken();
                length = exprParser.expression();
                match(PUNC_COMMA);
                decimals = exprParser.expression();
                match(PUNC_RIGHT_PAREN);
            }
            if (lexer.token() == KW_UNSIGNED) {
                unsigned = true;
                lexer.nextToken();
            }
            if (lexer.token() == KW_ZEROFILL) {
                zerofill = true;
                lexer.nextToken();
            }
            break typeName;
        case KW_NUMERIC:
        case KW_DECIMAL:
        case KW_DEC:
            // | DECIMAL[(length[,decimals])] [UNSIGNED] [ZEROFILL]
            // | NUMERIC[(length[,decimals])] [UNSIGNED] [ZEROFILL]
            typeName = DataType.DataTypeName.DECIMAL;
            if (lexer.nextToken() == PUNC_LEFT_PAREN) {
                lexer.nextToken();
                length = exprParser.expression();
                if (lexer.token() == PUNC_COMMA) {
                    match(PUNC_COMMA);
                    decimals = exprParser.expression();
                }
                match(PUNC_RIGHT_PAREN);
            }
            if (lexer.token() == KW_UNSIGNED) {
                unsigned = true;
                lexer.nextToken();
            }
            if (lexer.token() == KW_ZEROFILL) {
                zerofill = true;
                lexer.nextToken();
            }
            break typeName;
        case KW_CHAR:
            // | CHAR[(length)] [CHARACTER SET charset_name] [COLLATE
            // collation_name]
            typeName = DataType.DataTypeName.CHAR;
            if (lexer.nextToken() == PUNC_LEFT_PAREN) {
                lexer.nextToken();
                length = exprParser.expression();
                match(PUNC_RIGHT_PAREN);
            }
            if (lexer.token() == KW_CHARACTER) {
                lexer.nextToken();
                match(KW_SET);
                charSet = identifier();
            }
            if (lexer.token() == KW_COLLATE) {
                lexer.nextToken();
                collation = identifier();
            }
            break typeName;
        case KW_VARCHAR:
            // | VARCHAR(length) [CHARACTER SET charset_name] [COLLATE
            // collation_name]
            typeName = DataType.DataTypeName.VARCHAR;
            lexer.nextToken();
            match(PUNC_LEFT_PAREN);
            length = exprParser.expression();
            match(PUNC_RIGHT_PAREN);
            if (lexer.token() == KW_CHARACTER) {
                lexer.nextToken();
                match(KW_SET);
                charSet = identifier();
            }
            if (lexer.token() == KW_COLLATE) {
                lexer.nextToken();
                collation = identifier();
            }
            break typeName;
        case KW_BINARY:
            // | BINARY[(length)]
            typeName = DataType.DataTypeName.BINARY;
            if (lexer.nextToken() == PUNC_LEFT_PAREN) {
                lexer.nextToken();
                length = exprParser.expression();
                match(PUNC_RIGHT_PAREN);
            }
            break typeName;
        case KW_VARBINARY:
            // | VARBINARY(length)
            typeName = DataType.DataTypeName.VARBINARY;
            lexer.nextToken();
            match(PUNC_LEFT_PAREN);
            length = exprParser.expression();
            match(PUNC_RIGHT_PAREN);
            break typeName;
        case KW_TINYBLOB:
            typeName = DataType.DataTypeName.TINYBLOB;
            lexer.nextToken();
            break typeName;
        case KW_BLOB:
            typeName = DataType.DataTypeName.BLOB;
            lexer.nextToken();
            break typeName;
        case KW_MEDIUMBLOB:
            typeName = DataType.DataTypeName.MEDIUMBLOB;
            lexer.nextToken();
            break typeName;
        case KW_LONGBLOB:
            typeName = DataType.DataTypeName.LONGBLOB;
            lexer.nextToken();
            break typeName;
        case KW_TINYTEXT:
            // | TINYTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE
            // collation_name]
            typeName = DataType.DataTypeName.TINYTEXT;
            if (lexer.nextToken() == KW_BINARY) {
                lexer.nextToken();
                binary = true;
            }
            if (lexer.token() == KW_CHARACTER) {
                lexer.nextToken();
                match(KW_SET);
                charSet = identifier();
            }
            if (lexer.token() == KW_COLLATE) {
                lexer.nextToken();
                collation = identifier();
            }
            break typeName;
        case KW_MEDIUMTEXT:
            // | MEDIUMTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE
            // collation_name]
            typeName = DataType.DataTypeName.MEDIUMTEXT;
            if (lexer.nextToken() == KW_BINARY) {
                lexer.nextToken();
                binary = true;
            }
            if (lexer.token() == KW_CHARACTER) {
                lexer.nextToken();
                match(KW_SET);
                charSet = identifier();
            }
            if (lexer.token() == KW_COLLATE) {
                lexer.nextToken();
                collation = identifier();
            }
            break typeName;
        case KW_LONGTEXT:
            // | LONGTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE
            // collation_name]
            typeName = DataType.DataTypeName.LONGTEXT;
            if (lexer.nextToken() == KW_BINARY) {
                lexer.nextToken();
                binary = true;
            }
            if (lexer.token() == KW_CHARACTER) {
                lexer.nextToken();
                match(KW_SET);
                charSet = identifier();
            }
            if (lexer.token() == KW_COLLATE) {
                lexer.nextToken();
                collation = identifier();
            }
            break typeName;
        case KW_SET:
            // | SET(value1,value2,value3,...) [CHARACTER SET charset_name]
            // [COLLATE collation_name]
            typeName = DataType.DataTypeName.SET;
            lexer.nextToken();
            match(PUNC_LEFT_PAREN);
            for (int i = 0; lexer.token() != PUNC_RIGHT_PAREN; ++i) {
                if (i > 0)
                    match(PUNC_COMMA);
                else
                    collectionVals = new ArrayList<Expression>(2);
                collectionVals.add(exprParser.expression());
            }
            match(PUNC_RIGHT_PAREN);
            if (lexer.token() == KW_CHARACTER) {
                lexer.nextToken();
                match(KW_SET);
                charSet = identifier();
            }
            if (lexer.token() == KW_COLLATE) {
                lexer.nextToken();
                collation = identifier();
            }
            break typeName;
        case IDENTIFIER:
            SpecialIdentifier si = specialIdentifiers.get(lexer.stringValueUppercase());
            if (si != null) {
                switch(si) {
                    case BIT:
                        // BIT[(length)]
                        typeName = DataType.DataTypeName.BIT;
                        if (lexer.nextToken() == PUNC_LEFT_PAREN) {
                            lexer.nextToken();
                            length = exprParser.expression();
                            match(PUNC_RIGHT_PAREN);
                        }
                        break typeName;
                    case DATE:
                        typeName = DataType.DataTypeName.DATE;
                        lexer.nextToken();
                        break typeName;
                    case TIME:
                        typeName = DataType.DataTypeName.TIME;
                        lexer.nextToken();
                        break typeName;
                    case TIMESTAMP:
                        typeName = DataType.DataTypeName.TIMESTAMP;
                        lexer.nextToken();
                        break typeName;
                    case DATETIME:
                        typeName = DataType.DataTypeName.DATETIME;
                        lexer.nextToken();
                        break typeName;
                    case YEAR:
                        typeName = DataType.DataTypeName.YEAR;
                        lexer.nextToken();
                        break typeName;
                    case TEXT:
                        // | TEXT [BINARY] [CHARACTER SET charset_name] [COLLATE
                        // collation_name]
                        typeName = DataType.DataTypeName.TEXT;
                        if (lexer.nextToken() == KW_BINARY) {
                            lexer.nextToken();
                            binary = true;
                        }
                        if (lexer.token() == KW_CHARACTER) {
                            lexer.nextToken();
                            match(KW_SET);
                            charSet = identifier();
                        }
                        if (lexer.token() == KW_COLLATE) {
                            lexer.nextToken();
                            collation = identifier();
                        }
                        break typeName;
                    case ENUM:
                        // | ENUM(value1,value2,value3,...) [CHARACTER SET
                        // charset_name] [COLLATE collation_name]
                        typeName = DataType.DataTypeName.ENUM;
                        lexer.nextToken();
                        match(PUNC_LEFT_PAREN);
                        for (int i = 0; lexer.token() != PUNC_RIGHT_PAREN; ++i) {
                            if (i > 0)
                                match(PUNC_COMMA);
                            else
                                collectionVals = new ArrayList<Expression>(2);
                            collectionVals.add(exprParser.expression());
                        }
                        match(PUNC_RIGHT_PAREN);
                        if (lexer.token() == KW_CHARACTER) {
                            lexer.nextToken();
                            match(KW_SET);
                            charSet = identifier();
                        }
                        if (lexer.token() == KW_COLLATE) {
                            lexer.nextToken();
                            collation = identifier();
                        }
                        break typeName;
                }
            }
        default:
            return null;
    }
    return new DataType(typeName, unsigned, zerofill, binary, length, decimals, charSet, collation, collectionVals);
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) Expression(com.alibaba.cobar.parser.ast.expression.Expression) DataType(com.alibaba.cobar.parser.ast.fragment.ddl.datatype.DataType)

Aggregations

Expression (com.alibaba.cobar.parser.ast.expression.Expression)128 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)96 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)93 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)91 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)88 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)88 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)87 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)87 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)87 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)87 UserExpression (com.alibaba.cobar.parser.ast.expression.misc.UserExpression)87 CaseWhenOperatorExpression (com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression)87 LikeExpression (com.alibaba.cobar.parser.ast.expression.string.LikeExpression)87 CollateExpression (com.alibaba.cobar.parser.ast.expression.type.CollateExpression)87 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)76 MatchExpression (com.alibaba.cobar.parser.ast.expression.primary.MatchExpression)71 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)58 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)58 UnaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression)58 ArithmeticAddExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticAddExpression)45