22
33import com .github .collinalpert .java2db .exceptions .ConnectionFailedException ;
44import 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 .*;
97import com .mysql .cj .exceptions .CJCommunicationsException ;
108import com .mysql .cj .jdbc .exceptions .CommunicationsException ;
119
1210import 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 .*;
2013import java .util .concurrent .CompletableFuture ;
2114import java .util .function .Consumer ;
2215
@@ -63,7 +56,7 @@ public class DBConnection implements Closeable {
6356 */
6457 public static boolean LOG_QUERIES = true ;
6558
66- private Connection connection ;
59+ private Connection underlyingConnection ;
6760 private boolean isConnectionValid ;
6861
6962 public DBConnection () {
@@ -73,7 +66,7 @@ public DBConnection() {
7366 System .setProperty ("user" , USERNAME );
7467 System .setProperty ("password" , PASSWORD );
7568 DriverManager .setLoginTimeout (TIMEOUT );
76- connection = DriverManager .getConnection (connectionString , System .getProperties ());
69+ underlyingConnection = DriverManager .getConnection (connectionString , System .getProperties ());
7770 isConnectionValid = true ;
7871 } catch (CJCommunicationsException | CommunicationsException e ) {
7972 isConnectionValid = false ;
@@ -84,6 +77,11 @@ public DBConnection() {
8477 }
8578 }
8679
80+ public DBConnection (Connection underlyingConnection ) {
81+ this .underlyingConnection = underlyingConnection ;
82+ this .isConnectionValid = true ;
83+ }
84+
8785 /**
8886 * Checks if the connection is valid/successful.
8987 *
@@ -102,7 +100,7 @@ public boolean isValid() {
102100 * @throws SQLException if the query is malformed or cannot be executed.
103101 */
104102 public ResultSet execute (String query ) throws SQLException {
105- var statement = this .connection .createStatement ();
103+ var statement = this .underlyingConnection .createStatement ();
106104 log (query );
107105 var set = statement .executeQuery (query );
108106 statement .closeOnCompletion ();
@@ -119,7 +117,7 @@ public ResultSet execute(String query) throws SQLException {
119117 * @throws SQLException if the query is malformed or cannot be executed.
120118 */
121119 public ResultSet execute (String query , Object ... params ) throws SQLException {
122- var statement = this .connection .prepareStatement (query );
120+ var statement = this .underlyingConnection .prepareStatement (query );
123121 for (int i = 0 ; i < params .length ; i ++) {
124122 statement .setObject (i + 1 , params [i ]);
125123 }
@@ -138,7 +136,7 @@ public ResultSet execute(String query, Object... params) throws SQLException {
138136 * @throws SQLException if the query is malformed or cannot be executed.
139137 */
140138 public long update (String query ) throws SQLException {
141- var statement = this .connection .createStatement ();
139+ var statement = this .underlyingConnection .createStatement ();
142140 log (query );
143141 statement .executeUpdate (query , Statement .RETURN_GENERATED_KEYS );
144142 return updateInternal (statement );
@@ -153,7 +151,7 @@ public long update(String query) throws SQLException {
153151 * @throws SQLException if the query is malformed or cannot be executed.
154152 */
155153 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 );
157155 for (int i = 0 ; i < params .length ; i ++) {
158156 statement .setObject (i + 1 , params [i ]);
159157 }
@@ -171,7 +169,7 @@ public long update(String query, Object... params) throws SQLException {
171169 */
172170 public boolean isOpen () {
173171 try {
174- return !this .connection .isClosed ();
172+ return !this .underlyingConnection .isClosed ();
175173 } catch (SQLException e ) {
176174 System .err .println ("Could not determine connection status" );
177175 return this .isConnectionValid = false ;
@@ -184,8 +182,8 @@ public boolean isOpen() {
184182 @ Override
185183 public void close () {
186184 try {
187- if (this .connection != null ) {
188- this .connection .close ();
185+ if (this .underlyingConnection != null ) {
186+ this .underlyingConnection .close ();
189187 }
190188 } catch (SQLException e ) {
191189 System .err .println ("Could not close database connection" );
@@ -195,6 +193,16 @@ public void close() {
195193 }
196194 }
197195
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+ */
198206 public <T > Optional <T > callFunction (Class <T > returnType , String functionName , Object ... arguments ) throws SQLException {
199207 var joiner = new StringJoiner ("," );
200208 for (int i = 0 ; i < arguments .length ; i ++) {
@@ -210,6 +218,10 @@ public <T> CompletableFuture<Optional<T>> callFunctionAsync(Consumer<SQLExceptio
210218 return CompletableFuture .supplyAsync (supplierHandling (() -> this .callFunction (returnType , functionName , arguments ), exceptionHandler ));
211219 }
212220
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+
213225 public <T > Queryable <T > callStoredProcedure (Class <T > returnType , String storedProcedureName , Object ... arguments ) {
214226 return new StoredProcedureQuery <>(returnType , this , storedProcedureName , arguments );
215227 }
@@ -218,6 +230,10 @@ public <T> AsyncQueryable<T> callStoredProcedureAsync(Class<T> returnType, Strin
218230 return new AsyncStoredProcedureQuery <>(returnType , this , storedProcedureName , arguments );
219231 }
220232
233+ public Connection underlyingConnection () {
234+ return this .underlyingConnection ;
235+ }
236+
221237 /**
222238 * Prints queries to the console, while considering the {@link DBConnection#LOG_QUERIES} constant.
223239 *
0 commit comments