private void drop()

in openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaTool.java [898:1038]


    private void drop(SchemaGroup db, SchemaGroup repos, boolean considerDatabaseState)
        throws SQLException {
        // drop sequences
        Schema[] schemas = repos.getSchemas();
        if (_seqs) {
            Sequence[] seqs;
            Sequence dbSeq;
            for (Schema schema : schemas) {
                seqs = schema.getSequences();
                for (Sequence seq : seqs) {
                    if (!isDroppable(seq))
                        continue;
                    dbSeq = db.findSequence(seq);
                    if (dbSeq != null) {
                        if (dropSequence(seq))
                            dbSeq.getSchema().removeSequence(dbSeq);
                        else
                            _log.warn(_loc.get("drop-seq", seq));
                    }
                    else if (_writer != null) {
                        dropSequence(seq);
                    }
                }
            }
        }

        // calculate tables to drop; we can only drop tables if we're sure
        // the user listed the entire table definition in the stuff they want
        // dropped; else they might just want to drop a few columns
        Collection<Table> drops = new LinkedList<>();
        Table[] tabs;
        Table dbTable;
        Column[] dbCols;
        for (Schema value : schemas) {
            tabs = value.getTables();
            tables:
            for (Table tab : tabs) {
                if (!isDroppable(tab))
                    continue;

                if (!considerDatabaseState) {
                    drops.add(tab);
                    continue;
                }

                dbTable = db.findTable(tab);
                if (dbTable == null) {
                    if (_writer != null) {
                        drops.add(tab);
                    }
                    continue;
                }

                dbCols = dbTable.getColumns();
                for (Column dbCol : dbCols) {
                    if (!dbCol.getIdentifier().getName().equals(_dict.getIdentityColumnName()) &&
                            !tab.containsColumn(dbCol))
                        continue tables;
                }
                drops.add(tab);
            }
        }

        // order is important in this method; start with foreign keys mentioned
        // in the drop schema
        if (_fks) {
            ForeignKey[] fks;
            ForeignKey fk;
            for (Schema schema : schemas) {
                tabs = schema.getTables();
                for (Table tab : tabs) {
                    if (!isDroppable(tab))
                        continue;
                    fks = tab.getForeignKeys();
                    dbTable = db.findTable(tab);
                    for (ForeignKey foreignKey : fks) {
                        if (foreignKey.isLogical())
                            continue;

                        fk = null;
                        if (dbTable != null)
                            fk = findForeignKey(dbTable, foreignKey);
                        if (dbTable == null || fk == null)
                            continue;

                        if (dropForeignKey(foreignKey))
                            if (dbTable != null)
                                dbTable.removeForeignKey(fk);
                            else
                                _log.warn(_loc.get("drop-fk", foreignKey, tab));
                    }
                }
            }

            // also drop imported foreign keys for tables that will be dropped
            Table tab;
            for (Table drop : drops) {
                tab = drop;
                dbTable = db.findTable(tab);
                if (dbTable == null)
                    continue;

                fks = db.findExportedForeignKeys(dbTable.getPrimaryKey());
                for (ForeignKey foreignKey : fks) {
                    if (dropForeignKey(foreignKey))
                        dbTable.removeForeignKey(foreignKey);
                    else
                        _log.warn(_loc.get("drop-fk", foreignKey, dbTable));
                }
            }
        }

        // drop the tables we calculated above
        dropTables(drops, db);

        if (considerDatabaseState) {
            // columns
            Column[] cols;
            Column col;
            for (Schema schema : schemas) {
                tabs = schema.getTables();
                for (Table tab : tabs) {
                    if (!isDroppable(tab))
                        continue;
                    cols = tab.getColumns();
                    dbTable = db.findTable(tab);
                    for (Column column : cols) {
                        col = null;
                        if (dbTable != null)
                            col = dbTable.getColumn(column.getIdentifier());
                        if (dbTable == null || col == null)
                            continue;

                        if (dropColumn(column)) {
                            dbTable.removeColumn(col);
                        }
                    }
                }
            }
        }
    }