Search in sources :

Example 1 with ParameterPlaceholder

use of org.davidmoten.rx.jdbc.callable.internal.ParameterPlaceholder in project rxjava2-jdbc by davidmoten.

the class Call method execute.

private static List<PlaceAndType> execute(NamedCallableStatement stmt, List<Object> parameters, List<ParameterPlaceholder> parameterPlaceholders, int outCount, CallableStatement st) throws SQLException {
    Util.incrementCounter(st.getConnection());
    setParameters(st, parameters, parameterPlaceholders, stmt.names);
    int initialSize = outCount == Integer.MAX_VALUE ? 16 : outCount;
    List<PlaceAndType> outs = new ArrayList<PlaceAndType>(initialSize);
    for (int j = 0; j < parameterPlaceholders.size(); j++) {
        ParameterPlaceholder p = parameterPlaceholders.get(j);
        if (p instanceof OutParameterPlaceholder) {
            outs.add(new PlaceAndType(j + 1, ((OutParameterPlaceholder) p).type()));
            if (outs.size() == outCount) {
                break;
            }
        }
    }
    st.execute();
    return outs;
}
Also used : OutParameterPlaceholder(org.davidmoten.rx.jdbc.callable.internal.OutParameterPlaceholder) ArrayList(java.util.ArrayList) OutParameterPlaceholder(org.davidmoten.rx.jdbc.callable.internal.OutParameterPlaceholder) ParameterPlaceholder(org.davidmoten.rx.jdbc.callable.internal.ParameterPlaceholder) InParameterPlaceholder(org.davidmoten.rx.jdbc.callable.internal.InParameterPlaceholder)

Example 2 with ParameterPlaceholder

use of org.davidmoten.rx.jdbc.callable.internal.ParameterPlaceholder in project rxjava2-jdbc by davidmoten.

the class Call method createWithNResultSets.

private static Flowable<Notification<CallableResultSetN>> createWithNResultSets(Connection con, String sql, Flowable<List<Object>> parameterGroups, List<ParameterPlaceholder> parameterPlaceholders, List<Function<? super ResultSet, ?>> functions, int fetchSize) {
    Callable<NamedCallableStatement> resourceFactory = () -> Util.prepareCall(con, sql, parameterPlaceholders);
    final // 
    Function<NamedCallableStatement, Flowable<Notification<CallableResultSetN>>> flowableFactory = stmt -> // 
    parameterGroups.flatMap(parameters -> {
        List<Object> outputValues = executeAndReturnOutputValues(parameterPlaceholders, stmt, parameters);
        List<Flowable<?>> flowables = Lists.newArrayList();
        int i = 0;
        do {
            Function<? super ResultSet, ?> f = functions.get(i);
            flowables.add(createFlowable(stmt, f));
            i++;
        } while (stmt.stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT));
        return Single.just(new CallableResultSetN(outputValues, flowables)).toFlowable();
    }).materialize().doOnComplete(// 
    () -> Util.commit(stmt.stmt)).doOnError(e -> Util.rollback(stmt.stmt));
    Consumer<NamedCallableStatement> disposer = Util::closeCallableStatementAndConnection;
    return Flowable.using(resourceFactory, flowableFactory, disposer, true);
}
Also used : BiFunction(io.reactivex.functions.BiFunction) Connection(java.sql.Connection) Tuple2(org.davidmoten.rx.jdbc.tuple.Tuple2) Tuple3(org.davidmoten.rx.jdbc.tuple.Tuple3) BiConsumer(io.reactivex.functions.BiConsumer) Tuple4(org.davidmoten.rx.jdbc.tuple.Tuple4) LoggerFactory(org.slf4j.LoggerFactory) Lists(com.github.davidmoten.guavamini.Lists) Callable(java.util.concurrent.Callable) Single(io.reactivex.Single) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) Flowable(io.reactivex.Flowable) ResultSet(java.sql.ResultSet) CallableResultSet3(org.davidmoten.rx.jdbc.callable.CallableResultSet3) CallableResultSet4(org.davidmoten.rx.jdbc.callable.CallableResultSet4) CallableResultSet1(org.davidmoten.rx.jdbc.callable.CallableResultSet1) CallableResultSet2(org.davidmoten.rx.jdbc.callable.CallableResultSet2) Notification(io.reactivex.Notification) CallableResultSetN(org.davidmoten.rx.jdbc.callable.CallableResultSetN) Logger(org.slf4j.Logger) TupleN(org.davidmoten.rx.jdbc.tuple.TupleN) PreparedStatement(java.sql.PreparedStatement) Consumer(io.reactivex.functions.Consumer) Emitter(io.reactivex.Emitter) OutParameterPlaceholder(org.davidmoten.rx.jdbc.callable.internal.OutParameterPlaceholder) List(java.util.List) ParameterPlaceholder(org.davidmoten.rx.jdbc.callable.internal.ParameterPlaceholder) Function(io.reactivex.functions.Function) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) InParameterPlaceholder(org.davidmoten.rx.jdbc.callable.internal.InParameterPlaceholder) BiFunction(io.reactivex.functions.BiFunction) Function(io.reactivex.functions.Function) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) CallableResultSetN(org.davidmoten.rx.jdbc.callable.CallableResultSetN) Flowable(io.reactivex.Flowable)

Example 3 with ParameterPlaceholder

use of org.davidmoten.rx.jdbc.callable.internal.ParameterPlaceholder in project rxjava2-jdbc by davidmoten.

the class Call method setParameters.

static PreparedStatement setParameters(PreparedStatement ps, List<Object> parameters, List<ParameterPlaceholder> parameterPlaceholders, List<String> names) throws SQLException {
    // TODO handle Parameter objects (named)
    if (names.isEmpty()) {
        int i = 0;
        for (int j = 0; j < parameterPlaceholders.size() && i < parameters.size(); j++) {
            ParameterPlaceholder p = parameterPlaceholders.get(j);
            if (p instanceof InParameterPlaceholder) {
                Util.setParameter(ps, j + 1, parameters.get(i));
                i++;
            }
        }
    } else {
        // TODO
        throw new RuntimeException("named paramters not implemented yet for CallableStatement yet");
    // Util.setNamedParameters(ps, params, names);
    }
    return ps;
}
Also used : InParameterPlaceholder(org.davidmoten.rx.jdbc.callable.internal.InParameterPlaceholder) OutParameterPlaceholder(org.davidmoten.rx.jdbc.callable.internal.OutParameterPlaceholder) ParameterPlaceholder(org.davidmoten.rx.jdbc.callable.internal.ParameterPlaceholder) InParameterPlaceholder(org.davidmoten.rx.jdbc.callable.internal.InParameterPlaceholder)

Aggregations

InParameterPlaceholder (org.davidmoten.rx.jdbc.callable.internal.InParameterPlaceholder)3 OutParameterPlaceholder (org.davidmoten.rx.jdbc.callable.internal.OutParameterPlaceholder)3 ParameterPlaceholder (org.davidmoten.rx.jdbc.callable.internal.ParameterPlaceholder)3 ArrayList (java.util.ArrayList)2 Lists (com.github.davidmoten.guavamini.Lists)1 Emitter (io.reactivex.Emitter)1 Flowable (io.reactivex.Flowable)1 Notification (io.reactivex.Notification)1 Single (io.reactivex.Single)1 BiConsumer (io.reactivex.functions.BiConsumer)1 BiFunction (io.reactivex.functions.BiFunction)1 Consumer (io.reactivex.functions.Consumer)1 Function (io.reactivex.functions.Function)1 CallableStatement (java.sql.CallableStatement)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 List (java.util.List)1