KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > util > TransferResultSet


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.util;
33
34 import java.sql.ResultSet JavaDoc;
35 import java.sql.SQLException JavaDoc;
36 import java.util.Vector JavaDoc;
37
38 /**
39  * Helper class for transferring a result set
40  *
41  * @author Nicolas BAZIN
42  * @version 1.7.0
43  */

44 class TransferResultSet {
45
46     Vector JavaDoc vRows = null;
47     int iRowIdx;
48     int iMaxRowIdx;
49     int iColumnCount;
50     String JavaDoc[] sColumnNames = null;
51     int[] iColumnTypes = null;
52
53     TransferResultSet(ResultSet JavaDoc r) {
54
55         iRowIdx = 0;
56         iMaxRowIdx = 0;
57         iColumnCount = 0;
58         vRows = new Vector JavaDoc();
59
60         try {
61             while (r.next()) {
62                 if (sColumnNames == null) {
63                     iColumnCount = r.getMetaData().getColumnCount();
64                     sColumnNames = new String JavaDoc[iColumnCount + 1];
65                     iColumnTypes = new int[iColumnCount + 1];
66
67                     for (int Idx = 0; Idx < iColumnCount; Idx++) {
68                         sColumnNames[Idx + 1] =
69                             r.getMetaData().getColumnName(Idx + 1);
70                         iColumnTypes[Idx + 1] =
71                             r.getMetaData().getColumnType(Idx + 1);
72                     }
73
74                     vRows.addElement(null);
75                 }
76
77                 iMaxRowIdx++;
78
79                 Object JavaDoc[] Values = new Object JavaDoc[iColumnCount + 1];
80
81                 for (int Idx = 0; Idx < iColumnCount; Idx++) {
82                     Values[Idx + 1] = r.getObject(Idx + 1);
83                 }
84
85                 vRows.addElement(Values);
86             }
87         } catch (SQLException JavaDoc SQLE) {
88             iRowIdx = 0;
89             iMaxRowIdx = 0;
90             iColumnCount = 0;
91             vRows = new Vector JavaDoc();
92         }
93     }
94
95     TransferResultSet() {
96
97         iRowIdx = 0;
98         iMaxRowIdx = 0;
99         iColumnCount = 0;
100         vRows = new Vector JavaDoc();
101     }
102
103     void addRow(String JavaDoc[] Name, int[] type, Object JavaDoc[] Values,
104                 int nbColumns) throws Exception JavaDoc {
105
106         if ((Name.length != type.length) || (Name.length != Values.length)
107                 || (Name.length != (nbColumns + 1))) {
108             throw new Exception JavaDoc("Size of parameter incoherent");
109         }
110
111         if (sColumnNames == null) {
112             iColumnCount = nbColumns;
113             sColumnNames = Name;
114             iColumnTypes = type;
115
116             vRows.addElement(null);
117         }
118
119         if ((iMaxRowIdx > 0) && (this.getColumnCount() != nbColumns)) {
120             throw new Exception JavaDoc("Wrong number of columns: "
121                                 + this.getColumnCount()
122                                 + " column is expected");
123         }
124
125         iMaxRowIdx++;
126
127         vRows.addElement(Values);
128     }
129
130     boolean next() {
131
132         iRowIdx++;
133
134         return ((iRowIdx <= iMaxRowIdx) && (iMaxRowIdx > 0));
135     }
136
137     String JavaDoc getColumnName(int columnIdx) {
138
139         if ((iMaxRowIdx <= 0) || (iMaxRowIdx < iRowIdx)) {
140             return null;
141         }
142
143         return sColumnNames[columnIdx];
144     }
145
146     int getColumnCount() {
147
148         if ((iMaxRowIdx <= 0) || (iMaxRowIdx < iRowIdx)) {
149             return 0;
150         }
151
152         return iColumnCount;
153     }
154
155     int getColumnType(int columnIdx) {
156
157         if ((iMaxRowIdx <= 0) || (iMaxRowIdx < iRowIdx)) {
158             return 0;
159         }
160
161         return iColumnTypes[columnIdx];
162     }
163
164     Object JavaDoc getObject(int columnIdx) {
165
166         if ((iMaxRowIdx <= 0) || (iMaxRowIdx < iRowIdx)) {
167             return null;
168         }
169
170         return ((Object JavaDoc[]) vRows.elementAt(iRowIdx))[columnIdx];
171     }
172 }
173
Popular Tags