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 UPDATE-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 Updater extends DBActor { |
|
35 |
private static final Logger logger = Logger.getLogger(Updater.class.getName()); |
|
36 |
|
|
37 |
public Updater(PersistenceManager pm) { |
|
38 |
this.pm = pm; |
|
39 |
} |
|
40 |
|
|
41 |
/** |
|
42 |
* update an object in the database |
|
43 |
* |
|
44 |
* <p>Use this method for single updates. In cases where |
|
45 |
* several subsequent updates for objects of the same class |
|
46 |
* are required the use of method <code>update(Object, Record)</code> |
|
47 |
* is recommended instead to minimise instantiation |
|
48 |
* overhead.</p> |
|
49 |
* |
|
50 |
* @param o object to update |
|
51 |
* @return the object that was updated |
|
52 |
*/ |
|
53 |
public Object update(Object o) { |
|
54 |
return update(o, new GenericRecord(o.getClass())); |
|
55 |
} |
|
56 |
|
|
57 |
/** |
|
58 |
* update an object in the database |
|
59 |
* |
|
60 |
* <p>Use this method for single updates. In cases where |
|
61 |
* several subsequent updates for objects of the same class |
|
62 |
* are required the use of method <code>update(Connection, Object, Record)</code> |
|
63 |
* is recommended instead to minimise instantiation |
|
64 |
* overhead.</p> |
|
65 |
* |
|
66 |
* @param c the connection to use, expected to be open and established |
|
67 |
* @param o object to update |
|
68 |
* @return the object that was updated |
|
69 |
*/ |
|
70 |
public Object update(Connection c, Object o) { |
|
71 |
return update(c, o, new GenericRecord(o.getClass())); |
|
72 |
} |
|
73 |
|
|
74 |
/** |
|
75 |
* update an object in the database |
|
76 |
* @param o object to update |
|
77 |
* @param record reference to object to use to map between object and database |
|
78 |
* @return the object that was updated |
|
79 |
*/ |
|
80 |
public Object update(Object o, Record record) { |
|
81 |
Connection c = null; |
|
82 |
try { |
|
83 |
c = pm.getConnection(); |
|
84 |
o = update(c, o, record); |
|
85 |
c.close(); |
|
86 |
c = null; |
|
87 |
} catch (SQLException ex) { |
|
88 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
89 |
} finally { |
|
90 |
pm.closeConnectionFinally(c); |
|
91 |
} |
|
92 |
return o; |
|
93 |
} |
|
94 |
|
|
95 |
/** |
|
96 |
* update an object in the database |
|
97 |
* @param c the connection to use, expected to be open and established |
|
98 |
* @param o object to update |
|
99 |
* @param record reference to object to use to map between object and database |
|
100 |
* @return the object that was updated |
|
101 |
*/ |
|
102 |
public Object update(Connection c, Object o, Record record) { |
|
103 |
Object updatedObject = null; |
|
104 |
PreparedStatement ps = null; |
|
105 |
try { |
|
106 |
ps = record.getUpdateStatment(c, o); |
|
107 |
ps.executeUpdate(); |
|
108 |
ps.close(); |
|
109 |
ps = null; |
|
110 |
updatedObject = o; |
|
111 |
} catch (Exception ex) { |
|
112 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
113 |
} finally { |
|
114 |
pm.closeStatementFinally(ps); |
|
115 |
} |
|
116 |
return updatedObject; |
|
117 |
} |
|
118 |
|
|
119 |
} |