Search in sources :

Example 16 with ForeignKey

use of org.jumpmind.db.model.ForeignKey in project symmetric-ds by JumpMind.

the class AbstractDatabasePlatform method alterCaseToMatchDatabaseDefaultCase.

public void alterCaseToMatchDatabaseDefaultCase(Table table) {
    table.setName(alterCaseToMatchDatabaseDefaultCase(table.getName()));
    Column[] columns = table.getColumns();
    for (Column column : columns) {
        column.setName(alterCaseToMatchDatabaseDefaultCase(column.getName()));
    }
    IIndex[] indexes = table.getIndices();
    for (IIndex index : indexes) {
        index.setName(alterCaseToMatchDatabaseDefaultCase(index.getName()));
        IndexColumn[] indexColumns = index.getColumns();
        for (IndexColumn indexColumn : indexColumns) {
            indexColumn.setName(alterCaseToMatchDatabaseDefaultCase(indexColumn.getName()));
        }
    }
    ForeignKey[] fks = table.getForeignKeys();
    for (ForeignKey foreignKey : fks) {
        foreignKey.setName(alterCaseToMatchDatabaseDefaultCase(foreignKey.getName()));
        foreignKey.setForeignTableName(alterCaseToMatchDatabaseDefaultCase(foreignKey.getForeignTableName()));
        Reference[] references = foreignKey.getReferences();
        for (Reference reference : references) {
            reference.setForeignColumnName(alterCaseToMatchDatabaseDefaultCase(reference.getForeignColumnName()));
            reference.setLocalColumnName(alterCaseToMatchDatabaseDefaultCase(reference.getLocalColumnName()));
        }
    }
}
Also used : IIndex(org.jumpmind.db.model.IIndex) Column(org.jumpmind.db.model.Column) IndexColumn(org.jumpmind.db.model.IndexColumn) Reference(org.jumpmind.db.model.Reference) ForeignKey(org.jumpmind.db.model.ForeignKey) IndexColumn(org.jumpmind.db.model.IndexColumn)

Example 17 with ForeignKey

use of org.jumpmind.db.model.ForeignKey in project symmetric-ds by JumpMind.

the class AbstractDdlBuilder method findCorrespondingForeignKey.

/**
     * Searches in the given table for a corresponding foreign key. If the given
     * key has no name, then a foreign key to the same table with the same
     * columns in the same order is searched. If the given key has a name, then
     * the a corresponding key also needs to have the same name, or no name at
     * all, but not a different one.
     *
     * @param table
     *            The table to search in
     * @param fk
     *            The original foreign key
     * @return The corresponding foreign key if found
     */
protected ForeignKey findCorrespondingForeignKey(Table table, ForeignKey fk) {
    boolean caseMatters = delimitedIdentifierModeOn;
    boolean checkFkName = (fk.getName() != null) && (fk.getName().length() > 0);
    Reference[] refs = fk.getReferences();
    ArrayList<Reference> curRefs = new ArrayList<Reference>();
    for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++) {
        ForeignKey curFk = table.getForeignKey(fkIdx);
        boolean checkCurFkName = checkFkName && (curFk.getName() != null) && (curFk.getName().length() > 0);
        if ((!checkCurFkName || areEqual(fk.getName(), curFk.getName(), caseMatters)) && areEqual(fk.getForeignTableName(), curFk.getForeignTableName(), caseMatters)) {
            curRefs.clear();
            CollectionUtils.addAll(curRefs, curFk.getReferences());
            // the order is not fixed, so we have to take this long way
            if (curRefs.size() == refs.length) {
                for (int refIdx = 0; refIdx < refs.length; refIdx++) {
                    boolean found = false;
                    for (int curRefIdx = 0; !found && (curRefIdx < curRefs.size()); curRefIdx++) {
                        Reference curRef = curRefs.get(curRefIdx);
                        if ((caseMatters && refs[refIdx].equals(curRef)) || (!caseMatters && refs[refIdx].equalsIgnoreCase(curRef))) {
                            curRefs.remove(curRefIdx);
                            found = true;
                        }
                    }
                }
                if (curRefs.isEmpty()) {
                    return curFk;
                }
            }
        }
    }
    return null;
}
Also used : Reference(org.jumpmind.db.model.Reference) ArrayList(java.util.ArrayList) ForeignKey(org.jumpmind.db.model.ForeignKey)

Example 18 with ForeignKey

use of org.jumpmind.db.model.ForeignKey in project symmetric-ds by JumpMind.

the class DatabaseXmlAsciiDocBuilder method main.

public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        System.err.println("Usage: <input_xml_file> <output_asciidoc_file>");
        System.exit(-1);
    }
    Database db = DatabaseXmlUtil.read(new File(args[0]));
    PrintWriter out = new PrintWriter(new FileWriter(args[1]));
    Table[] tables = db.getTables();
    for (Table table : tables) {
        out.println("=== " + table.getName().toUpperCase());
        out.println();
        if (isNotBlank(table.getDescription())) {
            out.println(table.getDescription());
        }
        out.println();
        out.print(".");
        out.println(table.getName().toUpperCase());
        out.println("[cols=\"3,^1,^1,^1,^1,^1,5\"]");
        out.println("|===");
        out.println();
        out.println("|Name|Type|Size|Default|Keys|Not Null|Description");
        for (Column column : table.getColumns()) {
            out.print("|");
            out.print(column.getName().toUpperCase());
            out.print("|");
            out.print(column.getMappedType());
            out.print("|");
            out.print(isNotBlank(column.getSize()) ? column.getSize() : " ");
            out.print("|");
            out.print(isNotBlank(column.getDefaultValue()) ? column.getDefaultValue() : " ");
            out.print("|");
            if (column.isPrimaryKey()) {
                out.print("PK ");
            }
            ForeignKey[] keys = table.getForeignKeys();
            boolean fk = false;
            for (ForeignKey foreignKey : keys) {
                Reference[] references = foreignKey.getReferences();
                for (Reference reference : references) {
                    if (reference.getLocalColumn().getName().equals(column.getName()) && !fk) {
                        out.print("FK");
                        fk = true;
                    }
                }
            }
            out.print("|");
            if (column.isRequired()) {
                out.print("X");
            }
            out.print("|");
            out.println(column.getDescription());
        }
        out.println("|===");
    }
    out.close();
}
Also used : Table(org.jumpmind.db.model.Table) Column(org.jumpmind.db.model.Column) Reference(org.jumpmind.db.model.Reference) FileWriter(java.io.FileWriter) Database(org.jumpmind.db.model.Database) ForeignKey(org.jumpmind.db.model.ForeignKey) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 19 with ForeignKey

use of org.jumpmind.db.model.ForeignKey in project symmetric-ds by JumpMind.

the class AbstractDdlBuilder method writeEmbeddedForeignKeysStmt.

/**
     * Writes the foreign key constraints inside a create table () clause.
     */
protected void writeEmbeddedForeignKeysStmt(Table table, StringBuilder ddl) {
    for (int idx = 0; idx < table.getForeignKeyCount(); idx++) {
        ForeignKey key = table.getForeignKey(idx);
        if (key.getForeignTableName() == null) {
            log.warn("Foreign key table is null for key " + key);
        } else {
            printStartOfEmbeddedStatement(ddl);
            if (databaseInfo.isEmbeddedForeignKeysNamed()) {
                ddl.append("CONSTRAINT ");
                printIdentifier(getForeignKeyName(table, key), ddl);
                ddl.append(" ");
            }
            ddl.append("FOREIGN KEY (");
            writeLocalReferences(key, ddl);
            ddl.append(") REFERENCES ");
            if (StringUtils.isNotBlank(table.getCatalog())) {
                ddl.append(getDelimitedIdentifier(table.getCatalog())).append(".");
            }
            if (StringUtils.isNotBlank(table.getSchema())) {
                ddl.append(getDelimitedIdentifier(table.getSchema())).append(".");
            }
            printIdentifier(getTableName(key.getForeignTableName()), ddl);
            ddl.append(" (");
            writeForeignReferences(key, ddl);
            ddl.append(")");
        }
    }
}
Also used : ForeignKey(org.jumpmind.db.model.ForeignKey)

Example 20 with ForeignKey

use of org.jumpmind.db.model.ForeignKey in project symmetric-ds by JumpMind.

the class MsSql2000DdlBuilder method processChanges.

@Override
protected void processChanges(Database currentModel, Database desiredModel, List<IModelChange> changes, StringBuilder ddl) {
    if (!changes.isEmpty()) {
        writeQuotationOnStatement(ddl);
    }
    /*
         * For column data type and size changes, we need to drop and then
         * re-create indexes and foreign keys using the column, as well as any
         * primary keys containg these columns However, if the index/foreign
         * key/primary key is already slated for removal or change, then we
         * don't want to generate change duplication
         */
    HashSet<IIndex> removedIndexes = new HashSet<IIndex>();
    HashSet<ForeignKey> removedForeignKeys = new HashSet<ForeignKey>();
    HashSet<Table> removedPKs = new HashSet<Table>();
    for (Iterator<IModelChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
        IModelChange change = changeIt.next();
        if (change instanceof RemoveIndexChange) {
            removedIndexes.add(((RemoveIndexChange) change).getIndex());
        } else if (change instanceof RemoveForeignKeyChange) {
            removedForeignKeys.add(((RemoveForeignKeyChange) change).getForeignKey());
        } else if (change instanceof RemovePrimaryKeyChange) {
            removedPKs.add(((RemovePrimaryKeyChange) change).getChangedTable());
        }
    }
    ArrayList<TableChange> additionalChanges = new ArrayList<TableChange>();
    for (Iterator<IModelChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
        IModelChange change = changeIt.next();
        if ((change instanceof ColumnDataTypeChange) || (change instanceof ColumnSizeChange)) {
            Column column = ((ColumnChange) change).getChangedColumn();
            Table table = ((ColumnChange) change).getChangedTable();
            if (column.isPrimaryKey() && !removedPKs.contains(table)) {
                Column[] pk = table.getPrimaryKeyColumns();
                additionalChanges.add(new RemovePrimaryKeyChange(table, pk));
                additionalChanges.add(new AddPrimaryKeyChange(table, pk));
                removedPKs.add(table);
            }
            for (int idx = 0; idx < table.getIndexCount(); idx++) {
                IIndex index = table.getIndex(idx);
                if (index.hasColumn(column) && !removedIndexes.contains(index)) {
                    additionalChanges.add(new RemoveIndexChange(table, index));
                    additionalChanges.add(new AddIndexChange(table, index));
                    removedIndexes.add(index);
                }
            }
            for (int tableIdx = 0; tableIdx < currentModel.getTableCount(); tableIdx++) {
                Table curTable = currentModel.getTable(tableIdx);
                for (int fkIdx = 0; fkIdx < curTable.getForeignKeyCount(); fkIdx++) {
                    ForeignKey curFk = curTable.getForeignKey(fkIdx);
                    if ((curFk.hasLocalColumn(column) || curFk.hasForeignColumn(column)) && !removedForeignKeys.contains(curFk)) {
                        additionalChanges.add(new RemoveForeignKeyChange(curTable, curFk));
                        additionalChanges.add(new AddForeignKeyChange(curTable, curFk));
                        removedForeignKeys.add(curFk);
                    }
                }
            }
        }
    }
    changes.addAll(additionalChanges);
    super.processChanges(currentModel, desiredModel, changes, ddl);
}
Also used : IIndex(org.jumpmind.db.model.IIndex) ColumnDataTypeChange(org.jumpmind.db.alter.ColumnDataTypeChange) Table(org.jumpmind.db.model.Table) AddIndexChange(org.jumpmind.db.alter.AddIndexChange) RemovePrimaryKeyChange(org.jumpmind.db.alter.RemovePrimaryKeyChange) ArrayList(java.util.ArrayList) IModelChange(org.jumpmind.db.alter.IModelChange) AddForeignKeyChange(org.jumpmind.db.alter.AddForeignKeyChange) RemoveForeignKeyChange(org.jumpmind.db.alter.RemoveForeignKeyChange) ColumnSizeChange(org.jumpmind.db.alter.ColumnSizeChange) ForeignKey(org.jumpmind.db.model.ForeignKey) Column(org.jumpmind.db.model.Column) ColumnChange(org.jumpmind.db.alter.ColumnChange) AddColumnChange(org.jumpmind.db.alter.AddColumnChange) RemoveColumnChange(org.jumpmind.db.alter.RemoveColumnChange) RemoveIndexChange(org.jumpmind.db.alter.RemoveIndexChange) AddPrimaryKeyChange(org.jumpmind.db.alter.AddPrimaryKeyChange) HashSet(java.util.HashSet) TableChange(org.jumpmind.db.alter.TableChange)

Aggregations

ForeignKey (org.jumpmind.db.model.ForeignKey)24 Reference (org.jumpmind.db.model.Reference)13 Table (org.jumpmind.db.model.Table)12 ArrayList (java.util.ArrayList)9 Column (org.jumpmind.db.model.Column)8 IIndex (org.jumpmind.db.model.IIndex)6 ResultSet (java.sql.ResultSet)4 HashMap (java.util.HashMap)4 IndexColumn (org.jumpmind.db.model.IndexColumn)4 LinkedHashMap (java.util.LinkedHashMap)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 List (java.util.List)2 TableChange (org.jumpmind.db.alter.TableChange)2 Database (org.jumpmind.db.model.Database)2 PlatformColumn (org.jumpmind.db.model.PlatformColumn)2 Row (org.jumpmind.db.sql.Row)2 IoException (org.jumpmind.exception.IoException)2 File (java.io.File)1 FileWriter (java.io.FileWriter)1