001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.dto;
014
015import static org.apache.juneau.internal.IOUtils.*;
016
017import java.sql.*;
018import java.util.*;
019
020import org.apache.juneau.internal.*;
021
022/**
023 * Transforms an SQL {@link ResultSet ResultSet} into a list of maps.
024 * <p>
025 * Loads the entire result set into an in-memory data structure, and then closes the result set object.
026 */
027public final class ResultSetList extends LinkedList<Map<String,Object>> {
028
029   private static final long serialVersionUID = 1L;
030
031   /**
032    * Constructor.
033    *
034    * @param rs The result set to load into this DTO.
035    * @param pos The start position (zero-indexed).
036    * @param limit The maximum number of rows to retrieve.
037    * @param includeRowNums Make the first column be the row number.
038    * @throws SQLException Database error.
039    */
040   public ResultSetList(ResultSet rs, int pos, int limit, boolean includeRowNums) throws SQLException {
041      try {
042         int rowNum = pos;
043
044         // Get the column names.
045         ResultSetMetaData rsmd = rs.getMetaData();
046         int offset = (includeRowNums ? 1 : 0);
047         int cc = rsmd.getColumnCount();
048         String[] columns = new String[cc + offset];
049         if (includeRowNums)
050            columns[0] = "ROW";
051         int[] colTypes = new int[cc];
052
053         for (int i = 0; i < cc; i++) {
054            columns[i+offset] = rsmd.getColumnName(i+1);
055            colTypes[i] = rsmd.getColumnType(i+1);
056         }
057
058         while (--pos > 0 && rs.next()) {}
059
060         // Get the rows.
061         while (limit-- > 0 && rs.next()) {
062            Object[] row = new Object[cc + offset];
063            if (includeRowNums)
064               row[0] = rowNum++;
065            for (int i = 0; i < cc; i++) {
066               Object o = readEntry(rs, i+1, colTypes[i]);
067               row[i+offset] = o;
068            }
069            add(new SimpleMap<>(columns, row));
070         }
071      } finally {
072         rs.close();
073      }
074   }
075
076   /**
077    * Reads the specified column from the current row in the result set.
078    *
079    * <p>
080    * Subclasses can override this method to handle specific data types in special ways.
081    *
082    * @param rs The result set to read from.
083    * @param col The column number (indexed by 1).
084    * @param dataType The {@link Types type} of the entry.
085    * @return The entry as an Object.
086    */
087   protected static Object readEntry(ResultSet rs, int col, int dataType) {
088      try {
089         switch (dataType) {
090            case Types.BLOB:
091               Blob b = rs.getBlob(col);
092               return "blob["+b.length()+"]";
093            case Types.CLOB:
094               Clob c = rs.getClob(col);
095               return "clob["+c.length()+"]";
096            case Types.LONGVARBINARY:
097               return "longvarbinary["+count(rs.getBinaryStream(col))+"]";
098            case Types.LONGVARCHAR:
099               return "longvarchar["+count(rs.getAsciiStream(col))+"]";
100            case Types.LONGNVARCHAR:
101               return "longnvarchar["+count(rs.getCharacterStream(col))+"]";
102            case Types.TIMESTAMP:
103               return rs.getTimestamp(col);  // Oracle returns com.oracle.TIMESTAMP objects from getObject() which isn't a Timestamp.
104            default:
105               return rs.getObject(col);
106         }
107      } catch (Exception e) {
108         return e.getLocalizedMessage();
109      }
110   }
111}