package io.github.jklingsporn.vertx.jooq.classic.reactivepg;

import io.github.jklingsporn.vertx.jooq.shared.internal.QueryExecutor;
import io.vertx.core.Future;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.SqlClient;
import io.vertx.sqlclient.Transaction;
import org.jooq.*;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Created by jensklingsporn on 01.03.18.
 */
public class ReactiveClassicQueryExecutor<R extends UpdatableRecord<R>,P,T> extends ReactiveClassicGenericQueryExecutor implements QueryExecutor<R,T,Future<List<P>>,Future<P>,Future<Integer>,Future<T>>{

    private final Function<Row,P> pojoMapper;
    private final boolean isPostgres;

    public ReactiveClassicQueryExecutor(Configuration configuration, SqlClient delegate, Function<Row, P> pojoMapper) {
        super(configuration, delegate);
        this.pojoMapper = pojoMapper;
        this.isPostgres = configuration.dialect().family().equals(SQLDialect.POSTGRES);
    }

    @Override
    public Future<List<P>> findMany(Function<DSLContext, ? extends ResultQuery<R>> queryFunction) {
        return findManyRow(queryFunction).map(rows->rows.stream().map(pojoMapper).collect(Collectors.toList()));
    }

    @Override
    public Future<P> findOne(Function<DSLContext, ? extends ResultQuery<R>> queryFunction) {
        return findOneRow(queryFunction).map(val -> val == null?null:pojoMapper.apply(val));
    }

    @Override
    public Future<T> insertReturning(Function<DSLContext, ? extends InsertResultStep<R>> queryFunction, Function<Object, T> keyMapper) {
        /*
         * Support insert returning in mysql using lastinsertid: https://github.com/jklingsporn/vertx-jooq/issues/149
         */
        return isPostgres ?
                executeAny(queryFunction)
                .map(rows -> rows.iterator().next())
                .map(keyMapper::apply)
                : executeAny(queryFunction)
                .map(keyMapper::apply);
    }

    @Override
    @SuppressWarnings("unchecked")
    public Future<ReactiveClassicQueryExecutor<R,P,T>> beginTransaction() {
        return (Future<ReactiveClassicQueryExecutor<R, P, T>>) super.beginTransaction();
    }

    @Override
    protected Function<Transaction, ReactiveClassicQueryExecutor<R,P,T>> newInstance() {
        return pgTransaction -> new ReactiveClassicQueryExecutor<>(configuration(), pgTransaction, pojoMapper);
    }

}

@@ -305,16 +305,54 @@
 l.List;%0A
+import java.util.function.BiFunction;%0A
 import j
@@ -371,32 +371,32 @@
 ction.Function;%0A
-
 import java.util
@@ -738,26 +738,126 @@
 nal 
-boolean isPostgres
+BiFunction%3CFunction%3CDSLContext, ? extends InsertResultStep%3CR%3E%3E, Function%3CObject, T%3E,Future%3CT%3E%3E insertReturningDelegate
 ;%0A%0A 
@@ -1065,86 +1065,598 @@
     
-this.isPostgres = configuration.dialect().family().equals(SQLDialect.POSTGRES)
+/*%0A         * Support insert returning in mysql using lastinsertid: https://github.com/jklingsporn/vertx-jooq/issues/149%0A         */%0A        this.insertReturningDelegate =%0A                configuration.dialect().family().equals(SQLDialect.POSTGRES)%0A                        ? (queryFunction,keyMapper) -%3E executeAny(queryFunction)%0A                                .map(rows -%3E rows.iterator().next())%0A                                .map(keyMapper::apply)%0A                        : (queryFunction,keyMapper) -%3E executeAny(queryFunction)%0A                        .map(keyMapper::apply)%0A        
 ;%0A  
@@ -2264,368 +2264,59 @@
     
-/*%0A         * Support insert returning in mysql using lastinsertid: https://github.com/jklingsporn/vertx-jooq/issues/149%0A         */%0A        return isPostgres ?%0A                executeAny(queryFunction)%0A                .map(rows -%3E rows.iterator().next())%0A                .map(keyMapper::apply)%0A                : executeAny(queryFunction)%0A                .map(
+return insertReturningDelegate.apply(queryFunction,
 keyM
@@ -2320,23 +2320,16 @@
 eyMapper
-::apply
 );%0A    %7D

remove redundant database dialect check