Search in sources :

Example 1 with Literal

use of com.alibaba.cobar.parser.ast.expression.primary.literal.Literal 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)

Aggregations

Expression (com.alibaba.cobar.parser.ast.expression.Expression)1 Literal (com.alibaba.cobar.parser.ast.expression.primary.literal.Literal)1 LiteralString (com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString)1 ColumnDefinition (com.alibaba.cobar.parser.ast.fragment.ddl.ColumnDefinition)1 DataType (com.alibaba.cobar.parser.ast.fragment.ddl.datatype.DataType)1 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)1