| 1 | |
package liquibase.statement.core; |
| 2 | |
|
| 3 | |
import java.util.ArrayList; |
| 4 | |
import java.util.Arrays; |
| 5 | |
import java.util.HashMap; |
| 6 | |
import java.util.HashSet; |
| 7 | |
import java.util.List; |
| 8 | |
import java.util.Map; |
| 9 | |
import java.util.Set; |
| 10 | |
|
| 11 | |
import liquibase.database.structure.type.DataType; |
| 12 | |
import liquibase.statement.AbstractSqlStatement; |
| 13 | |
import liquibase.statement.AutoIncrementConstraint; |
| 14 | |
import liquibase.statement.ColumnConstraint; |
| 15 | |
import liquibase.statement.ForeignKeyConstraint; |
| 16 | |
import liquibase.statement.NotNullConstraint; |
| 17 | |
import liquibase.statement.PrimaryKeyConstraint; |
| 18 | |
import liquibase.statement.UniqueConstraint; |
| 19 | |
|
| 20 | |
public class CreateTableStatement extends AbstractSqlStatement { |
| 21 | |
private String schemaName; |
| 22 | |
private String tableName; |
| 23 | |
private String tablespace; |
| 24 | 109 | private List<String> columns = new ArrayList<String>(); |
| 25 | 109 | private Set<String> autoIncrementColumns = new HashSet<String>(); |
| 26 | 109 | private Map<String, DataType> columnTypes = new HashMap<String, DataType>(); |
| 27 | 109 | private Map<String, Object> defaultValues = new HashMap<String, Object>(); |
| 28 | |
|
| 29 | |
private PrimaryKeyConstraint primaryKeyConstraint; |
| 30 | 109 | private Set<String> notNullColumns = new HashSet<String>(); |
| 31 | 109 | private Set<ForeignKeyConstraint> foreignKeyConstraints = new HashSet<ForeignKeyConstraint>(); |
| 32 | 109 | private Set<UniqueConstraint> uniqueConstraints = new HashSet<UniqueConstraint>(); |
| 33 | |
|
| 34 | 109 | public CreateTableStatement(String schemaName, String tableName) { |
| 35 | 109 | this.schemaName = schemaName; |
| 36 | 109 | this.tableName = tableName; |
| 37 | 109 | } |
| 38 | |
|
| 39 | |
public String getSchemaName() { |
| 40 | 4 | return schemaName; |
| 41 | |
} |
| 42 | |
|
| 43 | |
public String getTableName() { |
| 44 | 38 | return tableName; |
| 45 | |
} |
| 46 | |
|
| 47 | |
public List<String> getColumns() { |
| 48 | 102 | return columns; |
| 49 | |
} |
| 50 | |
|
| 51 | |
public String getTablespace() { |
| 52 | 4 | return tablespace; |
| 53 | |
} |
| 54 | |
|
| 55 | |
public CreateTableStatement setTablespace(String tablespace) { |
| 56 | 62 | this.tablespace = tablespace; |
| 57 | 62 | return this; |
| 58 | |
} |
| 59 | |
|
| 60 | |
public PrimaryKeyConstraint getPrimaryKeyConstraint() { |
| 61 | 5 | return primaryKeyConstraint; |
| 62 | |
} |
| 63 | |
|
| 64 | |
public Set<ForeignKeyConstraint> getForeignKeyConstraints() { |
| 65 | 11 | return foreignKeyConstraints; |
| 66 | |
} |
| 67 | |
|
| 68 | |
public Set<UniqueConstraint> getUniqueConstraints() { |
| 69 | 5 | return uniqueConstraints; |
| 70 | |
} |
| 71 | |
|
| 72 | |
public Set<String> getNotNullColumns() { |
| 73 | 6 | return notNullColumns; |
| 74 | |
} |
| 75 | |
|
| 76 | |
public CreateTableStatement addPrimaryKeyColumn(String columnName, DataType columnType, Object defaultValue, |
| 77 | |
String keyName, String tablespace, ColumnConstraint... constraints) { |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | 1 | PrimaryKeyConstraint pkConstraint = new PrimaryKeyConstraint(keyName); |
| 83 | 1 | pkConstraint.addColumns(columnName); |
| 84 | 1 | pkConstraint.setTablespace(tablespace); |
| 85 | |
|
| 86 | 1 | List<ColumnConstraint> allConstraints = new ArrayList<ColumnConstraint>(); |
| 87 | 1 | allConstraints.addAll(Arrays.asList(constraints)); |
| 88 | 1 | allConstraints.add(new NotNullConstraint(columnName)); |
| 89 | 1 | allConstraints.add(pkConstraint); |
| 90 | |
|
| 91 | 1 | addColumn(columnName, columnType, defaultValue, |
| 92 | |
allConstraints.toArray(new ColumnConstraint[allConstraints.size()])); |
| 93 | |
|
| 94 | 1 | return this; |
| 95 | |
} |
| 96 | |
|
| 97 | |
public CreateTableStatement addColumn(String columnName, DataType columnType) { |
| 98 | 45 | return addColumn(columnName, columnType, null, new ColumnConstraint[0]); |
| 99 | |
} |
| 100 | |
|
| 101 | |
public CreateTableStatement addColumn(String columnName, DataType columnType, Object defaultValue) { |
| 102 | 17 | if (defaultValue instanceof ColumnConstraint) { |
| 103 | 0 | return addColumn(columnName, columnType, null, (ColumnConstraint) defaultValue); |
| 104 | |
} |
| 105 | 17 | return addColumn(columnName, columnType, defaultValue, new ColumnConstraint[0]); |
| 106 | |
} |
| 107 | |
|
| 108 | |
public CreateTableStatement addColumn(String columnName, DataType columnType, ColumnConstraint... constraints) { |
| 109 | 0 | return addColumn(columnName, columnType, null, constraints); |
| 110 | |
} |
| 111 | |
|
| 112 | |
public CreateTableStatement addColumn(String columnName, DataType columnType, Object defaultValue, |
| 113 | |
ColumnConstraint... constraints) { |
| 114 | 63 | this.getColumns().add(columnName); |
| 115 | 63 | this.columnTypes.put(columnName, columnType); |
| 116 | 63 | if (defaultValue != null) { |
| 117 | 6 | defaultValues.put(columnName, defaultValue); |
| 118 | |
} |
| 119 | 63 | if (constraints != null) { |
| 120 | 65 | for (ColumnConstraint constraint : constraints) { |
| 121 | 2 | if (constraint == null) { |
| 122 | 0 | continue; |
| 123 | |
} |
| 124 | |
|
| 125 | 2 | if (constraint instanceof PrimaryKeyConstraint) { |
| 126 | 1 | if (this.getPrimaryKeyConstraint() == null) { |
| 127 | 1 | this.primaryKeyConstraint = (PrimaryKeyConstraint) constraint; |
| 128 | |
} else { |
| 129 | 0 | for (String column : ((PrimaryKeyConstraint) constraint).getColumns()) { |
| 130 | 0 | this.getPrimaryKeyConstraint().addColumns(column); |
| 131 | |
} |
| 132 | |
} |
| 133 | 1 | } else if (constraint instanceof NotNullConstraint) { |
| 134 | 1 | ((NotNullConstraint) constraint).setColumnName(columnName); |
| 135 | 1 | getNotNullColumns().add(columnName); |
| 136 | 0 | } else if (constraint instanceof ForeignKeyConstraint) { |
| 137 | 0 | ((ForeignKeyConstraint) constraint).setColumn(columnName); |
| 138 | 0 | getForeignKeyConstraints().add(((ForeignKeyConstraint) constraint)); |
| 139 | 0 | } else if (constraint instanceof UniqueConstraint) { |
| 140 | 0 | ((UniqueConstraint) constraint).addColumns(columnName); |
| 141 | 0 | getUniqueConstraints().add(((UniqueConstraint) constraint)); |
| 142 | 0 | } else if (constraint instanceof AutoIncrementConstraint) { |
| 143 | 0 | autoIncrementColumns.add(columnName); |
| 144 | |
} else { |
| 145 | 0 | throw new RuntimeException("Unknown constraint type: " + constraint.getClass().getName()); |
| 146 | |
} |
| 147 | |
} |
| 148 | |
} |
| 149 | |
|
| 150 | 63 | return this; |
| 151 | |
} |
| 152 | |
|
| 153 | |
public Object getDefaultValue(String column) { |
| 154 | 8 | return defaultValues.get(column); |
| 155 | |
} |
| 156 | |
|
| 157 | |
public CreateTableStatement addColumnConstraint(NotNullConstraint notNullConstraint) { |
| 158 | 2 | getNotNullColumns().add(notNullConstraint.getColumnName()); |
| 159 | 2 | return this; |
| 160 | |
} |
| 161 | |
|
| 162 | |
public CreateTableStatement addColumnConstraint(ForeignKeyConstraint fkConstraint) { |
| 163 | 4 | getForeignKeyConstraints().add(fkConstraint); |
| 164 | 4 | return this; |
| 165 | |
} |
| 166 | |
|
| 167 | |
public CreateTableStatement addColumnConstraint(UniqueConstraint uniqueConstraint) { |
| 168 | 1 | getUniqueConstraints().add(uniqueConstraint); |
| 169 | 1 | return this; |
| 170 | |
} |
| 171 | |
|
| 172 | |
public CreateTableStatement addColumnConstraint(AutoIncrementConstraint autoIncrementConstraint) { |
| 173 | 0 | autoIncrementColumns.add(autoIncrementConstraint.getColumnName()); |
| 174 | 0 | return this; |
| 175 | |
} |
| 176 | |
|
| 177 | |
public Set<String> getAutoIncrementColumns() { |
| 178 | 2 | return autoIncrementColumns; |
| 179 | |
} |
| 180 | |
|
| 181 | |
public Map<String, DataType> getColumnTypes() { |
| 182 | 3 | return columnTypes; |
| 183 | |
} |
| 184 | |
|
| 185 | |
public Map<String, Object> getDefaultValues() { |
| 186 | 0 | return defaultValues; |
| 187 | |
} |
| 188 | |
|
| 189 | |
public void setSchemaName(String schemaName) { |
| 190 | 0 | this.schemaName = schemaName; |
| 191 | 0 | } |
| 192 | |
} |