commit | author | age
|
3bf522
|
1 |
/* |
U |
2 |
* BaseLink - Generic object relational mapping |
|
3 |
* Copyright (C) 2024 Ulrich Hilger, http://uhilger.de |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, either version 3 of the License, or |
|
8 |
* (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program. If not, see http://www.gnu.org/licenses/ |
|
17 |
*/ |
a59fca
|
18 |
package de.uhilger.baselink; |
U |
19 |
|
|
20 |
import java.sql.Connection; |
|
21 |
import java.sql.PreparedStatement; |
|
22 |
import java.sql.SQLException; |
|
23 |
import java.util.logging.Level; |
|
24 |
import java.util.logging.Logger; |
|
25 |
|
|
26 |
/** |
3bf522
|
27 |
* Methoden zum Ausfuehren von SQL DELETE-Anweisungen |
U |
28 |
* |
|
29 |
* @author Copyright (c) Ulrich Hilger, <a href="http://uhilger.de">http://uhilger.de</a> |
|
30 |
* @author Published under the terms and conditions of |
|
31 |
* the <a href="http://www.gnu.org/licenses/" target="_blank">GNU General Public License</a> |
|
32 |
* @version 1, January 22, 2024 |
a59fca
|
33 |
*/ |
U |
34 |
public class Eraser extends DBActor { |
|
35 |
|
|
36 |
private static final Logger logger = Logger.getLogger(Eraser.class.getName()); |
|
37 |
|
|
38 |
public Eraser(PersistenceManager pm) { |
|
39 |
this.pm = pm; |
|
40 |
} |
|
41 |
|
|
42 |
/** |
|
43 |
* Delete a given object from the database |
|
44 |
* |
|
45 |
* <p>Use this method for single deletes. In cases where |
|
46 |
* several subsequent deletes for objects of the same class |
|
47 |
* are required the use of method <code>delete(Object, Record)</code> |
|
48 |
* is recommended instead to minimise instantiation |
|
49 |
* overhead.</p> |
|
50 |
* |
|
51 |
* @param o object to delete |
|
52 |
* @return the deleted object |
|
53 |
*/ |
|
54 |
public Object delete(Object o) { |
|
55 |
return delete(o, new GenericRecord(o.getClass())); |
|
56 |
} |
|
57 |
|
|
58 |
/** |
|
59 |
* Delete a given object from the database |
|
60 |
* |
|
61 |
* <p>Use this method for single deletes. In cases where |
|
62 |
* several subsequent deletes for objects of the same class |
|
63 |
* are required the use of method <code>delete(Connection, Object, Record)</code> |
|
64 |
* is recommended instead to minimise instantiation |
|
65 |
* overhead.</p> |
|
66 |
* |
|
67 |
* @param c the connection to use, expected to be open and established |
|
68 |
* @param o object to delete |
|
69 |
* @return the deleted object |
|
70 |
*/ |
|
71 |
public Object delete(Connection c, Object o) { |
|
72 |
return delete(c, o, new GenericRecord(o.getClass())); |
|
73 |
} |
|
74 |
|
|
75 |
/** |
|
76 |
* Delete a given object from the database |
|
77 |
* @param o object to delete |
|
78 |
* @param record reference to object to use to map between object and database |
|
79 |
* @return the deleted object |
|
80 |
*/ |
|
81 |
public Object delete(Object o, Record record) { |
|
82 |
Connection c = null; |
|
83 |
try { |
|
84 |
c = pm.getConnection(); |
|
85 |
o = delete(c, o, record); |
|
86 |
c.close(); |
|
87 |
c = null; |
|
88 |
} catch (SQLException ex) { |
|
89 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
90 |
} finally { |
|
91 |
pm.closeConnectionFinally(c); |
|
92 |
} |
|
93 |
return o; |
|
94 |
} |
|
95 |
|
|
96 |
/** |
|
97 |
* Delete a given object from the database |
|
98 |
* @param c the connection to use, expected to be open and established |
|
99 |
* @param o object to delete |
|
100 |
* @param record reference to object to use to map between object and database |
|
101 |
* @return the deleted object |
|
102 |
*/ |
|
103 |
public Object delete(Connection c, Object o, Record record) { |
|
104 |
Object deletedObject = null; |
|
105 |
PreparedStatement ps = null; |
|
106 |
try { |
|
107 |
ps = record.getDeleteStatment(c, o); |
|
108 |
ps.executeUpdate(); |
|
109 |
ps.close(); |
|
110 |
ps = null; |
|
111 |
deletedObject = o; |
|
112 |
} catch (Exception ex) { |
|
113 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
114 |
} finally { |
|
115 |
pm.closeStatementFinally(ps); |
|
116 |
} |
|
117 |
return deletedObject; |
|
118 |
} |
|
119 |
|
|
120 |
} |