|
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.sql.Connection; |
|
32 |
| import java.sql.PreparedStatement; |
|
33 |
| import java.sql.ResultSet; |
|
34 |
| import java.sql.Statement; |
|
35 |
| import java.util.ArrayList; |
|
36 |
| import java.util.List; |
|
37 |
| |
|
38 |
| import org.jcontainer.dna.Logger; |
|
39 |
| import org.objectledge.database.Database; |
|
40 |
| import org.objectledge.database.DatabaseUtils; |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| public class DefaultPersistence implements Persistence |
|
50 |
| { |
|
51 |
| |
|
52 |
| private Database database; |
|
53 |
| |
|
54 |
| |
|
55 |
| private Logger logger; |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
4508
| public DefaultPersistence(Database database, Logger logger)
|
|
64 |
| { |
|
65 |
4508
| this.logger = logger;
|
|
66 |
4508
| this.database = database;
|
|
67 |
| } |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
138
| public Persistent load(long id, PersistentFactory factory) throws PersistenceException
|
|
80 |
| { |
|
81 |
138
| Connection conn = null;
|
|
82 |
138
| try
|
|
83 |
| { |
|
84 |
138
| conn = database.getConnection();
|
|
85 |
138
| Persistent obj = factory.newInstance();
|
|
86 |
138
| PreparedStatement statement = DefaultInputRecord.getSelectStatement(id, obj, conn);
|
|
87 |
138
| ResultSet rs = statement.executeQuery();
|
|
88 |
138
| if (!rs.next())
|
|
89 |
| { |
|
90 |
92
| return null;
|
|
91 |
| } |
|
92 |
46
| InputRecord record = new DefaultInputRecord(rs);
|
|
93 |
46
| obj.setData(record);
|
|
94 |
46
| obj.setSaved(record.getLong(obj.getKeyColumns()[0]));
|
|
95 |
46
| return obj;
|
|
96 |
| } |
|
97 |
| catch (Exception e) |
|
98 |
| { |
|
99 |
0
| throw new PersistenceException("Failed to retrieve object", e);
|
|
100 |
| } |
|
101 |
| finally |
|
102 |
| { |
|
103 |
138
| DatabaseUtils.close(conn);
|
|
104 |
| } |
|
105 |
| } |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
5520
| public List load(String where, PersistentFactory factory) throws PersistenceException
|
|
120 |
| { |
|
121 |
5520
| Connection conn = null;
|
|
122 |
5520
| try
|
|
123 |
| { |
|
124 |
5520
| conn = database.getConnection();
|
|
125 |
5520
| Persistent obj = factory.newInstance();
|
|
126 |
5520
| PreparedStatement statement = DefaultInputRecord.getSelectStatement(where, obj, conn);
|
|
127 |
5520
| ResultSet rs = statement.executeQuery();
|
|
128 |
5520
| InputRecord record = new DefaultInputRecord(rs);
|
|
129 |
5520
| ArrayList list = new ArrayList();
|
|
130 |
5520
| while (rs.next())
|
|
131 |
| { |
|
132 |
8004
| obj.setData(record);
|
|
133 |
8004
| obj.setSaved(record.getLong(obj.getKeyColumns()[0]));
|
|
134 |
8004
| list.add(obj);
|
|
135 |
8004
| obj = factory.newInstance();
|
|
136 |
| } |
|
137 |
5520
| return list;
|
|
138 |
| } |
|
139 |
| catch (Exception e) |
|
140 |
| { |
|
141 |
0
| throw new PersistenceException("Failed to retrieve object", e);
|
|
142 |
| } |
|
143 |
| finally |
|
144 |
| { |
|
145 |
5520
| DatabaseUtils.close(conn);
|
|
146 |
| } |
|
147 |
| } |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
4002
| public void save(Persistent object) throws PersistenceException
|
|
156 |
| { |
|
157 |
4002
| synchronized (object)
|
|
158 |
| { |
|
159 |
4002
| OutputRecord record = new DefaultOutputRecord(object);
|
|
160 |
4002
| String table = object.getTable();
|
|
161 |
4002
| String[] keys = object.getKeyColumns();
|
|
162 |
4002
| object.getData(record);
|
|
163 |
4002
| Connection conn = null;
|
|
164 |
| |
|
165 |
4002
| try
|
|
166 |
| { |
|
167 |
4002
| conn = database.getConnection();
|
|
168 |
4002
| PreparedStatement statement;
|
|
169 |
4002
| if (object.getSaved())
|
|
170 |
| { |
|
171 |
230
| statement = record.getUpdateStatement(conn);
|
|
172 |
230
| statement.execute();
|
|
173 |
| } |
|
174 |
| else |
|
175 |
| { |
|
176 |
3772
| long id;
|
|
177 |
3772
| if (keys.length == 1)
|
|
178 |
| { |
|
179 |
644
| id = database.getNextId(table);
|
|
180 |
644
| record.setLong(keys[0], id);
|
|
181 |
| } |
|
182 |
| else |
|
183 |
| { |
|
184 |
3128
| id = -1;
|
|
185 |
| } |
|
186 |
3772
| statement = record.getInsertStatement(conn);
|
|
187 |
3772
| statement.execute();
|
|
188 |
3772
| object.setSaved(id);
|
|
189 |
| } |
|
190 |
| } |
|
191 |
| catch (Exception e) |
|
192 |
| { |
|
193 |
0
| throw new PersistenceException("Failed to save object", e);
|
|
194 |
| } |
|
195 |
| finally |
|
196 |
| { |
|
197 |
4002
| DatabaseUtils.close(conn);
|
|
198 |
| } |
|
199 |
| } |
|
200 |
| } |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
138
| public void revert(Persistent object) throws PersistenceException, IllegalStateException
|
|
211 |
| { |
|
212 |
138
| synchronized (object)
|
|
213 |
| { |
|
214 |
138
| if (!object.getSaved())
|
|
215 |
| { |
|
216 |
46
| throw new IllegalStateException("no state has been saved yet");
|
|
217 |
| } |
|
218 |
92
| Connection conn = null;
|
|
219 |
92
| try
|
|
220 |
| { |
|
221 |
92
| conn = database.getConnection();
|
|
222 |
92
| PreparedStatement statement = DefaultInputRecord.getSelectStatements(object, conn);
|
|
223 |
92
| ResultSet rs = statement.executeQuery();
|
|
224 |
92
| if (!rs.next())
|
|
225 |
| { |
|
226 |
46
| throw new PersistenceException("saved state was lost");
|
|
227 |
| } |
|
228 |
46
| InputRecord irecord = new DefaultInputRecord(rs);
|
|
229 |
46
| object.setData(irecord);
|
|
230 |
| } |
|
231 |
| catch (Exception e) |
|
232 |
| { |
|
233 |
46
| throw new PersistenceException("failed to revert object's state", e);
|
|
234 |
| } |
|
235 |
| finally |
|
236 |
| { |
|
237 |
92
| DatabaseUtils.close(conn);
|
|
238 |
| } |
|
239 |
| } |
|
240 |
| } |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
322
| public void delete(Persistent object) throws PersistenceException
|
|
249 |
| { |
|
250 |
322
| synchronized (object)
|
|
251 |
| { |
|
252 |
322
| Connection conn = null;
|
|
253 |
322
| try
|
|
254 |
| { |
|
255 |
322
| conn = database.getConnection();
|
|
256 |
322
| OutputRecord record = new DefaultOutputRecord(object);
|
|
257 |
322
| object.getData(record);
|
|
258 |
322
| PreparedStatement statement = record.getDeleteStatement(conn);
|
|
259 |
322
| statement.execute();
|
|
260 |
322
| if(statement.getUpdateCount() != 1)
|
|
261 |
| { |
|
262 |
0
| throw new PersistenceException("unsuccessful DELETE statement");
|
|
263 |
| } |
|
264 |
| } |
|
265 |
| catch (Exception e) |
|
266 |
| { |
|
267 |
0
| if(e instanceof PersistenceException)
|
|
268 |
| { |
|
269 |
0
| throw (PersistenceException)e;
|
|
270 |
| } |
|
271 |
0
| throw new PersistenceException("Failed to delete object", e);
|
|
272 |
| } |
|
273 |
| finally |
|
274 |
| { |
|
275 |
322
| DatabaseUtils.close(conn);
|
|
276 |
| } |
|
277 |
| } |
|
278 |
| } |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
1196
| public void delete(String where, PersistentFactory factory) throws PersistenceException
|
|
288 |
| { |
|
289 |
1196
| Connection conn = null;
|
|
290 |
1196
| try
|
|
291 |
| { |
|
292 |
1196
| conn = database.getConnection();
|
|
293 |
1196
| Persistent obj = factory.newInstance();
|
|
294 |
1196
| PreparedStatement statement = conn.prepareStatement("DELETE FROM " + obj.getTable() +
|
|
295 |
| " WHERE " + where); |
|
296 |
1196
| statement.executeUpdate();
|
|
297 |
| } |
|
298 |
| catch (Exception e) |
|
299 |
| { |
|
300 |
0
| throw new PersistenceException("Failed to retrieve object", e);
|
|
301 |
| } |
|
302 |
| finally |
|
303 |
| { |
|
304 |
1196
| DatabaseUtils.close(conn);
|
|
305 |
| } |
|
306 |
| } |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
| |
|
316 |
| |
|
317 |
184
| public boolean exists(String table, String where) throws PersistenceException
|
|
318 |
| { |
|
319 |
184
| Connection conn = null;
|
|
320 |
184
| try
|
|
321 |
| { |
|
322 |
184
| conn = database.getConnection();
|
|
323 |
184
| Statement statement = conn.createStatement();
|
|
324 |
184
| ResultSet rs;
|
|
325 |
184
| if (where != null)
|
|
326 |
| { |
|
327 |
92
| rs = statement.executeQuery("SELECT DISTINCT 1 FROM " + table + " WHERE " + where);
|
|
328 |
| } |
|
329 |
| else |
|
330 |
| { |
|
331 |
92
| rs = statement.executeQuery("SELECT DISTINCT 1 FROM " + table);
|
|
332 |
| } |
|
333 |
184
| return (rs.next());
|
|
334 |
| } |
|
335 |
| catch (Exception e) |
|
336 |
| { |
|
337 |
0
| throw new PersistenceException("Failed to execute query", e);
|
|
338 |
| } |
|
339 |
| finally |
|
340 |
| { |
|
341 |
184
| DatabaseUtils.close(conn);
|
|
342 |
| } |
|
343 |
| } |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
| |
|
351 |
| |
|
352 |
| |
|
353 |
138
| public int count(String table, String where) throws PersistenceException
|
|
354 |
| { |
|
355 |
138
| Connection conn = null;
|
|
356 |
138
| try
|
|
357 |
| { |
|
358 |
138
| conn = database.getConnection();
|
|
359 |
138
| Statement statement = conn.createStatement();
|
|
360 |
138
| ResultSet rs;
|
|
361 |
138
| if (where != null)
|
|
362 |
| { |
|
363 |
46
| rs = statement.executeQuery("SELECT COUNT(*) FROM " + table + " WHERE " + where);
|
|
364 |
| } |
|
365 |
| else |
|
366 |
| { |
|
367 |
92
| rs = statement.executeQuery("SELECT COUNT(*) FROM " + table);
|
|
368 |
| } |
|
369 |
138
| if (!rs.next())
|
|
370 |
| { |
|
371 |
0
| throw new PersistenceException("internal error - no data???");
|
|
372 |
| } |
|
373 |
138
| return (rs.getInt(1));
|
|
374 |
| } |
|
375 |
| catch (Exception e) |
|
376 |
| { |
|
377 |
0
| throw new PersistenceException("Failed to execute query", e);
|
|
378 |
| } |
|
379 |
| finally |
|
380 |
| { |
|
381 |
138
| DatabaseUtils.close(conn);
|
|
382 |
| } |
|
383 |
| } |
|
384 |
| |
|
385 |
| |
|
386 |
| |
|
387 |
| |
|
388 |
4508
| public Database getDatabase()
|
|
389 |
| { |
|
390 |
4508
| return database;
|
|
391 |
| } |
|
392 |
| } |