001: package liquibase.database.structure;
002:
003: import java.util.List;
004:
005: public class ForeignKey implements DatabaseObject,
006: Comparable<ForeignKey> {
007: private Table primaryKeyTable;
008: private String primaryKeyColumns;
009:
010: private Table foreignKeyTable;
011: private String foreignKeyColumns;
012:
013: private String name;
014:
015: private boolean deferrable;
016: private boolean initiallyDeferred;
017:
018: public Table getPrimaryKeyTable() {
019: return primaryKeyTable;
020: }
021:
022: public void setPrimaryKeyTable(Table primaryKeyTable) {
023: this .primaryKeyTable = primaryKeyTable;
024: }
025:
026: public String getPrimaryKeyColumns() {
027: return primaryKeyColumns;
028: }
029:
030: public void addPrimaryKeyColumn(String primaryKeyColumn) {
031: if ((this .primaryKeyColumns == null)
032: || (this .primaryKeyColumns.length() == 0)) {
033: this .primaryKeyColumns = primaryKeyColumn;
034: } else {
035: this .primaryKeyColumns = this .primaryKeyColumns + ", "
036: + primaryKeyColumn;
037: }
038: }
039:
040: public void setPrimaryKeyColumns(String primaryKeyColumns) {
041: this .primaryKeyColumns = primaryKeyColumns;
042: }
043:
044: public Table getForeignKeyTable() {
045: return foreignKeyTable;
046: }
047:
048: public void setForeignKeyTable(Table foreignKeyTable) {
049: this .foreignKeyTable = foreignKeyTable;
050: }
051:
052: public String getForeignKeyColumns() {
053: return foreignKeyColumns;
054: }
055:
056: public void addForeignKeyColumn(String foreignKeyColumn) {
057: if ((this .foreignKeyColumns == null)
058: || (this .foreignKeyColumns.length() == 0)) {
059: this .foreignKeyColumns = foreignKeyColumn;
060: } else {
061: this .foreignKeyColumns = this .foreignKeyColumns + ", "
062: + foreignKeyColumn;
063: }
064: }
065:
066: public void setForeignKeyColumns(String foreignKeyColumns) {
067: this .foreignKeyColumns = foreignKeyColumns;
068: }
069:
070: public String getName() {
071: return name;
072: }
073:
074: public void setName(String name) {
075: this .name = name;
076: }
077:
078: public String toString() {
079: return getName() + "(" + getForeignKeyTable() + "."
080: + getForeignKeyColumns() + " ->" + getPrimaryKeyTable()
081: + "." + getPrimaryKeyColumns() + ")";
082: }
083:
084: public boolean isDeferrable() {
085: return deferrable;
086: }
087:
088: public void setDeferrable(boolean deferrable) {
089: this .deferrable = deferrable;
090: }
091:
092: public boolean isInitiallyDeferred() {
093: return initiallyDeferred;
094: }
095:
096: public void setInitiallyDeferred(boolean initiallyDeferred) {
097: this .initiallyDeferred = initiallyDeferred;
098: }
099:
100: public boolean equals(Object o) {
101: if (this == o)
102: return true;
103: if (o == null || getClass() != o.getClass())
104: return false;
105:
106: ForeignKey that = (ForeignKey) o;
107:
108: return getForeignKeyColumns().equalsIgnoreCase(
109: that.getForeignKeyColumns())
110: && foreignKeyTable.equals(that.foreignKeyTable)
111: && getPrimaryKeyColumns().equalsIgnoreCase(
112: that.getPrimaryKeyColumns())
113: && primaryKeyTable.equals(that.primaryKeyTable);
114:
115: }
116:
117: public int hashCode() {
118: int result = 0;
119: if (primaryKeyTable != null) {
120: result = primaryKeyTable.hashCode();
121: }
122:
123: if (primaryKeyColumns != null) {
124: result = 31 * result
125: + primaryKeyColumns.toUpperCase().hashCode();
126: }
127:
128: if (foreignKeyTable != null) {
129: result = 31 * result + foreignKeyTable.hashCode();
130: }
131:
132: if (foreignKeyColumns != null) {
133: result = 31 * result
134: + foreignKeyColumns.toUpperCase().hashCode();
135: }
136:
137: return result;
138: }
139:
140: public int compareTo(ForeignKey o) {
141: int returnValue = 0;
142: if (this .getForeignKeyTable() != null
143: && o.getForeignKeyTable() != null) {
144: returnValue = this .getForeignKeyTable().compareTo(
145: o.getForeignKeyTable());
146: }
147:
148: if (returnValue == 0 && this .getForeignKeyColumns() != null
149: && o.getForeignKeyColumns() != null) {
150: returnValue = this .getForeignKeyColumns().compareTo(
151: o.getForeignKeyColumns());
152: }
153:
154: if (returnValue == 0 && this .getPrimaryKeyTable() != null
155: && o.getPrimaryKeyTable() != null) {
156: returnValue = this .getPrimaryKeyTable().compareTo(
157: o.getPrimaryKeyTable());
158: }
159:
160: if (returnValue == 0 && this .getPrimaryKeyColumns() != null
161: && o.getPrimaryKeyColumns() != null) {
162: returnValue = this .getPrimaryKeyColumns().compareTo(
163: o.getPrimaryKeyColumns());
164: }
165:
166: return returnValue;
167: }
168:
169: private String toDisplayString(List<String> columnsNames) {
170: StringBuilder sb = new StringBuilder();
171: int i = 0;
172: for (String columnName : columnsNames) {
173: i++;
174: sb.append(columnName);
175: if (i < columnsNames.size()) {
176: sb.append(", ");
177: }
178: }
179: return sb.toString();
180: }
181: }
|