Skip to content

Commit 0ad5f27

Browse files
committed
Released 6.0.0
1 parent d515990 commit 0ad5f27

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1378
-856
lines changed

README.md

Lines changed: 112 additions & 39 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.collinalpert</groupId>
88
<artifactId>java2db</artifactId>
9-
<version>5.6.0</version>
9+
<version>6.0.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Java2DB</name>
@@ -62,7 +62,7 @@
6262
<dependency>
6363
<groupId>com.github.collinalpert</groupId>
6464
<artifactId>lambda2sql</artifactId>
65-
<version>2.3.0</version>
65+
<version>2.4.0</version>
6666
</dependency>
6767

6868
<dependency>
@@ -111,7 +111,10 @@
111111
<artifactId>maven-javadoc-plugin</artifactId>
112112
<version>3.2.0</version>
113113
<configuration>
114-
<additionalOptions>-html5</additionalOptions>
114+
<additionalOptions>
115+
<additionalOption>-html5</additionalOption>
116+
<additionalOption>-Xdoclint:none</additionalOption>
117+
</additionalOptions>
115118
</configuration>
116119
<executions>
117120
<execution>

src/main/java/com/github/collinalpert/java2db/annotations/ForeignKeyEntity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424
JoinTypes joinType() default JoinTypes.LEFT;
2525

26+
/**
27+
* An enum for specifying the type of join to use when leveraging the automatic join feature via the {@link ForeignKeyEntity} attribute.
28+
*/
2629
enum JoinTypes {
2730
LEFT("left"),
2831
INNER("inner"),

src/main/java/com/github/collinalpert/java2db/annotations/ForeignKeyPath.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/**
88
* This annotation is used to indicate that only a specific column of a table is supposed to be joined when executing the query.
9-
* It has to be used in conjunction with the {@link ForeignKeyEntity} attribute.
9+
* It must be used in conjunction with the {@link ForeignKeyEntity} attribute.
1010
*
1111
* @author Collin Alpert
1212
*/

src/main/java/com/github/collinalpert/java2db/contracts/IdentifiableEnum.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
* @author Collin Alpert
88
*/
99
public interface IdentifiableEnum {
10-
long getId();
10+
int getId();
1111
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.github.collinalpert.java2db.database;
2+
3+
/**
4+
* A container for database access. Contains all necessary information for this library to access a database.
5+
*
6+
* @author Collin Alpert
7+
*/
8+
public class ConnectionConfiguration {
9+
10+
/**
11+
* Specifies the hostname/ip address of the database.
12+
*/
13+
private final String host;
14+
15+
/**
16+
* Specifies the name of the database to connect to.
17+
*/
18+
private final String database;
19+
20+
/**
21+
* Specifies the username to log in on the database with.
22+
*/
23+
private final String username;
24+
25+
/**
26+
* Specifies the password to log in on the database with.
27+
*/
28+
private final String password;
29+
30+
/**
31+
* Specifies the port to connect to the database on.
32+
* This property is optional. If not specified, it will be set to 3306, the default port of MySQL.
33+
*/
34+
private final int port;
35+
36+
/**
37+
* Specifies the login timeout to the database in seconds. Default is 5 seconds.
38+
*/
39+
private final int timeout;
40+
41+
public ConnectionConfiguration(String host, String database, String username, String password) {
42+
this(host, database, username, password, 3306, 5);
43+
}
44+
45+
public ConnectionConfiguration(String host, String database, String username, String password, int port, int timeout) {
46+
this.host = host;
47+
this.database = database;
48+
this.username = username;
49+
this.password = password;
50+
this.port = port;
51+
this.timeout = timeout;
52+
}
53+
54+
public String getHost() {
55+
return host;
56+
}
57+
58+
public String getDatabase() {
59+
return database;
60+
}
61+
62+
public String getUsername() {
63+
return username;
64+
}
65+
66+
public String getPassword() {
67+
return password;
68+
}
69+
70+
public int getPort() {
71+
return port;
72+
}
73+
74+
public int getTimeout() {
75+
return timeout;
76+
}
77+
78+
79+
}

src/main/java/com/github/collinalpert/java2db/database/ConnectionPool.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/main/java/com/github/collinalpert/java2db/database/DBConnection.java

Lines changed: 84 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,13 @@
1616
import 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
*/
2124
public 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

Comments
 (0)