<commit_before>package com.codenvy.ide.ext.datasource.server;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.codenvy.dto.server.DtoFactory;
import com.codenvy.ide.ext.datasource.shared.MultipleRequestExecutionMode;
import com.codenvy.ide.ext.datasource.shared.RequestParameterDTO;
import com.codenvy.ide.ext.datasource.shared.exception.DatabaseDefinitionException;
import com.codenvy.ide.ext.datasource.shared.request.ExecutionErrorResultDTO;
import com.codenvy.ide.ext.datasource.shared.request.RequestResultDTO;
import com.codenvy.ide.ext.datasource.shared.request.RequestResultGroupDTO;
import com.codenvy.ide.ext.datasource.shared.request.SelectResultDTO;
import com.codenvy.ide.ext.datasource.shared.request.SqlExecutionError;
import com.codenvy.ide.ext.datasource.shared.request.UpdateResultDTO;
import com.google.common.base.Splitter;

public class SqlRequestService {

    private static final Logger                      LOG                   = LoggerFactory.getLogger(SqlRequestService.class);

    private static final String                      SQL_REQUEST_DELIMITER = ";";
    private static final Splitter                    SQL_REQUEST_SPLITTER  = Splitter.on(SQL_REQUEST_DELIMITER)
                                                                                     .omitEmptyStrings()
                                                                                     .trimResults();

    public static final MultipleRequestExecutionMode DEFAULT_MODE          = MultipleRequestExecutionMode.ONE_BY_ONE;

    public RequestResultGroupDTO executeSqlRequest(final RequestParameterDTO requestParameter,
                                                   final Connection connection,
                                                   final MultipleRequestExecutionMode mode) throws SQLException,
                                                                                           DatabaseDefinitionException {

        LOG.info("Execution request ; parameter : {}", requestParameter);
        final String agglutinatedRequests = requestParameter.getSqlRequest();
        Iterable<String> requests = SQL_REQUEST_SPLITTER.split(agglutinatedRequests);

        // prepare result dto
        final RequestResultGroupDTO resultGroup = DtoFactory.getInstance().createDto(RequestResultGroupDTO.class);
        final List<RequestResultDTO> resultList = new ArrayList<>();
        resultGroup.setResults(resultList);

        boolean terminate = false;
        for (final String request : requests) {
            try (final Statement statement = connection.createStatement();) {
                statement.setMaxRows(requestParameter.getResultLimit());
                resultList.addAll(processSingleRequest(request, statement));
            } catch (final SQLException e) {
                final ExecutionErrorResultDTO errorDto = DtoFactory.getInstance().createDto(ExecutionErrorResultDTO.class);
                final SqlExecutionError sqlError = DtoFactory.getInstance().createDto(SqlExecutionError.class);
                errorDto.setSqlExecutionError(sqlError);
                errorDto.setOriginRequest(request);
                sqlError.withErrorCode(e.getErrorCode())
                        .withErrorMessage(e.getMessage());
                resultList.add(errorDto);

                switch (mode) {
                    case STOP_AT_FIRST_ERROR:
                        terminate = true;<commit_msg>Add a TODO comment to allow semicolon inside comments and string litterals<commit_after>package com.codenvy.ide.ext.datasource.server;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.codenvy.dto.server.DtoFactory;
import com.codenvy.ide.ext.datasource.shared.MultipleRequestExecutionMode;
import com.codenvy.ide.ext.datasource.shared.RequestParameterDTO;
import com.codenvy.ide.ext.datasource.shared.exception.DatabaseDefinitionException;
import com.codenvy.ide.ext.datasource.shared.request.ExecutionErrorResultDTO;
import com.codenvy.ide.ext.datasource.shared.request.RequestResultDTO;
import com.codenvy.ide.ext.datasource.shared.request.RequestResultGroupDTO;
import com.codenvy.ide.ext.datasource.shared.request.SelectResultDTO;
import com.codenvy.ide.ext.datasource.shared.request.SqlExecutionError;
import com.codenvy.ide.ext.datasource.shared.request.UpdateResultDTO;
import com.google.common.base.Splitter;

public class SqlRequestService {

    private static final Logger                      LOG                   = LoggerFactory.getLogger(SqlRequestService.class);

    private static final String                      SQL_REQUEST_DELIMITER = ";";
    private static final Splitter                    SQL_REQUEST_SPLITTER  = Splitter.on(SQL_REQUEST_DELIMITER)
                                                                                     .omitEmptyStrings()
                                                                                     .trimResults();

    public static final MultipleRequestExecutionMode DEFAULT_MODE          = MultipleRequestExecutionMode.ONE_BY_ONE;

    public RequestResultGroupDTO executeSqlRequest(final RequestParameterDTO requestParameter,
                                                   final Connection connection,
                                                   final MultipleRequestExecutionMode mode) throws SQLException,
                                                                                           DatabaseDefinitionException {

        LOG.info("Execution request ; parameter : {}", requestParameter);
        final String agglutinatedRequests = requestParameter.getSqlRequest();
        Iterable<String> requests = SQL_REQUEST_SPLITTER.split(agglutinatedRequests); // TODO allow ; inside /* ... */ or '...'

        // prepare result dto
        final RequestResultGroupDTO resultGroup = DtoFactory.getInstance().createDto(RequestResultGroupDTO.class);
        final List<RequestResultDTO> resultList = new ArrayList<>();
        resultGroup.setResults(resultList);

        boolean terminate = false;
        for (final String request : requests) {
            try (final Statement statement = connection.createStatement();) {
                statement.setMaxRows(requestParameter.getResultLimit());
                resultList.addAll(processSingleRequest(request, statement));
            } catch (final SQLException e) {
                final ExecutionErrorResultDTO errorDto = DtoFactory.getInstance().createDto(ExecutionErrorResultDTO.class);
                final SqlExecutionError sqlError = DtoFactory.getInstance().createDto(SqlExecutionError.class);
                errorDto.setSqlExecutionError(sqlError);
                errorDto.setOriginRequest(request);
                sqlError.withErrorCode(e.getErrorCode())
                        .withErrorMessage(e.getMessage());
                resultList.add(errorDto);

                switch (mode) {
                    case STOP_AT_FIRST_ERROR:
                        terminate = true;
{'fraction_non_alphanumeric': 0.07640297498309669, 'fraction_numerical': 0.0005409060175794456, 'mean_word_length': 2.3496376811594204, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5457224', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1728, 'n_words': 308}