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.ResultSet; |
|
23 |
import java.sql.SQLException; |
|
24 |
import java.util.ArrayList; |
|
25 |
import java.util.List; |
|
26 |
import java.util.Map; |
|
27 |
import java.util.logging.Level; |
|
28 |
import java.util.logging.Logger; |
|
29 |
|
|
30 |
/** |
3bf522
|
31 |
* Methoden zum Ausfuehren von SQL SELECT-Anweisungen |
U |
32 |
* |
|
33 |
* @author Copyright (c) Ulrich Hilger, <a href="http://uhilger.de">http://uhilger.de</a> |
|
34 |
* @author Published under the terms and conditions of |
|
35 |
* the <a href="http://www.gnu.org/licenses/" target="_blank">GNU General Public License</a> |
|
36 |
* @version 1, January 22, 2024 |
a59fca
|
37 |
*/ |
U |
38 |
public class Selector extends DBActor { |
|
39 |
|
|
40 |
private static final Logger logger = Logger.getLogger(Selector.class.getName()); |
|
41 |
|
|
42 |
public Selector(PersistenceManager pm) { |
|
43 |
this.pm = pm; |
|
44 |
} |
|
45 |
|
|
46 |
/* --------------- selects ---------------- */ |
|
47 |
|
|
48 |
/** |
|
49 |
* Select a list of objects through a given SQL statement |
|
50 |
* @param sql sql query string that designates the requested objects |
|
51 |
* @param record object to use to map db records to objects |
|
52 |
* @return a list of objects that match the given query |
|
53 |
*/ |
|
54 |
public List<Object> select(String sql, Record record) { |
|
55 |
return select(sql, record, Record.WITH_BLOBS); |
|
56 |
} |
|
57 |
|
|
58 |
/** |
|
59 |
* Select a list of objects through a given SQL statement |
|
60 |
* @param sql sql query string that designates the requested objects |
|
61 |
* @param record object to use to map db records to objects |
|
62 |
* @param includeBlobs true when BLOB contents are to be retrieved, false if not |
|
63 |
* @return a list of objects that match the given query |
|
64 |
*/ |
|
65 |
public List<Object> select(String sql, Record record, boolean includeBlobs) { |
|
66 |
Connection c = null; |
|
67 |
ArrayList<Object> list = new ArrayList<>(); |
|
68 |
try { |
|
69 |
c = pm.getConnection(); |
|
70 |
list = (ArrayList<Object>) select(c, sql, record, includeBlobs); |
|
71 |
c.close(); |
|
72 |
c = null; |
|
73 |
} catch (SQLException ex) { |
|
74 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
75 |
} finally { |
|
76 |
pm.closeConnectionFinally(c); |
|
77 |
} |
|
78 |
return list; |
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* Select a list of objects through a given SQL statement |
|
83 |
* @param sql sql query string that designates the requested objects |
|
84 |
* @param record object to use to map db records to objects |
|
85 |
* @param includeBlobs true when BLOB contents are to be retrieved, false if not |
|
86 |
* @param params list of parameters in the order they appear in the SQL string |
|
87 |
* @return a list of objects that match the given query |
|
88 |
*/ |
|
89 |
public List<Object> select(String sql, Record record, boolean includeBlobs, Object... params) { |
|
90 |
Connection c = null; |
|
91 |
ArrayList<Object> list = new ArrayList<>(); |
|
92 |
try { |
|
93 |
c = pm.getConnection(); |
|
94 |
list = (ArrayList<Object>) select(c, sql, record, includeBlobs, params); |
|
95 |
c.close(); |
|
96 |
c = null; |
|
97 |
} catch (SQLException ex) { |
|
98 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
99 |
} finally { |
|
100 |
pm.closeConnectionFinally(c); |
|
101 |
} |
|
102 |
return list; |
|
103 |
} |
|
104 |
|
|
105 |
|
|
106 |
/** |
|
107 |
* Select a list of objects that match a given SQL statement |
|
108 |
* @param sql sql query string that designates the requested objects |
|
109 |
* @return a list of map objects, one for each record. An element in the |
|
110 |
* list can be accessed with list.get(recordno).get("fieldname") |
|
111 |
*/ |
|
112 |
public List<Map<String, Object>> select(String sql) { |
|
113 |
//Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).finest(sql); |
|
114 |
Connection c = null; |
|
115 |
List<Map<String, Object>> list = null; |
|
116 |
try { |
|
117 |
c = pm.getConnection(); |
|
118 |
list = select(c, sql); |
|
119 |
c.close(); |
|
120 |
c = null; |
|
121 |
} catch (SQLException ex) { |
|
122 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
123 |
} finally { |
|
124 |
pm.closeConnectionFinally(c); |
|
125 |
} |
|
126 |
return list; |
|
127 |
} |
|
128 |
|
|
129 |
/** |
|
130 |
* Select a list of objects that match a given SQL statement |
|
131 |
* @param sql sql query string that designates the requested objects |
|
132 |
* @param includeBlobs true when content of blob coloumns should be returned, false if not |
|
133 |
* @return a list of list objects, one for each record. An element in the |
|
134 |
* list can be accessed with list.get(recordno).get(fieldno), each element is of type String |
|
135 |
*/ |
|
136 |
public List<List<String>> select(String sql, boolean includeBlobs) { |
|
137 |
Connection c = null; |
|
138 |
List<List<String>> list = null; |
|
139 |
try { |
|
140 |
c = pm.getConnection(); |
|
141 |
list = select(c, sql, includeBlobs); |
|
142 |
c.close(); |
|
143 |
c = null; |
|
144 |
} catch (SQLException ex) { |
|
145 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
146 |
} finally { |
|
147 |
pm.closeConnectionFinally(c); |
|
148 |
} |
|
149 |
return list; |
|
150 |
} |
|
151 |
|
|
152 |
/** |
|
153 |
* Select a list of objects that match a given SQL statement |
|
154 |
* @param sql sql query string that designates the requested objects with ? at the position of params |
|
155 |
* @param includeBlobs true when content of blob coloumns should be returned, false if not |
|
156 |
* @param params list of parameters in the order they appear in the SQL string |
|
157 |
* @return a list of list objects, one for each record. An element in the |
|
158 |
* list can be accessed with list.get(recordno).get(fieldno), each element is of type String |
|
159 |
*/ |
|
160 |
public List<List<String>> select(String sql, boolean includeBlobs, Object... params) { |
|
161 |
Connection c = null; |
|
162 |
List<List<String>> list = null; |
|
163 |
try { |
|
164 |
c = pm.getConnection(); |
|
165 |
list = select(c, sql, includeBlobs, params); |
|
166 |
c.close(); |
|
167 |
c = null; |
|
168 |
} catch (SQLException ex) { |
|
169 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
170 |
} finally { |
|
171 |
pm.closeConnectionFinally(c); |
|
172 |
} |
|
173 |
return list; |
|
174 |
} |
|
175 |
|
|
176 |
/* ---------- mit Connection ---------- */ |
|
177 |
|
|
178 |
|
|
179 |
/** |
|
180 |
* Select a list of objects that match a given SQL statement |
|
181 |
* @param c the database connection to use for this query, expected to be established and open already |
|
182 |
* @param sql sql query string that designates the requested objects |
|
183 |
* @return a list of map objects, one for each record. An element in the |
|
184 |
* list can be accessed with list.get(recordno).get("fieldname") |
|
185 |
*/ |
|
186 |
public List<Map<String, Object>> select(Connection c, String sql) { |
|
187 |
PreparedStatement ps = null; |
|
188 |
ResultSet rs = null; |
|
189 |
List<Map<String, Object>> list = null; |
|
190 |
try { |
|
191 |
ps = c.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
|
192 |
rs = ps.executeQuery(); |
|
193 |
list = pm.toList(rs); |
|
194 |
rs.close(); |
|
195 |
rs = null; |
|
196 |
ps.close(); |
|
197 |
ps = null; |
|
198 |
} catch (SQLException ex) { |
|
199 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
200 |
} finally { |
|
201 |
pm.closeResultSetFinally(rs); |
|
202 |
pm.closeStatementFinally(ps); |
|
203 |
} |
|
204 |
return list; |
|
205 |
} |
|
206 |
|
|
207 |
/** |
|
208 |
* Select a list of objects that match a given SQL statement |
|
209 |
* @param c the database connection to use for this query, expected to be established and open already |
|
210 |
* @param sql sql query string that designates the requested objects |
|
211 |
* @param includeBlobs true when content of blob coloumns should be returned, false if not |
|
212 |
* @return a list of list objects, one for each record. An element in the |
|
213 |
* list can be accessed with list.get(recordno).get(fieldno), each element is of type String |
|
214 |
*/ |
|
215 |
public List<List<String>> select(Connection c, String sql, boolean includeBlobs) { |
|
216 |
PreparedStatement ps = null; |
|
217 |
ResultSet rs = null; |
|
218 |
List<List<String>> list = null; |
|
219 |
try { |
|
220 |
ps = c.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
|
221 |
rs = ps.executeQuery(); |
|
222 |
list = pm.toList(rs, includeBlobs); |
|
223 |
rs.close(); |
|
224 |
rs = null; |
|
225 |
ps.close(); |
|
226 |
ps = null; |
|
227 |
} catch (SQLException ex) { |
|
228 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
229 |
} finally { |
|
230 |
pm.closeResultSetFinally(rs); |
|
231 |
pm.closeStatementFinally(ps); |
|
232 |
} |
|
233 |
return list; |
|
234 |
} |
|
235 |
|
|
236 |
/** |
|
237 |
* Select a list of objects that match a given SQL statement |
|
238 |
* @param c the database connection to use for this query, expected to be established and open already |
|
239 |
* @param sql sql query string that designates the requested objects with ? at the position of params |
|
240 |
* @param includeBlobs true when content of blob coloumns should be returned, false if not |
|
241 |
* @param params list of parameters in the order they appear in the SQL string |
|
242 |
* @return a list of list objects, one for each record. An element in the |
|
243 |
* list can be accessed with list.get(recordno).get(fieldno), each element is of type String |
|
244 |
*/ |
|
245 |
public List<List<String>> select(Connection c, String sql, boolean includeBlobs, Object... params) { |
|
246 |
PreparedStatement ps = null; |
|
247 |
ResultSet rs = null; |
|
248 |
List<List<String>> list = null; |
|
249 |
try { |
|
250 |
ps = pm.buildQuery(c, sql, params); |
|
251 |
rs = ps.executeQuery(); |
|
252 |
list = pm.toList(rs, includeBlobs); |
|
253 |
rs.close(); |
|
254 |
rs = null; |
|
255 |
ps.close(); |
|
256 |
ps = null; |
|
257 |
} catch (Exception ex) { |
|
258 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
259 |
} finally { |
|
260 |
pm.closeResultSetFinally(rs); |
|
261 |
pm.closeStatementFinally(ps); |
|
262 |
} |
|
263 |
return list; |
|
264 |
} |
|
265 |
|
|
266 |
/** |
|
267 |
* Select a list of objects that match a given SQL statement |
|
268 |
* @param c the database connection to use for this query, expected to be established and open already |
|
269 |
* @param sql sql query string that designates the requested objects |
|
270 |
* @param record object to use to map db records to objects |
|
271 |
* @param includeBlobs true when BLOB contents are to be retrieved, false if not |
|
272 |
* @return a list of objects that match the given query |
|
273 |
*/ |
|
274 |
public List<Object> select(Connection c, String sql, Record record, boolean includeBlobs) { |
|
275 |
PreparedStatement ps = null; |
|
276 |
ResultSet rs = null; |
|
277 |
ArrayList<Object> list = new ArrayList<>(); |
|
278 |
try { |
|
279 |
ps = c.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
|
280 |
rs = ps.executeQuery(); |
|
281 |
if (rs.first()) { |
|
282 |
while (!rs.isAfterLast()) { |
|
283 |
list.add(record.toObject(rs, includeBlobs)); |
|
284 |
rs.next(); |
|
285 |
} |
|
286 |
} |
|
287 |
rs.close(); |
|
288 |
rs = null; |
|
289 |
ps.close(); |
|
290 |
ps = null; |
|
291 |
} catch (Exception ex) { |
|
292 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
293 |
} finally { |
|
294 |
pm.closeResultSetFinally(rs); |
|
295 |
pm.closeStatementFinally(ps); |
|
296 |
} |
|
297 |
return list; |
|
298 |
} |
|
299 |
|
|
300 |
/** |
|
301 |
* Select a list of objects that match a given SQL statement |
|
302 |
* @param c the database connection to use for this query, expected to be established and open already |
|
303 |
* @param sql sql query string that designates the requested objects |
|
304 |
* @param record object to use to map db records to objects |
|
305 |
* @param includeBlobs true when BLOB contents are to be retrieved, false if not |
|
306 |
* @param params list of parameters in the order they appear in the SQL string |
|
307 |
* @return a list of objects that match the given query |
|
308 |
*/ |
|
309 |
public List<Object> select(Connection c, String sql, Record record, boolean includeBlobs, Object... params) { |
|
310 |
PreparedStatement ps = null; |
|
311 |
ResultSet rs = null; |
|
312 |
ArrayList<Object> list = new ArrayList<>(); |
|
313 |
try { |
|
314 |
//ps = c.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
|
315 |
ps = pm.buildQuery(c, sql, params); |
|
316 |
rs = ps.executeQuery(); |
|
317 |
if (rs.first()) { |
|
318 |
while (!rs.isAfterLast()) { |
|
319 |
list.add(record.toObject(rs, includeBlobs)); |
|
320 |
rs.next(); |
|
321 |
} |
|
322 |
} |
|
323 |
rs.close(); |
|
324 |
rs = null; |
|
325 |
ps.close(); |
|
326 |
ps = null; |
|
327 |
} catch (Exception ex) { |
|
328 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
329 |
} finally { |
|
330 |
pm.closeResultSetFinally(rs); |
|
331 |
pm.closeStatementFinally(ps); |
|
332 |
} |
|
333 |
return list; |
|
334 |
} |
|
335 |
|
|
336 |
} |