1 | |
package org.kuali.db.jdbc; |
2 | |
|
3 | |
import static org.apache.commons.lang.StringUtils.isNotEmpty; |
4 | |
import static org.kuali.db.jdbc.DatabaseType.POSTGRESQL; |
5 | |
|
6 | |
import java.sql.Connection; |
7 | |
import java.sql.ResultSet; |
8 | |
import java.sql.SQLException; |
9 | |
import java.sql.Statement; |
10 | |
import java.util.List; |
11 | |
|
12 | |
import org.apache.commons.lang.Validate; |
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | 0 | public class JDBCUtils { |
18 | |
|
19 | |
List<JDBCConfiguration> jdbcConfigs; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public JDBCConfiguration getDatabaseConfiguration(final DatabaseType type) { |
28 | 0 | for (JDBCConfiguration jdbcConfig : jdbcConfigs) { |
29 | 0 | if (jdbcConfig.getType().equals(type)) { |
30 | 0 | return jdbcConfig; |
31 | |
} |
32 | |
} |
33 | 0 | return JDBCConfiguration.UNKNOWN_CONFIG; |
34 | |
} |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public JDBCConfiguration getDatabaseConfiguration(final String url) { |
43 | 0 | Validate.isTrue(isNotEmpty(url)); |
44 | |
|
45 | 0 | for (JDBCConfiguration jdbcConfig : jdbcConfigs) { |
46 | 0 | String urlFragment = jdbcConfig.getUrlFragment(); |
47 | 0 | if (url.contains(urlFragment)) { |
48 | 0 | return jdbcConfig; |
49 | |
} |
50 | 0 | } |
51 | 0 | return JDBCConfiguration.UNKNOWN_CONFIG; |
52 | |
} |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public String getDatabaseName(final String url) { |
62 | 0 | int leftIndex = url.lastIndexOf("/"); |
63 | 0 | if (leftIndex == -1) { |
64 | 0 | leftIndex = url.lastIndexOf(":"); |
65 | |
} |
66 | 0 | leftIndex++; |
67 | |
|
68 | 0 | int rightIndex = url.length(); |
69 | 0 | if (url.indexOf("?") != -1) { |
70 | 0 | rightIndex = url.indexOf("?"); |
71 | 0 | } else if (url.indexOf(";") != -1) { |
72 | 0 | rightIndex = url.indexOf(";"); |
73 | |
} |
74 | |
|
75 | 0 | return url.substring(leftIndex, rightIndex); |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public String getServerUrl(final String url) { |
87 | 0 | int rightIndex = url.length(); |
88 | 0 | if (url.lastIndexOf("/") != -1) { |
89 | 0 | rightIndex = url.lastIndexOf("/"); |
90 | 0 | } else if (url.lastIndexOf(":") != -1) { |
91 | 0 | rightIndex = url.lastIndexOf(":"); |
92 | |
} |
93 | |
|
94 | 0 | String baseUrl = url.substring(0, rightIndex); |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | 0 | if (POSTGRESQL.equals(getDatabaseConfiguration(url).getType())) { |
100 | 0 | baseUrl += "/postgres"; |
101 | |
} |
102 | |
|
103 | 0 | String options = ""; |
104 | 0 | int optionsIndex = url.indexOf("?"); |
105 | 0 | if (optionsIndex == -1) { |
106 | 0 | optionsIndex = url.indexOf(";"); |
107 | |
} |
108 | 0 | if (optionsIndex != -1) { |
109 | 0 | options = url.substring(optionsIndex); |
110 | |
} |
111 | |
|
112 | 0 | return baseUrl + options; |
113 | |
} |
114 | |
|
115 | |
public static void closeQuietly(final ResultSet rs, final Statement stmt, final Connection conn) { |
116 | 0 | closeQuietly(rs); |
117 | 0 | closeQuietly(stmt); |
118 | 0 | closeQuietly(conn); |
119 | 0 | } |
120 | |
|
121 | |
public static void closeQuietly(final Statement stmt, final Connection conn) { |
122 | 0 | closeQuietly(null, stmt, conn); |
123 | 0 | } |
124 | |
|
125 | |
public static void closeQuietly(final ResultSet rs) { |
126 | 0 | if (rs == null) { |
127 | 0 | return; |
128 | |
} |
129 | |
try { |
130 | 0 | rs.close(); |
131 | 0 | } catch (SQLException e) { |
132 | |
|
133 | 0 | } |
134 | 0 | } |
135 | |
|
136 | |
public static void closeQuietly(final Statement stmt) { |
137 | 0 | if (stmt == null) { |
138 | 0 | return; |
139 | |
} |
140 | |
try { |
141 | 0 | stmt.close(); |
142 | 0 | } catch (SQLException e) { |
143 | |
|
144 | 0 | } |
145 | 0 | } |
146 | |
|
147 | |
public static void closeQuietly(final Connection conn) { |
148 | 0 | if (conn == null) { |
149 | 0 | return; |
150 | |
} |
151 | |
try { |
152 | 0 | conn.close(); |
153 | 0 | } catch (SQLException e) { |
154 | |
|
155 | 0 | } |
156 | 0 | } |
157 | |
|
158 | |
public static void rollbackQuietly(final Connection conn) { |
159 | 0 | if (conn == null) { |
160 | 0 | return; |
161 | |
} |
162 | |
try { |
163 | 0 | conn.rollback(); |
164 | 0 | } catch (SQLException e) { |
165 | |
|
166 | 0 | } |
167 | 0 | } |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
public List<JDBCConfiguration> getJdbcConfigs() { |
173 | 0 | return jdbcConfigs; |
174 | |
} |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
public void setJdbcConfigs(final List<JDBCConfiguration> jdbcConfigs) { |
181 | 0 | this.jdbcConfigs = jdbcConfigs; |
182 | 0 | } |
183 | |
|
184 | |
} |