Add generic return type methods

This commit is contained in:
ceze88 2024-07-13 14:22:35 +02:00
parent c3cd6baf49
commit 9b7e65992a
5 changed files with 133 additions and 0 deletions

View File

@ -34,6 +34,16 @@ public interface DatabaseConnector {
*/
OptionalResult connectOptional(ConnectionOptionalCallback callback);
/**
* Executes a callback with a Connection passed and automatically closes it when finished
*
* @param callback The callback to execute once the connection is retrieved
* @param defaultValue The default value (First value only) to return if the callback fails (Also used to make automatic type inference work)
*
* @return The result of the callback
*/
<T> T connectResult(ConnectResult<T> callback, T... defaultValue);
/**
* Executes a callback with a DSLContext passed and automatically closes it when finished
*
@ -50,6 +60,16 @@ public interface DatabaseConnector {
*/
OptionalResult connectDSLOptional(DSLContextOptionalCallback callback);
/**
* Executes a callback with a DSLContext passed and automatically closes it when finished
*
* @param callback The callback to execute once the connection is retrieved
* @param defaultValue The default value (First value only) to return if the callback fails (Also used to make automatic type inference work)
*
* @return The result of the callback
*/
<T> T connectDSLResult(DSLConnectResult<T> callback, T... defaultValue);
/**
* Wraps a connection in a callback which will automagically handle catching sql errors
*/
@ -82,6 +102,19 @@ public interface DatabaseConnector {
OptionalResult accept(DSLContext context) throws SQLException;
}
/**
* Wraps a connection in a callback which will
* automagically handle catching sql errors
* Can return a value
*/
interface ConnectResult<T> {
T accept(Connection connection) throws SQLException;
}
interface DSLConnectResult<T> {
T accept(DSLContext context) throws SQLException;
}
/**
* Gets a connection from the database
*

View File

@ -100,6 +100,20 @@ public class H2Connector implements DatabaseConnector {
return OptionalResult.empty();
}
@Override
public <T> T connectResult(ConnectResult<T> callback, T... defaultValue) {
try (Connection connection = getConnection()) {
T result = callback.accept(connection);
return result != null ? result : defaultValue.length > 0 ? defaultValue[0] : null;
} catch (Exception ex) {
if (this.plugin != null) {
SongodaCore.getLogger().severe("An error occurred executing a H2 query: " + ex.getMessage());
}
ex.printStackTrace();
return defaultValue.length > 0 ? defaultValue[0] : null;
}
}
@Override
public void connectDSL(DSLContextCallback callback) {
try (Connection connection = getConnection()) {
@ -125,6 +139,20 @@ public class H2Connector implements DatabaseConnector {
return OptionalResult.empty();
}
@Override
public <T> T connectDSLResult(DSLConnectResult<T> callback, T... defaultValue) {
try (Connection connection = getConnection()) {
T result = callback.accept(DSL.using(connection, SQLDialect.MYSQL));
return result != null ? result : defaultValue.length > 0 ? defaultValue[0] : null;
} catch (Exception ex) {
if (this.plugin != null) {
SongodaCore.getLogger().severe("An error occurred executing a H2 query: " + ex.getMessage());
}
ex.printStackTrace();
return defaultValue.length > 0 ? defaultValue[0] : null;
}
}
@Override
public Connection getConnection() throws SQLException {
return this.hikari.getConnection();

View File

@ -83,6 +83,18 @@ public class MariaDBConnector implements DatabaseConnector {
return OptionalResult.empty();
}
@Override
public <T> T connectResult(ConnectResult<T> callback, T... defaultValue) {
try (Connection connection = getConnection()) {
T result = callback.accept(connection);
return result != null ? result : defaultValue.length > 0 ? defaultValue[0] : null;
} catch (Exception ex) {
this.plugin.getLogger().severe("An error occurred executing a MariaDB query: " + ex.getMessage());
ex.printStackTrace();
return defaultValue.length > 0 ? defaultValue[0] : null;
}
}
@Override
public void connectDSL(DSLContextCallback callback) {
try (Connection connection = getConnection()){
@ -104,6 +116,18 @@ public class MariaDBConnector implements DatabaseConnector {
return OptionalResult.empty();
}
@Override
public <T> T connectDSLResult(DSLConnectResult<T> callback, T... defaultValue) {
try (Connection connection = getConnection()) {
T result = callback.accept(DSL.using(connection, SQLDialect.MARIADB));
return result != null ? result : defaultValue.length > 0 ? defaultValue[0] : null;
} catch (Exception ex) {
this.plugin.getLogger().severe("An error occurred executing a MariaDB query: " + ex.getMessage());
ex.printStackTrace();
return defaultValue.length > 0 ? defaultValue[0] : null;
}
}
@Override
public Connection getConnection() throws SQLException {
return this.hikari.getConnection();

View File

@ -79,6 +79,18 @@ public class MySQLConnector implements DatabaseConnector {
return OptionalResult.empty();
}
@Override
public <T> T connectResult(ConnectResult<T> callback, T... defaultValue) {
try (Connection connection = getConnection()) {
T result = callback.accept(connection);
return result != null ? result : defaultValue.length > 0 ? defaultValue[0] : null;
} catch (Exception ex) {
SongodaCore.getLogger().severe("An error occurred executing a MySQL query: " + ex.getMessage());
ex.printStackTrace();
return defaultValue.length > 0 ? defaultValue[0] : null;
}
}
@Override
public void connectDSL(DSLContextCallback callback) {
try (Connection connection = getConnection()) {
@ -100,6 +112,18 @@ public class MySQLConnector implements DatabaseConnector {
return OptionalResult.empty();
}
@Override
public <T> T connectDSLResult(DSLConnectResult<T> callback, T... defaultValue) {
try (Connection connection = getConnection()) {
T result = callback.accept(DSL.using(connection, SQLDialect.MYSQL));
return result != null ? result : defaultValue.length > 0 ? defaultValue[0] : null;
} catch (Exception ex) {
SongodaCore.getLogger().severe("An error occurred executing a MySQL query: " + ex.getMessage());
ex.printStackTrace();
return defaultValue.length > 0 ? defaultValue[0] : null;
}
}
@Override
public Connection getConnection() throws SQLException {
return this.hikari.getConnection();

View File

@ -69,6 +69,18 @@ public class SQLiteConnector implements DatabaseConnector {
return OptionalResult.empty();
}
@Override
public <T> T connectResult(ConnectResult<T> callback, T... defaultValue) {
try (Connection connection = getConnection()) {
T result = callback.accept(connection);
return result != null ? result : defaultValue.length > 0 ? defaultValue[0] : null;
} catch (Exception ex) {
this.plugin.getLogger().severe("An error occurred executing a SQLite query: " + ex.getMessage());
ex.printStackTrace();
return defaultValue.length > 0 ? defaultValue[0] : null;
}
}
@Override
public void connectDSL(DSLContextCallback callback) {
try (Connection connection = getConnection()) {
@ -90,6 +102,18 @@ public class SQLiteConnector implements DatabaseConnector {
return OptionalResult.empty();
}
@Override
public <T> T connectDSLResult(DSLConnectResult<T> callback, T... defaultValue) {
try (Connection connection = getConnection()) {
T result = callback.accept(DSL.using(connection, SQLDialect.SQLITE));
return result != null ? result : defaultValue.length > 0 ? defaultValue[0] : null;
} catch (Exception ex) {
this.plugin.getLogger().severe("An error occurred executing a SQLite query: " + ex.getMessage());
ex.printStackTrace();
return defaultValue.length > 0 ? defaultValue[0] : null;
}
}
@Override
public Connection getConnection() {
try {