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 78b6d12  EMPIREDB-368 queryBeans optimization
78b6d12 is described below

commit 78b6d12a8899bf3fb272b3e99c948730f1ba8d8a
Author: Rainer Döbele <[email protected]>
AuthorDate: Tue Feb 1 11:26:55 2022 +0100

    EMPIREDB-368 queryBeans optimization
---
 .../org/apache/empire/samples/db/SampleApp.java    |   7 +-
 .../apache/empire/samples/db/beans/Department.java |   2 +-
 .../main/java/org/apache/empire/db/DBReader.java   |   8 +-
 .../main/java/org/apache/empire/db/DBRecord.java   |   8 +-
 .../java/org/apache/empire/db/DBRecordData.java    |   4 +-
 .../main/java/org/apache/empire/db/DBUtils.java    | 188 +++++++++++++--------
 .../exceptions/CommandWithoutSelectException.java} |  75 ++++----
 .../exceptions/InvalidArgumentException.java       |   5 +
 8 files changed, 171 insertions(+), 126 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 15ef03c..469ba31 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
@@ -26,6 +26,7 @@ import java.time.LocalDate;
 import java.util.List;
 
 import org.apache.empire.commons.StringUtils;
+import org.apache.empire.data.bean.BeanResult;
 import org.apache.empire.data.list.DataListEntry;
 import org.apache.empire.db.DBColumnExpr;
 import org.apache.empire.db.DBCommand;
@@ -723,13 +724,13 @@ public class SampleApp
                
                System.out.println(emp.toString());
            }
-
+           
            // load department
            Department department = 
context.getUtils().queryBean(Department.class, db.DEPARTMENTS.NAME.is("Sales"));
            Payment first = 
department.getEmployees().get(0).getPayments().get(0);
            log.info("First payment amount is {}", first.getAmount());
-           /*
-        // Query all males
+
+           // Query all males
            BeanResult<Employee> result = new 
BeanResult<Employee>(Employee.class, EMP);
         result.getCommand().where(EMP.GENDER.is(Gender.M));
            result.fetch(context);
diff --git 
a/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/beans/Department.java
 
b/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/beans/Department.java
index a9b8810..64402ef 100644
--- 
a/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/beans/Department.java
+++ 
b/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/beans/Department.java
@@ -69,7 +69,7 @@ public class Department implements Bean<SampleDB>
         DBCommand cmd = db.createCommand();
         cmd.where(db.EMPLOYEES.DEPARTMENT_ID.is(this.id));
         cmd.orderBy(db.EMPLOYEES.FIRSTNAME, db.EMPLOYEES.LASTNAME);
-        employees = context.getUtils().queryBeanList(cmd, Employee.class, 
this);
+        employees = context.getUtils().queryBeanList(cmd, Employee.class, 
db.EMPLOYEES, this);
     }
     
 }
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBReader.java 
b/empire-db/src/main/java/org/apache/empire/db/DBReader.java
index 914d4fc..4f6875e 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBReader.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBReader.java
@@ -744,7 +744,7 @@ public class DBReader extends DBRecordData implements 
Closeable
      * @return the number of column descriptions added to the Element
      */
     @Override
-    public int getXmlMeta(Element parent)
+    public int addXmlMeta(Element parent)
     {
         if (columns == null)
             throw new ObjectNotValidException(this);
@@ -762,7 +762,7 @@ public class DBReader extends DBRecordData implements 
Closeable
      * @return the number of row values added to the element
      */
     @Override
-    public int getXmlData(Element parent)
+    public int addXmlData(Element parent)
     {
         if (rset == null)
             throw new ObjectNotValidException(this);
@@ -802,7 +802,7 @@ public class DBReader extends DBRecordData implements 
Closeable
         String rowElementName = getXmlDictionary().getRowElementName();
         while (moveNext())
         {
-            getXmlData(XMLUtil.addElement(parent, rowElementName));
+            addXmlData(XMLUtil.addElement(parent, rowElementName));
             count++;
         }
         return count;
@@ -831,7 +831,7 @@ public class DBReader extends DBRecordData implements 
Closeable
         String rowsetElementName = getXmlDictionary().getRowSetElementName();
         Element root = XMLUtil.createDocument(rowsetElementName);
         // Add Field Description
-        getXmlMeta(root);
+        addXmlMeta(root);
         // Add row rset
         addRows(root);
         // return Document
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBRecord.java 
b/empire-db/src/main/java/org/apache/empire/db/DBRecord.java
index e02f937..25e7810 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBRecord.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBRecord.java
@@ -999,7 +999,7 @@ public class DBRecord extends DBRecordData implements 
Record, Cloneable, Seriali
      * @return the number of column descriptions added to the element
      */
     @Override
-    public int getXmlMeta(Element parent)
+    public int addXmlMeta(Element parent)
     {
         if (!isValid())
             throw new ObjectNotValidException(this);
@@ -1024,7 +1024,7 @@ public class DBRecord extends DBRecordData implements 
Record, Cloneable, Seriali
      * @return the number of row values added to the element
      */
     @Override
-    public int getXmlData(Element parent)
+    public int addXmlData(Element parent)
     {
         if (!isValid())
             throw new ObjectNotValidException(this);
@@ -1086,9 +1086,9 @@ public class DBRecord extends DBRecordData implements 
Record, Cloneable, Seriali
         if (rowset.getName() != null)
             root.setAttribute("name", rowset.getName());
         // Add Field Description
-        if (getXmlMeta(root)>0)
+        if (addXmlMeta(root)>0)
         {   // Add row Values
-            getXmlData(XMLUtil.addElement(root, xmlDic.getRowElementName()));
+            addXmlData(XMLUtil.addElement(root, xmlDic.getRowElementName()));
         }
         // return Document
         return root.getOwnerDocument();
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java 
b/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java
index d2b6865..91e3dd4 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java
@@ -76,8 +76,8 @@ public abstract class DBRecordData extends DBObject
     public abstract ColumnExpr getColumnExpr(int i);
 
     // xml
-    public abstract int getXmlMeta(Element parent);
-    public abstract int getXmlData(Element parent);
+    public abstract int addXmlMeta(Element parent);
+    public abstract int addXmlData(Element parent);
     public abstract Document getXmlDocument();
 
     // others
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 07baca1..709d1c5 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
@@ -17,6 +17,7 @@ import org.apache.empire.data.list.DataListFactoryImpl;
 import org.apache.empire.data.list.DataListHead;
 import org.apache.empire.db.context.DBContextAware;
 import org.apache.empire.db.exceptions.ConstraintViolationException;
+import org.apache.empire.db.exceptions.CommandWithoutSelectException;
 import org.apache.empire.db.exceptions.QueryFailedException;
 import org.apache.empire.db.exceptions.QueryNoResultException;
 import org.apache.empire.db.exceptions.StatementFailedException;
@@ -919,17 +920,18 @@ public class DBUtils implements DBContextAware
     }
 
     /**
-     * Crates a default DBBeanListFactory for Java bean class
+     * gets or creates DBBeanListFactory for the given rowset
      * @param beanType the beanType for which to create the list head 
-     * @param constructorParams the columns to be used for the constructor 
(optional) 
+     * @param rowset the rowset for which to return the factory 
      * @return the bean factory
      */
-    protected final <T> DBBeanListFactory<T> 
createDefaultBeanListFactory(Class<T> beanType, DBColumnExpr[] 
constructorParams) 
+    protected <T> DBBeanListFactory<T> getRowsetBeanListFactory(Class<T> 
beanType, DBRowSet rowset) 
     {
-        List<DBColumnExpr> params = new 
ArrayList<DBColumnExpr>(constructorParams.length);
-        for (int i=0; i<constructorParams.length; i++)
-            params.add(constructorParams[i]);
-        return createDefaultBeanListFactory(beanType, params);
+        @SuppressWarnings("unchecked")
+        DBBeanListFactory<T> factory = 
(DBBeanListFactory<T>)rowset.getBeanFactory();
+        if (factory==null)
+            factory =createDefaultBeanListFactory(beanType, 
rowset.getColumns());
+        return factory;
     }
     
     /**
@@ -1005,43 +1007,61 @@ public class DBUtils implements DBContextAware
     }
 
     /**
-     * Queries a single Java Bean for a given command
-     * If the command has no select expressions then the beanType must be 
assigned to a DBRowSet via DBRowSet.setBeanType() 
+     * Queries a list of Java beans for a given command
+     * @param cmd the query command
+     * @param factory the beanType factory used to instantiate the bean
+     * @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)
+    {
+        return queryBeanList(cmd, factory, parent, 0, -1);
+    }
+
+    /**
+     * Queries a list of Java beans for a given command
      * @param cmd the query command
      * @param beanType the beanType
-     * @param first first item of query (default is 0)
-     * @param pageSize the maximum number of items to add to the list or -1 
(default) for all
+     * @param constructorParams the list of params used for the bean 
constructor (optional, may be null)
+     * @param parent (optional) the parent bean if any 
      * @return the list of java beans
      */
-    @SuppressWarnings("unchecked")
-    public <T> List<T> queryBeanList(DBCommand cmd, Class<T> beanType, Object 
parent, int first, int pageSize)
+    public <T> List<T> queryBeanList(DBCommand cmd, Class<T> beanType, 
DBRowSet rowset, Object parent)
     {
-        DBBeanListFactory<T> factory;
-        if (cmd.hasSelectExpr()==false)
-        {
-            DBRowSet rowset = DBRowSet.getRowsetforType(beanType);
-            if (rowset==null)
-                throw new InvalidArgumentException("cmd", cmd);
-            // found, use factory of rowset
-            factory = (DBBeanListFactory<T>)rowset.getBeanFactory();
-        }
-        else
-        {   // use default rowset
-            factory = createDefaultBeanListFactory(beanType, 
cmd.getSelectExprList());
-        }
-        return queryBeanList(cmd, factory, parent, first, pageSize);
+        return queryBeanList(cmd, getRowsetBeanListFactory(beanType, rowset), 
parent, 0, -1);
     }
-    
-    public final <T> List<T> queryBeanList(DBCommand cmd, Class<T> beanType, 
List<? extends DBColumnExpr> constructorParams, Object parent)
+
+    /**
+     * Queries a list of Java beans for a given command
+     * @param cmd the query command
+     * @param beanType the beanType
+     * @param constructorParams (optional) the params used for the bean 
constructor 
+     * @param parent (optional) the parent bean if any 
+     * @return the list of java beans
+     */
+    public <T> List<T> queryBeanList(DBCommand cmd, Class<T> beanType, List<? 
extends DBColumnExpr> constructorParams, Object parent)
     {
+        if (cmd.hasSelectExpr()==false && (constructorParams==null || 
constructorParams.isEmpty()))
+            throw new CommandWithoutSelectException(cmd);
         return queryBeanList(cmd, createDefaultBeanListFactory(beanType, 
constructorParams), parent, 0, -1);
     }
-    
-    public final <T> List<T> queryBeanList(DBCommand cmd, Class<T> beanType, 
Object parent)
+
+    /**
+     * Queries a list of Java beans for a given command
+     * @param cmd the query command
+     * @param beanType the beanType
+     * @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)
     {
-        return queryBeanList(cmd, beanType, parent, 0, -1);
+        DBRowSet rowset = DBRowSet.getRowsetforType(beanType);
+        if (rowset!=null)
+            return queryBeanList(cmd, getRowsetBeanListFactory(beanType, 
rowset), parent);
+        else
+            return queryBeanList(cmd, beanType, cmd.getSelectExpressions(), 
parent);
     }
-
+    
     /**
      * queries a single Java Bean for a given command 
      * @param cmd the query command
@@ -1074,73 +1094,97 @@ public class DBUtils implements DBContextAware
 
     /**
      * Queries a single Java Bean for a given command
-     * If the command has no select expressions then the beanType must be 
assigned to a DBRowSet via DBRowSet.setBeanType() 
      * @param cmd the query command
      * @param beanType the bean type
+     * @param constructorParams (optional) the params used for the bean 
constructor 
      * @return the bean instance
      */
-    @SuppressWarnings("unchecked")
-    public <T> T queryBean(DBCommand cmd, Class<T> beanType)
+    public final <T> T queryBean(DBCommand cmd, Class<T> beanType, List<? 
extends DBColumnExpr> constructorParams)
     {
-        DBBeanListFactory<T> factory;
-        if (cmd.hasSelectExpr()==false)
-        {
-            DBRowSet rowset = DBRowSet.getRowsetforType(beanType);
-            if (rowset==null)
-                throw new UnknownBeanTypeException(beanType);
-            // found, use factory of rowset
-            factory = (DBBeanListFactory<T>)rowset.getBeanFactory();
-        }
-        else
-        {   // use default rowset
-            factory = createDefaultBeanListFactory(beanType, 
cmd.getSelectExprList());
-        }
-        return queryBean(cmd, factory);
+        if (cmd.hasSelectExpr()==false && (constructorParams==null || 
constructorParams.isEmpty()))
+            throw new CommandWithoutSelectException(cmd);
+        return queryBean(cmd, createDefaultBeanListFactory(beanType, 
constructorParams));
     }
-    
-    public final <T> T queryBean(DBCommand cmd, Class<T> beanType, List<? 
extends DBColumnExpr> constructorParams, Object parent)
+
+    /**
+     * Queries a single Java Bean for a given command
+     * @param cmd the query command
+     * @param beanType the beanType
+     * @param parent (optional) the parent bean if any 
+     * @return the list of java beans
+     */
+    public <T> T queryBean(DBCommand cmd, Class<T> beanType)
     {
-        return queryBean(cmd, createDefaultBeanListFactory(beanType, 
constructorParams));
+        DBRowSet rowset = DBRowSet.getRowsetforType(beanType);
+        if (rowset!=null)
+            return queryBean(cmd, getRowsetBeanListFactory(beanType, rowset));
+        else
+            return queryBean(cmd, beanType, cmd.getSelectExpressions());
     }
     
     /**
-     * Queries a single bean based on a set of where constraints
-     * @param beanType the beanType 
-     * @param whereConstraints
-     * @return
+     * Queries a single bean based on a where constraint
+     * @param beanType the beanType
+     * @param rowset the rowset used for the query 
+     * @param whereConstraints the constraints for the query
+     * @return the entity bean
      */
-    public final <T> T queryBean(Class<T> beanType, DBCompareExpr 
whereConstraints)
+    public final <T> T queryBean(Class<T> beanType, DBRowSet rowset, 
DBCompareExpr whereConstraints)
     {
-        if (whereConstraints==null)
-            throw new InvalidArgumentException("whereConstraints", 
whereConstraints);
+        DBObject.checkParamNull("rowset", rowset);
+        DBObject.checkParamNull("whereConstraints", whereConstraints);
         // find
         DBDatabase db = whereConstraints.getDatabase();
         DBCommand cmd = db.createCommand();
         cmd.where(whereConstraints);
-        return queryBean(cmd, beanType);
+        // use factory of rowset
+        return queryBean(cmd, getRowsetBeanListFactory(beanType, rowset));
     }
     
     /**
-     * Queries a single bean based on a set of where constraints
-     * @param beanType the beanType 
-     * @param whereConstraints
-     * @return
+     * Queries a single bean based on a where constraint
+     * @param beanType the beanType
+     * @param whereConstraints the constraints for the query
+     * @return the entity bean
      */
-    public final <T> T queryBean(Class<T> beanType, Object[] key)
+    public final <T> T queryBean(Class<T> beanType, DBCompareExpr 
whereConstraints)
     {
-        if (key==null)
-            throw new InvalidArgumentException("key", key);
-        // find rowset
         DBRowSet rowset = DBRowSet.getRowsetforType(beanType);
         if (rowset==null)
             throw new UnknownBeanTypeException(beanType);
+        return queryBean(beanType, rowset, whereConstraints);
+    }
+    
+    /**
+     * Queries a single bean based on primary key values
+     * @param beanType the beanType 
+     * @param rowset the rowset used for the query 
+     * @param key the primary key
+     * @return the entity bean
+     */
+    public final <T> T queryBean(Class<T> beanType, DBRowSet rowset, Object[] 
key)
+    {
+        DBObject.checkParamNull("rowset", rowset);
+        DBObject.checkParamNull("key", key);
         // set key constraints 
         DBCommand cmd = rowset.getDatabase().createCommand();
         rowset.setKeyConstraints(cmd, key);
         // use factory of rowset
-        @SuppressWarnings("unchecked")
-        DBBeanListFactory<T> factory = 
(DBBeanListFactory<T>)rowset.getBeanFactory();
-        return queryBean(cmd, factory);
+        return queryBean(cmd, getRowsetBeanListFactory(beanType, rowset));
+    }
+    
+    /**
+     * Queries a single bean based on primary key values
+     * @param beanType the beanType 
+     * @param key the primary key
+     * @return the entity bean
+     */
+    public final <T> T queryBean(Class<T> beanType, Object[] key)
+    {
+        DBRowSet rowset = DBRowSet.getRowsetforType(beanType);
+        if (rowset==null)
+            throw new UnknownBeanTypeException(beanType);
+        return queryBean(beanType, rowset, key);
     }
     
 }
diff --git 
a/empire-db/src/main/java/org/apache/empire/exceptions/InvalidArgumentException.java
 
b/empire-db/src/main/java/org/apache/empire/db/exceptions/CommandWithoutSelectException.java
similarity index 64%
copy from 
empire-db/src/main/java/org/apache/empire/exceptions/InvalidArgumentException.java
copy to 
empire-db/src/main/java/org/apache/empire/db/exceptions/CommandWithoutSelectException.java
index 55b2924..6c0cb15 100644
--- 
a/empire-db/src/main/java/org/apache/empire/exceptions/InvalidArgumentException.java
+++ 
b/empire-db/src/main/java/org/apache/empire/db/exceptions/CommandWithoutSelectException.java
@@ -1,40 +1,35 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.empire.exceptions;
-
-import org.apache.empire.commons.ErrorType;
-import org.apache.empire.commons.StringUtils;
-
-public class InvalidArgumentException extends EmpireException
-{
-    private static final long serialVersionUID = 1L;
-    
-    public static final ErrorType errorType = new 
ErrorType("error.invalidArgument", "Invalid Argument {0} for parameter {1}.");
-    
-    public InvalidArgumentException(String param, Object value)
-    {
-        super(errorType, new String[] { StringUtils.valueOf(value), param } );
-    }
-    
-    public InvalidArgumentException(String param, Object[] value)
-    {
-        super(errorType, new String[] { StringUtils.valueOf(value), param } );
-    }
-
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.empire.db.exceptions;
+
+import org.apache.empire.commons.ErrorType;
+import org.apache.empire.db.DBCommand;
+import org.apache.empire.exceptions.InvalidArgumentException;
+
+public class CommandWithoutSelectException extends InvalidArgumentException
+{
+    private static final long serialVersionUID = 1L;
+    
+    public static final ErrorType errorType = new 
ErrorType("error.db.commandWithoutSelect",  "The command statement has no 
select expressions.");
+    
+    public CommandWithoutSelectException(DBCommand cmd)
+    {
+        super(CommandWithoutSelectException.errorType, null);
+    }
+}
diff --git 
a/empire-db/src/main/java/org/apache/empire/exceptions/InvalidArgumentException.java
 
b/empire-db/src/main/java/org/apache/empire/exceptions/InvalidArgumentException.java
index 55b2924..14549b3 100644
--- 
a/empire-db/src/main/java/org/apache/empire/exceptions/InvalidArgumentException.java
+++ 
b/empire-db/src/main/java/org/apache/empire/exceptions/InvalidArgumentException.java
@@ -27,6 +27,11 @@ public class InvalidArgumentException extends EmpireException
     
     public static final ErrorType errorType = new 
ErrorType("error.invalidArgument", "Invalid Argument {0} for parameter {1}.");
     
+    protected InvalidArgumentException(ErrorType errorType, String[] params)
+    {
+        super(errorType, params);
+    }
+    
     public InvalidArgumentException(String param, Object value)
     {
         super(errorType, new String[] { StringUtils.valueOf(value), param } );

Reply via email to