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/21 12:57:48 UTC

[empire-db] branch version3 updated: EMPIREDB-364 fixes

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 51fbaf8  EMPIREDB-364 fixes
51fbaf8 is described below

commit 51fbaf89200941490645e41dcb0b0a5a4e6d104e
Author: Rainer Döbele <do...@apache.org>
AuthorDate: Fri Jan 21 13:57:46 2022 +0100

    EMPIREDB-364 fixes
---
 .../empire/vue/sample/db/records/SampleRecord.java |   2 +-
 .../main/java/org/apache/empire/db/DBQuery.java    |   4 +-
 .../main/java/org/apache/empire/db/DBRecord.java   | 124 +++++++--------------
 .../main/java/org/apache/empire/db/DBRowSet.java   |   2 +-
 .../java/org/apache/empire/db/SerializeTest.java   |   2 +-
 5 files changed, 43 insertions(+), 91 deletions(-)

diff --git a/empire-db-examples/empire-db-example-vue/src/main/java/org/apache/empire/vue/sample/db/records/SampleRecord.java b/empire-db-examples/empire-db-example-vue/src/main/java/org/apache/empire/vue/sample/db/records/SampleRecord.java
index 49df4a1..2c6976f 100644
--- a/empire-db-examples/empire-db-example-vue/src/main/java/org/apache/empire/vue/sample/db/records/SampleRecord.java
+++ b/empire-db-examples/empire-db-example-vue/src/main/java/org/apache/empire/vue/sample/db/records/SampleRecord.java
@@ -93,7 +93,7 @@ public abstract class SampleRecord<T extends SampleTable> extends DBRecord {
         // load original record
         if (newRecord)
         {   // init a new record
-            super.init(T, key, true);
+            super.init(key, true);
         }
         else
         {   // read the current record
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 04bf5a1..1015358 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
@@ -405,7 +405,7 @@ public class DBQuery extends DBRowSet
             // Read Record
             readRecord(rec, cmd);
             // Set RowSetData
-            rec.updateComplete(key.clone());
+            rec.onUpdateComplete(key.clone());
         } catch (QueryNoResultException e) {
             // Record not found
             throw new RecordNotFoundException(this, key);
@@ -588,7 +588,7 @@ public class DBQuery extends DBRowSet
             }
         }
         // success
-        rec.updateComplete(keys);
+        rec.onUpdateComplete(keys);
     }
 
     /**
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 5dbe56a..bf04d55 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
@@ -119,22 +119,6 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
             // check modified
             if (modified==null && s.modified!=null)
                 modified = copy(s.modified);
-            /*
-            if (this.fields==s.fields)
-            {
-                // combine modified
-                if (this.modified==null)
-                    this.modified =s.modified;  // for delete case only!
-                else if (s.modified!=null)
-                    combineModified(s.modified);
-            }
-            else
-            {   // warn
-                log.warn("record fields have changed {}/{}", record.getRowSet().getName(), StringUtils.arrayToString(record.getKeyValues(), "|"));
-                this.fields = s.fields;
-                this.modified = s.modified;
-            }
-            */
         }
 
         @Override
@@ -199,6 +183,16 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
     		return this.ordinal()>=other.ordinal();
     	}
     }
+
+    /**
+     * varArgs to Array
+     * @param parts
+     * @return
+     */
+    public static Object[] toKey(Object... parts)
+    {
+        return parts;
+    }
     
     protected static final Logger log    = LoggerFactory.getLogger(DBRecord.class);
 
@@ -210,10 +204,12 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
     private State           state;
     private Object[]        fields;
     private boolean[]       modified;
-    private boolean         validateFieldValues;
     // Special Rowset Data (usually null)
     private Object          rowsetData;
 
+    // options
+    private boolean         validateFieldValues;
+    
     /**
      * Create a new DBRecord object.<BR>
      * The record is not attached to a RowSet and the record's state is initially set to REC_INVALID.
@@ -301,13 +297,15 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
     @Override
     public void close()
     {
-        // rowset = null; -- do not change this --
+        // clear fields
         fields = null;
         modified = null;
         rowsetData = null;
         // change state
         if (state!=State.Invalid)
             changeState(State.Invalid);
+        // done
+        onRecordChanged();
     }
     
     /** {@inheritDoc} */
@@ -948,31 +946,13 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
     }
 
     /**
-     * Initializes this record object by attaching it to a rowset,
-     * setting its primary key values and setting the record state.<BR>
-     * This function is useful for updating a record without prior reading.
-     * <P>
-     * @param table the rowset
-     * @param keyValues a Object array, the primary key(s)
-     * @param insert if true change the state of this object to REC_NEW
-     */
-    protected void init(DBRowSet table, Object[] keyValues, boolean insert)
-    { 	// Init with keys
-        if (table!=null)
-            table.initRecord(this, keyValues, insert);
-        else
-            initData(null, false);
-    }
-
-    /**
      * Initializes this record object 
-     * @param table
-     * @param keyValues
-     * @param insert
+     * @param key the record key
+     * @param insert if true the record will be in the state NEW otherwise the state will be VALID
      */
-    public void init(boolean statusNew, Object... key)
+    public void init(Object[] key, boolean insert)
     {   // Init with keys
-        rowset.initRecord(this, key, statusNew);
+        rowset.initRecord(this, key, insert);
     }
 
     /**
@@ -981,8 +961,6 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
     public void create(boolean deferredInit)
     {
         rowset.createRecord(this, deferredInit);
-        // remove rollback
-        context.removeRollbackHandler(this);
     }
 
     /**
@@ -999,23 +977,8 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
      * @param key an array of the primary key values
      */
     public void read(Object[] key)
-    {   
-        /*
-        // temporarily check for rowset as first parameter
-        // invalid due to conversion from old Api where Rowset was first param
-        if (key[0] instanceof DBRowSet)
-            throw new MiscellaneousErrorException("Version 3 Migration Error: DBRecord.read() invalid key!");
-        
-        // temporarily check for connection
-        // invalid due to conversion from old Api where Connection was the last param
-        for (int i=0; i<key.length; i++)
-            if (key[i] instanceof Connection)
-                throw new MiscellaneousErrorException("Version 3 Migration Error: DBRecord.read() invalid key!");
-        */        
-        // read
+    {   // read
         rowset.readRecord(this, key);
-        // remove rollback
-        context.removeRollbackHandler(this);
     }
 
     /**
@@ -1046,8 +1009,6 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
         cmd.select(rowset.getColumns());
         cmd.where(whereConstraints);
         rowset.readRecord(this, cmd);
-        // remove rollback
-        context.removeRollbackHandler(this);
     }
 
     /**
@@ -1064,18 +1025,6 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
         // update
         rowset.updateRecord(this);
     }
-    
-    /**
-     * This method is used internally to indicate that the record update has completed<BR>
-     * This will set change the record's state to Valid
-     * @param rowSetData additional data held by the rowset for this record (optional)
-     */
-    void updateComplete(Object rowSetData)
-    {
-        this.rowsetData = rowSetData;
-        this.modified = null;
-        changeState(State.Valid);
-    }
 
     /**
      * This helper function calls the DBRowset.deleteRecord method 
@@ -1087,19 +1036,6 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
      * @see org.apache.empire.db.DBTable#deleteRecord(Object[], Connection)
      * @param conn a valid connection to the database.
      */
-    protected void delete(Connection conn)
-    {
-        if (isValid()==false)
-            throw new ObjectNotValidException(this);
-        // Delete only if record is not new
-        if (!isNew())
-        {
-            Object[] keys = rowset.getRecordKey(this);
-            rowset.deleteRecord(keys, context);
-        }
-        close();
-    }
-    
     public void delete()
     {
         if (isValid()==false)
@@ -1241,9 +1177,10 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
      *  
      * @return the number of fields set to default
      */
-    public int fillMissingDefaults(Connection conn)
+    public int fillMissingDefaults(boolean allowConnect)
     {
         int count = 0;
+        Connection conn = (allowConnect ? context.getConnection() : null);
         for (int i = 0; i < fields.length; i++)
         {
             if (fields[i] == ObjectUtils.NO_VALUE)
@@ -1366,6 +1303,9 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
     {
         if (log.isTraceEnabled() && isValid())
             log.trace("Record has been changed");
+        // remove rollback (but not when close is called)
+        if (fields!=null)
+            context.removeRollbackHandler(this);
     }
     
     /**
@@ -1377,4 +1317,16 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
             log.debug("Record field " + rowset.getColumn(i).getName() + " changed to " + String.valueOf(fields[i]));
     }
     
+    /**
+     * This method is used internally to indicate that the record update has completed<BR>
+     * This will set change the record's state to Valid
+     * @param rowSetData additional data held by the rowset for this record (optional)
+     */
+    protected void onUpdateComplete(Object rowSetData)
+    {
+        this.rowsetData = rowSetData;
+        this.modified = null;
+        changeState(State.Valid);
+    }
+    
 }
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 013c104..0d415dc 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
@@ -937,7 +937,7 @@ public abstract class DBRowSet extends DBExpr
                 fields[i] = timestamp;
         }
         // Change State
-        rec.updateComplete(rec.getRowSetData());
+        rec.onUpdateComplete(rec.getRowSetData());        
     }
     
     /**
diff --git a/empire-db/src/test/java/org/apache/empire/db/SerializeTest.java b/empire-db/src/test/java/org/apache/empire/db/SerializeTest.java
index aae916e..dc10b0e 100644
--- a/empire-db/src/test/java/org/apache/empire/db/SerializeTest.java
+++ b/empire-db/src/test/java/org/apache/empire/db/SerializeTest.java
@@ -58,7 +58,7 @@ public class SerializeTest {
     final TestDatabase db = new TestDatabase();
     DBContext context = new DBContextStatic(new MockDriver(), null); 
     DBRecord rec = new DBRecord(context, db.T_TEST);
-    rec.init(true);
+    rec.init(null, true);
     db.open(context);
     return new DBObject[] {
             db,