|
Author: doogie
Date: Fri Dec 18 05:38:09 2009 New Revision: 892142 URL: http://svn.apache.org/viewvc?rev=892142&view=rev Log: OrderBy is now a list of OrderByItem, instead of String, and support upper and lower functions. Added: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/OrderByItem.java Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/sql/EntityPlanner.java ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj ofbiz/trunk/framework/sql/src/org/ofbiz/sql/SQLSelect.java Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/sql/EntityPlanner.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/sql/EntityPlanner.java?rev=892142&r1=892141&r2=892142&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/sql/EntityPlanner.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/sql/EntityPlanner.java Fri Dec 18 05:38:09 2009 @@ -49,6 +49,7 @@ 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 @@ for (FieldDef fieldDef: selectStatement.getFieldDefs()) { addFieldDef(dve, groupBy, fieldDef.getAlias(), fieldDef); } - return new EntitySelectPlan(dve, plan(selectStatement.getWhereCondition()), plan(selectStatement.getHavingCondition()), selectStatement.getOrderBy()); + List<String> 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) { Added: 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=892142&view=auto ============================================================================== --- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/OrderByItem.java (added) +++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/OrderByItem.java Fri Dec 18 05:38:09 2009 @@ -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; + } +} 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=892142&r1=892141&r2=892142&view=diff ============================================================================== --- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj (original) +++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj Fri Dec 18 05:38:09 2009 @@ -107,6 +107,8 @@ | <INSERT: "INSERT"> | <RELATION: "RELATION"> | <EXCLUDE: "EXCLUDE"> +| <UPPER: "UPPER"> +| <LOWER: "LOWER"> | <TYPE: "TYPE"> | <TITLE: "TITLE"> | <SET: "SET"> @@ -114,6 +116,8 @@ | <SEMI: ";"> | <STAR: "*"> | <COMMA: ","> +| <PLUS: "+"> +| <MINUS: "-"> | <DESC: "DESC"> | <ASC: "ASC"> | <EQUALS: "="> @@ -269,7 +273,8 @@ private SQLSelect Select(): { Integer i; - List<String> orderBy = null, groupBy = null; + List<OrderByItem> orderBy = null; + List<String> groupBy = null; Map<String, FieldDef> fieldDefs = FastMap.newInstance(); List<FieldAll> fieldAlls = FastList.newInstance(); Table table; @@ -644,10 +649,10 @@ } } -private List<String> OrderByList(): +private List<OrderByItem> OrderByList(): { - List<String> orderBy = FastList.newInstance(); - String obi; + List<OrderByItem> orderBy = FastList.newInstance(); + OrderByItem obi; } { obi=OrderByItem() { orderBy.add(obi); } @@ -655,18 +660,27 @@ { return orderBy; } } -private String OrderByItem(): +private OrderByItem OrderByItem(): { - StringBuilder sb = new StringBuilder(); - String n; + String functionName = null, fieldName = null; + boolean descending = false, orderingSet = false; + OrderByItem.Order ordering = OrderByItem.Order.DEFAULT; } { - n=NamePart() { sb.append(n); } ( - <DESC> { sb.append(" DESC"); } - | <ASC> { sb.append(" ASC"); } + <PLUS> { ordering = OrderByItem.Order.ASCENDING; } + | <MINUS> { ordering = OrderByItem.Order.DESCENDING; } )? - { return sb.toString(); } + ( + ( <UPPER> | <LOWER> ) { functionName = getToken(0).image; } + <OPEN_PAREN> fieldName=NamePart() <CLOSE_PAREN> + | fieldName=NamePart() + ) + LOOKAHEAD({!orderingSet}) ( + <DESC> { ordering = OrderByItem.Order.DESCENDING; } + | <ASC> { ordering = OrderByItem.Order.ASCENDING; } + )? + { return new OrderByItem(ordering, functionName, fieldName); } } private Integer Integer(): Modified: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/SQLSelect.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/SQLSelect.java?rev=892142&r1=892141&r2=892142&view=diff ============================================================================== --- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/SQLSelect.java (original) +++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/SQLSelect.java Fri Dec 18 05:38:09 2009 @@ -35,10 +35,10 @@ private final Condition havingCondition; private final int offset; private final int limit; - private final List<String> orderBy; + private final List<OrderByItem> orderBy; private final List<String> groupBy; - public SQLSelect(List<FieldAll> fieldAlls, Map<String, FieldDef> fieldDefs, Table table, List<Relation> relations, Condition whereCondition, Condition havingCondition, List<String> groupBy, List<String> orderBy, int offset, int limit) { + public SQLSelect(List<FieldAll> fieldAlls, Map<String, FieldDef> fieldDefs, Table table, List<Relation> relations, Condition whereCondition, Condition havingCondition, List<String> groupBy, List<OrderByItem> orderBy, int offset, int limit) { this.fieldAlls = fieldAlls; this.fieldDefs = fieldDefs; this.table = table; @@ -79,7 +79,7 @@ return groupBy; } - public List<String> getOrderBy() { + public List<OrderByItem> getOrderBy() { return orderBy; } |
| Free forum by Nabble | Edit this page |
