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.common.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 *
027 * <h5 class='section'>See Also:</h5><ul>
028
029 * </ul>
030 *
031 * @serial exclude
032 */
033public final class ResultSetList extends LinkedList<Map<String,Object>> {
034
035   private static final long serialVersionUID = 1L;
036
037   /**
038    * Constructor.
039    *
040    * @param rs The result set to load into this DTO.
041    * @param pos The start position (zero-indexed).
042    * @param limit The maximum number of rows to retrieve.
043    * @param includeRowNums Make the first column be the row number.
044    * @throws SQLException Database error.
045    */
046   public ResultSetList(ResultSet rs, int pos, int limit, boolean includeRowNums) throws SQLException {
047      try {
048         int rowNum = pos;
049
050         // Get the column names.
051         ResultSetMetaData rsmd = rs.getMetaData();
052         int offset = (includeRowNums ? 1 : 0);
053         int cc = rsmd.getColumnCount();
054         String[] columns = new String[cc + offset];
055         if (includeRowNums)
056            columns[0] = "ROW";
057         int[] colTypes = new int[cc];
058
059         for (int i = 0; i < cc; i++) {
060            columns[i+offset] = rsmd.getColumnName(i+1);
061            colTypes[i] = rsmd.getColumnType(i+1);
062         }
063
064         while (--pos > 0 && rs.next()) {}
065
066         // Get the rows.
067         while (limit-- > 0 && rs.next()) {
068            Object[] row = new Object[cc + offset];
069            if (includeRowNums)
070               row[0] = rowNum++;
071            for (int i = 0; i < cc; i++) {
072               Object o = readEntry(rs, i+1, colTypes[i]);
073               row[i+offset] = o;
074            }
075            add(new SimpleMap<>(columns, row));
076         }
077      } finally {
078         rs.close();
079      }
080   }
081
082   /**
083    * Reads the specified column from the current row in the result set.
084    *
085    * <p>
086    * Subclasses can override this method to handle specific data types in special ways.
087    *
088    * @param rs The result set to read from.
089    * @param col The column number (indexed by 1).
090    * @param dataType The {@link Types type} of the entry.
091    * @return The entry as an Object.
092    */
093   protected static Object readEntry(ResultSet rs, int col, int dataType) {
094      try {
095         switch (dataType) {
096            case Types.BLOB:
097               Blob b = rs.getBlob(col);
098               return "blob["+b.length()+"]";
099            case Types.CLOB:
100               Clob c = rs.getClob(col);
101               return "clob["+c.length()+"]";
102            case Types.LONGVARBINARY:
103               return "longvarbinary["+count(rs.getBinaryStream(col))+"]";
104            case Types.LONGVARCHAR:
105               return "longvarchar["+count(rs.getAsciiStream(col))+"]";
106            case Types.LONGNVARCHAR:
107               return "longnvarchar["+count(rs.getCharacterStream(col))+"]";
108            case Types.TIMESTAMP:
109               return rs.getTimestamp(col);  // Oracle returns com.oracle.TIMESTAMP objects from getObject() which isn't a Timestamp.
110            default:
111               return rs.getObject(col);
112         }
113      } catch (Exception e) {
114         return e.getLocalizedMessage();
115      }
116   }
117}