/
DB2Database.java
202 lines (172 loc) · 6.84 KB
/
DB2Database.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package liquibase.database.core;
import liquibase.database.AbstractDatabase;
import liquibase.database.DataType;
import liquibase.database.DatabaseConnection;
import liquibase.exception.DatabaseException;
import liquibase.exception.DateParseException;
import java.sql.Types;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class DB2Database extends AbstractDatabase {
private static final DataType BOOLEAN_TYPE = new DataType("SMALLINT", true);
private static final DataType CURRENCY_TYPE = new DataType("DECIMAL(19,4)", false);
private static final DataType UUID_TYPE = new DataType("VARCHAR(36)", false);
private static final DataType CLOB_TYPE = new DataType("CLOB", true);
private static final DataType BLOB_TYPE = new DataType("BLOB", true);
private static final DataType DATETIME_TYPE = new DataType("TIMESTAMP", false);
public boolean isCorrectDatabaseImplementation(DatabaseConnection conn) throws DatabaseException {
return conn.getDatabaseProductName().startsWith("DB2");
}
public String getDefaultDriver(String url) {
if (url.startsWith("jdbc:db2")) {
return "com.ibm.db2.jcc.DB2Driver";
}
return null;
}
public String getTypeName() {
return "db2";
}
@Override
protected String getDefaultDatabaseSchemaName() throws DatabaseException {//NOPMD
return super.getDefaultDatabaseSchemaName().toUpperCase();
}
public boolean supportsInitiallyDeferrableColumns() {
return false;
}
@Override
public String getAutoIncrementClause() {
return "GENERATED BY DEFAULT AS IDENTITY";
}
public String getCurrentDateTimeFunction() {
return "CURRENT TIMESTAMP";
}
public DataType getBooleanType() {
return BOOLEAN_TYPE;
}
@Override
public String getTrueBooleanValue() {
return "1";
}
@Override
public String getFalseBooleanValue() {
return "0";
}
/**
* Return an DB2 date literal with the same value as a string formatted using ISO 8601.
* <p/>
* Convert an ISO8601 date string to one of the following results:
* to_date('1995-05-23', 'YYYY-MM-DD')
* to_date('1995-05-23 09:23:59', 'YYYY-MM-DD HH24:MI:SS')
* <p/>
* Implementation restriction:
* Currently, only the following subsets of ISO8601 are supported:
* YYYY-MM-DD
* hh:mm:ss
* YYYY-MM-DDThh:mm:ss
*/
@Override
public String getDateLiteral(String isoDate) {
String normalLiteral = super.getDateLiteral(isoDate);
if (isDateOnly(isoDate)) {
StringBuffer val = new StringBuffer();
val.append("DATE(");
val.append(normalLiteral);
val.append(')');
return val.toString();
} else if (isTimeOnly(isoDate)) {
StringBuffer val = new StringBuffer();
val.append("TIME(");
val.append(normalLiteral);
val.append(')');
return val.toString();
} else if (isDateTime(isoDate)) {
StringBuffer val = new StringBuffer();
val.append("TIMESTAMP(");
val.append(normalLiteral);
val.append(')');
return val.toString();
} else {
return "UNSUPPORTED:" + isoDate;
}
}
public DataType getCurrencyType() {
return CURRENCY_TYPE;
}
public DataType getUUIDType() {
return UUID_TYPE;
}
public DataType getClobType() {
return CLOB_TYPE;
}
public DataType getBlobType() {
return BLOB_TYPE;
}
public DataType getDateTimeType() {
return DATETIME_TYPE;
}
@Override
public boolean shouldQuoteValue(String value) {
return super.shouldQuoteValue(value)
&& !value.startsWith("\"SYSIBM\"");
}
public boolean supportsTablespaces() {
return true;
}
@Override
public String getViewDefinition(String schemaName, String name) throws DatabaseException {
return super.getViewDefinition(schemaName, name).replaceFirst("CREATE VIEW \\w+ AS ", ""); //db2 returns "create view....as select
}
@Override
public Object convertDatabaseValueToJavaObject(Object defaultValue, int dataType, int columnSize, int decimalDigits) throws ParseException {
if (defaultValue != null && defaultValue instanceof String) {
if (dataType == Types.TIMESTAMP) {
defaultValue = ((String) defaultValue).replaceFirst("^\"SYSIBM\".\"TIMESTAMP\"\\('", "").replaceFirst("'\\)", "");
} else if (dataType == Types.TIME) {
defaultValue = ((String) defaultValue).replaceFirst("^\"SYSIBM\".\"TIME\"\\('", "").replaceFirst("'\\)", "");
} else if (dataType == Types.DATE) {
defaultValue = ((String) defaultValue).replaceFirst("^\"SYSIBM\".\"DATE\"\\('", "").replaceFirst("'\\)", "");
}
}
return super.convertDatabaseValueToJavaObject(defaultValue, dataType, columnSize, decimalDigits);
}
@Override
protected java.util.Date parseDate(String dateAsString) throws DateParseException {
try {
if (dateAsString.indexOf(' ') > 0) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateAsString);
} else if (dateAsString.indexOf('.') > 0 && dateAsString.indexOf('-') > 0) {
return new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSSSSS").parse(dateAsString);
} else {
if (dateAsString.indexOf(':') > 0) {
return new SimpleDateFormat("HH:mm:ss").parse(dateAsString);
} else if (dateAsString.indexOf('.') > 0) {
return new SimpleDateFormat("HH.mm.ss").parse(dateAsString);
} else {
return new SimpleDateFormat("yyyy-MM-dd").parse(dateAsString);
}
}
} catch (ParseException e) {
throw new DateParseException(dateAsString);
}
}
@Override
public String convertRequestedSchemaToSchema(String requestedSchema) throws DatabaseException {
if (requestedSchema == null) {
return getDefaultDatabaseSchemaName();
} else {
return requestedSchema.toUpperCase();
}
}
@Override
public String convertRequestedSchemaToCatalog(String requestedSchema) throws DatabaseException {
return null;
}
@Override
public String generatePrimaryKeyName(String tableName) {
String pkName = super.generatePrimaryKeyName(tableName);
if (pkName.length() > 18) {
pkName = pkName.substring(0, 17);
}
return pkName;
}
}