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