Search in sources :

Example 1 with ColumnDefinition

use of com.alibaba.cobar.parser.ast.fragment.ddl.ColumnDefinition 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 ColumnDefinition

use of com.alibaba.cobar.parser.ast.fragment.ddl.ColumnDefinition 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 ColumnDefinition

use of com.alibaba.cobar.parser.ast.fragment.ddl.ColumnDefinition in project cobar by alibaba.

the class MySQLDDLParser method alterTable.

/**
 * token of table name has been consumed
 *
 * @throws SQLSyntaxErrorException
 */
private DDLAlterTableStatement alterTable(DDLAlterTableStatement stmt) throws SQLSyntaxErrorException {
    TableOptions options = new TableOptions();
    stmt.setTableOptions(options);
    Identifier id = null;
    Identifier id2 = null;
    Identifier id3 = null;
    ColumnDefinition colDef = null;
    IndexDefinition indexDef = null;
    Expression expr = null;
    for (int i = 0; lexer.token() != EOF; ++i) {
        if (i > 0) {
            match(PUNC_COMMA);
        }
        if (tableOptions(options)) {
            continue;
        }
        main_switch: switch(lexer.token()) {
            case KW_CONVERT:
                // | CONVERT TO CHARACTER SET charset_name [COLLATE
                // collation_name]
                lexer.nextToken();
                match(KW_TO);
                match(KW_CHARACTER);
                match(KW_SET);
                id = identifier();
                id2 = null;
                if (lexer.token() == KW_COLLATE) {
                    lexer.nextToken();
                    id2 = identifier();
                }
                stmt.setConvertCharset(new Pair<Identifier, Identifier>(id, id2));
                break main_switch;
            case KW_RENAME:
                // | RENAME [TO] new_tbl_name
                if (lexer.nextToken() == KW_TO) {
                    lexer.nextToken();
                }
                id = identifier();
                stmt.setRenameTo(id);
                break main_switch;
            case KW_DROP:
                drop_switch: switch(lexer.nextToken()) {
                    case KW_INDEX:
                    case KW_KEY:
                        // | DROP {INDEX|KEY} index_name
                        lexer.nextToken();
                        id = identifier();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.DropIndex(id));
                        break drop_switch;
                    case KW_PRIMARY:
                        // | DROP PRIMARY KEY
                        lexer.nextToken();
                        match(KW_KEY);
                        stmt.addAlterSpecification(new DDLAlterTableStatement.DropPrimaryKey());
                        break drop_switch;
                    case IDENTIFIER:
                        // | DROP [COLUMN] col_name
                        id = identifier();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.DropColumn(id));
                        break drop_switch;
                    case KW_COLUMN:
                        // | DROP [COLUMN] col_name
                        lexer.nextToken();
                        id = identifier();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.DropColumn(id));
                        break drop_switch;
                    default:
                        throw new SQLSyntaxErrorException("ALTER TABLE error for DROP");
                }
                break main_switch;
            case KW_CHANGE:
                // [FIRST|AFTER col_name]
                if (lexer.nextToken() == KW_COLUMN) {
                    lexer.nextToken();
                }
                id = identifier();
                id2 = identifier();
                colDef = columnDefinition();
                if (lexer.token() == IDENTIFIER) {
                    if ("FIRST".equals(lexer.stringValueUppercase())) {
                        lexer.nextToken();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.ChangeColumn(id, id2, colDef, null));
                    } else if ("AFTER".equals(lexer.stringValueUppercase())) {
                        lexer.nextToken();
                        id3 = identifier();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.ChangeColumn(id, id2, colDef, id3));
                    } else {
                        stmt.addAlterSpecification(new DDLAlterTableStatement.ChangeColumn(id, id2, colDef));
                    }
                } else {
                    stmt.addAlterSpecification(new DDLAlterTableStatement.ChangeColumn(id, id2, colDef));
                }
                break main_switch;
            case KW_ALTER:
                // DEFAULT}
                if (lexer.nextToken() == KW_COLUMN) {
                    lexer.nextToken();
                }
                id = identifier();
                switch(lexer.token()) {
                    case KW_SET:
                        lexer.nextToken();
                        match(KW_DEFAULT);
                        expr = exprParser.expression();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.AlterColumnDefaultVal(id, expr));
                        break;
                    case KW_DROP:
                        lexer.nextToken();
                        match(KW_DEFAULT);
                        stmt.addAlterSpecification(new DDLAlterTableStatement.AlterColumnDefaultVal(id));
                        break;
                    default:
                        throw new SQLSyntaxErrorException("ALTER TABLE error for ALTER");
                }
                break main_switch;
            case KW_ADD:
                add_switch: switch(lexer.nextToken()) {
                    case IDENTIFIER:
                        // | ADD [COLUMN] col_name column_definition [FIRST | AFTER
                        // col_name ]
                        id = identifier();
                        colDef = columnDefinition();
                        if (lexer.token() == IDENTIFIER) {
                            if ("FIRST".equals(lexer.stringValueUppercase())) {
                                lexer.nextToken();
                                stmt.addAlterSpecification(new DDLAlterTableStatement.AddColumn(id, colDef, null));
                            } else if ("AFTER".equals(lexer.stringValueUppercase())) {
                                lexer.nextToken();
                                id2 = identifier();
                                stmt.addAlterSpecification(new DDLAlterTableStatement.AddColumn(id, colDef, id2));
                            } else {
                                stmt.addAlterSpecification(new DDLAlterTableStatement.AddColumn(id, colDef));
                            }
                        } else {
                            stmt.addAlterSpecification(new DDLAlterTableStatement.AddColumn(id, colDef));
                        }
                        break add_switch;
                    case PUNC_LEFT_PAREN:
                        // | ADD [COLUMN] (col_name column_definition,...)
                        lexer.nextToken();
                        for (int j = 0; lexer.token() != PUNC_RIGHT_PAREN; ++j) {
                            DDLAlterTableStatement.AddColumns addColumns = new DDLAlterTableStatement.AddColumns();
                            stmt.addAlterSpecification(addColumns);
                            if (j > 0) {
                                match(PUNC_COMMA);
                            }
                            id = identifier();
                            colDef = columnDefinition();
                            addColumns.addColumn(id, colDef);
                        }
                        match(PUNC_RIGHT_PAREN);
                        break add_switch;
                    case KW_COLUMN:
                        if (lexer.nextToken() == PUNC_LEFT_PAREN) {
                            // | ADD [COLUMN] (col_name column_definition,...)
                            lexer.nextToken();
                            for (int j = 0; lexer.token() != PUNC_RIGHT_PAREN; ++j) {
                                DDLAlterTableStatement.AddColumns addColumns = new DDLAlterTableStatement.AddColumns();
                                stmt.addAlterSpecification(addColumns);
                                if (j > 0) {
                                    match(PUNC_COMMA);
                                }
                                id = identifier();
                                colDef = columnDefinition();
                                addColumns.addColumn(id, colDef);
                            }
                            match(PUNC_RIGHT_PAREN);
                        } else {
                            // | ADD [COLUMN] col_name column_definition [FIRST |
                            // AFTER col_name ]
                            id = identifier();
                            colDef = columnDefinition();
                            if (lexer.token() == IDENTIFIER) {
                                if ("FIRST".equals(lexer.stringValueUppercase())) {
                                    lexer.nextToken();
                                    stmt.addAlterSpecification(new DDLAlterTableStatement.AddColumn(id, colDef, null));
                                } else if ("AFTER".equals(lexer.stringValueUppercase())) {
                                    lexer.nextToken();
                                    id2 = identifier();
                                    stmt.addAlterSpecification(new DDLAlterTableStatement.AddColumn(id, colDef, id2));
                                } else {
                                    stmt.addAlterSpecification(new DDLAlterTableStatement.AddColumn(id, colDef));
                                }
                            } else {
                                stmt.addAlterSpecification(new DDLAlterTableStatement.AddColumn(id, colDef));
                            }
                        }
                        break add_switch;
                    case KW_INDEX:
                    case KW_KEY:
                        // | ADD {INDEX|KEY} [index_name] [index_type]
                        // (index_col_name,...) [index_option] ...
                        id = null;
                        if (lexer.nextToken() == IDENTIFIER) {
                            id = identifier();
                        }
                        indexDef = indexDefinition();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.AddIndex(id, indexDef));
                        break add_switch;
                    case KW_PRIMARY:
                        // | ADD PRIMARY KEY [index_type] (index_col_name,...)
                        // [index_option] ...
                        lexer.nextToken();
                        match(KW_KEY);
                        indexDef = indexDefinition();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.AddPrimaryKey(indexDef));
                        break add_switch;
                    case KW_UNIQUE:
                        // (index_col_name,...) [index_option] ...
                        switch(lexer.nextToken()) {
                            case KW_INDEX:
                            case KW_KEY:
                                lexer.nextToken();
                        }
                        id = null;
                        if (lexer.token() == IDENTIFIER) {
                            id = identifier();
                        }
                        indexDef = indexDefinition();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.AddUniqueKey(id, indexDef));
                        break add_switch;
                    case KW_FULLTEXT:
                        // (index_col_name,...) [index_option] ...
                        switch(lexer.nextToken()) {
                            case KW_INDEX:
                            case KW_KEY:
                                lexer.nextToken();
                        }
                        id = null;
                        if (lexer.token() == IDENTIFIER) {
                            id = identifier();
                        }
                        indexDef = indexDefinition();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.AddFullTextIndex(id, indexDef));
                        break add_switch;
                    case KW_SPATIAL:
                        // (index_col_name,...) [index_option] ...
                        switch(lexer.nextToken()) {
                            case KW_INDEX:
                            case KW_KEY:
                                lexer.nextToken();
                        }
                        id = null;
                        if (lexer.token() == IDENTIFIER) {
                            id = identifier();
                        }
                        indexDef = indexDefinition();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.AddSpatialIndex(id, indexDef));
                        break add_switch;
                    default:
                        throw new SQLSyntaxErrorException("ALTER TABLE error for ADD");
                }
                break main_switch;
            case IDENTIFIER:
                SpecialIdentifier si = specialIdentifiers.get(lexer.stringValueUppercase());
                if (si != null) {
                    switch(si) {
                        case IMPORT:
                            // | IMPORT TABLESPACE
                            lexer.nextToken();
                            matchIdentifier("TABLESPACE");
                            stmt.setImportTableSpace(true);
                            break main_switch;
                        case DISCARD:
                            // | DISCARD TABLESPACE
                            lexer.nextToken();
                            matchIdentifier("TABLESPACE");
                            stmt.setDiscardTableSpace(true);
                            break main_switch;
                        case ENABLE:
                            // | ENABLE KEYS
                            lexer.nextToken();
                            match(KW_KEYS);
                            stmt.setEnableKeys(true);
                            break main_switch;
                        case DISABLE:
                            // | DISABLE KEYS
                            lexer.nextToken();
                            match(KW_KEYS);
                            stmt.setDisableKeys(true);
                            break main_switch;
                        case MODIFY:
                            // AFTER col_name]
                            if (lexer.nextToken() == KW_COLUMN) {
                                lexer.nextToken();
                            }
                            id = identifier();
                            colDef = columnDefinition();
                            if (lexer.token() == IDENTIFIER) {
                                if ("FIRST".equals(lexer.stringValueUppercase())) {
                                    lexer.nextToken();
                                    stmt.addAlterSpecification(new DDLAlterTableStatement.ModifyColumn(id, colDef, null));
                                } else if ("AFTER".equals(lexer.stringValueUppercase())) {
                                    lexer.nextToken();
                                    id2 = identifier();
                                    stmt.addAlterSpecification(new DDLAlterTableStatement.ModifyColumn(id, colDef, id2));
                                } else {
                                    stmt.addAlterSpecification(new DDLAlterTableStatement.ModifyColumn(id, colDef));
                                }
                            } else {
                                stmt.addAlterSpecification(new DDLAlterTableStatement.ModifyColumn(id, colDef));
                            }
                            break main_switch;
                    }
                }
            default:
                throw new SQLSyntaxErrorException("unknown ALTER specification");
        }
    }
    return stmt;
}
Also used : SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) DDLAlterTableStatement(com.alibaba.cobar.parser.ast.stmt.ddl.DDLAlterTableStatement) TableOptions(com.alibaba.cobar.parser.ast.fragment.ddl.TableOptions) Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) IndexDefinition(com.alibaba.cobar.parser.ast.fragment.ddl.index.IndexDefinition) Pair(com.alibaba.cobar.parser.util.Pair) ColumnDefinition(com.alibaba.cobar.parser.ast.fragment.ddl.ColumnDefinition) Expression(com.alibaba.cobar.parser.ast.expression.Expression)

Aggregations

Expression (com.alibaba.cobar.parser.ast.expression.Expression)3 ColumnDefinition (com.alibaba.cobar.parser.ast.fragment.ddl.ColumnDefinition)3 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)3 Identifier (com.alibaba.cobar.parser.ast.expression.primary.Identifier)2 IndexDefinition (com.alibaba.cobar.parser.ast.fragment.ddl.index.IndexDefinition)2 Literal (com.alibaba.cobar.parser.ast.expression.primary.literal.Literal)1 LiteralString (com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString)1 TableOptions (com.alibaba.cobar.parser.ast.fragment.ddl.TableOptions)1 DataType (com.alibaba.cobar.parser.ast.fragment.ddl.datatype.DataType)1 DDLAlterTableStatement (com.alibaba.cobar.parser.ast.stmt.ddl.DDLAlterTableStatement)1 Pair (com.alibaba.cobar.parser.util.Pair)1