From 82ce693f5a1759d999ecaacf1bbb582e09e5d433 Mon Sep 17 00:00:00 2001 From: Jacques Le Roux Date: Mon, 7 Aug 2017 10:40:32 +0000 Subject: [PATCH] Fixed: Bug SQL Count Distinct command in GenericDAO.java (OFBIZ-5701) jleroux: Kieuanhvu's explanation was not totally clear. So I rather provide Renuka Srishti's at OFBIZ-9428 "getResultsSizeAfterPartialList() return wrong count with distinct() for View Entity" Here is the code sample to test the issue: EntityListIterator productAssocListItr = null; productAssocListItr = from("ProductAndAssoc").distinct().queryIterator(); productAssocListSize = productAssocListItr.getResultsSizeAfterPartialList(); productAssocListSize will differ from the actual distinct records in the ProductAndAssoc View Entity. This issue exists because it gives distinct records on the basis of the first column in the table. Thanks: Kieuanhvu for the patch, Renuka Srishti for a clear explanation and a simple way to test (in a groovy for me) git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1804319 13f79535-47bb-0310-9956-ffa450edef68 --- .../ofbiz/entity/datasource/GenericDAO.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/framework/entity/src/main/java/org/apache/ofbiz/entity/datasource/GenericDAO.java b/framework/entity/src/main/java/org/apache/ofbiz/entity/datasource/GenericDAO.java index ae8a8ea72a..580438f310 100644 --- a/framework/entity/src/main/java/org/apache/ofbiz/entity/datasource/GenericDAO.java +++ b/framework/entity/src/main/java/org/apache/ofbiz/entity/datasource/GenericDAO.java @@ -1024,6 +1024,7 @@ public class GenericDAO { } boolean isGroupBy = false; + boolean isCountGroup = false; ModelViewEntity modelViewEntity = null; if (modelEntity instanceof ModelViewEntity) { modelViewEntity = (ModelViewEntity) modelEntity; @@ -1054,11 +1055,20 @@ public class GenericDAO { // if the field has a function already we don't want to count just it, would be meaningless sqlBuffer.append("COUNT(DISTINCT *) "); } else { - 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(firstSelectField.getColValue()); - // sqlBuffer.append(modelEntity.colNameString(selectFields, ", ", "", datasource.aliasViews)); - sqlBuffer.append(")"); + isCountGroup = true; + StringBuilder sqlBufferTMP = new StringBuilder("SELECT COUNT(*) FROM("); + sqlBuffer.append("DISTINCT "); + for (int i = 0; i < selectFields.size() - 1; i++) { + ModelViewEntity.ModelAlias tmpMA = modelViewEntity != null ? modelViewEntity.getAlias(selectFields.get(i).getName()) : null; + if (tmpMA != null && !tmpMA.getColAlias().isEmpty()) { + sqlBuffer.append(selectFields.get(i).getColValue() + " as " + tmpMA.getColAlias() + ","); + } else { + sqlBuffer.append(selectFields.get(i).getColValue() + ","); + } + } + sqlBuffer.append(selectFields.get(selectFields.size() - 1).getColValue()); + sqlBufferTMP.append(sqlBuffer); + sqlBuffer = sqlBufferTMP; } } else { sqlBuffer.append("COUNT(DISTINCT *) "); @@ -1098,6 +1108,9 @@ public class GenericDAO { if (isGroupBy) { sqlBuffer.append(") TEMP_NAME"); } + if (isCountGroup) { + sqlBuffer.append(") TEMP_COUNT_NAME"); + } String sql = sqlBuffer.toString(); if (Debug.verboseOn()) Debug.logVerbose("Count select sql: " + sql, module);