01: package liquibase.database.sql;
02:
03: import liquibase.database.Database;
04:
05: import java.util.ArrayList;
06: import java.util.Date;
07: import java.util.List;
08:
09: public class DeleteStatement implements SqlStatement {
10: private String schemaName;
11: private String tableName;
12: private String whereClause;
13: private List<Object> whereParameters = new ArrayList<Object>();
14:
15: public DeleteStatement(String schemaName, String tableName) {
16: this .schemaName = schemaName;
17: this .tableName = tableName;
18: }
19:
20: public String getSchemaName() {
21: return schemaName;
22: }
23:
24: public String getTableName() {
25: return tableName;
26: }
27:
28: public String getWhereClause() {
29: return whereClause;
30: }
31:
32: public DeleteStatement setWhereClause(String whereClause) {
33: this .whereClause = whereClause;
34:
35: return this ;
36: }
37:
38: public void addWhereParameter(Object value) {
39: this .whereParameters.add(value);
40: }
41:
42: public boolean supportsDatabase(Database database) {
43: return true;
44: }
45:
46: public String getSqlStatement(Database database) {
47: StringBuffer sql = new StringBuffer("DELETE FROM "
48: + database.escapeTableName(getSchemaName(),
49: getTableName()));
50:
51: if (whereClause != null) {
52: String fixedWhereClause = " WHERE " + whereClause;
53: for (Object param : whereParameters) {
54: fixedWhereClause = fixedWhereClause.replaceFirst("\\?",
55: convertToString(param, database));
56: }
57: sql.append(" ").append(fixedWhereClause);
58: }
59:
60: return sql.toString();
61: }
62:
63: private String convertToString(Object newValue, Database database) {
64: String sqlString;
65: if (newValue == null) {
66: sqlString = "NULL";
67: } else if (newValue instanceof String
68: && database.shouldQuoteValue(((String) newValue))) {
69: sqlString = "'" + newValue + "'";
70: } else if (newValue instanceof Date) {
71: sqlString = database.getDateLiteral(((Date) newValue));
72: } else if (newValue instanceof Boolean) {
73: if (((Boolean) newValue)) {
74: sqlString = database.getTrueBooleanValue();
75: } else {
76: sqlString = database.getFalseBooleanValue();
77: }
78: } else {
79: sqlString = newValue.toString();
80: }
81: return sqlString;
82: }
83:
84: public String getEndDelimiter(Database database) {
85: return ";";
86: }
87: }
|