liquibase.structure.core.Index Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of liquibase-core Show documentation
Show all versions of liquibase-core Show documentation
Liquibase is a tool for managing and executing database changes.
package liquibase.structure.core;
import liquibase.parser.core.ParsedNode;
import liquibase.parser.core.ParsedNodeException;
import liquibase.resource.ResourceAccessor;
import liquibase.structure.AbstractDatabaseObject;
import liquibase.structure.DatabaseObject;
import liquibase.util.StringUtil;
import java.util.*;
public class Index extends AbstractDatabaseObject {
/** Marks Index as associated with Primary Key [PK] */
public static final String MARK_PRIMARY_KEY = "primaryKey";
/** Marks Index as associated with Foreign Key [FK] */
public static final String MARK_FOREIGN_KEY = "foreignKey";
/** Marks Index as associated with Unique Constraint [UC] */
public static final String MARK_UNIQUE_CONSTRAINT = "uniqueConstraint";
public Index() {
setAttribute("columns", new ArrayList());
setAttribute("associatedWith", new HashSet());
}
public Index(String indexName) {
this();
setName(indexName);
}
public Index(String indexName, String catalogName, String schemaName, String tableName, Column... columns) {
this();
setName(indexName);
if (tableName != null) {
setRelation(new Table(catalogName, schemaName, tableName));
if ((columns != null) && (columns.length > 0)) {
setColumns(Arrays.asList(columns));
}
}
}
@Override
public DatabaseObject[] getContainingObjects() {
return new DatabaseObject[] {
getRelation()
};
}
@Override
public String getName() {
return getAttribute("name", String.class);
}
@Override
public Index setName(String name) {
this.setAttribute("name", name);
return this;
}
@Override
public Schema getSchema() {
if (getRelation() == null) {
return null;
}
return getRelation().getSchema();
}
/**
* @deprecated Use {@link #getRelation()}
*/
@Deprecated
public Table getTable() {
Relation relation = getRelation();
if (relation instanceof Table)
return (Table) relation;
else
return null;
}
/**
* @deprecated Use {@link #setRelation(Relation)}
*/
@Deprecated
public Index setTable(Relation table) {
return setRelation(table);
}
public Relation getRelation() {
return getAttribute("table", Relation.class);
}
public Index setRelation(Relation relation) {
this.setAttribute("table", relation);
return this;
}
public String getTablespace() {
return getAttribute("tablespace", String.class);
}
public Index setTablespace(String tablespace) {
this.setAttribute("tablespace", tablespace);
return this;
}
public List getColumns() {
return getAttribute("columns", List.class);
}
public Index addColumn(Column column) {
column.setRelation(getRelation());
getColumns().add(column);
return this;
}
public Index setColumns(List columns) {
if (getAttribute("table", Object.class) instanceof Table) {
for (Column column :columns) {
column.setRelation(getRelation());
}
}
setAttribute("columns", columns);
return this;
}
public String getColumnNames() {
return StringUtil.join(getColumns(), ", ", new StringUtil.ToStringFormatter());
}
public Index setUnique(Boolean value) {
this.setAttribute("unique", value);
return this;
}
public Boolean isUnique() {
return getAttribute("unique", Boolean.class);
}
public Set getAssociatedWith() {
return getAttribute("associatedWith", Set.class);
}
public String getAssociatedWithAsString() {
return StringUtil.join(getAssociatedWith(), ",");
}
public void addAssociatedWith(String item) {
getAssociatedWith().add(item);
}
public boolean isAssociatedWith(String keyword) {
return getAssociatedWith().contains(keyword);
}
@Override
public Object getSerializableFieldValue(String field) {
//
// For columns within an Index, we now represent
// all the columns as actual Column objects with
// the forIndex flag set
//
if (field != null && field.equals("columns")) {
List