diff --git a/framework/entity/src/org/ofbiz/entity/sql/EntityPlanner.java b/framework/entity/src/org/ofbiz/entity/sql/EntityPlanner.java index 129850703f..f9032644ad 100644 --- a/framework/entity/src/org/ofbiz/entity/sql/EntityPlanner.java +++ b/framework/entity/src/org/ofbiz/entity/sql/EntityPlanner.java @@ -49,6 +49,7 @@ import org.ofbiz.sql.Joiner; import org.ofbiz.sql.KeyMap; import org.ofbiz.sql.MathValue; import org.ofbiz.sql.NumberValue; +import org.ofbiz.sql.OrderByItem; import org.ofbiz.sql.Planner; import org.ofbiz.sql.MathValue; import org.ofbiz.sql.Relation; @@ -94,7 +95,16 @@ public class EntityPlanner extends Planner orderBy; + if (selectStatement.getOrderBy() == null) { + orderBy = null; + } else { + orderBy = FastList.newInstance(); + for (OrderByItem orderByItem: selectStatement.getOrderBy()) { + orderBy.add(orderByItem.toString()); + } + } + return new EntitySelectPlan(dve, plan(selectStatement.getWhereCondition()), plan(selectStatement.getHavingCondition()), orderBy); } public EntityUpdatePlan planUpdate(SQLUpdate updateStatement) { diff --git a/framework/sql/src/org/ofbiz/sql/OrderByItem.java b/framework/sql/src/org/ofbiz/sql/OrderByItem.java new file mode 100644 index 0000000000..84ff5ea1ec --- /dev/null +++ b/framework/sql/src/org/ofbiz/sql/OrderByItem.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.ofbiz.sql; + +public final class OrderByItem extends Atom { + enum Order { DEFAULT, ASCENDING, DESCENDING }; + + private final Order order; + private final String functionName; + private final String fieldName; + + public OrderByItem(Order order, String functionName, String fieldName) { + this.order = order; + this.functionName = functionName; + this.fieldName = fieldName; + } + + public final Order getOrder() { + return order; + } + + public final String getFunctionName() { + return functionName; + } + + public final String getFieldName() { + return fieldName; + } + + public StringBuilder appendTo(StringBuilder sb) { + if (functionName != null) sb.append(functionName).append('('); + sb.append(fieldName); + if (functionName != null) sb.append(')'); + switch (order) { + case ASCENDING: + sb.append(" ASC"); + break; + case DESCENDING: + sb.append(" DESC"); + break; + } + return sb; + } +} diff --git a/framework/sql/src/org/ofbiz/sql/Parser.jj b/framework/sql/src/org/ofbiz/sql/Parser.jj index fc8812738d..4a8b362388 100644 --- a/framework/sql/src/org/ofbiz/sql/Parser.jj +++ b/framework/sql/src/org/ofbiz/sql/Parser.jj @@ -107,6 +107,8 @@ TOKEN: { | | | +| +| | | | @@ -114,6 +116,8 @@ TOKEN: { | | | +| +| | | | { sb.append(" ASC"); } + { ordering = OrderByItem.Order.ASCENDING; } + | { ordering = OrderByItem.Order.DESCENDING; } )? - { return sb.toString(); } + ( + ( | ) { functionName = getToken(0).image; } + fieldName=NamePart() + | fieldName=NamePart() + ) + LOOKAHEAD({!orderingSet}) ( + { ordering = OrderByItem.Order.DESCENDING; } + | { ordering = OrderByItem.Order.ASCENDING; } + )? + { return new OrderByItem(ordering, functionName, fieldName); } } private Integer Integer(): diff --git a/framework/sql/src/org/ofbiz/sql/SQLSelect.java b/framework/sql/src/org/ofbiz/sql/SQLSelect.java index 86596c0343..d5b1a0b1a2 100644 --- a/framework/sql/src/org/ofbiz/sql/SQLSelect.java +++ b/framework/sql/src/org/ofbiz/sql/SQLSelect.java @@ -35,10 +35,10 @@ public final class SQLSelect extends SQLStatement implements InsertSo private final Condition havingCondition; private final int offset; private final int limit; - private final List orderBy; + private final List orderBy; private final List groupBy; - public SQLSelect(List fieldAlls, Map fieldDefs, Table table, List relations, Condition whereCondition, Condition havingCondition, List groupBy, List orderBy, int offset, int limit) { + public SQLSelect(List fieldAlls, Map fieldDefs, Table table, List relations, Condition whereCondition, Condition havingCondition, List groupBy, List orderBy, int offset, int limit) { this.fieldAlls = fieldAlls; this.fieldDefs = fieldDefs; this.table = table; @@ -79,7 +79,7 @@ public final class SQLSelect extends SQLStatement implements InsertSo return groupBy; } - public List getOrderBy() { + public List getOrderBy() { return orderBy; }