56380
|
1 |
/*
|
56475
|
2 |
* Copyright (c) 2017, 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 |
*/
|
|
25 |
|
|
26 |
package jdk.incubator.sql2;
|
|
27 |
|
|
28 |
import java.util.LinkedList;
|
|
29 |
import java.util.List;
|
|
30 |
import java.util.function.Consumer;
|
|
31 |
|
|
32 |
/**
|
56797
|
33 |
* Uses the builder pattern to get a {@link Connection}. A {@link DataSource#getConnection}
|
56380
|
34 |
* method is provided as a convenience.
|
56475
|
35 |
*
|
|
36 |
* Implementations must be thread safe.
|
56380
|
37 |
*/
|
|
38 |
public interface DataSource
|
|
39 |
extends AutoCloseable {
|
|
40 |
|
|
41 |
/**
|
|
42 |
* Instances of this type are used to build {@link DataSource}s. This type is
|
|
43 |
* immutable once configured. No property can be set more than once. No
|
|
44 |
* property can be set after {@link build} is called.
|
56797
|
45 |
*
|
|
46 |
* ISSUE: Probably need property(DataSourceProperty prop, Object value).
|
56380
|
47 |
*/
|
|
48 |
public interface Builder {
|
|
49 |
|
|
50 |
/**
|
|
51 |
* A convenience method for setting the {@link AdbaConnectionProperty#URL}.
|
|
52 |
*
|
|
53 |
* @param url the value to be set for {@link AdbaConnectionProperty#URL}
|
|
54 |
* @return this {@link Builder}
|
|
55 |
* @see connectionProperty
|
|
56 |
*/
|
|
57 |
public default Builder url(String url) {
|
|
58 |
return connectionProperty(AdbaConnectionProperty.URL, url);
|
|
59 |
}
|
|
60 |
|
|
61 |
/**
|
|
62 |
* A convenience method for setting the {@link AdbaConnectionProperty#USER}.
|
|
63 |
*
|
|
64 |
* @param name the value to be set for {@link AdbaConnectionProperty#USER}
|
|
65 |
* @return this {@link Builder}
|
|
66 |
* @see connectionProperty
|
|
67 |
*/
|
|
68 |
public default Builder username(String name) {
|
|
69 |
return connectionProperty(AdbaConnectionProperty.USER, name);
|
|
70 |
}
|
|
71 |
|
|
72 |
/**
|
|
73 |
* A convenience method for setting the {@link AdbaConnectionProperty#PASSWORD}.
|
|
74 |
*
|
|
75 |
* @param password the value to be set for {@link AdbaConnectionProperty#PASSWORD}
|
|
76 |
* @return this {@link Builder}
|
|
77 |
* @see connectionProperty
|
|
78 |
*/
|
|
79 |
public default Builder password(String password) {
|
|
80 |
return connectionProperty(AdbaConnectionProperty.PASSWORD, password);
|
|
81 |
}
|
|
82 |
|
|
83 |
/**
|
|
84 |
* Specify the value of a {@link Connection} property that will be set by default on
|
|
85 |
* all {@link Connection}s produced by this {@link DataSource}. A different value can be set
|
|
86 |
* for a particular {@link Connection} via {@link Connection.Builder#property}.
|
|
87 |
*
|
|
88 |
* @param property the {@link ConnectionProperty} to be set. May not be {@code null}.
|
|
89 |
* @param value the value to be set for {@code property}
|
|
90 |
* @return this {@link Builder}
|
|
91 |
* @throws IllegalArgumentException if {@code property.validate(value)} does not
|
|
92 |
* return {@code true}. If it throws an {@link Exception} that {@link Exception} is the cause. Or if
|
|
93 |
* this property has been specified previously to this method or
|
|
94 |
* {@link connectionProperty} or {@link registerConnectionProperty}.
|
|
95 |
* @throws IllegalStateException if {@link build} has previously been called.
|
|
96 |
*/
|
|
97 |
public Builder defaultConnectionProperty(ConnectionProperty property, Object value);
|
|
98 |
|
|
99 |
/**
|
|
100 |
* Specify the value of a {@link Connection} property that will be set on
|
|
101 |
* all {@link Connection}s produced by the built {@link DataSource}.
|
|
102 |
* Attempting to set a different value via
|
|
103 |
* {@link Connection.Builder#property} will throw
|
|
104 |
* {@link IllegalArgumentException}.
|
|
105 |
*
|
|
106 |
* @param property the {@link ConnectionProperty} to set. May not be
|
|
107 |
* {@code null}.
|
|
108 |
* @param value the value to set as the default for {@code property}
|
|
109 |
* @return this {@link Builder}
|
|
110 |
* @throws IllegalArgumentException if {@code property.validate(value)} does
|
|
111 |
* not return {@code true}. If it throws an {@link Exception} that
|
|
112 |
* {@link Exception} is the cause. Or if this property has been specified
|
|
113 |
* previously to this method or {@link defaultConnectionProperty} or
|
|
114 |
* {@link registerConnectionProperty}.
|
|
115 |
* @throws IllegalStateException if {@link build} has previously been
|
|
116 |
* called.
|
|
117 |
*/
|
|
118 |
public Builder connectionProperty(ConnectionProperty property, Object value);
|
|
119 |
|
|
120 |
/**
|
|
121 |
* Make a user defined property known to the implementation. One reason to
|
|
122 |
* do this is so the default value of the property will be used. If the
|
|
123 |
* {@link DataSource} doesn't know about the property then it cannot know to
|
|
124 |
* set the default value. Convenience method.
|
|
125 |
*
|
|
126 |
* @param property the {@link ConnectionProperty} to make known. May not be
|
|
127 |
* {@code null}.
|
|
128 |
* @return this Builder
|
|
129 |
* @throws IllegalArgumentException if this property has been specified
|
|
130 |
* previously to this method or {@link connectionProperty} or
|
|
131 |
* {@link defaultConnectionProperty}.
|
|
132 |
* @throws IllegalStateException if {@link build} has previously been
|
|
133 |
* called.
|
|
134 |
*/
|
|
135 |
public default Builder registerConnectionProperty(ConnectionProperty property) {
|
|
136 |
return defaultConnectionProperty(property, property.defaultValue());
|
|
137 |
}
|
|
138 |
|
|
139 |
/**
|
56797
|
140 |
* Provide a method that the built {@link DataSource} will call to control the
|
|
141 |
* rate of {@link DataSource#connectOperation} submissions. The built
|
|
142 |
* {@link DataSource} will call {@code request} with a positive argument
|
|
143 |
* when the {@link DataSource} is able to accept more
|
|
144 |
* {@link DataSource#connectOperation} submissions. The difference between
|
|
145 |
* the sum of all arguments passed to {@code request} and the number of
|
|
146 |
* calls to {@link DataSource#builder} is the
|
|
147 |
* <i>demand</i>. The demand must always be non-negative. If a call is made to
|
|
148 |
* {@link DataSource#builder} that would make the demand negative that call
|
|
149 |
* throws {@link IllegalStateException}. If {@code requestHook} is not called,
|
|
150 |
* the demand is defined to be infinite.
|
|
151 |
*
|
|
152 |
* <p>
|
|
153 |
* An implementation may choose to delay detection of insufficient demand.
|
|
154 |
* Instead of checking when {@link DataSource#builder} is called an
|
|
155 |
* implementation may choose to check at some later point in Connection
|
|
156 |
* creation such as {@link Connection.Builder.build} or
|
|
157 |
* {@code Connection#connectOperation().submit()} or even later. In any case
|
|
158 |
* an implementation must throw IllegalStateException before allocating
|
|
159 |
* or waiting to allocate scarce resources if the demand is negative.</p>
|
|
160 |
*
|
|
161 |
* @param request accepts calls to increase the demand. Not null.
|
|
162 |
* @return this {@link Builder}
|
|
163 |
* @throws IllegalStateException if this method has been called previously
|
|
164 |
*/
|
|
165 |
public Builder requestHook(Consumer<Long> request);
|
|
166 |
|
|
167 |
/**
|
56380
|
168 |
* Return a DataSource configured as specified.
|
|
169 |
*
|
|
170 |
* @return a configured {@link DataSource}. Not {@code null}.
|
|
171 |
* @throws IllegalArgumentException if unable to return a {@link DataSource} due to
|
|
172 |
* problems with the configuration such is missing or conflicting properties.
|
|
173 |
*/
|
|
174 |
public DataSource build();
|
|
175 |
}
|
|
176 |
|
|
177 |
/**
|
|
178 |
* Returns a {@link Connection} builder. By default that builder will return
|
|
179 |
* {@link Connection}s with the {@code ConnectionProperty}s specified when creating this
|
|
180 |
* DataSource. Default and unspecified {@link ConnectionProperty}s can be set with
|
|
181 |
* the returned builder.
|
|
182 |
*
|
|
183 |
* @return a new {@link Connection} builder. Not {@code null}.
|
56797
|
184 |
* @throws IllegalStateException if this {@link DataSource} is closed
|
56380
|
185 |
*/
|
|
186 |
public Connection.Builder builder();
|
|
187 |
|
|
188 |
/**
|
|
189 |
* Returns a {@link Connection} that has a submitted connect {@link Operation}. Convenience
|
|
190 |
* method for use with try with resources.
|
|
191 |
*
|
|
192 |
* @return a {@link Connection}
|
56797
|
193 |
* @throws IllegalStateException if this {@link DataSource} is closed
|
56380
|
194 |
*/
|
|
195 |
public default Connection getConnection() {
|
|
196 |
return builder().build().connect();
|
|
197 |
}
|
|
198 |
|
|
199 |
/**
|
|
200 |
* Returns a {@link Connection} that has a submitted connect {@link Operation} with an error
|
|
201 |
* handler. Convenience method for use with try with resources. The error
|
|
202 |
* handle handles errors in the connect {@link Operation}.
|
|
203 |
*
|
|
204 |
* @param handler for errors in the connect {@link Operation}
|
|
205 |
* @return a {@link Connection}
|
56797
|
206 |
* @throws IllegalStateException if this {@link DataSource} is closed
|
56380
|
207 |
*/
|
|
208 |
public default Connection getConnection(Consumer<Throwable> handler) {
|
|
209 |
return builder().build().connect(handler);
|
|
210 |
}
|
|
211 |
|
|
212 |
/**
|
|
213 |
* Translates a SQL string from the format specified by the format argument
|
|
214 |
* to a format that can be used to create {@link Operation}s for the {@link Connection}s
|
|
215 |
* provided by this {@link DataSource}.
|
|
216 |
*
|
|
217 |
* ISSUE: Just an idea
|
|
218 |
*
|
|
219 |
* @param format not {@code null}
|
|
220 |
* @param source SQL in the format specified by {@code format}. Not {@code null}.
|
|
221 |
* @return SQL in the format supported by this {@link DataSource}. Not {@code null}.
|
|
222 |
* @throws IllegalArgumentException if the {@code format} is not supported or
|
|
223 |
* if the {@link DataSource} cannot translate the SQL
|
56797
|
224 |
* @throws IllegalStateException if this {@link DataSource} is closed
|
56380
|
225 |
*/
|
|
226 |
public default String translateSql(String format, String source) throws SqlException {
|
|
227 |
throw new IllegalArgumentException("Unsupported format: \"" + format + "\"");
|
|
228 |
}
|
|
229 |
|
|
230 |
/**
|
|
231 |
* Return a list of the source formats accepted by the {@link translateSql} method.
|
|
232 |
*
|
|
233 |
* ISSUE: Just an idea
|
|
234 |
*
|
|
235 |
* @return an array of Strings each of which identifies a supported format
|
56797
|
236 |
* @throws IllegalStateException if this {@link DataSource} is closed
|
56380
|
237 |
*/
|
|
238 |
public default List<String> supportedTranslateSqlFormats() {
|
|
239 |
return new LinkedList<>();
|
|
240 |
}
|
|
241 |
|
|
242 |
@Override
|
|
243 |
public void close();
|
|
244 |
|
|
245 |
}
|