1 | |
package liquibase.database.structure; |
2 | |
|
3 | |
import liquibase.util.StringUtils; |
4 | |
|
5 | |
import java.util.ArrayList; |
6 | |
import java.util.List; |
7 | |
|
8 | 0 | public class UniqueConstraint implements DatabaseObject, Comparable<UniqueConstraint> { |
9 | |
private String name; |
10 | |
private Table table; |
11 | 0 | private List<String> columns = new ArrayList<String>(); |
12 | |
|
13 | |
private boolean deferrable; |
14 | |
private boolean initiallyDeferred; |
15 | |
private boolean disabled; |
16 | |
|
17 | |
private String tablespace; |
18 | |
|
19 | |
public DatabaseObject[] getContainingObjects() { |
20 | 0 | List<DatabaseObject> columns = new ArrayList<DatabaseObject>(); |
21 | 0 | for (String column : this.columns) { |
22 | 0 | columns.add(new Column().setName(column).setTable(table)); |
23 | |
} |
24 | |
|
25 | 0 | return columns.toArray(new DatabaseObject[columns.size()]); |
26 | |
} |
27 | |
|
28 | |
public String getName() { |
29 | 0 | return name; |
30 | |
} |
31 | |
|
32 | |
public void setName(String constraintName) { |
33 | 0 | this.name = constraintName; |
34 | 0 | } |
35 | |
|
36 | |
public Table getTable() { |
37 | 0 | return table; |
38 | |
} |
39 | |
|
40 | |
public void setTable(Table table) { |
41 | 0 | this.table = table; |
42 | 0 | } |
43 | |
|
44 | |
public List<String> getColumns() { |
45 | 0 | return columns; |
46 | |
} |
47 | |
|
48 | |
public boolean isDeferrable() { |
49 | 0 | return deferrable; |
50 | |
} |
51 | |
|
52 | |
public void setDeferrable(boolean deferrable) { |
53 | 0 | this.deferrable = deferrable; |
54 | 0 | } |
55 | |
|
56 | |
public boolean isInitiallyDeferred() { |
57 | 0 | return initiallyDeferred; |
58 | |
} |
59 | |
|
60 | |
public void setInitiallyDeferred(boolean initiallyDeferred) { |
61 | 0 | this.initiallyDeferred = initiallyDeferred; |
62 | 0 | } |
63 | |
|
64 | |
public String getColumnNames() { |
65 | 0 | return StringUtils.join(columns, ", "); |
66 | |
} |
67 | |
|
68 | |
public void setDisabled(boolean disabled) { |
69 | 0 | this.disabled = disabled; |
70 | 0 | } |
71 | |
|
72 | |
public boolean isDisabled() { |
73 | 0 | return disabled; |
74 | |
} |
75 | |
|
76 | |
public String getTablespace() { |
77 | 0 | return tablespace; |
78 | |
} |
79 | |
|
80 | |
public void setTablespace(String tablespace) { |
81 | 0 | this.tablespace = tablespace; |
82 | 0 | } |
83 | |
|
84 | |
@Override |
85 | |
public boolean equals(Object o) { |
86 | 0 | if (this == o) |
87 | 0 | return true; |
88 | 0 | if (o == null || getClass() != o.getClass()) |
89 | 0 | return false; |
90 | 0 | if (null == this.getColumnNames()) |
91 | 0 | return false; |
92 | 0 | UniqueConstraint that = (UniqueConstraint) o; |
93 | 0 | boolean result = false; |
94 | 0 | result = !(getColumnNames() != null ? !getColumnNames().equalsIgnoreCase(that.getColumnNames()) : that |
95 | |
.getColumnNames() != null) |
96 | |
&& isDeferrable() == that.isDeferrable() |
97 | |
&& isInitiallyDeferred() == that.isInitiallyDeferred() && isDisabled() == that.isDisabled(); |
98 | |
|
99 | |
|
100 | 0 | if (result) { |
101 | 0 | if (null == this.getTable()) { |
102 | 0 | result = null == that.getTable(); |
103 | 0 | } else if (null == that.getTable()) { |
104 | 0 | result = false; |
105 | |
} else { |
106 | 0 | result = this.getTable().getName().equals(that.getTable().getName()); |
107 | |
} |
108 | |
} |
109 | |
|
110 | 0 | return result; |
111 | |
|
112 | |
} |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public int compareTo(UniqueConstraint o) { |
118 | |
|
119 | |
String thisTableName; |
120 | |
String thatTableName; |
121 | 0 | thisTableName = null == this.getTable() ? "" : this.getTable().getName(); |
122 | 0 | thatTableName = null == o.getTable() ? "" : o.getTable().getName(); |
123 | 0 | int returnValue = thisTableName.compareTo(thatTableName); |
124 | 0 | if (returnValue == 0) { |
125 | 0 | returnValue = this.getName().compareTo(o.getName()); |
126 | |
} |
127 | 0 | if (returnValue == 0) { |
128 | 0 | returnValue = this.getColumnNames().compareTo(o.getColumnNames()); |
129 | |
} |
130 | 0 | return returnValue; |
131 | |
} |
132 | |
|
133 | |
@Override |
134 | |
public int hashCode() { |
135 | 0 | int result = 0; |
136 | 0 | if (this.table != null) { |
137 | 0 | result = this.table.hashCode(); |
138 | |
} |
139 | 0 | if (this.name != null) { |
140 | 0 | result = 31 * result + this.name.toUpperCase().hashCode(); |
141 | |
} |
142 | 0 | if (getColumnNames() != null) { |
143 | 0 | result = 31 * result + getColumnNames().hashCode(); |
144 | |
} |
145 | 0 | return result; |
146 | |
} |
147 | |
|
148 | |
@Override |
149 | |
public String toString() { |
150 | 0 | return getName() + " on " + getTable().getName() + "(" + getColumnNames() + ")"; |
151 | |
} |
152 | |
} |