| 1 | |
package liquibase.database.structure; |
| 2 | |
|
| 3 | |
import java.util.List; |
| 4 | |
|
| 5 | 0 | public class ForeignKey implements DatabaseObject, Comparable<ForeignKey> { |
| 6 | |
private Table primaryKeyTable; |
| 7 | |
private String primaryKeyColumns; |
| 8 | |
|
| 9 | |
private Table foreignKeyTable; |
| 10 | |
private String foreignKeyColumns; |
| 11 | |
|
| 12 | |
private String name; |
| 13 | |
|
| 14 | |
private boolean deferrable; |
| 15 | |
private boolean initiallyDeferred; |
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | 0 | private boolean referencesUniqueColumn = false; |
| 20 | |
|
| 21 | |
private ForeignKeyConstraintType updateRule; |
| 22 | |
private ForeignKeyConstraintType deleteRule; |
| 23 | |
|
| 24 | |
public DatabaseObject[] getContainingObjects() { |
| 25 | 0 | return new DatabaseObject[] { |
| 26 | |
new liquibase.database.structure.Column().setName(getPrimaryKeyColumns()) |
| 27 | |
.setTable(getPrimaryKeyTable()), |
| 28 | |
new liquibase.database.structure.Column().setName(getForeignKeyColumns()) |
| 29 | |
.setTable(getForeignKeyTable()) |
| 30 | |
|
| 31 | |
}; |
| 32 | |
} |
| 33 | |
|
| 34 | |
public Table getPrimaryKeyTable() { |
| 35 | 0 | return primaryKeyTable; |
| 36 | |
} |
| 37 | |
|
| 38 | |
public void setPrimaryKeyTable(Table primaryKeyTable) { |
| 39 | 0 | this.primaryKeyTable = primaryKeyTable; |
| 40 | 0 | } |
| 41 | |
|
| 42 | |
public String getPrimaryKeyColumns() { |
| 43 | 0 | return primaryKeyColumns; |
| 44 | |
} |
| 45 | |
|
| 46 | |
public void addPrimaryKeyColumn(String primaryKeyColumn) { |
| 47 | 0 | if ((this.primaryKeyColumns == null) || (this.primaryKeyColumns.length() == 0)) { |
| 48 | 0 | this.primaryKeyColumns = primaryKeyColumn; |
| 49 | |
} else { |
| 50 | 0 | this.primaryKeyColumns = this.primaryKeyColumns + ", " + primaryKeyColumn; |
| 51 | |
} |
| 52 | 0 | } |
| 53 | |
|
| 54 | |
public void setPrimaryKeyColumns(String primaryKeyColumns) { |
| 55 | 0 | this.primaryKeyColumns = primaryKeyColumns; |
| 56 | 0 | } |
| 57 | |
|
| 58 | |
public Table getForeignKeyTable() { |
| 59 | 0 | return foreignKeyTable; |
| 60 | |
} |
| 61 | |
|
| 62 | |
public void setForeignKeyTable(Table foreignKeyTable) { |
| 63 | 0 | this.foreignKeyTable = foreignKeyTable; |
| 64 | 0 | } |
| 65 | |
|
| 66 | |
public String getForeignKeyColumns() { |
| 67 | 0 | return foreignKeyColumns; |
| 68 | |
} |
| 69 | |
|
| 70 | |
public void addForeignKeyColumn(String foreignKeyColumn) { |
| 71 | 0 | if ((this.foreignKeyColumns == null) || (this.foreignKeyColumns.length() == 0)) { |
| 72 | 0 | this.foreignKeyColumns = foreignKeyColumn; |
| 73 | |
} else { |
| 74 | 0 | this.foreignKeyColumns = this.foreignKeyColumns + ", " + foreignKeyColumn; |
| 75 | |
} |
| 76 | 0 | } |
| 77 | |
|
| 78 | |
public void setForeignKeyColumns(String foreignKeyColumns) { |
| 79 | 0 | this.foreignKeyColumns = foreignKeyColumns; |
| 80 | 0 | } |
| 81 | |
|
| 82 | |
public String getName() { |
| 83 | 0 | return name; |
| 84 | |
} |
| 85 | |
|
| 86 | |
public void setName(String name) { |
| 87 | 0 | this.name = name; |
| 88 | 0 | } |
| 89 | |
|
| 90 | |
@Override |
| 91 | |
public String toString() { |
| 92 | 0 | return getName() + "(" + getForeignKeyTable() + "." + getForeignKeyColumns() + " ->" + getPrimaryKeyTable() |
| 93 | |
+ "." + getPrimaryKeyColumns() + ")"; |
| 94 | |
} |
| 95 | |
|
| 96 | |
public boolean isDeferrable() { |
| 97 | 0 | return deferrable; |
| 98 | |
} |
| 99 | |
|
| 100 | |
public void setDeferrable(boolean deferrable) { |
| 101 | 0 | this.deferrable = deferrable; |
| 102 | 0 | } |
| 103 | |
|
| 104 | |
public boolean isInitiallyDeferred() { |
| 105 | 0 | return initiallyDeferred; |
| 106 | |
} |
| 107 | |
|
| 108 | |
public void setInitiallyDeferred(boolean initiallyDeferred) { |
| 109 | 0 | this.initiallyDeferred = initiallyDeferred; |
| 110 | 0 | } |
| 111 | |
|
| 112 | |
public void setUpdateRule(ForeignKeyConstraintType rule) { |
| 113 | 0 | this.updateRule = rule; |
| 114 | 0 | } |
| 115 | |
|
| 116 | |
public ForeignKeyConstraintType getUpdateRule() { |
| 117 | 0 | return this.updateRule; |
| 118 | |
} |
| 119 | |
|
| 120 | |
public void setDeleteRule(ForeignKeyConstraintType rule) { |
| 121 | 0 | this.deleteRule = rule; |
| 122 | 0 | } |
| 123 | |
|
| 124 | |
public ForeignKeyConstraintType getDeleteRule() { |
| 125 | 0 | return this.deleteRule; |
| 126 | |
} |
| 127 | |
|
| 128 | |
public boolean getReferencesUniqueColumn() { |
| 129 | 0 | return referencesUniqueColumn; |
| 130 | |
} |
| 131 | |
|
| 132 | |
public void setReferencesUniqueColumn(boolean referencesUniqueColumn) { |
| 133 | 0 | this.referencesUniqueColumn = referencesUniqueColumn; |
| 134 | 0 | } |
| 135 | |
|
| 136 | |
@Override |
| 137 | |
public boolean equals(Object o) { |
| 138 | 0 | if (this == o) |
| 139 | 0 | return true; |
| 140 | 0 | if (o == null || getClass() != o.getClass()) |
| 141 | 0 | return false; |
| 142 | |
|
| 143 | 0 | ForeignKey that = (ForeignKey) o; |
| 144 | |
|
| 145 | 0 | if (getForeignKeyColumns() == null) { |
| 146 | 0 | return this.getName().equalsIgnoreCase(that.getName()); |
| 147 | |
} |
| 148 | |
|
| 149 | 0 | return getForeignKeyColumns().equalsIgnoreCase(that.getForeignKeyColumns()) |
| 150 | |
&& foreignKeyTable.equals(that.foreignKeyTable) |
| 151 | |
&& getPrimaryKeyColumns().equalsIgnoreCase(that.getPrimaryKeyColumns()) |
| 152 | |
&& primaryKeyTable.equals(that.primaryKeyTable) |
| 153 | |
&& referencesUniqueColumn == that.getReferencesUniqueColumn(); |
| 154 | |
} |
| 155 | |
|
| 156 | |
@Override |
| 157 | |
public int hashCode() { |
| 158 | 0 | int result = 0; |
| 159 | 0 | if (primaryKeyTable != null) { |
| 160 | 0 | result = primaryKeyTable.hashCode(); |
| 161 | |
} |
| 162 | 0 | if (primaryKeyColumns != null) { |
| 163 | 0 | result = 31 * result + primaryKeyColumns.toUpperCase().hashCode(); |
| 164 | |
} |
| 165 | |
|
| 166 | 0 | if (foreignKeyTable != null) { |
| 167 | 0 | result = 31 * result + foreignKeyTable.hashCode(); |
| 168 | |
} |
| 169 | |
|
| 170 | 0 | if (foreignKeyColumns != null) { |
| 171 | 0 | result = 31 * result + foreignKeyColumns.toUpperCase().hashCode(); |
| 172 | |
} |
| 173 | |
|
| 174 | 0 | return result; |
| 175 | |
} |
| 176 | |
|
| 177 | |
public int compareTo(ForeignKey o) { |
| 178 | 0 | int returnValue = 0; |
| 179 | 0 | if (this.getForeignKeyTable() != null && o.getForeignKeyTable() != null) { |
| 180 | 0 | returnValue = this.getForeignKeyTable().compareTo(o.getForeignKeyTable()); |
| 181 | |
} |
| 182 | 0 | if (returnValue == 0 && this.getForeignKeyColumns() != null && o.getForeignKeyColumns() != null) { |
| 183 | 0 | returnValue = this.getForeignKeyColumns().compareToIgnoreCase(o.getForeignKeyColumns()); |
| 184 | |
} |
| 185 | 0 | if (returnValue == 0 && this.getName() != null && o.getName() != null) { |
| 186 | 0 | returnValue = this.getName().compareToIgnoreCase(o.getName()); |
| 187 | |
} |
| 188 | 0 | if (returnValue == 0 && this.getPrimaryKeyTable() != null && o.getPrimaryKeyTable() != null) { |
| 189 | 0 | returnValue = this.getPrimaryKeyTable().compareTo(o.getPrimaryKeyTable()); |
| 190 | |
} |
| 191 | |
|
| 192 | 0 | if (returnValue == 0 && this.getPrimaryKeyColumns() != null && o.getPrimaryKeyColumns() != null) { |
| 193 | 0 | returnValue = this.getPrimaryKeyColumns().compareToIgnoreCase(o.getPrimaryKeyColumns()); |
| 194 | |
} |
| 195 | 0 | if (returnValue == 0 && this.updateRule != null && o.getUpdateRule() != null) |
| 196 | 0 | returnValue = this.updateRule.compareTo(o.getUpdateRule()); |
| 197 | 0 | if (returnValue == 0 && this.deleteRule != null && o.getDeleteRule() != null) |
| 198 | 0 | returnValue = this.deleteRule.compareTo(o.getDeleteRule()); |
| 199 | 0 | return returnValue; |
| 200 | |
} |
| 201 | |
|
| 202 | |
private String toDisplayString(List<String> columnsNames) { |
| 203 | 0 | StringBuilder sb = new StringBuilder(); |
| 204 | 0 | int i = 0; |
| 205 | 0 | for (String columnName : columnsNames) { |
| 206 | 0 | i++; |
| 207 | 0 | sb.append(columnName); |
| 208 | 0 | if (i < columnsNames.size()) { |
| 209 | 0 | sb.append(", "); |
| 210 | |
} |
| 211 | |
} |
| 212 | 0 | return sb.toString(); |
| 213 | |
} |
| 214 | |
} |