You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@empire-db.apache.org by do...@apache.org on 2022/01/27 08:57:52 UTC

[empire-db] branch version3 updated: EMPIREDB-362 DBRowSet cleanup

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 98ba9a0  EMPIREDB-362 DBRowSet cleanup
98ba9a0 is described below

commit 98ba9a0a482601ba6690ab1f40b22b440c5b4afb
Author: Rainer Döbele <do...@apache.org>
AuthorDate: Thu Jan 27 09:57:50 2022 +0100

    EMPIREDB-362 DBRowSet cleanup
---
 .../jsf2/pageelements/BeanListPageElement.java     |  7 ++-
 .../org/apache/empire/commons/ObjectUtils.java     | 25 ++++++++-
 .../main/java/org/apache/empire/db/DBQuery.java    |  7 ++-
 .../main/java/org/apache/empire/db/DBRecord.java   |  2 +-
 .../main/java/org/apache/empire/db/DBRowSet.java   | 63 +++++++++-------------
 .../main/java/org/apache/empire/db/DBTable.java    | 12 +++++
 .../src/main/java/org/apache/empire/db/DBView.java | 23 +++++---
 7 files changed, 86 insertions(+), 53 deletions(-)

diff --git a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/BeanListPageElement.java b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/BeanListPageElement.java
index 303c7c3..6bac121 100644
--- a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/BeanListPageElement.java
+++ b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/BeanListPageElement.java
@@ -576,7 +576,7 @@ public class BeanListPageElement<T> extends ListPageElement<T> implements ListIt
 
     protected void generateIdParams(DBRowSet rowset, List<?> items)
     {
-        DBColumn[] keyCols = rowset.getKeyColumns();
+        Column[] keyCols = rowset.getKeyColumns();
         if (keyCols == null)
             return; // No Primary Key!
         // generate all
@@ -591,7 +591,7 @@ public class BeanListPageElement<T> extends ListPageElement<T> implements ListIt
         }
     }
 
-    protected Object[] getItemKey(DBColumn[] cols, Object item)
+    protected Object[] getItemKey(Column[] cols, Object item)
     {
         Object[] key = new Object[cols.length];
         for (int i = 0; i < cols.length; i++)
@@ -630,9 +630,8 @@ public class BeanListPageElement<T> extends ListPageElement<T> implements ListIt
         Set<Object[]> items = getSelectedItemKeys();
         if (items.size()>0)
         {
-            DBColumn[] pk = rowset.getKeyColumns();
+            DBColumn[] pk = (DBColumn[])rowset.getKeyColumns();
             DBColumnExpr keyExpr = pk[0];
-           
             for (int i=1; i<pk.length; i++)
             {
                 keyExpr = keyExpr.append(pk[i]);
diff --git a/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java b/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java
index 6041940..c440c34 100644
--- a/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java
+++ b/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java
@@ -934,9 +934,9 @@ public final class ObjectUtils
     }
     
     /**
-     * returns wheter or not a array contains a certain item
+     * returns whether or not a array contains a certain item
+     * performs a simple (==) comparison (fast)
      * 
-     * @param <T> the ype of the objects in the array
      * @param array the array to search
      * @param item the item to search for
      * 
@@ -950,6 +950,27 @@ public final class ObjectUtils
         {
             if (array[i]==item)
                 return true;
+        }
+        return false;
+    }
+    
+    /**
+     * returns whether or not a array contains a certain item
+     * Uses the equals() operator to compare 
+     * 
+     * @param array the array to search
+     * @param item the item to search for
+     * 
+     * @return true if the array contains the item or false otherwise
+     */
+    public static <T> boolean containsElement(T[] array, T item)
+    {
+        if (array==null)
+            return false;
+        for (int i=0; i<array.length; i++)
+        {
+            if (array[i]==item)
+                return true;
             if (array[i]!=null && array[i].equals(item))
                 return true;
         }
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBQuery.java b/empire-db/src/main/java/org/apache/empire/db/DBQuery.java
index 6a9a6cb..5649085 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBQuery.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBQuery.java
@@ -314,7 +314,10 @@ public class DBQuery extends DBRowSet
         // get Key
         Object rowSetData = record.getRowSetData();
         if (rowSetData==null)
+        {
             log.warn("No Record-key provided for query record!");
+            return super.getRecordKey(record);
+        }
         return (Object[])rowSetData;
     }
 
@@ -344,6 +347,7 @@ public class DBQuery extends DBRowSet
     @Override
     protected void initRecord(DBRecord rec, DBRecordData recData, Object rowSetData)
     {
+        /*
         if (keyColumns!=null)
         {   // check
             if (rowSetData!=null && !(rowSetData instanceof Object[]) && ((Object[])rowSetData).length!=keyColumns.length)
@@ -357,6 +361,7 @@ public class DBQuery extends DBRowSet
                 rowSetData = recordKey;
             }
         }
+        */
         // int
         super.initRecord(rec, recData, rowSetData);
     }
@@ -401,7 +406,7 @@ public class DBQuery extends DBRowSet
         // Read Record
         try {
             // Read Record
-            readRecord(rec, cmd, key.clone()); 
+            readRecord(rec, cmd, null); // key.clone() 
         } catch (QueryNoResultException e) {
             // Record not found
             throw new RecordNotFoundException(this, key);
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 f6b4717..8e92874 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
@@ -1009,7 +1009,7 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
         if (!isValid())
             throw new ObjectNotValidException(this);
         // set row key
-        DBColumn[] keyColumns = rowset.getKeyColumns();
+        Column[] keyColumns = rowset.getKeyColumns();
         if (keyColumns != null && keyColumns.length > 0)
         { // key exits
             if (keyColumns.length > 1)
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java b/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
index 2774ba1..6ae2910 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
@@ -101,7 +101,6 @@ public abstract class DBRowSet extends DBExpr implements Entity
     // Members
     protected final DBDatabase        db; /* transient */
     protected String                  comment          = null;
-    protected DBIndex                 primaryKey       = null;
     protected DBColumn                timestampColumn  = null;
     protected Map<DBColumn, DBColumn> columnReferences = null;
     
@@ -357,17 +356,6 @@ public abstract class DBRowSet extends DBExpr implements Entity
         // Check Update Column
         return (col.isReadOnly());
     }
-
-    /**
-     * Returns an array of all primary key columns.
-     * 
-     * @return an array of all primary key columns
-     */
-    @Override
-    public DBColumn[] getKeyColumns()
-    {
-        return ((primaryKey != null) ? primaryKey.getColumns() : null);
-    }
     
     /**
      * Checks whether a given column is part of the primary key for this RowSet 
@@ -376,16 +364,8 @@ public abstract class DBRowSet extends DBExpr implements Entity
      */
     public boolean isKeyColumn(DBColumn column)
     {
-        DBColumn[] keyColumns = getKeyColumns();
-        if (keyColumns==null)
-            return false;
-        // search
-        for (int i=0; i<keyColumns.length; i++)
-        {
-            if (keyColumns[i]==column)
-                return true;
-        }
-        return false;
+        Column[] keyColumns = getKeyColumns();
+        return ObjectUtils.contains(keyColumns, column);
     }
 
     /**
@@ -486,10 +466,10 @@ public abstract class DBRowSet extends DBExpr implements Entity
     {
         if (rec.getRowSet() != this)
             return null; // Invalid Argument
-        if (primaryKey == null)
-            return null; // No primary key
         // Check Columns
-        DBColumn[] keyColumns = primaryKey.getColumns();
+        Column[] keyColumns = getKeyColumns();
+        if (keyColumns == null || keyColumns.length==0)
+            return null; // No primary key
         Object[] keys = new Object[keyColumns.length];
         for (int i = 0; i < keyColumns.length; i++)
         {
@@ -531,12 +511,16 @@ public abstract class DBRowSet extends DBExpr implements Entity
          * ![fields[i] <> ObjectUtils.NO_VALUE];
          */    
         // Init Key Values
-        if (keyValues != null && primaryKey != null)
+        if (keyValues != null)
         {   // Check Columns
-            DBColumn[] keyColumns = primaryKey.getColumns();
+            DBColumn[] keyColumns =(DBColumn[])getKeyColumns();
+            if (keyColumns==null)
+                throw new NoPrimaryKeyException(this);
+            if (keyValues.length!=keyColumns.length)
+                throw new InvalidArgumentException("keyValues", keyValues);
             for (int i = 0; i < keyColumns.length; i++)
             {   // check
-                DBColumn keyColumn = keyColumns[i]; 
+                Column keyColumn = keyColumns[i]; 
                 if (newRecord && keyColumn.isAutoGenerated())
                     throw new FieldIsReadOnlyException(keyColumn);
                 // Ignore Validity Checks
@@ -579,13 +563,14 @@ public abstract class DBRowSet extends DBExpr implements Entity
         prepareInitRecord(rec, rowSetData, false);
         // Get Record Field Values
         Object[] fields = rec.getFields();
+        DBColumn[] keyColumns =(DBColumn[])getKeyColumns();
         for (int i = 0; i < fields.length; i++)
         {   // Read a value
         	DBColumnExpr column = getColumnExprAt(i);
         	int rdi = recData.getFieldIndex(column);
         	if (rdi<0)
         	{	// Field not available in Record Data
-        		if (primaryKey!=null && (column instanceof DBColumn) && primaryKey.contains((DBColumn)column))
+        		if (keyColumns!=null && ObjectUtils.contains(keyColumns, column))
         		{	// Error: Primary Key not supplied
         		    throw new ItemNotFoundException(column.getName());
         		}
@@ -665,20 +650,21 @@ public abstract class DBRowSet extends DBExpr implements Entity
     protected void setKeyConstraints(DBCommand cmd, Object[] key)
     {
         // Check Primary key
-        if (primaryKey == null ) 
+        DBColumn[] keyColumns =(DBColumn[])getKeyColumns();
+        if (keyColumns == null ) 
             throw new NoPrimaryKeyException(this); // Invalid Argument
         // Check Columns
-        DBColumn[] keyColumns = primaryKey.getColumns();
         if (key == null || key.length != keyColumns.length)
             throw new InvalidKeyException(this, key); // Invalid Argument
         // Add the key constraints
         for (int i = 0; i < key.length; i++)
         {   // prepare key value
+            DBColumn column = keyColumns[i];
             Object value = key[i];
             if (db.isPreparedStatementsEnabled())
-                value = cmd.addParam(keyColumns[i], value);
+                value = cmd.addParam(column, value);
             // set key column constraint
-            cmd.where(keyColumns[i].is(value));
+            cmd.where(column.is(value));
         }    
     }
     
@@ -881,6 +867,7 @@ public abstract class DBRowSet extends DBExpr implements Entity
         String sql = null;
         int setCount = 0;
         // Perform action
+        DBColumn[] keyColumns =(DBColumn[])getKeyColumns();
         DBRecord.State recordState = rec.getState(); 
         if (recordState==DBRecord.State.New)
         {	// Insert Record
@@ -916,7 +903,7 @@ public abstract class DBRowSet extends DBExpr implements Entity
                     cmd.set(col.to(value));
                     setCount++;
                 }
-                else if (primaryKey!=null && primaryKey.contains(col))
+                else if (ObjectUtils.contains(keyColumns, col))
                 {   // All primary key fields must be supplied
                     throw new FieldNotNullException(col);
                 }
@@ -929,7 +916,7 @@ public abstract class DBRowSet extends DBExpr implements Entity
         }
         else if (recordState==DBRecord.State.Modified)
         {	// Update Record
-            if (primaryKey == null)
+            if (keyColumns == null)
             { // Requires a primary key
                 log.error("updateRecord: "  + name + " no primary key defined!");
                 throw new NoPrimaryKeyException(this);
@@ -948,7 +935,7 @@ public abstract class DBRowSet extends DBExpr implements Entity
             	boolean modified = rec.wasModified(i);
             	boolean empty = ObjectUtils.isEmpty(value); 
                 DBTableColumn col = (DBTableColumn) columns.get(i);
-                if (primaryKey.contains(col))
+                if (ObjectUtils.contains(keyColumns, col))
                 { 	// Check for Modification
                     if (modified == true)
                     { // Requires a primary key
@@ -1042,7 +1029,7 @@ public abstract class DBRowSet extends DBExpr implements Entity
     {
         // Merge Sub-Records
         List<DBRelation> relations = db.getRelations();
-        DBColumn[] keyColumns = getKeyColumns();
+        DBColumn[] keyColumns =(DBColumn[])getKeyColumns();
         if (keyColumns==null)
             return; // No primary key - no references!
         // Find all relations
@@ -1076,7 +1063,7 @@ public abstract class DBRowSet extends DBExpr implements Entity
         if (refs.length!=parentKey.length)
             throw new InvalidArgumentException("refs", refs);
         // Rowset
-        DBColumn[] keyColumns = getKeyColumns();
+        DBColumn[] keyColumns =(DBColumn[])getKeyColumns();
         if (keyColumns==null || keyColumns.length==0)
         {   // No Primary Key
             DBCommand cmd = db.createCommand();
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBTable.java b/empire-db/src/main/java/org/apache/empire/db/DBTable.java
index 4bbd2aa..e051595 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBTable.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBTable.java
@@ -59,6 +59,7 @@ public class DBTable extends DBRowSet implements Cloneable
 
     private final String         name;
     private String               alias;
+    private DBIndex              primaryKey          = null;
     private final List<DBIndex>  indexes             = new ArrayList<DBIndex>();
     private Boolean              quoteName           = null;
     private DBCascadeAction      cascadeDeleteAction = DBCascadeAction.NONE;
@@ -129,6 +130,17 @@ public class DBTable extends DBRowSet implements Cloneable
     }
 
     /**
+     * Returns an array of all primary key columns.
+     * 
+     * @return an array of all primary key columns
+     */
+    @Override
+    public DBColumn[] getKeyColumns()
+    {
+        return ((primaryKey != null) ? primaryKey.getColumns() : null);
+    }
+
+    /**
      * Clones this table and assigns a new table alias.
      * This second instance of the same table can be used for self-joins.
      * <pre>
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBView.java b/empire-db/src/main/java/org/apache/empire/db/DBView.java
index 1dfca41..3dd19ce 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBView.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBView.java
@@ -22,7 +22,6 @@ import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.empire.commons.Options;
 import org.apache.empire.data.DataType;
-import org.apache.empire.db.DBIndex.DBIndexType;
 import org.apache.empire.db.expr.column.DBValueExpr;
 import org.apache.empire.exceptions.InvalidArgumentException;
 import org.apache.empire.exceptions.ItemExistsException;
@@ -187,10 +186,11 @@ public abstract class DBView extends DBRowSet
 
     private static AtomicInteger viewCount = new AtomicInteger(0);
 
-    private String     name;
-    private String     alias;
-    private boolean    updateable;  // true if the view is updateable
-    private Boolean    quoteName  = null;
+    private String               name;
+    private String               alias;
+    private DBViewColumn[]       keyColumns;
+    private boolean              updateable;                      // true if the view is updateable
+    private Boolean              quoteName = null;
 
     /**
      * Creates a view object for a given view in the database.
@@ -221,6 +221,16 @@ public abstract class DBView extends DBRowSet
     { // Set the column expressions
         this(name, db, false);
     }
+
+    /**
+     * Returns an array of all key columns.
+     * @return an array of all key columns
+     */
+    @Override
+    public DBColumn[] getKeyColumns()
+    {
+        return this.keyColumns;
+    }
     
     /**
      * identifies the columns that uniquely identify a row in the view
@@ -228,8 +238,7 @@ public abstract class DBView extends DBRowSet
      */
     protected void setKeyColumns(DBViewColumn... keyColumns)
     { // Set Key Columns
-        if (keyColumns != null)
-            primaryKey = new DBIndex(null, DBIndexType.PRIMARY_KEY, keyColumns);
+        this.keyColumns = keyColumns;
     }
 
     /**