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 package org.bitrepository.service.database;
26
27 import org.bitrepository.common.ArgumentValidator;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import java.sql.Connection;
32 import java.sql.PreparedStatement;
33 import java.sql.ResultSet;
34 import java.sql.SQLException;
35 import java.sql.Timestamp;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Date;
39 import java.util.List;
40
41
42
43
44 public class DatabaseUtils {
45
46 private static Logger log = LoggerFactory.getLogger(DatabaseUtils.class);
47
48
49 public static final String DERBY_EMBEDDED_DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
50
51
52 private DatabaseUtils() { }
53
54
55
56
57
58
59
60
61
62
63 public static Integer selectIntValue(DBConnector dbConnector, String query, Object... args) {
64 ArgumentValidator.checkNotNull(dbConnector, "DBConnector dbConnector");
65 ArgumentValidator.checkNotNullOrEmpty(query, "String query");
66 ArgumentValidator.checkNotNull(args, "Object... args");
67
68 PreparedStatement ps = null;
69 ResultSet res = null;
70 Connection conn = null;
71 try {
72 try {
73 conn = dbConnector.getConnection();
74 ps = createPreparedStatement(conn, query, args);
75 res = ps.executeQuery();
76 if (!res.next()) {
77 throw new IllegalStateException("No results from " + ps);
78 }
79 Integer resultInt = res.getInt(1);
80 if (res.wasNull()) {
81 resultInt = null;
82 }
83 if (res.next()) {
84 throw new IllegalStateException("Too many results from " + ps);
85 }
86 return resultInt;
87 } finally {
88 if(res != null) {
89 res.close();
90 }
91 if(ps != null) {
92 ps.close();
93 }
94 if(conn != null) {
95 conn.close();
96 }
97 }
98 } catch (SQLException e) {
99 throw failedExecutionOfStatement(e, conn, query, args);
100 }
101 }
102
103
104
105
106
107
108
109
110
111
112 public static Long selectLongValue(DBConnector dbConnector, String query, Object... args) {
113 ArgumentValidator.checkNotNull(dbConnector, "DBConnector dbConnector");
114 ArgumentValidator.checkNotNullOrEmpty(query, "String query");
115 ArgumentValidator.checkNotNull(args, "Object... args");
116
117 PreparedStatement ps = null;
118 ResultSet res = null;
119 Connection conn = null;
120 try {
121 try {
122 conn = dbConnector.getConnection();
123 ps = createPreparedStatement(conn, query, args);
124 res = ps.executeQuery();
125 if (!res.next()) {
126 log.trace("Got an empty result set for statement '" + query + "' with arguments '"
127 + Arrays.asList(args) + "' on database '" + conn + "'. Returning a null.");
128 return null;
129 }
130 Long resultLong = res.getLong(1);
131 if (res.wasNull()) {
132 resultLong = null;
133 }
134 if (res.next()) {
135 throw new IllegalStateException("Too many results from " + ps);
136 }
137 return resultLong;
138 } finally {
139 if(res != null) {
140 res.close();
141 }
142 if(ps != null) {
143 ps.close();
144 }
145 if(conn != null) {
146 conn.close();
147 }
148 }
149 } catch (SQLException e) {
150 throw failedExecutionOfStatement(e, conn, query, args);
151 }
152 }
153
154
155
156
157
158
159
160
161
162
163
164 public static Long selectFirstLongValue(DBConnector dbConnector, String query, Object... args) {
165 ArgumentValidator.checkNotNull(dbConnector, "DBConnector dbConnector");
166 ArgumentValidator.checkNotNullOrEmpty(query, "String query");
167 ArgumentValidator.checkNotNull(args, "Object... args");
168
169 PreparedStatement ps = null;
170 ResultSet res = null;
171 Connection conn = null;
172 try {
173 try {
174 conn = dbConnector.getConnection();
175 ps = createPreparedStatement(conn, query, args);
176 res = ps.executeQuery();
177 if (!res.next()) {
178 log.trace("Got an empty result set for statement '" + query + "' with arguments '"
179 + Arrays.asList(args) + "' on database '" + conn + "'. Returning a null.");
180 return null;
181 }
182 Long resultLong = res.getLong(1);
183 if (res.wasNull()) {
184 resultLong = null;
185 }
186 return resultLong;
187 } finally {
188 if(res != null) {
189 res.close();
190 }
191 if(ps != null) {
192 ps.close();
193 }
194 if(conn != null) {
195 conn.close();
196 }
197 }
198 } catch (SQLException e) {
199 throw failedExecutionOfStatement(e, conn, query, args);
200 }
201 }
202
203
204
205
206
207
208
209
210
211
212 public static List<Long> selectLongList(DBConnector dbConnector, String query, Object... args) {
213 ArgumentValidator.checkNotNull(dbConnector, "DBConnector dbConnector");
214 ArgumentValidator.checkNotNullOrEmpty(query, "String query");
215 ArgumentValidator.checkNotNull(args, "Object... args");
216
217 PreparedStatement ps = null;
218 ResultSet res = null;
219 Connection conn = null;
220 try {
221 List<Long> list = new ArrayList<Long>();
222 try {
223 conn = dbConnector.getConnection();
224 ps = createPreparedStatement(conn, query, args);
225 res = ps.executeQuery();
226
227 while(res.next()) {
228 list.add(res.getLong(1));
229 }
230 return list;
231 } finally {
232 if(res != null) {
233 res.close();
234 }
235 if(ps != null) {
236 ps.close();
237 }
238 if(conn != null) {
239 conn.close();
240 }
241 }
242 } catch (SQLException e) {
243 throw failedExecutionOfStatement(e, conn, query, args);
244 }
245 }
246
247
248
249
250
251
252
253
254
255
256 public static Date selectDateValue(DBConnector dbConnector, String query, Object... args) {
257 return retrieveDateValue(dbConnector, true, query, args);
258 }
259
260
261
262
263
264
265
266
267
268
269
270 public static Date selectFirstDateValue(DBConnector dbConnector, String query, Object... args) {
271 return retrieveDateValue(dbConnector, false, query, args);
272 }
273
274
275
276
277
278
279
280
281
282 private static Date retrieveDateValue(DBConnector dbConnector, boolean mustHaveOnlyOneResult, String query, Object... args) {
283 ArgumentValidator.checkNotNull(dbConnector, "DBConnector dbConnector");
284 ArgumentValidator.checkNotNullOrEmpty(query, "String query");
285 ArgumentValidator.checkNotNull(args, "Object... args");
286
287 PreparedStatement ps = null;
288 ResultSet res = null;
289 Connection conn = null;
290 try {
291 try {
292 conn = dbConnector.getConnection();
293 ps = createPreparedStatement(conn, query, args);
294
295 res = ps.executeQuery();
296 if (!res.next()) {
297 log.trace("Got an empty result set for statement '" + query + "' on database '"
298 + conn + "'. Returning a null.");
299 return null;
300 }
301 Timestamp resultDate = res.getTimestamp(1);
302 if (res.wasNull()) {
303 resultDate = null;
304 }
305 if (mustHaveOnlyOneResult && res.next()) {
306 throw new IllegalStateException("Too many results from " + ps);
307 }
308 return resultDate;
309 } finally {
310 if(res != null) {
311 res.close();
312 }
313 if(ps != null) {
314 ps.close();
315 }
316 if(conn != null) {
317 conn.close();
318 }
319 }
320 } catch (SQLException e) {
321 throw failedExecutionOfStatement(e, conn, query, args);
322 }
323 }
324
325
326
327
328
329
330
331
332
333
334 public static String selectStringValue(DBConnector dbConnector, String query, Object... args) {
335 ArgumentValidator.checkNotNull(dbConnector, "DBConnector dbConnector");
336 ArgumentValidator.checkNotNullOrEmpty(query, "String query");
337 ArgumentValidator.checkNotNull(args, "Object... args");
338
339 PreparedStatement ps = null;
340 ResultSet res = null;
341 Connection conn = null;
342 try {
343 try {
344 conn = dbConnector.getConnection();
345 ps = createPreparedStatement(conn, query, args);
346 res = ps.executeQuery();
347
348 if(!res.next()) {
349 log.trace("No string was found for the query '" + query + "'. A null has been returned.");
350 return null;
351 }
352
353 return res.getString(1);
354 } finally {
355 if(res != null) {
356 res.close();
357 }
358 if(ps != null) {
359 ps.close();
360 }
361 if(conn != null) {
362 conn.close();
363 }
364 }
365 } catch (SQLException e) {
366 throw failedExecutionOfStatement(e, conn, query, args);
367 }
368 }
369
370
371
372
373
374
375
376
377
378
379 public static List<String> selectStringList(DBConnector dbConnector, String query, Object... args) {
380 ArgumentValidator.checkNotNull(dbConnector, "DBConnector dbConnector");
381 ArgumentValidator.checkNotNullOrEmpty(query, "String query");
382 ArgumentValidator.checkNotNull(args, "Object... args");
383
384 PreparedStatement ps = null;
385 ResultSet res = null;
386 Connection conn = null;
387 try {
388 try {
389 conn = dbConnector.getConnection();
390 ps = createPreparedStatement(conn, query, args);
391 res = ps.executeQuery();
392
393 List<String> list = new ArrayList<String>();
394 while(res.next()) {
395 list.add(res.getString(1));
396 }
397 return list;
398 } finally {
399 if(res != null) {
400 res.close();
401 }
402 if(ps != null) {
403 ps.close();
404 }
405 if(conn != null) {
406 conn.close();
407 }
408 }
409 } catch (SQLException e) {
410 throw failedExecutionOfStatement(e, conn, query, args);
411 }
412 }
413
414
415
416
417
418
419
420
421
422 public static void executeStatement(DBConnector dbConnector, String query, Object... args) {
423 ArgumentValidator.checkNotNull(dbConnector, "DBConnector dbConnector");
424 ArgumentValidator.checkNotNullOrEmpty(query, "String query");
425 ArgumentValidator.checkNotNull(args, "Object... args");
426
427 PreparedStatement ps = null;
428 ResultSet res = null;
429 Connection conn = null;
430 try {
431 try {
432 conn = dbConnector.getConnection();
433 ps = createPreparedStatement(conn, query, args);
434 ps.executeUpdate();
435 conn.commit();
436 } finally {
437 if(res != null) {
438 res.close();
439 }
440 if(ps != null) {
441 ps.close();
442 }
443 if(conn != null) {
444 conn.close();
445 }
446 }
447 } catch (SQLException e) {
448 throw failedExecutionOfStatement(e, conn, query, args);
449 }
450 }
451
452
453
454
455
456
457
458
459
460
461
462
463 public static PreparedStatement createPreparedStatement(Connection dbConnection, String query, Object... args)
464 throws SQLException {
465 log.trace("Preparing the statement: '" + query + "' with arguments '" + Arrays.asList(args) + "'");
466 PreparedStatement s = dbConnection.prepareStatement(query);
467 int i = 1;
468 for (Object arg : args) {
469 if (arg instanceof String) {
470 s.setString(i, (String) arg);
471 } else if (arg instanceof Integer) {
472 s.setInt(i, (Integer) arg);
473 } else if (arg instanceof Long) {
474 s.setLong(i, (Long) arg);
475 } else if (arg instanceof Boolean) {
476 s.setBoolean(i, (Boolean) arg);
477 } else if (arg instanceof Date) {
478 s.setTimestamp(i, new Timestamp(((Date) arg).getTime()));
479 } else {
480 if(arg == null) {
481 throw new IllegalStateException("Cannot handle a null as argument for SQL query. We can only "
482 + "handle string, int, long, date or boolean args for query: " + query);
483 } else {
484 throw new IllegalStateException("Cannot handle type '" + arg.getClass().getName() + "'. We can only "
485 + "handle string, int, long, date or boolean args for query: " + query);
486 }
487 }
488 i++;
489 }
490
491 return s;
492 }
493
494
495
496
497
498
499
500
501
502
503 private static IllegalStateException failedExecutionOfStatement(Throwable e, Connection dbConnection,
504 String query, Object... args) {
505 return new IllegalStateException("Could not execute the query '" + query + "' with the arguments '"
506 + Arrays.asList(args) + "' on database '" + dbConnection + "'", e);
507 }
508 }