com.github.davidmoten.rx.jdbc.QuerySelect Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava-jdbc Show documentation
Show all versions of rxjava-jdbc Show documentation
rx-java Observables for jdbc
package com.github.davidmoten.rx.jdbc;
import static com.github.davidmoten.rx.jdbc.Conditions.checkNotNull;
import static com.github.davidmoten.rx.jdbc.Queries.bufferedParameters;
import java.sql.ResultSet;
import java.util.List;
import com.github.davidmoten.rx.Functions;
import com.github.davidmoten.rx.jdbc.NamedParameters.JdbcQuery;
import com.github.davidmoten.rx.jdbc.tuple.Tuple2;
import com.github.davidmoten.rx.jdbc.tuple.Tuple3;
import com.github.davidmoten.rx.jdbc.tuple.Tuple4;
import com.github.davidmoten.rx.jdbc.tuple.Tuple5;
import com.github.davidmoten.rx.jdbc.tuple.Tuple6;
import com.github.davidmoten.rx.jdbc.tuple.Tuple7;
import com.github.davidmoten.rx.jdbc.tuple.TupleN;
import com.github.davidmoten.rx.jdbc.tuple.Tuples;
import rx.Observable;
import rx.Observable.Transformer;
import rx.functions.Func1;
/**
* A query and its executable context.
*/
final public class QuerySelect implements Query {
// Note has one ? to match the expected one parameter
static final String RETURN_GENERATED_KEYS = "RETURN_GENERATED_KEYS?";
private final Observable parameters;
private final QueryContext context;
private Observable> depends = Observable.empty();
private final JdbcQuery jdbcQuery;
private final Func1 resultSetTransform;
/**
* Constructor.
*
* @param sql
* jdbc select statement or the word RETURN_GENERATED_KEYS
* @param parameters
* if sql == RETURN_GENERATED_KEYS then the first parameter will
* be the ResultSet to be used as source
* @param depends
* @param context
* @param resultSetTransform
* @param resultSetTransform
*/
QuerySelect(String sql, Observable parameters, Observable> depends,
QueryContext context, Func1 resultSetTransform) {
checkNotNull(sql);
checkNotNull(parameters);
checkNotNull(depends);
checkNotNull(context);
checkNotNull(resultSetTransform);
this.jdbcQuery = NamedParameters.parse(sql);
this.parameters = parameters;
this.depends = depends;
this.context = context;
this.resultSetTransform = resultSetTransform;
}
@Override
public String sql() {
return jdbcQuery.sql();
}
@Override
public QueryContext context() {
return context;
}
@Override
public Observable parameters() {
return parameters;
}
@Override
public List names() {
return jdbcQuery.names();
}
@Override
public String toString() {
return "QuerySelect [sql=" + sql() + "]";
}
@Override
public Observable> depends() {
return depends;
}
Func1 resultSetTransform() {
return resultSetTransform;
}
/**
* Returns the results of running a select query with all sets of
* parameters.
*
* @return
*/
public Observable execute(ResultSetMapper extends T> function) {
return bufferedParameters(this)
// execute once per set of parameters
.concatMap(executeOnce(function));
}
/**
* Returns a {@link Func1} that itself returns the results of pushing one
* set of parameters through a select query.
*
* @param query
* @return
*/
private Func1, Observable> executeOnce(
final ResultSetMapper extends T> function) {
return new Func1, Observable>() {
@Override
public Observable call(List params) {
return executeOnce(params, function);
}
};
}
/**
* Returns an Observable of the results of pushing one set of parameters
* through a select query.
*
* @param params
* one set of parameters to be run with the query
* @return
*/
private Observable executeOnce(final List params,
ResultSetMapper extends T> function) {
return QuerySelectOnSubscribe.execute(this, params, function)
.subscribeOn(context.scheduler());
}
/**
* Builds a {@link QuerySelect}.
*/
public static final class Builder {
/**
* Builds the standard stuff.
*/
private final QueryBuilder builder;
private Integer fetchSize;
/**
* The {@link ResultSet} is transformed before use.
*/
private Func1 resultSetTransform = Functions.identity();
/**
* Constructor.
*
* @param sql
* @param db
*/
public Builder(String sql, Database db) {
builder = new QueryBuilder(sql, db);
}
/**
* Appends the given parameters to the parameter list for the query. If
* there are more parameters than required for one execution of the
* query then more than one execution of the query will occur.
*
* @param parameters
* @return this
*/
public Builder parameters(Observable parameters) {
builder.parameters(parameters);
return this;
}
/**
* Appends the given parameter values to the parameter list for the
* query. If there are more parameters than required for one execution
* of the query then more than one execution of the query will occur.
*
* @param objects
* @return this
*/
public Builder parameters(Object... objects) {
builder.parameters(objects);
return this;
}
/**
* Appends a parameter to the parameter list for the query. If there are
* more parameters than required for one execution of the query then
* more than one execution of the query will occur.
*
* @param value
* @return this
*/
public Builder parameter(Object value) {
builder.parameter(value);
return this;
}
/**
* Sets a named parameter. If name is null throws a
* {@link NullPointerException}. If value is instance of Observable then
* throws an {@link IllegalArgumentException}.
*
* @param name
* the parameter name. Cannot be null.
* @param value
* the parameter value
*/
public Builder parameter(String name, Object value) {
builder.parameter(name, value);
return this;
}
/**
* Sets the {@code FETCH_SIZE} to be used by the query.
*
* @param fetchSize The fetch size to be used. A non-positive value will be ignored.
*/
public Builder fetchSize(int fetchSize) {
this.fetchSize = fetchSize;
return this;
}
/**
* Appends a dependency to the dependencies that have to complete their
* emitting before the query is executed.
*
* @param dependency
* @return this
*/
public Builder dependsOn(Observable> dependency) {
builder.dependsOn(dependency);
return this;
}
/**
* Appends a dependency on the result of the last transaction (
* true
for commit or false
for rollback) to
* the dependencies that have to complete their emitting before the
* query is executed.
*
* @return this
*/
public Builder dependsOnLastTransaction() {
builder.dependsOnLastTransaction();
return this;
}
/**
* The ResultSet is transformed by the given transform before the
* results are traversed.
*
* @param transform
* transforms the ResultSet
* @return this
*/
public Builder resultSetTransform(Func1 transform) {
this.resultSetTransform = transform;
return this;
}
/**
* Transforms the results using the given function.
*
* @param function
* @return the results of the query as an Observable
*/
public Observable get(ResultSetMapper extends T> function) {
return get(function, builder, resultSetTransform);
}
Observable get(ResultSetMapper extends T> function, QueryBuilder builder,
Func1 resultSetTransform) {
final QueryContext ctxt;
if (fetchSize != null) {
ctxt = builder.context().fetchSize(fetchSize);
} else {
ctxt = builder.context();
}
return new QuerySelect(builder.sql(), builder.parameters(), builder.depends(),
ctxt, resultSetTransform).execute(function);
}
/**
*
* Transforms each row of the {@link ResultSet} into an instance of
* T
using automapping of the ResultSet columns into
* corresponding constructor parameters that are assignable. Beyond
* normal assignable criteria (for example Integer 123 is assignable to
* a Double) other conversions exist to facilitate the automapping:
*
*
* They are:
*
* - java.sql.Blob ➟ byte[]
* - java.sql.Blob ➟ java.io.InputStream
* - java.sql.Clob ➟ String
s
* - java.sql.Clob ➟ java.io.Reader
* - java.sql.Date ➟ java.util.Date
* - java.sql.Date ➟ Long
* - java.sql.Timestamp ➟ java.util.Date
* - java.sql.Timestamp ➟ Long
* - java.sql.Time ➟ java.util.Date
* - java.sql.Time ➟ Long
* - java.math.BigInteger ➟
* Short,Integer,Long,Float,Double,BigDecimal
* - java.math.BigDecimal ➟
* Short,Integer,Long,Float,Double,BigInteger
*
*
* @param cls
* @return
*/
public Observable autoMap(Class cls) {
return autoMap(cls, builder, resultSetTransform);
}
Observable autoMap(Class cls, QueryBuilder builder,
Func1 resultSetTransform) {
Util.setSqlFromQueryAnnotation(cls, builder);
return get(Util.autoMap(cls), builder, resultSetTransform);
}
/**
* Automaps the first column of the ResultSet into the target class
* cls
.
*
* @param cls
* @return
*/
public Observable getAs(Class cls) {
return get(Tuples.single(cls));
}
/**
* Automaps all the columns of the {@link ResultSet} into the target
* class cls
. See {@link #autoMap(Class) autoMap()}.
*
* @param cls
* @return
*/
public Observable> getTupleN(Class cls) {
return get(Tuples.tupleN(cls));
}
/**
* Automaps all the columns of the {@link ResultSet} into {@link Object}
* . See {@link #autoMap(Class) autoMap()}.
*
* @param cls
* @return
*/
public Observable> getTupleN() {
return get(Tuples.tupleN(Object.class));
}
/**
* Automaps the columns of the {@link ResultSet} into the specified
* classes. See {@link #autoMap(Class) autoMap()}.
*
* @param cls1
* @param cls2
* @return
*/
public Observable> getAs(Class cls1, Class cls2) {
return get(Tuples.tuple(cls1, cls2));
}
/**
* Automaps the columns of the {@link ResultSet} into the specified
* classes. See {@link #autoMap(Class) autoMap()}.
*
* @param cls1
* @param cls2
* @param cls3
* @return
*/
public Observable> getAs(Class cls1, Class cls2,
Class cls3) {
return get(Tuples.tuple(cls1, cls2, cls3));
}
/**
* Automaps the columns of the {@link ResultSet} into the specified
* classes. See {@link #autoMap(Class) autoMap()}.
*
* @param cls1
* @param cls2
* @param cls3
* @param cls4
* @return
*/
public Observable> getAs(Class cls1,
Class cls2, Class cls3, Class cls4) {
return get(Tuples.tuple(cls1, cls2, cls3, cls4));
}
/**
* Automaps the columns of the {@link ResultSet} into the specified
* classes. See {@link #autoMap(Class) autoMap()}.
*
* @param cls1
* @param cls2
* @param cls3
* @param cls4
* @param cls5
* @return
*/
public Observable> getAs(Class cls1,
Class cls2, Class cls3, Class cls4, Class cls5) {
return get(Tuples.tuple(cls1, cls2, cls3, cls4, cls5));
}
/**
* Automaps the columns of the {@link ResultSet} into the specified
* classes. See {@link #autoMap(Class) autoMap()}.
*
* @param cls1
* @param cls2
* @param cls3
* @param cls4
* @param cls5
* @param cls6
* @return
*/
public Observable> getAs(
Class cls1, Class cls2, Class cls3, Class cls4, Class cls5,
Class cls6) {
return get(Tuples.tuple(cls1, cls2, cls3, cls4, cls5, cls6));
}
/**
* Automaps the columns of the {@link ResultSet} into the specified
* classes. See {@link #autoMap(Class) autoMap()}.
*
* @param cls1
* @param cls2
* @param cls3
* @param cls4
* @param cls5
* @param cls6
* @param cls7
* @return
*/
public Observable> getAs(
Class cls1, Class cls2, Class cls3, Class cls4, Class cls5,
Class cls6, Class cls7) {
return get(Tuples.tuple(cls1, cls2, cls3, cls4, cls5, cls6, cls7));
}
public Observable count() {
return get(Util.toOne()).count();
}
/**
* Returns an {@link Transformer} to allow the query to be pushed
* parameters via the {@link Observable#compose(Transformer)} method.
*
* @return Transformer that acts on parameters
*/
public TransformerBuilder
© 2015 - 2025 Weber Informatics LLC | Privacy Policy