|
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 |
| |
|
29 |
| package org.objectledge.database.persistence; |
|
30 |
| |
|
31 |
| import java.math.BigDecimal; |
|
32 |
| import java.net.URL; |
|
33 |
| import java.sql.Array; |
|
34 |
| import java.sql.Blob; |
|
35 |
| import java.sql.Clob; |
|
36 |
| import java.sql.Connection; |
|
37 |
| import java.sql.PreparedStatement; |
|
38 |
| import java.sql.Ref; |
|
39 |
| import java.sql.ResultSet; |
|
40 |
| import java.sql.SQLException; |
|
41 |
| import java.util.Date; |
|
42 |
| |
|
43 |
| import org.objectledge.database.DatabaseUtils; |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| public class DefaultInputRecord implements InputRecord |
|
50 |
| { |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| private ResultSet rs; |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
5612
| public DefaultInputRecord(ResultSet rs)
|
|
64 |
| { |
|
65 |
5612
| this.rs = rs;
|
|
66 |
| } |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
2760
| public boolean getBoolean(String field)
|
|
78 |
| throws PersistenceException |
|
79 |
| { |
|
80 |
2760
| try
|
|
81 |
| { |
|
82 |
2760
| return rs.getBoolean(field);
|
|
83 |
| } |
|
84 |
| catch(SQLException e) |
|
85 |
| { |
|
86 |
0
| throw new PersistenceException("Failed to read field "+field, e);
|
|
87 |
| } |
|
88 |
| } |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
276
| public byte getByte(String field)
|
|
98 |
| throws PersistenceException |
|
99 |
| { |
|
100 |
276
| try
|
|
101 |
| { |
|
102 |
276
| return rs.getByte(field);
|
|
103 |
| } |
|
104 |
| catch(SQLException e) |
|
105 |
| { |
|
106 |
0
| throw new PersistenceException("Failed to read field "+field, e);
|
|
107 |
| } |
|
108 |
| } |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
276
| public short getShort(String field)
|
|
118 |
| throws PersistenceException |
|
119 |
| { |
|
120 |
276
| try
|
|
121 |
| { |
|
122 |
276
| return rs.getShort(field);
|
|
123 |
| } |
|
124 |
| catch(SQLException e) |
|
125 |
| { |
|
126 |
0
| throw new PersistenceException("Failed to read field "+field, e);
|
|
127 |
| } |
|
128 |
| } |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
1656
| public int getInteger(String field)
|
|
138 |
| throws PersistenceException |
|
139 |
| { |
|
140 |
1656
| try
|
|
141 |
| { |
|
142 |
1656
| return rs.getInt(field);
|
|
143 |
| } |
|
144 |
| catch(SQLException e) |
|
145 |
| { |
|
146 |
0
| throw new PersistenceException("Failed to read field "+field, e);
|
|
147 |
| } |
|
148 |
| } |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
17664
| public long getLong(String field)
|
|
158 |
| throws PersistenceException |
|
159 |
| { |
|
160 |
17664
| try
|
|
161 |
| { |
|
162 |
17664
| return rs.getLong(field);
|
|
163 |
| } |
|
164 |
| catch(SQLException e) |
|
165 |
| { |
|
166 |
0
| throw new PersistenceException("Failed to read field "+field, e);
|
|
167 |
| } |
|
168 |
| } |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
276
| public BigDecimal getBigDecimal(String field)
|
|
178 |
| throws PersistenceException |
|
179 |
| { |
|
180 |
276
| try
|
|
181 |
| { |
|
182 |
276
| return rs.getBigDecimal(field);
|
|
183 |
| } |
|
184 |
| catch(SQLException e) |
|
185 |
| { |
|
186 |
0
| throw new PersistenceException("Failed to read field "+field, e);
|
|
187 |
| } |
|
188 |
| } |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
276
| public float getFloat(String field)
|
|
198 |
| throws PersistenceException |
|
199 |
| { |
|
200 |
276
| try
|
|
201 |
| { |
|
202 |
276
| return rs.getFloat(field);
|
|
203 |
| } |
|
204 |
| catch(SQLException e) |
|
205 |
| { |
|
206 |
0
| throw new PersistenceException("Failed to read field "+field, e);
|
|
207 |
| } |
|
208 |
| } |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
0
| public double getDouble(String field)
|
|
218 |
| throws PersistenceException |
|
219 |
| { |
|
220 |
0
| try
|
|
221 |
| { |
|
222 |
0
| return rs.getDouble(field);
|
|
223 |
| } |
|
224 |
| catch(SQLException e) |
|
225 |
| { |
|
226 |
0
| throw new PersistenceException("Failed to read field "+field, e);
|
|
227 |
| } |
|
228 |
| } |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
| |
|
236 |
| |
|
237 |
16054
| public String getString(String field)
|
|
238 |
| throws PersistenceException |
|
239 |
| { |
|
240 |
16054
| try
|
|
241 |
| { |
|
242 |
16054
| return DatabaseUtils.unescapeSqlString(rs.getString(field));
|
|
243 |
| } |
|
244 |
| catch(SQLException e) |
|
245 |
| { |
|
246 |
0
| throw new PersistenceException("Failed to read field "+field, e);
|
|
247 |
| } |
|
248 |
| } |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
0
| public byte[] getBytes(String field)
|
|
261 |
| throws PersistenceException |
|
262 |
| { |
|
263 |
0
| try
|
|
264 |
| { |
|
265 |
0
| String encoded = rs.getString(field);
|
|
266 |
0
| sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
|
|
267 |
0
| return decoder.decodeBuffer(encoded);
|
|
268 |
| } |
|
269 |
| catch(Exception e) |
|
270 |
| { |
|
271 |
0
| throw new PersistenceException("Failed to read field "+field, e);
|
|
272 |
| } |
|
273 |
| } |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
828
| public Date getDate(String field)
|
|
284 |
| throws PersistenceException |
|
285 |
| { |
|
286 |
828
| try
|
|
287 |
| { |
|
288 |
828
| java.sql.Timestamp ts = rs.getTimestamp(field);
|
|
289 |
828
| return ts == null ? null : new Date(ts.getTime());
|
|
290 |
| } |
|
291 |
| catch(SQLException e) |
|
292 |
| { |
|
293 |
0
| throw new PersistenceException("Failed to read field "+field, e);
|
|
294 |
| } |
|
295 |
| } |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
0
| public Array getArray(String field)
|
|
306 |
| throws PersistenceException |
|
307 |
| { |
|
308 |
0
| try
|
|
309 |
| { |
|
310 |
0
| return rs.getArray(field);
|
|
311 |
| } |
|
312 |
| catch(SQLException e) |
|
313 |
| { |
|
314 |
0
| throw new PersistenceException("Failed to read field "+field, e);
|
|
315 |
| } |
|
316 |
| } |
|
317 |
| |
|
318 |
| |
|
319 |
| |
|
320 |
| |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
0
| public Blob getBlob(String field)
|
|
327 |
| throws PersistenceException |
|
328 |
| { |
|
329 |
0
| try
|
|
330 |
| { |
|
331 |
0
| return rs.getBlob(field);
|
|
332 |
| } |
|
333 |
| catch(SQLException e) |
|
334 |
| { |
|
335 |
0
| throw new PersistenceException("Failed to read field "+field, e);
|
|
336 |
| } |
|
337 |
| } |
|
338 |
| |
|
339 |
| |
|
340 |
| |
|
341 |
| |
|
342 |
| |
|
343 |
| |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
0
| public Clob getClob(String field)
|
|
348 |
| throws PersistenceException |
|
349 |
| { |
|
350 |
0
| try
|
|
351 |
| { |
|
352 |
0
| return rs.getClob(field);
|
|
353 |
| } |
|
354 |
| catch(SQLException e) |
|
355 |
| { |
|
356 |
0
| throw new PersistenceException("Failed to read field "+field, e);
|
|
357 |
| } |
|
358 |
| } |
|
359 |
| |
|
360 |
| |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
| |
|
367 |
| |
|
368 |
0
| public Ref getRef(String field)
|
|
369 |
| throws PersistenceException |
|
370 |
| { |
|
371 |
0
| try
|
|
372 |
| { |
|
373 |
0
| return rs.getRef(field);
|
|
374 |
| } |
|
375 |
| catch(SQLException e) |
|
376 |
| { |
|
377 |
0
| throw new PersistenceException("Failed to read field "+field, e);
|
|
378 |
| } |
|
379 |
| } |
|
380 |
| |
|
381 |
| |
|
382 |
| |
|
383 |
| |
|
384 |
| |
|
385 |
| |
|
386 |
| |
|
387 |
| |
|
388 |
| |
|
389 |
0
| public URL getURL(String field)
|
|
390 |
| throws PersistenceException |
|
391 |
| { |
|
392 |
0
| try
|
|
393 |
| { |
|
394 |
0
| return rs.getURL(field);
|
|
395 |
| } |
|
396 |
| catch(SQLException e) |
|
397 |
| { |
|
398 |
0
| throw new PersistenceException("Failed to read field "+field, e);
|
|
399 |
| } |
|
400 |
| } |
|
401 |
| |
|
402 |
| |
|
403 |
| |
|
404 |
| |
|
405 |
| |
|
406 |
| |
|
407 |
| |
|
408 |
| |
|
409 |
| |
|
410 |
0
| public Object getObject(String field)
|
|
411 |
| throws PersistenceException |
|
412 |
| { |
|
413 |
0
| try
|
|
414 |
| { |
|
415 |
0
| return rs.getObject(field);
|
|
416 |
| } |
|
417 |
| catch(SQLException e) |
|
418 |
| { |
|
419 |
0
| throw new PersistenceException("Failed to read field "+field, e);
|
|
420 |
| } |
|
421 |
| } |
|
422 |
| |
|
423 |
| |
|
424 |
| |
|
425 |
| |
|
426 |
| |
|
427 |
| |
|
428 |
| |
|
429 |
| |
|
430 |
| |
|
431 |
| |
|
432 |
7452
| public boolean isNull(String field)
|
|
433 |
| throws PersistenceException |
|
434 |
| { |
|
435 |
7452
| try
|
|
436 |
| { |
|
437 |
7452
| rs.getString(field);
|
|
438 |
7452
| return rs.wasNull();
|
|
439 |
| } |
|
440 |
| catch(SQLException e) |
|
441 |
| { |
|
442 |
0
| throw new PersistenceException("Failed to read field "+field, e);
|
|
443 |
| } |
|
444 |
| } |
|
445 |
| |
|
446 |
| |
|
447 |
| |
|
448 |
| |
|
449 |
| |
|
450 |
| |
|
451 |
| |
|
452 |
| |
|
453 |
| |
|
454 |
| |
|
455 |
| |
|
456 |
| |
|
457 |
92
| public static PreparedStatement getSelectStatements(Persistent object, Connection conn)
|
|
458 |
| throws PersistenceException, SQLException |
|
459 |
| { |
|
460 |
92
| DefaultOutputRecord out = new DefaultOutputRecord(object);
|
|
461 |
92
| object.getData(out);
|
|
462 |
92
| PreparedStatement stmt = conn.prepareStatement("SELECT * FROM " + object.getTable() +
|
|
463 |
| " WHERE " + out.getWhereClause()); |
|
464 |
92
| out.setValues(stmt, true, false);
|
|
465 |
92
| return stmt;
|
|
466 |
| } |
|
467 |
| |
|
468 |
| |
|
469 |
| |
|
470 |
| |
|
471 |
| |
|
472 |
| |
|
473 |
| |
|
474 |
| |
|
475 |
| |
|
476 |
| |
|
477 |
138
| public static PreparedStatement getSelectStatement(long key, Persistent object, Connection conn)
|
|
478 |
| throws SQLException |
|
479 |
| { |
|
480 |
138
| return conn.prepareStatement("SELECT * FROM " + object.getTable() + " WHERE " +
|
|
481 |
| object.getKeyColumns()[0] + " = " + key); |
|
482 |
| } |
|
483 |
| |
|
484 |
| |
|
485 |
| |
|
486 |
| |
|
487 |
| |
|
488 |
| |
|
489 |
| |
|
490 |
| |
|
491 |
| |
|
492 |
| |
|
493 |
5520
| public static PreparedStatement getSelectStatement(String where, Persistent object,
|
|
494 |
| Connection conn) |
|
495 |
| throws SQLException |
|
496 |
| { |
|
497 |
5520
| if (where != null)
|
|
498 |
| { |
|
499 |
|