1 | |
package liquibase.database.jvm; |
2 | |
|
3 | |
import java.sql.CallableStatement; |
4 | |
import java.sql.Connection; |
5 | |
import java.sql.DatabaseMetaData; |
6 | |
import java.sql.PreparedStatement; |
7 | |
import java.sql.SQLException; |
8 | |
import java.sql.SQLWarning; |
9 | |
import java.sql.Savepoint; |
10 | |
import java.sql.Statement; |
11 | |
import java.util.Map; |
12 | |
|
13 | |
import liquibase.database.DatabaseConnection; |
14 | |
import liquibase.exception.DatabaseException; |
15 | |
import liquibase.exception.UnexpectedLiquibaseException; |
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
public class JdbcConnection implements DatabaseConnection { |
23 | |
private java.sql.Connection con; |
24 | |
|
25 | 0 | public JdbcConnection(java.sql.Connection connection) { |
26 | 0 | this.con = connection; |
27 | 0 | } |
28 | |
|
29 | |
@Override |
30 | |
public String getDatabaseProductName() throws DatabaseException { |
31 | |
try { |
32 | 0 | return con.getMetaData().getDatabaseProductName(); |
33 | 0 | } catch (SQLException e) { |
34 | 0 | throw new DatabaseException(e); |
35 | |
} |
36 | |
} |
37 | |
|
38 | |
@Override |
39 | |
public String getDatabaseProductVersion() throws DatabaseException { |
40 | |
try { |
41 | 0 | return con.getMetaData().getDatabaseProductVersion(); |
42 | 0 | } catch (SQLException e) { |
43 | 0 | throw new DatabaseException(e); |
44 | |
} |
45 | |
} |
46 | |
|
47 | |
@Override |
48 | |
public int getDatabaseMajorVersion() throws DatabaseException { |
49 | |
try { |
50 | 0 | return con.getMetaData().getDatabaseMajorVersion(); |
51 | 0 | } catch (SQLException e) { |
52 | 0 | throw new DatabaseException(e); |
53 | |
} |
54 | |
} |
55 | |
|
56 | |
@Override |
57 | |
public int getDatabaseMinorVersion() throws DatabaseException { |
58 | |
try { |
59 | 0 | return con.getMetaData().getDatabaseMinorVersion(); |
60 | 0 | } catch (SQLException e) { |
61 | 0 | throw new DatabaseException(e); |
62 | |
} |
63 | |
} |
64 | |
|
65 | |
@Override |
66 | |
public String getURL() { |
67 | |
try { |
68 | 0 | return con.getMetaData().getURL(); |
69 | 0 | } catch (SQLException e) { |
70 | 0 | throw new UnexpectedLiquibaseException(e); |
71 | |
} |
72 | |
} |
73 | |
|
74 | |
@Override |
75 | |
public String getConnectionUserName() { |
76 | |
try { |
77 | 0 | return con.getMetaData().getUserName(); |
78 | 0 | } catch (SQLException e) { |
79 | 0 | throw new UnexpectedLiquibaseException(e); |
80 | |
} |
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public Connection getWrappedConnection() { |
89 | 0 | return con; |
90 | |
} |
91 | |
|
92 | |
public void clearWarnings() throws DatabaseException { |
93 | |
try { |
94 | 0 | con.clearWarnings(); |
95 | 0 | } catch (SQLException e) { |
96 | 0 | throw new DatabaseException(e); |
97 | 0 | } |
98 | 0 | } |
99 | |
|
100 | |
@Override |
101 | |
public void close() throws DatabaseException { |
102 | 0 | rollback(); |
103 | |
try { |
104 | 0 | con.close(); |
105 | 0 | } catch (SQLException e) { |
106 | 0 | throw new DatabaseException(e); |
107 | 0 | } |
108 | 0 | } |
109 | |
|
110 | |
@Override |
111 | |
public void commit() throws DatabaseException { |
112 | |
try { |
113 | 0 | if (!con.getAutoCommit()) { |
114 | 0 | con.commit(); |
115 | |
} |
116 | 0 | } catch (SQLException e) { |
117 | 0 | throw new DatabaseException(e); |
118 | 0 | } |
119 | 0 | } |
120 | |
|
121 | |
public Statement createStatement() throws DatabaseException { |
122 | |
try { |
123 | 0 | return con.createStatement(); |
124 | 0 | } catch (SQLException e) { |
125 | 0 | throw new DatabaseException(e); |
126 | |
} |
127 | |
} |
128 | |
|
129 | |
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) |
130 | |
throws DatabaseException { |
131 | |
try { |
132 | 0 | return con.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability); |
133 | 0 | } catch (SQLException e) { |
134 | 0 | throw new DatabaseException(e); |
135 | |
} |
136 | |
} |
137 | |
|
138 | |
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws DatabaseException { |
139 | |
try { |
140 | 0 | return con.createStatement(resultSetType, resultSetConcurrency); |
141 | 0 | } catch (SQLException e) { |
142 | 0 | throw new DatabaseException(e); |
143 | |
} |
144 | |
} |
145 | |
|
146 | |
@Override |
147 | |
public boolean getAutoCommit() throws DatabaseException { |
148 | |
try { |
149 | 0 | return con.getAutoCommit(); |
150 | 0 | } catch (SQLException e) { |
151 | 0 | throw new DatabaseException(e); |
152 | |
} |
153 | |
} |
154 | |
|
155 | |
@Override |
156 | |
public String getCatalog() throws DatabaseException { |
157 | |
try { |
158 | 0 | return con.getCatalog(); |
159 | 0 | } catch (SQLException e) { |
160 | 0 | throw new DatabaseException(e); |
161 | |
} |
162 | |
} |
163 | |
|
164 | |
public int getHoldability() throws DatabaseException { |
165 | |
try { |
166 | 0 | return con.getHoldability(); |
167 | 0 | } catch (SQLException e) { |
168 | 0 | throw new DatabaseException(e); |
169 | |
} |
170 | |
} |
171 | |
|
172 | |
public DatabaseMetaData getMetaData() throws DatabaseException { |
173 | |
try { |
174 | 0 | return con.getMetaData(); |
175 | 0 | } catch (SQLException e) { |
176 | 0 | throw new DatabaseException(e); |
177 | |
} |
178 | |
} |
179 | |
|
180 | |
public int getTransactionIsolation() throws DatabaseException { |
181 | |
try { |
182 | 0 | return con.getTransactionIsolation(); |
183 | 0 | } catch (SQLException e) { |
184 | 0 | throw new DatabaseException(e); |
185 | |
} |
186 | |
} |
187 | |
|
188 | |
public Map<String, Class<?>> getTypeMap() throws DatabaseException { |
189 | |
try { |
190 | 0 | return con.getTypeMap(); |
191 | 0 | } catch (SQLException e) { |
192 | 0 | throw new DatabaseException(e); |
193 | |
} |
194 | |
} |
195 | |
|
196 | |
public SQLWarning getWarnings() throws DatabaseException { |
197 | |
try { |
198 | 0 | return con.getWarnings(); |
199 | 0 | } catch (SQLException e) { |
200 | 0 | throw new DatabaseException(e); |
201 | |
} |
202 | |
} |
203 | |
|
204 | |
@Override |
205 | |
public boolean isClosed() throws DatabaseException { |
206 | |
try { |
207 | 0 | return con.isClosed(); |
208 | 0 | } catch (SQLException e) { |
209 | 0 | throw new DatabaseException(e); |
210 | |
} |
211 | |
} |
212 | |
|
213 | |
public boolean isReadOnly() throws DatabaseException { |
214 | |
try { |
215 | 0 | return con.isReadOnly(); |
216 | 0 | } catch (SQLException e) { |
217 | 0 | throw new DatabaseException(e); |
218 | |
} |
219 | |
} |
220 | |
|
221 | |
@Override |
222 | |
public String nativeSQL(String sql) throws DatabaseException { |
223 | |
try { |
224 | 0 | return con.nativeSQL(sql); |
225 | 0 | } catch (SQLException e) { |
226 | 0 | throw new DatabaseException(e); |
227 | |
} |
228 | |
} |
229 | |
|
230 | |
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, |
231 | |
int resultSetHoldability) throws DatabaseException { |
232 | |
try { |
233 | 0 | return con.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability); |
234 | 0 | } catch (SQLException e) { |
235 | 0 | throw new DatabaseException(e); |
236 | |
} |
237 | |
} |
238 | |
|
239 | |
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) |
240 | |
throws DatabaseException { |
241 | |
try { |
242 | 0 | return con.prepareCall(sql, resultSetType, resultSetConcurrency); |
243 | 0 | } catch (SQLException e) { |
244 | 0 | throw new DatabaseException(e); |
245 | |
} |
246 | |
} |
247 | |
|
248 | |
public CallableStatement prepareCall(String sql) throws DatabaseException { |
249 | |
try { |
250 | 0 | return con.prepareCall(sql); |
251 | 0 | } catch (SQLException e) { |
252 | 0 | throw new DatabaseException(e); |
253 | |
} |
254 | |
} |
255 | |
|
256 | |
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, |
257 | |
int resultSetHoldability) throws DatabaseException { |
258 | |
try { |
259 | 0 | return con.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability); |
260 | 0 | } catch (SQLException e) { |
261 | 0 | throw new DatabaseException(e); |
262 | |
} |
263 | |
} |
264 | |
|
265 | |
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) |
266 | |
throws DatabaseException { |
267 | |
try { |
268 | 0 | return con.prepareStatement(sql, resultSetType, resultSetConcurrency); |
269 | 0 | } catch (SQLException e) { |
270 | 0 | throw new DatabaseException(e); |
271 | |
} |
272 | |
} |
273 | |
|
274 | |
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws DatabaseException { |
275 | |
try { |
276 | 0 | return con.prepareStatement(sql, autoGeneratedKeys); |
277 | 0 | } catch (SQLException e) { |
278 | 0 | throw new DatabaseException(e); |
279 | |
} |
280 | |
} |
281 | |
|
282 | |
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws DatabaseException { |
283 | |
try { |
284 | 0 | return con.prepareStatement(sql, columnIndexes); |
285 | 0 | } catch (SQLException e) { |
286 | 0 | throw new DatabaseException(e); |
287 | |
} |
288 | |
} |
289 | |
|
290 | |
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws DatabaseException { |
291 | |
try { |
292 | 0 | return con.prepareStatement(sql, columnNames); |
293 | 0 | } catch (SQLException e) { |
294 | 0 | throw new DatabaseException(e); |
295 | |
} |
296 | |
} |
297 | |
|
298 | |
public PreparedStatement prepareStatement(String sql) throws DatabaseException { |
299 | |
try { |
300 | 0 | return con.prepareStatement(sql); |
301 | 0 | } catch (SQLException e) { |
302 | 0 | throw new DatabaseException(e); |
303 | |
} |
304 | |
} |
305 | |
|
306 | |
public void releaseSavepoint(Savepoint savepoint) throws DatabaseException { |
307 | |
try { |
308 | 0 | con.releaseSavepoint(savepoint); |
309 | 0 | } catch (SQLException e) { |
310 | 0 | throw new DatabaseException(e); |
311 | 0 | } |
312 | 0 | } |
313 | |
|
314 | |
@Override |
315 | |
public void rollback() throws DatabaseException { |
316 | |
try { |
317 | 0 | if (!con.getAutoCommit() && !con.isClosed()) { |
318 | 0 | con.rollback(); |
319 | |
} |
320 | 0 | } catch (SQLException e) { |
321 | 0 | throw new DatabaseException(e); |
322 | 0 | } |
323 | 0 | } |
324 | |
|
325 | |
public void rollback(Savepoint savepoint) throws DatabaseException { |
326 | |
try { |
327 | 0 | if (!con.getAutoCommit()) { |
328 | 0 | con.rollback(savepoint); |
329 | |
} |
330 | 0 | } catch (SQLException e) { |
331 | 0 | throw new DatabaseException(e); |
332 | 0 | } |
333 | 0 | } |
334 | |
|
335 | |
@Override |
336 | |
public void setAutoCommit(boolean autoCommit) throws DatabaseException { |
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
try { |
342 | 0 | con.setAutoCommit(autoCommit); |
343 | 0 | } catch (SQLException e) { |
344 | 0 | throw new DatabaseException(e); |
345 | 0 | } |
346 | |
|
347 | 0 | } |
348 | |
|
349 | |
public void setCatalog(String catalog) throws DatabaseException { |
350 | |
try { |
351 | 0 | con.setCatalog(catalog); |
352 | 0 | } catch (SQLException e) { |
353 | 0 | throw new DatabaseException(e); |
354 | 0 | } |
355 | 0 | } |
356 | |
|
357 | |
public void setHoldability(int holdability) throws DatabaseException { |
358 | |
try { |
359 | 0 | con.setHoldability(holdability); |
360 | 0 | } catch (SQLException e) { |
361 | 0 | throw new DatabaseException(e); |
362 | 0 | } |
363 | 0 | } |
364 | |
|
365 | |
public void setReadOnly(boolean readOnly) throws DatabaseException { |
366 | |
try { |
367 | 0 | con.setReadOnly(readOnly); |
368 | 0 | } catch (SQLException e) { |
369 | 0 | throw new DatabaseException(e); |
370 | 0 | } |
371 | 0 | } |
372 | |
|
373 | |
public Savepoint setSavepoint() throws DatabaseException { |
374 | |
try { |
375 | 0 | return con.setSavepoint(); |
376 | 0 | } catch (SQLException e) { |
377 | 0 | throw new DatabaseException(e); |
378 | |
} |
379 | |
} |
380 | |
|
381 | |
public Savepoint setSavepoint(String name) throws DatabaseException { |
382 | |
try { |
383 | 0 | return con.setSavepoint(name); |
384 | 0 | } catch (SQLException e) { |
385 | 0 | throw new DatabaseException(e); |
386 | |
} |
387 | |
} |
388 | |
|
389 | |
public void setTransactionIsolation(int level) throws DatabaseException { |
390 | |
try { |
391 | 0 | con.setTransactionIsolation(level); |
392 | 0 | } catch (SQLException e) { |
393 | 0 | throw new DatabaseException(e); |
394 | 0 | } |
395 | 0 | } |
396 | |
|
397 | |
public void setTypeMap(Map<String, Class<?>> map) throws DatabaseException { |
398 | |
try { |
399 | 0 | con.setTypeMap(map); |
400 | 0 | } catch (SQLException e) { |
401 | 0 | throw new DatabaseException(e); |
402 | 0 | } |
403 | 0 | } |
404 | |
|
405 | |
public Connection getUnderlyingConnection() { |
406 | 0 | return con; |
407 | |
} |
408 | |
|
409 | |
@Override |
410 | |
public boolean equals(Object obj) { |
411 | 0 | return obj instanceof JdbcConnection |
412 | |
&& this.getUnderlyingConnection().equals(((JdbcConnection) obj).getUnderlyingConnection()); |
413 | |
|
414 | |
} |
415 | |
|
416 | |
@Override |
417 | |
public int hashCode() { |
418 | 0 | return this.getUnderlyingConnection().hashCode(); |
419 | |
} |
420 | |
} |