use of org.jumpmind.db.model.ForeignKey in project symmetric-ds by JumpMind.
the class DbFill method buildDependentColumnValues.
protected void buildDependentColumnValues(List<Table> tables) {
for (Table table : tables) {
Map<String, List<ForeignKeyReference>> columnReferences = new HashMap<String, List<ForeignKeyReference>>();
for (ForeignKey fk : table.getForeignKeys()) {
for (Reference ref : fk.getReferences()) {
List<ForeignKeyReference> references = columnReferences.get(ref.getLocalColumnName());
if (references == null) {
references = new ArrayList<ForeignKeyReference>();
columnReferences.put(ref.getLocalColumnName(), references);
}
references.add(new ForeignKeyReference(fk, ref));
}
}
for (String columnName : columnReferences.keySet()) {
List<ForeignKeyReference> references = columnReferences.get(columnName);
if (references.size() > 1) {
List<Object> commonValue = new ArrayList<Object>();
StringBuilder sb = null;
for (ForeignKeyReference fkr : references) {
String key = fkr.getForeignKey().getForeignTableName() + "." + fkr.getReference().getForeignColumnName();
commonDependencyValues.put(key, commonValue);
commonDependencyTables.add(getDbTable(fkr.getForeignKey().getForeignTableName()));
if (verbose) {
sb = (sb == null) ? new StringBuilder() : sb.append(", ");
sb.append(key);
}
}
if (verbose) {
log.info("Common dependency for table {}: {}", table.getName(), sb.toString());
}
}
}
}
}
use of org.jumpmind.db.model.ForeignKey in project symmetric-ds by JumpMind.
the class DbFill method buildForeignKeyReferences.
protected void buildForeignKeyReferences(List<Table> tables) {
for (Table table : tables) {
for (ForeignKey fk : table.getForeignKeys()) {
for (Reference ref : fk.getReferences()) {
String key = table.getQualifiedTableName() + "." + ref.getLocalColumnName();
foreignKeyReferences.put(key, new ForeignKeyReference(fk, ref));
}
}
}
}
use of org.jumpmind.db.model.ForeignKey in project symmetric-ds by JumpMind.
the class ModelComparator method compareTables.
/**
* Compares the two tables and returns the changes necessary to create the
* second table from the first one.
*
* @param sourceModel
* The source model which contains the source table
* @param sourceTable
* The source table
* @param targetModel
* The target model which contains the target table
* @param targetTable
* The target table
* @return The changes
*/
public List<IModelChange> compareTables(Database sourceModel, Table sourceTable, Database targetModel, Table targetTable) {
ArrayList<IModelChange> changes = new ArrayList<IModelChange>();
if (platformInfo.isForeignKeysSupported()) {
for (int fkIdx = 0; fkIdx < sourceTable.getForeignKeyCount(); fkIdx++) {
ForeignKey sourceFk = sourceTable.getForeignKey(fkIdx);
ForeignKey targetFk = findCorrespondingForeignKey(targetTable, sourceFk);
if (targetFk == null) {
if (log.isDebugEnabled()) {
log.debug(sourceFk + " needs to be removed from table " + sourceTable.getName());
}
changes.add(new RemoveForeignKeyChange(sourceTable, sourceFk));
}
}
for (int fkIdx = 0; fkIdx < targetTable.getForeignKeyCount(); fkIdx++) {
ForeignKey targetFk = targetTable.getForeignKey(fkIdx);
ForeignKey sourceFk = findCorrespondingForeignKey(sourceTable, targetFk);
if (sourceFk == null) {
if (log.isDebugEnabled()) {
log.debug(targetFk + " needs to be created for table " + sourceTable.getName());
}
/*
* we have to use the target table here because the foreign
* key might reference a new column
*/
changes.add(new AddForeignKeyChange(targetTable, targetFk));
}
}
}
if (platformInfo.isIndicesSupported()) {
for (int indexIdx = 0; indexIdx < sourceTable.getIndexCount(); indexIdx++) {
IIndex sourceIndex = sourceTable.getIndex(indexIdx);
IIndex targetIndex = findCorrespondingIndex(targetTable, sourceIndex);
if (targetIndex == null) {
if (log.isDebugEnabled()) {
log.debug("Index " + sourceIndex.getName() + " needs to be removed from table " + sourceTable.getName());
}
changes.add(new RemoveIndexChange(sourceTable, sourceIndex));
}
}
for (int indexIdx = 0; indexIdx < targetTable.getIndexCount(); indexIdx++) {
IIndex targetIndex = targetTable.getIndex(indexIdx);
IIndex sourceIndex = findCorrespondingIndex(sourceTable, targetIndex);
if (sourceIndex == null) {
if (log.isDebugEnabled()) {
log.debug("Index " + targetIndex.getName() + " needs to be created for table " + sourceTable.getName());
}
// we have to use the target table here because the index might
// reference a new column
changes.add(new AddIndexChange(targetTable, targetIndex));
}
}
}
HashMap<Column, TableChange> addColumnChanges = new HashMap<Column, TableChange>();
for (int columnIdx = 0; columnIdx < targetTable.getColumnCount(); columnIdx++) {
Column targetColumn = targetTable.getColumn(columnIdx);
Column sourceColumn = sourceTable.findColumn(targetColumn.getName(), caseSensitive);
if (sourceColumn == null) {
log.debug("Column {} needs to be created for table {}", new Object[] { targetColumn.getName(), sourceTable.getName() });
AddColumnChange change = new AddColumnChange(sourceTable, targetColumn, columnIdx > 0 ? targetTable.getColumn(columnIdx - 1) : null, columnIdx < targetTable.getColumnCount() - 1 ? targetTable.getColumn(columnIdx + 1) : null);
changes.add(change);
addColumnChanges.put(targetColumn, change);
} else {
changes.addAll(compareColumns(sourceTable, sourceColumn, targetTable, targetColumn));
}
}
// at the changes
for (int columnIdx = targetTable.getColumnCount() - 1; columnIdx >= 0; columnIdx--) {
Column targetColumn = targetTable.getColumn(columnIdx);
AddColumnChange change = (AddColumnChange) addColumnChanges.get(targetColumn);
if (change == null) {
// that were added
break;
} else {
change.setAtEnd(true);
}
}
Column[] sourcePK = sourceTable.getPrimaryKeyColumns();
Column[] targetPK = targetTable.getPrimaryKeyColumns();
if ((sourcePK.length == 0) && (targetPK.length > 0)) {
if (log.isDebugEnabled()) {
log.debug("A primary key needs to be added to the table " + sourceTable.getName());
}
// we have to use the target table here because the primary key
// might
// reference a new column
changes.add(new AddPrimaryKeyChange(targetTable, targetPK));
} else if ((targetPK.length == 0) && (sourcePK.length > 0)) {
if (log.isDebugEnabled()) {
log.debug("The primary key needs to be removed from the table " + sourceTable.getName());
}
changes.add(new RemovePrimaryKeyChange(sourceTable, sourcePK));
} else if ((sourcePK.length > 0) && (targetPK.length > 0)) {
boolean changePK = false;
if (sourcePK.length != targetPK.length) {
changePK = true;
} else {
for (int pkColumnIdx = 0; (pkColumnIdx < sourcePK.length) && !changePK; pkColumnIdx++) {
if ((caseSensitive && !sourcePK[pkColumnIdx].getName().equals(targetPK[pkColumnIdx].getName())) || (!caseSensitive && !sourcePK[pkColumnIdx].getName().equalsIgnoreCase(targetPK[pkColumnIdx].getName()))) {
changePK = true;
}
}
}
if (changePK) {
if (log.isDebugEnabled()) {
log.debug("The primary key of table " + sourceTable.getName() + " needs to be changed");
}
changes.add(new PrimaryKeyChange(sourceTable, sourcePK, targetPK));
}
}
for (int columnIdx = 0; columnIdx < sourceTable.getColumnCount(); columnIdx++) {
Column sourceColumn = sourceTable.getColumn(columnIdx);
Column targetColumn = targetTable.findColumn(sourceColumn.getName(), caseSensitive);
if (targetColumn == null) {
if (log.isDebugEnabled()) {
log.debug("Column " + sourceColumn.getName() + " needs to be removed from table " + sourceTable.getName());
}
changes.add(new RemoveColumnChange(sourceTable, sourceColumn));
}
}
return changes;
}
use of org.jumpmind.db.model.ForeignKey in project symmetric-ds by JumpMind.
the class DatabaseXmlUtil method write.
public static void write(Table table, Writer output) {
try {
output.write("\t<table name=\"" + StringEscapeUtils.escapeXml(table.getName()) + "\">\n");
for (Column column : table.getColumns()) {
output.write("\t\t<column name=\"" + StringEscapeUtils.escapeXml(column.getName()) + "\"");
if (column.isPrimaryKey()) {
output.write(" primaryKey=\"" + column.isPrimaryKey() + "\"");
}
if (column.isRequired()) {
output.write(" required=\"" + column.isRequired() + "\"");
}
if (column.getMappedType() != null) {
output.write(" type=\"" + column.getMappedType() + "\"");
}
if (column.getSize() != null) {
output.write(" size=\"" + column.getSize() + "\"");
}
if (column.getDefaultValue() != null) {
output.write(" default=\"" + StringEscapeUtils.escapeXml(column.getDefaultValue()) + "\"");
}
if (column.isAutoIncrement()) {
output.write(" autoIncrement=\"" + column.isAutoIncrement() + "\"");
}
if (column.getJavaName() != null) {
output.write(" javaName=\"" + column.getJavaName() + "\"");
}
if (column.isUnique()) {
output.write(" unique=\"" + column.isUnique() + "\"");
}
if (column.getPlatformColumns() != null && column.getPlatformColumns().size() > 0) {
Collection<PlatformColumn> platformColumns = column.getPlatformColumns().values();
output.write(">\n");
for (PlatformColumn platformColumn : platformColumns) {
output.write("\t\t\t<platform-column name=\"" + platformColumn.getName() + "\"");
output.write(" type=\"" + platformColumn.getType() + "\"");
if (platformColumn.getSize() > 0) {
output.write(" size=\"" + platformColumn.getSize() + "\"");
}
if (platformColumn.getDecimalDigits() > 0) {
output.write(" decimalDigits=\"" + platformColumn.getDecimalDigits() + "\"");
}
if (platformColumn.getDefaultValue() != null) {
output.write(" default=\"" + StringEscapeUtils.escapeXml(platformColumn.getDefaultValue()) + "\"");
}
output.write("/>\n");
}
output.write("\t\t</column>\n");
} else {
output.write("/>\n");
}
}
for (ForeignKey fk : table.getForeignKeys()) {
output.write("\t\t<foreign-key name=\"" + StringEscapeUtils.escapeXml(fk.getName()) + "\" foreignTable=\"" + StringEscapeUtils.escapeXml(fk.getForeignTableName()) + "\">\n");
for (Reference ref : fk.getReferences()) {
output.write("\t\t\t<reference local=\"" + StringEscapeUtils.escapeXml(ref.getLocalColumnName()) + "\" foreign=\"" + StringEscapeUtils.escapeXml(ref.getForeignColumnName()) + "\"/>\n");
}
output.write("\t\t</foreign-key>\n");
}
for (IIndex index : table.getIndices()) {
if (index.isUnique()) {
output.write("\t\t<unique name=\"" + StringEscapeUtils.escapeXml(index.getName()) + "\">\n");
for (IndexColumn column : index.getColumns()) {
output.write("\t\t\t<unique-column name=\"" + StringEscapeUtils.escapeXml(column.getName()) + "\"/>\n");
}
output.write("\t\t</unique>\n");
} else {
output.write("\t\t<index name=\"" + StringEscapeUtils.escapeXml(index.getName()) + "\">\n");
for (IndexColumn column : index.getColumns()) {
output.write("\t\t\t<index-column name=\"" + StringEscapeUtils.escapeXml(column.getName()) + "\"");
if (column.getSize() != null) {
output.write(" size=\"" + column.getSize() + "\"");
}
output.write("/>\n");
}
output.write("\t\t</index>\n");
}
}
output.write("\t</table>\n");
} catch (IOException e) {
throw new IoException(e);
}
}
use of org.jumpmind.db.model.ForeignKey in project symmetric-ds by JumpMind.
the class AbstractDatabasePlatform method prefixForeignKeys.
protected void prefixForeignKeys(Table table, String tablePrefix, boolean storesUpperCaseIdentifiers) throws CloneNotSupportedException {
ForeignKey[] keys = table.getForeignKeys();
for (ForeignKey key : keys) {
String prefixedName = tablePrefix + key.getForeignTableName();
prefixedName = storesUpperCaseIdentifiers ? prefixedName.toUpperCase() : prefixedName.toLowerCase();
key.setForeignTableName(prefixedName);
String keyName = tablePrefix + key.getName();
keyName = storesUpperCaseIdentifiers ? keyName.toUpperCase() : keyName.toLowerCase();
key.setName(keyName);
Reference[] refs = key.getReferences();
for (Reference reference : refs) {
reference.setForeignColumnName(storesUpperCaseIdentifiers ? reference.getForeignColumnName().toUpperCase() : reference.getForeignColumnName().toLowerCase());
reference.setLocalColumnName(storesUpperCaseIdentifiers ? reference.getLocalColumnName().toUpperCase() : reference.getLocalColumnName().toLowerCase());
}
}
}
Aggregations