1 | |
package liquibase.database.core; |
2 | |
|
3 | |
import java.lang.reflect.Method; |
4 | |
import java.sql.Driver; |
5 | |
|
6 | |
import liquibase.database.AbstractDatabase; |
7 | |
import liquibase.database.DatabaseConnection; |
8 | |
import liquibase.exception.DatabaseException; |
9 | |
import liquibase.logging.LogFactory; |
10 | |
import liquibase.logging.Logger; |
11 | |
|
12 | |
public class DerbyDatabase extends AbstractDatabase { |
13 | |
|
14 | 9 | private Logger log = LogFactory.getLogger(); |
15 | |
|
16 | |
protected int driverVersionMajor; |
17 | |
protected int driverVersionMinor; |
18 | |
|
19 | 9 | public DerbyDatabase() { |
20 | 9 | determineDriverVersion(); |
21 | 9 | } |
22 | |
|
23 | |
@Override |
24 | |
public boolean isCorrectDatabaseImplementation(DatabaseConnection conn) throws DatabaseException { |
25 | 0 | return "Apache Derby".equalsIgnoreCase(conn.getDatabaseProductName()); |
26 | |
} |
27 | |
|
28 | |
@Override |
29 | |
public String getDefaultDriver(String url) { |
30 | 2 | if (url.startsWith("jdbc:derby")) { |
31 | 1 | return "org.apache.derby.jdbc.EmbeddedDriver"; |
32 | |
} |
33 | 1 | return null; |
34 | |
} |
35 | |
|
36 | |
@Override |
37 | |
public int getPriority() { |
38 | 0 | return PRIORITY_DEFAULT; |
39 | |
} |
40 | |
|
41 | |
@Override |
42 | |
public String getTypeName() { |
43 | 55 | return "derby"; |
44 | |
} |
45 | |
|
46 | |
@Override |
47 | |
protected String getDefaultDatabaseSchemaName() throws DatabaseException { |
48 | 0 | return super.getDefaultDatabaseSchemaName().toUpperCase(); |
49 | |
} |
50 | |
|
51 | |
@Override |
52 | |
public boolean supportsSequences() { |
53 | 8 | if ((driverVersionMajor == 10 && driverVersionMinor >= 6) || driverVersionMajor >= 11) { |
54 | 0 | return true; |
55 | |
} else { |
56 | 8 | return false; |
57 | |
} |
58 | |
} |
59 | |
|
60 | |
@Override |
61 | |
public boolean supportsInitiallyDeferrableColumns() { |
62 | 0 | return false; |
63 | |
} |
64 | |
|
65 | |
@Override |
66 | |
public String getCurrentDateTimeFunction() { |
67 | 0 | if (currentDateTimeFunction != null) { |
68 | 0 | return currentDateTimeFunction; |
69 | |
} |
70 | |
|
71 | 0 | return "CURRENT_TIMESTAMP"; |
72 | |
} |
73 | |
|
74 | |
@Override |
75 | |
public String getAutoIncrementClause() { |
76 | 0 | return "GENERATED BY DEFAULT AS IDENTITY"; |
77 | |
} |
78 | |
|
79 | |
@Override |
80 | |
public String getDateLiteral(String isoDate) { |
81 | 4 | if (isDateOnly(isoDate)) { |
82 | 0 | return "DATE(" + super.getDateLiteral(isoDate) + ")"; |
83 | 4 | } else if (isTimeOnly(isoDate)) { |
84 | 0 | return "TIME(" + super.getDateLiteral(isoDate) + ")"; |
85 | |
} else { |
86 | 4 | String dateString = super.getDateLiteral(isoDate); |
87 | 4 | int decimalDigits = dateString.length() - dateString.indexOf('.') - 2; |
88 | 4 | String padding = ""; |
89 | 16 | for (int i = 6; i > decimalDigits; i--) { |
90 | 12 | padding += "0"; |
91 | |
} |
92 | 4 | return "TIMESTAMP(" + dateString.replaceFirst("'$", padding + "'") + ")"; |
93 | |
} |
94 | |
} |
95 | |
|
96 | |
@Override |
97 | |
public boolean supportsTablespaces() { |
98 | 0 | return false; |
99 | |
} |
100 | |
|
101 | |
@Override |
102 | |
public String getViewDefinition(String schemaName, String name) throws DatabaseException { |
103 | 0 | return super.getViewDefinition(schemaName, name).replaceFirst("CREATE VIEW \\w+ AS ", ""); |
104 | |
} |
105 | |
|
106 | |
@Override |
107 | |
public void close() throws DatabaseException { |
108 | 0 | String url = getConnection().getURL(); |
109 | 0 | String driverName = getDefaultDriver(url); |
110 | 0 | super.close(); |
111 | 0 | if (driverName.toLowerCase().contains("embedded")) { |
112 | |
try { |
113 | 0 | if (url.contains(";")) { |
114 | 0 | url = url.substring(0, url.indexOf(";")) + ";shutdown=true"; |
115 | |
} else { |
116 | 0 | url += ";shutdown=true"; |
117 | |
} |
118 | 0 | LogFactory.getLogger().info("Shutting down derby connection: " + url); |
119 | |
|
120 | 0 | ((Driver) Class.forName(driverName).newInstance()).connect(url, null); |
121 | 0 | } catch (Exception e) { |
122 | 0 | LogFactory.getLogger().severe("Error closing derby cleanly", e); |
123 | 0 | } |
124 | |
} |
125 | 0 | } |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
@SuppressWarnings({ "static-access", "unchecked" }) |
131 | |
protected void determineDriverVersion() { |
132 | |
try { |
133 | |
|
134 | |
@SuppressWarnings("rawtypes") |
135 | 9 | final Class sysinfoClass = getClass().forName("org.apache.derby.tools.sysinfo"); |
136 | 0 | final Method majorVersionGetter = sysinfoClass.getMethod("getMajorVersion"); |
137 | 0 | final Method minorVersionGetter = sysinfoClass.getMethod("getMinorVersion"); |
138 | 0 | driverVersionMajor = ((Integer) majorVersionGetter.invoke(null)).intValue(); |
139 | 0 | driverVersionMinor = ((Integer) minorVersionGetter.invoke(null)).intValue(); |
140 | 9 | } catch (Exception e) { |
141 | 9 | log.debug("Unable to load/access Apache Derby driver class " |
142 | |
+ "org.apache.derby.tools.sysinfo to check version: " + e.getMessage()); |
143 | 9 | driverVersionMajor = -1; |
144 | 9 | driverVersionMinor = -1; |
145 | 0 | } |
146 | 9 | } |
147 | |
} |