Search in sources :

Example 1 with IndexColumnName

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

the class MySQLDDLParser method indexDefinition.

// col_name column_definition
// | [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
// [index_option] ...
// | {INDEX|KEY} [index_name] [index_type] (index_col_name,...)
// [index_option] ...
// | [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type]
// (index_col_name,...) [index_option] ...
// | {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (index_col_name,...)
// [index_option] ...
// | [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,...)
// reference_definition
// | CHECK (expr)
private IndexDefinition indexDefinition() throws SQLSyntaxErrorException {
    IndexDefinition.IndexType indexType = null;
    List<IndexColumnName> columns = new ArrayList<IndexColumnName>(1);
    if (lexer.token() == KW_USING) {
        lexer.nextToken();
        int tp = matchIdentifier("BTREE", "HASH");
        indexType = tp == 0 ? IndexDefinition.IndexType.BTREE : IndexDefinition.IndexType.HASH;
    }
    match(PUNC_LEFT_PAREN);
    for (int i = 0; lexer.token() != PUNC_RIGHT_PAREN; ++i) {
        if (i > 0)
            match(PUNC_COMMA);
        IndexColumnName indexColumnName = indexColumnName();
        columns.add(indexColumnName);
    }
    match(PUNC_RIGHT_PAREN);
    List<IndexOption> options = indexOptions();
    return new IndexDefinition(indexType, columns, options);
}
Also used : IndexDefinition(com.alibaba.cobar.parser.ast.fragment.ddl.index.IndexDefinition) IndexColumnName(com.alibaba.cobar.parser.ast.fragment.ddl.index.IndexColumnName) IndexOption(com.alibaba.cobar.parser.ast.fragment.ddl.index.IndexOption) ArrayList(java.util.ArrayList)

Example 2 with IndexColumnName

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

Aggregations

IndexColumnName (com.alibaba.cobar.parser.ast.fragment.ddl.index.IndexColumnName)2 Expression (com.alibaba.cobar.parser.ast.expression.Expression)1 Identifier (com.alibaba.cobar.parser.ast.expression.primary.Identifier)1 IndexDefinition (com.alibaba.cobar.parser.ast.fragment.ddl.index.IndexDefinition)1 IndexOption (com.alibaba.cobar.parser.ast.fragment.ddl.index.IndexOption)1 ArrayList (java.util.ArrayList)1