1 | |
package liquibase.database.core; |
2 | |
|
3 | |
import java.text.ParseException; |
4 | |
import java.text.SimpleDateFormat; |
5 | |
|
6 | |
import liquibase.database.AbstractDatabase; |
7 | |
import liquibase.database.DatabaseConnection; |
8 | |
import liquibase.exception.DatabaseException; |
9 | |
import liquibase.exception.DateParseException; |
10 | |
|
11 | 4 | public class DB2Database extends AbstractDatabase { |
12 | |
|
13 | |
@Override |
14 | |
public boolean isCorrectDatabaseImplementation(DatabaseConnection conn) throws DatabaseException { |
15 | 0 | return conn.getDatabaseProductName().startsWith("DB2"); |
16 | |
} |
17 | |
|
18 | |
@Override |
19 | |
public String getDefaultDriver(String url) { |
20 | 2 | if (url.startsWith("jdbc:db2")) { |
21 | 1 | return "com.ibm.db2.jcc.DB2Driver"; |
22 | |
} |
23 | 1 | return null; |
24 | |
} |
25 | |
|
26 | |
@Override |
27 | |
public int getPriority() { |
28 | 0 | return PRIORITY_DEFAULT; |
29 | |
} |
30 | |
|
31 | |
@Override |
32 | |
public String getTypeName() { |
33 | 57 | return "db2"; |
34 | |
} |
35 | |
|
36 | |
@Override |
37 | |
protected String getDefaultDatabaseSchemaName() throws DatabaseException { |
38 | 0 | return super.getDefaultDatabaseSchemaName().toUpperCase(); |
39 | |
} |
40 | |
|
41 | |
@Override |
42 | |
public boolean supportsInitiallyDeferrableColumns() { |
43 | 0 | return false; |
44 | |
} |
45 | |
|
46 | |
@Override |
47 | |
public String getAutoIncrementClause() { |
48 | 0 | return "GENERATED BY DEFAULT AS IDENTITY"; |
49 | |
} |
50 | |
|
51 | |
@Override |
52 | |
public String getCurrentDateTimeFunction() { |
53 | 0 | if (currentDateTimeFunction != null) { |
54 | 0 | return currentDateTimeFunction; |
55 | |
} |
56 | |
|
57 | 0 | return "CURRENT TIMESTAMP"; |
58 | |
} |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
@Override |
70 | |
public String getDateLiteral(String isoDate) { |
71 | 0 | String normalLiteral = super.getDateLiteral(isoDate); |
72 | |
|
73 | 0 | if (isDateOnly(isoDate)) { |
74 | 0 | StringBuffer val = new StringBuffer(); |
75 | 0 | val.append("DATE("); |
76 | 0 | val.append(normalLiteral); |
77 | 0 | val.append(')'); |
78 | 0 | return val.toString(); |
79 | 0 | } else if (isTimeOnly(isoDate)) { |
80 | 0 | StringBuffer val = new StringBuffer(); |
81 | 0 | val.append("TIME("); |
82 | 0 | val.append(normalLiteral); |
83 | 0 | val.append(')'); |
84 | 0 | return val.toString(); |
85 | 0 | } else if (isDateTime(isoDate)) { |
86 | 0 | StringBuffer val = new StringBuffer(); |
87 | 0 | val.append("TIMESTAMP("); |
88 | 0 | val.append(normalLiteral); |
89 | 0 | val.append(')'); |
90 | 0 | return val.toString(); |
91 | |
} else { |
92 | 0 | return "UNSUPPORTED:" + isoDate; |
93 | |
} |
94 | |
} |
95 | |
|
96 | |
@Override |
97 | |
public boolean shouldQuoteValue(String value) { |
98 | 0 | return super.shouldQuoteValue(value) && !value.startsWith("\"SYSIBM\""); |
99 | |
} |
100 | |
|
101 | |
@Override |
102 | |
public boolean supportsTablespaces() { |
103 | 0 | return true; |
104 | |
} |
105 | |
|
106 | |
@Override |
107 | |
public String getViewDefinition(String schemaName, String name) throws DatabaseException { |
108 | 0 | return super.getViewDefinition(schemaName, name).replaceFirst("CREATE VIEW \\w+ AS ", ""); |
109 | |
|
110 | |
|
111 | |
} |
112 | |
|
113 | |
@Override |
114 | |
public java.util.Date parseDate(String dateAsString) throws DateParseException { |
115 | |
try { |
116 | 0 | if (dateAsString.indexOf(' ') > 0) { |
117 | 0 | return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateAsString); |
118 | 0 | } else if (dateAsString.indexOf('.') > 0 && dateAsString.indexOf('-') > 0) { |
119 | 0 | return new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSSSSS").parse(dateAsString); |
120 | |
|
121 | |
} else { |
122 | 0 | if (dateAsString.indexOf(':') > 0) { |
123 | 0 | return new SimpleDateFormat("HH:mm:ss").parse(dateAsString); |
124 | 0 | } else if (dateAsString.indexOf('.') > 0) { |
125 | 0 | return new SimpleDateFormat("HH.mm.ss").parse(dateAsString); |
126 | |
} else { |
127 | 0 | return new SimpleDateFormat("yyyy-MM-dd").parse(dateAsString); |
128 | |
} |
129 | |
} |
130 | 0 | } catch (ParseException e) { |
131 | 0 | throw new DateParseException(dateAsString); |
132 | |
} |
133 | |
} |
134 | |
|
135 | |
@Override |
136 | |
public String convertRequestedSchemaToSchema(String requestedSchema) throws DatabaseException { |
137 | 0 | if (requestedSchema == null) { |
138 | 0 | return getDefaultDatabaseSchemaName(); |
139 | |
} else { |
140 | 0 | return requestedSchema.toUpperCase(); |
141 | |
} |
142 | |
} |
143 | |
|
144 | |
@Override |
145 | |
public String convertRequestedSchemaToCatalog(String requestedSchema) throws DatabaseException { |
146 | 0 | return null; |
147 | |
} |
148 | |
|
149 | |
@Override |
150 | |
public String generatePrimaryKeyName(String tableName) { |
151 | 0 | String pkName = super.generatePrimaryKeyName(tableName); |
152 | 0 | if (pkName.length() > 18) { |
153 | 0 | pkName = pkName.substring(0, 17); |
154 | |
} |
155 | 0 | return pkName; |
156 | |
} |
157 | |
|
158 | |
@Override |
159 | |
public String escapeIndexName(String schemaName, String indexName) { |
160 | |
|
161 | 0 | return super.escapeIndexName(null, indexName); |
162 | |
} |
163 | |
|
164 | |
} |