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

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


The following commit(s) were added to refs/heads/version3 by this push:
     new 507b2ac  EMPIREDB-362 DBUtils replace params of type DBCommand with 
DBCommandExpr
507b2ac is described below

commit 507b2ac9d0f01b607ad1b148a5ec8afe6f313f5e
Author: Rainer Döbele <[email protected]>
AuthorDate: Fri Feb 4 23:05:31 2022 +0100

    EMPIREDB-362 DBUtils replace params of type DBCommand with DBCommandExpr
---
 .../java/org/apache/empire/db/DBCombinedCmd.java     | 20 ++++++++++++++++++++
 .../main/java/org/apache/empire/db/DBCommand.java    |  2 ++
 .../java/org/apache/empire/db/DBCommandExpr.java     | 12 ++++++++++++
 .../src/main/java/org/apache/empire/db/DBUtils.java  | 16 ++++++++--------
 .../db/exceptions/CommandWithoutSelectException.java |  4 ++--
 .../org/apache/empire/db/list/DBBeanListFactory.java |  4 ++--
 .../apache/empire/db/list/DBBeanListFactoryImpl.java | 14 ++++++++++----
 7 files changed, 56 insertions(+), 16 deletions(-)

diff --git a/empire-db/src/main/java/org/apache/empire/db/DBCombinedCmd.java 
b/empire-db/src/main/java/org/apache/empire/db/DBCombinedCmd.java
index 0eed3e7..5a58397 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBCombinedCmd.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBCombinedCmd.java
@@ -81,6 +81,26 @@ public class DBCombinedCmd extends DBCommandExpr
         left.addReferencedColumns(list);
         right.addReferencedColumns(list);
     }
+    
+    /**
+     * returns whether or not the command has any select expression 
+     * @return true if the command has any select expression of false otherwise
+     */
+    @Override
+    public boolean hasSelectExpr()
+    {
+        return left.hasSelectExpr();
+    }
+
+    /**
+     * returns whether or not the command has a specific select expression 
+     * @return true if the command contains the given select expression of 
false otherwise
+     */
+    @Override
+    public boolean hasSelectExpr(DBColumnExpr expr)
+    {
+        return left.hasSelectExpr(expr);
+    }
 
     /**
      * Returns all select expressions as unmodifiable list
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 8928be6..08e5fa0 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
@@ -979,6 +979,7 @@ public abstract class DBCommand extends DBCommandExpr
      * returns whether or not the command has any select expression 
      * @return true if the command has any select expression of false otherwise
      */
+    @Override
     public boolean hasSelectExpr()
     {
         return (select!=null && !select.isEmpty());
@@ -988,6 +989,7 @@ public abstract class DBCommand extends DBCommandExpr
      * returns whether or not the command has a specific select expression 
      * @return true if the command contains the given select expression of 
false otherwise
      */
+    @Override
     public boolean hasSelectExpr(DBColumnExpr expr)
     {
         return (select!=null ? (select.indexOf(expr)>=0) : false);
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBCommandExpr.java 
b/empire-db/src/main/java/org/apache/empire/db/DBCommandExpr.java
index e51f445..aae13fe 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBCommandExpr.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBCommandExpr.java
@@ -290,6 +290,18 @@ public abstract class DBCommandExpr extends DBExpr
     }
 
     public abstract boolean isValid();
+    
+    /**
+     * returns whether or not the command has any select expression 
+     * @return true if the command has any select expression of false otherwise
+     */
+    public abstract boolean hasSelectExpr();
+
+    /**
+     * returns whether or not the command has a specific select expression 
+     * @return true if the command contains the given select expression of 
false otherwise
+     */
+    public abstract boolean hasSelectExpr(DBColumnExpr expr);
 
     /**
      * Returns the list of all select expressions as an array
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 ece846f..4fba27e 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
@@ -942,7 +942,7 @@ public class DBUtils implements DBContextAware
      * @param rowset the rowset for which to return the factory 
      * @return the bean factory
      */
-    protected synchronized <T> DBBeanListFactory<T> 
getRowsetBeanListFactory(Class<T> beanType, DBRowSet rowset) 
+    public synchronized <T> DBBeanListFactory<T> 
getRowsetBeanListFactory(Class<T> beanType, DBRowSet rowset) 
     {
         DBBeanListFactory<T> factory = 
DBBeanFactoryCache.getFactoryForType(beanType);
         if (factory==null)
@@ -960,7 +960,7 @@ public class DBUtils implements DBContextAware
      * @param rowset the rowset for which to return the factory 
      * @return the bean factory
      */
-    protected synchronized <T> DBBeanListFactory<T> 
getCommandBeanListFactory(Class<T> beanType, DBCommand cmd) 
+    public synchronized <T> DBBeanListFactory<T> 
getCommandBeanListFactory(Class<T> beanType, DBCommandExpr cmd) 
     {
         DBBeanListFactory<T> factory = 
DBBeanFactoryCache.getFactoryForType(beanType);
         if (factory==null) 
@@ -983,7 +983,7 @@ public class DBUtils implements DBContextAware
      * @param pageSize the maximum number of items to add to the list or -1 
(default) for all
      * @return
      */
-    public <T> List<T> queryBeanList(DBCommand cmd, DBBeanListFactory<T> 
factory, Object parent, int first, int pageSize)
+    public <T> List<T> queryBeanList(DBCommandExpr cmd, DBBeanListFactory<T> 
factory, Object parent, int first, int pageSize)
     {
         List<T> list = null;
         DBReader r = new DBReader(context);
@@ -1056,7 +1056,7 @@ public class DBUtils implements DBContextAware
      * @param parent (optional) the parent bean if any 
      * @return the list of java beans
      */
-    public final <T> List<T> queryBeanList(DBCommand cmd, DBBeanListFactory<T> 
factory, Object parent)
+    public final <T> List<T> queryBeanList(DBCommandExpr cmd, 
DBBeanListFactory<T> factory, Object parent)
     {
         return queryBeanList(cmd, factory, parent, 0, -1);
     }
@@ -1069,7 +1069,7 @@ public class DBUtils implements DBContextAware
      * @param parent (optional) the parent bean if any 
      * @return the list of java beans
      */
-    public <T> List<T> queryBeanList(DBCommand cmd, Class<T> beanType, 
DBRowSet rowset, Object parent)
+    public <T> List<T> queryBeanList(DBCommandExpr cmd, Class<T> beanType, 
DBRowSet rowset, Object parent)
     {
         return queryBeanList(cmd, getRowsetBeanListFactory(beanType, rowset), 
parent, 0, -1);
     }
@@ -1081,7 +1081,7 @@ public class DBUtils implements DBContextAware
      * @param parent (optional) the parent bean if any 
      * @return the list of java beans
      */
-    public <T> List<T> queryBeanList(DBCommand cmd, Class<T> beanType, Object 
parent)
+    public <T> List<T> queryBeanList(DBCommandExpr cmd, Class<T> beanType, 
Object parent)
     {
         return queryBeanList(cmd, getCommandBeanListFactory(beanType, cmd), 
parent);
     }
@@ -1092,7 +1092,7 @@ public class DBUtils implements DBContextAware
      * @param factory the factory to create the bean instance
      * @return the bean instance
      */
-    public <T> T queryBean(DBCommand cmd, DBBeanListFactory<T> factory)
+    public <T> T queryBean(DBCommandExpr cmd, DBBeanListFactory<T> factory)
     {
         DBReader r = new DBReader(context);
         try
@@ -1123,7 +1123,7 @@ public class DBUtils implements DBContextAware
      * @param parent (optional) the parent bean if any 
      * @return the list of java beans
      */
-    public <T> T queryBean(DBCommand cmd, Class<T> beanType)
+    public <T> T queryBean(DBCommandExpr cmd, Class<T> beanType)
     {
         return queryBean(cmd, getCommandBeanListFactory(beanType, cmd));
     }
diff --git 
a/empire-db/src/main/java/org/apache/empire/db/exceptions/CommandWithoutSelectException.java
 
b/empire-db/src/main/java/org/apache/empire/db/exceptions/CommandWithoutSelectException.java
index 6c0cb15..22c5de6 100644
--- 
a/empire-db/src/main/java/org/apache/empire/db/exceptions/CommandWithoutSelectException.java
+++ 
b/empire-db/src/main/java/org/apache/empire/db/exceptions/CommandWithoutSelectException.java
@@ -19,7 +19,7 @@
 package org.apache.empire.db.exceptions;
 
 import org.apache.empire.commons.ErrorType;
-import org.apache.empire.db.DBCommand;
+import org.apache.empire.db.DBCommandExpr;
 import org.apache.empire.exceptions.InvalidArgumentException;
 
 public class CommandWithoutSelectException extends InvalidArgumentException
@@ -28,7 +28,7 @@ public class CommandWithoutSelectException extends 
InvalidArgumentException
     
     public static final ErrorType errorType = new 
ErrorType("error.db.commandWithoutSelect",  "The command statement has no 
select expressions.");
     
-    public CommandWithoutSelectException(DBCommand cmd)
+    public CommandWithoutSelectException(DBCommandExpr cmd)
     {
         super(CommandWithoutSelectException.errorType, null);
     }
diff --git 
a/empire-db/src/main/java/org/apache/empire/db/list/DBBeanListFactory.java 
b/empire-db/src/main/java/org/apache/empire/db/list/DBBeanListFactory.java
index 1200a1d..d4cfcb9 100644
--- a/empire-db/src/main/java/org/apache/empire/db/list/DBBeanListFactory.java
+++ b/empire-db/src/main/java/org/apache/empire/db/list/DBBeanListFactory.java
@@ -20,13 +20,13 @@ package org.apache.empire.db.list;
 
 import java.util.List;
 
-import org.apache.empire.db.DBCommand;
+import org.apache.empire.db.DBCommandExpr;
 import org.apache.empire.db.DBContext;
 import org.apache.empire.db.DBRecordData;
 
 public interface DBBeanListFactory<T extends Object>
 {
-    void prepareQuery(DBCommand cmd, DBContext context);
+    void prepareQuery(DBCommandExpr cmd, DBContext context);
     
     List<T> newList(int capacity);
 
diff --git 
a/empire-db/src/main/java/org/apache/empire/db/list/DBBeanListFactoryImpl.java 
b/empire-db/src/main/java/org/apache/empire/db/list/DBBeanListFactoryImpl.java
index fb011ff..0d286af 100644
--- 
a/empire-db/src/main/java/org/apache/empire/db/list/DBBeanListFactoryImpl.java
+++ 
b/empire-db/src/main/java/org/apache/empire/db/list/DBBeanListFactoryImpl.java
@@ -29,6 +29,7 @@ import org.apache.empire.data.Column;
 import org.apache.empire.db.DBColumn;
 import org.apache.empire.db.DBColumnExpr;
 import org.apache.empire.db.DBCommand;
+import org.apache.empire.db.DBCommandExpr;
 import org.apache.empire.db.DBContext;
 import org.apache.empire.db.DBRecordData;
 import org.apache.empire.db.exceptions.CommandWithoutSelectException;
@@ -158,7 +159,7 @@ public class DBBeanListFactoryImpl<T> implements 
DBBeanListFactory<T>
     }
 
     @Override
-    public void prepareQuery(DBCommand cmd, DBContext context)
+    public void prepareQuery(DBCommandExpr cmd, DBContext context)
     {
         boolean hasSelect = cmd.hasSelectExpr();
         // check if constructor params are selected and add if appropriate
@@ -167,18 +168,23 @@ public class DBBeanListFactoryImpl<T> implements 
DBBeanListFactory<T>
             for (DBColumnExpr expr : constructorParams)
             {
                 if (cmd.hasSelectExpr(expr)==false)
-                    cmd.select(expr);
+                {
+                    if (cmd instanceof DBCommand)
+                        ((DBCommand)cmd).select(expr);
+                    else
+                        throw new InvalidArgumentException("cmd", cmd);
+                }
             }
         }
         // check the rest of the columns, but only if no select is present
-        if (setterColumns!=null && !hasSelect)
+        if (setterColumns!=null && !hasSelect && (cmd instanceof DBCommand))
         {   // select all columns which are not already selected
             for (DBColumnExpr expr : setterColumns)
             {
                 if (constructorParams!=null && 
constructorParams.contains(expr))
                     continue; // already added
                 if (cmd.hasSelectExpr(expr)==false)
-                    cmd.select(expr);
+                    ((DBCommand)cmd).select(expr);
             }
         }
         // still no select ?

Reply via email to