<commit_before>/*
 *     This file is part of ToroDB.
 *
 *     ToroDB is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Affero General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     ToroDB is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Affero General Public License for more details.
 *
 *     You should have received a copy of the GNU Affero General Public License
 *     along with ToroDB. If not, see <http://www.gnu.org/licenses/>.
 *
 *     Copyright (c) 2014, 8Kdata Technology
 *     
 */

package com.torodb.backend;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.jooq.Converter;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.impl.DSL;

import com.torodb.backend.ErrorHandler.Context;
import com.torodb.backend.converters.jooq.DataTypeForKV;
import com.torodb.backend.converters.jooq.KVValueConverter;
import com.torodb.backend.converters.sql.SqlBinding;
import com.torodb.core.transaction.metainf.FieldType;
import com.torodb.kvdocument.values.KVValue;

@Singleton
public class SqlHelper {

    private final DataTypeProvider dataTypeProvider;
    private final ErrorHandler errorHandler;
    
    @Inject
    public SqlHelper(DataTypeProvider dataTypeProvider, ErrorHandler errorHandler) {
        super();
        this.dataTypeProvider = dataTypeProvider;
        this.errorHandler = errorHandler;
    }

    public void executeStatement(DSLContext dsl, String statement, Context context){
        Connection c = dsl.configuration().connectionProvider().acquire();
        try (PreparedStatement ps = c.prepareStatement(statement)) {
            ps.execute();
        } catch (SQLException ex) {
            throw errorHandler.handleException(context, ex);
        } finally {
            dsl.configuration().connectionProvider().release(c);
        }       
    }

    public Result<Record> executeStatementWithResult(DSLContext dsl, String statement, Context context){
        Connection c = dsl.configuration().connectionProvider().acquire();
        try (PreparedStatement ps = c.prepareStatement(statement)) {
            ResultSet resultSet = ps.executeQuery();
            return dsl.fetch(resultSet);
        } catch (SQLException ex) {
            throw errorHandler.handleException(context, ex);
        } finally {
            dsl.configuration().connectionProvider().release(c);
        }
    }
    
    public void executeUpdate(DSLContext dsl, String statement, Context context){
        Connection c = dsl.configuration().connectionProvider().acquire();
        try (PreparedStatement ps = c.prepareStatement(statement)) {
            ps.executeUpdate();
        } catch (SQLException ex) {
            throw errorHandler.handleException(context, ex);
        } finally {
            dsl.configuration().connectionProvider().release(c);
        }       
    }
    
    public void executeUpdate(Connection c, String statement, Context context){
        try (PreparedStatement ps = c.prepareStatement(statement)) {
            ps.executeUpdate();
        } catch (SQLException ex) {
            throw errorHandler.handleException(context, ex);
        }       
    }

    public String renderVal(String value) {
        return dsl().render(DSL.val(value));
    }

    public DSLContext dsl() {
        return DSL.using(dataTypeProvider.getDialect());
    }
    
    @SuppressWarnings({ "rawtypes" })
    public Object getResultSetValue(FieldType fieldType, ResultSet resultSet, int index) throws SQLException {
        DataTypeForKV dataType = dataTypeProvider
                .getDataType(fieldType);
        KVValueConverter valueConverter = dataType
                .getKVValueConverter();
        SqlBinding sqlBinding = valueConverter
                .getSqlBinding();
        return sqlBinding.get(resultSet, index);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void setPreparedStatementNullableValue(PreparedStatement preparedStatement, int parameterIndex,
            FieldType fieldType, KVValue<?> value) throws SQLException {
        DataTypeForKV dataType = dataTypeProvider
                .getDataType(fieldType);
        Converter converter = dataType.getConverter();
        KVValueConverter valueConverter = dataType
                .getKVValueConverter();
        SqlBinding sqlBinding = valueConverter
                .getSqlBinding();
        if (value != null) {
            sqlBinding.set(preparedStatement, parameterIndex, converter.to(value));
        } else {
            preparedStatement.setNull(parameterIndex, dataType.getSQLType());
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void setPreparedStatementValue(PreparedStatement preparedStatement, int parameterIndex,
            FieldType fieldType, KVValue<?> value) throws SQLException {
        DataTypeForKV dataType = dataTypeProvider
                .getDataType(fieldType);
        Converter converter = dataType.getConverter();
        KVValueConverter valueConverter = dataType
                .getKVValueConverter();
        SqlBinding sqlBinding = valueConverter
                .getSqlBinding();
        sqlBinding.set(preparedStatement, parameterIndex, converter.to(value));
    }
    
    @SuppressWarnings({ "rawtypes" })
    public String getPlaceholder(FieldType fieldType) {
        DataTypeForKV dataType = dataTypeProvider
                .getDataType(fieldType);
        KVValueConverter valueConverter = dataType
                .getKVValueConverter();
        SqlBinding sqlBinding = valueConverter
                .getSqlBinding();
        return sqlBinding.getPlaceholder();
    }
}
<commit_msg>SqlHelper.executeUpdate must return the number of rows affected<commit_after>/*
 *     This file is part of ToroDB.
 *
 *     ToroDB is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Affero General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     ToroDB is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Affero General Public License for more details.
 *
 *     You should have received a copy of the GNU Affero General Public License
 *     along with ToroDB. If not, see <http://www.gnu.org/licenses/>.
 *
 *     Copyright (c) 2014, 8Kdata Technology
 *     
 */

package com.torodb.backend;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.jooq.Converter;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.impl.DSL;

import com.torodb.backend.ErrorHandler.Context;
import com.torodb.backend.converters.jooq.DataTypeForKV;
import com.torodb.backend.converters.jooq.KVValueConverter;
import com.torodb.backend.converters.sql.SqlBinding;
import com.torodb.core.transaction.metainf.FieldType;
import com.torodb.kvdocument.values.KVValue;

@Singleton
public class SqlHelper {

    private final DataTypeProvider dataTypeProvider;
    private final ErrorHandler errorHandler;
    
    @Inject
    public SqlHelper(DataTypeProvider dataTypeProvider, ErrorHandler errorHandler) {
        super();
        this.dataTypeProvider = dataTypeProvider;
        this.errorHandler = errorHandler;
    }

    public void executeStatement(DSLContext dsl, String statement, Context context){
        Connection c = dsl.configuration().connectionProvider().acquire();
        try (PreparedStatement ps = c.prepareStatement(statement)) {
            ps.execute();
        } catch (SQLException ex) {
            throw errorHandler.handleException(context, ex);
        } finally {
            dsl.configuration().connectionProvider().release(c);
        }       
    }

    public Result<Record> executeStatementWithResult(DSLContext dsl, String statement, Context context){
        Connection c = dsl.configuration().connectionProvider().acquire();
        try (PreparedStatement ps = c.prepareStatement(statement)) {
            ResultSet resultSet = ps.executeQuery();
            return dsl.fetch(resultSet);
        } catch (SQLException ex) {
            throw errorHandler.handleException(context, ex);
        } finally {
            dsl.configuration().connectionProvider().release(c);
        }
    }
    
    public int executeUpdate(DSLContext dsl, String statement, Context context){
        Connection c = dsl.configuration().connectionProvider().acquire();
        try (PreparedStatement ps = c.prepareStatement(statement)) {
            return ps.executeUpdate();
        } catch (SQLException ex) {
            throw errorHandler.handleException(context, ex);
        } finally {
            dsl.configuration().connectionProvider().release(c);
        }       
    }
    
    public int executeUpdate(Connection c, String statement, Context context){
        try (PreparedStatement ps = c.prepareStatement(statement)) {
            return ps.executeUpdate();
        } catch (SQLException ex) {
            throw errorHandler.handleException(context, ex);
        }       
    }

    public String renderVal(String value) {
        return dsl().render(DSL.val(value));
    }

    public DSLContext dsl() {
        return DSL.using(dataTypeProvider.getDialect());
    }
    
    @SuppressWarnings({ "rawtypes" })
    public Object getResultSetValue(FieldType fieldType, ResultSet resultSet, int index) throws SQLException {
        DataTypeForKV dataType = dataTypeProvider
                .getDataType(fieldType);
        KVValueConverter valueConverter = dataType
                .getKVValueConverter();
        SqlBinding sqlBinding = valueConverter
                .getSqlBinding();
        return sqlBinding.get(resultSet, index);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void setPreparedStatementNullableValue(PreparedStatement preparedStatement, int parameterIndex,
            FieldType fieldType, KVValue<?> value) throws SQLException {
        DataTypeForKV dataType = dataTypeProvider
                .getDataType(fieldType);
        Converter converter = dataType.getConverter();
        KVValueConverter valueConverter = dataType
                .getKVValueConverter();
        SqlBinding sqlBinding = valueConverter
                .getSqlBinding();
        if (value != null) {
            sqlBinding.set(preparedStatement, parameterIndex, converter.to(value));
        } else {
            preparedStatement.setNull(parameterIndex, dataType.getSQLType());
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void setPreparedStatementValue(PreparedStatement preparedStatement, int parameterIndex,
            FieldType fieldType, KVValue<?> value) throws SQLException {
        DataTypeForKV dataType = dataTypeProvider
                .getDataType(fieldType);
        Converter converter = dataType.getConverter();
        KVValueConverter valueConverter = dataType
                .getKVValueConverter();
        SqlBinding sqlBinding = valueConverter
                .getSqlBinding();
        sqlBinding.set(preparedStatement, parameterIndex, converter.to(value));
    }
    
    @SuppressWarnings({ "rawtypes" })
    public String getPlaceholder(FieldType fieldType) {
        DataTypeForKV dataType = dataTypeProvider
                .getDataType(fieldType);
        KVValueConverter valueConverter = dataType
                .getKVValueConverter();
        SqlBinding sqlBinding = valueConverter
                .getSqlBinding();
        return sqlBinding.getPlaceholder();
    }
}

{'fraction_non_alphanumeric': 0.08627739614000654, 'fraction_numerical': 0.0009813542688910696, 'mean_word_length': 2.8323409589470385, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6815029', 'n_tokens_mistral': 3172, 'n_tokens_neox': 3123, 'n_words': 799}