From 4bcfda861d8909cfe9603bd65a28424cc4d808dd Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 5 Sep 2008 20:05:44 +0000 Subject: [PATCH] Ah, Key actually is PrimaryKey and does not in fact keep the primary key columns around. Cleaned some things up along those lines. --- .../samskivert/jdbc/depot/BindVisitor.java | 5 +- .../samskivert/jdbc/depot/BuildVisitor.java | 6 +- src/java/com/samskivert/jdbc/depot/Key.java | 95 +++++-------------- .../samskivert/jdbc/depot/PrimaryKeySet.java | 2 +- .../depot/expression/ExpressionVisitor.java | 2 +- 5 files changed, 30 insertions(+), 80 deletions(-) diff --git a/src/java/com/samskivert/jdbc/depot/BindVisitor.java b/src/java/com/samskivert/jdbc/depot/BindVisitor.java index aa4b689..6fe3e98 100644 --- a/src/java/com/samskivert/jdbc/depot/BindVisitor.java +++ b/src/java/com/samskivert/jdbc/depot/BindVisitor.java @@ -25,7 +25,6 @@ import java.sql.SQLException; import java.util.Map; import java.util.Set; -import com.samskivert.jdbc.depot.Key.WhereCondition; import com.samskivert.jdbc.depot.clause.DeleteClause; import com.samskivert.jdbc.depot.clause.FieldDefinition; import com.samskivert.jdbc.depot.clause.ForUpdate; @@ -73,9 +72,9 @@ public class BindVisitor implements ExpressionVisitor // nothing needed } - public void visit (WhereCondition whereCondition) + public void visit (Key key) { - for (Comparable value : whereCondition.getValues()) { + for (Comparable value : key.getValues()) { if (value != null) { writeValueToStatement(value); } diff --git a/src/java/com/samskivert/jdbc/depot/BuildVisitor.java b/src/java/com/samskivert/jdbc/depot/BuildVisitor.java index ad7ded6..8c6e174 100644 --- a/src/java/com/samskivert/jdbc/depot/BuildVisitor.java +++ b/src/java/com/samskivert/jdbc/depot/BuildVisitor.java @@ -90,11 +90,11 @@ public abstract class BuildVisitor implements ExpressionVisitor } } - public void visit (Key.WhereCondition whereCondition) + public void visit (Key key) { - Class pClass = whereCondition.getPersistentClass(); + Class pClass = key.getPersistentClass(); String[] keyFields = KeyUtil.getKeyFields(pClass); - List> values = whereCondition.getValues(); + List> values = key.getValues(); for (int ii = 0; ii < keyFields.length; ii ++) { if (ii > 0) { _builder.append(" and "); diff --git a/src/java/com/samskivert/jdbc/depot/Key.java b/src/java/com/samskivert/jdbc/depot/Key.java index 3793efc..9dca48e 100644 --- a/src/java/com/samskivert/jdbc/depot/Key.java +++ b/src/java/com/samskivert/jdbc/depot/Key.java @@ -42,58 +42,6 @@ import com.samskivert.util.StringUtil; 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, Serializable - { - public WhereCondition (Class pClass, ArrayList> values) - { - _pClass = pClass; - _values = values; - } - - // from SQLExpression - public void accept (ExpressionVisitor builder) - { - builder.visit(this); - } - - // from SQLExpression - public void addClasses (Collection> classSet) - { - } - - public Class getPersistentClass () - { - return _pClass; - } - - public ArrayList> getValues () - { - return _values; - } - - @Override public boolean equals (Object obj) - { - if (this == obj) { - return true; - } - if (obj == null || getClass() != obj.getClass()) { - return false; - } - WhereCondition other = (WhereCondition) obj; - return _pClass == other._pClass && _values.equals(other.getValues()); - } - - @Override public int hashCode () - { - return _pClass.hashCode() ^ _values.hashCode(); - } - - protected Class _pClass; - protected ArrayList> _values; // List is not Serializable - } - /** * Constructs a new single-column {@code Key} with the given value. */ @@ -129,6 +77,9 @@ public class Key extends WhereClause throw new IllegalArgumentException("Field and Value arrays must be of equal length."); } + // keep this for posterity + _pClass = pClass; + // build a local map of field name -> field value Map> map = new HashMap>(); for (int i = 0; i < fields.length; i ++) { @@ -139,7 +90,7 @@ public class Key extends WhereClause String[] keyFields = KeyUtil.getKeyFields(pClass); // now extract the values in field order and ensure none are extra or missing - ArrayList> newValues = new ArrayList>(); + _values = new ArrayList>(); for (int ii = 0; ii < keyFields.length; ii++) { Comparable nugget = map.remove(keyFields[ii]); if (nugget == null) { @@ -147,7 +98,7 @@ public class Key extends WhereClause throw new IllegalArgumentException("Missing value for key field: " + keyFields[ii]); } if (nugget instanceof Serializable) { - newValues.add(nugget); + _values.add(nugget); continue; } throw new IllegalArgumentException( @@ -159,8 +110,6 @@ public class Key extends WhereClause throw new IllegalArgumentException( "Non-key columns given: " + StringUtil.join(map.keySet().toArray(), ", ")); } - - _condition = new WhereCondition(pClass, newValues); } /** @@ -168,7 +117,7 @@ public class Key extends WhereClause */ public Class getPersistentClass () { - return _condition.getPersistentClass(); + return _pClass; } /** @@ -176,19 +125,19 @@ public class Key extends WhereClause */ public ArrayList> getValues () { - return _condition.getValues(); + return _values; } // from WhereClause public SQLExpression getWhereExpression () { - return _condition; + throw new UnsupportedOperationException("Key is bound specially."); } // from SQLExpression public void addClasses (Collection> classSet) { - classSet.add(_condition.getPersistentClass()); + classSet.add(_pClass); } // from SQLExpression @@ -200,23 +149,22 @@ public class Key extends WhereClause // from CacheKey public String getCacheId () { - return _condition.getPersistentClass().getName(); + return _pClass.getName(); } // from CacheKey public Serializable getCacheKey () { - return _condition.getValues(); + return _values; } // from ValidatingCacheInvalidator public void validateFlushType (Class pClass) { - if (!pClass.equals(_condition.getPersistentClass())) { + if (!pClass.equals(_pClass)) { throw new IllegalArgumentException( "Class mismatch between persistent record and cache invalidator " + - "[record=" + pClass.getSimpleName() + - ", invtype=" + _condition.getPersistentClass().getSimpleName() + "]."); + "[record=" + pClass.getSimpleName() + ", invtype=" + _pClass.getSimpleName() + "]."); } } @@ -230,7 +178,7 @@ public class Key extends WhereClause public void validateQueryType (Class pClass) { super.validateQueryType(pClass); - validateTypesMatch(pClass, _condition.getPersistentClass()); + validateTypesMatch(pClass, _pClass); } @Override @@ -242,31 +190,34 @@ public class Key extends WhereClause if (obj == null || getClass() != obj.getClass()) { return false; } - return _condition.equals(((Key) obj)._condition); + return _values.equals(((Key) obj)._values); } @Override public int hashCode () { - return _condition.hashCode(); + return _values.hashCode(); } @Override public String toString () { - StringBuilder builder = new StringBuilder(_condition.getPersistentClass().getSimpleName()); + StringBuilder builder = new StringBuilder(_pClass.getSimpleName()); builder.append("("); - String[] keyFields = KeyUtil.getKeyFields(_condition.getPersistentClass()); + String[] keyFields = KeyUtil.getKeyFields(_pClass); for (int ii = 0; ii < keyFields.length; ii ++) { if (ii > 0) { builder.append(", "); } - builder.append(keyFields[ii]).append("=").append(_condition.getValues().get(ii)); + builder.append(keyFields[ii]).append("=").append(_values.get(ii)); } builder.append(")"); return builder.toString(); } + /** The persistent record type for which we are a key. */ + protected final Class _pClass; + /** The expression that identifies our row. */ - protected final WhereCondition _condition; + protected final ArrayList> _values; } diff --git a/src/java/com/samskivert/jdbc/depot/PrimaryKeySet.java b/src/java/com/samskivert/jdbc/depot/PrimaryKeySet.java index 607c1da..71d4ba7 100644 --- a/src/java/com/samskivert/jdbc/depot/PrimaryKeySet.java +++ b/src/java/com/samskivert/jdbc/depot/PrimaryKeySet.java @@ -132,7 +132,7 @@ public class PrimaryKeySet extends WhereClause if (obj == null || getClass() != obj.getClass()) { return false; } - return _condition.equals(((Key) obj)._condition); + return _condition.equals(((PrimaryKeySet) obj)._condition); } @Override diff --git a/src/java/com/samskivert/jdbc/depot/expression/ExpressionVisitor.java b/src/java/com/samskivert/jdbc/depot/expression/ExpressionVisitor.java index 46d3a0b..e7ce803 100644 --- a/src/java/com/samskivert/jdbc/depot/expression/ExpressionVisitor.java +++ b/src/java/com/samskivert/jdbc/depot/expression/ExpressionVisitor.java @@ -52,7 +52,7 @@ import com.samskivert.jdbc.depot.operator.SQLOperator.MultiOperator; public interface ExpressionVisitor { public void visit (FieldDefinition fieldOverride); - public void visit (Key.WhereCondition whereCondition); + public void visit (Key key); public void visit (MultiKey key); public void visit (FunctionExp functionExp); public void visit (EpochSeconds epochSeconds);