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

com.jporm.rx.connection.datasource.DataSourceRxConnection Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright 2015 Francesco Cina'
 *
 * 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 com.jporm.rx.connection.datasource;

import java.util.Collection;
import java.util.concurrent.Executor;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

import com.jporm.commons.core.function.IntBiFunction;
import com.jporm.commons.core.transaction.TransactionIsolation;
import com.jporm.rm.connection.datasource.DataSourceConnection;
import com.jporm.rx.connection.RxConnection;
import com.jporm.rx.util.Futures;
import com.jporm.types.io.BatchPreparedStatementSetter;
import com.jporm.types.io.GeneratedKeyReader;
import com.jporm.types.io.ResultEntry;
import com.jporm.types.io.ResultSet;
import com.jporm.types.io.Statement;

import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.Single;

public class DataSourceRxConnection implements RxConnection {

    private final DataSourceConnection rmConnection;
    private final Executor executor;

    public DataSourceRxConnection(final DataSourceConnection rmConnection, Executor executor) {
        this.rmConnection = rmConnection;
        this.executor = executor;
    }

    @Override
    public Single batchUpdate(final Collection sqls, Function sqlPreProcessor) {
        return Futures.toSingle(executor, () -> {
            return rmConnection.batchUpdate(sqls, sqlPreProcessor);
        });

    }

    @Override
    public Single batchUpdate(final String sql, final BatchPreparedStatementSetter psc) {
        return Futures.toSingle(executor, () -> {
            return rmConnection.batchUpdate(sql, psc);
        });
    }

    @Override
    public Single batchUpdate(final String sql, final Collection> args) {
        return Futures.toSingle(executor, () -> {
            return rmConnection.batchUpdate(sql, args);
        });
    }

    @Override
    public Completable execute(final String sql) {
        return Futures.toCompletable(executor, () -> {
            rmConnection.execute(sql);
        });
    }

    @Override
    public  Observable query(final String sql, final Consumer pss, final BiConsumer, ResultSet> rse) {
        return Observable. create(onSubscribe -> {
            executor.execute(() -> {
                try {
                    rmConnection.query(sql, pss, rs -> {
                        rse.accept(onSubscribe, rs);
                        return null;
                    });
                } catch (Throwable e) {
                    onSubscribe.onError(e);
                }
            });
        });
    }

    @Override
    public  Observable query(final String sql, final Consumer pss, final IntBiFunction rse) {
        return Observable. create(onSubscribe -> {
            executor.execute(() -> {
                try {
                    rmConnection.query(sql, pss, rs -> {
                        int count = 0;
                        while(!rmConnection.isClosed() && rs.hasNext()) {
                            onSubscribe.onNext(rse.apply(rs.next(), count++));
                        }
                        return null;
                    });
                    onSubscribe.onComplete();
                } catch (Throwable e) {
                    onSubscribe.onError(e);
                }
            });
        });
    }

    @Override
    public void setReadOnly(final boolean readOnly) {
        rmConnection.setReadOnly(readOnly);
    }

    @Override
    public void setTimeout(final int timeout) {
        rmConnection.setTimeout(timeout);
    }

    @Override
    public void setTransactionIsolation(final TransactionIsolation isolation) {
        rmConnection.setTransactionIsolation(isolation);
    }

    @Override
    public  Single update(final String sql, final GeneratedKeyReader generatedKeyReader, final Consumer pss) {
        return Futures.toSingle(executor, () -> {
            return rmConnection.update(sql, generatedKeyReader, pss);
        });
    }

    @Override
    public Single update(String sql, Consumer pss) {
        return Futures.toSingle(executor, () -> {
            return rmConnection.update(sql, pss);
        });
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy