Author: doogie
Date: Fri Nov 13 04:08:59 2009
New Revision: 835731

URL: http://svn.apache.org/viewvc?rev=835731&view=rev
Log:
Implement ORDER BY and GROUP BY.

Modified:
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/sql/Parser.jjt
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/sql/SQLSelect.java

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/sql/Parser.jjt
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/sql/Parser.jjt?rev=835731&r1=835730&r2=835731&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/sql/Parser.jjt (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/sql/Parser.jjt Fri Nov 13 
04:08:59 2009
@@ -38,8 +38,11 @@
 import org.ofbiz.entity.condition.EntityConditionValue;
 import org.ofbiz.entity.condition.EntityFieldValue;
 import org.ofbiz.entity.condition.EntityOperator;
+import org.ofbiz.entity.condition.OrderByItem;
+import org.ofbiz.entity.condition.OrderByList;
 import org.ofbiz.entity.model.DynamicViewEntity;
 import org.ofbiz.entity.model.ModelKeyMap;
+import org.ofbiz.entity.model.ModelViewEntity.ModelAlias;
 
 public class Parser {
 }
@@ -85,6 +88,8 @@
 |      <SEMI: ";">
 |      <STAR: "*">
 |      <COMMA: ",">
+|      <DESC: "DESC">
+|      <ASC: "ASC">
 |      <START_DQUOTE: "\""> { pushState(IN_DQUOTE); }
 |      <START_SQUOTE: "'"> { pushState(IN_SQUOTE); }
 |      <INTEGER:
@@ -144,14 +149,16 @@
        int i;
        DynamicViewEntity dve = new DynamicViewEntity();
        jjtThis.setDynamicViewEntity(dve);
+       List<String> fieldList;
+       List<String> orderBy;
 }
 {
        <SELECT> FieldDefs(dve)
        <FROM> Table(dve)
        ( <WHERE> condition=ConditionExpression() { 
jjtThis.setWhereCondition(condition); } )?
        ( <HAVING> condition=ConditionExpression() { 
jjtThis.setHavingCondition(condition); } )?
-       ( <GROUP> <BY> FieldList() )?
-       ( <ORDER> <BY> FieldList() )?
+       ( <GROUP> <BY> fieldList=FieldList() { dve.setGroupBy(fieldList); } )?
+       ( <ORDER> <BY> orderBy=OrderByList() { jjtThis.setOrderBy(orderBy); } )?
        ( <OFFSET> i=Integer() { jjtThis.setOffset(i); } )?
        ( <LIMIT> i=Integer() { jjtThis.setLimit(i); } )?
        <SEMI>
@@ -251,6 +258,7 @@
        <LEFT> <JOIN> { return Boolean.TRUE; }
 |      <JOIN> { return Boolean.FALSE; }
 }
+
 private void FieldDefs(DynamicViewEntity dve) #void:
 {}
 {
@@ -315,21 +323,50 @@
        { return sb.toString(); }
 }
 
-private void FieldList():
-{}
+private List<String> FieldList() #void:
 {
-       FieldUse() ( <COMMA> FieldUse() )*
+       List<String> list = FastList.newInstance();
+       String n;
+}
+{
+       n=NamePart() { list.add(n); }
+       ( <COMMA> n=NamePart() { list.add(n); } )*
+       { return list; }
 }
 
-private List<String> FieldUse() #void:
+private EntityConditionValue FieldUse() #void:
 {
-       List<String> list = FastList.newInstance();
-       String s;
+       String tableAlias = null, fieldName, s;
 }
 {
-       s=NamePart() { list.add(s); }
-       ( <PERIOD> s=NamePart() { list.add(s); } )?
-       { return list; }
+       s=NamePart() { fieldName = s; }
+       ( <PERIOD> s=NamePart() { tableAlias = fieldName; fieldName = s; } )?
+       { return EntityFieldValue.makeFieldValue(fieldName, tableAlias, null, 
null); }
+}
+
+private List<String> OrderByList() #void:
+{
+       List<String> orderBy = FastList.newInstance();
+       String obi;
+}
+{
+       obi=OrderByItem() { orderBy.add(obi); }
+       ( <COMMA> obi=OrderByItem() { orderBy.add(obi); } )*
+       { return orderBy; }
+}
+
+private String OrderByItem() #void:
+{
+       StringBuilder sb = new StringBuilder();
+       String n;
+}
+{
+       n=NamePart() { sb.append(n); }
+       (
+               <DESC> { sb.append(" DESC"); } 
+               | <ASC> { sb.append(" ASC"); }
+       )?
+       { return sb.toString(); }
 }
 
 private Integer Integer() #void:
@@ -351,15 +388,10 @@
 {
        EntityConditionValue ecv;
        String s;
-       List<String> fieldUse;
        int i;
 }
 {
-       fieldUse=FieldUse() {
-               if (fieldUse.size() == 1) return 
EntityFieldValue.makeFieldValue(fieldUse.get(0));
-               if (fieldUse.size() == 2) return 
EntityFieldValue.makeFieldValue(fieldUse.get(1), fieldUse.get(1), null, null);
-               return null;
-}
+       ecv=FieldUse() { return ecv; }
 |      i=Integer() { return i; }
 |      s=SQuoted() { return s; }
 }

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/sql/SQLSelect.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/sql/SQLSelect.java?rev=835731&r1=835730&r2=835731&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/sql/SQLSelect.java 
(original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/sql/SQLSelect.java Fri 
Nov 13 04:08:59 2009
@@ -18,10 +18,17 @@
  */
 package org.ofbiz.entity.sql;
 
+import java.util.List;
+import java.util.Map;
+
+import javolution.util.FastList;
+import javolution.util.FastMap;
+
 import org.ofbiz.entity.Delegator;
 import org.ofbiz.entity.GenericEntityException;
 import org.ofbiz.entity.condition.EntityCondition;
 import org.ofbiz.entity.model.DynamicViewEntity;
+import org.ofbiz.entity.model.ModelKeyMap;
 import org.ofbiz.entity.util.EntityListIterator;
 
 public class SQLSelect extends SimpleNode {
@@ -30,6 +37,7 @@
     private EntityCondition havingCondition;
     private int offset = -1;
     private int limit = -1;
+    private List<String> orderBy;
 
     public SQLSelect(int id) {
         super(id);
@@ -46,7 +54,7 @@
     }
 
     public EntityListIterator getEntityListIterator(Delegator delegator) 
throws GenericEntityException {
-        return delegator.findListIteratorByCondition(dve, whereCondition, 
havingCondition, null, null, null);
+        return delegator.findListIteratorByCondition(dve, whereCondition, 
havingCondition, null, orderBy, null);
     }
 
     void setDynamicViewEntity(DynamicViewEntity dve) {
@@ -73,6 +81,14 @@
         return havingCondition;
     }
 
+    void setOrderBy(List<String> orderBy) {
+        this.orderBy = orderBy;
+    }
+
+    public List<String> getOrderBy() {
+        return orderBy;
+    }
+
     void setOffset(int offset) {
         this.offset = offset;
     }


Reply via email to