001: package liquibase.change;
002:
003: import liquibase.database.Database;
004: import liquibase.database.sql.CreateIndexStatement;
005: import liquibase.database.sql.SqlStatement;
006: import liquibase.database.structure.DatabaseObject;
007: import liquibase.database.structure.Index;
008: import liquibase.database.structure.Table;
009: import liquibase.exception.UnsupportedChangeException;
010: import org.w3c.dom.Document;
011: import org.w3c.dom.Element;
012:
013: import java.util.*;
014:
015: /**
016: * Creates an index on an existing column.
017: */
018: public class CreateIndexChange extends AbstractChange {
019:
020: private String schemaName;
021: private String tableName;
022: private String indexName;
023: private String tablespace;
024: private List<ColumnConfig> columns;
025:
026: public CreateIndexChange() {
027: super ("createIndex", "Create Index");
028: columns = new ArrayList<ColumnConfig>();
029: }
030:
031: public String getIndexName() {
032: return indexName;
033: }
034:
035: public void setIndexName(String indexName) {
036: this .indexName = indexName;
037: }
038:
039: public String getSchemaName() {
040: return schemaName;
041: }
042:
043: public void setSchemaName(String schemaName) {
044: this .schemaName = schemaName;
045: }
046:
047: public String getTableName() {
048: return tableName;
049: }
050:
051: public void setTableName(String tableName) {
052: this .tableName = tableName;
053: }
054:
055: public List<ColumnConfig> getColumns() {
056: return columns;
057: }
058:
059: public void setColumns(List<ColumnConfig> columns) {
060: this .columns = columns;
061: }
062:
063: public void addColumn(ColumnConfig column) {
064: columns.add(column);
065: }
066:
067: public String getTablespace() {
068: return tablespace;
069: }
070:
071: public void setTablespace(String tablespace) {
072: this .tablespace = tablespace;
073: }
074:
075: public SqlStatement[] generateStatements(Database database)
076: throws UnsupportedChangeException {
077: List<String> columns = new ArrayList<String>();
078: for (ColumnConfig column : getColumns()) {
079: columns.add(column.getName());
080: }
081:
082: return new SqlStatement[] { new CreateIndexStatement(
083: getIndexName(), getSchemaName() == null ? database
084: .getDefaultSchemaName() : getSchemaName(),
085: getTableName(), columns.toArray(new String[getColumns()
086: .size()])).setTablespace(getTablespace()) };
087: }
088:
089: protected Change[] createInverses() {
090: DropIndexChange inverse = new DropIndexChange();
091: inverse.setSchemaName(getSchemaName());
092: inverse.setTableName(getTableName());
093: inverse.setIndexName(getIndexName());
094:
095: return new Change[] { inverse };
096: }
097:
098: public String getConfirmationMessage() {
099: return "Index " + getIndexName() + " created";
100: }
101:
102: public Element createNode(Document currentChangeLogFileDOM) {
103: Element element = currentChangeLogFileDOM
104: .createElement("createIndex");
105: element.setAttribute("indexName", getIndexName());
106: if (getSchemaName() != null) {
107: element.setAttribute("schemaName", getSchemaName());
108: }
109:
110: element.setAttribute("tableName", getTableName());
111:
112: for (ColumnConfig column : getColumns()) {
113: Element columnElement = currentChangeLogFileDOM
114: .createElement("column");
115: columnElement.setAttribute("name", column.getName());
116: element.appendChild(columnElement);
117: }
118:
119: return element;
120: }
121:
122: public Set<DatabaseObject> getAffectedDatabaseObjects() {
123: Index index = new Index();
124: index.setTable(new Table(tableName));
125: index.setName(indexName);
126:
127: Table table = new Table(getTableName());
128:
129: return new HashSet<DatabaseObject>(Arrays.asList(index, table));
130: }
131:
132: }
|