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 878386f EMPIREDB-385 Change DBRecord.read() param from long to object
878386f is described below
commit 878386fb29e81daabcaa421f453746b50b0fac4d
Author: Rainer Döbele <[email protected]>
AuthorDate: Thu Mar 17 16:51:33 2022 +0100
EMPIREDB-385 Change DBRecord.read() param from long to object
---
.../samples/db/advanced/records/BrandRecord.java | 10 -------
.../samples/db/advanced/records/ModelRecord.java | 2 +-
.../main/java/org/apache/empire/db/DBCmdParam.java | 1 -
.../main/java/org/apache/empire/db/DBRecord.java | 35 +++++++++++++++++++---
.../java/org/apache/empire/db/DBRecordBean.java | 34 ++++++++++++++++++---
5 files changed, 62 insertions(+), 20 deletions(-)
diff --git
a/empire-db-examples/empire-db-example-advanced/src/main/java/org/apache/empire/samples/db/advanced/records/BrandRecord.java
b/empire-db-examples/empire-db-example-advanced/src/main/java/org/apache/empire/samples/db/advanced/records/BrandRecord.java
index 3efd4ba..80a2c1b 100644
---
a/empire-db-examples/empire-db-example-advanced/src/main/java/org/apache/empire/samples/db/advanced/records/BrandRecord.java
+++
b/empire-db-examples/empire-db-example-advanced/src/main/java/org/apache/empire/samples/db/advanced/records/BrandRecord.java
@@ -18,7 +18,6 @@
*/
package org.apache.empire.samples.db.advanced.records;
-import org.apache.empire.db.DBRecord;
import org.apache.empire.db.generic.TRecord;
import org.apache.empire.samples.db.advanced.SampleContext;
import org.apache.empire.samples.db.advanced.db.CarSalesDB;
@@ -50,13 +49,4 @@ public class BrandRecord extends TRecord<SampleContext,
CarSalesDB.Brand>
return this;
}
- /**
- * Overloaded read for convenience
- * @param wmi
- */
- public void read(String wmi)
- {
- super.read(DBRecord.key(wmi));
- }
-
}
diff --git
a/empire-db-examples/empire-db-example-advanced/src/main/java/org/apache/empire/samples/db/advanced/records/ModelRecord.java
b/empire-db-examples/empire-db-example-advanced/src/main/java/org/apache/empire/samples/db/advanced/records/ModelRecord.java
index 5587b2c..2ea250a 100644
---
a/empire-db-examples/empire-db-example-advanced/src/main/java/org/apache/empire/samples/db/advanced/records/ModelRecord.java
+++
b/empire-db-examples/empire-db-example-advanced/src/main/java/org/apache/empire/samples/db/advanced/records/ModelRecord.java
@@ -81,7 +81,7 @@ public class ModelRecord extends TRecord<SampleContext,
CarSalesDB.Model>
{
if (brand==null) {
brand = new BrandRecord(CTX);
- brand.read(getString(T.WMI));
+ brand.read(get(T.WMI));
}
return brand;
}
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBCmdParam.java
b/empire-db/src/main/java/org/apache/empire/db/DBCmdParam.java
index e9961cc..0118385 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBCmdParam.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBCmdParam.java
@@ -18,7 +18,6 @@
*/
package org.apache.empire.db;
-import java.math.BigDecimal;
import java.util.Set;
import org.apache.empire.commons.ObjectUtils;
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 12edb5f..9b32cc0 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
@@ -22,12 +22,15 @@ import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
+import java.util.Collection;
import org.apache.empire.commons.ClassUtils;
+import org.apache.empire.commons.ObjectUtils;
import org.apache.empire.commons.StringUtils;
import org.apache.empire.data.Column;
import org.apache.empire.data.Record;
import org.apache.empire.db.DBRowSet.PartialMode;
+import org.apache.empire.db.exceptions.InvalidKeyException;
import org.apache.empire.db.exceptions.NoPrimaryKeyException;
import org.apache.empire.db.expr.compare.DBCompareExpr;
import org.apache.empire.exceptions.InvalidArgumentException;
@@ -263,8 +266,10 @@ public class DBRecord extends DBRecordBase
/**
* Reads a record from the database
- * Hint: variable args param (Object...) caused problems with migration
* @param key an array of the primary key values
+ *
+ * @throws NoPrimaryKeyException if the associated RowSet has no primary
key
+ * @throws InvalidKeyException if the key does not match the key columns
of the associated RowSet
*/
public DBRecord read(Object[] key)
{ // read
@@ -276,11 +281,33 @@ public class DBRecord extends DBRecordBase
/**
* Reads a record from the database
- * @param identity the record id value
+ * This method can only be used for tables with a single primary key
+ * @param id the primary key of the record
+ *
+ * @throws NoPrimaryKeyException if the associated RowSet has no primary
key
+ * @throws InvalidKeyException if the associated RowSet does not have a
single column primary key
*/
- public final DBRecord read(long identity)
+ public DBRecord read(Object id)
{
- return read(new Object[] { identity });
+ if (ObjectUtils.isEmpty(id))
+ throw new InvalidArgumentException("id", id);
+ // convert to array
+ Object[] key;
+ if (id instanceof Object[])
+ { // Cast to array
+ key = (Object[])id;
+ } else if (id instanceof Collection<?>) {
+ // Convert collection to array
+ Collection<?> col = (Collection<?>)id;
+ key = new Object[col.size()];
+ int i=0;
+ for (Object v : col)
+ key[i++] = v;
+ } else {
+ // Single value
+ key = new Object[] { id };
+ }
+ return read(key);
}
/**
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBRecordBean.java
b/empire-db/src/main/java/org/apache/empire/db/DBRecordBean.java
index 0aa366f..8c0ceb5 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBRecordBean.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBRecordBean.java
@@ -19,7 +19,9 @@
package org.apache.empire.db;
import java.sql.Connection;
+import java.util.Collection;
+import org.apache.empire.commons.ObjectUtils;
import org.apache.empire.commons.StringUtils;
import org.apache.empire.data.Column;
import org.apache.empire.db.DBRowSet.PartialMode;
@@ -187,8 +189,10 @@ public class DBRecordBean extends DBRecordBase
/**
* Reads a record from the database
- * Hint: variable args param (Object...) caused problems with migration
* @param key an array of the primary key values
+ *
+ * @throws NoPrimaryKeyException if the associated RowSet has no primary
key
+ * @throws InvalidKeyException if the key does not match the key columns
of the associated RowSet
*/
public DBRecordBean read(DBContext context, DBRowSet rowset, Object[] key)
{ // read
@@ -204,11 +208,33 @@ public class DBRecordBean extends DBRecordBase
/**
* Reads a record from the database
- * @param identity the record id value
+ * This method can only be used for tables with a single primary key
+ * @param id the primary key of the record
+ *
+ * @throws NoPrimaryKeyException if the associated RowSet has no primary
key
+ * @throws InvalidKeyException if the associated RowSet does not have a
single column primary key
*/
- public final DBRecordBean read(DBContext context, DBRowSet rowset, long
identity)
+ public DBRecordBean read(DBContext context, DBRowSet rowset, Object id)
{
- return read(context, rowset, new Object[] { identity });
+ if (ObjectUtils.isEmpty(id))
+ throw new InvalidArgumentException("id", id);
+ // convert to array
+ Object[] key;
+ if (id instanceof Object[])
+ { // Cast to array
+ key = (Object[])id;
+ } else if (id instanceof Collection<?>) {
+ // Convert collection to array
+ Collection<?> col = (Collection<?>)id;
+ key = new Object[col.size()];
+ int i=0;
+ for (Object v : col)
+ key[i++] = v;
+ } else {
+ // Single value
+ key = new Object[] { id };
+ }
+ return read(context, rowset, key);
}
/**