This is an automated email from the ASF dual-hosted git repository.

doebele pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/empire-db.git


The following commit(s) were added to refs/heads/master by this push:
     new 586fdb4d EMPIREDB-431 DBCommand groupBy ignore Value expressions
586fdb4d is described below

commit 586fdb4d366bd22afec0450af569a71a25918cf8
Author: Rainer Döbele <[email protected]>
AuthorDate: Tue Dec 24 12:00:52 2024 +0100

    EMPIREDB-431
    DBCommand groupBy ignore Value expressions
---
 .../org/apache/empire/samples/db/SampleApp.java    |  3 ++-
 .../main/java/org/apache/empire/db/DBCommand.java  |  6 +++++-
 .../main/java/org/apache/empire/db/DBUtils.java    | 11 +++-------
 .../empire/db/list/DBRecordListFactoryImpl.java    | 25 ++++++++++++++++++++--
 4 files changed, 33 insertions(+), 12 deletions(-)

diff --git 
a/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java
 
b/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java
index 97cdee2c..d79bcef7 100644
--- 
a/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java
+++ 
b/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java
@@ -493,7 +493,8 @@ public class SampleApp
            .orderBy(EMPLOYEE_NAME);
 
         // Add payment of last year using a SUM aggregation
-        cmd.groupBy(cmd.getSelectExpressions());
+        // cmd.groupBy(cmd.getSelectExpressions());
+        cmd.groupAll();
         cmd.select(PAYMENTS_LAST_YEAR);
 
         /*
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBCommand.java 
b/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
index 14c08487..0fd6b1c4 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
@@ -30,6 +30,7 @@ import org.apache.empire.commons.ObjectUtils;
 import org.apache.empire.commons.StringUtils;
 import org.apache.empire.data.DataType;
 import org.apache.empire.db.expr.column.DBAliasExpr;
+import org.apache.empire.db.expr.column.DBValueExpr;
 import org.apache.empire.db.expr.compare.DBCompareAndOrExpr;
 import org.apache.empire.db.expr.compare.DBCompareColExpr;
 import org.apache.empire.db.expr.compare.DBCompareExpr;
@@ -1238,6 +1239,9 @@ public abstract class DBCommand extends DBCommandExpr
         // Unwrap DBAliasExpr only
         if (columnExpr instanceof DBAliasExpr)
             columnExpr = ((DBAliasExpr)columnExpr).unwrap();
+        // Ignore Value expr (added 20241224)
+        if (columnExpr instanceof DBValueExpr)
+            return this;
         // Already present?
         if (groupBy.contains(columnExpr))
             return this;
@@ -1287,7 +1291,7 @@ public abstract class DBCommand extends DBCommandExpr
         // check select expression
         if (select == null)
             return this;
-        // make a group by array
+        // group all columns
         for (DBColumnExpr expr : select)
         {
             if (expr.isAggregate())
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBUtils.java 
b/empire-db/src/main/java/org/apache/empire/db/DBUtils.java
index 38785c09..3c2c1a18 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBUtils.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBUtils.java
@@ -1065,14 +1065,9 @@ public class DBUtils implements DBContextAware
                 R entry = factory.newRecord(rownum, r);
                 if (entry==null)
                     continue;
-                // check
-                if (entry.isValid())
-                {   // add entry
-                    list.add(entry);
-                    rownum++;
-                }
-                else
-                    log.trace("Record {} is not valid thus it will not be 
added to the RecordListQuery.", rownum);
+                // add entry
+                list.add(entry);
+                rownum++;
                 // Decrease count
                 if (maxCount > 0)
                     maxCount--;
diff --git 
a/empire-db/src/main/java/org/apache/empire/db/list/DBRecordListFactoryImpl.java
 
b/empire-db/src/main/java/org/apache/empire/db/list/DBRecordListFactoryImpl.java
index 70192453..f9d7f6db 100644
--- 
a/empire-db/src/main/java/org/apache/empire/db/list/DBRecordListFactoryImpl.java
+++ 
b/empire-db/src/main/java/org/apache/empire/db/list/DBRecordListFactoryImpl.java
@@ -24,10 +24,12 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.empire.commons.ClassUtils;
+import org.apache.empire.db.DBColumn;
 import org.apache.empire.db.DBCommand;
 import org.apache.empire.db.DBContext;
 import org.apache.empire.db.DBRecord;
 import org.apache.empire.db.DBRecordBase;
+import org.apache.empire.db.DBRecordBase.State;
 import org.apache.empire.db.DBRecordBean;
 import org.apache.empire.db.DBRecordData;
 import org.apache.empire.db.DBRowSet;
@@ -79,6 +81,7 @@ public class DBRecordListFactoryImpl<T extends DBRecordBase> 
implements DBRecord
      */
     protected final Constructor<T> constructor;
     protected final DBRowSet rowset;
+    protected final int timestampIndex;
     
     /**
      * Constructs a DBRecordListFactoryImpl based on an DBRecord constructor
@@ -89,6 +92,9 @@ public class DBRecordListFactoryImpl<T extends DBRecordBase> 
implements DBRecord
     {
         this.constructor = constructor;
         this.rowset = rowset;
+        // Timestamp column index
+        DBColumn timestampCol = rowset.getTimestampColumn();
+        this.timestampIndex = (timestampCol!=null ? 
rowset.getColumnIndex(timestampCol) : -1);
     }
     
     /**
@@ -124,7 +130,11 @@ public class DBRecordListFactoryImpl<T extends 
DBRecordBase> implements DBRecord
     @Override
     public T newRecord(int rownum, DBRecordData recData)
     {   try
-        {   // create item
+        {   // detect state
+            State state = detectRecordState(recData);
+            if (state==null)
+                return null; // ignore this record
+            // create item
             T record;
             switch(constructor.getParameterCount())
             {
@@ -140,7 +150,9 @@ public class DBRecordListFactoryImpl<T extends 
DBRecordBase> implements DBRecord
                 log.warn("DBRecordListFactoryImpl rowset ({}) and actual 
record rowset ({}) don't match!", rowset.getName(), 
record.getRowSet().getName());
                 throw new InvalidPropertyException("rowset", 
record.getRowSet());
             }
-            rowset.initRecord(record, recData);
+            // load data
+            if (state!=State.Invalid)
+                rowset.initRecord(record, recData, (state==State.New));
             return record;
         }
         catch (InstantiationException | IllegalAccessException | 
IllegalArgumentException | InvocationTargetException e)
@@ -155,4 +167,13 @@ public class DBRecordListFactoryImpl<T extends 
DBRecordBase> implements DBRecord
         /* Nothing */
     }
     
+    protected State detectRecordState(DBRecordData recData)
+    {
+        // new or existing record?
+        if (timestampIndex>=0 && recData.isNull(timestampIndex))
+            return State.New;
+        // valid
+        return State.Valid;
+    }
+    
 }

Reply via email to