1 | |
package liquibase.change.core; |
2 | |
|
3 | |
import liquibase.database.Database; |
4 | |
import liquibase.database.typeconversion.TypeConverterFactory; |
5 | |
import liquibase.exception.RollbackImpossibleException; |
6 | |
import liquibase.exception.UnsupportedChangeException; |
7 | |
import liquibase.exception.LiquibaseException; |
8 | |
import liquibase.statement.SqlStatement; |
9 | |
import liquibase.statement.core.DeleteStatement; |
10 | |
import liquibase.statement.core.InsertOrUpdateStatement; |
11 | |
import liquibase.statement.core.InsertStatement; |
12 | |
|
13 | |
import java.util.ArrayList; |
14 | |
import java.util.Date; |
15 | |
import java.util.List; |
16 | |
|
17 | |
public class LoadUpdateDataChange extends LoadDataChange { |
18 | |
private String primaryKey; |
19 | |
|
20 | |
@Override |
21 | |
public SqlStatement[] generateStatements(Database database) { |
22 | 6 | return super.generateStatements(database); |
23 | |
|
24 | |
} |
25 | |
|
26 | |
public LoadUpdateDataChange() { |
27 | 31 | super("loadUpdateData", "Smart Load Data"); |
28 | 31 | } |
29 | |
|
30 | |
public void setPrimaryKey(String primaryKey) throws LiquibaseException { |
31 | 5 | if (primaryKey == null) { |
32 | 1 | throw new LiquibaseException("primaryKey cannot be null."); |
33 | |
} |
34 | 4 | this.primaryKey = primaryKey; |
35 | 4 | } |
36 | |
|
37 | |
public String getPrimaryKey() { |
38 | 1 | return primaryKey; |
39 | |
} |
40 | |
|
41 | |
@Override |
42 | |
protected InsertStatement createStatement(String schemaName, String tableName) { |
43 | 12 | return new InsertOrUpdateStatement(schemaName, tableName, this.primaryKey); |
44 | |
} |
45 | |
|
46 | |
@Override |
47 | |
public SqlStatement[] generateRollbackStatements(Database database) throws UnsupportedChangeException, |
48 | |
RollbackImpossibleException { |
49 | 1 | List<SqlStatement> statements = new ArrayList<SqlStatement>(); |
50 | 1 | SqlStatement[] forward = this.generateStatements(database); |
51 | |
|
52 | 3 | for (SqlStatement thisForward : forward) { |
53 | 2 | InsertOrUpdateStatement thisInsert = (InsertOrUpdateStatement) thisForward; |
54 | 2 | DeleteStatement delete = new DeleteStatement(getSchemaName(), getTableName()); |
55 | 2 | delete.setWhereClause(getWhereClause(thisInsert, database)); |
56 | 2 | statements.add(delete); |
57 | |
} |
58 | |
|
59 | 1 | return statements.toArray(new SqlStatement[statements.size()]); |
60 | |
} |
61 | |
|
62 | |
private String getWhereClause(InsertOrUpdateStatement insertOrUpdateStatement, Database database) { |
63 | 4 | StringBuffer where = new StringBuffer(); |
64 | |
|
65 | 4 | String[] pkColumns = insertOrUpdateStatement.getPrimaryKey().split(","); |
66 | |
|
67 | 8 | for (String thisPkColumn : pkColumns) { |
68 | 4 | where.append(database.escapeColumnName(insertOrUpdateStatement.getSchemaName(), |
69 | |
insertOrUpdateStatement.getTableName(), thisPkColumn) |
70 | |
+ " = "); |
71 | 4 | Object newValue = insertOrUpdateStatement.getColumnValues().get(thisPkColumn); |
72 | 4 | if (newValue == null || newValue.toString().equals("NULL")) { |
73 | 0 | where.append("NULL"); |
74 | 4 | } else if (newValue instanceof String && database.shouldQuoteValue(((String) newValue))) { |
75 | 4 | where.append("'").append(database.escapeStringForDatabase((String) newValue)).append("'"); |
76 | 0 | } else if (newValue instanceof Date) { |
77 | 0 | where.append(database.getDateLiteral(((Date) newValue))); |
78 | 0 | } else if (newValue instanceof Boolean) { |
79 | 0 | if (((Boolean) newValue)) { |
80 | 0 | where.append(TypeConverterFactory.getInstance().findTypeConverter(database).getBooleanType() |
81 | |
.getTrueBooleanValue()); |
82 | |
} else { |
83 | 0 | where.append(TypeConverterFactory.getInstance().findTypeConverter(database).getBooleanType() |
84 | |
.getFalseBooleanValue()); |
85 | |
} |
86 | |
} else { |
87 | 0 | where.append(newValue); |
88 | |
} |
89 | |
|
90 | 4 | where.append(" AND "); |
91 | |
} |
92 | |
|
93 | 4 | where.delete(where.lastIndexOf(" AND "), where.lastIndexOf(" AND ") + " AND ".length()); |
94 | 4 | return where.toString(); |
95 | |
} |
96 | |
|
97 | |
} |