Search in sources :

Example 6 with MySqlUnique

use of com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique in project druid by alibaba.

the class MySqlExprParser method parseUnique.

public MySqlUnique parseUnique() {
    MySqlUnique unique = new MySqlUnique();
    parseIndex(unique.getIndexDefinition());
    return unique;
/*
        accept(Token.UNIQUE);

        if (lexer.token() == Token.KEY) {
            lexer.nextToken();
        }

        if (lexer.token() == Token.INDEX) {
            lexer.nextToken();
        }

        MySqlUnique unique = new MySqlUnique();

        if (lexer.token() != Token.LPAREN && !lexer.identifierEquals(FnvHash.Constants.USING)) {
            SQLName indexName = name();
            unique.setName(indexName);
        }

        //5.5语法 USING BTREE 放在index 名字后
        if (lexer.identifierEquals(FnvHash.Constants.USING)) {
            lexer.nextToken();
            unique.setIndexType(lexer.stringVal());
            lexer.nextToken();
        }

        parseIndexRest(unique);

        for (;;) {
            if (lexer.token() == Token.COMMENT) {
                lexer.nextToken();
                SQLExpr comment = this.primary();
                unique.setComment(comment);
            } else if (lexer.identifierEquals(FnvHash.Constants.KEY_BLOCK_SIZE)) {
                lexer.nextToken();
                if (lexer.token() == Token.EQ) {
                    lexer.nextToken();
                }
                SQLExpr keyBlockSize = this.primary();
                unique.setKeyBlockSize(keyBlockSize);
            } else if (lexer.identifierEquals(FnvHash.Constants.USING)) {
                lexer.nextToken();
                unique.setIndexType(lexer.stringVal());
                accept(Token.IDENTIFIER);
            } else {
                break;
            }
        }

        return unique;
        */
}
Also used : MySqlUnique(com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique)

Example 7 with MySqlUnique

use of com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique in project druid by alibaba.

the class MySqlCreateTableStatement method apply.

public boolean apply(SQLAlterTableAddIndex item) {
    SQLName name = item.getIndexDefinition().getName();
    if (name != null) {
        long nameHashCode = name.nameHashCode64();
        for (int i = 0; i < tableElementList.size(); i++) {
            SQLTableElement e = tableElementList.get(i);
            if (e instanceof MySqlTableIndex) {
                SQLName name1 = ((MySqlTableIndex) e).getName();
                if (name1 != null && name1.nameHashCode64() == nameHashCode) {
                    return false;
                }
            }
        }
    }
    if (item.isUnique()) {
        MySqlUnique x = new MySqlUnique();
        item.cloneTo(x);
        x.setParent(this);
        this.tableElementList.add(x);
        return true;
    }
    if (item.isKey()) {
        MySqlKey x = new MySqlKey();
        item.cloneTo(x);
        x.setParent(this);
        this.tableElementList.add(x);
        return true;
    }
    MySqlTableIndex x = new MySqlTableIndex();
    item.cloneTo(x);
    x.setParent(this);
    this.tableElementList.add(x);
    return true;
}
Also used : MySqlUnique(com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique) MySqlKey(com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey)

Example 8 with MySqlUnique

use of com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique in project dble by actiontech.

the class DbleCreateTableParser method parseCreateTable.

public MySqlCreateTableStatement parseCreateTable(boolean acceptCreate) {
    MySqlCreateTableStatement stmt = new MySqlCreateTableStatement();
    if (acceptCreate) {
        if (lexer.hasComment() && lexer.isKeepComments()) {
            stmt.addBeforeComment(lexer.readAndResetComments());
        }
        accept(Token.CREATE);
    }
    if (lexer.identifierEquals("TEMPORARY")) {
        lexer.nextToken();
        stmt.setType(SQLCreateTableStatement.Type.GLOBAL_TEMPORARY);
    } else if (lexer.identifierEquals("SHADOW")) {
        lexer.nextToken();
        stmt.setType(SQLCreateTableStatement.Type.SHADOW);
    }
    if (lexer.identifierEquals(FnvHash.Constants.DIMENSION)) {
        lexer.nextToken();
        stmt.setDimension(true);
    }
    if (lexer.token() == Token.HINT) {
        this.exprParser.parseHints(stmt.getHints());
    }
    if (lexer.identifierEquals(FnvHash.Constants.EXTERNAL)) {
        lexer.nextToken();
        stmt.setExternal(true);
    }
    accept(Token.TABLE);
    if (lexer.token() == Token.IF || lexer.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.WITH) {
        SQLSelect query = new MySqlSelectParser(this.exprParser).select();
        stmt.setSelect(query);
    } else if (lexer.token() == (Token.LPAREN)) {
        lexer.nextToken();
        if (lexer.token() == Token.SELECT) {
            SQLSelect query = new MySqlSelectParser(this.exprParser).select();
            stmt.setSelect(query);
        } else {
            for (; ; ) {
                SQLColumnDefinition column = null;
                boolean global = false;
                if (lexer.identifierEquals(FnvHash.Constants.GLOBAL)) {
                    final Lexer.SavePoint mark = lexer.mark();
                    lexer.nextToken();
                    if (lexer.token() == Token.INDEX || lexer.token() == Token.UNIQUE) {
                        global = true;
                    } else {
                        lexer.reset(mark);
                    }
                }
                if (lexer.token() == Token.FULLTEXT) {
                    Lexer.SavePoint mark = lexer.mark();
                    lexer.nextToken();
                    if (lexer.token() == Token.KEY) {
                        MySqlKey fulltextKey = new MySqlKey();
                        this.exprParser.parseIndex(fulltextKey.getIndexDefinition());
                        fulltextKey.setIndexType("FULLTEXT");
                        fulltextKey.setParent(stmt);
                        stmt.getTableElementList().add(fulltextKey);
                        while (lexer.token() == Token.HINT) {
                            lexer.nextToken();
                        }
                        if (lexer.token() == Token.RPAREN) {
                            break;
                        } else if (lexer.token() == Token.COMMA) {
                            lexer.nextToken();
                            continue;
                        }
                    } else if (lexer.token() == Token.INDEX) {
                        MySqlTableIndex idx = new MySqlTableIndex();
                        this.exprParser.parseIndex(idx.getIndexDefinition());
                        idx.setIndexType("FULLTEXT");
                        idx.setParent(stmt);
                        stmt.getTableElementList().add(idx);
                        if (lexer.token() == Token.RPAREN) {
                            break;
                        } else if (lexer.token() == Token.COMMA) {
                            lexer.nextToken();
                            continue;
                        }
                    } else if (lexer.token() == Token.IDENTIFIER && MySqlUtils.isBuiltinDataType(lexer.stringVal())) {
                        lexer.reset(mark);
                    } else {
                        MySqlTableIndex idx = new MySqlTableIndex();
                        this.exprParser.parseIndex(idx.getIndexDefinition());
                        idx.setIndexType("FULLTEXT");
                        idx.setParent(stmt);
                        stmt.getTableElementList().add(idx);
                        if (lexer.token() == Token.RPAREN) {
                            break;
                        } else if (lexer.token() == Token.COMMA) {
                            lexer.nextToken();
                            continue;
                        }
                    }
                } else if (lexer.identifierEquals(FnvHash.Constants.SPATIAL)) {
                    Lexer.SavePoint mark = lexer.mark();
                    lexer.nextToken();
                    if (lexer.token() == Token.INDEX || lexer.token() == Token.KEY || lexer.token() != Token.IDENTIFIER || !MySqlUtils.isBuiltinDataType(lexer.stringVal())) {
                        MySqlTableIndex idx = new MySqlTableIndex();
                        this.exprParser.parseIndex(idx.getIndexDefinition());
                        idx.setIndexType("SPATIAL");
                        idx.setParent(stmt);
                        stmt.getTableElementList().add(idx);
                        if (lexer.token() == Token.RPAREN) {
                            break;
                        } else if (lexer.token() == Token.COMMA) {
                            lexer.nextToken();
                            continue;
                        }
                    } else {
                        lexer.reset(mark);
                    }
                }
                if (lexer.identifierEquals(FnvHash.Constants.ANN)) {
                    Lexer.SavePoint mark = lexer.mark();
                    lexer.nextToken();
                    if (lexer.token() == Token.INDEX || lexer.token() == Token.KEY) {
                        MySqlTableIndex idx = new MySqlTableIndex();
                        this.exprParser.parseIndex(idx.getIndexDefinition());
                        idx.setIndexType("ANN");
                        idx.setParent(stmt);
                        stmt.getTableElementList().add(idx);
                        if (lexer.token() == Token.RPAREN) {
                            break;
                        } else if (lexer.token() == Token.COMMA) {
                            lexer.nextToken();
                            continue;
                        }
                    } else {
                        lexer.reset(mark);
                    }
                }
                if (lexer.identifierEquals(FnvHash.Constants.CLUSTERED)) {
                    lexer.nextToken();
                    if (lexer.token() == Token.KEY) {
                        MySqlKey clsKey = new MySqlKey();
                        this.exprParser.parseIndex(clsKey.getIndexDefinition());
                        clsKey.setIndexType("CLUSTERED");
                        clsKey.setParent(stmt);
                        stmt.getTableElementList().add(clsKey);
                        if (lexer.token() == Token.COMMA) {
                            lexer.nextToken();
                            continue;
                        }
                    } else if (lexer.token() == Token.INDEX) {
                        MySqlTableIndex idx = new MySqlTableIndex();
                        this.exprParser.parseIndex(idx.getIndexDefinition());
                        idx.setIndexType("CLUSTERED");
                        idx.setParent(stmt);
                        stmt.getTableElementList().add(idx);
                        if (lexer.token() == Token.RPAREN) {
                            break;
                        } else if (lexer.token() == Token.COMMA) {
                            lexer.nextToken();
                            continue;
                        }
                    }
                } else if (lexer.identifierEquals(FnvHash.Constants.CLUSTERING)) {
                    lexer.nextToken();
                    if (lexer.token() == Token.KEY) {
                        MySqlKey clsKey = new MySqlKey();
                        this.exprParser.parseIndex(clsKey.getIndexDefinition());
                        clsKey.setIndexType("CLUSTERING");
                        clsKey.setParent(stmt);
                        stmt.getTableElementList().add(clsKey);
                        if (lexer.token() == Token.COMMA) {
                            lexer.nextToken();
                            continue;
                        }
                    } else if (lexer.token() == Token.INDEX) {
                        MySqlTableIndex idx = new MySqlTableIndex();
                        this.exprParser.parseIndex(idx.getIndexDefinition());
                        idx.setIndexType("CLUSTERING");
                        idx.setParent(stmt);
                        stmt.getTableElementList().add(idx);
                        if (lexer.token() == Token.RPAREN) {
                            break;
                        } else if (lexer.token() == Token.COMMA) {
                            lexer.nextToken();
                            continue;
                        }
                    }
                } else if (lexer.token() == Token.IDENTIFIER || lexer.token() == Token.LITERAL_CHARS) {
                    column = this.exprParser.parseColumn();
                    column.setParent(stmt);
                    stmt.getTableElementList().add(column);
                    if (lexer.isKeepComments() && lexer.hasComment()) {
                        column.addAfterComment(lexer.readAndResetComments());
                    }
                } else if (lexer.token() == Token.CONSTRAINT || lexer.token() == Token.PRIMARY || lexer.token() == Token.UNIQUE) {
                    SQLTableConstraint constraint = this.parseConstraint();
                    constraint.setParent(stmt);
                    if (constraint instanceof MySqlUnique) {
                        MySqlUnique unique = (MySqlUnique) constraint;
                        if (global) {
                            unique.setGlobal(true);
                        }
                    }
                    stmt.getTableElementList().add(constraint);
                } else if (lexer.token() == (Token.INDEX)) {
                    MySqlTableIndex idx = new MySqlTableIndex();
                    this.exprParser.parseIndex(idx.getIndexDefinition());
                    if (global) {
                        idx.getIndexDefinition().setGlobal(true);
                    }
                    idx.setParent(stmt);
                    stmt.getTableElementList().add(idx);
                } else if (lexer.token() == (Token.KEY)) {
                    Lexer.SavePoint savePoint = lexer.mark();
                    lexer.nextToken();
                    boolean isColumn = false;
                    if (lexer.identifierEquals(FnvHash.Constants.VARCHAR)) {
                        isColumn = true;
                    }
                    lexer.reset(savePoint);
                    if (isColumn) {
                        column = this.exprParser.parseColumn();
                        stmt.getTableElementList().add(column);
                    } else {
                        stmt.getTableElementList().add(parseConstraint());
                    }
                } 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 if (lexer.token() == Token.LIKE) {
                    lexer.nextToken();
                    SQLTableLike tableLike = new SQLTableLike();
                    tableLike.setTable(new SQLExprTableSource(this.exprParser.name()));
                    tableLike.setParent(stmt);
                    stmt.getTableElementList().add(tableLike);
                    if (lexer.identifierEquals(FnvHash.Constants.INCLUDING)) {
                        lexer.nextToken();
                        acceptIdentifier("PROPERTIES");
                        tableLike.setIncludeProperties(true);
                    } else if (lexer.identifierEquals(FnvHash.Constants.EXCLUDING)) {
                        lexer.nextToken();
                        acceptIdentifier("PROPERTIES");
                        tableLike.setExcludeProperties(true);
                    }
                } else {
                    column = this.exprParser.parseColumn();
                    stmt.getTableElementList().add(column);
                }
                if (lexer.token() != Token.COMMA) {
                    break;
                } else {
                    lexer.nextToken();
                    if (lexer.isKeepComments() && lexer.hasComment() && column != null) {
                        column.addAfterComment(lexer.readAndResetComments());
                    }
                }
            }
        }
        if (lexer.token() == Token.HINT) {
            lexer.nextToken();
        }
        accept(Token.RPAREN);
        if (lexer.token() == Token.HINT && lexer.stringVal().charAt(0) == '!') {
            lexer.nextToken();
        }
    }
    for (; ; ) {
        if (lexer.token() == Token.COMMA) {
            lexer.nextToken();
        }
        if (lexer.identifierEquals(FnvHash.Constants.ENGINE)) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            SQLExpr expr = null;
            if (lexer.token() == Token.MERGE) {
                expr = new SQLIdentifierExpr(lexer.stringVal());
                lexer.nextToken();
            } else {
                expr = this.exprParser.expr();
            }
            stmt.addOption("ENGINE", expr);
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.BLOCK_SIZE)) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            SQLExpr expr = null;
            if (lexer.token() == Token.MERGE) {
                expr = new SQLIdentifierExpr(lexer.stringVal());
                lexer.nextToken();
            } else {
                expr = this.exprParser.integerExpr();
            }
            stmt.addOption("BLOCK_SIZE", expr);
            continue;
        }
        if (lexer.identifierEquals("BLOCK_FORMAT")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            SQLExpr expr = this.exprParser.primary();
            stmt.addOption("BLOCK_FORMAT", expr);
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.REPLICA_NUM)) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            SQLExpr expr = this.exprParser.integerExpr();
            stmt.addOption("REPLICA_NUM", expr);
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.TABLET_SIZE)) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            SQLExpr expr = this.exprParser.integerExpr();
            stmt.addOption("TABLET_SIZE", expr);
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.PCTFREE)) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            SQLExpr expr = this.exprParser.integerExpr();
            stmt.addOption("PCTFREE", expr);
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.USE_BLOOM_FILTER)) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            SQLExpr expr = this.exprParser.primary();
            stmt.addOption("USE_BLOOM_FILTER", expr);
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.AUTO_INCREMENT)) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            LOGGER.warn("parse sql drop AUTO_INCREMENT detail in " + stmt.getTableSource().getName());
            this.exprParser.expr();
            continue;
        }
        if (lexer.identifierEquals("AVG_ROW_LENGTH")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.addOption("AVG_ROW_LENGTH", this.exprParser.expr());
            continue;
        }
        if (lexer.token() == Token.DEFAULT) {
            lexer.nextToken();
            parseTableOptionCharsetOrCollate(stmt);
            continue;
        }
        if (parseTableOptionCharsetOrCollate(stmt)) {
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.CHECKSUM)) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.addOption("CHECKSUM", this.exprParser.expr());
            continue;
        }
        if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.setComment(this.exprParser.expr());
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.CONNECTION)) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.addOption("CONNECTION", this.exprParser.expr());
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.DATA)) {
            lexer.nextToken();
            acceptIdentifier("DIRECTORY");
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.addOption("DATA DIRECTORY", this.exprParser.expr());
            continue;
        }
        if (lexer.identifierEquals("DELAY_KEY_WRITE")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.addOption("DELAY_KEY_WRITE", this.exprParser.expr());
            continue;
        }
        if (lexer.identifierEquals("FULLTEXT_DICT")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.addOption("FULLTEXT_DICT", this.exprParser.charExpr());
            continue;
        }
        if (lexer.token() == Token.INDEX) {
            lexer.nextToken();
            acceptIdentifier("DIRECTORY");
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.addOption("INDEX DIRECTORY", this.exprParser.expr());
            continue;
        }
        if (lexer.identifierEquals("INSERT_METHOD")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.addOption("INSERT_METHOD", this.exprParser.expr());
            continue;
        }
        if (lexer.identifierEquals("KEY_BLOCK_SIZE")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.addOption("KEY_BLOCK_SIZE", this.exprParser.expr());
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.MAX_ROWS)) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.addOption("MAX_ROWS", this.exprParser.expr());
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.MIN_ROWS)) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.addOption("MIN_ROWS", this.exprParser.expr());
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.PACK_KEYS)) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.addOption("PACK_KEYS", this.exprParser.expr());
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.PASSWORD)) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.addOption("PASSWORD", this.exprParser.expr());
            continue;
        }
        if (lexer.identifierEquals("ROW_FORMAT")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.addOption("ROW_FORMAT", this.exprParser.expr());
            continue;
        }
        if (lexer.identifierEquals("STATS_AUTO_RECALC")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.addOption("STATS_AUTO_RECALC", this.exprParser.expr());
            continue;
        }
        if (lexer.identifierEquals("STATS_PERSISTENT")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.addOption("STATS_PERSISTENT", this.exprParser.expr());
            continue;
        }
        if (lexer.identifierEquals("STATS_SAMPLE_PAGES")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.addOption("STATS_SAMPLE_PAGES", this.exprParser.expr());
            continue;
        }
        if (lexer.token() == Token.UNION) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            accept(Token.LPAREN);
            SQLListExpr list = new SQLListExpr();
            this.exprParser.exprList(list.getItems(), list);
            stmt.addOption("UNION", list);
            accept(Token.RPAREN);
            continue;
        }
        if (lexer.token() == Token.TABLESPACE) {
            lexer.nextToken();
            MySqlCreateTableStatement.TableSpaceOption option = new MySqlCreateTableStatement.TableSpaceOption();
            option.setName(this.exprParser.name());
            if (lexer.identifierEquals("STORAGE")) {
                lexer.nextToken();
                option.setStorage(this.exprParser.name());
            }
            stmt.addOption("TABLESPACE", option);
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.TABLEGROUP)) {
            lexer.nextToken();
            SQLName tableGroup = this.exprParser.name();
            stmt.setTableGroup(tableGroup);
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.TYPE)) {
            lexer.nextToken();
            accept(Token.EQ);
            stmt.addOption("TYPE", this.exprParser.expr());
            continue;
        }
        if (lexer.identifierEquals("INDEX_ALL")) {
            lexer.nextToken();
            accept(Token.EQ);
            if (lexer.token() == Token.LITERAL_CHARS) {
                if ("Y".equalsIgnoreCase(lexer.stringVal())) {
                    lexer.nextToken();
                    stmt.addOption("INDEX_ALL", new SQLCharExpr("Y"));
                } else if ("N".equalsIgnoreCase(lexer.stringVal())) {
                    lexer.nextToken();
                    stmt.addOption("INDEX_ALL", new SQLCharExpr("N"));
                } else {
                    throw new ParserException("INDEX_ALL accept parameter ['Y' or 'N'] only.");
                }
            }
            continue;
        }
        if (lexer.identifierEquals("RT_INDEX_ALL")) {
            lexer.nextToken();
            accept(Token.EQ);
            if (lexer.token() == Token.LITERAL_CHARS) {
                if ("Y".equalsIgnoreCase(lexer.stringVal())) {
                    lexer.nextToken();
                    stmt.addOption("RT_INDEX_ALL", new SQLCharExpr("Y"));
                } else if ("N".equalsIgnoreCase(lexer.stringVal())) {
                    lexer.nextToken();
                    stmt.addOption("RT_INDEX_ALL", new SQLCharExpr("N"));
                } else {
                    throw new ParserException("RT_INDEX_ALL accepts parameter ['Y' or 'N'] only.");
                }
            }
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.ARCHIVE)) {
            lexer.nextToken();
            accept(Token.BY);
            acceptIdentifier("OSS");
            stmt.setArchiveBy(new SQLIdentifierExpr("OSS"));
            continue;
        }
        if (lexer.identifierEquals("STORAGE_TYPE")) {
            lexer.nextToken();
            accept(Token.EQ);
            stmt.addOption("STORAGE_TYPE", this.exprParser.charExpr());
            continue;
        }
        if (lexer.identifierEquals("STORAGE_POLICY")) {
            lexer.nextToken();
            accept(Token.EQ);
            stmt.addOption("STORAGE_POLICY", this.exprParser.charExpr());
            continue;
        }
        if (lexer.identifierEquals("HOT_PARTITION_COUNT")) {
            lexer.nextToken();
            accept(Token.EQ);
            try {
                stmt.addOption("HOT_PARTITION_COUNT", this.exprParser.integerExpr());
            } catch (Exception e) {
                throw new ParserException("only integer number is supported for hot_partition_count");
            }
            continue;
        }
        if (lexer.identifierEquals("TABLE_PROPERTIES")) {
            lexer.nextToken();
            accept(Token.EQ);
            stmt.addOption("TABLE_PROPERTIES", exprParser.charExpr());
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.ENCRYPTION)) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.addOption("ENCRYPTION", this.exprParser.expr());
            continue;
        } else if (lexer.identifierEquals(FnvHash.Constants.COMPRESSION)) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.addOption("COMPRESSION", this.exprParser.expr());
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.CLUSTERED)) {
            lexer.nextToken();
            accept(Token.BY);
            accept(Token.LPAREN);
            for (; ; ) {
                SQLSelectOrderByItem item = this.exprParser.parseSelectOrderByItem();
                stmt.addClusteredByItem(item);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                }
                break;
            }
            accept(Token.RPAREN);
            continue;
        }
        if (lexer.token() == Token.PARTITION) {
            SQLPartitionBy partitionClause = parsePartitionBy();
            stmt.setPartitioning(partitionClause);
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.BROADCAST)) {
            lexer.nextToken();
            stmt.setBroadCast(true);
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.DISTRIBUTE) || lexer.identifierEquals(FnvHash.Constants.DISTRIBUTED)) {
            lexer.nextToken();
            accept(Token.BY);
            if (lexer.identifierEquals(FnvHash.Constants.HASH)) {
                lexer.nextToken();
                accept(Token.LPAREN);
                for (; ; ) {
                    SQLName name = this.exprParser.name();
                    stmt.getDistributeBy().add(name);
                    if (lexer.token() == Token.COMMA) {
                        lexer.nextToken();
                        continue;
                    }
                    break;
                }
                accept(Token.RPAREN);
                stmt.setDistributeByType(new SQLIdentifierExpr("HASH"));
            } else if (lexer.identifierEquals(FnvHash.Constants.BROADCAST)) {
                lexer.nextToken();
                stmt.setDistributeByType(new SQLIdentifierExpr("BROADCAST"));
            }
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.DBPARTITION)) {
            lexer.nextToken();
            accept(Token.BY);
            SQLExpr dbPartitoinBy = this.exprParser.primary();
            stmt.setDbPartitionBy(dbPartitoinBy);
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.DBPARTITIONS)) {
            lexer.nextToken();
            SQLExpr dbPartitoins = this.exprParser.primary();
            stmt.setDbPartitions(dbPartitoins);
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.TBPARTITION)) {
            lexer.nextToken();
            accept(Token.BY);
            SQLExpr expr = this.exprParser.expr();
            if (lexer.identifierEquals(FnvHash.Constants.STARTWITH)) {
                lexer.nextToken();
                SQLExpr start = this.exprParser.primary();
                acceptIdentifier("ENDWITH");
                SQLExpr end = this.exprParser.primary();
                expr = new SQLBetweenExpr(expr, start, end);
            }
            stmt.setTablePartitionBy(expr);
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.TBPARTITIONS)) {
            lexer.nextToken();
            SQLExpr tbPartitions = this.exprParser.primary();
            stmt.setTablePartitions(tbPartitions);
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.EXTPARTITION)) {
            lexer.nextToken();
            accept(Token.LPAREN);
            MySqlExtPartition partitionDef = new MySqlExtPartition();
            for (; ; ) {
                MySqlExtPartition.Item item = new MySqlExtPartition.Item();
                if (lexer.identifierEquals(FnvHash.Constants.DBPARTITION)) {
                    lexer.nextToken();
                    SQLName name = this.exprParser.name();
                    item.setDbPartition(name);
                    accept(Token.BY);
                    SQLExpr value = this.exprParser.primary();
                    item.setDbPartitionBy(value);
                }
                if (lexer.identifierEquals(FnvHash.Constants.TBPARTITION)) {
                    lexer.nextToken();
                    SQLName name = this.exprParser.name();
                    item.setTbPartition(name);
                    accept(Token.BY);
                    SQLExpr value = this.exprParser.primary();
                    item.setTbPartitionBy(value);
                }
                item.setParent(partitionDef);
                partitionDef.getItems().add(item);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                } else {
                    break;
                }
            }
            accept(Token.RPAREN);
            stmt.setExPartition(partitionDef);
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.OPTIONS)) {
            lexer.nextToken();
            accept(Token.LPAREN);
            stmt.putAttribute("ads.options", Boolean.TRUE);
            for (; ; ) {
                String name = lexer.stringVal();
                lexer.nextToken();
                accept(Token.EQ);
                SQLExpr value = this.exprParser.primary();
                stmt.addOption(name, value);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                }
                break;
            }
            accept(Token.RPAREN);
            continue;
        }
        if (lexer.identifierEquals(FnvHash.Constants.STORED)) {
            lexer.nextToken();
            accept(Token.BY);
            SQLName name = this.exprParser.name();
            stmt.setStoredBy(name);
        }
        if (lexer.token() == Token.WITH) {
            lexer.nextToken();
            accept(Token.LPAREN);
            for (; ; ) {
                String name = lexer.stringVal();
                lexer.nextToken();
                accept(Token.EQ);
                SQLName value = this.exprParser.name();
                stmt.getWith().put(name, value);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                }
                break;
            }
            accept(Token.RPAREN);
            continue;
        }
        if (lexer.token() == (Token.HINT)) {
            this.exprParser.parseHints(stmt.getOptionHints());
            continue;
        }
        break;
    }
    if (lexer.token() == (Token.ON)) {
        throw new ParserException("TODO. " + lexer.info());
    }
    if (lexer.token() == Token.REPLACE) {
        lexer.nextToken();
        stmt.setReplace(true);
    } else if (lexer.identifierEquals("IGNORE")) {
        lexer.nextToken();
        stmt.setIgnore(true);
    }
    if (lexer.token() == (Token.AS)) {
        lexer.nextToken();
        if (lexer.token() == Token.LPAREN) {
            lexer.nextToken();
            SQLSelect query = new MySqlSelectParser(this.exprParser).select();
            stmt.setSelect(query);
            accept(Token.RPAREN);
        }
    }
    SQLCommentHint hint = null;
    if (lexer.token() == Token.HINT) {
        hint = this.exprParser.parseHint();
    }
    if (lexer.token() == (Token.SELECT)) {
        SQLSelect query = new MySqlSelectParser(this.exprParser).select();
        if (hint != null) {
            query.setHeadHint(hint);
        }
        stmt.setSelect(query);
        if (lexer.token() == Token.WITH) {
            lexer.nextToken();
            if (lexer.identifierEquals(FnvHash.Constants.NO)) {
                lexer.nextToken();
                acceptIdentifier("DATA");
                stmt.setWithData(false);
            } else {
                acceptIdentifier("DATA");
                stmt.setWithData(true);
            }
        }
    }
    while (lexer.token() == (Token.HINT)) {
        this.exprParser.parseHints(stmt.getOptionHints());
    }
    return stmt;
}
Also used : MySqlKey(com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey) MySqlUnique(com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique) ParserException(com.alibaba.druid.sql.parser.ParserException) MySqlSelectParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlSelectParser) ParserException(com.alibaba.druid.sql.parser.ParserException) Lexer(com.alibaba.druid.sql.parser.Lexer)

Example 9 with MySqlUnique

use of com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique in project dble by actiontech.

the class TestParser method test.

public void test(String sql) {
    System.out.println("-----------------------------------------------------------");
    System.out.println("-----------------------------------------------------------");
    System.out.println(sql);
    SQLStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement statement = parser.parseStatement();
    if (statement instanceof MySqlExplainStatement) {
        System.out.println("MySqlExplainStatement" + statement.toString());
    } else if (statement instanceof MySqlCreateTableStatement) {
        MySqlCreateTableStatement createStment = (MySqlCreateTableStatement) statement;
        SQLExpr expr = createStment.getTableSource().getExpr();
        if (expr instanceof SQLPropertyExpr) {
            SQLPropertyExpr propertyExpr = (SQLPropertyExpr) expr;
            System.out.println((propertyExpr.getOwner().toString()));
            System.out.println(propertyExpr.getName());
        } else if (expr instanceof SQLIdentifierExpr) {
            SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) expr;
            System.out.println(identifierExpr.getName());
        } else {
            System.out.println(expr.getClass() + "\n");
        }
    } else if (statement instanceof SQLAlterTableStatement) {
        SQLAlterTableStatement alterStatement = (SQLAlterTableStatement) statement;
        SQLExprTableSource tableSource = alterStatement.getTableSource();
        for (SQLAlterTableItem alterItem : alterStatement.getItems()) {
            if (alterItem instanceof SQLAlterTableAddColumn) {
                SQLAlterTableAddColumn addColumn = (SQLAlterTableAddColumn) alterItem;
                boolean isFirst = addColumn.isFirst();
                SQLName afterColumn = addColumn.getAfterColumn();
                if (afterColumn != null) {
                    System.out.println(sql + ": afterColumns:\n" + afterColumn.getClass().toString() + "\n");
                }
                for (SQLColumnDefinition columnDef : addColumn.getColumns()) {
                }
            } else if (alterItem instanceof SQLAlterTableAddIndex) {
                SQLAlterTableAddIndex addIndex = (SQLAlterTableAddIndex) alterItem;
                SQLName name = addIndex.getName();
                System.out.println(sql + ":indexname:\n" + name.getClass().toString() + "\n");
                String type = addIndex.getType();
                // ??
                addIndex.isUnique();
                for (SQLSelectOrderByItem item : addIndex.getItems()) {
                    System.out.println(sql + ": item.getExpr():\n" + item.getExpr().getClass().toString() + "\n");
                }
            } else if (alterItem instanceof SQLAlterTableAddConstraint) {
                SQLAlterTableAddConstraint addConstraint = (SQLAlterTableAddConstraint) alterItem;
                SQLConstraint constraint = addConstraint.getConstraint();
                if (constraint instanceof MySqlUnique) {
                    MySqlUnique unique = (MySqlUnique) constraint;
                    unique.getName();
                } else if (constraint instanceof MySqlPrimaryKey) {
                } else if (constraint instanceof MysqlForeignKey) {
                    System.out.println("NOT SUPPORT\n");
                }
                System.out.println(sql + ": constraint:\n" + constraint.getClass().toString() + "\n");
            } else if (alterItem instanceof SQLAlterTableDropIndex) {
                SQLAlterTableDropIndex dropIndex = (SQLAlterTableDropIndex) alterItem;
                SQLIdentifierExpr indexName = (SQLIdentifierExpr) dropIndex.getIndexName();
                String strIndexName = indexName.getName();
            } else if (alterItem instanceof SQLAlterTableDropKey) {
                SQLAlterTableDropKey dropIndex = (SQLAlterTableDropKey) alterItem;
                SQLIdentifierExpr indexName = (SQLIdentifierExpr) dropIndex.getKeyName();
                String strIndexName = indexName.getName();
            } else if (alterItem instanceof MySqlAlterTableChangeColumn) {
                MySqlAlterTableChangeColumn changeColumn = (MySqlAlterTableChangeColumn) alterItem;
                boolean isFirst = changeColumn.isFirst();
                SQLIdentifierExpr afterColumn = (SQLIdentifierExpr) changeColumn.getAfterColumn();
                // SQLExpr afterColumn = changeColumn.getAfterColumn();
                if (afterColumn != null) {
                    String strAfterColumn = afterColumn.getName();
                }
                SQLColumnDefinition columnDef = changeColumn.getNewColumnDefinition();
            } else if (alterItem instanceof MySqlAlterTableModifyColumn) {
                MySqlAlterTableModifyColumn modifyColumn = (MySqlAlterTableModifyColumn) alterItem;
                boolean isFirst = modifyColumn.isFirst();
                SQLExpr afterColumn = modifyColumn.getAfterColumn();
                if (afterColumn != null) {
                    System.out.println(sql + ": afterColumns:\n" + afterColumn.getClass().toString() + "\n");
                }
                SQLColumnDefinition columnDef = modifyColumn.getNewColumnDefinition();
            } else if (alterItem instanceof SQLAlterTableDropColumnItem) {
                SQLAlterTableDropColumnItem dropColumn = (SQLAlterTableDropColumnItem) alterItem;
                for (SQLName dropName : dropColumn.getColumns()) {
                    System.out.println(sql + ":dropName:\n" + dropName.getClass().toString() + "\n");
                }
            } else if (alterItem instanceof SQLAlterTableDropPrimaryKey) {
                SQLAlterTableDropPrimaryKey dropPrimary = (SQLAlterTableDropPrimaryKey) alterItem;
            } else {
                System.out.println(sql + ":\n" + alterItem.getClass().toString() + "\n");
            }
            System.out.println("\n" + statement.toString());
        }
    } else if (statement instanceof SQLDropTableStatement) {
    } else if (statement instanceof SQLTruncateStatement) {
    // TODO:Sequence?
    } else if (statement instanceof SQLDropIndexStatement) {
    // TODO
    } else if (statement instanceof MySqlDeleteStatement) {
        MySqlDeleteStatement deleteStatement = (MySqlDeleteStatement) statement;
        SQLTableSource tableSource = deleteStatement.getTableSource();
        System.out.println(sql + ":getTableSource:" + tableSource.getClass().toString() + "\n");
        if (deleteStatement.getFrom() != null) {
            System.out.println(sql + ":getSchema:" + deleteStatement.getFrom().getClass().toString() + "\n");
        }
        System.out.println("\n");
    } else if (statement instanceof MySqlUpdateStatement) {
        MySqlUpdateStatement updateStatement = (MySqlUpdateStatement) statement;
        SQLTableSource tableSource = updateStatement.getTableSource();
        System.out.println(sql + ":getTableSource:" + tableSource.getClass().toString() + "\n");
        System.out.println("\n" + statement.toString());
    } else if (statement instanceof SQLCreateIndexStatement) {
        SQLCreateIndexStatement stament = (SQLCreateIndexStatement) statement;
        SQLTableSource tableSource = stament.getTable();
        System.out.println(sql + ":getTableSource:" + tableSource.getClass().toString() + "\n");
        System.out.println(sql + stament.getType());
    } else if (statement instanceof SQLSelectStatement) {
        SQLSelectStatement stament = (SQLSelectStatement) statement;
        SQLSelectQuery sqlSelectQuery = stament.getSelect().getQuery();
        if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
            MySqlSelectQueryBlock selectQueryBlock = (MySqlSelectQueryBlock) sqlSelectQuery;
            SQLTableSource fromSource = selectQueryBlock.getFrom();
            if (fromSource instanceof SQLJoinTableSource) {
                SQLJoinTableSource fromJoinSource = (SQLJoinTableSource) fromSource;
                System.out.println("SQLJoinTableSource:");
                System.out.println("all:" + fromJoinSource.toString());
                System.out.println("left:" + fromJoinSource.getLeft().toString() + ",class" + fromJoinSource.getLeft().getClass());
                System.out.println("right:" + fromJoinSource.getRight().toString() + ",class" + fromJoinSource.getRight().getClass());
                System.out.println("---------------------------");
            }
            for (SQLSelectItem item : selectQueryBlock.getSelectList()) {
                if (item.getExpr() != null) {
                    SQLExpr func = item.getExpr();
                    if (func instanceof SQLAggregateExpr) {
                        System.out.println("SQLAggregateExpr:");
                        SQLAggregateExpr agg = (SQLAggregateExpr) func;
                        System.out.println("MethodName:" + agg.getMethodName() + ",getArguments size =" + agg.getArguments().size() + ",Option:" + agg.getOption());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLMethodInvokeExpr) {
                        System.out.println("SQLMethodInvokeExpr:");
                        SQLMethodInvokeExpr method = (SQLMethodInvokeExpr) func;
                        System.out.println("MethodName:" + method.getMethodName() + ",getArguments size =" + method.getArguments().size() + ",OWNER:" + method.getOwner());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLCastExpr) {
                        SQLCastExpr cast = (SQLCastExpr) func;
                        System.out.println("SQLCastExpr:");
                        System.out.println("Expr:" + cast.getExpr().getClass() + ",getDataType:" + cast.getDataType());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLBinaryOpExpr) {
                        SQLBinaryOpExpr Op = (SQLBinaryOpExpr) func;
                        System.out.println("SQLBinaryOpExpr:");
                        System.out.println("left:" + Op.getLeft().getClass());
                        System.out.println("right:" + Op.getRight().getClass());
                        System.out.println("operator:" + Op.getOperator().getClass());
                        System.out.println("dbtype:" + Op.getDbType());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLUnaryExpr) {
                        SQLUnaryExpr Op = (SQLUnaryExpr) func;
                        System.out.println("SQLUnaryExpr:");
                        System.out.println("EXPR:" + Op.getExpr().getClass());
                        System.out.println("operator:" + Op.getOperator().getClass());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLBetweenExpr) {
                        SQLBetweenExpr between = (SQLBetweenExpr) func;
                        System.out.println("SQLBetweenExpr:");
                        System.out.println("begin EXPR:" + between.getBeginExpr());
                        System.out.println("end EXPR:" + between.getEndExpr());
                        System.out.println("isnot :" + between.isNot());
                        System.out.println("test :" + between.getTestExpr());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLInListExpr) {
                        SQLInListExpr in = (SQLInListExpr) func;
                        System.out.println("SQLInListExpr:");
                        System.out.println("EXPR:" + in.getExpr());
                        System.out.println("isnot :" + in.isNot());
                        System.out.println("getTargetList size :" + in.getTargetList().size());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLNotExpr) {
                        SQLNotExpr not = (SQLNotExpr) func;
                        System.out.println("SQLNotExpr:");
                        System.out.println("EXPR:" + not.getExpr());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLExtractExpr) {
                        SQLExtractExpr extract = (SQLExtractExpr) func;
                        System.out.println("MySqlExtractExpr:");
                        System.out.println("value:" + extract.getValue());
                        System.out.println("unit:" + extract.getUnit());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLCaseExpr) {
                        SQLCaseExpr Case = (SQLCaseExpr) func;
                        System.out.println("SQLCaseExpr:");
                        System.out.println("value:" + Case.getValueExpr());
                        System.out.println("else:" + Case.getElseExpr());
                        System.out.println("items size:" + Case.getItems().size());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLVariantRefExpr) {
                        SQLVariantRefExpr variant = (SQLVariantRefExpr) func;
                        System.out.println("SQLVariantRefExpr:");
                        System.out.println("name:" + variant.getName());
                        System.out.println("Global:" + variant.isGlobal());
                        System.out.println("index:" + variant.getIndex());
                        System.out.println("---------------------------");
                    } else // SQLAllColumnExpr
                    {
                        // MySqlOutputVisitor
                        System.out.println("item.getExpr():   :" + item.getExpr().getClass().toString() + "\n");
                    }
                }
            }
            // SQLInSubQueryExpr
            if (selectQueryBlock.getGroupBy() != null) {
                SQLSelectGroupByClause groupBy = selectQueryBlock.getGroupBy();
                for (SQLExpr groupByItem : groupBy.getItems()) {
                    System.out.println("groupByItem:");
                    System.out.println("class :" + groupByItem.getClass());
                    System.out.println("---------------------------");
                }
                if (groupBy.getHaving() != null) {
                    SQLExpr having = groupBy.getHaving();
                    System.out.println("having:");
                    System.out.println("class :" + having.getClass());
                    System.out.println("---------------------------");
                }
            // with rollup...
            }
            if (selectQueryBlock.getOrderBy() != null) {
                for (SQLSelectOrderByItem orderItem : selectQueryBlock.getOrderBy().getItems()) {
                    System.out.println("OrderBy:");
                    System.out.println("class :" + orderItem.getExpr().getClass());
                    System.out.println("---------------------------");
                }
            }
            if (selectQueryBlock.getWhere() != null) {
                if (selectQueryBlock.getWhere() instanceof SQLBinaryOpExpr) {
                    SQLBinaryOpExpr where = (SQLBinaryOpExpr) (selectQueryBlock.getWhere());
                    System.out.println("where right:");
                    System.out.println("class :" + where.getRight().getClass());
                    System.out.println("---------------------------");
                } else {
                    System.out.println("where:");
                    System.out.println("class :" + selectQueryBlock.getWhere().getClass());
                    System.out.println("---------------------------");
                }
            }
        } else if (sqlSelectQuery instanceof SQLUnionQuery) {
        }
    } else if (statement instanceof SQLShowColumnsStatement) {
        SQLShowColumnsStatement showColumnsStatement = (SQLShowColumnsStatement) statement;
        showColumnsStatement.setDatabase(null);
        showColumnsStatement.toString();
        System.out.println("change to->" + showColumnsStatement.toString());
    } else if (statement instanceof SQLShowIndexesStatement) {
        SQLShowIndexesStatement mySqlShowIndexesStatement = (SQLShowIndexesStatement) statement;
        mySqlShowIndexesStatement.setDatabase(null);
        mySqlShowIndexesStatement.toString();
        System.out.println("change to 1->" + mySqlShowIndexesStatement.toString());
        System.out.println("change to 2->" + SQLUtils.toMySqlString(mySqlShowIndexesStatement));
    } else if (statement instanceof SQLSetStatement) {
        SQLSetStatement setStatement = (SQLSetStatement) statement;
        for (SQLAssignItem assignItem : setStatement.getItems()) {
            if (assignItem.getTarget() instanceof SQLVariantRefExpr) {
                SQLVariantRefExpr target = (SQLVariantRefExpr) assignItem.getTarget();
                System.out.println("target is " + target + ", global is " + target.isGlobal() + ", session is " + target.isSession());
            } else if (assignItem.getTarget() instanceof SQLPropertyExpr) {
                SQLPropertyExpr target = (SQLPropertyExpr) assignItem.getTarget();
                System.out.println("target is " + target.getName() + ", Owner is " + target.getOwner());
            } else if (assignItem.getTarget() instanceof SQLIdentifierExpr) {
                SQLIdentifierExpr target = (SQLIdentifierExpr) assignItem.getTarget();
                System.out.println("target is " + target.getName());
            } else {
                System.out.println("target is " + assignItem.getTarget() + ", class is " + assignItem.getTarget().getClass());
            }
            if (assignItem.getValue() instanceof SQLIdentifierExpr) {
                SQLIdentifierExpr value = (SQLIdentifierExpr) assignItem.getValue();
                System.out.println("value is " + value.getName() + ", class is " + assignItem.getValue().getClass());
            } else if (assignItem.getValue() instanceof MySqlCharExpr) {
                MySqlCharExpr value = (MySqlCharExpr) assignItem.getValue();
                System.out.println("value is " + value.getText() + ",charset is " + value.getCharset() + ",collate is " + value.getCollate() + ", class is " + assignItem.getValue().getClass());
            } else if (assignItem.getValue() instanceof SQLCharExpr) {
                SQLCharExpr value = (SQLCharExpr) assignItem.getValue();
                System.out.println("value is " + value.getText() + ", class is " + assignItem.getValue().getClass());
            } else if (assignItem.getValue() instanceof SQLDefaultExpr) {
                SQLDefaultExpr value = (SQLDefaultExpr) assignItem.getValue();
                System.out.println("value is " + value.toString() + ", class is " + assignItem.getValue().getClass());
            } else {
                System.out.println("value is " + assignItem.getValue() + ", class is " + assignItem.getValue().getClass());
            }
        }
    // } else if (statement instanceof MySqlSetNamesStatement) {
    // MySqlSetNamesStatement setStatement = (MySqlSetNamesStatement) statement;
    // System.out.println("charset ="+setStatement.getCharSet()+ ",Collate ="+setStatement.getCollate()+",default ="+setStatement.isDefault());
    // } else if (statement instanceof MySqlSetCharSetStatement) {
    // MySqlSetCharSetStatement setStatement = (MySqlSetCharSetStatement) statement;
    // System.out.println("charset ="+setStatement.getCharSet()+ ",Collate ="+setStatement.getCollate()+",default ="+setStatement.isDefault());
    } else if (statement instanceof MySqlSetTransactionStatement) {
        MySqlSetTransactionStatement setStatement = (MySqlSetTransactionStatement) statement;
        System.out.println("global" + setStatement.getGlobal() + ",IsolationLevel=" + setStatement.getIsolationLevel() + ",access mode" + setStatement.getAccessModel());
    } else {
        System.out.println("statement:" + statement + "," + statement.getClass().toString());
    }
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MySqlUnique(com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique) MySqlCharExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) MysqlForeignKey(com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey) MySqlPrimaryKey(com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey)

Example 10 with MySqlUnique

use of com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique in project dble by actiontech.

the class Testparser method test.

public void test(String sql) {
    System.out.println("-----------------------------------------------------------");
    System.out.println("-----------------------------------------------------------");
    System.out.println(sql);
    SQLStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement statement = parser.parseStatement();
    if (statement instanceof MySqlExplainStatement) {
        System.out.println("MySqlExplainStatement" + statement.toString());
    } else if (statement instanceof MySqlCreateTableStatement) {
        MySqlCreateTableStatement createStment = (MySqlCreateTableStatement) statement;
        SQLExpr expr = createStment.getTableSource().getExpr();
        if (expr instanceof SQLPropertyExpr) {
            SQLPropertyExpr propertyExpr = (SQLPropertyExpr) expr;
            System.out.println((propertyExpr.getOwner().toString()));
            System.out.println(propertyExpr.getName());
        } else if (expr instanceof SQLIdentifierExpr) {
            SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) expr;
            System.out.println(identifierExpr.getName());
        } else {
            System.out.println(expr.getClass() + "\n");
        }
    } else if (statement instanceof SQLAlterTableStatement) {
        SQLAlterTableStatement alterStatement = (SQLAlterTableStatement) statement;
        SQLExprTableSource tableSource = alterStatement.getTableSource();
        for (SQLAlterTableItem alterItem : alterStatement.getItems()) {
            if (alterItem instanceof SQLAlterTableAddColumn) {
                SQLAlterTableAddColumn addColumn = (SQLAlterTableAddColumn) alterItem;
                boolean isFirst = addColumn.isFirst();
                SQLName afterColumn = addColumn.getAfterColumn();
                if (afterColumn != null) {
                    System.out.println(sql + ": afterColumns:\n" + afterColumn.getClass().toString() + "\n");
                }
                for (SQLColumnDefinition columnDef : addColumn.getColumns()) {
                }
            } else if (alterItem instanceof SQLAlterTableAddIndex) {
                SQLAlterTableAddIndex addIndex = (SQLAlterTableAddIndex) alterItem;
                SQLName name = addIndex.getName();
                System.out.println(sql + ":indexname:\n" + name.getClass().toString() + "\n");
                String type = addIndex.getType();
                // ??
                addIndex.isUnique();
                for (SQLSelectOrderByItem item : addIndex.getItems()) {
                    System.out.println(sql + ": item.getExpr():\n" + item.getExpr().getClass().toString() + "\n");
                }
            } else if (alterItem instanceof SQLAlterTableAddConstraint) {
                SQLAlterTableAddConstraint addConstraint = (SQLAlterTableAddConstraint) alterItem;
                SQLConstraint constraint = addConstraint.getConstraint();
                if (constraint instanceof MySqlUnique) {
                    MySqlUnique unique = (MySqlUnique) constraint;
                    unique.getName();
                } else if (constraint instanceof MySqlPrimaryKey) {
                } else if (constraint instanceof MysqlForeignKey) {
                    System.out.println("NOT SUPPORT\n");
                }
                System.out.println(sql + ": constraint:\n" + constraint.getClass().toString() + "\n");
            } else if (alterItem instanceof SQLAlterTableDropIndex) {
                SQLAlterTableDropIndex dropIndex = (SQLAlterTableDropIndex) alterItem;
                SQLIdentifierExpr indexName = (SQLIdentifierExpr) dropIndex.getIndexName();
                String strIndexName = indexName.getName();
            } else if (alterItem instanceof SQLAlterTableDropKey) {
                SQLAlterTableDropKey dropIndex = (SQLAlterTableDropKey) alterItem;
                SQLIdentifierExpr indexName = (SQLIdentifierExpr) dropIndex.getKeyName();
                String strIndexName = indexName.getName();
            } else if (alterItem instanceof MySqlAlterTableChangeColumn) {
                MySqlAlterTableChangeColumn changeColumn = (MySqlAlterTableChangeColumn) alterItem;
                boolean isFirst = changeColumn.isFirst();
                SQLIdentifierExpr afterColumn = (SQLIdentifierExpr) changeColumn.getAfterColumn();
                // SQLExpr afterColumn = changeColumn.getAfterColumn();
                if (afterColumn != null) {
                    String strAfterColumn = afterColumn.getName();
                }
                SQLColumnDefinition columnDef = changeColumn.getNewColumnDefinition();
            } else if (alterItem instanceof MySqlAlterTableModifyColumn) {
                MySqlAlterTableModifyColumn modifyColumn = (MySqlAlterTableModifyColumn) alterItem;
                boolean isFirst = modifyColumn.isFirst();
                SQLExpr afterColumn = modifyColumn.getAfterColumn();
                if (afterColumn != null) {
                    System.out.println(sql + ": afterColumns:\n" + afterColumn.getClass().toString() + "\n");
                }
                SQLColumnDefinition columnDef = modifyColumn.getNewColumnDefinition();
            } else if (alterItem instanceof SQLAlterTableDropColumnItem) {
                SQLAlterTableDropColumnItem dropColumn = (SQLAlterTableDropColumnItem) alterItem;
                for (SQLName dropName : dropColumn.getColumns()) {
                    System.out.println(sql + ":dropName:\n" + dropName.getClass().toString() + "\n");
                }
            } else if (alterItem instanceof SQLAlterTableDropPrimaryKey) {
                SQLAlterTableDropPrimaryKey dropPrimary = (SQLAlterTableDropPrimaryKey) alterItem;
            } else {
                System.out.println(sql + ":\n" + alterItem.getClass().toString() + "\n");
            }
            System.out.println("\n" + statement.toString());
        }
    } else if (statement instanceof SQLDropTableStatement) {
    } else if (statement instanceof SQLTruncateStatement) {
    // TODO:Sequence?
    } else if (statement instanceof SQLDropIndexStatement) {
    // TODO
    } else if (statement instanceof MySqlDeleteStatement) {
        MySqlDeleteStatement deleteStatement = (MySqlDeleteStatement) statement;
        SQLTableSource tableSource = deleteStatement.getTableSource();
        System.out.println(sql + ":getTableSource:" + tableSource.getClass().toString() + "\n");
        if (deleteStatement.getFrom() != null) {
            System.out.println(sql + ":getSchema:" + deleteStatement.getFrom().getClass().toString() + "\n");
        }
        System.out.println("\n");
    } else if (statement instanceof MySqlUpdateStatement) {
        MySqlUpdateStatement updateStatement = (MySqlUpdateStatement) statement;
        SQLTableSource tableSource = updateStatement.getTableSource();
        System.out.println(sql + ":getTableSource:" + tableSource.getClass().toString() + "\n");
        System.out.println("\n" + statement.toString());
    } else if (statement instanceof SQLCreateIndexStatement) {
        SQLCreateIndexStatement stament = (SQLCreateIndexStatement) statement;
        SQLTableSource tableSource = stament.getTable();
        System.out.println(sql + ":getTableSource:" + tableSource.getClass().toString() + "\n");
        System.out.println(sql + stament.getType());
    } else if (statement instanceof SQLSelectStatement) {
        SQLSelectStatement stament = (SQLSelectStatement) statement;
        SQLSelectQuery sqlSelectQuery = stament.getSelect().getQuery();
        if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
            MySqlSelectQueryBlock selectQueryBlock = (MySqlSelectQueryBlock) sqlSelectQuery;
            SQLTableSource fromSource = selectQueryBlock.getFrom();
            if (fromSource instanceof SQLJoinTableSource) {
                SQLJoinTableSource fromJoinSource = (SQLJoinTableSource) fromSource;
                System.out.println("SQLJoinTableSource:");
                System.out.println("all:" + fromJoinSource.toString());
                System.out.println("left:" + fromJoinSource.getLeft().toString() + ",class" + fromJoinSource.getLeft().getClass());
                System.out.println("right:" + fromJoinSource.getRight().toString() + ",class" + fromJoinSource.getRight().getClass());
                System.out.println("---------------------------");
            }
            for (SQLSelectItem item : selectQueryBlock.getSelectList()) {
                if (item.getExpr() != null) {
                    SQLExpr func = item.getExpr();
                    if (func instanceof SQLAggregateExpr) {
                        System.out.println("SQLAggregateExpr:");
                        SQLAggregateExpr agg = (SQLAggregateExpr) func;
                        System.out.println("MethodName:" + agg.getMethodName() + ",getArguments size =" + agg.getArguments().size() + ",Option:" + agg.getOption());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLMethodInvokeExpr) {
                        System.out.println("SQLMethodInvokeExpr:");
                        SQLMethodInvokeExpr method = (SQLMethodInvokeExpr) func;
                        System.out.println("MethodName:" + method.getMethodName() + ",getArguments size =" + method.getParameters().size() + ",OWNER:" + method.getOwner());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLCastExpr) {
                        SQLCastExpr cast = (SQLCastExpr) func;
                        System.out.println("SQLCastExpr:");
                        System.out.println("Expr:" + cast.getExpr().getClass() + ",getDataType:" + cast.getDataType());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLBinaryOpExpr) {
                        SQLBinaryOpExpr Op = (SQLBinaryOpExpr) func;
                        System.out.println("SQLBinaryOpExpr:");
                        System.out.println("left:" + Op.getLeft().getClass());
                        System.out.println("right:" + Op.getRight().getClass());
                        System.out.println("operator:" + Op.getOperator().getClass());
                        System.out.println("dbtype:" + Op.getDbType());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLUnaryExpr) {
                        SQLUnaryExpr Op = (SQLUnaryExpr) func;
                        System.out.println("SQLUnaryExpr:");
                        System.out.println("EXPR:" + Op.getExpr().getClass());
                        System.out.println("operator:" + Op.getOperator().getClass());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLBetweenExpr) {
                        SQLBetweenExpr between = (SQLBetweenExpr) func;
                        System.out.println("SQLBetweenExpr:");
                        System.out.println("begin EXPR:" + between.getBeginExpr());
                        System.out.println("end EXPR:" + between.getEndExpr());
                        System.out.println("isnot :" + between.isNot());
                        System.out.println("test :" + between.getTestExpr());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLInListExpr) {
                        SQLInListExpr in = (SQLInListExpr) func;
                        System.out.println("SQLInListExpr:");
                        System.out.println("EXPR:" + in.getExpr());
                        System.out.println("isnot :" + in.isNot());
                        System.out.println("getTargetList size :" + in.getTargetList().size());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLNotExpr) {
                        SQLNotExpr not = (SQLNotExpr) func;
                        System.out.println("SQLNotExpr:");
                        System.out.println("EXPR:" + not.getExpr());
                        System.out.println("---------------------------");
                    } else if (func instanceof MySqlExtractExpr) {
                        MySqlExtractExpr extract = (MySqlExtractExpr) func;
                        System.out.println("MySqlExtractExpr:");
                        System.out.println("value:" + extract.getValue());
                        System.out.println("unit:" + extract.getUnit());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLCaseExpr) {
                        SQLCaseExpr Case = (SQLCaseExpr) func;
                        System.out.println("SQLCaseExpr:");
                        System.out.println("value:" + Case.getValueExpr());
                        System.out.println("else:" + Case.getElseExpr());
                        System.out.println("items size:" + Case.getItems().size());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLVariantRefExpr) {
                        SQLVariantRefExpr variant = (SQLVariantRefExpr) func;
                        System.out.println("SQLVariantRefExpr:");
                        System.out.println("name:" + variant.getName());
                        System.out.println("Global:" + variant.isGlobal());
                        System.out.println("index:" + variant.getIndex());
                        System.out.println("---------------------------");
                    } else // SQLAllColumnExpr
                    {
                        // MySqlOutputVisitor
                        System.out.println("item.getExpr():   :" + item.getExpr().getClass().toString() + "\n");
                    }
                }
            }
            // SQLInSubQueryExpr
            if (selectQueryBlock.getGroupBy() != null) {
                SQLSelectGroupByClause groupBy = selectQueryBlock.getGroupBy();
                for (SQLExpr groupByItem : groupBy.getItems()) {
                    System.out.println("groupByItem:");
                    System.out.println("class :" + groupByItem.getClass());
                    System.out.println("---------------------------");
                }
                if (groupBy.getHaving() != null) {
                    SQLExpr having = groupBy.getHaving();
                    System.out.println("having:");
                    System.out.println("class :" + having.getClass());
                    System.out.println("---------------------------");
                }
            // with rollup...
            }
            if (selectQueryBlock.getOrderBy() != null) {
                for (SQLSelectOrderByItem orderItem : selectQueryBlock.getOrderBy().getItems()) {
                    System.out.println("OrderBy:");
                    System.out.println("class :" + orderItem.getExpr().getClass());
                    System.out.println("---------------------------");
                }
            }
            if (selectQueryBlock.getWhere() != null) {
                if (selectQueryBlock.getWhere() instanceof SQLBinaryOpExpr) {
                    SQLBinaryOpExpr where = (SQLBinaryOpExpr) (selectQueryBlock.getWhere());
                    System.out.println("where right:");
                    System.out.println("class :" + where.getRight().getClass());
                    System.out.println("---------------------------");
                } else {
                    System.out.println("where:");
                    System.out.println("class :" + selectQueryBlock.getWhere().getClass());
                    System.out.println("---------------------------");
                }
            }
        } else if (sqlSelectQuery instanceof MySqlUnionQuery) {
        }
    } else if (statement instanceof MySqlShowColumnsStatement) {
        MySqlShowColumnsStatement showColumnsStatement = (MySqlShowColumnsStatement) statement;
        showColumnsStatement.setDatabase(null);
        showColumnsStatement.toString();
        System.out.println("change to->" + showColumnsStatement.toString());
    } else if (statement instanceof MySqlShowIndexesStatement) {
        MySqlShowIndexesStatement mySqlShowIndexesStatement = (MySqlShowIndexesStatement) statement;
        mySqlShowIndexesStatement.setDatabase(null);
        mySqlShowIndexesStatement.toString();
        System.out.println("change to 1->" + mySqlShowIndexesStatement.toString());
        System.out.println("change to 2->" + SQLUtils.toMySqlString(mySqlShowIndexesStatement));
    } else if (statement instanceof MySqlShowKeysStatement) {
        MySqlShowKeysStatement mySqlShowKeysStatement = (MySqlShowKeysStatement) statement;
        mySqlShowKeysStatement.setDatabase(null);
        mySqlShowKeysStatement.toString();
        System.out.println("change to 1->" + mySqlShowKeysStatement.toString());
        System.out.println("change to 2->" + SQLUtils.toMySqlString(mySqlShowKeysStatement));
    } else if (statement instanceof SQLSetStatement) {
        SQLSetStatement setStatement = (SQLSetStatement) statement;
        for (SQLAssignItem assignItem : setStatement.getItems()) {
            System.out.println("value is " + assignItem.getValue() + ", class is " + assignItem.getValue().getClass());
            if (assignItem.getTarget() instanceof SQLVariantRefExpr) {
                SQLVariantRefExpr target = (SQLVariantRefExpr) assignItem.getTarget();
                System.out.println("target is " + target + ", global is " + target.isGlobal());
            } else if (assignItem.getTarget() instanceof SQLPropertyExpr) {
                SQLPropertyExpr target = (SQLPropertyExpr) assignItem.getTarget();
                System.out.println("target is " + target.getName() + ", Owner is " + target.getOwner());
            } else {
                System.out.println("target is " + assignItem.getTarget() + ", class is " + assignItem.getTarget().getClass());
            }
        }
    } else if (statement instanceof MySqlSetNamesStatement) {
        MySqlSetNamesStatement setStatement = (MySqlSetNamesStatement) statement;
        System.out.println("charset =" + setStatement.getCharSet() + ",Collate =" + setStatement.getCollate() + ",default =" + setStatement.isDefault());
    } else if (statement instanceof MySqlSetCharSetStatement) {
        MySqlSetCharSetStatement setStatement = (MySqlSetCharSetStatement) statement;
        System.out.println("charset =" + setStatement.getCharSet() + ",Collate =" + setStatement.getCollate() + ",default =" + setStatement.isDefault());
    } else if (statement instanceof MySqlSetTransactionStatement) {
        MySqlSetTransactionStatement setStatement = (MySqlSetTransactionStatement) statement;
        System.out.println("global" + setStatement.getGlobal() + ",IsolationLevel=" + setStatement.getIsolationLevel() + ",access mode" + setStatement.getAccessModel());
    } else {
        System.out.println("statement:" + statement + "," + statement.getClass().toString());
    }
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlExtractExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MySqlUnique(com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) MysqlForeignKey(com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey) MySqlPrimaryKey(com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey)

Aggregations

MySqlUnique (com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique)13 MySqlKey (com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey)7 MySqlPrimaryKey (com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey)6 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 MysqlForeignKey (com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey)4 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)3 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)3 SQLName (com.alibaba.druid.sql.ast.SQLName)2 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)2 SQLSelectOrderByItem (com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)2 SQLTableElement (com.alibaba.druid.sql.ast.statement.SQLTableElement)2 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)2 ParserException (com.alibaba.druid.sql.parser.ParserException)2 SQLDataType (com.alibaba.druid.sql.ast.SQLDataType)1 SQLDataTypeImpl (com.alibaba.druid.sql.ast.SQLDataTypeImpl)1 SQLNullExpr (com.alibaba.druid.sql.ast.expr.SQLNullExpr)1 SQLColumnConstraint (com.alibaba.druid.sql.ast.statement.SQLColumnConstraint)1 SQLColumnPrimaryKey (com.alibaba.druid.sql.ast.statement.SQLColumnPrimaryKey)1 SQLColumnUniqueKey (com.alibaba.druid.sql.ast.statement.SQLColumnUniqueKey)1 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)1