1 /*
2 * The Apache Software License, Version 1.1
3 *
4 * Copyright (c) 1999 The Apache Software Foundation. All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. The end-user documentation included with the redistribution, if
20 * any, must include the following acknowlegement:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowlegement may appear in the software itself,
24 * if and wherever such third-party acknowlegements normally appear.
25 *
26 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
27 * Foundation" must not be used to endorse or promote products derived
28 * from this software without prior written permission. For written
29 * permission, please contact apache@apache.org.
30 *
31 * 5. Products derived from this software may not be called "Apache"
32 * nor may "Apache" appear in their names without prior written
33 * permission of the Apache Group.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
53 *
54 */
55
56 package javax.servlet.jsp.jstl.sql;
57
58 import java.sql.*;
59 import java.util.*;
60
61 /***
62 * <p>This class creates a cached version of a <tt>ResultSet</tt>.
63 * It's represented as a <tt>Result</tt> implementation, capable of
64 * returing an array of <tt>Row</tt> objects containing a <tt>Column</tt>
65 * instance for each column in the row. It is not part of the JSTL
66 * API; it serves merely as a back-end to ResultSupport's static methods.
67 * Thus, we scope its access to the package.
68 *
69 * @author Hans Bergsten
70 * @author Justyna Horwat
71 */
72
73 class ResultImpl implements Result {
74 private List rowMap;
75 private List rowByIndex;
76 private String[] columnNames;
77 private boolean isLimited;
78
79 /***
80 * This constructor reads the ResultSet and saves a cached
81 * copy.
82 *
83 * @param rs an open <tt>ResultSet</tt>, positioned before the first
84 * row
85 * @param startRow, beginning row to be cached
86 * @param maxRows, query maximum rows limit
87 * @exception if a database error occurs
88 */
89 public ResultImpl(ResultSet rs, int startRow, int maxRows)
90 throws SQLException
91 {
92 rowMap = new ArrayList();
93 rowByIndex = new ArrayList();
94
95 ResultSetMetaData rsmd = rs.getMetaData();
96 int noOfColumns = rsmd.getColumnCount();
97
98 // Create the column name array
99 columnNames = new String[noOfColumns];
100 for (int i = 1; i <= noOfColumns; i++) {
101 columnNames[i-1] = rsmd.getColumnName(i);
102 }
103
104 // Throw away all rows upto startRow
105 for (int i = 0; i < startRow; i++) {
106 rs.next();
107 }
108
109 // Process the remaining rows upto maxRows
110 int processedRows = 0;
111 while (rs.next()) {
112 if ((maxRows != -1) && (processedRows == maxRows)) {
113 isLimited = true;
114 break;
115 }
116 Object[] columns = new Object[noOfColumns];
117 SortedMap columnMap =
118 new TreeMap(String.CASE_INSENSITIVE_ORDER);
119
120 // JDBC uses 1 as the lowest index!
121 for (int i = 1; i <= noOfColumns; i++) {
122 Object value = rs.getObject(i);
123 if (rs.wasNull()) {
124 value = null;
125 }
126 columns[i-1] = value;
127 columnMap.put(columnNames[i-1], value);
128 }
129 rowMap.add(columnMap);
130 rowByIndex.add(columns);
131 processedRows++;
132 }
133 }
134
135 /***
136 * Returns an array of SortedMap objects. The SortedMap
137 * object key is the ColumnName and the value is the ColumnValue.
138 * SortedMap was created using the CASE_INSENSITIVE_ORDER
139 * Comparator so the key is the case insensitive representation
140 * of the ColumnName.
141 *
142 * @return an array of Map, or null if there are no rows
143 */
144 public SortedMap[] getRows() {
145 if (rowMap == null) {
146 return null;
147 }
148
149 //should just be able to return SortedMap[] object
150 return (SortedMap []) rowMap.toArray(new SortedMap[0]);
151 }
152
153
154 /***
155 * Returns an array of Object[] objects. The first index
156 * designates the Row, the second the Column. The array
157 * stores the value at the specified row and column.
158 *
159 * @return an array of Object[], or null if there are no rows
160 */
161 public Object[][] getRowsByIndex() {
162 if (rowByIndex == null) {
163 return null;
164 }
165
166 //should just be able to return Object[][] object
167 return (Object [][])rowByIndex.toArray(new Object[0][0]);
168 }
169
170 /***
171 * Returns an array of String objects. The array represents
172 * the names of the columns arranged in the same order as in
173 * the getRowsByIndex() method.
174 *
175 * @return an array of String[]
176 */
177 public String[] getColumnNames() {
178 return columnNames;
179 }
180
181 /***
182 * Returns the number of rows in the cached ResultSet
183 *
184 * @return the number of cached rows, or -1 if the Result could
185 * not be initialized due to SQLExceptions
186 */
187 public int getRowCount() {
188 if (rowMap == null) {
189 return -1;
190 }
191 return rowMap.size();
192 }
193
194 /***
195 * Returns true of the query was limited by a maximum row setting
196 *
197 * @return true if the query was limited by a MaxRows attribute
198 */
199 public boolean isLimitedByMaxRows() {
200 return isLimited;
201 }
202
203 }
This page was automatically generated by Maven