Author: doogie
Date: Mon Aug 2 17:43:53 2010 New Revision: 981636 URL: http://svn.apache.org/viewvc?rev=981636&view=rev Log: FEATURE: ORDER BY now supports any ConstantValue, which includes any function, and also math operations. 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=981636&r1=981635&r2=981636&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:53 2010 @@ -27,14 +27,12 @@ public final class OrderByItem extends A private final Order order; private final Nulls nulls; - private final String functionName; - private final String fieldName; + private final ConstantValue value; - public OrderByItem(Order order, Nulls nulls, String functionName, String fieldName) { + public OrderByItem(Order order, Nulls nulls, ConstantValue value) { this.order = order; this.nulls = nulls; - this.functionName = functionName; - this.fieldName = fieldName; + this.value = value; } public final Order getOrder() { @@ -45,27 +43,21 @@ public final class OrderByItem extends A return nulls; } - public final String getFunctionName() { - return functionName; - } - - public final String getFieldName() { - return fieldName; + public final ConstantValue getValue() { + return value; } public boolean equals(Object o) { if (o instanceof OrderByItem) { OrderByItem other = (OrderByItem) o; - return order.equals(other.order) && nulls.equals(other.nulls) && equalsHelper(functionName, other.functionName) && fieldName.equals(other.fieldName); + return order.equals(other.order) && nulls.equals(other.nulls) && value.equals(other.value); } else { return false; } } public StringBuilder appendTo(StringBuilder sb) { - if (functionName != null) sb.append(functionName).append('('); - sb.append(fieldName); - if (functionName != null) sb.append(')'); + value.appendTo(sb); switch (order) { case ASCENDING: sb.append(" ASC"); 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=981636&r1=981635&r2=981636&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:53 2010 @@ -111,8 +111,6 @@ TOKEN: { | <INSERT: "INSERT"> | <RELATION: "RELATION"> | <EXCLUDE: "EXCLUDE"> - | <UPPER: "UPPER"> - | <LOWER: "LOWER"> | <TYPE: "TYPE"> | <TITLE: "TITLE"> | <SET: "SET"> @@ -697,7 +695,7 @@ private List<OrderByItem> OrderByList(): } public OrderByItem OrderByItem(): { - String functionName = null, fieldName = null; + ConstantValue value; OrderByItem.Order ordering = OrderByItem.Order.DEFAULT; OrderByItem.Nulls nulls = OrderByItem.Nulls.DEFAULT; } { @@ -707,18 +705,9 @@ public OrderByItem OrderByItem(): { <PLUS> { ordering = OrderByItem.Order.ASCENDING; } | <MINUS> { ordering = OrderByItem.Order.DESCENDING; } ) - ( - ( <UPPER> | <LOWER> ) { functionName = getToken(0).image; } - <OPEN_PAREN> fieldName=NamePart() <CLOSE_PAREN> - | fieldName=NamePart() - ) + value=ConstantValue() ) - | ( - ( <UPPER> | <LOWER> ) { functionName = getToken(0).image; } - <OPEN_PAREN> fieldName=NamePart() <CLOSE_PAREN> - | fieldName=NamePart() - ) - ( + | value=ConstantValue() ( <DESC> { ordering = OrderByItem.Order.DESCENDING; } | <ASC> { ordering = OrderByItem.Order.ASCENDING; } )? @@ -729,7 +718,7 @@ public OrderByItem OrderByItem(): { | <LAST> { nulls = OrderByItem.Nulls.LAST; } ) )? - { return new OrderByItem(ordering, nulls, functionName, fieldName); } + { return new OrderByItem(ordering, nulls, value); } } 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=981636&r1=981635&r2=981636&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:53 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, 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") + new OrderByItem(OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, new FunctionCall("LOWER", GenericTestCaseBase.<Value>list(new FieldValue(null, "lastName")))), + new OrderByItem(OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, new FieldValue(null, "firstName")), + new OrderByItem(OrderByItem.Order.DESCENDING, OrderByItem.Nulls.DEFAULT, new FieldValue(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=981636&r1=981635&r2=981636&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:53 2010 @@ -55,6 +55,7 @@ public class SelectTest extends GenericT private static final NumberValue<Long> nv1 = NumberValue.valueOf(1); private static final NumberValue<Double> nv2 = NumberValue.valueOf(2D); private static final NumberValue<Long> nv3 = NumberValue.valueOf(3); + private static final FunctionCall fc1 = new FunctionCall("LOWER", GenericTestCaseBase.<Value>list(fv1)); public SelectTest(String name) { super(name); @@ -116,27 +117,26 @@ public class SelectTest extends GenericT fieldDefTest("v5", v5, fv1, null, "partyId", v1, true); } - private static void orderByItemTest(String label, OrderByItem v, OrderByItem.Order order, OrderByItem.Nulls nulls, String functionName, String fieldName, OrderByItem o, boolean matches) throws Exception { + private static void orderByItemTest(String label, OrderByItem v, OrderByItem.Order order, OrderByItem.Nulls nulls, ConstantValue value, 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 + ":value", value, v.getValue()); assertEquals(label + ":parse", v, parser(v).parse_OrderByItem()); basicTest(label, OrderByItem.class, v, o, matches); } public void testOrderByItem() throws Exception { - 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); + OrderByItem v1 = new OrderByItem(OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, fv1); + orderByItemTest("v1", v1, OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, fv1, null, false); + OrderByItem v2 = new OrderByItem(OrderByItem.Order.ASCENDING, OrderByItem.Nulls.FIRST, fv1); + orderByItemTest("v3", v2, OrderByItem.Order.ASCENDING, OrderByItem.Nulls.FIRST, fv1, v1, false); + OrderByItem v3 = new OrderByItem(OrderByItem.Order.DESCENDING, OrderByItem.Nulls.LAST, fv1); + orderByItemTest("v2", v3, OrderByItem.Order.DESCENDING, OrderByItem.Nulls.LAST, fv1, v1, false); + OrderByItem v4 = new OrderByItem(OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, fc1); + orderByItemTest("v4", v4, OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, fc1, v1, false); + OrderByItem v5 = new OrderByItem(OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, fv4); + orderByItemTest("v5", v5, OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, fv4, v1, false); + OrderByItem v6 = new OrderByItem(OrderByItem.Order.DEFAULT, OrderByItem.Nulls.LAST, fv4); + orderByItemTest("v6", v6, OrderByItem.Order.DEFAULT, OrderByItem.Nulls.LAST, fv4, v1, false); } } |
Free forum by Nabble | Edit this page |