KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > servlet > jsp > jstl > sql > ResultImpl


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package javax.servlet.jsp.jstl.sql;
18
19 import java.io.Serializable JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.sql.ResultSetMetaData JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.SortedMap JavaDoc;
26 import java.util.TreeMap JavaDoc;
27
28 /**
29  * <p>This class creates a cached version of a <tt>ResultSet</tt>.
30  * It's represented as a <tt>Result</tt> implementation, capable of
31  * returing an array of <tt>Row</tt> objects containing a <tt>Column</tt>
32  * instance for each column in the row. It is not part of the JSTL
33  * API; it serves merely as a back-end to ResultSupport's static methods.
34  * Thus, we scope its access to the package.
35  *
36  * @author Hans Bergsten
37  * @author Justyna Horwat
38  */

39
40 class ResultImpl implements Result, Serializable JavaDoc {
41     private List JavaDoc rowMap;
42     private List JavaDoc rowByIndex;
43     private String JavaDoc[] columnNames;
44     private boolean isLimited;
45
46     /**
47      * This constructor reads the ResultSet and saves a cached
48      * copy.
49      * It's important to note that this object will be serializable only
50      * if the objects returned by the ResultSet are serializable too.
51      *
52      * @param rs an open <tt>ResultSet</tt>, positioned before the first
53      * row
54      * @param startRow beginning row to be cached
55      * @param maxRows query maximum rows limit
56      * @exception java.sql.SQLException if a database error occurs
57      */

58     public ResultImpl(ResultSet JavaDoc rs, int startRow, int maxRows)
59         throws SQLException JavaDoc
60     {
61         rowMap = new ArrayList JavaDoc();
62         rowByIndex = new ArrayList JavaDoc();
63
64         ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
65         int noOfColumns = rsmd.getColumnCount();
66
67         // Create the column name array
68
columnNames = new String JavaDoc[noOfColumns];
69         for (int i = 1; i <= noOfColumns; i++) {
70             columnNames[i-1] = rsmd.getColumnName(i);
71         }
72
73         // Throw away all rows upto startRow
74
for (int i = 0; i < startRow; i++) {
75             rs.next();
76         }
77
78         // Process the remaining rows upto maxRows
79
int processedRows = 0;
80         while (rs.next()) {
81             if ((maxRows != -1) && (processedRows == maxRows)) {
82                 isLimited = true;
83                 break;
84             }
85             Object JavaDoc[] columns = new Object JavaDoc[noOfColumns];
86             SortedMap JavaDoc columnMap =
87                 new TreeMap JavaDoc(String.CASE_INSENSITIVE_ORDER);
88
89             // JDBC uses 1 as the lowest index!
90
for (int i = 1; i <= noOfColumns; i++) {
91                 Object JavaDoc value = rs.getObject(i);
92                 if (rs.wasNull()) {
93                     value = null;
94                 }
95                 columns[i-1] = value;
96                 columnMap.put(columnNames[i-1], value);
97             }
98             rowMap.add(columnMap);
99             rowByIndex.add(columns);
100             processedRows++;
101         }
102     }
103
104     /**
105      * Returns an array of SortedMap objects. The SortedMap
106      * object key is the ColumnName and the value is the ColumnValue.
107      * SortedMap was created using the CASE_INSENSITIVE_ORDER
108      * Comparator so the key is the case insensitive representation
109      * of the ColumnName.
110      *
111      * @return an array of Map, or null if there are no rows
112      */

113     public SortedMap JavaDoc[] getRows() {
114         if (rowMap == null) {
115             return null;
116         }
117
118         //should just be able to return SortedMap[] object
119
return (SortedMap JavaDoc []) rowMap.toArray(new SortedMap JavaDoc[0]);
120     }
121
122
123     /**
124      * Returns an array of Object[] objects. The first index
125      * designates the Row, the second the Column. The array
126      * stores the value at the specified row and column.
127      *
128      * @return an array of Object[], or null if there are no rows
129      */

130     public Object JavaDoc[][] getRowsByIndex() {
131         if (rowByIndex == null) {
132             return null;
133         }
134
135         //should just be able to return Object[][] object
136
return (Object JavaDoc [][])rowByIndex.toArray(new Object JavaDoc[0][0]);
137     }
138
139     /**
140      * Returns an array of String objects. The array represents
141      * the names of the columns arranged in the same order as in
142      * the getRowsByIndex() method.
143      *
144      * @return an array of String[]
145      */

146     public String JavaDoc[] getColumnNames() {
147         return columnNames;
148     }
149
150     /**
151      * Returns the number of rows in the cached ResultSet
152      *
153      * @return the number of cached rows, or -1 if the Result could
154      * not be initialized due to SQLExceptions
155      */

156     public int getRowCount() {
157         if (rowMap == null) {
158             return -1;
159         }
160         return rowMap.size();
161     }
162
163     /**
164      * Returns true if the query was limited by a maximum row setting
165      *
166      * @return true if the query was limited by a MaxRows attribute
167      */

168     public boolean isLimitedByMaxRows() {
169         return isLimited;
170     }
171
172 }
173
Popular Tags