We need Key to export a Key.Expression which we can use inside the SQL for a

KeySet so that the "where" keyword is not repeated every time we bind a key.
This commit is contained in:
Michael Bayne
2008-09-08 17:54:11 +00:00
parent 56c4394b29
commit f1dd34e4b3
5 changed files with 23 additions and 6 deletions
@@ -71,7 +71,7 @@ public class BindVisitor implements ExpressionVisitor
// nothing needed // nothing needed
} }
public void visit (Key<? extends PersistentRecord> key) public void visit (Key<? extends PersistentRecord>.Expression key)
{ {
for (Comparable<?> value : key.getValues()) { for (Comparable<?> value : key.getValues()) {
if (value != null) { if (value != null) {
@@ -94,9 +94,8 @@ public abstract class BuildVisitor implements ExpressionVisitor
where.getWhereExpression().accept(this); where.getWhereExpression().accept(this);
} }
public void visit (Key<? extends PersistentRecord> key) public void visit (Key<? extends PersistentRecord>.Expression key)
{ {
_builder.append(" where ");
Class<? extends PersistentRecord> pClass = key.getPersistentClass(); Class<? extends PersistentRecord> pClass = key.getPersistentClass();
String[] keyFields = KeyUtil.getKeyFields(pClass); String[] keyFields = KeyUtil.getKeyFields(pClass);
List<Comparable<?>> values = key.getValues(); List<Comparable<?>> values = key.getValues();
+19 -1
View File
@@ -41,6 +41,24 @@ import com.samskivert.util.StringUtil;
public class Key<T extends PersistentRecord> extends WhereClause public class Key<T extends PersistentRecord> extends WhereClause
implements SQLExpression, CacheKey, ValidatingCacheInvalidator, Serializable implements SQLExpression, CacheKey, ValidatingCacheInvalidator, Serializable
{ {
/** Handles the matching of the key columns to its bound values. This is needed so that we can
* combine a buncy of keys into a {@link KeySet}. */
public class Expression implements SQLExpression
{
public Class<T> getPersistentClass () {
return Key.this.getPersistentClass();
}
public List<Comparable<?>> getValues () {
return Key.this.getValues();
}
public void accept (ExpressionVisitor builder) {
builder.visit(this);
}
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet) {
classSet.add(getPersistentClass());
}
}
/** /**
* Constructs a new single-column {@code Key} with the given value. * Constructs a new single-column {@code Key} with the given value.
*/ */
@@ -130,7 +148,7 @@ public class Key<T extends PersistentRecord> extends WhereClause
// from WhereClause // from WhereClause
public SQLExpression getWhereExpression () public SQLExpression getWhereExpression ()
{ {
throw new UnsupportedOperationException("Key is bound specially."); return new Expression();
} }
// from SQLExpression // from SQLExpression
@@ -72,7 +72,7 @@ public class KeySet<T extends PersistentRecord> extends WhereClause
SQLExpression[] keyArray = new SQLExpression[keys.size()]; SQLExpression[] keyArray = new SQLExpression[keys.size()];
int ii = 0; int ii = 0;
for (Key<T> key : keys) { for (Key<T> key : keys) {
keyArray[ii++] = key; keyArray[ii++] = key.getWhereExpression();
} }
_condition = new Logic.Or(keyArray); _condition = new Logic.Or(keyArray);
} }
@@ -69,7 +69,7 @@ public interface ExpressionVisitor
public void visit (LiteralExp literalExp); public void visit (LiteralExp literalExp);
public void visit (ValueExp valueExp); public void visit (ValueExp valueExp);
public void visit (WhereClause where); public void visit (WhereClause where);
public void visit (Key<? extends PersistentRecord> key); public void visit (Key<? extends PersistentRecord>.Expression key);
public void visit (MultiKey<? extends PersistentRecord> key); public void visit (MultiKey<? extends PersistentRecord> key);
public void visit (Exists<? extends PersistentRecord> exists); public void visit (Exists<? extends PersistentRecord> exists);
public void visit (SelectClause<? extends PersistentRecord> selectClause); public void visit (SelectClause<? extends PersistentRecord> selectClause);