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 | |
|
26 | |
|
27 | |
|
28 | |
package net.mtu.eggplant.util.sql; |
29 | |
|
30 | |
import java.sql.Connection; |
31 | |
import java.sql.DatabaseMetaData; |
32 | |
import java.sql.PreparedStatement; |
33 | |
import java.sql.ResultSet; |
34 | |
import java.sql.SQLException; |
35 | |
import java.sql.Statement; |
36 | |
import java.sql.Types; |
37 | |
import java.util.Collection; |
38 | |
import java.util.LinkedList; |
39 | |
|
40 | |
import org.slf4j.Logger; |
41 | |
import org.slf4j.LoggerFactory; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public final class SQLFunctions { |
49 | |
|
50 | 0 | private static final Logger LOGGER = LoggerFactory.getLogger(SQLFunctions.class); |
51 | |
|
52 | 0 | private SQLFunctions() { |
53 | 0 | } |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public static Class<?> getClassForType(final int type) { |
59 | |
try { |
60 | 0 | switch (type) { |
61 | |
case Types.CHAR: |
62 | |
case Types.LONGVARCHAR: |
63 | |
case Types.VARCHAR: |
64 | 0 | return Class.forName("java.lang.String"); |
65 | |
case Types.DECIMAL: |
66 | |
case Types.NUMERIC: |
67 | 0 | return Class.forName("java.math.BigDecimal"); |
68 | |
case Types.BIT: |
69 | 0 | return Class.forName("java.lang.Boolean"); |
70 | |
case Types.SMALLINT: |
71 | |
case Types.TINYINT: |
72 | |
case Types.INTEGER: |
73 | 0 | return Class.forName("java.lang.Integer"); |
74 | |
case Types.BIGINT: |
75 | 0 | return Class.forName("java.lang.Long"); |
76 | |
case Types.REAL: |
77 | 0 | return Class.forName("java.lang.Float"); |
78 | |
case Types.DOUBLE: |
79 | |
case Types.FLOAT: |
80 | 0 | return Class.forName("java.lang.Double"); |
81 | |
case Types.BINARY: |
82 | |
case Types.LONGVARBINARY: |
83 | |
case Types.VARBINARY: |
84 | 0 | return (new byte[0]).getClass(); |
85 | |
case Types.DATE: |
86 | 0 | return Class.forName("java.sql.Date"); |
87 | |
case Types.TIME: |
88 | 0 | return Class.forName("java.sql.Time"); |
89 | |
case Types.TIMESTAMP: |
90 | 0 | return Class.forName("java.sql.Timestamp"); |
91 | |
case Types.NULL: |
92 | 0 | return Class.forName("java.lang.Void"); |
93 | |
case Types.OTHER: |
94 | |
default: |
95 | 0 | return Class.forName("java.lang.Object"); |
96 | |
} |
97 | 0 | } catch (final ClassNotFoundException cnfe) { |
98 | 0 | throw new RuntimeException("Can't find standard class type " |
99 | |
+ cnfe); |
100 | |
} |
101 | |
} |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public static void close(final Statement stmt) { |
108 | |
try { |
109 | 0 | if (null != stmt) { |
110 | 0 | stmt.close(); |
111 | |
} |
112 | 0 | } catch (final SQLException sqle) { |
113 | |
|
114 | 0 | if (LOGGER.isDebugEnabled()) { |
115 | 0 | LOGGER.debug("exception closing statement", sqle); |
116 | |
} |
117 | 0 | } |
118 | 0 | } |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public static void close(final PreparedStatement prep) { |
125 | |
try { |
126 | 0 | if (null != prep) { |
127 | 0 | prep.close(); |
128 | |
} |
129 | 0 | } catch (final SQLException sqle) { |
130 | |
|
131 | 0 | if (LOGGER.isDebugEnabled()) { |
132 | 0 | LOGGER.debug("exception closing prepared statement", sqle); |
133 | |
} |
134 | 0 | } |
135 | 0 | } |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public static void close(final ResultSet rs) { |
142 | |
try { |
143 | 0 | if (null != rs) { |
144 | 0 | rs.close(); |
145 | |
} |
146 | 0 | } catch (final SQLException sqle) { |
147 | |
|
148 | 0 | if (LOGGER.isDebugEnabled()) { |
149 | 0 | LOGGER.debug("exception closing result set", sqle); |
150 | |
} |
151 | 0 | } |
152 | 0 | } |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public static void close(final Connection connection) { |
159 | |
try { |
160 | 0 | if (null != connection) { |
161 | 0 | connection.close(); |
162 | |
} |
163 | 0 | } catch (final SQLException sqle) { |
164 | |
|
165 | 0 | if (LOGGER.isDebugEnabled()) { |
166 | 0 | LOGGER.debug("exception closing connection", sqle); |
167 | |
} |
168 | 0 | } |
169 | 0 | } |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
public static Collection<String> getTablesInDB(final Connection connection) throws SQLException { |
180 | 0 | final Collection<String> tables = new LinkedList<String>(); |
181 | 0 | ResultSet rs = null; |
182 | |
try { |
183 | |
|
184 | 0 | final DatabaseMetaData metadata = connection.getMetaData(); |
185 | 0 | rs = metadata.getTables(null, null, "%", new String[] { "TABLE" }); |
186 | 0 | while (rs.next()) { |
187 | 0 | final String tableName = rs.getString("TABLE_NAME").toLowerCase(); |
188 | 0 | tables.add(tableName); |
189 | 0 | } |
190 | 0 | if (LOGGER.isDebugEnabled()) { |
191 | 0 | LOGGER.debug("Tables:" |
192 | |
+ tables); |
193 | |
} |
194 | |
} finally { |
195 | 0 | SQLFunctions.close(rs); |
196 | 0 | } |
197 | 0 | return tables; |
198 | |
} |
199 | |
|
200 | |
} |