1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.common.database;
21
22 import org.waarp.common.database.exception.WaarpDatabaseNoConnectionException;
23 import org.waarp.common.database.exception.WaarpDatabaseNoDataException;
24 import org.waarp.common.database.exception.WaarpDatabaseSqlException;
25 import org.waarp.common.logging.WaarpLogger;
26 import org.waarp.common.logging.WaarpLoggerFactory;
27
28 import java.sql.ResultSet;
29 import java.sql.SQLException;
30 import java.sql.Statement;
31
32
33
34
35 public class DbRequest {
36
37
38
39 private static final WaarpLogger logger =
40 WaarpLoggerFactory.getLogger(DbRequest.class);
41 private static final String SQL_EXCEPTION_REQUEST = "SQL Exception Request:";
42 private static final String SQL_EXCEPTION_REQUEST1 = "SQL Exception Request:";
43
44
45
46
47 private Statement stmt;
48
49
50
51
52 private ResultSet rs;
53
54
55
56
57 private final DbSession ls;
58
59
60
61
62
63
64
65
66 public DbRequest(final DbSession ls)
67 throws WaarpDatabaseNoConnectionException {
68 if (ls.isDisActive()) {
69 ls.checkConnection();
70 }
71 this.ls = ls;
72 }
73
74
75
76
77
78
79
80
81
82 private Statement createStatement()
83 throws WaarpDatabaseNoConnectionException, WaarpDatabaseSqlException {
84 if (ls == null) {
85 throw new WaarpDatabaseNoConnectionException("No connection");
86 }
87 if (ls.getConn() == null) {
88 throw new WaarpDatabaseNoConnectionException("No connection");
89 }
90 if (ls.isDisActive()) {
91 ls.checkConnection();
92 }
93 try {
94 return ls.getConn().createStatement();
95 } catch (final SQLException e) {
96 ls.checkConnection();
97 try {
98 return ls.getConn().createStatement();
99 } catch (final SQLException e1) {
100 throw new WaarpDatabaseSqlException("Error while Create Statement", e);
101 }
102 }
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116 public final void select(final String select)
117 throws WaarpDatabaseNoConnectionException, WaarpDatabaseSqlException {
118 close();
119 stmt = createStatement();
120
121
122 try {
123 if (stmt.execute(select)) {
124 rs = stmt.getResultSet();
125 }
126 } catch (final SQLException e) {
127 logger.error(SQL_EXCEPTION_REQUEST + select + ' ' + e.getMessage());
128 DbConstant.error(e);
129 ls.checkConnectionNoException();
130 throw new WaarpDatabaseSqlException(SQL_EXCEPTION_REQUEST1 + select, e);
131 }
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147 public final void select(final String select, final int timeout)
148 throws WaarpDatabaseNoConnectionException, WaarpDatabaseSqlException {
149 close();
150 stmt = createStatement();
151 if (timeout > 0) {
152 try {
153 stmt.setQueryTimeout(timeout);
154 } catch (final SQLException e1) {
155
156 }
157 }
158
159
160 try {
161 if (stmt.execute(select)) {
162 rs = stmt.getResultSet();
163 }
164 } catch (final SQLException e) {
165 logger.error(SQL_EXCEPTION_REQUEST1 + select + ' ' + e.getMessage());
166 DbConstant.error(e);
167 ls.checkConnectionNoException();
168 throw new WaarpDatabaseSqlException(SQL_EXCEPTION_REQUEST1 + select, e);
169 }
170 }
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 public final int query(final String query)
186 throws WaarpDatabaseNoConnectionException, WaarpDatabaseSqlException {
187 close();
188 stmt = createStatement();
189 try {
190 final int rowcount = stmt.executeUpdate(query);
191 logger.debug("QUERY({}): {}", rowcount, query);
192 return rowcount;
193 } catch (final SQLException e) {
194 logger.error(SQL_EXCEPTION_REQUEST1 + query + ' ' + e.getMessage());
195 DbConstant.error(e);
196 ls.checkConnectionNoException();
197 throw new WaarpDatabaseSqlException(SQL_EXCEPTION_REQUEST1 + query, e);
198 }
199 }
200
201
202
203
204 public final void close() {
205
206
207
208
209 if (rs != null) {
210 try {
211 rs.close();
212 } catch (final SQLException sqlEx) {
213 ls.checkConnectionNoException();
214 }
215 rs = null;
216 }
217 if (stmt != null) {
218 try {
219 stmt.close();
220 } catch (final SQLException sqlEx) {
221 ls.checkConnectionNoException();
222 }
223 stmt = null;
224 }
225 }
226
227
228
229
230
231
232
233
234
235
236 public final long getLastId() throws WaarpDatabaseNoDataException {
237 ResultSet rstmp = null;
238 long result = DbConstant.ILLEGALVALUE;
239 try {
240 rstmp = stmt.getGeneratedKeys();
241 if (rstmp.next()) {
242 result = rstmp.getLong(1);
243 }
244 } catch (final SQLException e) {
245 DbConstant.error(e);
246 ls.checkConnectionNoException();
247 throw new WaarpDatabaseNoDataException("No data found", e);
248 } finally {
249 if (rstmp != null) {
250 try {
251 rstmp.close();
252 } catch (final SQLException e) {
253
254 }
255 }
256 }
257 return result;
258 }
259
260
261
262
263
264
265
266
267
268 public final boolean getNext()
269 throws WaarpDatabaseNoConnectionException, WaarpDatabaseSqlException {
270 if (rs == null) {
271 logger.error("SQL ResultSet is Null into getNext");
272 throw new WaarpDatabaseNoConnectionException(
273 "SQL ResultSet is Null into getNext");
274 }
275 if (ls.isDisActive()) {
276 ls.checkConnection();
277 throw new WaarpDatabaseSqlException(
278 "Request cannot be executed since connection was recreated between");
279 }
280 try {
281 return rs.next();
282 } catch (final SQLException e) {
283 logger.warn("SQL Exception to getNextRow" + ' ' + e.getMessage());
284 DbConstant.error(e);
285 ls.checkConnectionNoException();
286 throw new WaarpDatabaseSqlException("SQL Exception to getNextRow", e);
287 }
288 }
289
290
291
292
293
294
295 public final ResultSet getResultSet()
296 throws WaarpDatabaseNoConnectionException {
297 if (rs == null) {
298 throw new WaarpDatabaseNoConnectionException(
299 "SQL ResultSet is Null into getResultSet");
300 }
301 return rs;
302 }
303
304
305
306
307
308
309
310
311 public static String getIsNull(final String value) {
312 return value == null? " is NULL" : " = '" + value + '\'';
313 }
314 }