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 static de.uhilger.baselink.PersistenceManager.NULL_STR; |
|
21 |
import java.sql.Blob; |
|
22 |
import java.sql.ResultSet; |
|
23 |
import java.sql.ResultSetMetaData; |
|
24 |
import java.sql.SQLException; |
|
25 |
import java.sql.Types; |
|
26 |
import java.util.ArrayList; |
|
27 |
import java.util.List; |
|
28 |
import java.util.Map; |
|
29 |
import java.util.TreeMap; |
|
30 |
import java.util.logging.Logger; |
|
31 |
|
|
32 |
/** |
3bf522
|
33 |
* Methoden zur Umwandlung von ResultSets in Listen |
U |
34 |
* |
|
35 |
* @author Copyright (c) Ulrich Hilger, <a href="http://uhilger.de">http://uhilger.de</a> |
|
36 |
* @author Published under the terms and conditions of |
|
37 |
* the <a href="http://www.gnu.org/licenses/" target="_blank">GNU General Public License</a> |
|
38 |
* @version 1, January 22, 2024 |
a59fca
|
39 |
*/ |
U |
40 |
public class ListConverter { |
|
41 |
|
|
42 |
private static final Logger logger = Logger.getLogger(ListConverter.class.getName()); |
|
43 |
|
|
44 |
/** |
|
45 |
* Helper method that converts a ResultSet into a list of lists, one per row, |
|
46 |
* each row is a list of strings |
|
47 |
* |
|
48 |
* @param rs result set to convert |
|
49 |
* @param includeBlobs true when blob columns should be returned, false if not |
|
50 |
* @return a list of list objects, one for each record. An element in the |
|
51 |
* list can be accessed with list.get(recordno).get(fieldno), each element is of type String. |
|
52 |
* This first row has the field names |
|
53 |
* @throws java.sql.SQLException |
|
54 |
*/ |
|
55 |
public List<List<String>> toList(ResultSet rs, boolean includeBlobs) throws SQLException { |
|
56 |
List<List<String>> rows = new ArrayList<>(); |
|
57 |
ResultSetMetaData meta = rs.getMetaData(); |
|
58 |
int columnCount = meta.getColumnCount(); |
|
59 |
List<String> header = new ArrayList<>(); |
|
60 |
for (int i = 1; i <= columnCount; i++) { |
|
61 |
header.add(meta.getColumnName(i)); |
|
62 |
} |
|
63 |
rows.add(header); |
|
64 |
while (rs.next()) { |
|
65 |
List<String> row = new ArrayList<>(); |
|
66 |
for (int i = 1; i <= columnCount; i++) { |
|
67 |
String data = null; |
|
68 |
if (meta.getColumnType(i) == Types.BLOB) { |
|
69 |
if (includeBlobs) { |
|
70 |
Blob blob = rs.getBlob(i); |
|
71 |
if(blob != null) { |
|
72 |
data = new String(blob.getBytes((long) 1, (int) blob.length())); |
|
73 |
} |
|
74 |
} |
|
75 |
} else { |
|
76 |
Object o = rs.getObject(i); |
|
77 |
if(o != null) { |
|
78 |
data = o.toString(); |
|
79 |
} else { |
|
80 |
data = NULL_STR; |
|
81 |
} |
|
82 |
logger.finest(data); |
|
83 |
} |
|
84 |
row.add(data); |
|
85 |
} |
|
86 |
rows.add(row); |
|
87 |
} |
|
88 |
return rows; |
|
89 |
} |
|
90 |
|
|
91 |
/** |
|
92 |
* Helper method that maps a ResultSet into a list of maps, one per row |
|
93 |
* |
|
94 |
* @param rs database content to transform |
|
95 |
* @param wantedColumnNames list of columns names to include in the result map |
|
96 |
* @return list of maps, one per column row, with column names as keys |
|
97 |
* @throws SQLException if the connection fails |
|
98 |
*/ |
|
99 |
public List<Map<String, Object>> toList(ResultSet rs, List<String> wantedColumnNames) throws SQLException { |
|
100 |
// TODO BLOB handling |
|
101 |
List<Map<String, Object>> rows = new ArrayList<>(); |
|
102 |
int numWantedColumns = wantedColumnNames.size(); |
|
103 |
while (rs.next()) { |
|
104 |
Map<String, Object> row = new TreeMap<>(); |
|
105 |
for (int i = 0; i < numWantedColumns; ++i) { |
|
106 |
String columnName = wantedColumnNames.get(i); |
|
107 |
Object value = rs.getObject(columnName); |
|
108 |
if (value != null) { |
|
109 |
row.put(columnName, value); |
|
110 |
//Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).finest(columnName + " " + value); |
|
111 |
} else { |
|
112 |
row.put(columnName, ""); |
|
113 |
} |
|
114 |
} |
|
115 |
rows.add(row); |
|
116 |
} |
|
117 |
return rows; |
|
118 |
} |
|
119 |
|
|
120 |
/** |
|
121 |
* Helper method that converts a ResultSet into a list of maps, one per row |
|
122 |
* |
|
123 |
* @param rs database content to transform |
|
124 |
* @return list of maps, one per row, with column name as the key |
|
125 |
* @throws SQLException if the connection fails |
|
126 |
*/ |
|
127 |
public List<Map<String, Object>> toList(ResultSet rs) throws SQLException { |
|
128 |
List<String> wantedColumnNames = getColumnNames(rs); |
|
129 |
return toList(rs, wantedColumnNames); |
|
130 |
} |
|
131 |
|
|
132 |
/** |
|
133 |
* Return all column names as a list of strings |
|
134 |
* |
|
135 |
* @param database query result set |
|
136 |
* @return list of column name strings |
|
137 |
* @throws SQLException if the query fails |
|
138 |
*/ |
|
139 |
private List<String> getColumnNames(ResultSet rs) throws SQLException { |
|
140 |
List<String> columnNames = new ArrayList<String>(); |
|
141 |
ResultSetMetaData meta = rs.getMetaData(); |
|
142 |
int numColumns = meta.getColumnCount(); |
|
143 |
for (int i = 1; i <= numColumns; ++i) { |
|
144 |
String cName = meta.getColumnName(i); |
|
145 |
columnNames.add(cName); |
|
146 |
} |
|
147 |
return columnNames; |
|
148 |
} |
|
149 |
|
|
150 |
|
|
151 |
} |