<commit_before>/*
 * Copyright (c) 2010-2018. Axon Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.axonframework.common.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.IntStream;

/**
 * Utility class for safely executing Jdbc queries.
 *
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 * @since 2.2
 */
public class JdbcUtils {

    /**
     * Execute the query given by the {@code sqlFunction}. The {@link ResultSet} returned when the query is executed
     * will be converted using the given {@code sqlResultConverter}. Any errors will be handled by the given {@code
     * errorHandler}.
     *
     * @param connection         connection to the underlying database that should be used for the query
     * @param sqlFunction        the function that returns a {@link PreparedStatement} to execute the query against
     * @param sqlResultConverter converts the result set to a value of type R
     * @param errorHandler       handles errors as result of executing the query or converting the result set
     * @param <R>                the result of the query after conversion
     * @return the query result
     */
    public static <R> R executeQuery(Connection connection, SqlFunction sqlFunction,
                                     SqlResultConverter<R> sqlResultConverter,
                                     Function<SQLException, RuntimeException> errorHandler) {
        return executeQuery(connection, sqlFunction, sqlResultConverter, errorHandler, true);
    }

    /**
     * Execute the query given by the {@code sqlFunction}. The {@link ResultSet} returned when the query is executed
     * will be converted using the given {@code sqlResultConverter}. Any errors will be handled by the given {@code
     * errorHandler}.
     *
     * @param connection         connection to the underlying database that should be used for the query
     * @param sqlFunction        the function that returns a {@link PreparedStatement} to execute the query against
     * @param sqlResultConverter converts the result set to a value of type R
     * @param errorHandler       handles errors as result of executing the query or converting the result set
     * @param <R>                the result of the query after conversion
     * @param closeConnection    whether provided {@code connection} should be closed or not
     * @return the query result
     */<commit_msg>Adapted JavaDoc.<commit_after>/*
 * Copyright (c) 2010-2018. Axon Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.axonframework.common.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.IntStream;

/**
 * Utility class for safely executing Jdbc queries.
 *
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 * @since 2.2
 */
public class JdbcUtils {

    /**
     * Execute the query given by the {@code sqlFunction}. The {@link ResultSet} returned when the query is executed
     * will be converted using the given {@code sqlResultConverter}. Any errors will be handled by the given {@code
     * errorHandler}.
     *
     * @param connection         connection to the underlying database that should be used for the query
     * @param sqlFunction        the function that returns a {@link PreparedStatement} to execute the query against
     * @param sqlResultConverter converts the result set to a value of type R
     * @param errorHandler       handles errors as result of executing the query or converting the result set
     * @param <R>                the type result of the query
     * @return the query result  the result of the query
     */
    public static <R> R executeQuery(Connection connection, SqlFunction sqlFunction,
                                     SqlResultConverter<R> sqlResultConverter,
                                     Function<SQLException, RuntimeException> errorHandler) {
        return executeQuery(connection, sqlFunction, sqlResultConverter, errorHandler, true);
    }

    /**
     * Execute the query given by the {@code sqlFunction}. The {@link ResultSet} returned when the query is executed
     * will be converted using the given {@code sqlResultConverter}. Any errors will be handled by the given {@code
     * errorHandler}.
     *
     * @param connection         connection to the underlying database that should be used for the query
     * @param sqlFunction        the function that returns a {@link PreparedStatement} to execute the query against
     * @param sqlResultConverter converts the result set to a value of type R
     * @param errorHandler       handles errors as result of executing the query or converting the result set
     * @param closeConnection    whether provided {@code connection} should be closed or not
     * @param <R>                the type result of the query
     * @return the query result  the result of the query
     */
{'fraction_non_alphanumeric': 0.06819250666248629, 'fraction_numerical': 0.004389402727700266, 'mean_word_length': 3.0456563094483196, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3228709', 'n_tokens_mistral': 1561, 'n_tokens_neox': 1512, 'n_words': 729}