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 09:43:11 UTC
[empire-db] branch version3 updated: EMPIREDB-362 DBRecord rowsetData rework
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 72cc786 EMPIREDB-362 DBRecord rowsetData rework
72cc786 is described below
commit 72cc786b7f0251145500b81039aa7c9451ecb8ca
Author: Rainer Döbele <do...@apache.org>
AuthorDate: Thu Jan 27 10:43:09 2022 +0100
EMPIREDB-362 DBRecord rowsetData rework
---
.../org/apache/empire/commons/ObjectUtils.java | 26 +++++++---
.../org/apache/empire/data/list/DataListHead.java | 7 +--
.../main/java/org/apache/empire/db/DBQuery.java | 36 ++++++-------
.../main/java/org/apache/empire/db/DBReader.java | 8 ++-
.../main/java/org/apache/empire/db/DBRecord.java | 21 ++------
.../main/java/org/apache/empire/db/DBRowSet.java | 59 ++++++++++------------
.../apache/empire/db/expr/column/DBAliasExpr.java | 2 +-
7 files changed, 78 insertions(+), 81 deletions(-)
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 c440c34..79eb745 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
@@ -946,35 +946,49 @@ public final class ObjectUtils
{
if (array==null)
return false;
+ // 1st try (quick)
for (int i=0; i<array.length; i++)
{
if (array[i]==item)
return true;
}
+ // 2nd try (thorough)
+ for (int i=0; i<array.length; i++)
+ {
+ if (array[i]!=null && array[i].equals(item))
+ return true;
+ }
+ // not found
return false;
}
/**
* returns whether or not a array contains a certain item
- * Uses the equals() operator to compare
+ * performs a simple (==) comparison (fast)
*
* @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)
+ public static <T> int indexOf(T[] array, T item)
{
if (array==null)
- return false;
+ return -1;
+ // 1st try (quick)
for (int i=0; i<array.length; i++)
{
if (array[i]==item)
- return true;
+ return i;
+ }
+ // 2nd try (thorough)
+ for (int i=0; i<array.length; i++)
+ {
if (array[i]!=null && array[i].equals(item))
- return true;
+ return i;
}
- return false;
+ // not found
+ return -1;
}
}
diff --git a/empire-db/src/main/java/org/apache/empire/data/list/DataListHead.java b/empire-db/src/main/java/org/apache/empire/data/list/DataListHead.java
index 325858a..91a4dae 100644
--- a/empire-db/src/main/java/org/apache/empire/data/list/DataListHead.java
+++ b/empire-db/src/main/java/org/apache/empire/data/list/DataListHead.java
@@ -51,9 +51,10 @@ public class DataListHead implements Serializable
public int getColumnIndex(ColumnExpr column)
{
- for (int i=0; i<columns.length; i++)
- if (columns[i]==column || columns[i].unwrap()==column)
- return i;
+ // find
+ int i = ObjectUtils.indexOf(columns, column);
+ if (i>=0)
+ return i;
// Not found, try by name
return getColumnIndex(column.getName());
}
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 5649085..e8bf8df 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
@@ -312,13 +312,11 @@ public class DBQuery extends DBRowSet
if (record == null || record.getRowSet() != this)
throw new InvalidArgumentException("record", record);
// 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;
+ Object rowSetData = getRowsetData(record);
+ if (rowSetData instanceof Object[])
+ return (Object[])rowSetData;
+ // generate key now
+ return super.getRecordKey(record);
}
/**
@@ -345,25 +343,26 @@ public class DBQuery extends DBRowSet
* Add rowset data
*/
@Override
- protected void initRecord(DBRecord rec, DBRecordData recData, Object rowSetData)
+ public void initRecord(DBRecord rec, DBRecordData recData)
{
- /*
+ // init
+ super.initRecord(rec, recData);
+ // set record key as rowset data (optional)
if (keyColumns!=null)
{ // check
- if (rowSetData!=null && !(rowSetData instanceof Object[]) && ((Object[])rowSetData).length!=keyColumns.length)
- throw new InvalidArgumentException("rowSetData", rowSetData);
+ Object rowsetData = getRowsetData(rec);
+ if (rowsetData!=null && !(rowsetData instanceof Object[]) && ((Object[])rowsetData).length!=keyColumns.length)
+ throw new InvalidArgumentException("rowSetData", rowsetData);
// create key if not already set
- if (rowSetData==null)
+ if (rowsetData==null)
{ // create key
Object[] recordKey = new Object[keyColumns.length];
for (int i=0; i<recordKey.length; i++)
recordKey[i]=recData.getValue(keyColumns[i]);
- rowSetData = recordKey;
+ rowsetData = recordKey;
+ setRowsetData(rec, rowsetData);
}
}
- */
- // int
- super.initRecord(rec, recData, rowSetData);
}
/**
@@ -406,7 +405,8 @@ public class DBQuery extends DBRowSet
// Read Record
try {
// Read Record
- readRecord(rec, cmd, null); // key.clone()
+ readRecord(rec, cmd);
+ // setRowsetData(rec, key.clone()); /* not required */
} catch (QueryNoResultException e) {
// Record not found
throw new RecordNotFoundException(this, key);
@@ -590,7 +590,7 @@ public class DBQuery extends DBRowSet
}
}
// success
- rec.updateComplete(key);
+ rec.updateComplete();
}
/**
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 aafc759..e80cfd8 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
@@ -892,11 +892,9 @@ public class DBReader extends DBRecordData implements DBContextAware, Closeable
if (columns == null)
return -1;
// First chance: Try to find an exact match
- for (int i = 0; i < columns.length; i++)
- {
- if (columns[i].equals(column))
- return i;
- }
+ int index = ObjectUtils.indexOf(columns, column);
+ if (index>= 0)
+ return index;
// Second chance: Try Update Column
if (column instanceof DBColumn)
{
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 8e92874..81cbc04 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
@@ -234,7 +234,7 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
private State state;
private Object[] fields;
private boolean[] modified;
- private Object rowsetData; // Special Rowset Data (usually null)
+ Object rowsetData; // Special Rowset Data (usually null)
// options
protected boolean enableRollbackHandling;
@@ -1125,16 +1125,6 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
Object[] key2 = other.getKey();
return ObjectUtils.compareEqual(key1, key2);
}
-
- /**
- * Returns the DBRowSet object.
- *
- * @return the DBRowSet object
- */
- protected Object getRowSetData()
- {
- return rowsetData;
- }
/**
* This function provides direct access to the record fields.<BR>
@@ -1179,7 +1169,7 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
* @param rowsetData any further RowSet specific data
* @param newRecord
*/
- protected void initData(Object rowsetData, boolean newRecord)
+ protected void initData(boolean newRecord)
{
// Init rowset
int colCount = rowset.getColumns().size();
@@ -1192,8 +1182,8 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
fields[i]=null;
}
// Set State
- this.rowsetData = rowsetData;
this.modified = null;
+ this.rowsetData = null;
changeState((rowset==null ? State.Invalid : (newRecord ? State.New : State.Valid)));
}
@@ -1202,11 +1192,8 @@ public class DBRecord extends DBRecordData implements DBContextAware, Record, Cl
* This will set change the record's state to Valid
* @param rowsetData additional data held by the rowset for this record (optional)
*/
- protected void updateComplete(Object rowsetData)
+ protected void updateComplete()
{
- // change rowsetData
- if (rowsetData!=ObjectUtils.NO_VALUE)
- this.rowsetData = rowsetData;
// Change state
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 6ae2910..61b353a 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
@@ -503,7 +503,7 @@ public abstract class DBRowSet extends DBExpr implements Entity
protected void initRecord(DBRecord rec, Object[] keyValues, Connection conn, boolean setDefaults, boolean newRecord)
{
// Prepare
- prepareInitRecord(rec, null, newRecord);
+ prepareInitRecord(rec, newRecord);
// Initialize all Fields
Object[] fields = rec.getFields();
/*
@@ -536,17 +536,6 @@ public abstract class DBRowSet extends DBExpr implements Entity
// Init
completeInitRecord(rec);
}
-
- /**
- * Initializes a DBRecord for this rowset using the record data provided (i.e. from a DBReader)<BR>
- * The record may then be modified and updated.<BR>
- * At least all primary key columns must be supplied.<BR>
- * We strongly recommend to supply the value of the update timestamp column in order to detect concurrent changes.<BR>
- */
- public void initRecord(DBRecord rec, DBRecordData recData)
- {
- initRecord(rec, recData, null);
- }
/**
* Initializes a DBRecord for this rowset using the record data provided (i.e. from a DBReader)<BR>
@@ -557,10 +546,10 @@ public abstract class DBRowSet extends DBExpr implements Entity
* @param rec the record object
* @param recData the record data from which to initialized the record
*/
- protected void initRecord(DBRecord rec, DBRecordData recData, Object rowSetData)
+ public void initRecord(DBRecord rec, DBRecordData recData)
{
// Initialize the record
- prepareInitRecord(rec, rowSetData, false);
+ prepareInitRecord(rec, false);
// Get Record Field Values
Object[] fields = rec.getFields();
DBColumn[] keyColumns =(DBColumn[])getKeyColumns();
@@ -621,14 +610,14 @@ public abstract class DBRowSet extends DBExpr implements Entity
* @param rowSetData any further RowSet specific data
* @param insert
*/
- protected void prepareInitRecord(DBRecord rec, Object rowSetData, boolean newRecord)
+ protected void prepareInitRecord(DBRecord rec, boolean newRecord)
{
if (rec==null || rec.getRowSet()!=this)
throw new InvalidArgumentException("rec", rec);
if (columns.size() < 1)
throw new ObjectNotValidException(this);
// Init
- rec.initData(rowSetData, newRecord);
+ rec.initData(newRecord);
}
/**
@@ -676,31 +665,19 @@ public abstract class DBRowSet extends DBExpr implements Entity
* @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, Object rowSetData)
+ protected void readRecord(DBRecord rec, DBCommand cmd)
{
DBReader reader = null;
try
{ // read record using a DBReader
reader = new DBReader(rec.getContext(), false);
reader.getRecordData(cmd);
- initRecord(rec, reader, rowSetData);
+ initRecord(rec, reader);
} finally {
reader.close();
}
}
-
- /**
- * 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 cmd the SQL-Command used to query the record
- */
- protected final void readRecord(DBRecord rec, DBCommand cmd)
- {
- readRecord(rec, cmd, null);
- }
/**
* Reads the record with the given primary key from the database.
@@ -1005,7 +982,7 @@ public abstract class DBRowSet extends DBExpr implements Entity
fields[i] = timestamp;
}
// Change State
- rec.updateComplete(ObjectUtils.NO_VALUE);
+ rec.updateComplete();
}
/**
@@ -1096,6 +1073,26 @@ public abstract class DBRowSet extends DBExpr implements Entity
}
// Done
}
+
+ /**
+ * Returns additional data stored on a record by the RowSet
+ * @param record the record
+ * @return the rowset data
+ */
+ protected Object getRowsetData(DBRecord record)
+ {
+ return record.rowsetData;
+ }
+
+ /**
+ * May be used by a Rowset to store additional data on a record
+ * @param rec the record
+ * @return the rowset data
+ */
+ protected void setRowsetData(DBRecord record, Object rowsetData)
+ {
+ record.rowsetData = rowsetData;
+ }
}
diff --git a/empire-db/src/main/java/org/apache/empire/db/expr/column/DBAliasExpr.java b/empire-db/src/main/java/org/apache/empire/db/expr/column/DBAliasExpr.java
index f13310b..1ca4c46 100644
--- a/empire-db/src/main/java/org/apache/empire/db/expr/column/DBAliasExpr.java
+++ b/empire-db/src/main/java/org/apache/empire/db/expr/column/DBAliasExpr.java
@@ -188,7 +188,7 @@ public class DBAliasExpr extends DBColumnExpr
@Override
public boolean equals(Object other)
{
- if (super.equals(other))
+ if (other==this || expr.equals(other))
return true;
// Check for another Alias Expression
if (other instanceof DBAliasExpr)