/* * BaseLink - Generic object relational mapping * Copyright (C) 2024 Ulrich Hilger, http://uhilger.de * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/ */ package de.uhilger.baselink; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; /** * Methoden zum Ausfuehren von SQL SELECT-Anweisungen * * @author Copyright (c) Ulrich Hilger, http://uhilger.de * @author Published under the terms and conditions of * the GNU General Public License * @version 1, January 22, 2024 */ public class Selector extends DBActor { private static final Logger logger = Logger.getLogger(Selector.class.getName()); public Selector(PersistenceManager pm) { this.pm = pm; } /* --------------- selects ---------------- */ /** * Select a list of objects through a given SQL statement * @param sql sql query string that designates the requested objects * @param record object to use to map db records to objects * @return a list of objects that match the given query */ public List select(String sql, Record record) { return select(sql, record, Record.WITH_BLOBS); } /** * Select a list of objects through a given SQL statement * @param sql sql query string that designates the requested objects * @param record object to use to map db records to objects * @param includeBlobs true when BLOB contents are to be retrieved, false if not * @return a list of objects that match the given query */ public List select(String sql, Record record, boolean includeBlobs) { Connection c = null; ArrayList list = new ArrayList<>(); try { c = pm.getConnection(); list = (ArrayList) select(c, sql, record, includeBlobs); c.close(); c = null; } catch (SQLException ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } finally { pm.closeConnectionFinally(c); } return list; } /** * Select a list of objects through a given SQL statement * @param sql sql query string that designates the requested objects * @param record object to use to map db records to objects * @param includeBlobs true when BLOB contents are to be retrieved, false if not * @param params list of parameters in the order they appear in the SQL string * @return a list of objects that match the given query */ public List select(String sql, Record record, boolean includeBlobs, Object... params) { Connection c = null; ArrayList list = new ArrayList<>(); try { c = pm.getConnection(); list = (ArrayList) select(c, sql, record, includeBlobs, params); c.close(); c = null; } catch (SQLException ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } finally { pm.closeConnectionFinally(c); } return list; } /** * Select a list of objects that match a given SQL statement * @param sql sql query string that designates the requested objects * @return a list of map objects, one for each record. An element in the * list can be accessed with list.get(recordno).get("fieldname") */ public List> select(String sql) { //Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).finest(sql); Connection c = null; List> list = null; try { c = pm.getConnection(); list = select(c, sql); c.close(); c = null; } catch (SQLException ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } finally { pm.closeConnectionFinally(c); } return list; } /** * Select a list of objects that match a given SQL statement * @param sql sql query string that designates the requested objects * @param includeBlobs true when content of blob coloumns should be returned, false if not * @return a list of list objects, one for each record. An element in the * list can be accessed with list.get(recordno).get(fieldno), each element is of type String */ public List> select(String sql, boolean includeBlobs) { Connection c = null; List> list = null; try { c = pm.getConnection(); list = select(c, sql, includeBlobs); c.close(); c = null; } catch (SQLException ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } finally { pm.closeConnectionFinally(c); } return list; } /** * Select a list of objects that match a given SQL statement * @param sql sql query string that designates the requested objects with ? at the position of params * @param includeBlobs true when content of blob coloumns should be returned, false if not * @param params list of parameters in the order they appear in the SQL string * @return a list of list objects, one for each record. An element in the * list can be accessed with list.get(recordno).get(fieldno), each element is of type String */ public List> select(String sql, boolean includeBlobs, Object... params) { Connection c = null; List> list = null; try { c = pm.getConnection(); list = select(c, sql, includeBlobs, params); c.close(); c = null; } catch (SQLException ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } finally { pm.closeConnectionFinally(c); } return list; } /* ---------- mit Connection ---------- */ /** * Select a list of objects that match a given SQL statement * @param c the database connection to use for this query, expected to be established and open already * @param sql sql query string that designates the requested objects * @return a list of map objects, one for each record. An element in the * list can be accessed with list.get(recordno).get("fieldname") */ public List> select(Connection c, String sql) { PreparedStatement ps = null; ResultSet rs = null; List> list = null; try { ps = c.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = ps.executeQuery(); list = pm.toList(rs); rs.close(); rs = null; ps.close(); ps = null; } catch (SQLException ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } finally { pm.closeResultSetFinally(rs); pm.closeStatementFinally(ps); } return list; } /** * Select a list of objects that match a given SQL statement * @param c the database connection to use for this query, expected to be established and open already * @param sql sql query string that designates the requested objects * @param includeBlobs true when content of blob coloumns should be returned, false if not * @return a list of list objects, one for each record. An element in the * list can be accessed with list.get(recordno).get(fieldno), each element is of type String */ public List> select(Connection c, String sql, boolean includeBlobs) { PreparedStatement ps = null; ResultSet rs = null; List> list = null; try { ps = c.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = ps.executeQuery(); list = pm.toList(rs, includeBlobs); rs.close(); rs = null; ps.close(); ps = null; } catch (SQLException ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } finally { pm.closeResultSetFinally(rs); pm.closeStatementFinally(ps); } return list; } /** * Select a list of objects that match a given SQL statement * @param c the database connection to use for this query, expected to be established and open already * @param sql sql query string that designates the requested objects with ? at the position of params * @param includeBlobs true when content of blob coloumns should be returned, false if not * @param params list of parameters in the order they appear in the SQL string * @return a list of list objects, one for each record. An element in the * list can be accessed with list.get(recordno).get(fieldno), each element is of type String */ public List> select(Connection c, String sql, boolean includeBlobs, Object... params) { PreparedStatement ps = null; ResultSet rs = null; List> list = null; try { ps = pm.buildQuery(c, sql, params); rs = ps.executeQuery(); list = pm.toList(rs, includeBlobs); rs.close(); rs = null; ps.close(); ps = null; } catch (Exception ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } finally { pm.closeResultSetFinally(rs); pm.closeStatementFinally(ps); } return list; } /** * Select a list of objects that match a given SQL statement * @param c the database connection to use for this query, expected to be established and open already * @param sql sql query string that designates the requested objects * @param record object to use to map db records to objects * @param includeBlobs true when BLOB contents are to be retrieved, false if not * @return a list of objects that match the given query */ public List select(Connection c, String sql, Record record, boolean includeBlobs) { PreparedStatement ps = null; ResultSet rs = null; ArrayList list = new ArrayList<>(); try { ps = c.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = ps.executeQuery(); if (rs.first()) { while (!rs.isAfterLast()) { list.add(record.toObject(rs, includeBlobs)); rs.next(); } } rs.close(); rs = null; ps.close(); ps = null; } catch (Exception ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } finally { pm.closeResultSetFinally(rs); pm.closeStatementFinally(ps); } return list; } /** * Select a list of objects that match a given SQL statement * @param c the database connection to use for this query, expected to be established and open already * @param sql sql query string that designates the requested objects * @param record object to use to map db records to objects * @param includeBlobs true when BLOB contents are to be retrieved, false if not * @param params list of parameters in the order they appear in the SQL string * @return a list of objects that match the given query */ public List select(Connection c, String sql, Record record, boolean includeBlobs, Object... params) { PreparedStatement ps = null; ResultSet rs = null; ArrayList list = new ArrayList<>(); try { //ps = c.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ps = pm.buildQuery(c, sql, params); rs = ps.executeQuery(); if (rs.first()) { while (!rs.isAfterLast()) { list.add(record.toObject(rs, includeBlobs)); rs.next(); } } rs.close(); rs = null; ps.close(); ps = null; } catch (Exception ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } finally { pm.closeResultSetFinally(rs); pm.closeStatementFinally(ps); } return list; } }