View Javadoc
1   package org.davidmoten.rx.jdbc;
2   
3   import java.sql.ResultSet;
4   import java.util.Optional;
5   
6   import javax.annotation.Nonnull;
7   
8   import org.davidmoten.rx.jdbc.tuple.Tuple2;
9   import org.davidmoten.rx.jdbc.tuple.Tuple3;
10  import org.davidmoten.rx.jdbc.tuple.Tuple4;
11  import org.davidmoten.rx.jdbc.tuple.Tuple5;
12  import org.davidmoten.rx.jdbc.tuple.Tuple6;
13  import org.davidmoten.rx.jdbc.tuple.Tuple7;
14  import org.davidmoten.rx.jdbc.tuple.TupleN;
15  import org.davidmoten.rx.jdbc.tuple.Tuples;
16  
17  import com.github.davidmoten.guavamini.Preconditions;
18  
19  import io.reactivex.Flowable;
20  import io.reactivex.Single;
21  
22  public interface GetterTx {
23  
24      /**
25       * Transforms the results using the given function.
26       *
27       * @param mapper
28       *            transforms ResultSet row to an object of type T
29       * @param <T>
30       *            the type being mapped to
31       * @return the results of the query as an Observable
32       */
33      <T> Flowable<Tx<T>> get(@Nonnull ResultSetMapper<? extends T> mapper);
34  
35      default <T> Flowable<Tx<T>> getAs(@Nonnull Class<T> cls) {
36          Preconditions.checkNotNull(cls, "cls cannot be null");
37          return get(rs -> Util.mapObject(rs, cls, 1));
38      }
39  
40      default <T> Flowable<Tx<Optional<T>>> getAsOptional(@Nonnull Class<T> cls) {
41          Preconditions.checkNotNull(cls, "cls cannot be null");
42          return get(rs -> Optional.ofNullable(Util.mapObject(rs, cls, 1)));
43      }
44  
45      /**
46       * <p>
47       * Transforms each row of the {@link ResultSet} into an instance of
48       * <code>T</code> using <i>automapping</i> of the ResultSet columns into
49       * corresponding constructor parameters that are assignable. Beyond normal
50       * assignable criteria (for example Integer 123 is assignable to a Double) other
51       * conversions exist to facilitate the automapping:
52       * </p>
53       * <p>
54       * They are:
55       * <ul>
56       * <li>java.sql.Blob --&gt; byte[]</li>
57       * <li>java.sql.Blob --&gt; java.io.InputStream</li>
58       * <li>java.sql.Clob --&gt; String</li>
59       * <li>java.sql.Clob --&gt; java.io.Reader</li>
60       * <li>java.sql.Date --&gt; java.util.Date</li>
61       * <li>java.sql.Date --&gt; Long</li>
62       * <li>java.sql.Timestamp --&gt; java.util.Date</li>
63       * <li>java.sql.Timestamp --&gt; Long</li>
64       * <li>java.sql.Time --&gt; java.util.Date</li>
65       * <li>java.sql.Time --&gt; Long</li>
66       * <li>java.math.BigInteger --&gt;
67       * Short,Integer,Long,Float,Double,BigDecimal</li>
68       * <li>java.math.BigDecimal --&gt;
69       * Short,Integer,Long,Float,Double,BigInteger</li>
70       * </ul>
71       *
72       * @param cls
73       *            class to automap each row of the ResultSet to
74       * @param <T>
75       *            generic type of returned stream emissions
76       * @return Flowable of T
77       * 
78       */
79      default <T> Flowable<Tx<T>> autoMap(@Nonnull Class<T> cls) {
80          Preconditions.checkNotNull(cls, "cls cannot be null");
81          return get(Util.autoMap(cls));
82      }
83  
84      /**
85       * Automaps all the columns of the {@link ResultSet} into the target class
86       * <code>cls</code>. See {@link #autoMap(Class) autoMap()}.
87       *
88       * @param cls
89       *            class of the TupleN elements
90       * @param <T>
91       *            generic type of returned stream emissions
92       * @return stream of transaction items
93       */
94      default <T> Flowable<Tx<TupleN<T>>> getTupleN(@Nonnull Class<T> cls) {
95          Preconditions.checkNotNull(cls, "cls cannot be null");
96          return get(Tuples.tupleN(cls));
97      }
98  
99      /**
100      * Automaps all the columns of the {@link ResultSet} into {@link Object} . See
101      * {@link #autoMap(Class) autoMap()}.
102      *
103      * @return stream of transaction items
104      */
105     default Flowable<Tx<TupleN<Object>>> getTupleN() {
106         return get(Tuples.tupleN(Object.class));
107     }
108 
109     /**
110      * Automaps the columns of the {@link ResultSet} into the specified classes. See
111      * {@link #autoMap(Class) autoMap()}.
112      *
113      * @param cls1
114      *            first class
115      * @param cls2
116      *            second class
117      * @param <T1>
118      *            type of first class
119      * @param <T2>
120      *            type of second class
121      * @return stream of transaction items
122      */
123     default <T1, T2> Flowable<Tx<Tuple2<T1, T2>>> getAs(@Nonnull Class<T1> cls1,
124             @Nonnull Class<T2> cls2) {
125         return get(Tuples.tuple(cls1, cls2));
126     }
127 
128     /**
129      * Automaps the columns of the {@link ResultSet} into the specified classes. See
130      * {@link #autoMap(Class) autoMap()}.
131      *
132      * @param cls1
133      *            first class
134      * @param cls2
135      *            second class
136      * @param cls3
137      *            third class
138      * @param <T1>
139      *            type of first class
140      * @param <T2>
141      *            type of second class
142      * @param <T3>
143      *            type of third class
144      * @return stream of tuples
145      */
146     default <T1, T2, T3> Flowable<Tx<Tuple3<T1, T2, T3>>> getAs(@Nonnull Class<T1> cls1,
147             @Nonnull Class<T2> cls2, @Nonnull Class<T3> cls3) {
148         return get(Tuples.tuple(cls1, cls2, cls3));
149     }
150 
151     /**
152      * Automaps the columns of the {@link ResultSet} into the specified classes. See
153      * {@link #autoMap(Class) autoMap()}.
154      *
155      * @param cls1
156      *            first class
157      * @param cls2
158      *            second class
159      * @param cls3
160      *            third class
161      * @param cls4
162      *            fourth class
163      * @param <T1>
164      *            type of first class
165      * @param <T2>
166      *            type of second class
167      * @param <T3>
168      *            type of third class
169      * @param <T4>
170      *            type of fourth class
171      * @return stream of tuples
172      */
173     default <T1, T2, T3, T4> Flowable<Tx<Tuple4<T1, T2, T3, T4>>> getAs(@Nonnull Class<T1> cls1,
174             @Nonnull Class<T2> cls2, @Nonnull Class<T3> cls3, @Nonnull Class<T4> cls4) {
175         return get(Tuples.tuple(cls1, cls2, cls3, cls4));
176     }
177 
178     /**
179      * Automaps the columns of the {@link ResultSet} into the specified classes. See
180      * {@link #autoMap(Class) autoMap()}.
181      *
182      * @param cls1
183      *            first class
184      * @param cls2
185      *            second class
186      * @param cls3
187      *            third class
188      * @param cls4
189      *            fourth class
190      * @param cls5
191      *            fifth class
192      * @param <T1>
193      *            type of first class
194      * @param <T2>
195      *            type of second class
196      * @param <T3>
197      *            type of third class
198      * @param <T4>
199      *            type of fourth class
200      * @param <T5>
201      *            type of fifth class
202      * @return stream of transaction items
203      */
204     default <T1, T2, T3, T4, T5> Flowable<Tx<Tuple5<T1, T2, T3, T4, T5>>> getAs(
205             @Nonnull Class<T1> cls1, @Nonnull Class<T2> cls2, @Nonnull Class<T3> cls3,
206             @Nonnull Class<T4> cls4, @Nonnull Class<T5> cls5) {
207         return get(Tuples.tuple(cls1, cls2, cls3, cls4, cls5));
208     }
209 
210     /**
211      * Automaps the columns of the {@link ResultSet} into the specified classes. See
212      * {@link #autoMap(Class) autoMap()}.
213      *
214      * @param cls1
215      *            first class
216      * @param cls2
217      *            second class
218      * @param cls3
219      *            third class
220      * @param cls4
221      *            fourth class
222      * @param cls5
223      *            fifth class
224      * @param cls6
225      *            sixth class
226      * @param <T1>
227      *            type of first class
228      * @param <T2>
229      *            type of second class
230      * @param <T3>
231      *            type of third class
232      * @param <T4>
233      *            type of fourth class
234      * @param <T5>
235      *            type of fifth class
236      * @param <T6>
237      *            type of sixth class
238      * @return stream of transaction items
239      */
240     default <T1, T2, T3, T4, T5, T6> Flowable<Tx<Tuple6<T1, T2, T3, T4, T5, T6>>> getAs(
241             @Nonnull Class<T1> cls1, @Nonnull Class<T2> cls2, @Nonnull Class<T3> cls3,
242             @Nonnull Class<T4> cls4, @Nonnull Class<T5> cls5, @Nonnull Class<T6> cls6) {
243         return get(Tuples.tuple(cls1, cls2, cls3, cls4, cls5, cls6));
244     }
245 
246     /**
247      * Automaps the columns of the {@link ResultSet} into the specified classes. See
248      * {@link #autoMap(Class) autoMap()}.
249      *
250      * @param cls1
251      *            first class
252      * @param cls2
253      *            second class
254      * @param cls3
255      *            third class
256      * @param cls4
257      *            fourth class
258      * @param cls5
259      *            fifth class
260      * @param cls6
261      *            sixth class
262      * @param cls7
263      *            seventh class
264      * @param <T1>
265      *            type of first class
266      * @param <T2>
267      *            type of second class
268      * @param <T3>
269      *            type of third class
270      * @param <T4>
271      *            type of fourth class
272      * @param <T5>
273      *            type of fifth class
274      * @param <T6>
275      *            type of sixth class
276      * @param <T7>
277      *            type of seventh class
278      * @return stream of transaction items
279      */
280     default <T1, T2, T3, T4, T5, T6, T7> Flowable<Tx<Tuple7<T1, T2, T3, T4, T5, T6, T7>>> getAs(
281             @Nonnull Class<T1> cls1, @Nonnull Class<T2> cls2, @Nonnull Class<T3> cls3,
282             @Nonnull Class<T4> cls4, @Nonnull Class<T5> cls5, @Nonnull Class<T6> cls6,
283             @Nonnull Class<T7> cls7) {
284         return get(Tuples.tuple(cls1, cls2, cls3, cls4, cls5, cls6, cls7));
285     }
286 
287     default Single<Long> count() {
288         return get(rs -> 1).count();
289     }
290 }