Skip to content

Commit

Permalink
Added support for Java 15.
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinAlpert committed Nov 21, 2020
1 parent 5766e4b commit d515990
Show file tree
Hide file tree
Showing 42 changed files with 603 additions and 216 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.collinalpert</groupId>
<artifactId>java2db</artifactId>
<version>5.5.1</version>
<version>5.6.0</version>
<packaging>jar</packaging>

<name>Java2DB</name>
Expand Down Expand Up @@ -62,19 +62,19 @@
<dependency>
<groupId>com.github.collinalpert</groupId>
<artifactId>lambda2sql</artifactId>
<version>2.2.1</version>
<version>2.3.0</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
<version>8.0.22</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.1</version>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down Expand Up @@ -146,7 +146,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<version>3.3.0</version>
<executions>
<execution>
<phase>package</phase>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.github.collinalpert.java2db.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;

/**
* Sets the name of a column in a table for a POJO field.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.github.collinalpert.java2db.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;

/**
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.github.collinalpert.java2db.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;

/**
* This annotation tells Java2DB to use the database-default for a column if the corresponding Java field marked with this annotation is {@code null}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package com.github.collinalpert.java2db.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;

/**
* Marks a field as the correspondent object to a foreign key.
* This property does not have to exist on the database.
* Its parameter is the name of the foreign key column.
*
* <p>
* This annotation can be used on two types of objects:
* 1) Entities which represent real objects of foreign key tables. In this case the entity has to extend
* {@link com.github.collinalpert.java2db.entities.BaseEntity}. Because this entity represents a table it should extend {@code BaseEntity} anyway.
*
* <p>
* 2) Foreign keys to a table with static values that can be represented by an enum because they don't change.
* In that case the enum must extend {@link com.github.collinalpert.java2db.contracts.IdentifiableEnum} and map the ids from the table.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

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

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;

/**
* This annotation is used to indicate that only a specific column of a table is supposed to be joined when executing the query.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.github.collinalpert.java2db.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;

/**
* Marks a field as ignored, meaning it does not exist on the database or should not be filled with values.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.github.collinalpert.java2db.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;

/**
* Specifies the database table name for an entity.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.collinalpert.java2db.database;

import com.github.collinalpert.java2db.transactions.Transaction;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
* @author Collin Alpert
*/
public class ConnectionPool {

private static final Map<String, Transaction> transactions;
private static final DBConnection connection;

static {
connection = new DBConnection();
transactions = new ConcurrentHashMap<>();
}

public static DBConnection getConnection() {
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());
if (transactionOptional.isEmpty()) {
return connection;
}

return transactionOptional.get().getConnection();
}

public static void enlistTransaction(String transactionId, Transaction transaction) {
transactions.put(transactionId, transaction);
}

public static void removeTransaction(String transactionId) {
transactions.remove(transactionId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@

import com.github.collinalpert.java2db.exceptions.ConnectionFailedException;
import com.github.collinalpert.java2db.mappers.FieldMapper;
import com.github.collinalpert.java2db.queries.Queryable;
import com.github.collinalpert.java2db.queries.StoredProcedureQuery;
import com.github.collinalpert.java2db.queries.async.AsyncQueryable;
import com.github.collinalpert.java2db.queries.async.AsyncStoredProcedureQuery;
import com.github.collinalpert.java2db.queries.*;
import com.github.collinalpert.java2db.queries.async.*;
import com.mysql.cj.exceptions.CJCommunicationsException;
import com.mysql.cj.jdbc.exceptions.CommunicationsException;

import java.io.Closeable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Optional;
import java.util.StringJoiner;
import java.sql.*;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

Expand Down Expand Up @@ -63,7 +56,7 @@ public class DBConnection implements Closeable {
*/
public static boolean LOG_QUERIES = true;

private Connection connection;
private Connection underlyingConnection;
private boolean isConnectionValid;

public DBConnection() {
Expand All @@ -73,7 +66,7 @@ public DBConnection() {
System.setProperty("user", USERNAME);
System.setProperty("password", PASSWORD);
DriverManager.setLoginTimeout(TIMEOUT);
connection = DriverManager.getConnection(connectionString, System.getProperties());
underlyingConnection = DriverManager.getConnection(connectionString, System.getProperties());
isConnectionValid = true;
} catch (CJCommunicationsException | CommunicationsException e) {
isConnectionValid = false;
Expand All @@ -84,6 +77,11 @@ public DBConnection() {
}
}

public DBConnection(Connection underlyingConnection) {
this.underlyingConnection = underlyingConnection;
this.isConnectionValid = true;
}

/**
* Checks if the connection is valid/successful.
*
Expand All @@ -102,7 +100,7 @@ public boolean isValid() {
* @throws SQLException if the query is malformed or cannot be executed.
*/
public ResultSet execute(String query) throws SQLException {
var statement = this.connection.createStatement();
var statement = this.underlyingConnection.createStatement();
log(query);
var set = statement.executeQuery(query);
statement.closeOnCompletion();
Expand All @@ -119,7 +117,7 @@ public ResultSet execute(String query) throws SQLException {
* @throws SQLException if the query is malformed or cannot be executed.
*/
public ResultSet execute(String query, Object... params) throws SQLException {
var statement = this.connection.prepareStatement(query);
var statement = this.underlyingConnection.prepareStatement(query);
for (int i = 0; i < params.length; i++) {
statement.setObject(i + 1, params[i]);
}
Expand All @@ -138,7 +136,7 @@ public ResultSet execute(String query, Object... params) throws SQLException {
* @throws SQLException if the query is malformed or cannot be executed.
*/
public long update(String query) throws SQLException {
var statement = this.connection.createStatement();
var statement = this.underlyingConnection.createStatement();
log(query);
statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
return updateInternal(statement);
Expand All @@ -153,7 +151,7 @@ public long update(String query) throws SQLException {
* @throws SQLException if the query is malformed or cannot be executed.
*/
public long update(String query, Object... params) throws SQLException {
var statement = this.connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
var statement = this.underlyingConnection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
for (int i = 0; i < params.length; i++) {
statement.setObject(i + 1, params[i]);
}
Expand All @@ -171,7 +169,7 @@ public long update(String query, Object... params) throws SQLException {
*/
public boolean isOpen() {
try {
return !this.connection.isClosed();
return !this.underlyingConnection.isClosed();
} catch (SQLException e) {
System.err.println("Could not determine connection status");
return this.isConnectionValid = false;
Expand All @@ -184,8 +182,8 @@ public boolean isOpen() {
@Override
public void close() {
try {
if (this.connection != null) {
this.connection.close();
if (this.underlyingConnection != null) {
this.underlyingConnection.close();
}
} catch (SQLException e) {
System.err.println("Could not close database connection");
Expand All @@ -195,6 +193,16 @@ public void close() {
}
}

/**
* Calls an SQL function.
*
* @param returnType The Java equivalent of the SQL datatype the function returns.
* @param functionName The name of the function.
* @param arguments The arguments to be supplied to the function.
* @param <T> The functions return type.
* @return The value of the function, as a Java datatype.
* @throws SQLException In case there is an error communicating with the database, i.e. the function does not exist.
*/
public <T> Optional<T> callFunction(Class<T> returnType, String functionName, Object... arguments) throws SQLException {
var joiner = new StringJoiner(",");
for (int i = 0; i < arguments.length; i++) {
Expand All @@ -210,6 +218,10 @@ public <T> CompletableFuture<Optional<T>> callFunctionAsync(Consumer<SQLExceptio
return CompletableFuture.supplyAsync(supplierHandling(() -> this.callFunction(returnType, functionName, arguments), exceptionHandler));
}

public <T> CompletableFuture<Void> callFunctionAsync(Consumer<SQLException> exceptionHandler, Consumer<? super Optional<T>> callback, Class<T> returnType, String functionName, Object... arguments) {
return CompletableFuture.supplyAsync(supplierHandling(() -> this.callFunction(returnType, functionName, arguments), exceptionHandler)).thenAcceptAsync(callback);
}

public <T> Queryable<T> callStoredProcedure(Class<T> returnType, String storedProcedureName, Object... arguments) {
return new StoredProcedureQuery<>(returnType, this, storedProcedureName, arguments);
}
Expand All @@ -218,6 +230,10 @@ public <T> AsyncQueryable<T> callStoredProcedureAsync(Class<T> returnType, Strin
return new AsyncStoredProcedureQuery<>(returnType, this, storedProcedureName, arguments);
}

public Connection underlyingConnection() {
return this.underlyingConnection;
}

/**
* Prints queries to the console, while considering the {@link DBConnection#LOG_QUERIES} constant.
*
Expand Down

0 comments on commit d515990

Please sign in to comment.