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

com.firefly.reactive.adapter.db.ReactiveSQLConnectionAdapter Maven / Gradle / Ivy

There is a newer version: 5.0.0-dev6
Show newest version
package com.firefly.reactive.adapter.db;

import com.firefly.db.SQLConnection;
import com.firefly.db.SQLResultSet;
import com.firefly.db.TransactionIsolation;
import com.firefly.utils.concurrent.Promise;
import com.firefly.utils.function.Func1;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

/**
 * @author Pengtao Qiu
 */
public class ReactiveSQLConnectionAdapter implements ReactiveSQLConnection {

    private final SQLConnection sqlConnection;

    public ReactiveSQLConnectionAdapter(SQLConnection sqlConnection) {
        this.sqlConnection = sqlConnection;
    }

    @Override
    public  Mono queryForSingleColumn(String sql, Object... params) {
        return Mono.fromCompletionStage(sqlConnection.queryForSingleColumn(sql, params));
    }

    @Override
    public  Mono namedQueryForSingleColumn(String sql, Map paramMap) {
        return Mono.fromCompletionStage(sqlConnection.namedQueryForSingleColumn(sql, paramMap));
    }

    @Override
    public  Mono namedQueryForSingleColumn(String sql, Object paramObject) {
        return Mono.fromCompletionStage(sqlConnection.namedQueryForSingleColumn(sql, paramObject));
    }

    @Override
    public  Mono queryForObject(String sql, Class clazz, Object... params) {
        return Mono.fromCompletionStage(sqlConnection.queryForObject(sql, clazz, params));
    }

    @Override
    public  Mono namedQueryForObject(String sql, Class clazz, Map paramMap) {
        return Mono.fromCompletionStage(sqlConnection.namedQueryForObject(sql, clazz, paramMap));
    }

    @Override
    public  Mono namedQueryForObject(String sql, Class clazz, Object paramObject) {
        return Mono.fromCompletionStage(sqlConnection.namedQueryForObject(sql, clazz, paramObject));
    }

    @Override
    public  Mono queryById(Object id, Class clazz) {
        return Mono.fromCompletionStage(sqlConnection.queryById(id, clazz));
    }

    @Override
    public  Mono> queryForBeanMap(String sql, Class valueClass, Object... params) {
        return Mono.fromCompletionStage(sqlConnection.queryForBeanMap(sql, valueClass, params));
    }

    @Override
    public  Mono> namedQueryForBeanMap(String sql, Class valueClass, Map paramMap) {
        return Mono.fromCompletionStage(sqlConnection.namedQueryForBeanMap(sql, valueClass, paramMap));
    }

    @Override
    public  Mono> namedQueryForBeanMap(String sql, Class valueClass, Object paramObject) {
        return Mono.fromCompletionStage(sqlConnection.namedQueryForBeanMap(sql, valueClass, paramObject));
    }

    @Override
    public  Mono> queryForList(String sql, Class clazz, Object... params) {
        return Mono.fromCompletionStage(sqlConnection.queryForList(sql, clazz, params));
    }

    @Override
    public  Mono> namedQueryForList(String sql, Class clazz, Map paramMap) {
        return Mono.fromCompletionStage(sqlConnection.namedQueryForList(sql, clazz, paramMap));
    }

    @Override
    public  Mono> namedQueryForList(String sql, Class clazz, Object paramObject) {
        return Mono.fromCompletionStage(sqlConnection.namedQueryForList(sql, clazz, paramObject));
    }

    @Override
    public  Mono query(String sql, Func1 handler, Object... params) {
        return Mono.fromCompletionStage(sqlConnection.query(sql, handler, params));
    }

    @Override
    public  Mono namedQuery(String sql, Func1 handler, Map paramMap) {
        return Mono.fromCompletionStage(sqlConnection.namedQuery(sql, handler, paramMap));
    }

    @Override
    public  Mono namedQuery(String sql, Func1 handler, Object paramObject) {
        return Mono.fromCompletionStage(sqlConnection.namedQuery(sql, handler, paramObject));
    }

    @Override
    public Mono update(String sql, Object... params) {
        return Mono.fromCompletionStage(sqlConnection.update(sql, params));
    }

    @Override
    public Mono namedUpdate(String sql, Map paramMap) {
        return Mono.fromCompletionStage(sqlConnection.namedUpdate(sql, paramMap));
    }

    @Override
    public Mono namedUpdate(String sql, Object paramObject) {
        return Mono.fromCompletionStage(sqlConnection.namedUpdate(sql, paramObject));
    }

    @Override
    public  Mono updateObject(T object) {
        return Mono.fromCompletionStage(sqlConnection.updateObject(object));
    }

    @Override
    public  Mono insert(String sql, Object... params) {
        return Mono.fromCompletionStage(sqlConnection.insert(sql, params));
    }

    @Override
    public  Mono namedInsert(String sql, Map paramMap) {
        return Mono.fromCompletionStage(sqlConnection.namedInsert(sql, paramMap));
    }

    @Override
    public  Mono namedInsert(String sql, Object paramObject) {
        return Mono.fromCompletionStage(sqlConnection.namedInsert(sql, paramObject));
    }

    @Override
    public  Mono insertObject(T object) {
        return Mono.fromCompletionStage(sqlConnection.insertObject(object));
    }

    @Override
    public  Mono insertObjectBatch(List list, Class clazz, Func1 handler) {
        return Mono.fromCompletionStage(sqlConnection.insertObjectBatch(list, clazz, handler));
    }

    @Override
    public  Mono> insertObjectBatch(List list, Class clazz) {
        return Mono.fromCompletionStage(sqlConnection.insertObjectBatch(list, clazz));
    }

    @Override
    public  Mono insertBatch(String sql, Object[][] params, Func1 handler) {
        return Mono.fromCompletionStage(sqlConnection.insertBatch(sql, params, handler));
    }

    @Override
    public  Mono deleteById(Object id, Class clazz) {
        return Mono.fromCompletionStage(sqlConnection.deleteById(id, clazz));
    }

    @Override
    public Mono executeBatch(String sql, Object[][] params) {
        return Mono.fromCompletionStage(sqlConnection.executeBatch(sql, params));
    }

    @Override
    public Mono setTransactionIsolation(TransactionIsolation transactionIsolation) {
        return Mono.create(sink -> sqlConnection.setTransactionIsolation(transactionIsolation).thenAccept(aVoid -> sink.success(true)).exceptionally(x -> {
            sink.error(x);
            return null;
        }));
    }

    @Override
    public Mono setAutoCommit(boolean autoCommit) {
        return Mono.create(sink -> sqlConnection.setAutoCommit(autoCommit).thenAccept(aVoid -> sink.success(true)).exceptionally(x -> {
            sink.error(x);
            return null;
        }));
    }

    @Override
    public boolean getAutoCommit() {
        return sqlConnection.getAutoCommit();
    }

    @Override
    public Mono rollback() {
        return Mono.create(sink -> sqlConnection.rollback().thenAccept(aVoid -> sink.success(true)).exceptionally(x -> {
            sink.error(x);
            return null;
        }));
    }

    @Override
    public Mono commit() {
        return Mono.create(sink -> sqlConnection.commit().thenAccept(aVoid -> sink.success(true)).exceptionally(x -> {
            sink.error(x);
            return null;
        }));
    }

    @Override
    public Mono close() {
        return Mono.create(sink -> sqlConnection.close().thenAccept(aVoid -> sink.success(true)).exceptionally(x -> {
            sink.error(x);
            return null;
        }));
    }

    @Override
    public Mono commitAndClose() {
        return Mono.create(sink -> sqlConnection.commitAndClose().thenAccept(aVoid -> sink.success(true)).exceptionally(x -> {
            sink.error(x);
            return null;
        }));
    }

    @Override
    public Mono rollbackAndClose() {
        return Mono.create(sink -> sqlConnection.rollbackAndClose().thenAccept(aVoid -> sink.success(true)).exceptionally(x -> {
            sink.error(x);
            return null;
        }));
    }

    @Override
    public  Mono inTransaction(Func1> func1) {
        return Mono.fromCompletionStage(sqlConnection.inTransaction(conn -> {
            Promise.Completable completable = new Promise.Completable<>();
            func1.call(this)
                 .subscribe(completable::succeeded, completable::failed);
            return completable;
        }));
    }

    @Override
    public Mono beginTransaction() {
        return Mono.fromCompletionStage(sqlConnection.beginTransaction());
    }

    @Override
    public Mono rollbackAndEndTransaction() {
        return Mono.create(sink -> sqlConnection.rollbackAndEndTransaction().thenAccept(aVoid -> sink.success(true)).exceptionally(x -> {
            sink.error(x);
            return null;
        }));
    }

    @Override
    public Mono commitAndEndTransaction() {
        return Mono.create(sink -> sqlConnection.commitAndEndTransaction().thenAccept(aVoid -> sink.success(true)).exceptionally(x -> {
            sink.error(x);
            return null;
        }));
    }

    @Override
    public SQLConnection getSQLConnection() {
        return sqlConnection;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy