Author: jleroux
Date: Mon Aug 7 10:47:51 2017 New Revision: 1804321 URL: http://svn.apache.org/viewvc?rev=1804321&view=rev Log: "Applied BY HAND fix from trunk framework for revision: 1804319 " ------------------------------------------------------------------------ r1804319 | jleroux | 2017-08-07 12:40:32 +0200 (lun. 07 août 2017) | 20 lignes 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) ------------------------------------------------------------------------ Modified: ofbiz/branches/release15.12/ (props changed) ofbiz/branches/release15.12/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java Propchange: ofbiz/branches/release15.12/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Mon Aug 7 10:47:51 2017 @@ -9,5 +9,5 @@ /ofbiz/branches/json-integration-refactoring:1634077-1635900 /ofbiz/branches/multitenant20100310:921280-927264 /ofbiz/branches/release13.07:1547657 -/ofbiz/ofbiz-framework/trunk:1786214,1786525,1787176,1789665,1789863,1789874,1791342,1791634,1796262,1798803,1798808,1799327,1800250 +/ofbiz/ofbiz-framework/trunk:1786214,1786525,1787176,1789665,1789863,1789874,1791342,1791634,1796262,1798803,1798808,1799327,1800250,1804319 /ofbiz/trunk:1722712,1723007,1723248,1724402,1724411,1724566,1724689,1724763,1724916,1724918,1724925,1724930,1724940,1724943,1724946,1724951,1724957,1724975,1724978,1725006,1725217,1725257,1725561,1725574,1726388,1726486,1726493,1726828,1727894,1728398,1728411,1729005,1729078,1729609,1729809,1730035,1730456,1730735-1730736,1730747,1730758,1730882,1730889,1731382,1731396,1732454,1732570,1732721,1733951,1733956,1734246,1734269,1734276,1734912,1734918,1735021,1735244,1735385,1735398,1735569,1735731,1735734,1735750,1735753,1735756,1735759,1735773,1736083,1736087,1736272,1736434,1736628,1736851,1736854,1736890,1737156,1737440,1738235,1738303,1738407,1738902,1739438,1739448,1739571,1740008,1740442,1740629,1741146,1741563,1741684,1741925,1741930,1741960,1742018,1742097,1742103,1742712,1742737,1742741,1743025,1743027,1743230,1743411-1743412,1743656,1743937,1744117,1744198,1744396,1744662,1744768,1744773,1744873,1744911,1745111,1745264,1745428,1745438,1745573,1745577,1745592,1745751,1746228, 1746422,1746459,1746524,1746527,1746536,1746601,1746676,1746714,1746755,1746805,1746832,1746890,1747223,1747349,1747402,1747498,1747639,1747642,1747646,1747650,1747661,1747956,1747959,1747963,1748121,1748133,1748206,1748218,1748223,1748260,1748357,1748394,1748401,1748543,1748559,1748628,1748689,1748693,1748703,1748837,1748854,1748907,1748925,1748929,1748944,1748972,1749026,1749083,1749092,1749111,1749220,1749282,1749519,1749525,1749543,1749546,1749552,1749704,1750045,1750166,1750336,1750555,1750624,1750651,1750922,1750925,1750940,1750959,1750964,1751030,1751042,1751357,1751362,1751423,1752928,1752933,1752941,1752947,1752954,1752956,1752966,1753834,1757963,1757968,1757992,1758017,1758089,1758143,1758280,1759002,1759005,1759014,1759024,1759678,1759941,1760406,1761127,1761170,1761179,1761492,1761618,1761678,1761978,1762331,1762738,1765017,1765038,1765104,1766074,1770481,1772346,1775441,1779724,1782520 Modified: ofbiz/branches/release15.12/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release15.12/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java?rev=1804321&r1=1804320&r2=1804321&view=diff ============================================================================== --- ofbiz/branches/release15.12/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java (original) +++ ofbiz/branches/release15.12/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java Mon Aug 7 10:47:51 2017 @@ -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); |
Free forum by Nabble | Edit this page |