Search in sources :

Example 6 with SQLColumnDefinition

use of com.alibaba.druid.sql.ast.statement.SQLColumnDefinition in project druid by alibaba.

the class OdpsCreateTableParser method parseCrateTable.

public SQLCreateTableStatement parseCrateTable(boolean acceptCreate) {
    OdpsCreateTableStatement stmt = new OdpsCreateTableStatement();
    if (acceptCreate) {
        accept(Token.CREATE);
    }
    accept(Token.TABLE);
    if (lexer.token() == Token.IF || identifierEquals("IF")) {
        lexer.nextToken();
        accept(Token.NOT);
        accept(Token.EXISTS);
        stmt.setIfNotExiists(true);
    }
    stmt.setName(this.exprParser.name());
    if (identifierEquals("LIFECYCLE")) {
        lexer.nextToken();
        stmt.setLifecycle(this.exprParser.expr());
    }
    if (lexer.token() == Token.LIKE) {
        lexer.nextToken();
        SQLName name = this.exprParser.name();
        stmt.setLike(name);
    } else if (lexer.token() == Token.AS) {
        lexer.nextToken();
        OdpsSelectParser selectParser = new OdpsSelectParser(this.exprParser);
        SQLSelect select = selectParser.select();
        stmt.setSelect(select);
    } else {
        accept(Token.LPAREN);
        if (lexer.isKeepComments() && lexer.hasComment()) {
            stmt.addBodyBeforeComment(lexer.readAndResetComments());
        }
        for (; ; ) {
            if (lexer.token() != Token.IDENTIFIER) {
                throw new ParserException("expect identifier");
            }
            SQLColumnDefinition column = this.exprParser.parseColumn();
            stmt.getTableElementList().add(column);
            if (lexer.isKeepComments() && lexer.hasComment()) {
                column.addAfterComment(lexer.readAndResetComments());
            }
            if (!(lexer.token() == (Token.COMMA))) {
                break;
            } else {
                lexer.nextToken();
                if (lexer.isKeepComments() && lexer.hasComment()) {
                    column.addAfterComment(lexer.readAndResetComments());
                }
            }
        }
        accept(Token.RPAREN);
    }
    if (lexer.token() == Token.COMMENT) {
        lexer.nextToken();
        stmt.setComment(this.exprParser.primary());
    }
    if (lexer.token() == Token.PARTITIONED) {
        lexer.nextToken();
        accept(Token.BY);
        accept(Token.LPAREN);
        for (; ; ) {
            if (lexer.token() != Token.IDENTIFIER) {
                throw new ParserException("expect identifier");
            }
            SQLColumnDefinition column = this.exprParser.parseColumn();
            stmt.addPartitionColumn(column);
            if (lexer.isKeepComments() && lexer.hasComment()) {
                column.addAfterComment(lexer.readAndResetComments());
            }
            if (!(lexer.token() == (Token.COMMA))) {
                break;
            } else {
                lexer.nextToken();
                if (lexer.isKeepComments() && lexer.hasComment()) {
                    column.addAfterComment(lexer.readAndResetComments());
                }
            }
        }
        accept(Token.RPAREN);
    }
    if (identifierEquals("LIFECYCLE")) {
        lexer.nextToken();
        stmt.setLifecycle(this.exprParser.expr());
    }
    return stmt;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) OdpsCreateTableStatement(com.alibaba.druid.sql.dialect.odps.ast.OdpsCreateTableStatement) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)

Example 7 with SQLColumnDefinition

use of com.alibaba.druid.sql.ast.statement.SQLColumnDefinition in project druid by alibaba.

the class PGSQLStatementParser method parseAlterColumn.

protected SQLAlterTableAlterColumn parseAlterColumn() {
    if (lexer.token() == Token.COLUMN) {
        lexer.nextToken();
    }
    SQLColumnDefinition column = this.exprParser.parseColumn();
    SQLAlterTableAlterColumn alterColumn = new SQLAlterTableAlterColumn();
    alterColumn.setColumn(column);
    if (column.getDataType() == null && column.getConstraints().size() == 0) {
        if (lexer.token() == Token.SET) {
            lexer.nextToken();
            if (lexer.token() == Token.NOT) {
                lexer.nextToken();
                accept(Token.NULL);
                alterColumn.setSetNotNull(true);
            } else {
                accept(Token.DEFAULT);
                SQLExpr defaultValue = this.exprParser.expr();
                alterColumn.setSetDefault(defaultValue);
            }
        } else if (lexer.token() == Token.DROP) {
            lexer.nextToken();
            if (lexer.token() == Token.NOT) {
                lexer.nextToken();
                accept(Token.NULL);
                alterColumn.setDropNotNull(true);
            } else {
                accept(Token.DEFAULT);
                alterColumn.setDropDefault(true);
            }
        }
    }
    return alterColumn;
}
Also used : SQLAlterTableAlterColumn(com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 8 with SQLColumnDefinition

use of com.alibaba.druid.sql.ast.statement.SQLColumnDefinition in project druid by alibaba.

the class OracleSchemaStatVisitor method visit.

@Override
public boolean visit(OracleAlterTableModify x) {
    SQLAlterTableStatement stmt = (SQLAlterTableStatement) x.getParent();
    String table = stmt.getName().toString();
    for (SQLColumnDefinition column : x.getColumns()) {
        String columnName = column.getName().toString();
        addColumn(table, columnName);
    }
    return false;
}
Also used : SQLAlterTableStatement(com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)

Example 9 with SQLColumnDefinition

use of com.alibaba.druid.sql.ast.statement.SQLColumnDefinition in project druid by alibaba.

the class SQLCreateTableParser method parseCrateTable.

public SQLCreateTableStatement parseCrateTable(boolean acceptCreate) {
    if (acceptCreate) {
        accept(Token.CREATE);
    }
    SQLCreateTableStatement createTable = newCreateStatement();
    if (identifierEquals("GLOBAL")) {
        lexer.nextToken();
        if (identifierEquals("TEMPORARY")) {
            lexer.nextToken();
            createTable.setType(SQLCreateTableStatement.Type.GLOBAL_TEMPORARY);
        } else {
            throw new ParserException("syntax error " + lexer.token() + " " + lexer.stringVal());
        }
    } else if (lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase("LOCAL")) {
        lexer.nextToken();
        if (lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase("TEMPORAY")) {
            lexer.nextToken();
            createTable.setType(SQLCreateTableStatement.Type.LOCAL_TEMPORARY);
        } else {
            throw new ParserException("syntax error");
        }
    }
    accept(Token.TABLE);
    createTable.setName(this.exprParser.name());
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        for (; ; ) {
            if (//
            lexer.token() == Token.IDENTIFIER || lexer.token() == Token.LITERAL_ALIAS) {
                SQLColumnDefinition column = this.exprParser.parseColumn();
                createTable.getTableElementList().add(column);
            } else if (//
            lexer.token == Token.PRIMARY || //
            lexer.token == Token.UNIQUE || //
            lexer.token == Token.CHECK || lexer.token == Token.CONSTRAINT) {
                SQLConstraint constraint = this.exprParser.parseConstaint();
                constraint.setParent(createTable);
                createTable.getTableElementList().add((SQLTableElement) constraint);
            } else if (lexer.token() == Token.TABLESPACE) {
                throw new ParserException("TODO " + lexer.token());
            } else {
                SQLColumnDefinition column = this.exprParser.parseColumn();
                createTable.getTableElementList().add(column);
            }
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                if (lexer.token() == Token.RPAREN) {
                    // compatible for sql server
                    break;
                }
                continue;
            }
            break;
        }
        // while
        // (this.tokenList.current().equals(OracleToken.ConstraintToken)) {
        // parseConstaint(table.getConstraints());
        //
        // if (this.tokenList.current().equals(OracleToken.CommaToken))
        // ;
        // lexer.nextToken();
        // }
        accept(Token.RPAREN);
        if (identifierEquals("INHERITS")) {
            lexer.nextToken();
            accept(Token.LPAREN);
            SQLName inherits = this.exprParser.name();
            createTable.setInherits(new SQLExprTableSource(inherits));
            accept(Token.RPAREN);
        }
    }
    return createTable;
}
Also used : SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLConstraint(com.alibaba.druid.sql.ast.statement.SQLConstraint) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLTableElement(com.alibaba.druid.sql.ast.statement.SQLTableElement) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)

Example 10 with SQLColumnDefinition

use of com.alibaba.druid.sql.ast.statement.SQLColumnDefinition in project druid by alibaba.

the class SQLServerExprParser method parseColumnRest.

public SQLColumnDefinition parseColumnRest(SQLColumnDefinition column) {
    if (lexer.token() == Token.IDENTITY) {
        lexer.nextToken();
        SQLColumnDefinition.Identity identity = new SQLColumnDefinition.Identity();
        if (lexer.token() == Token.LPAREN) {
            lexer.nextToken();
            SQLIntegerExpr seed = (SQLIntegerExpr) this.primary();
            accept(Token.COMMA);
            SQLIntegerExpr increment = (SQLIntegerExpr) this.primary();
            accept(Token.RPAREN);
            identity.setSeed((Integer) seed.getNumber());
            identity.setIncrement((Integer) increment.getNumber());
        }
        if (lexer.token() == Token.NOT) {
            lexer.nextToken();
            if (lexer.token() == Token.NULL) {
                lexer.nextToken();
                column.setDefaultExpr(new SQLNullExpr());
            } else {
                accept(Token.FOR);
                identifierEquals("REPLICATION ");
                identity.setNotForReplication(true);
            }
        }
        column.setIdentity(identity);
    }
    return super.parseColumnRest(column);
}
Also used : SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLNullExpr(com.alibaba.druid.sql.ast.expr.SQLNullExpr) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)

Aggregations

SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)14 SQLName (com.alibaba.druid.sql.ast.SQLName)5 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)4 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)3 SQLCharacterDataType (com.alibaba.druid.sql.ast.statement.SQLCharacterDataType)3 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)3 SQLTableElement (com.alibaba.druid.sql.ast.statement.SQLTableElement)3 MySqlCreateTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)3 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)2 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)2 ParserException (com.alibaba.druid.sql.parser.ParserException)2 TableConfig (io.mycat.config.model.TableConfig)2 AbstractPartitionAlgorithm (io.mycat.route.function.AbstractPartitionAlgorithm)2 SlotFunction (io.mycat.route.function.SlotFunction)2 SQLDataTypeImpl (com.alibaba.druid.sql.ast.SQLDataTypeImpl)1 SQLPartition (com.alibaba.druid.sql.ast.SQLPartition)1 SQLPartitionBy (com.alibaba.druid.sql.ast.SQLPartitionBy)1 SQLPartitionByHash (com.alibaba.druid.sql.ast.SQLPartitionByHash)1 SQLPartitionByList (com.alibaba.druid.sql.ast.SQLPartitionByList)1