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

com.github.davidmoten.rx.jdbc.QuerySelectOperator Maven / Gradle / Ivy

There is a newer version: 0.7.19
Show newest version
package com.github.davidmoten.rx.jdbc;

import static com.github.davidmoten.rx.OperationToOperator.toOperator;

import java.sql.ResultSet;

import com.github.davidmoten.rx.jdbc.QuerySelect.Builder;

import rx.Observable;
import rx.Observable.Operator;
import rx.Subscriber;
import rx.functions.Func1;

/**
 * Operator corresponding to the QuerySelectOperation.
 * 
 * @param 
 */
final class QuerySelectOperator implements Operator {

    private final Operator operator;

    /**
     * Constructor.
     * 
     * @param builder
     * @param function
     * @param operatorType
     */
    QuerySelectOperator(final QuerySelect.Builder builder, final Func1 function,
            final OperatorType operatorType) {
        operator = toOperator(new ApplyQuerySelect(builder, function, operatorType));
    }

    @Override
    public Subscriber call(Subscriber subscriber) {
        return operator.call(subscriber);
    }

    private static class ApplyQuerySelect implements Func1, Observable> {

        private Builder builder;
        private Func1 function;
        private OperatorType operatorType;

        private ApplyQuerySelect(QuerySelect.Builder builder, Func1 function,
                OperatorType operatorType) {
            this.builder = builder;
            this.function = function;
            this.operatorType = operatorType;
        }

        @Override
        public Observable call(Observable observable) {
            if (operatorType == OperatorType.PARAMETER)
                return builder.parameters(observable).get(function);
            else if (operatorType == OperatorType.DEPENDENCY)
                // dependency
                return builder.dependsOn(observable).get(function);
            else // PARAMETER_LIST
            {
                @SuppressWarnings("unchecked")
                Observable> obs = (Observable>) observable;
                return obs.concatMap(new Func1, Observable>() {
                    @Override
                    public Observable call(Observable parameters) {
                        return builder.parameters(parameters).get(function);
                    }
                });
            }
        }

    }
}