1 | /* |
---|
2 | Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. |
---|
3 | |
---|
4 | |
---|
5 | The MySQL Connector/J is licensed under the terms of the GPLv2 |
---|
6 | <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors. |
---|
7 | There are special exceptions to the terms and conditions of the GPLv2 as it is applied to |
---|
8 | this software, see the FLOSS License Exception |
---|
9 | <http://www.mysql.com/about/legal/licensing/foss-exception.html>. |
---|
10 | |
---|
11 | This program is free software; you can redistribute it and/or modify it under the terms |
---|
12 | of the GNU General Public License as published by the Free Software Foundation; version 2 |
---|
13 | of the License. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
---|
16 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
---|
17 | See the GNU General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License along with this |
---|
20 | program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth |
---|
21 | Floor, Boston, MA 02110-1301 USA |
---|
22 | |
---|
23 | |
---|
24 | |
---|
25 | */ |
---|
26 | package com.mysql.jdbc; |
---|
27 | |
---|
28 | import java.sql.SQLException; |
---|
29 | import java.util.List; |
---|
30 | |
---|
31 | /** |
---|
32 | * Represents an in-memory result set |
---|
33 | * |
---|
34 | * @author dgan |
---|
35 | * @version $Id$ |
---|
36 | */ |
---|
37 | public class RowDataStatic implements RowData { |
---|
38 | private Field[] metadata; |
---|
39 | |
---|
40 | private int index; |
---|
41 | |
---|
42 | ResultSetImpl owner; |
---|
43 | |
---|
44 | private List<ResultSetRow> rows; |
---|
45 | |
---|
46 | /** |
---|
47 | * Creates a new RowDataStatic object. |
---|
48 | * |
---|
49 | * @param rows |
---|
50 | * DOCUMENT ME! |
---|
51 | */ |
---|
52 | public RowDataStatic(List<ResultSetRow> rows) { |
---|
53 | this.index = -1; |
---|
54 | this.rows = rows; |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * DOCUMENT ME! |
---|
59 | * |
---|
60 | * @param row |
---|
61 | * DOCUMENT ME! |
---|
62 | */ |
---|
63 | public void addRow(ResultSetRow row) { |
---|
64 | this.rows.add(row); |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * Moves to after last. |
---|
69 | */ |
---|
70 | public void afterLast() { |
---|
71 | this.index = this.rows.size(); |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * Moves to before first. |
---|
76 | */ |
---|
77 | public void beforeFirst() { |
---|
78 | this.index = -1; |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * DOCUMENT ME! |
---|
83 | */ |
---|
84 | public void beforeLast() { |
---|
85 | this.index = this.rows.size() - 2; |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * DOCUMENT ME! |
---|
90 | */ |
---|
91 | public void close() { |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * DOCUMENT ME! |
---|
96 | * |
---|
97 | * @param atIndex |
---|
98 | * DOCUMENT ME! |
---|
99 | * |
---|
100 | * @return DOCUMENT ME! |
---|
101 | */ |
---|
102 | public ResultSetRow getAt(int atIndex) throws SQLException { |
---|
103 | if ((atIndex < 0) || (atIndex >= this.rows.size())) { |
---|
104 | return null; |
---|
105 | } |
---|
106 | |
---|
107 | return (this.rows.get(atIndex)).setMetadata(this.metadata); |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * DOCUMENT ME! |
---|
112 | * |
---|
113 | * @return DOCUMENT ME! |
---|
114 | */ |
---|
115 | public int getCurrentRowNumber() { |
---|
116 | return this.index; |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | * @see com.mysql.jdbc.RowData#getOwner() |
---|
121 | */ |
---|
122 | public ResultSetInternalMethods getOwner() { |
---|
123 | return this.owner; |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * DOCUMENT ME! |
---|
128 | * |
---|
129 | * @return DOCUMENT ME! |
---|
130 | */ |
---|
131 | public boolean hasNext() { |
---|
132 | boolean hasMore = (this.index + 1) < this.rows.size(); |
---|
133 | |
---|
134 | return hasMore; |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * Returns true if we got the last element. |
---|
139 | * |
---|
140 | * @return DOCUMENT ME! |
---|
141 | */ |
---|
142 | public boolean isAfterLast() { |
---|
143 | return this.index >= this.rows.size(); |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * Returns if iteration has not occured yet. |
---|
148 | * |
---|
149 | * @return DOCUMENT ME! |
---|
150 | */ |
---|
151 | public boolean isBeforeFirst() { |
---|
152 | return (this.index == -1) && (this.rows.size() != 0); |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | * DOCUMENT ME! |
---|
157 | * |
---|
158 | * @return DOCUMENT ME! |
---|
159 | */ |
---|
160 | public boolean isDynamic() { |
---|
161 | return false; |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * DOCUMENT ME! |
---|
166 | * |
---|
167 | * @return DOCUMENT ME! |
---|
168 | */ |
---|
169 | public boolean isEmpty() { |
---|
170 | return this.rows.size() == 0; |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | * DOCUMENT ME! |
---|
175 | * |
---|
176 | * @return DOCUMENT ME! |
---|
177 | */ |
---|
178 | public boolean isFirst() { |
---|
179 | return this.index == 0; |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * DOCUMENT ME! |
---|
184 | * |
---|
185 | * @return DOCUMENT ME! |
---|
186 | */ |
---|
187 | public boolean isLast() { |
---|
188 | // |
---|
189 | // You can never be on the 'last' row of |
---|
190 | // an empty result set |
---|
191 | // |
---|
192 | if (this.rows.size() == 0) { |
---|
193 | return false; |
---|
194 | } |
---|
195 | |
---|
196 | return (this.index == (this.rows.size() - 1)); |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * DOCUMENT ME! |
---|
201 | * |
---|
202 | * @param rows |
---|
203 | * DOCUMENT ME! |
---|
204 | */ |
---|
205 | public void moveRowRelative(int rowsToMove) { |
---|
206 | this.index += rowsToMove; |
---|
207 | } |
---|
208 | |
---|
209 | /** |
---|
210 | * DOCUMENT ME! |
---|
211 | * |
---|
212 | * @return DOCUMENT ME! |
---|
213 | */ |
---|
214 | public ResultSetRow next() throws SQLException { |
---|
215 | this.index++; |
---|
216 | |
---|
217 | if (this.index < this.rows.size()) { |
---|
218 | ResultSetRow row = this.rows.get(this.index); |
---|
219 | |
---|
220 | return row.setMetadata(this.metadata); |
---|
221 | } |
---|
222 | |
---|
223 | return null; |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | * DOCUMENT ME! |
---|
228 | * |
---|
229 | * @param atIndex |
---|
230 | * DOCUMENT ME! |
---|
231 | */ |
---|
232 | public void removeRow(int atIndex) { |
---|
233 | this.rows.remove(atIndex); |
---|
234 | } |
---|
235 | |
---|
236 | /** |
---|
237 | * DOCUMENT ME! |
---|
238 | * |
---|
239 | * @param newIndex |
---|
240 | * DOCUMENT ME! |
---|
241 | */ |
---|
242 | public void setCurrentRow(int newIndex) { |
---|
243 | this.index = newIndex; |
---|
244 | } |
---|
245 | |
---|
246 | /** |
---|
247 | * @see com.mysql.jdbc.RowData#setOwner(com.mysql.jdbc.ResultSetInternalMethods) |
---|
248 | */ |
---|
249 | public void setOwner(ResultSetImpl rs) { |
---|
250 | this.owner = rs; |
---|
251 | } |
---|
252 | |
---|
253 | /** |
---|
254 | * DOCUMENT ME! |
---|
255 | * |
---|
256 | * @return DOCUMENT ME! |
---|
257 | */ |
---|
258 | public int size() { |
---|
259 | return this.rows.size(); |
---|
260 | } |
---|
261 | |
---|
262 | public boolean wasEmpty() { |
---|
263 | return (this.rows != null && this.rows.size() == 0); |
---|
264 | } |
---|
265 | |
---|
266 | public void setMetadata(Field[] metadata) { |
---|
267 | this.metadata = metadata; |
---|
268 | } |
---|
269 | } |
---|