View Javadoc
1   package com.github.davidmoten.rx.jdbc.tuple;
2   
3   import static com.github.davidmoten.rx.jdbc.Util.mapObject;
4   
5   import java.sql.ResultSet;
6   import java.sql.SQLException;
7   import java.util.ArrayList;
8   import java.util.List;
9   
10  import com.github.davidmoten.rx.jdbc.ResultSetMapper;
11  import com.github.davidmoten.rx.jdbc.exceptions.SQLRuntimeException;
12  
13  /**
14   * Utility methods for tuples.
15   */
16  public final class Tuples {
17  
18      /**
19       * Private constructor to prevent instantiation.
20       */
21      private Tuples() {
22          // prevent instantiation.
23      }
24  
25      public static <T> ResultSetMapper<T> single(final Class<T> cls) {
26          return new ResultSetMapper<T>() {
27  
28              @SuppressWarnings("unchecked")
29              @Override
30              public T call(ResultSet rs) {
31                  return (T) mapObject(rs, cls, 1);
32              }
33  
34          };
35      }
36  
37      public static <T1, T2> ResultSetMapper<Tuple2<T1, T2>> tuple(final Class<T1> cls1,
38              final Class<T2> cls2) {
39          return new ResultSetMapper<Tuple2<T1, T2>>() {
40  
41              @SuppressWarnings("unchecked")
42              @Override
43              public Tuple2<T1, T2> call(ResultSet rs) {
44                  return new Tuple2<T1, T2>((T1) mapObject(rs, cls1, 1), (T2) mapObject(rs, cls2, 2));
45              }
46          };
47      }
48  
49      public static <T1, T2, T3> ResultSetMapper<Tuple3<T1, T2, T3>> tuple(final Class<T1> cls1,
50              final Class<T2> cls2, final Class<T3> cls3) {
51          return new ResultSetMapper<Tuple3<T1, T2, T3>>() {
52              @SuppressWarnings("unchecked")
53              @Override
54              public Tuple3<T1, T2, T3> call(ResultSet rs) {
55                  return new Tuple3<T1, T2, T3>((T1) mapObject(rs, cls1, 1), (T2) mapObject(rs, cls2,
56                          2), (T3) mapObject(rs, cls3, 3));
57              }
58          };
59      }
60  
61      public static <T1, T2, T3, T4> ResultSetMapper<Tuple4<T1, T2, T3, T4>> tuple(
62              final Class<T1> cls1, final Class<T2> cls2, final Class<T3> cls3, final Class<T4> cls4) {
63          return new ResultSetMapper<Tuple4<T1, T2, T3, T4>>() {
64              @SuppressWarnings("unchecked")
65              @Override
66              public Tuple4<T1, T2, T3, T4> call(ResultSet rs) {
67                  return new Tuple4<T1, T2, T3, T4>((T1) mapObject(rs, cls1, 1), (T2) mapObject(rs,
68                          cls2, 2), (T3) mapObject(rs, cls3, 3), (T4) mapObject(rs, cls4, 4));
69              }
70          };
71      }
72  
73      public static <T1, T2, T3, T4, T5> ResultSetMapper<Tuple5<T1, T2, T3, T4, T5>> tuple(
74              final Class<T1> cls1, final Class<T2> cls2, final Class<T3> cls3, final Class<T4> cls4,
75              final Class<T5> cls5) {
76          return new ResultSetMapper<Tuple5<T1, T2, T3, T4, T5>>() {
77              @SuppressWarnings("unchecked")
78              @Override
79              public Tuple5<T1, T2, T3, T4, T5> call(ResultSet rs) {
80                  return new Tuple5<T1, T2, T3, T4, T5>((T1) mapObject(rs, cls1, 1), (T2) mapObject(
81                          rs, cls2, 2), (T3) mapObject(rs, cls3, 3), (T4) mapObject(rs, cls4, 4),
82                          (T5) mapObject(rs, cls5, 5));
83              }
84          };
85      }
86  
87      public static <T1, T2, T3, T4, T5, T6> ResultSetMapper<Tuple6<T1, T2, T3, T4, T5, T6>> tuple(
88              final Class<T1> cls1, final Class<T2> cls2, final Class<T3> cls3, final Class<T4> cls4,
89              final Class<T5> cls5, final Class<T6> cls6) {
90  
91          return new ResultSetMapper<Tuple6<T1, T2, T3, T4, T5, T6>>() {
92              @SuppressWarnings("unchecked")
93              @Override
94              public Tuple6<T1, T2, T3, T4, T5, T6> call(ResultSet rs) {
95                  return new Tuple6<T1, T2, T3, T4, T5, T6>((T1) mapObject(rs, cls1, 1),
96                          (T2) mapObject(rs, cls2, 2), (T3) mapObject(rs, cls3, 3), (T4) mapObject(
97                                  rs, cls4, 4), (T5) mapObject(rs, cls5, 5), (T6) mapObject(rs, cls6,
98                                  6));
99              }
100         };
101     }
102 
103     public static <T1, T2, T3, T4, T5, T6, T7> ResultSetMapper<Tuple7<T1, T2, T3, T4, T5, T6, T7>> tuple(
104             final Class<T1> cls1, final Class<T2> cls2, final Class<T3> cls3, final Class<T4> cls4,
105             final Class<T5> cls5, final Class<T6> cls6, final Class<T7> cls7) {
106 
107         return new ResultSetMapper<Tuple7<T1, T2, T3, T4, T5, T6, T7>>() {
108             @SuppressWarnings("unchecked")
109             @Override
110             public Tuple7<T1, T2, T3, T4, T5, T6, T7> call(ResultSet rs) {
111                 return new Tuple7<T1, T2, T3, T4, T5, T6, T7>((T1) mapObject(rs, cls1, 1),
112                         (T2) mapObject(rs, cls2, 2), (T3) mapObject(rs, cls3, 3), (T4) mapObject(
113                                 rs, cls4, 4), (T5) mapObject(rs, cls5, 5), (T6) mapObject(rs, cls6,
114                                 6), (T7) mapObject(rs, cls7, 7));
115             }
116         };
117     }
118 
119     public static <T> ResultSetMapper<TupleN<T>> tupleN(final Class<T> cls) {
120         return new ResultSetMapper<TupleN<T>>() {
121             @Override
122             public TupleN<T> call(ResultSet rs) {
123                 return toTupleN(cls, rs);
124             }
125         };
126     }
127 
128     @SuppressWarnings("unchecked")
129     private static <T> TupleN<T> toTupleN(final Class<T> cls, ResultSet rs) {
130         try {
131             int n = rs.getMetaData().getColumnCount();
132             List<T> list = new ArrayList<T>();
133             for (int i = 1; i <= n; i++) {
134                 list.add((T) mapObject(rs, cls, i));
135             }
136             return new TupleN<T>(list);
137         } catch (SQLException e) {
138             throw new SQLRuntimeException(e);
139         }
140     }
141 }