-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for transacted() autoMap and getAs tuple
- Loading branch information
1 parent
f46bf85
commit 593dde4
Showing
3 changed files
with
384 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
package org.davidmoten.rx.jdbc; | ||
|
||
import java.sql.ResultSet; | ||
import java.util.Optional; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import org.davidmoten.rx.jdbc.tuple.Tuple2; | ||
import org.davidmoten.rx.jdbc.tuple.Tuple3; | ||
import org.davidmoten.rx.jdbc.tuple.Tuple4; | ||
import org.davidmoten.rx.jdbc.tuple.Tuple5; | ||
import org.davidmoten.rx.jdbc.tuple.Tuple6; | ||
import org.davidmoten.rx.jdbc.tuple.Tuple7; | ||
import org.davidmoten.rx.jdbc.tuple.TupleN; | ||
import org.davidmoten.rx.jdbc.tuple.Tuples; | ||
|
||
import com.github.davidmoten.guavamini.Preconditions; | ||
|
||
import io.reactivex.Flowable; | ||
import io.reactivex.Single; | ||
|
||
public interface GetterTx { | ||
|
||
/** | ||
* Transforms the results using the given function. | ||
* | ||
* @param function | ||
* transforms results | ||
* @param <T> | ||
* the type being mapped to | ||
* @return the results of the query as an Observable | ||
*/ | ||
<T> Flowable<Tx<T>> get(@Nonnull ResultSetMapper<? extends T> function); | ||
|
||
default <T> Flowable<Tx<T>> getAs(@Nonnull Class<T> cls) { | ||
Preconditions.checkNotNull(cls, "cls cannot be null"); | ||
return get(rs -> Util.mapObject(rs, cls, 1)); | ||
} | ||
|
||
default <T> Flowable<Tx<Optional<T>>> getAsOptional(@Nonnull Class<T> cls) { | ||
Preconditions.checkNotNull(cls, "cls cannot be null"); | ||
return get(rs -> Optional.ofNullable(Util.mapObject(rs, cls, 1))); | ||
} | ||
|
||
/** | ||
* <p> | ||
* Transforms each row of the {@link ResultSet} into an instance of | ||
* <code>T</code> using <i>automapping</i> 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: | ||
* </p> | ||
* <p> | ||
* They are: | ||
* <ul> | ||
* <li>java.sql.Blob --> byte[]</li> | ||
* <li>java.sql.Blob --> java.io.InputStream</li> | ||
* <li>java.sql.Clob --> String</li> | ||
* <li>java.sql.Clob --> java.io.Reader</li> | ||
* <li>java.sql.Date --> java.util.Date</li> | ||
* <li>java.sql.Date --> Long</li> | ||
* <li>java.sql.Timestamp --> java.util.Date</li> | ||
* <li>java.sql.Timestamp --> Long</li> | ||
* <li>java.sql.Time --> java.util.Date</li> | ||
* <li>java.sql.Time --> Long</li> | ||
* <li>java.math.BigInteger --> | ||
* Short,Integer,Long,Float,Double,BigDecimal</li> | ||
* <li>java.math.BigDecimal --> | ||
* Short,Integer,Long,Float,Double,BigInteger</li> | ||
* </ul> | ||
* | ||
* @param cls | ||
* class to automap each row of the ResultSet to | ||
* @param <T> | ||
* generic type of returned stream emissions | ||
* @return Flowable of T | ||
* | ||
*/ | ||
default <T> Flowable<Tx<T>> autoMap(@Nonnull Class<T> cls) { | ||
Preconditions.checkNotNull(cls, "cls cannot be null"); | ||
return get(Util.autoMap(cls)); | ||
} | ||
|
||
/** | ||
* Automaps all the columns of the {@link ResultSet} into the target class | ||
* <code>cls</code>. See {@link #autoMap(Class) autoMap()}. | ||
* | ||
* @param cls | ||
* class of the TupleN elements | ||
* @param <T> | ||
* generic type of returned stream emissions | ||
* @return a stream of TupleN | ||
*/ | ||
default <T> Flowable<Tx<TupleN<T>>> getTupleN(@Nonnull Class<T> cls) { | ||
Preconditions.checkNotNull(cls, "cls cannot be null"); | ||
return get(Tuples.tupleN(cls)); | ||
} | ||
|
||
/** | ||
* Automaps all the columns of the {@link ResultSet} into {@link Object} . See | ||
* {@link #autoMap(Class) autoMap()}. | ||
* | ||
* @return stream of TupleN | ||
*/ | ||
default Flowable<Tx<TupleN<Object>>> 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 | ||
* first class | ||
* @param cls2 | ||
* second class | ||
* @param <T1> | ||
* type of first class | ||
* @param <T2> | ||
* type of second class | ||
* @return flowable | ||
*/ | ||
default <T1, T2> Flowable<Tx<Tuple2<T1, T2>>> getAs(@Nonnull Class<T1> cls1, @Nonnull Class<T2> cls2) { | ||
Preconditions.checkNotNull(cls1, "cls1 cannot be null"); | ||
Preconditions.checkNotNull(cls2, "cls2 cannot be null"); | ||
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 | ||
*/ | ||
default <T1, T2, T3> Flowable<Tx<Tuple3<T1, T2, T3>>> getAs(@Nonnull Class<T1> cls1, @Nonnull Class<T2> cls2, | ||
@Nonnull Class<T3> cls3) { | ||
Preconditions.checkNotNull(cls1, "cls1 cannot be null"); | ||
Preconditions.checkNotNull(cls2, "cls2 cannot be null"); | ||
Preconditions.checkNotNull(cls3, "cls3 cannot be null"); | ||
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 | ||
*/ | ||
default <T1, T2, T3, T4> Flowable<Tx<Tuple4<T1, T2, T3, T4>>> getAs(@Nonnull Class<T1> cls1, @Nonnull Class<T2> cls2, | ||
@Nonnull Class<T3> cls3, @Nonnull Class<T4> cls4) { | ||
Preconditions.checkNotNull(cls1, "cls1 cannot be null"); | ||
Preconditions.checkNotNull(cls2, "cls2 cannot be null"); | ||
Preconditions.checkNotNull(cls3, "cls3 cannot be null"); | ||
Preconditions.checkNotNull(cls4, "cls4 cannot be null"); | ||
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 | ||
*/ | ||
default <T1, T2, T3, T4, T5> Flowable<Tx<Tuple5<T1, T2, T3, T4, T5>>> getAs(@Nonnull Class<T1> cls1, | ||
@Nonnull Class<T2> cls2, @Nonnull Class<T3> cls3, @Nonnull Class<T4> cls4, @Nonnull Class<T5> cls5) { | ||
Preconditions.checkNotNull(cls1, "cls1 cannot be null"); | ||
Preconditions.checkNotNull(cls2, "cls2 cannot be null"); | ||
Preconditions.checkNotNull(cls3, "cls3 cannot be null"); | ||
Preconditions.checkNotNull(cls4, "cls4 cannot be null"); | ||
Preconditions.checkNotNull(cls5, "cls5 cannot be null"); | ||
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 | ||
*/ | ||
default <T1, T2, T3, T4, T5, T6> Flowable<Tx<Tuple6<T1, T2, T3, T4, T5, T6>>> getAs(@Nonnull Class<T1> cls1, | ||
@Nonnull Class<T2> cls2, @Nonnull Class<T3> cls3, @Nonnull Class<T4> cls4, @Nonnull Class<T5> cls5, | ||
@Nonnull Class<T6> cls6) { | ||
Preconditions.checkNotNull(cls1, "cls1 cannot be null"); | ||
Preconditions.checkNotNull(cls2, "cls2 cannot be null"); | ||
Preconditions.checkNotNull(cls3, "cls3 cannot be null"); | ||
Preconditions.checkNotNull(cls4, "cls4 cannot be null"); | ||
Preconditions.checkNotNull(cls5, "cls5 cannot be null"); | ||
Preconditions.checkNotNull(cls6, "cls6 cannot be null"); | ||
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 | ||
*/ | ||
default <T1, T2, T3, T4, T5, T6, T7> Flowable<Tx<Tuple7<T1, T2, T3, T4, T5, T6, T7>>> getAs(@Nonnull Class<T1> cls1, | ||
@Nonnull Class<T2> cls2, @Nonnull Class<T3> cls3, @Nonnull Class<T4> cls4, @Nonnull Class<T5> cls5, | ||
@Nonnull Class<T6> cls6, @Nonnull Class<T7> cls7) { | ||
Preconditions.checkNotNull(cls1, "cls1 cannot be null"); | ||
Preconditions.checkNotNull(cls2, "cls2 cannot be null"); | ||
Preconditions.checkNotNull(cls3, "cls3 cannot be null"); | ||
Preconditions.checkNotNull(cls4, "cls4 cannot be null"); | ||
Preconditions.checkNotNull(cls5, "cls5 cannot be null"); | ||
Preconditions.checkNotNull(cls6, "cls6 cannot be null"); | ||
Preconditions.checkNotNull(cls7, "cls7 cannot be null"); | ||
return get(Tuples.tuple(cls1, cls2, cls3, cls4, cls5, cls6, cls7)); | ||
} | ||
|
||
default Single<Long> count() { | ||
return get(rs -> 1).count(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.