KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > ResultImpl


1 /*
2  * Copyright (c) 1998-2003 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jstl;
30
31 import javax.servlet.jsp.jstl.sql.Result;
32 import java.sql.ResultSet JavaDoc;
33 import java.sql.ResultSetMetaData JavaDoc;
34 import java.sql.SQLException JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.SortedMap JavaDoc;
37 import java.util.TreeMap JavaDoc;
38
39 /**
40  * Returns the result of a SQL query.
41  */

42 public class ResultImpl implements Result {
43   private ArrayList JavaDoc _rows = new ArrayList JavaDoc();
44   private Object JavaDoc [][]_objectRows;
45   private SortedMap JavaDoc []_sortedRows;
46   private String JavaDoc []_columnNames;
47   private boolean _isLimitedByMaxRows;
48
49   public ResultImpl(ResultSet JavaDoc rs, int maxRows)
50     throws SQLException JavaDoc
51   {
52     ResultSetMetaData JavaDoc metaData = rs.getMetaData();
53     int columnCount = metaData.getColumnCount();
54
55     _columnNames = new String JavaDoc[columnCount];
56     for (int i = 0; i < _columnNames.length; i++)
57       _columnNames[i] = metaData.getColumnName(i + 1);
58
59     if (maxRows < 0)
60       maxRows = Integer.MAX_VALUE;
61
62     while (rs.next() && maxRows-- > 0) {
63       Object JavaDoc []row = new Object JavaDoc[columnCount];
64
65       for (int i = 0; i < columnCount; i++)
66         row[i] = rs.getObject(i + 1);
67
68       _rows.add(row);
69     }
70
71     if (maxRows == 0)
72       _isLimitedByMaxRows = true;
73   }
74
75   public SortedMap JavaDoc[] getRows()
76   {
77     if (_sortedRows == null) {
78       _sortedRows = new SortedMap JavaDoc[_rows.size()];
79
80       for (int i = _rows.size() - 1; i >=0; i--) {
81         SortedMap JavaDoc map = new TreeMap JavaDoc(String.CASE_INSENSITIVE_ORDER);
82         _sortedRows[i] = map;
83         Object JavaDoc []row = (Object JavaDoc []) _rows.get(i);
84
85         for (int j = _columnNames.length - 1; j >= 0; j--)
86           map.put(_columnNames[j], row[j]);
87       }
88     }
89
90     return _sortedRows;
91   }
92
93   public Object JavaDoc[][] getRowsByIndex()
94   {
95     if (_objectRows == null) {
96       _objectRows = new Object JavaDoc[_rows.size()][];
97       _rows.toArray(_objectRows);
98     }
99
100     return _objectRows;
101   }
102
103   public String JavaDoc []getColumnNames()
104   {
105     return _columnNames;
106   }
107
108   public int getRowCount()
109   {
110     return _rows.size();
111   }
112
113   public boolean isLimitedByMaxRows()
114   {
115     return _isLimitedByMaxRows;
116   }
117 }
118
Popular Tags