001: package liquibase.change;
002:
003: import liquibase.database.Database;
004: import liquibase.database.sql.*;
005: import liquibase.database.structure.Column;
006: import liquibase.database.structure.DatabaseObject;
007: import liquibase.database.structure.Table;
008: import liquibase.exception.UnsupportedChangeException;
009: import liquibase.util.StringUtils;
010: import org.w3c.dom.Document;
011: import org.w3c.dom.Element;
012:
013: import java.util.ArrayList;
014: import java.util.HashSet;
015: import java.util.List;
016: import java.util.Set;
017:
018: /**
019: * Creates a new table.
020: */
021: public class CreateTableChange extends AbstractChange {
022:
023: private List<ColumnConfig> columns;
024: private String schemaName;
025: private String tableName;
026: private String tablespace;
027: private String remarks;
028:
029: public CreateTableChange() {
030: super ("createTable", "Create Table");
031: columns = new ArrayList<ColumnConfig>();
032: }
033:
034: public SqlStatement[] generateStatements(Database database)
035: throws UnsupportedChangeException {
036:
037: String schemaName = getSchemaName() == null ? database
038: .getDefaultSchemaName() : getSchemaName();
039: CreateTableStatement statement = new CreateTableStatement(
040: schemaName, getTableName());
041: for (ColumnConfig column : getColumns()) {
042: ConstraintsConfig constraints = column.getConstraints();
043: boolean isAutoIncrement = column.isAutoIncrement() != null
044: && column.isAutoIncrement();
045: if (constraints != null
046: && constraints.isPrimaryKey() != null
047: && constraints.isPrimaryKey()) {
048:
049: statement.addPrimaryKeyColumn(column.getName(),
050: database.getColumnType(column.getType(),
051: isAutoIncrement));
052:
053: } else {
054: String defaultValue = null;
055: if (column.hasDefaultValue()) {
056: defaultValue = StringUtils.trimToNull(column
057: .getDefaultColumnValue(database));
058: }
059: statement.addColumn(column.getName(), database
060: .getColumnType(column.getType(), column
061: .isAutoIncrement()), defaultValue);
062: }
063:
064: if (constraints != null) {
065: if (constraints.isNullable() != null
066: && !constraints.isNullable()) {
067: statement
068: .addColumnConstraint(new NotNullConstraint(
069: column.getName()));
070: }
071:
072: if (constraints.getReferences() != null) {
073: if (StringUtils.trimToNull(constraints
074: .getForeignKeyName()) == null) {
075: throw new UnsupportedChangeException(
076: "createTable with references requires foreignKeyName");
077: }
078: ForeignKeyConstraint fkConstraint = new ForeignKeyConstraint(
079: constraints.getForeignKeyName(),
080: constraints.getReferences());
081: fkConstraint.setColumn(column.getName());
082: fkConstraint.setDeleteCascade(constraints
083: .isDeleteCascade() != null
084: && constraints.isDeleteCascade());
085: fkConstraint.setInitiallyDeferred(constraints
086: .isInitiallyDeferred() != null
087: && constraints.isInitiallyDeferred());
088: fkConstraint.setDeferrable(constraints
089: .isDeferrable() != null
090: && constraints.isDeferrable());
091: statement.addColumnConstraint(fkConstraint);
092: }
093:
094: if (constraints.isUnique() != null
095: && constraints.isUnique()) {
096: statement.addColumnConstraint(new UniqueConstraint(
097: constraints.getUniqueConstraintName())
098: .addColumns(column.getName()));
099: }
100: }
101:
102: if (isAutoIncrement) {
103: statement
104: .addColumnConstraint(new AutoIncrementConstraint(
105: column.getName()));
106: }
107: }
108:
109: statement
110: .setTablespace(StringUtils.trimToNull(getTablespace()));
111:
112: List<SqlStatement> statements = new ArrayList<SqlStatement>();
113: statements.add(statement);
114:
115: return statements.toArray(new SqlStatement[statements.size()]);
116: }
117:
118: protected Change[] createInverses() {
119: DropTableChange inverse = new DropTableChange();
120: inverse.setSchemaName(getSchemaName());
121: inverse.setTableName(getTableName());
122:
123: return new Change[] { inverse };
124: }
125:
126: public List<ColumnConfig> getColumns() {
127: return columns;
128: }
129:
130: public String getSchemaName() {
131: return schemaName;
132: }
133:
134: public void setSchemaName(String schemaName) {
135: this .schemaName = schemaName;
136: }
137:
138: public String getTableName() {
139: return tableName;
140: }
141:
142: public void setTableName(String tableName) {
143: this .tableName = tableName;
144: }
145:
146: public String getTablespace() {
147: return tablespace;
148: }
149:
150: public void setTablespace(String tablespace) {
151: this .tablespace = tablespace;
152: }
153:
154: public void addColumn(ColumnConfig column) {
155: columns.add(column);
156: }
157:
158: public String getRemarks() {
159: return remarks;
160: }
161:
162: public void setRemarks(String remarks) {
163: this .remarks = remarks;
164: }
165:
166: public String getConfirmationMessage() {
167: return "Table " + tableName + " created";
168: }
169:
170: public Element createNode(Document currentChangeLogFileDOM) {
171: Element element = currentChangeLogFileDOM
172: .createElement("createTable");
173: if (getSchemaName() != null) {
174: element.setAttribute("schemaName", getSchemaName());
175: }
176:
177: element.setAttribute("tableName", getTableName());
178: if (StringUtils.trimToNull(tablespace) != null) {
179: element.setAttribute("tablespace", tablespace);
180: }
181: if (StringUtils.trimToNull(remarks) != null) {
182: element.setAttribute("remarks", remarks);
183: }
184: for (ColumnConfig column : getColumns()) {
185: element.appendChild(column
186: .createNode(currentChangeLogFileDOM));
187: }
188: return element;
189: }
190:
191: public Set<DatabaseObject> getAffectedDatabaseObjects() {
192: Set<DatabaseObject> returnSet = new HashSet<DatabaseObject>();
193:
194: Table table = new Table(getTableName());
195: returnSet.add(table);
196:
197: for (ColumnConfig columnConfig : getColumns()) {
198: Column column = new Column();
199: column.setTable(table);
200: column.setName(columnConfig.getName());
201:
202: returnSet.add(column);
203: }
204:
205: return returnSet;
206: }
207: }
|