svn commit: r897605 - in /ofbiz/trunk/framework/entity/src/org/ofbiz/entity: datasource/GenericDAO.java util/EntityListIterator.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r897605 - in /ofbiz/trunk/framework/entity/src/org/ofbiz/entity: datasource/GenericDAO.java util/EntityListIterator.java

jonesde
Author: jonesde
Date: Sun Jan 10 09:16:47 2010
New Revision: 897605

URL: http://svn.apache.org/viewvc?rev=897605&view=rev
Log:
Small improvement to the EntityListIterator count total results code when distinct is used and partial fields are selected; note that this assumes the most unique field is first in the list since doing distinct on multiple fields in this way does not seem to work

Modified:
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityListIterator.java

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java?rev=897605&r1=897604&r2=897605&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java Sun Jan 10 09:16:47 2010
@@ -969,6 +969,10 @@
     }
 
     public long selectCountByCondition(ModelEntity modelEntity, EntityCondition whereEntityCondition, EntityCondition havingEntityCondition, EntityFindOptions findOptions) throws GenericEntityException {
+        return selectCountByCondition(modelEntity, whereEntityCondition, havingEntityCondition, null, findOptions);
+    }
+    
+    public long selectCountByCondition(ModelEntity modelEntity, EntityCondition whereEntityCondition, EntityCondition havingEntityCondition, List<ModelField> selectFields, EntityFindOptions findOptions) throws GenericEntityException {
         if (modelEntity == null) {
             return 0;
         }
@@ -1007,7 +1011,15 @@
         }
 
         if (findOptions.getDistinct()) {
-            sqlBuffer.append("DISTINCT COUNT(*) ");
+            if (selectFields != null && selectFields.size() > 0) {
+                sqlBuffer.append("COUNT(DISTINCT ");
+                // this only seems to support a single column, which is not desirable but seems a lot better than no columns or in certain cases all columns
+                sqlBuffer.append(selectFields.get(0).getColName());
+                // sqlBuffer.append(modelEntity.colNameString(selectFields, ", ", "", datasourceInfo.aliasViews));
+                sqlBuffer.append(")");
+            } else {
+                sqlBuffer.append("COUNT(DISTINCT *) ");
+            }
         } else {
             // NOTE DEJ20080701 Changed from COUNT(*) to COUNT(1) to improve performance, and should get the same results at least when there is no DISTINCT
             sqlBuffer.append("COUNT(1) ");

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityListIterator.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityListIterator.java?rev=897605&r1=897604&r2=897605&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityListIterator.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityListIterator.java Sun Jan 10 09:16:47 2010
@@ -512,7 +512,7 @@
                     efo = new EntityFindOptions();
                     efo.setDistinct(distinctQuery);
                 }
-                resultSize = (int) genericDAO.selectCountByCondition(modelEntity, whereCondition, havingCondition, efo);
+                resultSize = (int) genericDAO.selectCountByCondition(modelEntity, whereCondition, havingCondition, selectFields, efo);
             }
             return resultSize;
         } else if (this.last()) {


Reply | Threaded
Open this post in threaded view
|

Re: svn commit: r897605 - in /ofbiz/trunk/framework/entity/src/org/ofbiz/entity: datasource/GenericDAO.java util/EntityListIterator.java

rohit
Hi,

I don't think this commit is compatible with older database created by ofbiz.

For eg. if you update the code without recreating the ofbiz database you get error as in

http://n4.nabble.com/Error-in-product-search-result-when-using-multiple-keywords-td1012687.html#a1012687

I had to reverse this commit to remove the error.

I guess there might be a better way to do this than to drop and recreate the tables for this commit to work.

Rohit
 
jonesde wrote
Author: jonesde
Date: Sun Jan 10 09:16:47 2010
New Revision: 897605

URL: http://svn.apache.org/viewvc?rev=897605&view=rev
Log:
Small improvement to the EntityListIterator count total results code when distinct is used and partial fields are selected; note that this assumes the most unique field is first in the list since doing distinct on multiple fields in this way does not seem to work

Modified:
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityListIterator.java

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java?rev=897605&r1=897604&r2=897605&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java Sun Jan 10 09:16:47 2010
@@ -969,6 +969,10 @@
     }
 
     public long selectCountByCondition(ModelEntity modelEntity, EntityCondition whereEntityCondition, EntityCondition havingEntityCondition, EntityFindOptions findOptions) throws GenericEntityException {
+        return selectCountByCondition(modelEntity, whereEntityCondition, havingEntityCondition, null, findOptions);
+    }
+    
+    public long selectCountByCondition(ModelEntity modelEntity, EntityCondition whereEntityCondition, EntityCondition havingEntityCondition, List<ModelField> selectFields, EntityFindOptions findOptions) throws GenericEntityException {
         if (modelEntity == null) {
             return 0;
         }
@@ -1007,7 +1011,15 @@
         }
 
         if (findOptions.getDistinct()) {
-            sqlBuffer.append("DISTINCT COUNT(*) ");
+            if (selectFields != null && selectFields.size() > 0) {
+                sqlBuffer.append("COUNT(DISTINCT ");
+                // this only seems to support a single column, which is not desirable but seems a lot better than no columns or in certain cases all columns
+                sqlBuffer.append(selectFields.get(0).getColName());
+                // sqlBuffer.append(modelEntity.colNameString(selectFields, ", ", "", datasourceInfo.aliasViews));
+                sqlBuffer.append(")");
+            } else {
+                sqlBuffer.append("COUNT(DISTINCT *) ");
+            }
         } else {
             // NOTE DEJ20080701 Changed from COUNT(*) to COUNT(1) to improve performance, and should get the same results at least when there is no DISTINCT
             sqlBuffer.append("COUNT(1) ");

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityListIterator.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityListIterator.java?rev=897605&r1=897604&r2=897605&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityListIterator.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityListIterator.java Sun Jan 10 09:16:47 2010
@@ -512,7 +512,7 @@
                     efo = new EntityFindOptions();
                     efo.setDistinct(distinctQuery);
                 }
-                resultSize = (int) genericDAO.selectCountByCondition(modelEntity, whereCondition, havingCondition, efo);
+                resultSize = (int) genericDAO.selectCountByCondition(modelEntity, whereCondition, havingCondition, selectFields, efo);
             }
             return resultSize;
         } else if (this.last()) {