Search in sources :

Example 1 with MysqlForeignKey

use of com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey 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 MysqlForeignKey

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

the class MySqlCreateTableParser method parseConstraint.

protected SQLTableConstraint parseConstraint() {
    SQLName name = null;
    boolean hasConstaint = false;
    if (lexer.token() == (Token.CONSTRAINT)) {
        hasConstaint = true;
        lexer.nextToken();
    }
    if (lexer.token() == Token.IDENTIFIER) {
        name = this.exprParser.name();
    }
    SQLTableConstraint constraint = null;
    if (lexer.token() == (Token.KEY)) {
        MySqlKey key = new MySqlKey();
        this.exprParser.parseIndex(key.getIndexDefinition());
        key.setHasConstraint(hasConstaint);
        if (name != null) {
            key.setName(name);
        }
        constraint = key;
    } else if (lexer.token() == Token.PRIMARY) {
        MySqlPrimaryKey pk = this.getExprParser().parsePrimaryKey();
        if (name != null) {
            pk.setName(name);
        }
        pk.setHasConstraint(hasConstaint);
        constraint = pk;
    } else if (lexer.token() == Token.UNIQUE) {
        MySqlUnique uk = this.getExprParser().parseUnique();
        // should not use CONSTRAINT [symbol] for index name if index_name already specified
        if (name != null && uk.getName() == null) {
            uk.setName(name);
        }
        uk.setHasConstraint(hasConstaint);
        constraint = uk;
    } else if (lexer.token() == Token.FOREIGN) {
        MysqlForeignKey fk = this.getExprParser().parseForeignKey();
        fk.setName(name);
        fk.setHasConstraint(hasConstaint);
        constraint = fk;
    } else if (lexer.token() == Token.CHECK) {
        lexer.nextToken();
        SQLCheck check = new SQLCheck();
        check.setName(name);
        SQLExpr expr = this.exprParser.primary();
        check.setExpr(expr);
        constraint = check;
        boolean enforce = true;
        if (Token.NOT.equals(lexer.token())) {
            enforce = false;
            lexer.nextToken();
        }
        if (lexer.stringVal().equalsIgnoreCase("ENFORCED")) {
            check.setEnforced(enforce);
            lexer.nextToken();
        }
        if (lexer.token() == Token.HINT) {
            String hintText = lexer.stringVal();
            if (hintText != null) {
                hintText = hintText.trim();
            }
            if (hintText.startsWith("!")) {
                if (hintText.endsWith("NOT ENFORCED")) {
                    check.setEnforced(false);
                } else if (hintText.endsWith(" ENFORCED")) {
                    check.setEnforced(true);
                }
                lexer.nextToken();
            }
        }
    }
    if (constraint != null) {
        if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
            SQLExpr comment = this.exprParser.primary();
            constraint.setComment(comment);
        }
        return constraint;
    }
    throw new ParserException("TODO. " + lexer.info());
}
Also used : MysqlForeignKey(com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey) MySqlUnique(com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique) MySqlKey(com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey) MySqlPrimaryKey(com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey)

Example 3 with MysqlForeignKey

use of com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey 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)

Example 4 with MysqlForeignKey

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

the class MySqlExprParser method parseForeignKey.

public MysqlForeignKey parseForeignKey() {
    accept(Token.FOREIGN);
    accept(Token.KEY);
    MysqlForeignKey fk = new MysqlForeignKey();
    if (lexer.token() != Token.LPAREN) {
        SQLName indexName = name();
        fk.setIndexName(indexName);
    }
    accept(Token.LPAREN);
    this.names(fk.getReferencingColumns(), fk);
    accept(Token.RPAREN);
    accept(Token.REFERENCES);
    fk.setReferencedTableName(this.name());
    accept(Token.LPAREN);
    this.names(fk.getReferencedColumns());
    accept(Token.RPAREN);
    if (lexer.identifierEquals(FnvHash.Constants.MATCH)) {
        lexer.nextToken();
        if (lexer.identifierEquals("FULL") || lexer.token() == Token.FULL) {
            fk.setReferenceMatch(Match.FULL);
            lexer.nextToken();
        } else if (lexer.identifierEquals(FnvHash.Constants.PARTIAL)) {
            fk.setReferenceMatch(Match.PARTIAL);
            lexer.nextToken();
        } else if (lexer.identifierEquals(FnvHash.Constants.SIMPLE)) {
            fk.setReferenceMatch(Match.SIMPLE);
            lexer.nextToken();
        } else {
            throw new ParserException("TODO : " + lexer.info());
        }
    }
    while (lexer.token() == Token.ON) {
        lexer.nextToken();
        if (lexer.token() == Token.DELETE) {
            lexer.nextToken();
            Option option = parseReferenceOption();
            fk.setOnDelete(option);
        } else if (lexer.token() == Token.UPDATE) {
            lexer.nextToken();
            Option option = parseReferenceOption();
            fk.setOnUpdate(option);
        } else {
            throw new ParserException("syntax error, expect DELETE or UPDATE, actual " + lexer.token() + " " + lexer.info());
        }
    }
    return fk;
}
Also used : MysqlForeignKey(com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey) Option(com.alibaba.druid.sql.ast.statement.SQLForeignKeyImpl.Option)

Aggregations

MysqlForeignKey (com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SQLName (com.alibaba.druid.sql.ast.SQLName)2 MySqlPrimaryKey (com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey)2 MySqlUnique (com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique)2 SQLDeclareItem (com.alibaba.druid.sql.ast.SQLDeclareItem)1 SQLPartition (com.alibaba.druid.sql.ast.SQLPartition)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)1 Option (com.alibaba.druid.sql.ast.statement.SQLForeignKeyImpl.Option)1 MySqlKey (com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey)1 MySqlExtractExpr (com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr)1 MySqlAlterTableAlterColumn (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableAlterColumn)1 MySqlAlterTableChangeColumn (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn)1 MySqlAlterTableCharacter (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableCharacter)1 MySqlAlterTableDiscardTablespace (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableDiscardTablespace)1 MySqlAlterTableImportTablespace (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableImportTablespace)1 MySqlAlterTableModifyColumn (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn)1 MySqlAlterTableOption (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableOption)1