|
1 /* |
|
2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
|
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 */ |
|
25 package jdk.incubator.sql2; |
|
26 |
|
27 import java.time.Duration; |
|
28 import java.util.concurrent.CompletionStage; |
|
29 import java.util.function.BiConsumer; |
|
30 import java.util.function.Consumer; |
|
31 import java.util.function.Function; |
|
32 |
|
33 /** |
|
34 * |
|
35 * A multi-operation is an {@link Operation} that returns one or more results in |
|
36 * addition to the result defined by the {@link Operation}. A {@link DynamicMultiOperation} is a |
|
37 * multi-operation where the number and types of the results are determined at |
|
38 * execution. |
|
39 * |
|
40 * NOTE: In general one way to do things is sufficient however the API provides |
|
41 * two ways to handle multiple results. This way, the dynamic way, is required |
|
42 * because the number and type of results cannot always be known in advance. The |
|
43 * static way is also provided because it is much easier to use when the number |
|
44 * and type of results is known. The improvement in ease of use outweighs the |
|
45 * duplication IMO. If necessary one or the other can be eliminated. Eliminating |
|
46 * dynamic reduces functionality. Eliminating static reduces ease of use in what |
|
47 * I believe to be a common case. |
|
48 * |
|
49 * @param <T> type of the result of this DynamicMultiOperation |
|
50 */ |
|
51 public interface DynamicMultiOperation<T> extends OutOperation<T> { |
|
52 |
|
53 /** |
|
54 * Provides a handler for count results. The provided handler is called for |
|
55 * each count result. When called the first argument is the number of results |
|
56 * that preceeded the current result. The second argument is a CountOperation |
|
57 * that will process the current result. This CountOperation has not been |
|
58 * configured in any way nor has it been submitted. The handler configures the |
|
59 * CountOperation and submits it. The count result is processed when the |
|
60 * CountOperation is submitted. |
|
61 * |
|
62 * If this method is not called any count result is ignored. |
|
63 * |
|
64 * @param handler not null |
|
65 * @return this DynamicMultiOperation |
|
66 * @throws IllegalStateException if the CountOperation has not been submitted |
|
67 * when the call to the handler returns |
|
68 */ |
|
69 public DynamicMultiOperation<T> onCount(BiConsumer<Integer, CountOperation<T>> handler); |
|
70 |
|
71 /** |
|
72 * Provides a handler for row sequence results. The provided handler is called for |
|
73 * each row sequence result. When called the first argument is the number of results |
|
74 * that preceeded the current result. The second argument is a RowOperation |
|
75 * that will process the current result. This RowOperation has not been |
|
76 * configured in any way nor has it been submitted. The handler configures the |
|
77 * RowOperation and submits it. The row sequence result is processed when the |
|
78 * RowOperation is submitted. |
|
79 * |
|
80 * If this method is not called any row sequence result is ignored. |
|
81 * |
|
82 * @param handler |
|
83 * @return This DynamicMultiOperation |
|
84 * @throws IllegalStateException if the RowOperation has not been submitted |
|
85 * when the call to the handler returns |
|
86 */ |
|
87 public DynamicMultiOperation<T> onRows(BiConsumer<Integer, RowOperation<T>> handler); |
|
88 |
|
89 /** |
|
90 * Provides an error handler for this {@link Operation}. The provided handler |
|
91 * is called for each error that occurs. When called the first argument is the |
|
92 * number of results, including errors, that preceeded the current error. The |
|
93 * second argument is a {@link Throwable} corresponding to the error. When the |
|
94 * handler returns processing of the DynamicMultiOperation results continues. |
|
95 * Only one onError method may be called. |
|
96 * |
|
97 * @param handler a BiConsumer that handles an error |
|
98 * @return this DynamicMultiOperation |
|
99 * @throws IllegalStateException if any onError method was called previously |
|
100 */ |
|
101 public DynamicMultiOperation<T> onError(BiConsumer<Integer, Throwable> handler); |
|
102 |
|
103 // Covariant overrides |
|
104 |
|
105 /** |
|
106 * Provides an error handler for this {@link Operation}. If execution of this |
|
107 * {@link Operation} results in an error, before the Operation is completed, |
|
108 * the handler is called with the {@link Throwable} as the argument. When the |
|
109 * handler returns the {@link Operation} is completed exceptionally with the |
|
110 * {@link Throwable}. |
|
111 * |
|
112 * @param handler |
|
113 * @return this DynamicMultiOperation |
|
114 * @throws IllegalStateException if any onError method was called previously |
|
115 */ |
|
116 @Override |
|
117 public DynamicMultiOperation<T> onError(Consumer<Throwable> handler); |
|
118 |
|
119 @Override |
|
120 public DynamicMultiOperation<T> outParameter(String id, SqlType type); |
|
121 |
|
122 @Override |
|
123 public DynamicMultiOperation<T> apply(Function<Result.OutParameterMap, ? extends T> processor); |
|
124 |
|
125 @Override |
|
126 public DynamicMultiOperation<T> set(String id, Object value); |
|
127 |
|
128 @Override |
|
129 public DynamicMultiOperation<T> set(String id, Object value, SqlType type); |
|
130 |
|
131 @Override |
|
132 public DynamicMultiOperation<T> set(String id, CompletionStage<?> source); |
|
133 |
|
134 @Override |
|
135 public DynamicMultiOperation<T> set(String id, CompletionStage<?> source, SqlType type); |
|
136 |
|
137 @Override |
|
138 public DynamicMultiOperation<T> timeout(Duration minTime); |
|
139 |
|
140 } |