From 2523ad5454716a9e08f0d3220a92760aa50773e2 Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 4 Apr 2016 19:32:44 -0400 Subject: [PATCH] Fixes #21: Order by descending on primitives --- releases.moxie | 1 + src/main/java/com/iciql/Query.java | 32 +++++++++++++++++++ src/test/java/com/iciql/test/ModelsTest.java | 2 +- .../java/com/iciql/test/PrimitivesTest.java | 5 ++- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/releases.moxie b/releases.moxie index 6a448cb..3630f1d 100644 --- a/releases.moxie +++ b/releases.moxie @@ -10,6 +10,7 @@ r30: { text: ~ security: ~ fixes: + - Fixed order by descending on primitives (issue-21) - Fixed setting null column values (issue-22) - Fixed Postgres 9.5 Upsert syntax (issue-23) changes: ~ diff --git a/src/main/java/com/iciql/Query.java b/src/main/java/com/iciql/Query.java index f8fd4ca..7a8a1ff 100644 --- a/src/main/java/com/iciql/Query.java +++ b/src/main/java/com/iciql/Query.java @@ -736,6 +736,38 @@ public class Query { return this; } + public Query orderByDesc(byte field) { + return orderByDescPrimitive(field); + } + + public Query orderByDesc(short field) { + return orderByDescPrimitive(field); + } + + public Query orderByDesc(int field) { + return orderByDescPrimitive(field); + } + + public Query orderByDesc(long field) { + return orderByDescPrimitive(field); + } + + public Query orderByDesc(float field) { + return orderByDescPrimitive(field); + } + + public Query orderByDesc(double field) { + return orderByDescPrimitive(field); + } + + Query orderByDescPrimitive(Object field) { + Object alias = getPrimitiveAliasByValue(field); + if (alias == null) { + return orderByDesc(field); + } + return orderByDesc(alias); + } + public Query orderByDesc(Object expr) { OrderExpression e = new OrderExpression(this, expr, true, false, false); addOrderBy(e); diff --git a/src/test/java/com/iciql/test/ModelsTest.java b/src/test/java/com/iciql/test/ModelsTest.java index a5474df..4230f4e 100644 --- a/src/test/java/com/iciql/test/ModelsTest.java +++ b/src/test/java/com/iciql/test/ModelsTest.java @@ -173,6 +173,6 @@ public class ModelsTest { .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()); + assertEquals("[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]", ids.toString()); } } diff --git a/src/test/java/com/iciql/test/PrimitivesTest.java b/src/test/java/com/iciql/test/PrimitivesTest.java index 938fd32..6250649 100644 --- a/src/test/java/com/iciql/test/PrimitivesTest.java +++ b/src/test/java/com/iciql/test/PrimitivesTest.java @@ -106,14 +106,13 @@ public class PrimitivesTest { // 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); + 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()); + assertEquals("[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]", list.toString()); } } -- 2.39.5