Skip to content

Commit d515990

Browse files
committed
Added support for Java 15.
1 parent 5766e4b commit d515990

Some content is hidden

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

42 files changed

+603
-216
lines changed

pom.xml

Lines changed: 5 additions & 5 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.5.1</version>
9+
<version>5.6.0</version>
1010
<packaging>jar</packaging>
1111

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

6868
<dependency>
6969
<groupId>mysql</groupId>
7070
<artifactId>mysql-connector-java</artifactId>
71-
<version>8.0.19</version>
71+
<version>8.0.22</version>
7272
</dependency>
7373

7474
<dependency>
7575
<groupId>org.junit.jupiter</groupId>
7676
<artifactId>junit-jupiter-api</artifactId>
77-
<version>5.6.1</version>
77+
<version>5.7.0</version>
7878
<scope>test</scope>
7979
</dependency>
8080
</dependencies>
@@ -146,7 +146,7 @@
146146
<plugin>
147147
<groupId>org.apache.maven.plugins</groupId>
148148
<artifactId>maven-assembly-plugin</artifactId>
149-
<version>3.2.0</version>
149+
<version>3.3.0</version>
150150
<executions>
151151
<execution>
152152
<phase>package</phase>

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.github.collinalpert.java2db.annotations;
22

3-
import java.lang.annotation.ElementType;
4-
import java.lang.annotation.Retention;
5-
import java.lang.annotation.RetentionPolicy;
6-
import java.lang.annotation.Target;
3+
import java.lang.annotation.*;
74

85
/**
96
* Sets the name of a column in a table for a POJO field.

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.github.collinalpert.java2db.annotations;
22

3-
import java.lang.annotation.ElementType;
4-
import java.lang.annotation.Retention;
5-
import java.lang.annotation.RetentionPolicy;
6-
import java.lang.annotation.Target;
3+
import java.lang.annotation.*;
74

85
/**
96
* This annotation tells Java2DB to always use the database-default for a column on create or update. Not to be confused with the {@link DefaultIfNull} annotation.

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.github.collinalpert.java2db.annotations;
22

3-
import java.lang.annotation.ElementType;
4-
import java.lang.annotation.Retention;
5-
import java.lang.annotation.RetentionPolicy;
6-
import java.lang.annotation.Target;
3+
import java.lang.annotation.*;
74

85
/**
96
* This annotation tells Java2DB to use the database-default for a column if the corresponding Java field marked with this annotation is {@code null}.

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
package com.github.collinalpert.java2db.annotations;
22

3-
import java.lang.annotation.ElementType;
4-
import java.lang.annotation.Retention;
5-
import java.lang.annotation.RetentionPolicy;
6-
import java.lang.annotation.Target;
3+
import java.lang.annotation.*;
74

85
/**
96
* Marks a field as the correspondent object to a foreign key.
107
* This property does not have to exist on the database.
118
* Its parameter is the name of the foreign key column.
12-
*
9+
* <p>
1310
* This annotation can be used on two types of objects:
1411
* 1) Entities which represent real objects of foreign key tables. In this case the entity has to extend
1512
* {@link com.github.collinalpert.java2db.entities.BaseEntity}. Because this entity represents a table it should extend {@code BaseEntity} anyway.
16-
*
13+
* <p>
1714
* 2) Foreign keys to a table with static values that can be represented by an enum because they don't change.
1815
* In that case the enum must extend {@link com.github.collinalpert.java2db.contracts.IdentifiableEnum} and map the ids from the table.
1916
*

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import com.github.collinalpert.java2db.entities.BaseEntity;
44

5-
import java.lang.annotation.ElementType;
6-
import java.lang.annotation.Retention;
7-
import java.lang.annotation.RetentionPolicy;
8-
import java.lang.annotation.Target;
5+
import java.lang.annotation.*;
96

107
/**
118
* This annotation is used to indicate that only a specific column of a table is supposed to be joined when executing the query.

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.github.collinalpert.java2db.annotations;
22

3-
import java.lang.annotation.ElementType;
4-
import java.lang.annotation.Retention;
5-
import java.lang.annotation.RetentionPolicy;
6-
import java.lang.annotation.Target;
3+
import java.lang.annotation.*;
74

85
/**
96
* Marks a field as ignored, meaning it does not exist on the database or should not be filled with values.

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.github.collinalpert.java2db.annotations;
22

3-
import java.lang.annotation.ElementType;
4-
import java.lang.annotation.Retention;
5-
import java.lang.annotation.RetentionPolicy;
6-
import java.lang.annotation.Target;
3+
import java.lang.annotation.*;
74

85
/**
96
* Specifies the database table name for an entity.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.github.collinalpert.java2db.database;
2+
3+
import com.github.collinalpert.java2db.transactions.Transaction;
4+
5+
import java.util.*;
6+
import java.util.concurrent.ConcurrentHashMap;
7+
8+
/**
9+
* @author Collin Alpert
10+
*/
11+
public class ConnectionPool {
12+
13+
private static final Map<String, Transaction> transactions;
14+
private static final DBConnection connection;
15+
16+
static {
17+
connection = new DBConnection();
18+
transactions = new ConcurrentHashMap<>();
19+
}
20+
21+
public static DBConnection getConnection() {
22+
var transactionOptional = StackWalker.getInstance().walk(s -> s.limit(10).map(x -> transactions.get(String.format("%s$%s", x.getClassName(), x.getMethodName()))).filter(Objects::nonNull).findFirst());
23+
if (transactionOptional.isEmpty()) {
24+
return connection;
25+
}
26+
27+
return transactionOptional.get().getConnection();
28+
}
29+
30+
public static void enlistTransaction(String transactionId, Transaction transaction) {
31+
transactions.put(transactionId, transaction);
32+
}
33+
34+
public static void removeTransaction(String transactionId) {
35+
transactions.remove(transactionId);
36+
}
37+
}

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

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,14 @@
22

33
import com.github.collinalpert.java2db.exceptions.ConnectionFailedException;
44
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.*;
97
import com.mysql.cj.exceptions.CJCommunicationsException;
108
import com.mysql.cj.jdbc.exceptions.CommunicationsException;
119

1210
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.*;
2013
import java.util.concurrent.CompletableFuture;
2114
import 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

Comments
 (0)