This is an automated email from the ASF dual-hosted git repository.

doebele pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/empire-db.git


The following commit(s) were added to refs/heads/master by this push:
     new 55b16c6  EMPIREDB-282 Added key generation function on DBTable for 
convenience
55b16c6 is described below

commit 55b16c652aae2e6bd614dddb1eefadd2b31ccb5e
Author: Rainer Döbele <[email protected]>
AuthorDate: Wed Sep 11 16:53:05 2019 +0200

    EMPIREDB-282
    Added key generation function on DBTable for convenience
---
 .../src/main/java/org/apache/empire/db/DBIndex.java      | 10 ++++++++++
 .../src/main/java/org/apache/empire/db/DBRecord.java     |  7 +++----
 .../src/main/java/org/apache/empire/db/DBTable.java      | 16 ++++++++++++++++
 3 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/empire-db/src/main/java/org/apache/empire/db/DBIndex.java 
b/empire-db/src/main/java/org/apache/empire/db/DBIndex.java
index de8b910..e1beb1f 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBIndex.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBIndex.java
@@ -115,6 +115,16 @@ public class DBIndex extends DBObject
     }
     
     /**
+     * Returns the number of columns belonging to this index.
+     * 
+     * @return the number of columns belonging to this index
+     */
+    public int getColumnCount()
+    {
+        return columns.length;
+    }
+    
+    /**
      * checks whether the columns of this index match the supplied columns
      * @param columns
      * @return true if columns match or false otherwise
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 c499c60..8a1f1cc 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
@@ -70,7 +70,7 @@ public class DBRecord extends DBRecordData implements Record, 
Cloneable
                return this.ordinal()>=other.ordinal();
        }
     }
-
+    
     protected static final Logger log    = 
LoggerFactory.getLogger(DBRecord.class);
 
     // This is the record data
@@ -793,7 +793,6 @@ public class DBRecord extends DBRecordData implements 
Record, Cloneable
      * @param keyValues a Object array, the primary key(s)
      * @param insert if true change the state of this object to REC_NEW
      */
-     
     public void init(DBRowSet table, Object[] keyValues, boolean insert)
     {  // Init with keys
         if (table!=null)
@@ -842,12 +841,12 @@ public class DBRecord extends DBRecordData implements 
Record, Cloneable
      * @param keys an array of the primary key values
      * @param conn a valid connection to the database.
      */
-    public void read(DBRowSet table, Object[] keys, Connection conn)
+    public void read(DBRowSet table, Object[] key, Connection conn)
     {
         if (table==null)
             throw new InvalidArgumentException("table", table);
         // read
-        table.readRecord(this, keys, conn);
+        table.readRecord(this, key, conn);
     }
 
     /**
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 45f5962..a0fa3a3 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
@@ -634,6 +634,22 @@ public class DBTable extends DBRowSet implements Cloneable
     {
         this.cascadeDeleteAction = cascadeDeleteAction;
     }
+    
+    /**
+     * Creates a record key from a list of key values.
+     * The supplied values must be in the correct order. 
+     * @param keyValues
+     * @return the record key
+     */
+    public Object[] key(Object... keyValues)
+    {   // Check size
+        if (keyValues==null || keyValues.length==0)
+            throw new InvalidArgumentException("keyValues", keyValues);
+        if (this.primaryKey!=null && 
keyValues.length!=this.primaryKey.getColumnCount())
+            throw new InvalidArgumentException("keyValues:length", 
keyValues.length);
+        // Return the key
+        return keyValues;
+    }
 
     /**
      * Creates a delete SQL-Command by using the DBCommand getDelete method

Reply via email to