Skip to content

Commit

Permalink
Merge pull request #12 from Jaoow/fix/exceptions
Browse files Browse the repository at this point in the history
🐛 Fixing error calls.
  • Loading branch information
SrBlecaute01 authored Jan 17, 2024
2 parents e3996ef + de86834 commit a0f396a
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.jaoow.sql.connector;

import org.jetbrains.annotations.NotNull;

import java.sql.Connection;
import java.sql.SQLException;

/**
* This is a functional interface that accepts the {@link Connection} without
* needing to handle the {@link SQLException} error.
*/
@FunctionalInterface
public interface ConnectionConsumer {

/**
* Performs this operation with the @{@link Connection}.
*
* @param connection The @{@link Connection}.
*/
void execute(@NotNull Connection connection) throws SQLException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.jaoow.sql.connector;

import lombok.NonNull;

/**
* Custom exception for throwing connector exceptions at runtime.
*/
public class ConnectorException extends RuntimeException {

/**
* Constructs a new connector runtime exception with null cause.
*/
public ConnectorException() {
super();
}

/**
* Constructs a new connector runtime exception.
*
* @param cause The cause.
*/
public ConnectorException(@NonNull Throwable cause) {
super(cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

import org.jetbrains.annotations.NotNull;

import java.sql.Connection;
import java.util.function.Consumer;

@FunctionalInterface
public interface SQLConnector {

void execute(@NotNull Consumer<Connection> consumer);
void execute(@NotNull ConnectionConsumer connection);

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jaoow.sql.connector.type.impl;

import com.jaoow.sql.connector.ConnectorException;
import com.jaoow.sql.connector.SQLConnector;
import com.jaoow.sql.connector.type.SQLDatabaseType;
import com.zaxxer.hikari.HikariDataSource;
Expand Down Expand Up @@ -78,16 +79,16 @@ public SQLDatabaseType configureDataSource(@NotNull Consumer<HikariDataSource> c
@NotNull
@Override
public SQLConnector connect() throws SQLException {

// Test if connection was established.
dataSource.getConnection().close();

return consumer -> {
try (Connection connection = dataSource.getConnection()) {
consumer.accept(connection);
} catch (SQLException e) {
e.printStackTrace();
consumer.execute(connection);
} catch (SQLException exception) {
throw new ConnectorException(exception);
}
};

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jaoow.sql.connector.type.impl;

import com.jaoow.sql.connector.ConnectorException;
import com.jaoow.sql.connector.SQLConnector;
import com.jaoow.sql.connector.type.SQLDatabaseType;
import lombok.Builder;
Expand Down Expand Up @@ -56,6 +57,7 @@ public SQLiteDatabaseType(@NotNull File file) {
}

@Override
@NotNull
public SQLConnector connect() throws SQLException {

try {
Expand All @@ -66,9 +68,9 @@ public SQLConnector connect() throws SQLException {

return consumer -> {
try (Connection connection = source.getConnection()) {
consumer.accept(connection);
consumer.execute(connection);
} catch (SQLException exception) {
exception.printStackTrace();
throw new ConnectorException(exception);
}
};
}
Expand Down
46 changes: 8 additions & 38 deletions executor/src/main/java/com/jaoow/sql/executor/SQLExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public final class SQLExecutor {

@NotNull private final SQLConnector sqlConnector;
@NotNull private final Map<Class<?>, SQLResultAdapter<?>> adapters;

@NotNull private Executor executor = ForkJoinPool.commonPool();

/**
Expand Down Expand Up @@ -158,9 +159,6 @@ public void execute(@Language("MySQL") @NotNull String sql, int autoGeneratedKey
prepare.accept(statement);
statement.execute();
result.accept(statement);

} catch (SQLException exception) {
exception.printStackTrace();
}
});
}
Expand Down Expand Up @@ -263,9 +261,6 @@ public <T> Optional<T> query(@Language("MySQL") @NotNull String query,
try (ResultSet resultSet = statement.executeQuery()) {
reference.set(Optional.ofNullable(function.apply(resultSet)));
}

} catch (SQLException exception) {
exception.printStackTrace();
}
});

Expand Down Expand Up @@ -296,14 +291,7 @@ public <T> Optional<T> query(@Language("MySQL") @NotNull String query, @NotNull
* @see #queryAsync(String, Class) to execute in asynchronous thread
*/
public <T> Optional<T> query(@Language("MySQL") @NotNull String query, @NotNull Class<T> clazz) {
return query(query, EMPTY_STATEMENT, resultSet -> {
try {
return resultSet.next() ? getAdapter(clazz).adaptResult(resultSet) : null;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
});
return query(query, EMPTY_STATEMENT, resultSet -> resultSet.next() ? getAdapter(clazz).adaptResult(resultSet) : null);
}

/**
Expand All @@ -321,14 +309,7 @@ public <T> Optional<T> query(@Language("MySQL") @NotNull String query,
@NotNull StatementConsumer consumer,
@NotNull Class<T> clazz
) {
return query(query, consumer, resultSet -> {
try {
return resultSet.next() ? getAdapter(clazz).adaptResult(resultSet) : null;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
});
return query(query, consumer, resultSet -> resultSet.next() ? getAdapter(clazz).adaptResult(resultSet) : null);
}

/**
Expand Down Expand Up @@ -358,8 +339,6 @@ public Optional<ResultSet> query(@Language("MySQL") @NotNull String query, @NotN
try (PreparedStatement statement = connection.prepareStatement(query)) {
consumer.accept(statement);
reference.set(Optional.ofNullable(statement.executeQuery()));
} catch (SQLException exception) {
exception.printStackTrace();
}
});
return reference.get();
Expand Down Expand Up @@ -502,17 +481,10 @@ public <T> Set<T> queryMany(@Language("MySQL") @NotNull String query,
return this.query(query, consumer, result -> {

Set<T> elements = new LinkedHashSet<>();
while (true) {
try {
if (!result.next()) break;
T value = adapter.adaptResult(result);

if (value != null) {
elements.add(value);
}
} catch (SQLException e) {
e.printStackTrace();
}
while (result.next()) {
T value = adapter.adaptResult(result);
if (value != null)
elements.add(value);
}

return elements;
Expand Down Expand Up @@ -634,9 +606,6 @@ public void executeBatch(@NotNull BatchBuilder builder, int autoGeneratedKeys, @

statement.executeBatch();
result.accept(statement);

} catch (SQLException e) {
e.printStackTrace();
}
});
}
Expand Down Expand Up @@ -681,6 +650,7 @@ public void executeBatch(@NotNull BatchBuilder builder, int autoGeneratedKeys, @
public @NotNull CompletableFuture<Void> executeBatchAsync(@NotNull BatchBuilder builder, @NotNull ResultSetConsumer result) {
return CompletableFuture.runAsync(() -> this.executeBatch(builder, result));
}

/**
* Executes a batched database execution and retrieve statement.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public SQLExecutorBuilder(@NotNull SQLConnector connector) {
* @param executor the @{@link Executor}
* @return the @{@link SQLExecutorBuilder}
*/

@NotNull
public SQLExecutorBuilder setExecutor(@Nullable Executor executor) {
this.executor = executor;
Expand All @@ -56,7 +55,6 @@ public SQLExecutorBuilder setExecutor(@Nullable Executor executor) {
* @param <T> the type
* @return the @{@link SQLExecutorBuilder}
*/

@NotNull
public <T> SQLExecutorBuilder registerAdapter(@NotNull Class<T> clazz, @NotNull SQLResultAdapter<T> adapter) {
adapters.put(clazz, adapter);
Expand All @@ -68,13 +66,11 @@ public <T> SQLExecutorBuilder registerAdapter(@NotNull Class<T> clazz, @NotNull
*
* @return the @{@link SQLExecutor}
*/

@NotNull
public SQLExecutor build() {
Map<Class<?>, SQLResultAdapter<?>> immutable = Collections.unmodifiableMap(adapters);
return executor == null ?
new SQLExecutor(connector, immutable) :
new SQLExecutor(connector, immutable, executor);

}
}

0 comments on commit a0f396a

Please sign in to comment.