| 1 | |
package liquibase.database.structure; |
| 2 | |
|
| 3 | |
import java.util.ArrayList; |
| 4 | |
import java.util.HashSet; |
| 5 | |
import java.util.List; |
| 6 | |
import java.util.Set; |
| 7 | |
|
| 8 | |
import liquibase.util.StringUtils; |
| 9 | |
|
| 10 | 0 | public class Index implements DatabaseObject, Comparable<Index> { |
| 11 | |
|
| 12 | |
|
| 13 | |
public final static String MARK_PRIMARY_KEY = "primaryKey"; |
| 14 | |
|
| 15 | |
public final static String MARK_FOREIGN_KEY = "foreignKey"; |
| 16 | |
|
| 17 | |
public final static String MARK_UNIQUE_CONSTRAINT = "uniqueConstraint"; |
| 18 | |
|
| 19 | |
private String name; |
| 20 | |
private Table table; |
| 21 | |
private String tablespace; |
| 22 | |
private Boolean unique; |
| 23 | 0 | private List<String> columns = new ArrayList<String>(); |
| 24 | |
private String filterCondition; |
| 25 | |
|
| 26 | |
|
| 27 | 0 | private Set<String> associatedWith = new HashSet<String>(); |
| 28 | |
|
| 29 | |
@Override |
| 30 | |
public DatabaseObject[] getContainingObjects() { |
| 31 | 0 | return new DatabaseObject[] { table }; |
| 32 | |
} |
| 33 | |
|
| 34 | |
public String getName() { |
| 35 | 0 | return name; |
| 36 | |
} |
| 37 | |
|
| 38 | |
public void setName(String name) { |
| 39 | 0 | this.name = name; |
| 40 | 0 | } |
| 41 | |
|
| 42 | |
public Table getTable() { |
| 43 | 0 | return table; |
| 44 | |
} |
| 45 | |
|
| 46 | |
public void setTable(Table table) { |
| 47 | 0 | this.table = table; |
| 48 | 0 | } |
| 49 | |
|
| 50 | |
public String getTablespace() { |
| 51 | 0 | return tablespace; |
| 52 | |
} |
| 53 | |
|
| 54 | |
public void setTablespace(String tablespace) { |
| 55 | 0 | this.tablespace = tablespace; |
| 56 | 0 | } |
| 57 | |
|
| 58 | |
public List<String> getColumns() { |
| 59 | 0 | return columns; |
| 60 | |
} |
| 61 | |
|
| 62 | |
public String getColumnNames() { |
| 63 | 0 | return StringUtils.join(columns, ", "); |
| 64 | |
} |
| 65 | |
|
| 66 | |
public String getFilterCondition() { |
| 67 | 0 | return filterCondition; |
| 68 | |
} |
| 69 | |
|
| 70 | |
public void setFilterCondition(String filterCondition) { |
| 71 | 0 | this.filterCondition = filterCondition; |
| 72 | 0 | } |
| 73 | |
|
| 74 | |
public void setUnique(Boolean value) { |
| 75 | 0 | this.unique = value; |
| 76 | 0 | } |
| 77 | |
|
| 78 | |
public Boolean isUnique() { |
| 79 | 0 | return this.unique; |
| 80 | |
} |
| 81 | |
|
| 82 | |
public Set<String> getAssociatedWith() { |
| 83 | 0 | return associatedWith; |
| 84 | |
} |
| 85 | |
|
| 86 | |
public String getAssociatedWithAsString() { |
| 87 | 0 | return StringUtils.join(associatedWith, ","); |
| 88 | |
} |
| 89 | |
|
| 90 | |
public void addAssociatedWith(String item) { |
| 91 | 0 | associatedWith.add(item); |
| 92 | 0 | } |
| 93 | |
|
| 94 | |
public boolean isAssociatedWith(String keyword) { |
| 95 | 0 | return associatedWith.contains(keyword); |
| 96 | |
} |
| 97 | |
|
| 98 | |
@Override |
| 99 | |
public boolean equals(Object o) { |
| 100 | 0 | if (this == o) |
| 101 | 0 | return true; |
| 102 | 0 | if (o == null || getClass() != o.getClass()) |
| 103 | 0 | return false; |
| 104 | |
|
| 105 | 0 | Index index = (Index) o; |
| 106 | 0 | boolean equals = true; |
| 107 | 0 | for (String column : index.getColumns()) { |
| 108 | 0 | if (!columns.contains(column)) { |
| 109 | 0 | equals = false; |
| 110 | |
} |
| 111 | |
} |
| 112 | |
|
| 113 | 0 | if (this.unique == null && index.isUnique() == null) { |
| 114 | |
|
| 115 | 0 | } else if (this.unique == null && index.isUnique() != null) { |
| 116 | 0 | equals = false; |
| 117 | 0 | } else if (this.unique != null && index.isUnique() == null) { |
| 118 | 0 | equals = false; |
| 119 | 0 | } else if (!this.unique.equals(index.isUnique())) { |
| 120 | 0 | equals = false; |
| 121 | |
} |
| 122 | |
|
| 123 | 0 | return equals || table.getName().equalsIgnoreCase(index.table.getName()); |
| 124 | |
|
| 125 | |
} |
| 126 | |
|
| 127 | |
@Override |
| 128 | |
public int hashCode() { |
| 129 | |
int result; |
| 130 | 0 | result = table.getName().toUpperCase().hashCode(); |
| 131 | 0 | result = 31 * result + columns.hashCode(); |
| 132 | 0 | result = 31 * result + (unique == null ? 2 : unique ? 1 : 0); |
| 133 | 0 | return result; |
| 134 | |
} |
| 135 | |
|
| 136 | |
@Override |
| 137 | |
public int compareTo(Index o) { |
| 138 | 0 | int returnValue = this.table.getName().compareTo(o.table.getName()); |
| 139 | |
|
| 140 | 0 | if (returnValue == 0) { |
| 141 | 0 | String thisName = StringUtils.trimToEmpty(this.getName()); |
| 142 | 0 | String oName = StringUtils.trimToEmpty(o.getName()); |
| 143 | 0 | returnValue = thisName.compareTo(oName); |
| 144 | |
} |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | 0 | return returnValue; |
| 152 | |
} |
| 153 | |
|
| 154 | |
@Override |
| 155 | |
public String toString() { |
| 156 | 0 | StringBuffer stringBuffer = new StringBuffer(); |
| 157 | 0 | stringBuffer.append(getName()); |
| 158 | 0 | if (this.unique != null && !this.unique) { |
| 159 | 0 | stringBuffer.append(" unique "); |
| 160 | |
} |
| 161 | 0 | if (table != null && columns != null) { |
| 162 | 0 | stringBuffer.append(" on ").append(table.getName()); |
| 163 | 0 | if (columns != null) { |
| 164 | 0 | stringBuffer.append("("); |
| 165 | 0 | for (String column : columns) { |
| 166 | 0 | stringBuffer.append(column).append(", "); |
| 167 | |
} |
| 168 | 0 | stringBuffer.delete(stringBuffer.length() - 2, stringBuffer.length()); |
| 169 | 0 | stringBuffer.append(")"); |
| 170 | |
} |
| 171 | |
} |
| 172 | 0 | return stringBuffer.toString(); |
| 173 | |
} |
| 174 | |
} |