Both In() and IsNull() will work on general expressions, not just columns.

This commit is contained in:
Par Winzell
2010-02-22 18:58:57 +00:00
parent cdeb8eac8a
commit 1fe2c451f8
7 changed files with 33 additions and 34 deletions
@@ -26,7 +26,6 @@ import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.clause.Join;
import com.samskivert.depot.impl.FragmentVisitor;
import com.samskivert.depot.impl.operator.In;
import com.samskivert.depot.impl.operator.IsNull;
import com.samskivert.depot.impl.operator.Like;
/**
@@ -55,18 +54,6 @@ public class ColumnExp extends FluentExp
return new ColumnExp(oClass, name);
}
/** Returns an {@link IsNull} with this column as its target. */
public IsNull isNull ()
{
return new IsNull(this);
}
/** Returns an {@link In} with this column and the supplied values. */
public In in (Comparable<?>... values)
{
return new In(this, values);
}
/** Returns an {@link In} with this column and the supplied values. */
public In in (Collection<? extends Comparable<?>> values)
{
@@ -28,6 +28,8 @@ import com.samskivert.depot.impl.operator.Div;
import com.samskivert.depot.impl.operator.Equals;
import com.samskivert.depot.impl.operator.GreaterThan;
import com.samskivert.depot.impl.operator.GreaterThanEquals;
import com.samskivert.depot.impl.operator.In;
import com.samskivert.depot.impl.operator.IsNull;
import com.samskivert.depot.impl.operator.LessThan;
import com.samskivert.depot.impl.operator.LessThanEquals;
import com.samskivert.depot.impl.operator.Mul;
@@ -64,6 +66,18 @@ public abstract class FluentExp
return new NotEquals(this, expr);
}
/** Returns an {@link IsNull} with this expression as its target. */
public IsNull isNull ()
{
return new IsNull(this);
}
/** Returns an {@link In} with this expression and the supplied values. */
public In in (Comparable<?>... values)
{
return new In(this, values);
}
/** Returns a {@link GreaterThan} with this expression and the supplied target. */
public FluentExp greaterThan (Comparable<?> value)
{
@@ -197,7 +197,7 @@ public abstract class BuildVisitor implements FragmentVisitor<Void>
public Void visit (IsNull isNull)
{
isNull.getColumn().accept(this);
isNull.getExpression().accept(this);
_builder.append(" is null");
return null;
}
@@ -209,7 +209,7 @@ public abstract class BuildVisitor implements FragmentVisitor<Void>
new ValueExp(false).accept(this);
return null;
}
in.getColumn().accept(this);
in.getExpression().accept(this);
_builder.append(" in (");
Comparable<?>[] values = in.getValues();
for (int ii = 0; ii < values.length; ii ++) {
@@ -137,13 +137,13 @@ public class ExpressionEvaluator
public Object visit (IsNull isNull)
{
Object operand = isNull.getColumn().accept(this);
Object operand = isNull.getExpression().accept(this);
return (operand instanceof NoValue) ? operand : operand != null;
}
public Object visit (In in)
{
Object operand = in.getColumn().accept(this);
Object operand = in.getExpression().accept(this);
return (operand instanceof NoValue) ? operand :
-1 != ArrayUtil.indexOf(in.getValues(), operand);
}
@@ -45,7 +45,7 @@ public class PostgreSQL4Builder extends PostgreSQLBuilder
new ValueExp(false).accept(this);
return null;
}
in.getColumn().accept(this);
in.getExpression().accept(this);
_builder.append(" = any (?)");
_bindables.add(new Bindable() {
public void doBind (Connection conn, PreparedStatement stmt, int argIdx)
@@ -23,7 +23,6 @@ package com.samskivert.depot.impl.operator;
import java.util.Collection;
import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.expression.ColumnExp;
import com.samskivert.depot.expression.SQLExpression;
import com.samskivert.depot.impl.FragmentVisitor;
@@ -36,20 +35,20 @@ public class In
/** The maximum number of keys allowed in an IN() clause. */
public static final int MAX_KEYS = Short.MAX_VALUE;
public In (ColumnExp column, Comparable<?>... values)
public In (SQLExpression expression, Comparable<?>... values)
{
_column = column;
_expression = expression;
_values = values;
}
public In (ColumnExp pColumn, Collection<? extends Comparable<?>> values)
public In (SQLExpression pColumn, Collection<? extends Comparable<?>> values)
{
this(pColumn, values.toArray(new Comparable<?>[values.size()]));
}
public ColumnExp getColumn ()
public SQLExpression getExpression ()
{
return _column;
return _expression;
}
public Comparable<?>[] getValues ()
@@ -66,14 +65,14 @@ public class In
// from SQLFragment
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet)
{
_column.addClasses(classSet);
_expression.addClasses(classSet);
}
@Override // from Object
public String toString ()
{
StringBuilder builder = new StringBuilder();
builder.append(_column).append(" in (");
builder.append(_expression).append(" in (");
for (int ii = 0; ii < _values.length; ii++) {
if (ii > 0) {
builder.append(", ");
@@ -84,6 +83,6 @@ public class In
return builder.append(")").toString();
}
protected ColumnExp _column;
protected SQLExpression _expression;
protected Comparable<?>[] _values;
}
@@ -23,7 +23,6 @@ package com.samskivert.depot.impl.operator;
import java.util.Collection;
import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.expression.ColumnExp;
import com.samskivert.depot.expression.SQLExpression;
import com.samskivert.depot.impl.FragmentVisitor;
@@ -33,14 +32,14 @@ import com.samskivert.depot.impl.FragmentVisitor;
public class IsNull
implements SQLExpression
{
public IsNull (ColumnExp column)
public IsNull (SQLExpression expression)
{
_column = column;
_expression = expression;
}
public ColumnExp getColumn()
public SQLExpression getExpression ()
{
return _column;
return _expression;
}
// from SQLFragment
@@ -57,8 +56,8 @@ public class IsNull
@Override // from Object
public String toString ()
{
return "IsNull(" + _column + ")";
return "IsNull(" + _expression + ")";
}
protected ColumnExp _column;
protected SQLExpression _expression;
}