Author: gk
Date: Wed May  8 09:16:31 2024
New Revision: 1917567

URL: http://svn.apache.org/viewvc?rev=1917567&view=rev
Log:
Fix TORQUE-365
Fix changes.xml files

Modified:
    db/torque/trunk/torque-runtime/src/changes/changes.xml
    
db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/util/CountHelper.java
    
db/torque/trunk/torque-runtime/src/test/java/org/apache/torque/criteria/CriteriaTest.java
    db/torque/trunk/torque-templates/src/changes/changes.xml

Modified: db/torque/trunk/torque-runtime/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/db/torque/trunk/torque-runtime/src/changes/changes.xml?rev=1917567&r1=1917566&r2=1917567&view=diff
==============================================================================
--- db/torque/trunk/torque-runtime/src/changes/changes.xml (original)
+++ db/torque/trunk/torque-runtime/src/changes/changes.xml Wed May  8 09:16:31 
2024
@@ -28,6 +28,9 @@
       <action type="update" dev="tv">
        Use Charset where appropriate
       </action>
+       <action type="fix" dev="mwriedt" issue="TORQUE-365">
+       CountHelper doesn't work, when Criteria has "setDisctint()"
+      </action>
      <action type="fix" dev="gk" issue="TORQUE-363">
        Criterion "is Null" fix, Thanks for the report to Max Philipp Wriedt.
       </action>

Modified: 
db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/util/CountHelper.java
URL: 
http://svn.apache.org/viewvc/db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/util/CountHelper.java?rev=1917567&r1=1917566&r2=1917567&view=diff
==============================================================================
--- 
db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/util/CountHelper.java
 (original)
+++ 
db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/util/CountHelper.java
 Wed May  8 09:16:31 2024
@@ -30,6 +30,7 @@ package org.apache.torque.util;
  */
 import java.sql.Connection;
 import java.util.List;
+import java.util.Objects;
 
 import org.apache.torque.Column;
 import org.apache.torque.ColumnImpl;
@@ -199,7 +200,8 @@ public class CountHelper
         UniqueList<String> criteriaSelectModifiers
         = c.getSelectModifiers();
 
-        boolean distinct = isAndSetDistinct(criteriaSelectModifiers);
+        //  COUNT(DISTINCT *) would be not valid SQL, exclude *
+        boolean distinct = isAndSetDistinct(criteriaSelectModifiers) && 
!Objects.equals(columnName, "*");
 
         c.addSelectColumn(new Count(new ColumnImpl(columnName), distinct));
 

Modified: 
db/torque/trunk/torque-runtime/src/test/java/org/apache/torque/criteria/CriteriaTest.java
URL: 
http://svn.apache.org/viewvc/db/torque/trunk/torque-runtime/src/test/java/org/apache/torque/criteria/CriteriaTest.java?rev=1917567&r1=1917566&r2=1917567&view=diff
==============================================================================
--- 
db/torque/trunk/torque-runtime/src/test/java/org/apache/torque/criteria/CriteriaTest.java
 (original)
+++ 
db/torque/trunk/torque-runtime/src/test/java/org/apache/torque/criteria/CriteriaTest.java
 Wed May  8 09:16:31 2024
@@ -27,6 +27,7 @@ import java.util.Date;
 import java.util.GregorianCalendar;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 import org.apache.commons.lang3.SerializationUtils;
 import org.apache.torque.BaseTestCase;
@@ -40,7 +41,9 @@ import org.apache.torque.map.TableMap;
 import org.apache.torque.sql.OrderBy;
 import org.apache.torque.sql.Query;
 import org.apache.torque.sql.SqlBuilder;
+import org.apache.torque.util.CountHelper;
 import org.apache.torque.util.UniqueList;
+import org.apache.torque.util.functions.Count;
 
 import static org.junit.jupiter.api.Assertions.assertNotSame;
 import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -690,6 +693,37 @@ public class CriteriaTest extends BaseTe
         = result.getPreparedStatementReplacements();
         assertEquals(0, preparedStatementReplacements.size());
     }
+    
+    @Test
+    public void testDistinctCount() throws TorqueException
+    {
+        // distinct without count
+        c.where(new ColumnImpl("TABLE", "COLUMN"),SqlEnum.ISNOTNULL );
+        c.addSelectColumn(new ColumnImpl("TABLE", "COLUMN"));
+        c.setDistinct();
+        
+        Query result = SqlBuilder.buildQuery(c);
+        assertEquals(
+                "SELECT DISTINCT TABLE.COLUMN FROM TABLE WHERE "
+                        + "TABLE.COLUMN IS NOT NULL",
+                        result.toString());
+        
+        // now check distinct with count
+        // use case would be:
+        // CountHelper ch = new CountHelper();
+        // int count = ch.count( c ) -> sets columnName to *
+        
+        /* Clear the select columns. */
+        c.getSelectColumns().clear();
+        //  COUNT(DISTINCT *) would be not valid SQL, exclude if columnName = *
+        boolean distinct = false;
+        c.addSelectColumn(new Count(new ColumnImpl("*"), distinct));
+        Query result2 = SqlBuilder.buildQuery(c);
+        assertEquals(
+                "SELECT DISTINCT COUNT(*) FROM TABLE WHERE "
+                        + "TABLE.COLUMN IS NOT NULL",
+                        result2.toString());
+    }
 
     @Test
     public void testAndCurrentTime() throws TorqueException

Modified: db/torque/trunk/torque-templates/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/db/torque/trunk/torque-templates/src/changes/changes.xml?rev=1917567&r1=1917566&r2=1917567&view=diff
==============================================================================
--- db/torque/trunk/torque-templates/src/changes/changes.xml (original)
+++ db/torque/trunk/torque-templates/src/changes/changes.xml Wed May  8 
09:16:31 2024
@@ -24,7 +24,7 @@
 
   <body>
     <release version="5.2-SNAPSHOT" date="in SVN">   
-     <action type="fix" issue="TORQUE-364" dev="mwriedt" date="2024-05-06">
+     <action type="fix" issue="TORQUE-364" dev="mwriedt" date="2024-05-06" 
due-to="Rene Fabian Reichow">
       Improve RecordMapper on many columns in table Add generated static 
variable for sql expressions, add set to optimize loop in processRow method.
     </action>
     <action type="update" dev="gk" date="2024-04-28">



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscr...@db.apache.org
For additional commands, e-mail: torque-dev-h...@db.apache.org

Reply via email to