Skip to content

Commit

Permalink
feat: batch sql queries
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Nov 14, 2023
1 parent 379ca89 commit 14fa8f2
Show file tree
Hide file tree
Showing 14 changed files with 161 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* SOFTWARE.
*/

package com.github.mjeanroy.dbunit.core.jdbc;
package com.github.mjeanroy.dbunit.commons.jdbc;

import com.github.mjeanroy.dbunit.exception.JdbcException;
import com.github.mjeanroy.dbunit.loggers.Logger;
Expand All @@ -36,14 +36,23 @@
import java.util.Collection;
import java.util.List;

final class JdbcUtils {
/**
* Static JDBC Utilities, should only be used internally.
*/
public final class JdbcUtils {

private static final Logger log = Loggers.getLogger(JdbcUtils.class);

private JdbcUtils() {
}

static void loadDriver(String driverClassName) {
/**
* Load JDBC Driver.
*
* @param driverClassName Driver classname.
* @throws JdbcException If JDBC driver cannot be loaded.
*/
public static void loadDriver(String driverClassName) {
log.info("Loading driver: {}", driverClassName);

try {
Expand All @@ -54,7 +63,15 @@ static void loadDriver(String driverClassName) {
}
}

static ResultSet executeQuery(Connection connection, String query) {
/**
* Execute SQL Query and returns result.
*
* @param connection JDBC Connection.
* @param query SQL Query.
* @return JDBC Result Set.
* @throws JdbcException If the SQL cannot be executed.
*/
public static ResultSet executeQuery(Connection connection, String query) {
log.debug("Executing query: {}", query);

try {
Expand All @@ -65,7 +82,16 @@ static ResultSet executeQuery(Connection connection, String query) {
}
}

static <T> List<T> executeQuery(Connection connection, String query, ResultSetMapFunction<T> mapFunction) {
/**
* Execute SQL Query and returns mapped result.
*
* @param connection JDBC Connection.
* @param query SQL Query.
* @param mapFunction The resultset mapping function, executed for each returned rows.
* @return Results.
* @param <T> Type of results being returned.
*/
public static <T> List<T> executeQuery(Connection connection, String query, ResultSetMapFunction<T> mapFunction) {
log.debug("Executing query: {}", query);

try (ResultSet resultSet = connection.createStatement().executeQuery(query)) {
Expand All @@ -82,24 +108,33 @@ static <T> List<T> executeQuery(Connection connection, String query, ResultSetMa
}
}

static void executeUpdates(Connection connection, Collection<String> queries) {
log.debug("Executing queries: {}", queries);
for (String query : queries) {
executeUpdate(connection, query);
/**
* Execute SQL Queries in a batch statement.
*
* @param connection JDBC Connection.
* @param queries SQL Queries.
*/
public static void executeQueries(Connection connection, Collection<String> queries) {
if (queries.isEmpty()) {
return;
}

if (queries.size() == 1) {
executeQuery(connection, queries.iterator().next());
return;
}
}

static void executeUpdate(Connection connection, String query) {
log.debug("Executing update: {}", query);
log.debug("Executing batched queries: {}", queries);
try (Statement statement = connection.createStatement()) {
statement.executeUpdate(query);
for (String query : queries) {
log.debug("Adding batched query: {}", query);
statement.addBatch(query);
}

statement.executeBatch();
}
catch (SQLException ex) {
throw new JdbcException("Cannot execute query: " + query, ex);
throw new JdbcException("Cannot execute queries: " + queries, ex);
}
}

interface ResultSetMapFunction<T> {
T apply(ResultSet resultSet) throws Exception;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2015-2023 Mickael Jeanroy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.github.mjeanroy.dbunit.commons.jdbc;

import java.sql.ResultSet;

/**
* Functional interface used to map {@link ResultSet} to another data structure.
*
* @param <T> Mapped type.
*/
public interface ResultSetMapFunction<T> {

/**
* Map {@code resultSet} to another type.
*
* @param resultSet Input.
* @return Output.
* @throws Exception If an error occurred during mapping.
*/
T apply(ResultSet resultSet) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.util.ArrayList;
import java.util.List;

import static com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.executeUpdates;
import static com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils.executeQueries;

abstract class AbstractJdbcDropCreateForeignKeyManager<T> implements JdbcForeignKeyManager {

Expand Down Expand Up @@ -95,7 +95,7 @@ private void dropForeignKeys(Connection connection) {
}

try {
executeUpdates(connection, queries);
executeQueries(connection, queries);
}
catch (Exception ex) {
log.error(ex.getMessage(), ex);
Expand All @@ -119,7 +119,7 @@ private void reCreateForeignKeys(Connection connection) {
}

try {
executeUpdates(connection, queries);
executeQueries(connection, queries);
}
catch (Exception ex) {
log.error(ex.getMessage(), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
package com.github.mjeanroy.dbunit.core.jdbc;

import com.github.mjeanroy.dbunit.commons.lang.ToStringBuilder;
import com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.ResultSetMapFunction;
import com.github.mjeanroy.dbunit.commons.jdbc.ResultSetMapFunction;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.List;
import java.util.Objects;

import static com.github.mjeanroy.dbunit.commons.lang.PreConditions.notEmpty;
import static com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.executeQuery;
import static com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils.executeQuery;
import static java.util.Collections.singletonList;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package com.github.mjeanroy.dbunit.core.jdbc;

import com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils;
import com.github.mjeanroy.dbunit.commons.lang.ToStringBuilder;

import java.sql.Connection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package com.github.mjeanroy.dbunit.core.jdbc;

import com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils;
import com.github.mjeanroy.dbunit.exception.JdbcException;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
package com.github.mjeanroy.dbunit.core.jdbc;

import com.github.mjeanroy.dbunit.commons.lang.ToStringBuilder;
import com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.ResultSetMapFunction;
import com.github.mjeanroy.dbunit.commons.jdbc.ResultSetMapFunction;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.List;
import java.util.Objects;

import static com.github.mjeanroy.dbunit.commons.lang.PreConditions.notEmpty;
import static com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.executeQuery;
import static com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils.executeQuery;
import static java.util.Collections.singletonList;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
package com.github.mjeanroy.dbunit.core.jdbc;

import com.github.mjeanroy.dbunit.commons.lang.ToStringBuilder;
import com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.ResultSetMapFunction;
import com.github.mjeanroy.dbunit.commons.jdbc.ResultSetMapFunction;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.List;
import java.util.Objects;

import static com.github.mjeanroy.dbunit.commons.lang.PreConditions.notEmpty;
import static com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.executeQuery;
import static com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils.executeQuery;
import static java.util.Collections.singletonList;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package com.github.mjeanroy.dbunit.core.jdbc;

import com.github.mjeanroy.dbunit.commons.jdbc.ResultSetMapFunction;
import com.github.mjeanroy.dbunit.commons.lang.ToStringBuilder;

import java.sql.Connection;
Expand All @@ -32,7 +33,7 @@
import java.util.Objects;

import static com.github.mjeanroy.dbunit.commons.lang.PreConditions.notEmpty;
import static com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.executeQuery;
import static com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils.executeQuery;
import static java.util.Collections.singletonList;

/**
Expand Down Expand Up @@ -133,7 +134,7 @@ public String toString() {
}
}

private static final class ForeignKeyMapFunction implements JdbcUtils.ResultSetMapFunction<ForeignKey> {
private static final class ForeignKeyMapFunction implements ResultSetMapFunction<ForeignKey> {

@Override
public ForeignKey apply(ResultSet resultSet) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
package com.github.mjeanroy.dbunit.core.jdbc;

import com.github.mjeanroy.dbunit.commons.lang.ToStringBuilder;
import com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.ResultSetMapFunction;
import com.github.mjeanroy.dbunit.commons.jdbc.ResultSetMapFunction;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.List;
import java.util.Objects;

import static com.github.mjeanroy.dbunit.commons.lang.PreConditions.notEmpty;
import static com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.executeQuery;
import static com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils.executeQuery;
import static java.util.Collections.singletonList;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import java.sql.Connection;
import java.sql.SQLException;

import static com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils.executeQueries;
import static com.github.mjeanroy.dbunit.commons.lang.PreConditions.notNull;
import static com.github.mjeanroy.dbunit.core.sql.SqlScriptParser.executeQueries;

/**
* Function to execute SQL scripts against SQL connection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.List;

import static com.github.mjeanroy.dbunit.commons.io.Io.readLines;
import static com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils.executeQueries;
import static com.github.mjeanroy.dbunit.commons.lang.Objects.firstNonNull;

/**
Expand Down Expand Up @@ -144,17 +145,4 @@ public static void executeScript(Connection connection, String sqlFilePath, SqlS
List<String> queries = parseScript(sqlFilePath, configuration);
executeQueries(connection, queries);
}

/**
* Execute SQL Queries.
*
* @param connection SQL Connection.
* @param queries SQL Queries.
* @throws SQLException If an error occurred while executing queries.
*/
public static void executeQueries(Connection connection, List<String> queries) throws SQLException {
for (String query : queries) {
connection.prepareStatement(query).execute();
}
}
}
Loading

0 comments on commit 14fa8f2

Please sign in to comment.