From: James Moger Date: Mon, 4 Apr 2016 23:17:43 +0000 (-0400) Subject: Allow selecting a column from a table X-Git-Tag: release-2.0.0~6 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=27979f07905dbf95b478a48babc99643d71bfdec;p=iciql.git Allow selecting a column from a table --- diff --git a/releases.moxie b/releases.moxie index df08871..6a448cb 100644 --- a/releases.moxie +++ b/releases.moxie @@ -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 diff --git a/src/main/java/com/iciql/Db.java b/src/main/java/com/iciql/Db.java index 088fc30..d0756bc 100644 --- a/src/main/java/com/iciql/Db.java +++ b/src/main/java/com/iciql/Db.java @@ -140,6 +140,10 @@ public class Db implements AutoCloseable { return x; } + static boolean isToken(X x) { + return TOKENS.containsKey(x); + } + static Token getToken(Object x) { return TOKENS.get(x); } diff --git a/src/main/java/com/iciql/Query.java b/src/main/java/com/iciql/Query.java index 1dbbc4e..f8fd4ca 100644 --- a/src/main/java/com/iciql/Query.java +++ b/src/main/java/com/iciql/Query.java @@ -389,9 +389,21 @@ public class Query { @SuppressWarnings("unchecked") private List 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 col = getColumnByReference(x); + if (col == null) { + col = getColumnByReference(getPrimitiveAliasByValue(x)); + } + if (col != null) { + return (List) 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 { } @SuppressWarnings("unchecked") - private List selectSimple(X x, boolean distinct) { + private List selectFunction(X x, boolean distinct) { SQLStatement stat = getSelectStatement(distinct); appendSQL(stat, null, x); appendFromWhere(stat); ResultSet rs = stat.executeQuery(); List result = Utils.newArrayList(); - Class> 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 List selectColumn(SelectColumn col, Class clazz, boolean distinct) { + SQLStatement stat = getSelectStatement(distinct); + col.appendSQL(stat); + appendFromWhere(stat); + ResultSet rs = stat.executeQuery(); + List result = Utils.newArrayList(); + Class> 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); } } diff --git a/src/test/java/com/iciql/test/ModelsTest.java b/src/test/java/com/iciql/test/ModelsTest.java index ff27bf5..a5474df 100644 --- a/src/test/java/com/iciql/test/ModelsTest.java +++ b/src/test/java/com/iciql/test/ModelsTest.java @@ -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 original = SupportedTypes.createList(); + db.insertAll(original); + List myStrings = db.from(SupportedTypes.SAMPLE) + .select(SupportedTypes.SAMPLE.myString); + assertEquals(10, myStrings.size()); + + List 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()); + } } diff --git a/src/test/java/com/iciql/test/PrimitivesTest.java b/src/test/java/com/iciql/test/PrimitivesTest.java index 0e8bf20..938fd32 100644 --- a/src/test/java/com/iciql/test/PrimitivesTest.java +++ b/src/test/java/com/iciql/test/PrimitivesTest.java @@ -99,4 +99,21 @@ public class PrimitivesTest { } db.close(); } + + @Test + public void testPrimitiveColumnSelection() { + Db db = IciqlSuite.openNewDb(); + + // insert random models in reverse order + List models = PrimitivesModel.getList(); + PrimitivesModel model = models.get(0); + Collections.reverse(models); + // insert them in reverse order + db.insertAll(models); + + PrimitivesModel p = new PrimitivesModel(); + List list = db.from(p)/*.orderByDesc(p.myLong)*/.select(p.myLong); + assertEquals(models.size(), list.size()); + assertEquals("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]", list.toString()); + } }