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 d5c0464  EMPIREDB-362 DBRecord improvements
d5c0464 is described below

commit d5c0464fdef4c7ed576ace84e8f2ebefb366b0ce
Author: Rainer Döbele <[email protected]>
AuthorDate: Fri Jan 28 11:15:51 2022 +0100

    EMPIREDB-362 DBRecord improvements
---
 .../java/org/apache/empire/db/DBCommandExpr.java   |  11 --
 .../main/java/org/apache/empire/db/DBObject.java   |  15 ++
 .../main/java/org/apache/empire/db/DBQuery.java    |   5 +-
 .../main/java/org/apache/empire/db/DBRecord.java   | 170 +++++++++++----------
 .../main/java/org/apache/empire/db/DBRowSet.java   | 149 +++++++-----------
 5 files changed, 164 insertions(+), 186 deletions(-)

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 6772bcd..e51f445 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
@@ -118,17 +118,6 @@ public abstract class DBCommandExpr extends DBExpr
             throw new NotSupportedException(this, "getKeyColumns");
         }
 
-        /**
-         * Prints the error message: ERR_NOTSUPPORTED.
-         * 
-         * @return null
-         */
-        @Override
-        public Object[] getRecordKey(DBRecord rec)
-        {
-            throw new NotSupportedException(this, "getRecordKey");
-        }
-
         /** throws ERR_NOTSUPPORTED */
         @Override
         public void createRecord(DBRecord rec, Object[] initalKey, boolean 
deferredInit)
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBObject.java 
b/empire-db/src/main/java/org/apache/empire/db/DBObject.java
index bca1788..e6dfab4 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBObject.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBObject.java
@@ -18,6 +18,8 @@
  */
 package org.apache.empire.db;
 
+import org.apache.empire.exceptions.InvalidArgumentException;
+
 /**
  * Base class for all objects that directly or indirectly belong to a database 
including the database object itself.
  * Examples are: tables, views, columns, indexes, relations etc.
@@ -28,6 +30,19 @@ public abstract class DBObject // *Deprecated* implements 
Serializable
     // *Deprecated* private static final long serialVersionUID = 1L;
 
     /**
+     * Internally used for parameter checking
+     * @param name
+     * @param param
+     * @return
+     */
+    public static <T extends Object> T checkParamNull(String name, T param)
+    {
+        if (param==null)
+            throw new InvalidArgumentException(name, param);
+        return param;
+    }
+    
+    /**
      * Returns the database object to which this object belongs to.
      * For the database object itself this function will return the this 
pointer.
      * 
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 0d6e3c1..9a5aa92 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
@@ -307,8 +307,7 @@ public class DBQuery extends DBRowSet
      * @param record the DBRecord object, contains all fields and the field 
properties
      * @return a array of primary key columns
      */
-    @Override
-    public Object[] getRecordKey(DBRecord record)
+    protected Object[] getRecordKey(DBRecord record)
     {
         if (record == null || record.getRowSet() != this)
             throw new InvalidArgumentException("record", record);
@@ -317,7 +316,7 @@ public class DBQuery extends DBRowSet
         if (rowSetData instanceof Object[])
             return (Object[])rowSetData;
         // generate key now
-        return super.getRecordKey(record);
+        return record.getKey();
     }
 
     /**
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 0041019..c8fb198 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
@@ -41,11 +41,11 @@ import org.apache.empire.db.context.DBContextAware;
 import org.apache.empire.db.context.DBRollbackHandler;
 import org.apache.empire.db.exceptions.FieldIsReadOnlyException;
 import org.apache.empire.db.exceptions.FieldValueNotFetchedException;
+import org.apache.empire.db.exceptions.NoPrimaryKeyException;
 import org.apache.empire.db.expr.compare.DBCompareExpr;
 import org.apache.empire.exceptions.BeanPropertyGetException;
 import org.apache.empire.exceptions.InvalidArgumentException;
 import org.apache.empire.exceptions.ItemNotFoundException;
-import org.apache.empire.exceptions.NotSupportedException;
 import org.apache.empire.exceptions.ObjectNotValidException;
 import org.apache.empire.exceptions.UnspecifiedErrorException;
 import org.apache.empire.xml.XMLUtil;
@@ -304,15 +304,11 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
     }
     
     /**
-     * Constructs a new DBRecord.<BR>
-     * @param context the DBContext for this record
-     * @param rowset the corresponding RowSet(Table, View, Query, etc.)
+     * Internal constructor for DBRecord
+     * May be used by derived classes to provide special behaviour
      */
-    public DBRecord(DBContext context, DBRowSet rowset)
+    protected DBRecord(DBContext context, DBRowSet rowset, boolean 
enableRollbackHandling)
     {
-        // Check params
-        if (context==null || rowset==null)
-            throw new InvalidArgumentException("context|rowset", context);
         // init
         this.context = context;
         this.rowset = rowset;
@@ -320,12 +316,24 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
         this.fields = null;
         this.modified = null;
         this.rowsetData = null;
-        // options                         
-        enableRollbackHandling = context.isRollbackHandlingEnabled();
-        validateFieldValues = true;
+        // options
+        this.enableRollbackHandling = enableRollbackHandling;
+        this.validateFieldValues = true;
     }
 
     /**
+     * Constructs a new DBRecord.<BR>
+     * @param context the DBContext for this record
+     * @param rowset the corresponding RowSet(Table, View, Query, etc.)
+     */
+    public DBRecord(DBContext context, DBRowSet rowset)
+    {
+        this(checkParamNull("context", context),
+             checkParamNull("rowset", rowset),
+             context.isRollbackHandlingEnabled());
+    }
+    
+    /**
      * Closes the record by releasing all resources and resetting the record's 
state to invalid.
      */
     @Override
@@ -349,8 +357,6 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
         try 
         {
             DBRecord rec = (DBRecord)super.clone();
-            if (rec.rowset!= this.rowset)
-                throw new NotSupportedException(this, "clone");
             rec.state = this.state;
             if (rec.fields == fields && fields!=null)
                 rec.fields = fields.clone();
@@ -375,29 +381,33 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
     @SuppressWarnings("unchecked")
     public <T extends DBContext> T  getContext()
     {
+        if (this.context==null)
+            throw new ObjectNotValidException(this);
         return ((T)context);
     }
 
     /**
-     * Returns the current DBDatabase object.
+     * Returns the DBRowSet object.
      * 
-     * @return the current DBDatabase object
+     * @return the DBRowSet object
      */
-    @Override
-    public final <T extends DBDatabase> T getDatabase()
+    @SuppressWarnings("unchecked")
+    public <T extends DBRowSet> T getRowSet()
     {
-        return rowset.getDatabase();
+        if (this.rowset==null)
+            throw new ObjectNotValidException(this);
+        return (T)this.rowset;
     }
 
     /**
-     * Returns the DBRowSet object.
+     * Returns the current DBDatabase object.
      * 
-     * @return the DBRowSet object
+     * @return the current DBDatabase object
      */
-    @SuppressWarnings("unchecked")
-    public <T extends DBRowSet> T getRowSet()
+    @Override
+    public final <T extends DBDatabase> T getDatabase()
     {
-        return (T)this.rowset;
+        return getRowSet().getDatabase();
     }
 
     /**
@@ -487,7 +497,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
     @Override
     public int getFieldIndex(ColumnExpr column)
     {
-        return (rowset != null) ? rowset.getColumnIndex(column) : -1;
+        return getRowSet().getColumnIndex(column);
     }
 
     /**
@@ -498,41 +508,26 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
     @Override
     public int getFieldIndex(String column)
     {
-        if (rowset != null)
+        List<DBColumn> columns = getRowSet().getColumns();
+        for (int i = 0; i < columns.size(); i++)
         {
-            List<DBColumn> columns = rowset.getColumns();
-            for (int i = 0; i < columns.size(); i++)
-            {
-                DBColumn col = columns.get(i);
-                if (col.getName().equalsIgnoreCase(column))
-                    return i;
-            }
+            DBColumn col = columns.get(i);
+            if (col.getName().equalsIgnoreCase(column))
+                return i;
         }
         // not found
         return -1;
     }
 
     /**
-     * Returns the DBColumn for the field at the given index.
-     * 
-     * @param index the field index 
-     * 
-     * @return the index value
-     */
-    public DBColumn getDBColumn(int index)
-    {
-        return (rowset!=null ? rowset.getColumn(index) : null);
-    }
-
-    /**
      * Implements the Record Interface getColumn method.<BR>
      * Internally calls getDBColumn()
      * @return the Column at the specified index 
      */
     @Override
-    public final Column getColumn(int index)
+    public final DBColumn getColumn(int index)
     {
-        return getDBColumn(index);
+        return getRowSet().getColumn(index);
     }
     
     /**
@@ -542,7 +537,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
     @Override
     public final ColumnExpr getColumnExpr(int index)
     {
-        return getDBColumn(index);
+        return getColumn(index);
     }
     
     /**
@@ -597,17 +592,33 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
     @Override
     public Column[] getKeyColumns()
     {
-        return rowset.getKeyColumns();
+        return getRowSet().getKeyColumns();
     }
 
     /**
-     * Returns the array of primary key columns.
-     * @return the array of primary key columns
+     * Returns a array of primary key columns by a specified DBRecord object.
+     * 
+     * @param rec the DBRecord object, contains all fields and the field 
properties
+     * @return a array of primary key columns
      */
     @Override
     public Object[] getKey()
     {
-        return ((rowset != null) ? rowset.getRecordKey(this) : null);
+        // Check Columns
+        Column[] keyColumns = getKeyColumns();
+        if (keyColumns == null || keyColumns.length==0)
+            throw new NoPrimaryKeyException(getRowSet());
+        // create the key
+        Object[] keys = new Object[keyColumns.length];
+        for (int i = 0; i < keyColumns.length; i++)
+        {
+            keys[i] = getValue(keyColumns[i]);
+            if (keys[i] == null)
+            { // Primary Key not set
+                log.warn("DBRecord.getKey() failed: " + getRowSet().getName() 
+ " primary key value is null!");
+            }
+        }
+        return keys;
     }
 
     /**
@@ -690,7 +701,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
             if (modified[index]==false || fields[index]==ObjectUtils.NO_VALUE)
                 continue;
             // Auto-generated ?
-            DBColumn column = rowset.getColumn(index);
+            DBColumn column = getColumn(index);
             if (column.isAutoGenerated())
                 continue;
             // validate this one
@@ -721,7 +732,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
         if (current==ObjectUtils.NO_VALUE)
             throw new FieldValueNotFetchedException(getColumn(index));
         // convert
-        DBColumn column = rowset.getColumn(index);
+        DBColumn column = getColumn(index);
         // must convert enums
         if (value instanceof Enum<?>)
         {   // convert enum
@@ -801,7 +812,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
        public void setRollbackHandlingEnabled(boolean enabled) 
        {
            // check
-           if (enabled && !context.isRollbackHandlingEnabled())
+           if (enabled && !getContext().isRollbackHandlingEnabled())
                throw new UnspecifiedErrorException("Rollback handling cannot 
be enabled for this record since it is not supported for this context!");
            // enable now
                this.enableRollbackHandling = enabled;
@@ -837,13 +848,11 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
     @Override
     public boolean isFieldVisible(Column column)
     {
-        if (rowset==null)
-            return false;
        // Check value
-        int index = rowset.getColumnIndex(column);
+        int index = getRowSet().getColumnIndex(column);
         if (index<0)
         {   // Column not found
-            log.warn("Column {} does not exist for record of {}", 
column.getName(), rowset.getName());
+            log.warn("Column {} does not exist for record of {}", 
column.getName(), getRowSet().getName());
         }
         return (index>=0 && isValueValid(index));
     }
@@ -858,8 +867,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
     @Override
     public boolean isFieldReadOnly(Column column)
     {
-        if (rowset==null)
-            throw new ObjectNotValidException(this);
+        DBRowSet rowset = getRowSet();
        if (getFieldIndex(column)<0)
             throw new InvalidArgumentException("column", column);
        // Check key column 
@@ -879,9 +887,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
     @Override
     public boolean isFieldRequired(Column column)
     {
-        if (rowset==null)
-            throw new ObjectNotValidException(this);
-       if (rowset.getColumnIndex(column)<0)
+       if (getRowSet().getColumnIndex(column)<0)
             throw new InvalidArgumentException("column", column);
         // from column definition
         return (column.isRequired());
@@ -892,7 +898,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
      */
     public void create(Object[] initalKey)
     {
-        rowset.createRecord(this, initalKey, true);
+        getRowSet().createRecord(this, initalKey, true);
     }
 
     /**
@@ -900,7 +906,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
      */
     public void create()
     {
-        rowset.createRecord(this, null, false);
+        getRowSet().createRecord(this, null, false);
     }
     
     /**
@@ -910,7 +916,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
      */
     public void read(Object[] key)
     {   // read
-        rowset.readRecord(this, key);
+        getRowSet().readRecord(this, key);
     }
 
     /**
@@ -928,7 +934,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
      */
     public void read(DBCompareExpr whereConstraints)
     {
-        rowset.readRecord(this, whereConstraints);
+        getRowSet().readRecord(this, whereConstraints);
     }
     
     /**
@@ -943,7 +949,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
      */
     public void read(Object[] key, PartialMode mode, DBColumn... columns)
     {
-        rowset.readRecord(this, key, mode, columns);
+        getRowSet().readRecord(this, key, mode, columns);
     }
 
     /**
@@ -957,9 +963,9 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
             return; /* Not modified. Nothing to do! */
         // allow rollback
         if (enableRollbackHandling)
-            context.appendRollbackHandler(createRollbackHandler());
+            getContext().appendRollbackHandler(createRollbackHandler());
         // update
-        rowset.updateRecord(this);
+        getRowSet().updateRecord(this);
     }
 
     /**
@@ -978,12 +984,12 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
             throw new ObjectNotValidException(this);
         // allow rollback
         if (enableRollbackHandling)
-            context.appendRollbackHandler(createRollbackHandler());
+            getContext().appendRollbackHandler(createRollbackHandler());
         // Delete only if record is not new
         if (!isNew())
         {
-            Object[] keys = rowset.getRecordKey(this);
-            rowset.deleteRecord(keys, context);
+            Object[] keys = getKey();
+            getRowSet().deleteRecord(keys, getContext());
         }
         close();
     }
@@ -1000,7 +1006,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
             throw new ObjectNotValidException(this);
         // Add Field Description
         int count = 0;
-        List<DBColumn> columns = rowset.getColumns();
+        List<DBColumn> columns = getRowSet().getColumns();
         for (int i = 0; i < columns.size(); i++)
         { // Add Field
             DBColumn column = columns.get(i);
@@ -1024,7 +1030,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
         if (!isValid())
             throw new ObjectNotValidException(this);
         // set row key
-        Column[] keyColumns = rowset.getKeyColumns();
+        Column[] keyColumns = getKeyColumns();
         if (keyColumns != null && keyColumns.length > 0)
         { // key exits
             if (keyColumns.length > 1)
@@ -1046,7 +1052,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
             parent.setAttribute("new", "1");
         // Add all children
         int count = 0;
-        List<DBColumn> columns = rowset.getColumns();
+        List<DBColumn> columns = getRowSet().getColumns();
         for (int i = 0; i < fields.length; i++)
         { // Read all
             DBColumn column = columns.get(i);
@@ -1077,6 +1083,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
         // Create Document
         DBXmlDictionary xmlDic = getXmlDictionary();
         Element root = XMLUtil.createDocument(xmlDic.getRowSetElementName());
+        DBRowSet rowset = getRowSet();
         if (rowset.getName() != null)
             root.setAttribute("name", rowset.getName());
         // Add Field Description
@@ -1100,7 +1107,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
         int count = 0;
         for (int i = 0; i < getFieldCount(); i++)
         { // Check Property
-            DBColumn column = getDBColumn(i);
+            DBColumn column = getColumn(i);
             if (column.isReadOnly())
                 continue;
             if (ignoreList != null && ignoreList.contains(column))
@@ -1133,7 +1140,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
         if (!isValid() || !other.isValid())
             return false;
         // compare table
-        if (!rowset.isSame(other.getRowSet()))
+        if (!getRowSet().isSame(other.getRowSet()))
             return false;
         // compare key
         Object[] key1 = getKey();
@@ -1187,6 +1194,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
     protected void initData(boolean newRecord)
     {
         // Init rowset
+        DBRowSet rowset = getRowSet();
         int colCount = rowset.getColumns().size();
         if (fields==null || fields.length!=colCount)
             fields = new Object[colCount];
@@ -1226,7 +1234,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
         if (column.isAutoGenerated() && (!isNew() || !isNull(column)))
             return false;
         // Check key Column
-        if (!isNew() && rowset.isKeyColumn(column))
+        if (!isNew() && getRowSet().isKeyColumn(column))
             return false;
         // done
         return true;
@@ -1369,7 +1377,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
             log.trace("Record has been changed");
         // Remove rollback (but not when close() is called!)
         if (enableRollbackHandling && fields!=null)
-            context.removeRollbackHandler(this);
+            getContext().removeRollbackHandler(this);
     }
     
     /**
@@ -1378,7 +1386,7 @@ public class DBRecord extends DBRecordData implements 
DBContextAware, Record, Cl
     protected void onFieldChanged(int i)
     {
         if (log.isDebugEnabled())
-            log.debug("Record field " + rowset.getColumn(i).getName() + " 
changed to " + String.valueOf(fields[i]));
+            log.debug("Record field " + getColumn(i).getName() + " changed to 
" + String.valueOf(fields[i]));
     }
     
 }
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 ab17196..3b147b2 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
@@ -235,7 +235,7 @@ public abstract class DBRowSet extends DBExpr implements 
Entity
     
     public abstract boolean isUpdateable();
 
-    public abstract void createRecord(DBRecord rec, Object[] initalKey, 
boolean deferredInit);
+    public abstract void createRecord(DBRecord record, Object[] initalKey, 
boolean deferredInit);
 
     public abstract void deleteRecord(Object[] keys, DBContext context);
     
@@ -466,32 +466,6 @@ public abstract class DBRowSet extends DBExpr implements 
Entity
     }
 
     /**
-     * Returns a array of primary key columns by a specified DBRecord object.
-     * 
-     * @param rec the DBRecord object, contains all fields and the field 
properties
-     * @return a array of primary key columns
-     */
-    public Object[] getRecordKey(DBRecord rec)
-    {
-        if (rec.getRowSet() != this)
-            return null; // Invalid Argument
-        // Check Columns
-        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++)
-        {
-            keys[i] = rec.getValue(keyColumns[i]);
-            if (keys[i] == null)
-            { // Primary Key not set
-                log.warn("getRecordKey: " + getName() + " primary key value is 
null!");
-            }
-        }
-        return keys;
-    }
-
-    /**
      * Returns the column expression at a given column index
      * Allow overrides in derived classes
      * @param index
@@ -506,15 +480,15 @@ public abstract class DBRowSet extends DBExpr implements 
Entity
      * Initializes a DBRecord for this RowSet and sets primary key values (the 
Object[] keyValues).
      * The record may then be modified and updated.<BR>
      * <P>
-     * @param rec the Record object
+     * @param record the Record object
      * @param keyValues an array of the primary key columns
      */
-    protected void initRecord(DBRecord rec, Object[] keyValues, Connection 
conn, boolean setDefaults, boolean newRecord)
+    protected void initRecord(DBRecord record, Object[] keyValues, Connection 
conn, boolean setDefaults, boolean newRecord)
     {
         // Prepare
-        prepareInitRecord(rec, newRecord);
+        prepareInitRecord(record, newRecord);
         // Initialize all Fields
-        Object[] fields = rec.getFields();
+        Object[] fields = record.getFields();
         /* 
          * DO NOT fill with ObjectUtils.NO_VALUE
          * ![fields[i] <> ObjectUtils.NO_VALUE];
@@ -540,10 +514,10 @@ public abstract class DBRowSet extends DBExpr implements 
Entity
         // Set defaults (don't provide connection here)
         if (setDefaults)
         {
-            initRecordDefaultValues(rec, conn);
+            initRecordDefaultValues(record, conn);
         }
         // Init
-        completeInitRecord(rec);
+        completeInitRecord(record);
     }
     
     /**
@@ -591,17 +565,17 @@ public abstract class DBRowSet extends DBExpr implements 
Entity
     
     /**
      * initializes the Record Default Values
-     * @param rec the record
+     * @param record the record
      * @param conn (optional) to allow the dbms handle autogenerated fields
      */
-    protected void initRecordDefaultValues(DBRecord rec, Connection conn)
+    protected void initRecordDefaultValues(DBRecord record, Connection conn)
     {
         /**
          * Overridden in DBTable
          * 
          * Set to NO_VALUE for Views and Queries
          */
-        Object[] fields = rec.getFields();
+        Object[] fields = record.getFields();
         // Set Default values
         for (int i = 0; i < fields.length; i++)
         {   // already set ?
@@ -615,29 +589,29 @@ public abstract class DBRowSet extends DBExpr implements 
Entity
     /**
      * Initialize this DBRowSet object and sets it's initial state.
      * 
-     * @param rec the DBRecord object to initialize this DBRowSet object
+     * @param record the DBRecord object to initialize this DBRowSet object
      * @param rowSetData any further RowSet specific data
      * @param insert
      */
-    protected void prepareInitRecord(DBRecord rec, boolean newRecord)
+    protected void prepareInitRecord(DBRecord record, boolean newRecord)
     {
-        if (rec==null || rec.getRowSet()!=this)
-            throw new InvalidArgumentException("rec", rec);
+        if (record==null || record.getRowSet()!=this)
+            throw new InvalidArgumentException("rec", record);
         if (columns.size() < 1)
             throw new ObjectNotValidException(this);
         // Init
-        rec.initData(newRecord);
+        record.initData(newRecord);
     }
     
     /**
      * Completes the record initialization.<BR>
      * Override this function to do post initialization processing.
      * <P>
-     * @param rec the DBRecord object to initialize
+     * @param record the DBRecord object to initialize
      */
-    protected void completeInitRecord(DBRecord rec)
+    protected void completeInitRecord(DBRecord record)
     {
-       rec.onRecordChanged();
+       record.onRecordChanged();
     }
     
     /**
@@ -670,19 +644,22 @@ public abstract class DBRowSet extends DBExpr implements 
Entity
      * Reads a single record from the database using the given command 
object.<BR>
      * If a record is found the DBRecord object will hold all record data. 
      * <P>
-     * @param rec the DBRecord object which holds the record data
+     * @param record the DBRecord object which holds the record data
      * @param cmd the SQL-Command used to query the record
      * @param rowSetData optional rowset specific data to be held on the record
      */
-    protected void readRecord(DBRecord rec, DBCommand cmd)
+    protected void readRecord(DBRecord record, DBCommand cmd)
     {
         DBReader reader = null;
         try
         {   // read record using a DBReader
-            reader = new DBReader(rec.getContext(), false);
+            reader = new DBReader(record.getContext(), false);
             reader.getRecordData(cmd);
-            initRecord(rec, reader);
-            
+            initRecord(record, reader);
+        } catch (QueryNoResultException e) {
+            // Translate exception
+            Object[] key = ((getKeyColumns()!=null) ? record.getKey() : null);
+            throw new RecordNotFoundException(this, key);
         } finally {
             reader.close();
         }
@@ -692,36 +669,32 @@ public abstract class DBRowSet extends DBExpr implements 
Entity
      * Reads the record with the given primary key from the database.
      * If the record cannot be found, a RecordNotFoundException is thrown.
      * <P>
-     * @param rec the DBRecord object which will hold the record data
+     * @param record the DBRecord object which will hold the record data
      * @param key the primary key values
      */
-    public void readRecord(DBRecord rec, Object[] key)
+    public void readRecord(DBRecord record, Object[] key)
     {
         // Check Arguments
-        if (rec == null)
-            throw new InvalidArgumentException("conn|rec", null);
+        checkParamNull("record", record);
+        checkParamNull("key", key);
         // Select
         DBCommand cmd = db.createCommand();
         cmd.select(columns);
         // Set key constraints
         setKeyConstraints(cmd, key);
-        try {
-            // Read Record
-            readRecord(rec, cmd);
-        } catch (QueryNoResultException e) {
-            // Translate exception
-            throw new RecordNotFoundException(this, key);
-        }
+        // Read Record
+        readRecord(record, cmd);
     }
    
     /**
      * Reads a record from the database
      * @param key an array of the primary key values
      */
-    public void readRecord(DBRecord rec, DBCompareExpr whereConstraints)
+    public void readRecord(DBRecord record, DBCompareExpr whereConstraints)
     {
-        if (whereConstraints==null)
-            throw new InvalidArgumentException("whereConstraints", null);
+        // Check Arguments
+        checkParamNull("record", record);
+        checkParamNull("whereConstraints", whereConstraints);
         // check constraints
         Set<DBColumn> columns = new HashSet<DBColumn>();
         whereConstraints.addReferencedColumns(columns);
@@ -732,21 +705,21 @@ public abstract class DBRowSet extends DBExpr implements 
Entity
         DBCommand cmd = getDatabase().createCommand();
         cmd.select(getColumns());
         cmd.where(whereConstraints);
-        readRecord(rec, cmd);
+        readRecord(record, cmd);
     }
     
     /**
      * Reads the partial record for a given primary key from the database
-     * @param rec the DBRecord object which will hold the record data
+     * @param record the DBRecord object which will hold the record data
      * @param key the primary key values
      * @param mode flag whether to include only the given columns or whether 
to add all but the given columns
      * @param columns the columns to include or exclude (depending on mode)
      */
-    public void readRecord(DBRecord rec, Object[] key, PartialMode mode, 
DBColumn... columns)
+    public void readRecord(DBRecord record, Object[] key, PartialMode mode, 
DBColumn... columns)
     {
         // Check Arguments
-        if (rec == null)
-            throw new InvalidArgumentException("conn|rec", null);
+        checkParamNull("record", record);
+        checkParamNull("key", key);
         // create command
         DBCommand cmd = db.createCommand();        
         for (DBColumn column : this.columns)
@@ -772,13 +745,8 @@ public abstract class DBRowSet extends DBExpr implements 
Entity
         }
         // Set key constraints
         setKeyConstraints(cmd, key);
-        try {
-            // Read Record
-            readRecord(rec, cmd);
-        } catch (QueryNoResultException e) {
-            // Translate exception
-            throw new RecordNotFoundException(this, key);
-        }
+        // Read Record
+        readRecord(record, cmd);
     }
 
     /**
@@ -791,8 +759,8 @@ public abstract class DBRowSet extends DBExpr implements 
Entity
     public boolean recordExists(Object[] key, DBContext context)
     {
         // Check Arguments
-        if (context == null)
-            throw new InvalidArgumentException("context", context);
+        checkParamNull("key", key);
+        checkParamNull("context", context);
         // Select
         DBCommand cmd = db.createCommand();
         cmd.select(count());
@@ -824,34 +792,33 @@ public abstract class DBRowSet extends DBExpr implements 
Entity
      * If the record has been modified by another user, an error of type 
      * DBErrors.RecordUpdateFailed will be set.  
      * <P>
-     * @param rec the DBRecord object. contains all fields and the field 
properties
+     * @param record the DBRecord object. contains all fields and the field 
properties
      */
-    public void updateRecord(DBRecord rec)
+    public void updateRecord(DBRecord record)
     {
         // check updateable
         if (isUpdateable()==false)
             throw new NotSupportedException(this, "updateRecord");
         // Check Arguments
-        if (rec == null)
-            throw new InvalidArgumentException("record", rec);
-        if (rec.isValid()==false)
-            throw new ObjectNotValidException(rec);
+        checkParamNull("record", record);
+        if (record.isValid()==false)
+            throw new ObjectNotValidException(record);
         // the connection
-        DBContext context = rec.getContext();
+        DBContext context = record.getContext();
         Connection conn = context.getConnection();
         // Get the new Timestamp
         String name = getName();
         Timestamp timestamp = (timestampColumn!=null) ? 
context.getDbms().getUpdateTimestamp(conn) : null;
         DBMSHandler.DBSetGenKeys setGenKey = null;
         // Get the fields and the flags
-        Object[] fields = rec.getFields();
+        Object[] fields = record.getFields();
         // Build SQL-Statement
         DBCommand cmd = db.createCommand();
         String sql = null;
         int setCount = 0;
         // Perform action
         DBColumn[] keyColumns =(DBColumn[])getKeyColumns();
-        DBRecord.State recordState = rec.getState(); 
+        DBRecord.State recordState = record.getState(); 
         if (recordState==DBRecord.State.New)
         {      // Insert Record
             for (int i = 0; i < columns.size(); i++)
@@ -915,7 +882,7 @@ public abstract class DBRowSet extends DBExpr implements 
Entity
                     // next
                     continue;
                }
-               boolean modified = rec.wasModified(i);
+               boolean modified = record.wasModified(i);
                boolean empty = ObjectUtils.isEmpty(value); 
                 DBTableColumn col = (DBTableColumn) columns.get(i);
                 if (ObjectUtils.contains(keyColumns, col))
@@ -974,21 +941,21 @@ public abstract class DBRowSet extends DBExpr implements 
Entity
         } 
         else if (affected == 0)
         { // Record not found
-            throw new RecordUpdateInvalidException(this, getRecordKey(rec));
+            throw new RecordUpdateInvalidException(this, record.getKey());
         } 
         else if (affected > 1)
         { // Multiple Records affected
-            throw new RecordUpdateFailedException(this, getRecordKey(rec));
+            throw new RecordUpdateFailedException(this, record.getKey());
         }
         // Correct Timestamp
         if (timestampColumn!=null && timestamp!=null)
         {   // Set the correct Timestamp
-            int i = rec.getFieldIndex(timestampColumn);
+            int i = record.getFieldIndex(timestampColumn);
             if (i >= 0)
                 fields[i] = timestamp;
         }
         // Change State
-        rec.updateComplete();        
+        record.updateComplete();        
     }
     
     /**

Reply via email to