ulrich
2024-01-22 a59fcaadb5c928a243f198c91d69313234e07afe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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;
 
/**
 *
 * @author Ulrich Hilger
 */
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;
  }
  
  
}