2
2
3
3
import com .github .collinalpert .java2db .exceptions .ConnectionFailedException ;
4
4
import com .github .collinalpert .java2db .mappers .FieldMapper ;
5
- import com .github .collinalpert .java2db .queries .Queryable ;
6
- import com .github .collinalpert .java2db .queries .StoredProcedureQuery ;
7
- import com .github .collinalpert .java2db .queries .async .AsyncQueryable ;
8
- import com .github .collinalpert .java2db .queries .async .AsyncStoredProcedureQuery ;
5
+ import com .github .collinalpert .java2db .queries .*;
6
+ import com .github .collinalpert .java2db .queries .async .*;
9
7
import com .mysql .cj .exceptions .CJCommunicationsException ;
10
8
import com .mysql .cj .jdbc .exceptions .CommunicationsException ;
11
9
12
10
import java .io .Closeable ;
13
- import java .sql .Connection ;
14
- import java .sql .DriverManager ;
15
- import java .sql .ResultSet ;
16
- import java .sql .SQLException ;
17
- import java .sql .Statement ;
18
- import java .util .Optional ;
19
- import java .util .StringJoiner ;
11
+ import java .sql .*;
12
+ import java .util .*;
20
13
import java .util .concurrent .CompletableFuture ;
21
14
import java .util .function .Consumer ;
22
15
@@ -63,7 +56,7 @@ public class DBConnection implements Closeable {
63
56
*/
64
57
public static boolean LOG_QUERIES = true ;
65
58
66
- private Connection connection ;
59
+ private Connection underlyingConnection ;
67
60
private boolean isConnectionValid ;
68
61
69
62
public DBConnection () {
@@ -73,7 +66,7 @@ public DBConnection() {
73
66
System .setProperty ("user" , USERNAME );
74
67
System .setProperty ("password" , PASSWORD );
75
68
DriverManager .setLoginTimeout (TIMEOUT );
76
- connection = DriverManager .getConnection (connectionString , System .getProperties ());
69
+ underlyingConnection = DriverManager .getConnection (connectionString , System .getProperties ());
77
70
isConnectionValid = true ;
78
71
} catch (CJCommunicationsException | CommunicationsException e ) {
79
72
isConnectionValid = false ;
@@ -84,6 +77,11 @@ public DBConnection() {
84
77
}
85
78
}
86
79
80
+ public DBConnection (Connection underlyingConnection ) {
81
+ this .underlyingConnection = underlyingConnection ;
82
+ this .isConnectionValid = true ;
83
+ }
84
+
87
85
/**
88
86
* Checks if the connection is valid/successful.
89
87
*
@@ -102,7 +100,7 @@ public boolean isValid() {
102
100
* @throws SQLException if the query is malformed or cannot be executed.
103
101
*/
104
102
public ResultSet execute (String query ) throws SQLException {
105
- var statement = this .connection .createStatement ();
103
+ var statement = this .underlyingConnection .createStatement ();
106
104
log (query );
107
105
var set = statement .executeQuery (query );
108
106
statement .closeOnCompletion ();
@@ -119,7 +117,7 @@ public ResultSet execute(String query) throws SQLException {
119
117
* @throws SQLException if the query is malformed or cannot be executed.
120
118
*/
121
119
public ResultSet execute (String query , Object ... params ) throws SQLException {
122
- var statement = this .connection .prepareStatement (query );
120
+ var statement = this .underlyingConnection .prepareStatement (query );
123
121
for (int i = 0 ; i < params .length ; i ++) {
124
122
statement .setObject (i + 1 , params [i ]);
125
123
}
@@ -138,7 +136,7 @@ public ResultSet execute(String query, Object... params) throws SQLException {
138
136
* @throws SQLException if the query is malformed or cannot be executed.
139
137
*/
140
138
public long update (String query ) throws SQLException {
141
- var statement = this .connection .createStatement ();
139
+ var statement = this .underlyingConnection .createStatement ();
142
140
log (query );
143
141
statement .executeUpdate (query , Statement .RETURN_GENERATED_KEYS );
144
142
return updateInternal (statement );
@@ -153,7 +151,7 @@ public long update(String query) throws SQLException {
153
151
* @throws SQLException if the query is malformed or cannot be executed.
154
152
*/
155
153
public long update (String query , Object ... params ) throws SQLException {
156
- var statement = this .connection .prepareStatement (query , Statement .RETURN_GENERATED_KEYS );
154
+ var statement = this .underlyingConnection .prepareStatement (query , Statement .RETURN_GENERATED_KEYS );
157
155
for (int i = 0 ; i < params .length ; i ++) {
158
156
statement .setObject (i + 1 , params [i ]);
159
157
}
@@ -171,7 +169,7 @@ public long update(String query, Object... params) throws SQLException {
171
169
*/
172
170
public boolean isOpen () {
173
171
try {
174
- return !this .connection .isClosed ();
172
+ return !this .underlyingConnection .isClosed ();
175
173
} catch (SQLException e ) {
176
174
System .err .println ("Could not determine connection status" );
177
175
return this .isConnectionValid = false ;
@@ -184,8 +182,8 @@ public boolean isOpen() {
184
182
@ Override
185
183
public void close () {
186
184
try {
187
- if (this .connection != null ) {
188
- this .connection .close ();
185
+ if (this .underlyingConnection != null ) {
186
+ this .underlyingConnection .close ();
189
187
}
190
188
} catch (SQLException e ) {
191
189
System .err .println ("Could not close database connection" );
@@ -195,6 +193,16 @@ public void close() {
195
193
}
196
194
}
197
195
196
+ /**
197
+ * Calls an SQL function.
198
+ *
199
+ * @param returnType The Java equivalent of the SQL datatype the function returns.
200
+ * @param functionName The name of the function.
201
+ * @param arguments The arguments to be supplied to the function.
202
+ * @param <T> The functions return type.
203
+ * @return The value of the function, as a Java datatype.
204
+ * @throws SQLException In case there is an error communicating with the database, i.e. the function does not exist.
205
+ */
198
206
public <T > Optional <T > callFunction (Class <T > returnType , String functionName , Object ... arguments ) throws SQLException {
199
207
var joiner = new StringJoiner ("," );
200
208
for (int i = 0 ; i < arguments .length ; i ++) {
@@ -210,6 +218,10 @@ public <T> CompletableFuture<Optional<T>> callFunctionAsync(Consumer<SQLExceptio
210
218
return CompletableFuture .supplyAsync (supplierHandling (() -> this .callFunction (returnType , functionName , arguments ), exceptionHandler ));
211
219
}
212
220
221
+ public <T > CompletableFuture <Void > callFunctionAsync (Consumer <SQLException > exceptionHandler , Consumer <? super Optional <T >> callback , Class <T > returnType , String functionName , Object ... arguments ) {
222
+ return CompletableFuture .supplyAsync (supplierHandling (() -> this .callFunction (returnType , functionName , arguments ), exceptionHandler )).thenAcceptAsync (callback );
223
+ }
224
+
213
225
public <T > Queryable <T > callStoredProcedure (Class <T > returnType , String storedProcedureName , Object ... arguments ) {
214
226
return new StoredProcedureQuery <>(returnType , this , storedProcedureName , arguments );
215
227
}
@@ -218,6 +230,10 @@ public <T> AsyncQueryable<T> callStoredProcedureAsync(Class<T> returnType, Strin
218
230
return new AsyncStoredProcedureQuery <>(returnType , this , storedProcedureName , arguments );
219
231
}
220
232
233
+ public Connection underlyingConnection () {
234
+ return this .underlyingConnection ;
235
+ }
236
+
221
237
/**
222
238
* Prints queries to the console, while considering the {@link DBConnection#LOG_QUERIES} constant.
223
239
*
0 commit comments