1 | |
package liquibase.sqlgenerator.core; |
2 | |
|
3 | |
import java.util.Arrays; |
4 | |
import java.util.Date; |
5 | |
import java.util.HashSet; |
6 | |
import liquibase.database.Database; |
7 | |
import liquibase.database.core.MySQLDatabase; |
8 | |
import liquibase.database.typeconversion.TypeConverterFactory; |
9 | |
import liquibase.sqlgenerator.SqlGeneratorChain; |
10 | |
import liquibase.statement.core.InsertOrUpdateStatement; |
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | 10 | public class InsertOrUpdateGeneratorMySQL extends InsertOrUpdateGenerator { |
17 | |
@Override |
18 | |
public boolean supports(InsertOrUpdateStatement statement, Database database) { |
19 | 0 | return database instanceof MySQLDatabase; |
20 | |
} |
21 | |
|
22 | |
@Override |
23 | |
protected String getInsertStatement(InsertOrUpdateStatement insertOrUpdateStatement, Database database, |
24 | |
SqlGeneratorChain sqlGeneratorChain) { |
25 | 0 | StringBuffer sql = new StringBuffer(super.getInsertStatement(insertOrUpdateStatement, database, |
26 | |
sqlGeneratorChain)); |
27 | |
|
28 | 0 | sql.deleteCharAt(sql.lastIndexOf(";")); |
29 | |
|
30 | 0 | StringBuffer updateClause = new StringBuffer("ON DUPLICATE KEY UPDATE "); |
31 | 0 | String[] pkFields = insertOrUpdateStatement.getPrimaryKey().split(","); |
32 | 0 | HashSet<String> hashPkFields = new HashSet<String>(Arrays.asList(pkFields)); |
33 | 0 | boolean hasFields = false; |
34 | 0 | for (String columnKey : insertOrUpdateStatement.getColumnValues().keySet()) { |
35 | 0 | if (!hashPkFields.contains(columnKey)) { |
36 | 0 | hasFields = true; |
37 | 0 | updateClause.append(columnKey).append(" = "); |
38 | 0 | updateClause.append(convertToString(insertOrUpdateStatement.getColumnValue(columnKey), database)); |
39 | 0 | updateClause.append(","); |
40 | |
} |
41 | |
} |
42 | |
|
43 | 0 | if (hasFields) { |
44 | |
|
45 | 0 | updateClause.deleteCharAt(updateClause.lastIndexOf(",")); |
46 | 0 | sql.append(updateClause); |
47 | |
} else { |
48 | |
|
49 | 0 | sql.insert(sql.indexOf("INSERT ") + "INSERT ".length(), "IGNORE "); |
50 | |
} |
51 | |
|
52 | 0 | return sql.toString(); |
53 | |
} |
54 | |
|
55 | |
@Override |
56 | |
protected String getUpdateStatement(InsertOrUpdateStatement insertOrUpdateStatement, Database database, |
57 | |
String whereClause, SqlGeneratorChain sqlGeneratorChain) { |
58 | 0 | return ""; |
59 | |
} |
60 | |
|
61 | |
@Override |
62 | |
protected String getRecordCheck(InsertOrUpdateStatement insertOrUpdateStatement, Database database, |
63 | |
String whereClause) { |
64 | 0 | return ""; |
65 | |
} |
66 | |
|
67 | |
@Override |
68 | |
protected String getElse(Database database) { |
69 | 0 | return ""; |
70 | |
} |
71 | |
|
72 | |
private String convertToString(Object newValue, Database database) { |
73 | |
String sqlString; |
74 | 0 | if (newValue == null || newValue.toString().equals("") || newValue.toString().equalsIgnoreCase("NULL")) { |
75 | 0 | sqlString = "NULL"; |
76 | 0 | } else if (newValue instanceof String && database.shouldQuoteValue(((String) newValue))) { |
77 | 0 | sqlString = "'" + database.escapeStringForDatabase(newValue.toString()) + "'"; |
78 | 0 | } else if (newValue instanceof Date) { |
79 | 0 | sqlString = database.getDateLiteral(((Date) newValue)); |
80 | 0 | } else if (newValue instanceof Boolean) { |
81 | 0 | if (((Boolean) newValue)) { |
82 | 0 | sqlString = TypeConverterFactory.getInstance().findTypeConverter(database).getBooleanType() |
83 | |
.getTrueBooleanValue(); |
84 | |
} else { |
85 | 0 | sqlString = TypeConverterFactory.getInstance().findTypeConverter(database).getBooleanType() |
86 | |
.getFalseBooleanValue(); |
87 | |
} |
88 | |
} else { |
89 | 0 | sqlString = newValue.toString(); |
90 | |
} |
91 | 0 | return sqlString; |
92 | |
} |
93 | |
} |