1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.jdbc;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.sql.Connection;
21 import java.sql.SQLException;
22 import java.sql.Statement;
23 import java.util.List;
24
25 import javax.sql.DataSource;
26
27 import org.kuali.common.jdbc.supplier.SqlSupplier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.jdbc.datasource.DataSourceUtils;
31 import org.springframework.util.Assert;
32
33 public class JdbcUtils {
34
35 private static final Logger logger = LoggerFactory.getLogger(JdbcUtils.class);
36
37
38
39
40 public static long getSqlSize(List<SqlSupplier> suppliers) {
41 long size = 0;
42 for (SqlSupplier supplier : suppliers) {
43 SqlMetaData smd = supplier.getMetaData();
44 size += smd.getSize();
45 }
46 return size;
47 }
48
49
50
51
52 public static long getSqlCount(List<SqlSupplier> suppliers) {
53 long count = 0;
54 for (SqlSupplier supplier : suppliers) {
55 SqlMetaData smd = supplier.getMetaData();
56 count += smd.getCount();
57 }
58 return count;
59 }
60
61 public static final void closeQuietly(DataSource dataSource, Connection conn, Statement statement) {
62 closeQuietly(statement);
63 closeQuietly(dataSource, conn);
64 }
65
66 public static final void closeQuietly(Statement statement) {
67 if (statement == null) {
68 return;
69 }
70 try {
71 statement.close();
72 } catch (SQLException e) {
73 throw new IllegalStateException(e);
74 }
75 }
76
77 public static final void closeQuietly(DataSource dataSource, Connection conn) {
78 if (conn == null && dataSource == null) {
79 return;
80 }
81 Assert.notNull(dataSource, "dataSource is null but conn is not");
82 try {
83 logger.trace("releasing connection");
84 DataSourceUtils.doReleaseConnection(conn, dataSource);
85 } catch (SQLException e) {
86 throw new IllegalStateException(e);
87 }
88 }
89
90 public static SqlMetaData getSqlMetaData(BufferedReader in, SqlReader reader) throws IOException {
91 long count = 0;
92 long size = 0;
93 List<String> sql = reader.getSql(in);
94 while (sql != null) {
95 for (String s : sql) {
96 count++;
97 size += s.length();
98 }
99 sql = reader.getSql(in);
100 }
101 return new SqlMetaData(count, size);
102 }
103
104 }