/*
* 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, http://uhilger.de
* @author Published under the terms and conditions of
* the GNU General Public License
* @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> toList(ResultSet rs, boolean includeBlobs) throws SQLException {
List> rows = new ArrayList<>();
ResultSetMetaData meta = rs.getMetaData();
int columnCount = meta.getColumnCount();
List header = new ArrayList<>();
for (int i = 1; i <= columnCount; i++) {
header.add(meta.getColumnName(i));
}
rows.add(header);
while (rs.next()) {
List 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