/*
|
* 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 static de.uhilger.baselink.PersistenceManager.NULL_STR;
|
import java.sql.Blob;
|
import java.sql.ResultSet;
|
import java.sql.ResultSetMetaData;
|
import java.sql.SQLException;
|
import java.sql.Types;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.TreeMap;
|
import java.util.logging.Logger;
|
|
/**
|
* Methoden zur Umwandlung von ResultSets in Listen
|
*
|
* @author Copyright (c) Ulrich Hilger, <a href="http://uhilger.de">http://uhilger.de</a>
|
* @author Published under the terms and conditions of
|
* the <a href="http://www.gnu.org/licenses/" target="_blank">GNU General Public License</a>
|
* @version 1, January 22, 2024
|
*/
|
public class ListConverter {
|
|
private static final Logger logger = Logger.getLogger(ListConverter.class.getName());
|
|
/**
|
* Helper method that converts a ResultSet into a list of lists, one per row,
|
* each row is a list of strings
|
*
|
* @param rs result set to convert
|
* @param includeBlobs true when blob columns 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.
|
* This first row has the field names
|
* @throws java.sql.SQLException
|
*/
|
public List<List<String>> toList(ResultSet rs, boolean includeBlobs) throws SQLException {
|
List<List<String>> rows = new ArrayList<>();
|
ResultSetMetaData meta = rs.getMetaData();
|
int columnCount = meta.getColumnCount();
|
List<String> header = new ArrayList<>();
|
for (int i = 1; i <= columnCount; i++) {
|
header.add(meta.getColumnName(i));
|
}
|
rows.add(header);
|
while (rs.next()) {
|
List<String> row = new ArrayList<>();
|
for (int i = 1; i <= columnCount; i++) {
|
String data = null;
|
if (meta.getColumnType(i) == Types.BLOB) {
|
if (includeBlobs) {
|
Blob blob = rs.getBlob(i);
|
if(blob != null) {
|
data = new String(blob.getBytes((long) 1, (int) blob.length()));
|
}
|
}
|
} else {
|
Object o = rs.getObject(i);
|
if(o != null) {
|
data = o.toString();
|
} else {
|
data = NULL_STR;
|
}
|
logger.finest(data);
|
}
|
row.add(data);
|
}
|
rows.add(row);
|
}
|
return rows;
|
}
|
|
/**
|
* Helper method that maps a ResultSet into a list of maps, one per row
|
*
|
* @param rs database content to transform
|
* @param wantedColumnNames list of columns names to include in the result map
|
* @return list of maps, one per column row, with column names as keys
|
* @throws SQLException if the connection fails
|
*/
|
public List<Map<String, Object>> toList(ResultSet rs, List<String> wantedColumnNames) throws SQLException {
|
// TODO BLOB handling
|
List<Map<String, Object>> rows = new ArrayList<>();
|
int numWantedColumns = wantedColumnNames.size();
|
while (rs.next()) {
|
Map<String, Object> row = new TreeMap<>();
|
for (int i = 0; i < numWantedColumns; ++i) {
|
String columnName = wantedColumnNames.get(i);
|
Object value = rs.getObject(columnName);
|
if (value != null) {
|
row.put(columnName, value);
|
//Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).finest(columnName + " " + value);
|
} else {
|
row.put(columnName, "");
|
}
|
}
|
rows.add(row);
|
}
|
return rows;
|
}
|
|
/**
|
* Helper method that converts a ResultSet into a list of maps, one per row
|
*
|
* @param rs database content to transform
|
* @return list of maps, one per row, with column name as the key
|
* @throws SQLException if the connection fails
|
*/
|
public List<Map<String, Object>> toList(ResultSet rs) throws SQLException {
|
List<String> wantedColumnNames = getColumnNames(rs);
|
return toList(rs, wantedColumnNames);
|
}
|
|
/**
|
* Return all column names as a list of strings
|
*
|
* @param database query result set
|
* @return list of column name strings
|
* @throws SQLException if the query fails
|
*/
|
private List<String> getColumnNames(ResultSet rs) throws SQLException {
|
List<String> columnNames = new ArrayList<String>();
|
ResultSetMetaData meta = rs.getMetaData();
|
int numColumns = meta.getColumnCount();
|
for (int i = 1; i <= numColumns; ++i) {
|
String cName = meta.getColumnName(i);
|
columnNames.add(cName);
|
}
|
return columnNames;
|
}
|
|
|
}
|