]> source.dussan.org Git - iciql.git/commitdiff
Allow selecting a column from a table
authorJames Moger <james.moger@gitblit.com>
Mon, 4 Apr 2016 23:17:43 +0000 (19:17 -0400)
committerJames Moger <james.moger@gitblit.com>
Mon, 4 Apr 2016 23:17:43 +0000 (19:17 -0400)
releases.moxie
src/main/java/com/iciql/Db.java
src/main/java/com/iciql/Query.java
src/test/java/com/iciql/test/ModelsTest.java
src/test/java/com/iciql/test/PrimitivesTest.java

index df0887162d773d4ed02d82e605e7f1de79051c5d..6a448cbbe3e191a9c3f5bd2944293115c2e5b584 100644 (file)
@@ -15,6 +15,7 @@ r30: {
     changes: ~
     additions:
     - Added groupBy methods to QueryWhere (issue-20)
+    - Allow selecting a column from a table
     dependencyChanges: ~
     contributors:
     - James Moger
index 088fc3070c40c1ef0fe1f40372089544aec6243e..d0756bcbceb9a610d2614219bfcec98e9807d54c 100644 (file)
@@ -140,6 +140,10 @@ public class Db implements AutoCloseable {
                return x;
        }
 
+       static <X> boolean isToken(X x) {
+               return TOKENS.containsKey(x);
+       }
+
        static Token getToken(Object x) {
                return TOKENS.get(x);
        }
index 1dbbc4ea22a061b0009c4a657e0b0ca5461a0c55..f8fd4cae260e9ce99d87e14568cd2040c37c2864 100644 (file)
@@ -389,9 +389,21 @@ public class Query<T> {
        @SuppressWarnings("unchecked")
        private <X, Z> List<X> select(Z x, boolean distinct) {
                Class<?> clazz = x.getClass();
-               if (Utils.isSimpleType(clazz)) {
-                       return selectSimple((X) x, distinct);
+               if (Db.isToken(x)) {
+                       // selecting a function
+                       return selectFunction((X) x, distinct);
+               } else {
+                       // selecting a column
+                       SelectColumn<T> col = getColumnByReference(x);
+                       if (col == null) {
+                               col = getColumnByReference(getPrimitiveAliasByValue(x));
+                       }
+                       if (col != null) {
+                               return (List<X>) selectColumn(col, clazz, distinct);
+                       }
                }
+
+               // selecting into a new object type
                Class<?> enclosingClass = clazz.getEnclosingClass();
                if (enclosingClass != null) {
                        // anonymous inner class
@@ -426,18 +438,41 @@ public class Query<T> {
        }
 
        @SuppressWarnings("unchecked")
-       private <X> List<X> selectSimple(X x, boolean distinct) {
+       private <X> List<X> selectFunction(X x, boolean distinct) {
                SQLStatement stat = getSelectStatement(distinct);
                appendSQL(stat, null, x);
                appendFromWhere(stat);
                ResultSet rs = stat.executeQuery();
                List<X> result = Utils.newArrayList();
-               Class<? extends DataTypeAdapter<?>> typeAdapter = Utils.getDataTypeAdapter(x.getClass().getAnnotations());
                try {
                        // SQLite returns pre-closed ResultSets for query results with 0 rows
                        if (!rs.isClosed()) {
                                while (rs.next()) {
-                                       X value = (X) db.getDialect().deserialize(rs, 1, x.getClass(), typeAdapter);
+                                       X value = (X) rs.getObject(1);
+                                       result.add(value);
+                               }
+                       }
+               } catch (Exception e) {
+                       throw IciqlException.fromSQL(stat.getSQL(), e);
+               } finally {
+                       JdbcUtils.closeSilently(rs, true);
+               }
+               return result;
+       }
+
+       @SuppressWarnings("unchecked")
+       private <X> List<X> selectColumn(SelectColumn<T> col, Class<X> clazz, boolean distinct) {
+               SQLStatement stat = getSelectStatement(distinct);
+               col.appendSQL(stat);
+               appendFromWhere(stat);
+               ResultSet rs = stat.executeQuery();
+               List<X> result = Utils.newArrayList();
+               Class<? extends DataTypeAdapter<?>> typeAdapter = col.getFieldDefinition().typeAdapter;
+               try {
+                       // SQLite returns pre-closed ResultSets for query results with 0 rows
+                       if (!rs.isClosed()) {
+                               while (rs.next()) {
+                                       X value = (X) db.getDialect().deserialize(rs, 1, clazz, typeAdapter);
                                        result.add(value);
                                }
                        }
index ff27bf5090e347cb7005c3595a1573667a3e9f62..a5474dfc60806d0fcb3651070aa464cc7fbf9037 100644 (file)
@@ -160,4 +160,19 @@ public class ModelsTest {
                SupportedTypes s2 = db.from(SupportedTypes.SAMPLE).where(SupportedTypes.SAMPLE.id).is(1).selectFirst();
                assertEquals(40, s2.myString.length());
        }
+
+       @Test
+       public void testColumnSelection() {
+               List<SupportedTypes> original = SupportedTypes.createList();
+               db.insertAll(original);
+               List<String> myStrings = db.from(SupportedTypes.SAMPLE)
+                               .select(SupportedTypes.SAMPLE.myString);
+               assertEquals(10, myStrings.size());
+
+               List<Integer> ids = db.from(SupportedTypes.SAMPLE)
+                               .orderByDesc(SupportedTypes.SAMPLE.id)
+                               .selectDistinct(SupportedTypes.SAMPLE.id);
+               assertEquals(10, ids.size());
+               assertEquals("[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]", ids.toString());
+       }
 }
index 0e8bf20d5989779c7418c2c8e5fd78f7f57459b8..938fd32ebe30cdcbcccc06d3e29fdf2ceb45d8fa 100644 (file)
@@ -99,4 +99,21 @@ public class PrimitivesTest {
                }\r
                db.close();\r
        }\r
+\r
+       @Test\r
+       public void testPrimitiveColumnSelection() {\r
+               Db db = IciqlSuite.openNewDb();\r
+\r
+               // insert random models in reverse order\r
+               List<PrimitivesModel> models = PrimitivesModel.getList();\r
+               PrimitivesModel model = models.get(0);\r
+               Collections.reverse(models);\r
+               // insert them in reverse order\r
+               db.insertAll(models);\r
+\r
+               PrimitivesModel p = new PrimitivesModel();\r
+               List<Long> list = db.from(p)/*.orderByDesc(p.myLong)*/.select(p.myLong);\r
+               assertEquals(models.size(), list.size());\r
+               assertEquals("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]", list.toString());\r
+       }\r
 }\r