56380
|
1 |
/*
|
56797
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
56380
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
56797
|
25 |
package jdk.incubator.sql2;
|
56380
|
26 |
|
56797
|
27 |
import java.util.ArrayList;
|
|
28 |
import java.util.Iterator;
|
|
29 |
import java.util.List;
|
|
30 |
import java.util.NoSuchElementException;
|
|
31 |
import java.util.Spliterator;
|
|
32 |
import java.util.function.Consumer;
|
56380
|
33 |
|
|
34 |
/**
|
|
35 |
*
|
|
36 |
*/
|
|
37 |
public interface Result {
|
|
38 |
|
|
39 |
/**
|
56997
|
40 |
* A {@code Result} that is just a number of rows modified, a {@link Long}.
|
56380
|
41 |
*
|
56797
|
42 |
* Note: It is certainly true that this is not needed; {@link Long} could be
|
|
43 |
* used instead. Seems like there might be a documentational advantage to
|
|
44 |
* having this type. If you don't like it, just mentally replace it with
|
|
45 |
* {@link Long} everywhere it appears.
|
56380
|
46 |
*/
|
56797
|
47 |
public static interface RowCount extends Result {
|
56380
|
48 |
|
|
49 |
/**
|
|
50 |
*
|
56797
|
51 |
* @return
|
56380
|
52 |
*/
|
|
53 |
public long getCount();
|
|
54 |
}
|
|
55 |
|
|
56 |
/**
|
56797
|
57 |
* A mutable handle to one value of an ordered sequence of columns of a row or
|
|
58 |
* of out parameters. Columns have a 1-based index and optionally an
|
56997
|
59 |
* identifier. Identifiers are not guaranteed to be unique.
|
|
60 |
* <br>
|
|
61 |
* A newly created Column is initially positioned on the first column of
|
|
62 |
* it's sequence. The position is modified by calls to {@link #at(int)},
|
|
63 |
* {@link #at(String)}, {@link #next()}, or {@link #next(int)}.
|
|
64 |
* The methods {@link #clone()}, {@link #slice(int)},
|
|
65 |
* {@link #forEach(Consumer)}, and {@link #iterator()} create new instances.
|
|
66 |
* All other methods return this instance (modifying it if necessary)
|
|
67 |
* including {@link #next()}, and {@link #forEachRemaining(Consumer)}.
|
|
68 |
* <br>
|
|
69 |
* In cases where the result of an operation has no columns, an instance of
|
|
70 |
* this class may represent an empty sequence of columns. Instances
|
|
71 |
* associated to the empty sequence return 0 from calls to {@link #index()}
|
|
72 |
* and {@link #absoluteIndex()}. It is illegal to modify the position or
|
|
73 |
* accesses any attribute of a column if the sequence is empty.
|
56380
|
74 |
*/
|
56797
|
75 |
public interface Column extends Result, Iterable<Column>, Iterator<Column>, Cloneable {
|
|
76 |
|
|
77 |
/**
|
|
78 |
* Return the value of this column as an instance of the given type.
|
|
79 |
*
|
|
80 |
* @param <T>
|
|
81 |
* @param type
|
|
82 |
* @return the value of this {@link Column}
|
56997
|
83 |
* @throws IllegalStateException if the column sequence is empty.
|
56797
|
84 |
*/
|
|
85 |
public <T> T get(Class<T> type);
|
|
86 |
|
|
87 |
/**
|
|
88 |
* Return the value of this {@link Column} as an instance of the default
|
|
89 |
* Java type for this column.
|
|
90 |
*
|
|
91 |
* @param <T>
|
|
92 |
* @return the value of this {@link Column}
|
56997
|
93 |
* @throws IllegalStateException if the column sequence is empty.
|
56797
|
94 |
*/
|
|
95 |
public default <T> T get() {
|
|
96 |
return get(javaType());
|
|
97 |
}
|
|
98 |
|
|
99 |
/**
|
|
100 |
* Return the identifier of this {@link Column}. May be null.
|
|
101 |
*
|
|
102 |
* @return the identifier of this {@link Column}. May be null
|
56997
|
103 |
* @throws IllegalStateException if the column sequence is empty.
|
56797
|
104 |
*/
|
|
105 |
public String identifier();
|
|
106 |
|
|
107 |
/**
|
56997
|
108 |
* Return the 1-based index of this {@link Column}. If the column
|
|
109 |
* sequence is empty, the return value is 0.
|
|
110 |
* <br>
|
|
111 |
* The returned value is relative to the slice if this {@link Column} is
|
|
112 |
* the result of a call to {@link #slice(int)}.
|
|
113 |
* <br>
|
|
114 |
* {@code col.slice(n).index() == 1}.
|
|
115 |
* <br>
|
|
116 |
* {@code col.slice(n).next().index() == 2}.
|
|
117 |
*
|
56797
|
118 |
* @return the index of this {@link Column}
|
|
119 |
*/
|
|
120 |
public int index();
|
|
121 |
|
|
122 |
/**
|
|
123 |
* Return the 1-based index of this {@link Column} relative to the original
|
56997
|
124 |
* sequence of values. If the column sequence is empty, the return value
|
|
125 |
* is 0.
|
|
126 |
* <br>
|
56797
|
127 |
* {@code col.absoluteIndex() == col.slice(n).absoluteIndex()}.
|
|
128 |
*
|
|
129 |
* @return the absolute 1-based index of this {@link Column}
|
|
130 |
*/
|
|
131 |
public int absoluteIndex();
|
|
132 |
|
|
133 |
/**
|
|
134 |
* Return the SQL type of the value of this {@link Column}.
|
|
135 |
*
|
|
136 |
* @return the SQL type of this value
|
56997
|
137 |
* @throws IllegalStateException if the column sequence is empty.
|
56797
|
138 |
*/
|
|
139 |
public SqlType sqlType();
|
|
140 |
|
|
141 |
/**
|
|
142 |
* Return the Java type that best represents the value of this
|
|
143 |
* {@link Column}.
|
|
144 |
*
|
|
145 |
* @param <T>
|
|
146 |
* @return a {@link Class} that best represents the value of this
|
|
147 |
* {@link Column}
|
56997
|
148 |
* @throws IllegalStateException if the column sequence is empty.
|
56797
|
149 |
*/
|
|
150 |
public <T>Class<T> javaType();
|
|
151 |
|
|
152 |
/**
|
|
153 |
* The length of the current value if defined.
|
|
154 |
*
|
|
155 |
* @return
|
|
156 |
* @throws UnsupportedOperationException if the length of the current value
|
|
157 |
* is undefined
|
56997
|
158 |
* @throws IllegalStateException if the column sequence is empty.
|
56797
|
159 |
*/
|
|
160 |
public long length();
|
|
161 |
|
|
162 |
/**
|
|
163 |
* Return the number of remaining values accessible by this {@link Column}
|
|
164 |
* excluding the current value. This is the number of times {@code next()}
|
|
165 |
* can be called before {@code hasNext()} returns false.
|
|
166 |
*
|
|
167 |
* @return the number of values remaining
|
|
168 |
*/
|
|
169 |
public int numberOfValuesRemaining();
|
56380
|
170 |
|
|
171 |
/**
|
56797
|
172 |
* Modify this {@link Column} to point to a value identified by id.
|
|
173 |
*
|
|
174 |
* @apiNote The value specified for {@code id} represents the name of a
|
|
175 |
* column or parameter marker for the underlying data source and is
|
|
176 |
* implementation specific. This may be a simple SQL identifier, a quoted
|
|
177 |
* identifier, or any other type of identifier supported by the data source.
|
|
178 |
* <p>
|
|
179 |
* Consult your implementation's documentation for additional
|
|
180 |
* information.
|
|
181 |
*
|
|
182 |
* @param id an identifier. Not null
|
|
183 |
* @return this {@link Column}
|
|
184 |
* @throws NoSuchElementException if id does not identify exactly one value
|
56997
|
185 |
* @throws IllegalStateException if the column sequence is empty.
|
56797
|
186 |
*/
|
|
187 |
public Column at(String id);
|
|
188 |
|
|
189 |
/**
|
|
190 |
* Modify this {@link Column} to point to the value at {@code index}. The
|
56997
|
191 |
* first value is at index 1. Negative numbers count back from the last
|
56797
|
192 |
* value. The last value is at index -1.
|
56380
|
193 |
*
|
56797
|
194 |
* @param index a new index
|
|
195 |
* @return this {@link Column}
|
56997
|
196 |
* @throws NoSuchElementException if {@code index > length},
|
|
197 |
* {@code index < -length}, or {@code index == 0}
|
|
198 |
* @throws IllegalStateException if the column sequence is empty.
|
56380
|
199 |
*/
|
56797
|
200 |
public Column at(int index);
|
|
201 |
|
|
202 |
/**
|
|
203 |
* Modify this {@link Column} to point to the value at the current index +
|
|
204 |
* {@code offset}. If {@code offset} is 0 this is a noop. If {@code offset}
|
|
205 |
* is negative the new index is less than the current index. If the new
|
56997
|
206 |
* index would be less than 1 or greater than the sequence length this
|
|
207 |
* {@link Column} is not modified and {@link IllegalArgumentException} is
|
|
208 |
* thrown.
|
56797
|
209 |
*
|
|
210 |
* @param offset an increment to the current index
|
|
211 |
* @return this {@link Column}
|
|
212 |
* @throws NoSuchElementException if the new index would be less than 1 or
|
56997
|
213 |
* greater than the sequence length.
|
|
214 |
* @throws IllegalStateException if the column sequence is empty.
|
56797
|
215 |
*/
|
|
216 |
public default Column next(int offset) {
|
56997
|
217 |
if (index() == 0) {
|
|
218 |
throw new IllegalStateException();
|
|
219 |
}
|
56797
|
220 |
int newIndex = index() + offset;
|
|
221 |
if (offset > numberOfValuesRemaining() || newIndex < 1) {
|
|
222 |
throw new NoSuchElementException();
|
|
223 |
}
|
|
224 |
return at(newIndex);
|
|
225 |
}
|
56380
|
226 |
|
|
227 |
/**
|
56797
|
228 |
* Return a new {@link Column} that is a handle to a subsequence of the
|
|
229 |
* sequence of values referenced by this {@link Column}. The subsequence
|
|
230 |
* consists of {@code numValues} number of values. If {@code numValues} is
|
|
231 |
* positive the values are the value of this column and its successors. If
|
|
232 |
* {@code numValues} is negative the values are the predecessors of this
|
56997
|
233 |
* column not including this {@link Column}.
|
|
234 |
* <br>
|
|
235 |
* The returned {@link Column} is positioned on the first value of the
|
|
236 |
* slice.
|
|
237 |
* <br>
|
|
238 |
* The order of the values of the new {@link Column} is the same as the
|
|
239 |
* order of the values of this {@link Column}. This {@link Column}
|
|
240 |
* is not modified.
|
56797
|
241 |
*
|
|
242 |
* @param numValues the number of columns to include in the slice
|
|
243 |
* @return a new {@link Column}.
|
|
244 |
* @throws NoSuchElementException if the current index plus
|
56997
|
245 |
* {@code numValues} is greater than the sequence length or less than 1
|
|
246 |
* @throws IllegalStateException if the column sequence is empty.
|
56797
|
247 |
*/
|
|
248 |
public Column slice(int numValues);
|
|
249 |
|
|
250 |
/**
|
|
251 |
* Return a new {@link Column} that is a duplicate of this {@link Column}.
|
|
252 |
* This {@link Column} is not modified.
|
|
253 |
*
|
|
254 |
* @return a new {@link Column}
|
|
255 |
*/
|
|
256 |
public Column clone();
|
|
257 |
|
|
258 |
/**
|
|
259 |
* Modify this {@link Column} to point to the next value in the sequence.
|
56380
|
260 |
*
|
56797
|
261 |
* @return this {@link Column}
|
|
262 |
* @throws NoSuchElementException if the new index would be greater than
|
56997
|
263 |
* the sequence length.
|
|
264 |
* @throws IllegalStateException if the column sequence is empty.
|
56797
|
265 |
*/
|
|
266 |
public default Column next() {
|
|
267 |
return next(1);
|
|
268 |
}
|
|
269 |
|
56997
|
270 |
/**
|
|
271 |
* {@inheritDoc}
|
|
272 |
*/
|
56797
|
273 |
@Override
|
|
274 |
public default boolean hasNext() {
|
|
275 |
return numberOfValuesRemaining() > 0;
|
|
276 |
}
|
|
277 |
|
56997
|
278 |
/**
|
|
279 |
* {@inheritDoc}
|
|
280 |
*/
|
56797
|
281 |
@Override
|
|
282 |
public default void forEach(Consumer<? super Column> action) {
|
56997
|
283 |
if (index() == 0) {
|
|
284 |
return;
|
|
285 |
}
|
|
286 |
action.accept(this.clone());
|
|
287 |
while (hasNext()) {
|
|
288 |
action.accept(next().clone());
|
|
289 |
}
|
56797
|
290 |
}
|
|
291 |
|
56997
|
292 |
/**
|
|
293 |
* {@inheritDoc}
|
|
294 |
*/
|
56797
|
295 |
@Override
|
56997
|
296 |
public default Iterator<Column> iterator() {
|
|
297 |
return new Iterator<Column>() {
|
|
298 |
Column next = Column.this.index() == 0 ? null : Column.this.clone();
|
|
299 |
|
|
300 |
@Override
|
|
301 |
public boolean hasNext() {
|
|
302 |
return next != null;
|
|
303 |
}
|
|
304 |
|
|
305 |
@Override
|
|
306 |
public Column next() {
|
|
307 |
if (!hasNext()) {
|
|
308 |
throw new NoSuchElementException();
|
|
309 |
}
|
|
310 |
|
|
311 |
Column prev = next.clone();
|
|
312 |
|
|
313 |
if (next.hasNext()) {
|
|
314 |
next.next();
|
|
315 |
}
|
|
316 |
else {
|
|
317 |
next = null;
|
|
318 |
}
|
|
319 |
|
|
320 |
return prev;
|
|
321 |
}
|
|
322 |
};
|
56797
|
323 |
}
|
|
324 |
|
|
325 |
/**
|
|
326 |
* TODO This almost certainly works correctly but it doesn't integrate well
|
|
327 |
* with the other access patterns. A better approach would be a Spliterator
|
|
328 |
* that overrides trySplit and creates new slices for each batch.
|
56380
|
329 |
*
|
56797
|
330 |
* There is a fundamental problem with mixing Spliterator with the other
|
|
331 |
* access patterns. The other patterns assume navigation from one column to
|
|
332 |
* an arbitrary other column. Spliterator.trySplit can divide the column
|
|
333 |
* sequence in arbitrary places invalidating the assumption about column
|
|
334 |
* navigation.
|
|
335 |
*
|
|
336 |
* @return a {@link java.util.Spliterator}
|
56380
|
337 |
*/
|
56997
|
338 |
/**
|
|
339 |
* {@inheritDoc}
|
|
340 |
*/
|
56797
|
341 |
@Override
|
|
342 |
public default Spliterator<Column> spliterator() {
|
56997
|
343 |
List<Column> list = new ArrayList<>(numberOfValuesRemaining());
|
56797
|
344 |
this.clone().forEach(c -> list.add(c.slice(1)));
|
|
345 |
return java.util.Spliterators.spliterator(list.toArray(), numberOfValuesRemaining());
|
|
346 |
}
|
56380
|
347 |
}
|
|
348 |
|
|
349 |
/**
|
56797
|
350 |
* Used by {@link OutOperation} to expose the out parameters of a stored
|
|
351 |
* procedure call.
|
|
352 |
*
|
|
353 |
* This exists to allow for future additions.
|
56380
|
354 |
*/
|
56797
|
355 |
public interface OutColumn extends Column {
|
|
356 |
|
56380
|
357 |
}
|
|
358 |
|
|
359 |
/**
|
|
360 |
* Used by {@link RowOperation} to expose each row of a row sequence.
|
|
361 |
*/
|
56797
|
362 |
public static interface RowColumn extends Column {
|
56380
|
363 |
|
|
364 |
/**
|
56797
|
365 |
* The count of rows in the row sequence preceeding this {@link RowColumn}.
|
|
366 |
* For the first row in the row sequence the {@code rowNumber} is 0.
|
56380
|
367 |
*
|
56797
|
368 |
* @return the count of rows in the row sequence preceeding this
|
|
369 |
* {@link RowColumn}
|
|
370 |
* @throws IllegalStateException if the call that was passed this
|
56997
|
371 |
* {@code Result} has ended
|
56380
|
372 |
*/
|
|
373 |
public long rowNumber();
|
|
374 |
|
|
375 |
/**
|
56797
|
376 |
* Terminate processing of the rows in this {@link RowOperation}. No further
|
|
377 |
* rows in the row sequence will be processed. All subsequent rows, if any,
|
|
378 |
* will be ignored. Any rows already fetched will not be processed. Any rows
|
|
379 |
* not yet fetched may or may not be fetched. If fetched they will not be
|
|
380 |
* processed. The RowOperation will be completed normally, as though the
|
|
381 |
* current row were the last row of the row sequence.
|
56380
|
382 |
*
|
56797
|
383 |
* @throws IllegalStateException if the call that was passed this
|
|
384 |
* {@link RowColumn} has ended
|
56380
|
385 |
*/
|
|
386 |
public void cancel();
|
|
387 |
|
|
388 |
}
|
|
389 |
|
|
390 |
}
|