All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.simpleflatmapper.jdbc.impl.MultiRowsBatchInsertCrud Maven / Gradle / Ivy

package org.simpleflatmapper.jdbc.impl;

import org.simpleflatmapper.jdbc.Crud;
import org.simpleflatmapper.jdbc.SelectQuery;
import org.simpleflatmapper.util.CheckedConsumer;

import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collection;

public final class MultiRowsBatchInsertCrud implements Crud {
    private final BatchQueryExecutor batchInsertQueryExecutor;
    private final BatchQueryExecutor batchUpsertQueryExecutor;

    private final DefaultCrud delegate;

    public MultiRowsBatchInsertCrud(DefaultCrud delegate,
                                    BatchQueryExecutor batchInsertQueryPreparer,
                                    BatchQueryExecutor batchUpsertQueryExecutor) {
        this.delegate = delegate;
        this.batchInsertQueryExecutor = batchInsertQueryPreparer;
        this.batchUpsertQueryExecutor = batchUpsertQueryExecutor;
    }

    @Override
    public void create(Connection connection, T value) throws SQLException {
        delegate.create(connection, value);
    }

    @Override
    public void create(Connection connection, Collection values) throws SQLException {
        create(connection, values, null);
    }

    @Override
    public > RH create(Connection connection, T value, RH keyConsumer) throws SQLException {
        return delegate.create(connection, value, keyConsumer);
    }

    @Override
    public > RH create(Connection connection, Collection values, final RH keyConsumer) throws SQLException {
        batchInsertQueryExecutor.insert(connection, values, new CheckedConsumer() {
            @Override
            public void accept(PreparedStatement preparedStatement) throws Exception {
                if (delegate.hasGeneratedKeys && keyConsumer != null) {
                    delegate.handleGeneratedKeys(keyConsumer, preparedStatement);
                }
            }
        });
        return keyConsumer;
    }

    @Override
    public T read(Connection connection, K key) throws SQLException {
        return delegate.read(connection, key);
    }

    @Override
    public > RH read(Connection connection, Collection keys, RH consumer) throws SQLException {
        return delegate.read(connection, keys, consumer);
    }

    @Override
    public void update(Connection connection, T value) throws SQLException {
        delegate.update(connection, value);
    }

    @Override
    public void update(Connection connection, Collection values) throws SQLException {
        delegate.update(connection, values);
    }

    @Override
    public void delete(Connection connection, K key) throws SQLException {
        delegate.delete(connection, key);
    }

    @Override
    public void delete(Connection connection, Collection keys) throws SQLException {
        delegate.delete(connection, keys);
    }

    @Override
    public void createOrUpdate(Connection connection, T value) throws SQLException {
        delegate.createOrUpdate(connection, value);
    }

    @Override
    public void createOrUpdate(Connection connection, Collection values) throws SQLException {
        createOrUpdate(connection, values, null);
    }

    @Override
    public > RH  createOrUpdate(Connection connection, T value, RH keyConsumer) throws SQLException {
        return delegate.createOrUpdate(connection, value, keyConsumer);
    }

    @Override
    public > RH  createOrUpdate(Connection connection, Collection values, final RH keyConsumer) throws SQLException {
        batchUpsertQueryExecutor.insert(connection, values, new CheckedConsumer() {
            @Override
            public void accept(PreparedStatement preparedStatement) throws Exception {
                if (delegate.hasGeneratedKeys && keyConsumer != null) {
                    delegate.handleGeneratedKeys(keyConsumer, preparedStatement);
                }
            }
        });
        return keyConsumer;
    }

    @Override
    public 

SelectQuery where(String whereClause, Type paramClass) { return delegate.where(whereClause, paramClass); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy