Author: doogie
Date: Mon Aug 2 17:43:36 2010 New Revision: 981633 URL: http://svn.apache.org/viewvc?rev=981633&view=rev Log: FEATURE: Add support for NULLS FIRST/LAST in ORDER BY. Modified: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/OrderByItem.java ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SelectTest.java Modified: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/OrderByItem.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/OrderByItem.java?rev=981633&r1=981632&r2=981633&view=diff ============================================================================== --- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/OrderByItem.java (original) +++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/OrderByItem.java Mon Aug 2 17:43:36 2010 @@ -23,13 +23,16 @@ import org.ofbiz.base.lang.SourceMonitor @SourceMonitored public final class OrderByItem extends Atom { public enum Order { DEFAULT, ASCENDING, DESCENDING }; + public enum Nulls { DEFAULT, FIRST, LAST }; private final Order order; + private final Nulls nulls; private final String functionName; private final String fieldName; - public OrderByItem(Order order, String functionName, String fieldName) { + public OrderByItem(Order order, Nulls nulls, String functionName, String fieldName) { this.order = order; + this.nulls = nulls; this.functionName = functionName; this.fieldName = fieldName; } @@ -38,6 +41,10 @@ public final class OrderByItem extends A return order; } + public final Nulls getNulls() { + return nulls; + } + public final String getFunctionName() { return functionName; } @@ -49,7 +56,7 @@ public final class OrderByItem extends A public boolean equals(Object o) { if (o instanceof OrderByItem) { OrderByItem other = (OrderByItem) o; - return order.equals(other.order) && equalsHelper(functionName, other.functionName) && fieldName.equals(other.fieldName); + return order.equals(other.order) && nulls.equals(other.nulls) && equalsHelper(functionName, other.functionName) && fieldName.equals(other.fieldName); } else { return false; } @@ -67,6 +74,14 @@ public final class OrderByItem extends A sb.append(" DESC"); break; } + switch (nulls) { + case FIRST: + sb.append(" NULLS FIRST"); + break; + case LAST: + sb.append(" NULLS LAST"); + break; + } return sb; } } Modified: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj?rev=981633&r1=981632&r2=981633&view=diff ============================================================================== --- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj (original) +++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj Mon Aug 2 17:43:36 2010 @@ -134,6 +134,9 @@ TOKEN: { | <NOT: "NOT"> | <NULL: "NULL"> | <MAP: "MAP"> + | <NULLS: "NULLS"> + | <FIRST: "FIRST"> + | <LAST: "LAST"> | <START_DQUOTE: "\""> { pushState(IN_DQUOTE); } | <START_SQUOTE: "'"> { pushState(IN_SQUOTE); } | <INTEGER: @@ -696,6 +699,7 @@ private List<OrderByItem> OrderByList(): private OrderByItem OrderByItem(): { String functionName = null, fieldName = null; OrderByItem.Order ordering = OrderByItem.Order.DEFAULT; + OrderByItem.Nulls nulls = OrderByItem.Nulls.DEFAULT; } { ( ( @@ -719,7 +723,13 @@ private OrderByItem OrderByItem(): { | <ASC> { ordering = OrderByItem.Order.ASCENDING; } )? ) - { return new OrderByItem(ordering, functionName, fieldName); } + ( + <NULLS> ( + <FIRST> { nulls = OrderByItem.Nulls.FIRST; } + | <LAST> { nulls = OrderByItem.Nulls.LAST; } + ) + )? + { return new OrderByItem(ordering, nulls, functionName, fieldName); } } private Integer Integer(): { Modified: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java?rev=981633&r1=981632&r2=981633&view=diff ============================================================================== --- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java (original) +++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java Mon Aug 2 17:43:36 2010 @@ -135,9 +135,9 @@ public class SQLTest extends GenericTest new BooleanCondition(new FieldValue("b", "firstName"), "LIKE", new StringValue("%foo%")), null, list( - new OrderByItem(OrderByItem.Order.DEFAULT, "LOWER", "lastName"), - new OrderByItem(OrderByItem.Order.DEFAULT, null, "firstName"), - new OrderByItem(OrderByItem.Order.DESCENDING, null, "birthDate") + new OrderByItem(OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, "LOWER", "lastName"), + new OrderByItem(OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, null, "firstName"), + new OrderByItem(OrderByItem.Order.DESCENDING, OrderByItem.Nulls.DEFAULT, null, "birthDate") ), 5, 10 Modified: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SelectTest.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SelectTest.java?rev=981633&r1=981632&r2=981633&view=diff ============================================================================== --- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SelectTest.java (original) +++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SelectTest.java Mon Aug 2 17:43:36 2010 @@ -116,8 +116,9 @@ public class SelectTest extends GenericT fieldDefTest("v5", v5, fv1, null, "partyId", v1, true); } - private static void orderByItemTest(String label, OrderByItem v, OrderByItem.Order order, String functionName, String fieldName, OrderByItem o, boolean matches) throws Exception { + private static void orderByItemTest(String label, OrderByItem v, OrderByItem.Order order, OrderByItem.Nulls nulls, String functionName, String fieldName, OrderByItem o, boolean matches) throws Exception { assertEquals(label + ":order", order, v.getOrder()); + assertEquals(label + ":nulls", nulls, v.getNulls()); assertEquals(label + ":function-name", functionName, v.getFunctionName()); assertEquals(label + ":field-name", fieldName, v.getFieldName()); assertEquals(label + ":parse", v, parser(v).parse_OrderByItem()); @@ -125,15 +126,17 @@ public class SelectTest extends GenericT } public void testOrderByItem() throws Exception { - OrderByItem v1 = new OrderByItem(OrderByItem.Order.DEFAULT, null, "partyId"); - orderByItemTest("v1", v1, OrderByItem.Order.DEFAULT, null, "partyId", null, false); - OrderByItem v2 = new OrderByItem(OrderByItem.Order.ASCENDING, null, "partyId"); - orderByItemTest("v3", v2, OrderByItem.Order.ASCENDING, null, "partyId", v1, false); - OrderByItem v3 = new OrderByItem(OrderByItem.Order.DESCENDING, null, "partyId"); - orderByItemTest("v2", v3, OrderByItem.Order.DESCENDING, null, "partyId", v1, false); - OrderByItem v4 = new OrderByItem(OrderByItem.Order.DEFAULT, "LOWER", "partyId"); - orderByItemTest("v4", v4, OrderByItem.Order.DEFAULT, "LOWER", "partyId", v1, false); - OrderByItem v5 = new OrderByItem(OrderByItem.Order.DEFAULT, null, "firstName"); - orderByItemTest("v5", v5, OrderByItem.Order.DEFAULT, null, "firstName", v1, false); + OrderByItem v1 = new OrderByItem(OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, null, "partyId"); + orderByItemTest("v1", v1, OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, null, "partyId", null, false); + OrderByItem v2 = new OrderByItem(OrderByItem.Order.ASCENDING, OrderByItem.Nulls.FIRST, null, "partyId"); + orderByItemTest("v3", v2, OrderByItem.Order.ASCENDING, OrderByItem.Nulls.FIRST, null, "partyId", v1, false); + OrderByItem v3 = new OrderByItem(OrderByItem.Order.DESCENDING, OrderByItem.Nulls.LAST, null, "partyId"); + orderByItemTest("v2", v3, OrderByItem.Order.DESCENDING, OrderByItem.Nulls.LAST, null, "partyId", v1, false); + OrderByItem v4 = new OrderByItem(OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, "LOWER", "partyId"); + orderByItemTest("v4", v4, OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, "LOWER", "partyId", v1, false); + OrderByItem v5 = new OrderByItem(OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, null, "firstName"); + orderByItemTest("v5", v5, OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, null, "firstName", v1, false); + OrderByItem v6 = new OrderByItem(OrderByItem.Order.DEFAULT, OrderByItem.Nulls.LAST, null, "firstName"); + orderByItemTest("v6", v6, OrderByItem.Order.DEFAULT, OrderByItem.Nulls.LAST, null, "firstName", v1, false); } } |
Free forum by Nabble | Edit this page |