| 1 | |
package liquibase.change.core; |
| 2 | |
|
| 3 | |
import java.util.ArrayList; |
| 4 | |
import java.util.List; |
| 5 | |
|
| 6 | |
import liquibase.change.AbstractChange; |
| 7 | |
import liquibase.change.Change; |
| 8 | |
import liquibase.change.ChangeMetaData; |
| 9 | |
import liquibase.change.ColumnConfig; |
| 10 | |
import liquibase.database.Database; |
| 11 | |
import liquibase.database.core.DB2Database; |
| 12 | |
import liquibase.database.core.SQLiteDatabase; |
| 13 | |
import liquibase.database.core.SQLiteDatabase.AlterTableVisitor; |
| 14 | |
import liquibase.database.structure.Index; |
| 15 | |
import liquibase.statement.SqlStatement; |
| 16 | |
import liquibase.statement.core.AddPrimaryKeyStatement; |
| 17 | |
import liquibase.statement.core.ReorganizeTableStatement; |
| 18 | |
import liquibase.util.StringUtils; |
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
public class AddPrimaryKeyChange extends AbstractChange { |
| 24 | |
|
| 25 | |
private String schemaName; |
| 26 | |
private String tableName; |
| 27 | |
private String tablespace; |
| 28 | |
private String columnNames; |
| 29 | |
private String constraintName; |
| 30 | |
|
| 31 | |
public AddPrimaryKeyChange() { |
| 32 | 75 | super("addPrimaryKey", "Add Primary Key", ChangeMetaData.PRIORITY_DEFAULT); |
| 33 | 75 | } |
| 34 | |
|
| 35 | |
public String getTableName() { |
| 36 | 112 | return tableName; |
| 37 | |
} |
| 38 | |
|
| 39 | |
public void setTableName(String tableName) { |
| 40 | 61 | this.tableName = tableName; |
| 41 | 61 | } |
| 42 | |
|
| 43 | |
public String getSchemaName() { |
| 44 | 122 | return schemaName; |
| 45 | |
} |
| 46 | |
|
| 47 | |
public void setSchemaName(String schemaName) { |
| 48 | 60 | this.schemaName = StringUtils.trimToNull(schemaName); |
| 49 | 60 | } |
| 50 | |
|
| 51 | |
public String getColumnNames() { |
| 52 | 105 | return columnNames; |
| 53 | |
} |
| 54 | |
|
| 55 | |
public void setColumnNames(String columnNames) { |
| 56 | 61 | this.columnNames = columnNames; |
| 57 | 61 | } |
| 58 | |
|
| 59 | |
public String getConstraintName() { |
| 60 | 104 | return constraintName; |
| 61 | |
} |
| 62 | |
|
| 63 | |
public void setConstraintName(String constraintName) { |
| 64 | 1 | this.constraintName = constraintName; |
| 65 | 1 | } |
| 66 | |
|
| 67 | |
public String getTablespace() { |
| 68 | 104 | return tablespace; |
| 69 | |
} |
| 70 | |
|
| 71 | |
public void setTablespace(String tablespace) { |
| 72 | 1 | this.tablespace = tablespace; |
| 73 | 1 | } |
| 74 | |
|
| 75 | |
@Override |
| 76 | |
public SqlStatement[] generateStatements(Database database) { |
| 77 | |
|
| 78 | 104 | String schemaName = getSchemaName() == null ? database.getDefaultSchemaName() : getSchemaName(); |
| 79 | |
|
| 80 | 104 | AddPrimaryKeyStatement statement = new AddPrimaryKeyStatement(schemaName, getTableName(), getColumnNames(), |
| 81 | |
getConstraintName()); |
| 82 | 104 | statement.setTablespace(getTablespace()); |
| 83 | |
|
| 84 | 104 | if (database instanceof DB2Database) { |
| 85 | 7 | return new SqlStatement[] { statement, new ReorganizeTableStatement(schemaName, getTableName()) }; |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
} |
| 90 | |
|
| 91 | 97 | return new SqlStatement[] { statement }; |
| 92 | |
} |
| 93 | |
|
| 94 | |
private SqlStatement[] generateStatementsForSQLiteDatabase(Database database) { |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | 0 | List<SqlStatement> statements = new ArrayList<SqlStatement>(); |
| 100 | |
|
| 101 | |
|
| 102 | 0 | AlterTableVisitor rename_alter_visitor = new AlterTableVisitor() { |
| 103 | |
@Override |
| 104 | |
public ColumnConfig[] getColumnsToAdd() { |
| 105 | 0 | return new ColumnConfig[0]; |
| 106 | |
} |
| 107 | |
|
| 108 | |
@Override |
| 109 | |
public boolean copyThisColumn(ColumnConfig column) { |
| 110 | 0 | return true; |
| 111 | |
} |
| 112 | |
|
| 113 | |
@Override |
| 114 | |
public boolean createThisColumn(ColumnConfig column) { |
| 115 | 0 | String[] split_columns = getColumnNames().split("[ ]*,[ ]*"); |
| 116 | 0 | for (String split_column : split_columns) { |
| 117 | 0 | if (column.getName().equals(split_column)) { |
| 118 | 0 | column.getConstraints().setPrimaryKey(true); |
| 119 | |
} |
| 120 | |
} |
| 121 | 0 | return true; |
| 122 | |
} |
| 123 | |
|
| 124 | |
@Override |
| 125 | |
public boolean createThisIndex(Index index) { |
| 126 | 0 | return true; |
| 127 | |
} |
| 128 | |
}; |
| 129 | |
|
| 130 | |
try { |
| 131 | |
|
| 132 | 0 | statements.addAll(SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, getSchemaName(), |
| 133 | |
getTableName())); |
| 134 | 0 | } catch (Exception e) { |
| 135 | 0 | e.printStackTrace(); |
| 136 | 0 | } |
| 137 | |
|
| 138 | 0 | return statements.toArray(new SqlStatement[statements.size()]); |
| 139 | |
} |
| 140 | |
|
| 141 | |
@Override |
| 142 | |
protected Change[] createInverses() { |
| 143 | 0 | DropPrimaryKeyChange inverse = new DropPrimaryKeyChange(); |
| 144 | 0 | inverse.setSchemaName(getSchemaName()); |
| 145 | 0 | inverse.setTableName(getTableName()); |
| 146 | 0 | inverse.setConstraintName(getConstraintName()); |
| 147 | |
|
| 148 | 0 | return new Change[] { inverse, }; |
| 149 | |
} |
| 150 | |
|
| 151 | |
@Override |
| 152 | |
public String getConfirmationMessage() { |
| 153 | 1 | return "Primary key added to " + getTableName() + " (" + getColumnNames() + ")"; |
| 154 | |
} |
| 155 | |
} |