1616import static com .github .collinalpert .java2db .utilities .Utilities .supplierHandling ;
1717
1818/**
19+ * Wrapper around {@link Connection} which eases use of connecting to a database and running queries.
20+ * Also supports running functions and stored procedures.
21+ *
1922 * @author Collin Alpert
2023 */
2124public class DBConnection implements Closeable {
2225
23- /**
24- * Specifies the hostname/ip address of the database.
25- */
26- public static String HOST ;
27-
28- /**
29- * Specifies the name of the database to connect to.
30- */
31- public static String DATABASE ;
32-
33- /**
34- * Specifies the username to log in on the database with.
35- */
36- public static String USERNAME ;
37-
38- /**
39- * Specifies the password to log in on the database with.
40- */
41- public static String PASSWORD ;
42-
43- /**
44- * Specifies the port to connect to the database on.
45- * This property is optional. If not specified, it will be set to 3306, the default port of MySQL.
46- */
47- public static int PORT = 3306 ;
48-
49- /**
50- * Specifies the login timeout to the database in seconds.
51- */
52- public static int TIMEOUT = 5 ;
53-
5426 /**
5527 * Constant which determines if the queries generated by Java2DB will be logged in the console.
5628 */
@@ -59,13 +31,13 @@ public class DBConnection implements Closeable {
5931 private Connection underlyingConnection ;
6032 private boolean isConnectionValid ;
6133
62- public DBConnection () {
34+ public DBConnection (ConnectionConfiguration configuration ) {
6335 try {
64- var connectionString = String .format ("jdbc:mysql://%s:%d/%s?rewriteBatchedStatements=true" , HOST , PORT , DATABASE );
36+ var connectionString = String .format ("jdbc:mysql://%s:%d/%s?rewriteBatchedStatements=true" , configuration . getHost (), configuration . getPort (), configuration . getDatabase () );
6537 Class .forName ("com.mysql.cj.jdbc.Driver" );
66- System .setProperty ("user" , USERNAME );
67- System .setProperty ("password" , PASSWORD );
68- DriverManager .setLoginTimeout (TIMEOUT );
38+ System .setProperty ("user" , configuration . getUsername () );
39+ System .setProperty ("password" , configuration . getPassword () );
40+ DriverManager .setLoginTimeout (configuration . getTimeout () );
6941 underlyingConnection = DriverManager .getConnection (connectionString , System .getProperties ());
7042 isConnectionValid = true ;
7143 } catch (CJCommunicationsException | CommunicationsException e ) {
@@ -135,7 +107,7 @@ public ResultSet execute(String query, Object... params) throws SQLException {
135107 * @return the last generated ID. This return value should only be used with INSERT statements.
136108 * @throws SQLException if the query is malformed or cannot be executed.
137109 */
138- public long update (String query ) throws SQLException {
110+ public int update (String query ) throws SQLException {
139111 var statement = this .underlyingConnection .createStatement ();
140112 log (query );
141113 statement .executeUpdate (query , Statement .RETURN_GENERATED_KEYS );
@@ -150,7 +122,7 @@ public long update(String query) throws SQLException {
150122 * @return the last generated ID. This return value should only be used with INSERT statements.
151123 * @throws SQLException if the query is malformed or cannot be executed.
152124 */
153- public long update (String query , Object ... params ) throws SQLException {
125+ public int update (String query , Object ... params ) throws SQLException {
154126 var statement = this .underlyingConnection .prepareStatement (query , Statement .RETURN_GENERATED_KEYS );
155127 for (int i = 0 ; i < params .length ; i ++) {
156128 statement .setObject (i + 1 , params [i ]);
@@ -198,9 +170,9 @@ public void close() {
198170 *
199171 * @param returnType The Java equivalent of the SQL datatype the function returns.
200172 * @param functionName The name of the function.
201- * @param arguments The arguments to be supplied to the function.
173+ * @param arguments The arguments to be supplied to the function, if any .
202174 * @param <T> The functions return type.
203- * @return The value of the function, as a Java datatype.
175+ * @return The return value of the function, as a Java datatype.
204176 * @throws SQLException In case there is an error communicating with the database, i.e. the function does not exist.
205177 */
206178 public <T > Optional <T > callFunction (Class <T > returnType , String functionName , Object ... arguments ) throws SQLException {
@@ -214,22 +186,91 @@ public <T> Optional<T> callFunction(Class<T> returnType, String functionName, Ob
214186 }
215187 }
216188
189+ /**
190+ * Calls an SQL function asynchronously.
191+ *
192+ * @param exceptionHandler The exception handler which handles the {@link SQLException} in case something goes wrong.
193+ * @param returnType The Java type which the result will be mapped into. It needs to have a parameterless constructor available.
194+ * @param functionName The name of the function to call.
195+ * @param arguments The arguments to supply to the function, if any.
196+ * @param <T> The functions return type.
197+ * @return The return value of the function, as a Java datatype.
198+ */
217199 public <T > CompletableFuture <Optional <T >> callFunctionAsync (Consumer <SQLException > exceptionHandler , Class <T > returnType , String functionName , Object ... arguments ) {
218200 return CompletableFuture .supplyAsync (supplierHandling (() -> this .callFunction (returnType , functionName , arguments ), exceptionHandler ));
219201 }
220202
221- public <T > CompletableFuture <Void > callFunctionAsync (Consumer <SQLException > exceptionHandler , Consumer <? super Optional <T >> callback , Class <T > returnType , String functionName , Object ... arguments ) {
203+ /**
204+ * Calls an SQL function asynchronously. If an exception occurs, it gets printed to the console.
205+ *
206+ * @param returnType The Java type which the result will be mapped into. It needs to have a parameterless constructor available.
207+ * @param functionName The name of the function to call.
208+ * @param arguments The arguments to supply to the function, if any.
209+ * @param <T> The functions return type.
210+ * @return The return value of the function, as a Java datatype.
211+ */
212+ public <T > CompletableFuture <Optional <T >> callFunctionAsync (Class <T > returnType , String functionName , Object ... arguments ) {
213+ return CompletableFuture .supplyAsync (supplierHandling (() -> this .callFunction (returnType , functionName , arguments )));
214+ }
215+
216+ /**
217+ * Calls an SQL function asynchronously and applies a {@link Consumer} to the returned value.
218+ *
219+ * @param returnType The Java type which the result will be mapped into. It needs to have a parameterless constructor available.
220+ * @param callback A consumer which specifies which action to perform once the function has been called.
221+ * @param functionName The name of the function to call.
222+ * @param arguments The arguments to supply to the function, if any.
223+ * @param <T> The functions return type.
224+ * @return The return value of the function, as a Java datatype.
225+ */
226+ public <T > CompletableFuture <Void > callFunctionAsync (Class <T > returnType , Consumer <? super Optional <T >> callback , String functionName , Object ... arguments ) {
227+ return CompletableFuture .supplyAsync (supplierHandling (() -> this .callFunction (returnType , functionName , arguments ))).thenAcceptAsync (callback );
228+ }
229+
230+ /**
231+ * Calls an SQL function asynchronously and applies a {@link Consumer} to the returned value.
232+ *
233+ * @param exceptionHandler The exception handler which handles the {@link SQLException} in case something goes wrong.
234+ * @param returnType The Java type which the result will be mapped into. If it is a complex type, it needs to have a parameterless constructor available.
235+ * @param callback A consumer which specifies which action to perform once the function has been called.
236+ * @param functionName The name of the function to call.
237+ * @param arguments The arguments to supply to the function, if any.
238+ * @param <T> The functions return type.
239+ * @return The return value of the function, as a Java datatype.
240+ */
241+ public <T > CompletableFuture <Void > callFunctionAsync (Consumer <SQLException > exceptionHandler , Class <T > returnType , Consumer <? super Optional <T >> callback , String functionName , Object ... arguments ) {
222242 return CompletableFuture .supplyAsync (supplierHandling (() -> this .callFunction (returnType , functionName , arguments ), exceptionHandler )).thenAcceptAsync (callback );
223243 }
224244
245+ /**
246+ * Calls a stored procedure and returns a {@link Queryable} to fetch the data in the desired format.
247+ *
248+ * @param returnType The Java type which the result will be mapped into. If it is a complex type, it needs to have a parameterless constructor available.
249+ * @param storedProcedureName The name of the stored procedure to call.
250+ * @param arguments The arguments to pass to the stored procedure.
251+ * @param <T> The Java type to be returned?
252+ * @return A {@link Queryable} to fetch the data in the desired format.
253+ */
225254 public <T > Queryable <T > callStoredProcedure (Class <T > returnType , String storedProcedureName , Object ... arguments ) {
226255 return new StoredProcedureQuery <>(returnType , this , storedProcedureName , arguments );
227256 }
228257
258+ /**
259+ * Calls a stored procedure asynchronously and returns a {@link Queryable} to fetch the data in the desired format.
260+ *
261+ * @param returnType The Java type which the result will be mapped into. If it is a complex type, it needs to have a parameterless constructor available.
262+ * @param storedProcedureName The name of the stored procedure to call.
263+ * @param arguments The arguments to pass to the stored procedure.
264+ * @param <T> The Java type to be returned?
265+ * @return An {@link AsyncQueryable} to fetch the data in the desired format.
266+ */
229267 public <T > AsyncQueryable <T > callStoredProcedureAsync (Class <T > returnType , String storedProcedureName , Object ... arguments ) {
230268 return new AsyncStoredProcedureQuery <>(returnType , this , storedProcedureName , arguments );
231269 }
232270
271+ /**
272+ * @return The {@link Connection} object this class uses to operate on.
273+ */
233274 public Connection underlyingConnection () {
234275 return this .underlyingConnection ;
235276 }
@@ -245,11 +286,11 @@ private void log(String text) {
245286 }
246287 }
247288
248- private long updateInternal (Statement statement ) throws SQLException {
289+ private int updateInternal (Statement statement ) throws SQLException {
249290 statement .closeOnCompletion ();
250291 var set = statement .getGeneratedKeys ();
251292 if (set .next ()) {
252- return set .getLong (1 );
293+ return set .getInt (1 );
253294 }
254295
255296 return -1 ;
0 commit comments