diff --git a/src/java/com/samskivert/jdbc/depot/BindVisitor.java b/src/java/com/samskivert/jdbc/depot/BindVisitor.java index 672cf09..1274c07 100644 --- a/src/java/com/samskivert/jdbc/depot/BindVisitor.java +++ b/src/java/com/samskivert/jdbc/depot/BindVisitor.java @@ -72,10 +72,9 @@ public class BindVisitor implements ExpressionVisitor public void visit (WhereCondition whereCondition) throws Exception { - Comparable[] values = whereCondition.getValues(); - for (int ii = 0; ii < values.length; ii ++) { - if (values[ii] != null) { - _stmt.setObject(_argIdx ++, values[ii]); + for (Comparable value : whereCondition.getValues()) { + if (value != null) { + _stmt.setObject(_argIdx ++, value); } } } diff --git a/src/java/com/samskivert/jdbc/depot/BuildVisitor.java b/src/java/com/samskivert/jdbc/depot/BuildVisitor.java index c55409d..0a59e1a 100644 --- a/src/java/com/samskivert/jdbc/depot/BuildVisitor.java +++ b/src/java/com/samskivert/jdbc/depot/BuildVisitor.java @@ -21,6 +21,7 @@ package com.samskivert.jdbc.depot; import java.sql.SQLException; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -98,7 +99,7 @@ public abstract class BuildVisitor implements ExpressionVisitor { Class pClass = whereCondition.getPersistentClass(); String[] keyFields = Key.getKeyFields(pClass); - Comparable[] values = whereCondition.getValues(); + List values = whereCondition.getValues(); for (int ii = 0; ii < keyFields.length; ii ++) { if (ii > 0) { _builder.append(" and "); @@ -109,7 +110,7 @@ public abstract class BuildVisitor implements ExpressionVisitor _enableOverrides = true; appendRhsColumn(pClass, keyFields[ii]); _enableOverrides = saved; - _builder.append(values[ii] == null ? " is null " : " = ? "); + _builder.append(values.get(ii) == null ? " is null " : " = ? "); } } diff --git a/src/java/com/samskivert/jdbc/depot/FindAllQuery.java b/src/java/com/samskivert/jdbc/depot/FindAllQuery.java index 26e4f4e..c15f81b 100644 --- a/src/java/com/samskivert/jdbc/depot/FindAllQuery.java +++ b/src/java/com/samskivert/jdbc/depot/FindAllQuery.java @@ -117,7 +117,7 @@ public abstract class FindAllQuery // Single-column keys result in the compact IN(keyVal1, keyVal2, ...) Comparable[] keyFieldValues = new Comparable[fetchKeys.size()]; for (int ii = 0; ii < keyFieldValues.length; ii ++) { - keyFieldValues[ii] = fetchKeys.get(ii).condition.getValues()[0]; + keyFieldValues[ii] = fetchKeys.get(ii).condition.getValues().get(0); } condition = new In(_type, _marsh.getPrimaryKeyFields()[0], keyFieldValues); diff --git a/src/java/com/samskivert/jdbc/depot/Key.java b/src/java/com/samskivert/jdbc/depot/Key.java index 2fc9ccb..1b81ca7 100644 --- a/src/java/com/samskivert/jdbc/depot/Key.java +++ b/src/java/com/samskivert/jdbc/depot/Key.java @@ -23,7 +23,6 @@ package com.samskivert.jdbc.depot; import java.io.Serializable; import java.lang.reflect.Field; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Map; @@ -44,9 +43,10 @@ public class Key extends WhereClause implements SQLExpression, CacheKey, ValidatingCacheInvalidator, Serializable { /** An expression that contains our key columns and values. */ - public static class WhereCondition implements SQLExpression + public static class WhereCondition + implements SQLExpression, Serializable { - public WhereCondition (Class pClass, Comparable[] values) + public WhereCondition (Class pClass, ArrayList values) { _pClass = pClass; _values = values; @@ -69,7 +69,7 @@ public class Key extends WhereClause return _pClass; } - public Comparable[] getValues () + public ArrayList getValues () { return _values; } @@ -84,17 +84,17 @@ public class Key extends WhereClause return false; } WhereCondition other = (WhereCondition) obj; - return _pClass == other._pClass && Arrays.equals(_values, other._values); + return _pClass == other._pClass && _values.equals(other.getValues()); } @Override public int hashCode () { - return _pClass.hashCode() ^ Arrays.hashCode(_values); + return _pClass.hashCode() ^ _values.hashCode(); } protected Class _pClass; - protected Comparable[] _values; + protected ArrayList _values; } /** The expression that identifies our row. */ @@ -145,13 +145,19 @@ public class Key extends WhereClause String[] keyFields = getKeyFields(pClass); // now extract the values in field order and ensure none are extra or missing - Comparable[] newValues = new Comparable[fields.length]; + ArrayList newValues = new ArrayList(); for (int ii = 0; ii < keyFields.length; ii++) { - newValues[ii] = map.remove(keyFields[ii]); - // make sure we were provided with a value for this primary key field - if (newValues[ii] == null) { + Comparable nugget = map.remove(keyFields[ii]); + if (nugget == null) { + // make sure we were provided with a value for this primary key field throw new IllegalArgumentException("Missing value for key field: " + keyFields[ii]); } + if (nugget instanceof Serializable) { + newValues.add(nugget); + continue; + } + throw new IllegalArgumentException( + "Non-serializable argument [key=" + keyFields[ii] + ", arg=" + nugget + "]"); } // finally make sure we were not given any fields that are not in fact primary key fields @@ -185,7 +191,7 @@ public class Key extends WhereClause // from CacheKey public Serializable getCacheKey () { - return this; // TODO: Optimally return a special class here containing only _values. + return condition.getValues(); } // from ValidatingCacheInvalidator @@ -240,7 +246,7 @@ public class Key extends WhereClause if (ii > 0) { builder.append(", "); } - builder.append(keyFields[ii]).append("=").append(condition.getValues()[ii]); + builder.append(keyFields[ii]).append("=").append(condition.getValues().get(ii)); } builder.append(")"); return builder.toString();