Search in sources :

Example 1 with ParserException

use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.

the class MySqlStatementParser method parseAlterTable.

protected SQLStatement parseAlterTable(boolean ignore) {
    lexer.nextToken();
    SQLAlterTableStatement stmt = new SQLAlterTableStatement(getDbType());
    stmt.setIgnore(ignore);
    stmt.setName(this.exprParser.name());
    for (; ; ) {
        if (lexer.token() == Token.DROP) {
            parseAlterDrop(stmt);
        } else if (lexer.token() == Token.TRUNCATE) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableTruncatePartition item = new SQLAlterTableTruncatePartition();
            if (lexer.token() == Token.ALL) {
                item.getPartitions().add(new SQLIdentifierExpr("ALL"));
                lexer.nextToken();
            } else {
                this.exprParser.names(item.getPartitions(), item);
            }
            stmt.addItem(item);
        } else if (identifierEquals("ADD")) {
            lexer.nextToken();
            if (lexer.token() == Token.COLUMN) {
                lexer.nextToken();
                parseAlterTableAddColumn(stmt);
            } else if (lexer.token() == Token.INDEX) {
                SQLAlterTableAddIndex item = parseAlterTableAddIndex();
                item.setParent(stmt);
                stmt.addItem(item);
            } else if (lexer.token() == Token.UNIQUE) {
                SQLAlterTableAddIndex item = parseAlterTableAddIndex();
                item.setParent(stmt);
                stmt.addItem(item);
            } else if (lexer.token() == Token.PRIMARY) {
                SQLPrimaryKey primaryKey = this.exprParser.parsePrimaryKey();
                SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(primaryKey);
                stmt.addItem(item);
            } else if (lexer.token() == Token.KEY) {
                // throw new ParserException("TODO " + lexer.token() +
                // " " + lexer.stringVal());
                SQLAlterTableAddIndex item = parseAlterTableAddIndex();
                item.setParent(stmt);
                stmt.addItem(item);
            } else if (lexer.token() == Token.CONSTRAINT) {
                lexer.nextToken();
                SQLName constraintName = this.exprParser.name();
                if (lexer.token() == Token.PRIMARY) {
                    SQLPrimaryKey primaryKey = ((MySqlExprParser) this.exprParser).parsePrimaryKey();
                    primaryKey.setName(constraintName);
                    SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(primaryKey);
                    item.setParent(stmt);
                    stmt.addItem(item);
                } else if (lexer.token() == Token.FOREIGN) {
                    MysqlForeignKey fk = this.getExprParser().parseForeignKey();
                    fk.setName(constraintName);
                    fk.setHasConstraint(true);
                    SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(fk);
                    stmt.addItem(item);
                } else if (lexer.token() == Token.UNIQUE) {
                    SQLUnique unique = this.exprParser.parseUnique();
                    SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(unique);
                    stmt.addItem(item);
                } else {
                    throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
                }
            } else if (lexer.token() == Token.PARTITION) {
                lexer.nextToken();
                SQLAlterTableAddPartition item = new SQLAlterTableAddPartition();
                if (identifierEquals("PARTITIONS")) {
                    lexer.nextToken();
                    item.setPartitionCount(this.exprParser.integerExpr());
                }
                if (lexer.token() == Token.LPAREN) {
                    lexer.nextToken();
                    SQLPartition partition = this.getExprParser().parsePartition();
                    accept(Token.RPAREN);
                    item.addPartition(partition);
                }
                stmt.addItem(item);
            } else if (identifierEquals(FULLTEXT)) {
                throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
            } else if (identifierEquals(SPATIAL)) {
                throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
            } else {
                parseAlterTableAddColumn(stmt);
            }
        } else if (lexer.token() == Token.ALTER) {
            lexer.nextToken();
            if (lexer.token() == Token.COLUMN) {
                lexer.nextToken();
            }
            MySqlAlterTableAlterColumn alterColumn = new MySqlAlterTableAlterColumn();
            alterColumn.setColumn(this.exprParser.name());
            if (lexer.token() == Token.SET) {
                lexer.nextToken();
                accept(Token.DEFAULT);
                alterColumn.setDefaultExpr(this.exprParser.expr());
            } else {
                accept(Token.DROP);
                accept(Token.DEFAULT);
                alterColumn.setDropDefault(true);
            }
            stmt.addItem(alterColumn);
        } else if (identifierEquals("CHANGE")) {
            lexer.nextToken();
            if (lexer.token() == Token.COLUMN) {
                lexer.nextToken();
            }
            MySqlAlterTableChangeColumn item = new MySqlAlterTableChangeColumn();
            item.setColumnName(this.exprParser.name());
            item.setNewColumnDefinition(this.exprParser.parseColumn());
            if (identifierEquals("AFTER")) {
                lexer.nextToken();
                item.setAfterColumn(this.exprParser.name());
            } else if (identifierEquals("FIRST")) {
                lexer.nextToken();
                if (lexer.token() == Token.IDENTIFIER) {
                    item.setFirstColumn(this.exprParser.name());
                } else {
                    item.setFirst(true);
                }
            }
            stmt.addItem(item);
        } else if (identifierEquals("MODIFY")) {
            lexer.nextToken();
            if (lexer.token() == Token.COLUMN) {
                lexer.nextToken();
            }
            boolean paren = false;
            if (lexer.token() == Token.LPAREN) {
                paren = true;
                lexer.nextToken();
            }
            for (; ; ) {
                MySqlAlterTableModifyColumn item = new MySqlAlterTableModifyColumn();
                item.setNewColumnDefinition(this.exprParser.parseColumn());
                if (identifierEquals("AFTER")) {
                    lexer.nextToken();
                    item.setAfterColumn(this.exprParser.name());
                } else if (identifierEquals("FIRST")) {
                    lexer.nextToken();
                    if (lexer.token() == Token.IDENTIFIER) {
                        item.setFirstColumn(this.exprParser.name());
                    } else {
                        item.setFirst(true);
                    }
                }
                stmt.addItem(item);
                if (paren && lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                }
                break;
            }
            if (paren) {
                accept(Token.RPAREN);
            }
        } else if (lexer.token() == Token.DISABLE) {
            lexer.nextToken();
            if (lexer.token() == Token.CONSTRAINT) {
                lexer.nextToken();
                SQLAlterTableDisableConstraint item = new SQLAlterTableDisableConstraint();
                item.setConstraintName(this.exprParser.name());
                stmt.addItem(item);
            } else {
                acceptIdentifier("KEYS");
                SQLAlterTableDisableKeys item = new SQLAlterTableDisableKeys();
                stmt.addItem(item);
            }
        } else if (lexer.token() == Token.ENABLE) {
            lexer.nextToken();
            if (lexer.token() == Token.CONSTRAINT) {
                lexer.nextToken();
                SQLAlterTableEnableConstraint item = new SQLAlterTableEnableConstraint();
                item.setConstraintName(this.exprParser.name());
                stmt.addItem(item);
            } else {
                acceptIdentifier("KEYS");
                SQLAlterTableEnableKeys item = new SQLAlterTableEnableKeys();
                stmt.addItem(item);
            }
        } else if (identifierEquals("RENAME")) {
            lexer.nextToken();
            if (lexer.token() == Token.TO || lexer.token() == Token.AS) {
                lexer.nextToken();
            }
            MySqlRenameTableStatement renameStmt = new MySqlRenameTableStatement();
            MySqlRenameTableStatement.Item item = new MySqlRenameTableStatement.Item();
            item.setName(stmt.getTableSource().getExpr());
            item.setTo(this.exprParser.name());
            renameStmt.addItem(item);
            return renameStmt;
        } else if (lexer.token() == Token.ORDER) {
            throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
        } else if (identifierEquals("CONVERT")) {
            lexer.nextToken();
            accept(Token.TO);
            acceptIdentifier("CHARACTER");
            accept(Token.SET);
            SQLAlterTableConvertCharSet item = new SQLAlterTableConvertCharSet();
            SQLExpr charset = this.exprParser.primary();
            item.setCharset(charset);
            if (identifierEquals("COLLATE")) {
                lexer.nextToken();
                SQLExpr collate = this.exprParser.primary();
                item.setCollate(collate);
            }
            stmt.addItem(item);
        } else if (lexer.token() == Token.DEFAULT) {
            throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
        } else if (identifierEquals("DISCARD")) {
            lexer.nextToken();
            if (lexer.token() == Token.PARTITION) {
                lexer.nextToken();
                SQLAlterTableDiscardPartition item = new SQLAlterTableDiscardPartition();
                if (lexer.token() == Token.ALL) {
                    lexer.nextToken();
                    item.getPartitions().add(new SQLIdentifierExpr("ALL"));
                } else {
                    this.exprParser.names(item.getPartitions(), item);
                }
                stmt.addItem(item);
            } else {
                accept(Token.TABLESPACE);
                MySqlAlterTableDiscardTablespace item = new MySqlAlterTableDiscardTablespace();
                stmt.addItem(item);
            }
        } else if (lexer.token() == Token.CHECK) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableCheckPartition item = new SQLAlterTableCheckPartition();
            if (lexer.token() == Token.ALL) {
                lexer.nextToken();
                item.getPartitions().add(new SQLIdentifierExpr("ALL"));
            } else {
                this.exprParser.names(item.getPartitions(), item);
            }
            stmt.addItem(item);
        } else if (identifierEquals("IMPORT")) {
            lexer.nextToken();
            if (lexer.token() == Token.PARTITION) {
                lexer.nextToken();
                SQLAlterTableImportPartition item = new SQLAlterTableImportPartition();
                if (lexer.token() == Token.ALL) {
                    lexer.nextToken();
                    item.getPartitions().add(new SQLIdentifierExpr("ALL"));
                } else {
                    this.exprParser.names(item.getPartitions(), item);
                }
                stmt.addItem(item);
            } else {
                accept(Token.TABLESPACE);
                MySqlAlterTableImportTablespace item = new MySqlAlterTableImportTablespace();
                stmt.addItem(item);
            }
        } else if (lexer.token() == Token.ANALYZE) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableAnalyzePartition item = new SQLAlterTableAnalyzePartition();
            if (lexer.token() == Token.ALL) {
                lexer.nextToken();
                item.getPartitions().add(new SQLIdentifierExpr("ALL"));
            } else {
                this.exprParser.names(item.getPartitions(), item);
            }
            stmt.addItem(item);
        } else if (identifierEquals("FORCE")) {
            throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
        } else if (identifierEquals("COALESCE")) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableCoalescePartition item = new SQLAlterTableCoalescePartition();
            SQLIntegerExpr countExpr = this.exprParser.integerExpr();
            item.setCount(countExpr);
            stmt.addItem(item);
        } else if (identifierEquals("REORGANIZE")) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableReOrganizePartition item = new SQLAlterTableReOrganizePartition();
            this.exprParser.names(item.getNames(), item);
            accept(Token.INTO);
            accept(Token.LPAREN);
            for (; ; ) {
                SQLPartition partition = this.getExprParser().parsePartition();
                item.addPartition(partition);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                } else {
                    break;
                }
            }
            accept(Token.RPAREN);
            stmt.addItem(item);
        } else if (identifierEquals("EXCHANGE")) {
            throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
        } else if (lexer.token() == Token.OPTIMIZE) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableOptimizePartition item = new SQLAlterTableOptimizePartition();
            if (lexer.token() == Token.ALL) {
                lexer.nextToken();
                item.getPartitions().add(new SQLIdentifierExpr("ALL"));
            } else {
                this.exprParser.names(item.getPartitions(), item);
            }
            stmt.addItem(item);
        } else if (identifierEquals("REBUILD")) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableRebuildPartition item = new SQLAlterTableRebuildPartition();
            if (lexer.token() == Token.ALL) {
                lexer.nextToken();
                item.getPartitions().add(new SQLIdentifierExpr("ALL"));
            } else {
                this.exprParser.names(item.getPartitions(), item);
            }
            stmt.addItem(item);
        } else if (identifierEquals("REPAIR")) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableRepairPartition item = new SQLAlterTableRepairPartition();
            if (lexer.token() == Token.ALL) {
                lexer.nextToken();
                item.getPartitions().add(new SQLIdentifierExpr("ALL"));
            } else {
                this.exprParser.names(item.getPartitions(), item);
            }
            stmt.addItem(item);
        } else if (identifierEquals("REMOVE")) {
            lexer.nextToken();
            acceptIdentifier("PARTITIONING");
            stmt.setRemovePatiting(true);
        } else if (identifierEquals("UPGRADE")) {
            lexer.nextToken();
            acceptIdentifier("PARTITIONING");
            stmt.setUpgradePatiting(true);
        } else if (identifierEquals("ALGORITHM")) {
            lexer.nextToken();
            accept(Token.EQ);
            stmt.addItem(new MySqlAlterTableOption("ALGORITHM", lexer.stringVal()));
            lexer.nextToken();
        } else if (identifierEquals(ENGINE)) {
            lexer.nextToken();
            accept(Token.EQ);
            stmt.addItem(new MySqlAlterTableOption(ENGINE, lexer.stringVal()));
            lexer.nextToken();
        } else if (identifierEquals(AUTO_INCREMENT)) {
            lexer.nextToken();
            accept(Token.EQ);
            stmt.addItem(new MySqlAlterTableOption(AUTO_INCREMENT, lexer.integerValue()));
            lexer.nextToken();
        } else if (identifierEquals(COLLATE2)) {
            lexer.nextToken();
            accept(Token.EQ);
            stmt.addItem(new MySqlAlterTableOption(COLLATE2, lexer.stringVal()));
            lexer.nextToken();
        } else if (identifierEquals("PACK_KEYS")) {
            lexer.nextToken();
            accept(Token.EQ);
            if (identifierEquals("PACK")) {
                lexer.nextToken();
                accept(Token.ALL);
                stmt.addItem(new MySqlAlterTableOption("PACK_KEYS", "PACK ALL"));
            } else {
                stmt.addItem(new MySqlAlterTableOption("PACK_KEYS", lexer.stringVal()));
                lexer.nextToken();
            }
        } else if (identifierEquals(CHARACTER)) {
            lexer.nextToken();
            accept(Token.SET);
            accept(Token.EQ);
            MySqlAlterTableCharacter item = new MySqlAlterTableCharacter();
            item.setCharacterSet(this.exprParser.primary());
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                acceptIdentifier(COLLATE2);
                accept(Token.EQ);
                item.setCollate(this.exprParser.primary());
            }
            stmt.addItem(item);
        } else if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                accept(Token.EQ);
            }
            stmt.addItem(new MySqlAlterTableOption("COMMENT", '\'' + lexer.stringVal() + '\''));
            lexer.nextToken();
        } else if (lexer.token() == Token.UNION) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            accept(Token.LPAREN);
            SQLTableSource tableSrc = this.createSQLSelectParser().parseTableSource();
            stmt.getTableOptions().put("UNION", tableSrc);
            accept(Token.RPAREN);
        } else if (identifierEquals("ROW_FORMAT")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            if (lexer.token() == Token.DEFAULT || lexer.token() == Token.IDENTIFIER) {
                SQLIdentifierExpr rowFormat = new SQLIdentifierExpr(lexer.stringVal());
                lexer.nextToken();
                stmt.getTableOptions().put("ROW_FORMAT", rowFormat);
            } else {
                throw new ParserException("illegal syntax.");
            }
        } else {
            break;
        }
        if (lexer.token() == Token.COMMA) {
            lexer.nextToken();
            continue;
        } else {
            break;
        }
    }
    return stmt;
}
Also used : MySqlAlterTableModifyColumn(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn) MySqlAlterTableCharacter(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableCharacter) MySqlAlterTableAlterColumn(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableAlterColumn) SQLDeclareItem(com.alibaba.druid.sql.ast.SQLDeclareItem) MySqlAlterTableDiscardTablespace(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableDiscardTablespace) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) MySqlRenameTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLName(com.alibaba.druid.sql.ast.SQLName) MySqlAlterTableOption(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableOption) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) MysqlForeignKey(com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey) SQLPartition(com.alibaba.druid.sql.ast.SQLPartition) MySqlAlterTableImportTablespace(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableImportTablespace) MySqlAlterTableChangeColumn(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn)

Example 2 with ParserException

use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.

the class MySqlStatementParser method parseDeclareCondition.

/**
     * zhujun [455910092@qq.com]
     * 2016-04-17
     * 定义条件
     * @return
     */
public MySqlDeclareConditionStatement parseDeclareCondition() {
    /*
    	DECLARE condition_name CONDITION FOR condition_value

    	condition_value:
    	    SQLSTATE [VALUE] sqlstate_value
    	  | mysql_error_code
    	*/
    MySqlDeclareConditionStatement stmt = new MySqlDeclareConditionStatement();
    accept(Token.DECLARE);
    stmt.setConditionName(exprParser.name().toString());
    accept(Token.CONDITION);
    accept(Token.FOR);
    String tokenName = lexer.stringVal();
    ConditionValue condition = new ConditionValue();
    if (tokenName.equalsIgnoreCase("SQLSTATE")) {
        //for SQLSTATE (SQLSTATE '10001') 
        condition.setType(ConditionType.SQLSTATE);
        lexer.nextToken();
        condition.setValue(exprParser.name().toString());
    } else if (lexer.token() == Token.LITERAL_INT) {
        condition.setType(ConditionType.MYSQL_ERROR_CODE);
        condition.setValue(lexer.integerValue().toString());
        lexer.nextToken();
    } else {
        throw new ParserException("declare condition grammer error.");
    }
    stmt.setConditionValue(condition);
    accept(Token.SEMI);
    return stmt;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) ConditionValue(com.alibaba.druid.sql.dialect.mysql.ast.clause.ConditionValue) MySqlDeclareConditionStatement(com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareConditionStatement)

Example 3 with ParserException

use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.

the class MySqlStatementParser method parseCreate.

public SQLStatement parseCreate() {
    char markChar = lexer.current();
    int markBp = lexer.bp();
    accept(Token.CREATE);
    boolean replace = false;
    if (lexer.token() == Token.OR) {
        lexer.nextToken();
        accept(Token.REPLACE);
        replace = true;
    }
    List<SQLCommentHint> hints = this.exprParser.parseHints();
    if (lexer.token() == Token.TABLE || identifierEquals(TEMPORARY)) {
        if (replace) {
            lexer.reset(markBp, markChar, Token.CREATE);
        }
        MySqlCreateTableParser parser = new MySqlCreateTableParser(this.exprParser);
        MySqlCreateTableStatement stmt = parser.parseCrateTable(false);
        stmt.setHints(hints);
        return stmt;
    }
    if (lexer.token() == Token.DATABASE) {
        if (replace) {
            lexer.reset(markBp, markChar, Token.CREATE);
        }
        return parseCreateDatabase();
    }
    if (lexer.token() == Token.UNIQUE || lexer.token() == Token.INDEX || identifierEquals(FULLTEXT) || identifierEquals(SPATIAL)) {
        if (replace) {
            lexer.reset(markBp, markChar, Token.CREATE);
        }
        return parseCreateIndex(false);
    }
    if (lexer.token() == Token.USER) {
        if (replace) {
            lexer.reset(markBp, markChar, Token.CREATE);
        }
        return parseCreateUser();
    }
    if (lexer.token() == Token.VIEW || identifierEquals("ALGORITHM")) {
        if (replace) {
            lexer.reset(markBp, markChar, Token.CREATE);
        }
        return parseCreateView();
    }
    if (lexer.token() == Token.TRIGGER) {
        if (replace) {
            lexer.reset(markBp, markChar, Token.CREATE);
        }
        return parseCreateTrigger();
    }
    // parse create procedure
    if (lexer.token() == Token.PROCEDURE || identifierEquals("DEFINER")) {
        if (replace) {
            lexer.reset(markBp, markChar, Token.CREATE);
        }
        return parseCreateProcedure();
    }
    throw new ParserException("TODO " + lexer.info());
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint)

Example 4 with ParserException

use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.

the class MySqlCreateTableParser method parseCrateTable.

public MySqlCreateTableStatement parseCrateTable(boolean acceptCreate) {
    if (acceptCreate) {
        accept(Token.CREATE);
    }
    MySqlCreateTableStatement stmt = new MySqlCreateTableStatement();
    if (identifierEquals("TEMPORARY")) {
        lexer.nextToken();
        stmt.setType(SQLCreateTableStatement.Type.GLOBAL_TEMPORARY);
    }
    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 (lexer.token() == Token.LIKE) {
        lexer.nextToken();
        SQLName name = this.exprParser.name();
        stmt.setLike(name);
    }
    if (lexer.token() == (Token.LPAREN)) {
        lexer.nextToken();
        if (lexer.token() == Token.LIKE) {
            lexer.nextToken();
            SQLName name = this.exprParser.name();
            stmt.setLike(name);
        } else {
            for (; ; ) {
                if (//
                lexer.token() == Token.IDENTIFIER || lexer.token() == Token.LITERAL_CHARS) {
                    SQLColumnDefinition column = this.exprParser.parseColumn();
                    stmt.getTableElementList().add(column);
                } else if (//
                lexer.token() == Token.CONSTRAINT || //
                lexer.token() == Token.PRIMARY || lexer.token() == Token.UNIQUE) {
                    SQLTableConstraint constraint = this.parseConstraint();
                    stmt.getTableElementList().add(constraint);
                } else if (lexer.token() == (Token.INDEX)) {
                    lexer.nextToken();
                    MySqlTableIndex idx = new MySqlTableIndex();
                    if (lexer.token() == Token.IDENTIFIER) {
                        if (!"USING".equalsIgnoreCase(lexer.stringVal())) {
                            idx.setName(this.exprParser.name());
                        }
                    }
                    if (identifierEquals("USING")) {
                        lexer.nextToken();
                        idx.setIndexType(lexer.stringVal());
                        lexer.nextToken();
                    }
                    accept(Token.LPAREN);
                    for (; ; ) {
                        idx.addColumn(this.exprParser.expr());
                        if (!(lexer.token() == (Token.COMMA))) {
                            break;
                        } else {
                            lexer.nextToken();
                        }
                    }
                    accept(Token.RPAREN);
                    if (identifierEquals("USING")) {
                        lexer.nextToken();
                        idx.setIndexType(lexer.stringVal());
                        lexer.nextToken();
                    }
                    stmt.getTableElementList().add(idx);
                } else if (lexer.token() == (Token.KEY)) {
                    stmt.getTableElementList().add(parseConstraint());
                } else if (lexer.token() == (Token.PRIMARY)) {
                    SQLTableConstraint pk = parseConstraint();
                    pk.setParent(stmt);
                    stmt.getTableElementList().add(pk);
                } else if (lexer.token() == (Token.FOREIGN)) {
                    SQLForeignKeyConstraint fk = this.getExprParser().parseForeignKey();
                    fk.setParent(stmt);
                    stmt.getTableElementList().add(fk);
                } else if (lexer.token() == Token.CHECK) {
                    SQLCheck check = this.exprParser.parseCheck();
                    stmt.getTableElementList().add(check);
                } else {
                    SQLColumnDefinition column = this.exprParser.parseColumn();
                    stmt.getTableElementList().add(column);
                }
                if (!(lexer.token() == (Token.COMMA))) {
                    break;
                } else {
                    lexer.nextToken();
                }
            }
        }
        accept(Token.RPAREN);
    }
    for (; ; ) {
        if (identifierEquals("ENGINE")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("ENGINE", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("AUTO_INCREMENT")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("AUTO_INCREMENT", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("AVG_ROW_LENGTH")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("AVG_ROW_LENGTH", this.exprParser.expr());
            continue;
        }
        if (lexer.token() == Token.DEFAULT) {
            lexer.nextToken();
            parseTableOptionCharsetOrCollate(stmt);
            continue;
        }
        if (parseTableOptionCharsetOrCollate(stmt)) {
            continue;
        }
        if (identifierEquals("CHECKSUM")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("CHECKSUM", this.exprParser.expr());
            continue;
        }
        if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("COMMENT", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("CONNECTION")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("CONNECTION", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("DATA")) {
            lexer.nextToken();
            acceptIdentifier("DIRECTORY");
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("DATA DIRECTORY", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("DELAY_KEY_WRITE")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("DELAY_KEY_WRITE", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("INDEX")) {
            lexer.nextToken();
            acceptIdentifier("DIRECTORY");
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("INDEX DIRECTORY", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("INSERT_METHOD")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("INSERT_METHOD", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("KEY_BLOCK_SIZE")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("KEY_BLOCK_SIZE", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("MAX_ROWS")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("MAX_ROWS", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("MIN_ROWS")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("MIN_ROWS", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("PACK_KEYS")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("PACK_KEYS", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("PASSWORD")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("PASSWORD", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("ROW_FORMAT")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("ROW_FORMAT", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("STATS_AUTO_RECALC")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("STATS_AUTO_RECALC", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("STATS_PERSISTENT")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("STATS_PERSISTENT", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("STATS_SAMPLE_PAGES")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("STATS_SAMPLE_PAGES", this.exprParser.expr());
            continue;
        }
        if (lexer.token() == Token.UNION) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            accept(Token.LPAREN);
            SQLTableSource tableSrc = this.createSQLSelectParser().parseTableSource();
            stmt.getTableOptions().put("UNION", tableSrc);
            accept(Token.RPAREN);
            continue;
        }
        if (lexer.token() == Token.TABLESPACE) {
            lexer.nextToken();
            TableSpaceOption option = new TableSpaceOption();
            option.setName(this.exprParser.name());
            if (identifierEquals("STORAGE")) {
                lexer.nextToken();
                option.setStorage(this.exprParser.name());
            }
            stmt.getTableOptions().put("TABLESPACE", option);
            continue;
        }
        if (identifierEquals("TABLEGROUP")) {
            lexer.nextToken();
            SQLName tableGroup = this.exprParser.name();
            stmt.setTableGroup(tableGroup);
            continue;
        }
        if (identifierEquals("TYPE")) {
            lexer.nextToken();
            accept(Token.EQ);
            stmt.getTableOptions().put("TYPE", this.exprParser.expr());
            lexer.nextToken();
            continue;
        }
        if (lexer.token() == Token.PARTITION) {
            lexer.nextToken();
            accept(Token.BY);
            SQLPartitionBy partitionClause;
            boolean linera = false;
            if (identifierEquals("LINEAR")) {
                lexer.nextToken();
                linera = true;
            }
            if (lexer.token() == Token.KEY) {
                MySqlPartitionByKey clause = new MySqlPartitionByKey();
                lexer.nextToken();
                if (linera) {
                    clause.setLinear(true);
                }
                accept(Token.LPAREN);
                if (lexer.token() != Token.RPAREN) {
                    for (; ; ) {
                        clause.addColumn(this.exprParser.name());
                        if (lexer.token() == Token.COMMA) {
                            lexer.nextToken();
                            continue;
                        }
                        break;
                    }
                }
                accept(Token.RPAREN);
                partitionClause = clause;
                partitionClauseRest(clause);
            } else if (identifierEquals("HASH")) {
                lexer.nextToken();
                SQLPartitionByHash clause = new SQLPartitionByHash();
                if (linera) {
                    clause.setLinear(true);
                }
                if (lexer.token() == Token.KEY) {
                    lexer.nextToken();
                    clause.setKey(true);
                }
                accept(Token.LPAREN);
                clause.setExpr(this.exprParser.expr());
                accept(Token.RPAREN);
                partitionClause = clause;
                partitionClauseRest(clause);
            } else if (identifierEquals("RANGE")) {
                SQLPartitionByRange clause = partitionByRange();
                partitionClause = clause;
                partitionClauseRest(clause);
            } else if (identifierEquals("LIST")) {
                lexer.nextToken();
                SQLPartitionByList clause = new SQLPartitionByList();
                if (lexer.token() == Token.LPAREN) {
                    lexer.nextToken();
                    clause.setExpr(this.exprParser.expr());
                    accept(Token.RPAREN);
                } else {
                    acceptIdentifier("COLUMNS");
                    accept(Token.LPAREN);
                    for (; ; ) {
                        clause.addColumn(this.exprParser.name());
                        if (lexer.token() == Token.COMMA) {
                            lexer.nextToken();
                            continue;
                        }
                        break;
                    }
                    accept(Token.RPAREN);
                }
                partitionClause = clause;
                partitionClauseRest(clause);
            } else {
                throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
            }
            if (lexer.token() == Token.LPAREN) {
                lexer.nextToken();
                for (; ; ) {
                    SQLPartition partitionDef = this.getExprParser().parsePartition();
                    partitionClause.addPartition(partitionDef);
                    if (lexer.token() == Token.COMMA) {
                        lexer.nextToken();
                        continue;
                    } else {
                        break;
                    }
                }
                accept(Token.RPAREN);
            }
            stmt.setPartitioning(partitionClause);
            continue;
        }
        break;
    }
    if (lexer.token() == (Token.ON)) {
        throw new ParserException("TODO");
    }
    if (lexer.token() == (Token.AS)) {
        lexer.nextToken();
    }
    if (lexer.token() == (Token.SELECT)) {
        SQLSelect query = new MySqlSelectParser(this.exprParser).select();
        stmt.setSelect(query);
    }
    while (lexer.token() == (Token.HINT)) {
        this.exprParser.parseHints(stmt.getOptionHints());
    }
    return stmt;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) MySqlTableIndex(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlTableIndex) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLForeignKeyConstraint(com.alibaba.druid.sql.ast.statement.SQLForeignKeyConstraint) MySqlPartitionByKey(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey) SQLPartitionByRange(com.alibaba.druid.sql.ast.SQLPartitionByRange) SQLPartitionByHash(com.alibaba.druid.sql.ast.SQLPartitionByHash) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource) SQLCheck(com.alibaba.druid.sql.ast.statement.SQLCheck) SQLPartition(com.alibaba.druid.sql.ast.SQLPartition) SQLTableConstraint(com.alibaba.druid.sql.ast.statement.SQLTableConstraint) SQLPartitionBy(com.alibaba.druid.sql.ast.SQLPartitionBy) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) TableSpaceOption(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement.TableSpaceOption) SQLPartitionByList(com.alibaba.druid.sql.ast.SQLPartitionByList)

Example 5 with ParserException

use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.

the class MySqlExprParser method parseInterval.

protected SQLExpr parseInterval() {
    accept(Token.INTERVAL);
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr("INTERVAL");
        if (lexer.token() != Token.RPAREN) {
            exprList(methodInvokeExpr.getParameters(), methodInvokeExpr);
        }
        accept(Token.RPAREN);
        if (// 
        methodInvokeExpr.getParameters().size() == 1 && lexer.token() == Token.IDENTIFIER) {
            SQLExpr value = methodInvokeExpr.getParameters().get(0);
            String unit = lexer.stringVal();
            lexer.nextToken();
            MySqlIntervalExpr intervalExpr = new MySqlIntervalExpr();
            intervalExpr.setValue(value);
            intervalExpr.setUnit(MySqlIntervalUnit.valueOf(unit.toUpperCase()));
            return intervalExpr;
        } else {
            return primaryRest(methodInvokeExpr);
        }
    } else {
        SQLExpr value = expr();
        if (lexer.token() != Token.IDENTIFIER) {
            throw new ParserException("Syntax error");
        }
        String unit = lexer.stringVal();
        lexer.nextToken();
        MySqlIntervalExpr intervalExpr = new MySqlIntervalExpr();
        intervalExpr.setValue(value);
        intervalExpr.setUnit(MySqlIntervalUnit.valueOf(unit.toUpperCase()));
        return intervalExpr;
    }
}
Also used : MySqlIntervalExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

ParserException (com.alibaba.druid.sql.parser.ParserException)98 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)25 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)18 SQLName (com.alibaba.druid.sql.ast.SQLName)16 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)12 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)10 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)9 Token (com.alibaba.druid.sql.parser.Token)9 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)5 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)5 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)5 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)5 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)4 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)4 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)4 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)4 OracleConstraint (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleConstraint)4 DbType (com.alibaba.druid.DbType)3 SQLDeclareItem (com.alibaba.druid.sql.ast.SQLDeclareItem)3 SQLPartitionByHash (com.alibaba.druid.sql.ast.SQLPartitionByHash)3