Extracted BinaryOperator and MultiOperator into top-level classes in
preparation for possibly nixing SQLOperator, which appears to me to be unused if not unnecessary.
This commit is contained in:
@@ -91,12 +91,12 @@ import com.samskivert.depot.impl.expression.StringFun.Position;
|
|||||||
import com.samskivert.depot.impl.expression.StringFun.Substring;
|
import com.samskivert.depot.impl.expression.StringFun.Substring;
|
||||||
import com.samskivert.depot.impl.expression.StringFun.Trim;
|
import com.samskivert.depot.impl.expression.StringFun.Trim;
|
||||||
import com.samskivert.depot.impl.expression.StringFun.Upper;
|
import com.samskivert.depot.impl.expression.StringFun.Upper;
|
||||||
|
import com.samskivert.depot.impl.operator.BinaryOperator;
|
||||||
import com.samskivert.depot.impl.operator.Exists;
|
import com.samskivert.depot.impl.operator.Exists;
|
||||||
import com.samskivert.depot.impl.operator.In;
|
import com.samskivert.depot.impl.operator.In;
|
||||||
import com.samskivert.depot.impl.operator.IsNull;
|
import com.samskivert.depot.impl.operator.IsNull;
|
||||||
|
import com.samskivert.depot.impl.operator.MultiOperator;
|
||||||
import com.samskivert.depot.impl.operator.Not;
|
import com.samskivert.depot.impl.operator.Not;
|
||||||
import com.samskivert.depot.impl.operator.SQLOperator.BinaryOperator;
|
|
||||||
import com.samskivert.depot.impl.operator.SQLOperator.MultiOperator;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements the base functionality of the SQL-building pass of {@link SQLBuilder}. Dialectal
|
* Implements the base functionality of the SQL-building pass of {@link SQLBuilder}. Dialectal
|
||||||
|
|||||||
@@ -81,12 +81,12 @@ import com.samskivert.depot.impl.expression.StringFun.Position;
|
|||||||
import com.samskivert.depot.impl.expression.StringFun.Substring;
|
import com.samskivert.depot.impl.expression.StringFun.Substring;
|
||||||
import com.samskivert.depot.impl.expression.StringFun.Trim;
|
import com.samskivert.depot.impl.expression.StringFun.Trim;
|
||||||
import com.samskivert.depot.impl.expression.StringFun.Upper;
|
import com.samskivert.depot.impl.expression.StringFun.Upper;
|
||||||
|
import com.samskivert.depot.impl.operator.BinaryOperator;
|
||||||
import com.samskivert.depot.impl.operator.Exists;
|
import com.samskivert.depot.impl.operator.Exists;
|
||||||
import com.samskivert.depot.impl.operator.In;
|
import com.samskivert.depot.impl.operator.In;
|
||||||
import com.samskivert.depot.impl.operator.IsNull;
|
import com.samskivert.depot.impl.operator.IsNull;
|
||||||
|
import com.samskivert.depot.impl.operator.MultiOperator;
|
||||||
import com.samskivert.depot.impl.operator.Not;
|
import com.samskivert.depot.impl.operator.Not;
|
||||||
import com.samskivert.depot.impl.operator.SQLOperator.BinaryOperator;
|
|
||||||
import com.samskivert.depot.impl.operator.SQLOperator.MultiOperator;
|
|
||||||
import com.samskivert.util.ArrayUtil;
|
import com.samskivert.util.ArrayUtil;
|
||||||
import com.samskivert.util.Tuple;
|
import com.samskivert.util.Tuple;
|
||||||
|
|
||||||
|
|||||||
@@ -75,12 +75,12 @@ import com.samskivert.depot.impl.expression.StringFun.Position;
|
|||||||
import com.samskivert.depot.impl.expression.StringFun.Substring;
|
import com.samskivert.depot.impl.expression.StringFun.Substring;
|
||||||
import com.samskivert.depot.impl.expression.StringFun.Trim;
|
import com.samskivert.depot.impl.expression.StringFun.Trim;
|
||||||
import com.samskivert.depot.impl.expression.StringFun.Upper;
|
import com.samskivert.depot.impl.expression.StringFun.Upper;
|
||||||
|
import com.samskivert.depot.impl.operator.BinaryOperator;
|
||||||
import com.samskivert.depot.impl.operator.Exists;
|
import com.samskivert.depot.impl.operator.Exists;
|
||||||
import com.samskivert.depot.impl.operator.In;
|
import com.samskivert.depot.impl.operator.In;
|
||||||
import com.samskivert.depot.impl.operator.IsNull;
|
import com.samskivert.depot.impl.operator.IsNull;
|
||||||
|
import com.samskivert.depot.impl.operator.MultiOperator;
|
||||||
import com.samskivert.depot.impl.operator.Not;
|
import com.samskivert.depot.impl.operator.Not;
|
||||||
import com.samskivert.depot.impl.operator.SQLOperator.BinaryOperator;
|
|
||||||
import com.samskivert.depot.impl.operator.SQLOperator.MultiOperator;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enumerates visitation methods for every possible SQL expression type.
|
* Enumerates visitation methods for every possible SQL expression type.
|
||||||
|
|||||||
@@ -54,8 +54,8 @@ import com.samskivert.depot.impl.expression.StringFun.Lower;
|
|||||||
import com.samskivert.depot.impl.operator.BitAnd;
|
import com.samskivert.depot.impl.operator.BitAnd;
|
||||||
import com.samskivert.depot.impl.operator.BitOr;
|
import com.samskivert.depot.impl.operator.BitOr;
|
||||||
import com.samskivert.depot.impl.operator.Like;
|
import com.samskivert.depot.impl.operator.Like;
|
||||||
|
import com.samskivert.depot.impl.operator.MultiOperator;
|
||||||
import com.samskivert.depot.impl.operator.Or;
|
import com.samskivert.depot.impl.operator.Or;
|
||||||
import com.samskivert.depot.impl.operator.SQLOperator.MultiOperator;
|
|
||||||
|
|
||||||
public class HSQLBuilder
|
public class HSQLBuilder
|
||||||
extends SQLBuilder
|
extends SQLBuilder
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ import com.samskivert.depot.expression.SQLExpression;
|
|||||||
/**
|
/**
|
||||||
* Represents a condition that is true iff all its subconditions are true.
|
* Represents a condition that is true iff all its subconditions are true.
|
||||||
*/
|
*/
|
||||||
public class And extends SQLOperator.MultiOperator
|
public class And extends MultiOperator
|
||||||
{
|
{
|
||||||
public And (Collection<? extends SQLExpression> conditions)
|
public And (Collection<? extends SQLExpression> conditions)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ package com.samskivert.depot.impl.operator;
|
|||||||
|
|
||||||
import com.samskivert.depot.expression.SQLExpression;
|
import com.samskivert.depot.expression.SQLExpression;
|
||||||
import com.samskivert.depot.impl.expression.ValueExp;
|
import com.samskivert.depot.impl.expression.ValueExp;
|
||||||
import com.samskivert.depot.impl.operator.SQLOperator.MultiOperator;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A convenient container for implementations of arithmetic operators.
|
* A convenient container for implementations of arithmetic operators.
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
//
|
||||||
|
// $Id$
|
||||||
|
|
||||||
|
package com.samskivert.depot.impl.operator;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import com.google.common.base.Function;
|
||||||
|
import com.google.common.base.Predicates;
|
||||||
|
import com.google.common.collect.Iterables;
|
||||||
|
import com.samskivert.depot.expression.SQLExpression;
|
||||||
|
import com.samskivert.depot.impl.expression.ArgumentExp;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A base class for all operators.
|
||||||
|
*/
|
||||||
|
public abstract class BaseOperator extends ArgumentExp
|
||||||
|
implements SQLOperator
|
||||||
|
{
|
||||||
|
public static Function<Object, Long> INTEGRAL = new Function<Object, Long>() {
|
||||||
|
public Long apply (Object o) {
|
||||||
|
if ((o instanceof Integer) || (o instanceof Long)) {
|
||||||
|
return ((Number) o).longValue();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public static Function<Object, Double> NUMERICAL = new Function<Object, Double>() {
|
||||||
|
public Double apply (Object o) {
|
||||||
|
return (o instanceof Number) ? ((Number) o).doubleValue() : null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public static Function<Object, String> STRING = new Function<Object, String>() {
|
||||||
|
public String apply (Object o) {
|
||||||
|
return (o instanceof String) ? (String) o : null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public static Function<Object, Date> DATE = new Function<Object, Date>() {
|
||||||
|
public Date apply (Object o) {
|
||||||
|
return (o instanceof Date) ? (Date) o : null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public static <S, T> boolean all (Function<S, T> fun, S... obj) {
|
||||||
|
return Iterables.all(Arrays.asList(obj), Predicates.compose(Predicates.isNull(), fun));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <S, T extends Comparable<T>> int compare (Function<S, T> fun, S lhs, S rhs) {
|
||||||
|
return fun.apply(lhs).compareTo(fun.apply(rhs));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <S, T> T accumulate (Function<S, T> fun, S[] ops, T v,
|
||||||
|
BaseOperator.Accumulator<T> acc) {
|
||||||
|
for (S op : ops) {
|
||||||
|
v = acc.accumulate(v, fun.apply(op));
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected BaseOperator (SQLExpression... operands)
|
||||||
|
{
|
||||||
|
super(operands);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static interface Accumulator<T>
|
||||||
|
{
|
||||||
|
T accumulate (T left, T right);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
//
|
||||||
|
// $Id$
|
||||||
|
|
||||||
|
package com.samskivert.depot.impl.operator;
|
||||||
|
|
||||||
|
import com.samskivert.depot.expression.SQLExpression;
|
||||||
|
import com.samskivert.depot.impl.ExpressionVisitor;
|
||||||
|
import com.samskivert.depot.impl.expression.ValueExp;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Does the real work for simple binary operators such as Equals.
|
||||||
|
*/
|
||||||
|
public abstract class BinaryOperator extends BaseOperator
|
||||||
|
{
|
||||||
|
public BinaryOperator (SQLExpression lhs, SQLExpression rhs)
|
||||||
|
{
|
||||||
|
super(lhs, rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BinaryOperator (SQLExpression lhs, Comparable<?> rhs)
|
||||||
|
{
|
||||||
|
this(lhs, new ValueExp(rhs));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the string representation of the operator.
|
||||||
|
*/
|
||||||
|
public abstract String operator();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates our value.
|
||||||
|
*/
|
||||||
|
public abstract Object evaluate (Object left, Object right);
|
||||||
|
|
||||||
|
// from SQLExpression
|
||||||
|
public Object accept (ExpressionVisitor<?> builder)
|
||||||
|
{
|
||||||
|
return builder.visit(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SQLExpression getLeftHandSide ()
|
||||||
|
{
|
||||||
|
return _args[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
public SQLExpression getRightHandSide ()
|
||||||
|
{
|
||||||
|
return _args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override // from Object
|
||||||
|
public String toString ()
|
||||||
|
{
|
||||||
|
return "(" + _args[0] + operator() + _args[1] + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -25,7 +25,7 @@ import com.samskivert.depot.expression.SQLExpression;
|
|||||||
/**
|
/**
|
||||||
* The SQL '=' operator.
|
* The SQL '=' operator.
|
||||||
*/
|
*/
|
||||||
public class Equals extends SQLOperator.BinaryOperator
|
public class Equals extends BinaryOperator
|
||||||
{
|
{
|
||||||
public Equals (SQLExpression column, Comparable<?> value)
|
public Equals (SQLExpression column, Comparable<?> value)
|
||||||
{
|
{
|
||||||
@@ -37,13 +37,13 @@ public class Equals extends SQLOperator.BinaryOperator
|
|||||||
super(column, value);
|
super(column, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // from SQLOperator.BinaryOperator
|
@Override // from BinaryOperator
|
||||||
public String operator()
|
public String operator()
|
||||||
{
|
{
|
||||||
return "=";
|
return "=";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // from SQLOperator.BinaryOperator
|
@Override // from BinaryOperator
|
||||||
public Object evaluate (Object left, Object right)
|
public Object evaluate (Object left, Object right)
|
||||||
{
|
{
|
||||||
if (left == null || right == null) {
|
if (left == null || right == null) {
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ import com.samskivert.depot.expression.SQLExpression;
|
|||||||
/**
|
/**
|
||||||
* The SQL '>' operator.
|
* The SQL '>' operator.
|
||||||
*/
|
*/
|
||||||
public class GreaterThan extends SQLOperator.BinaryOperator
|
public class GreaterThan extends BinaryOperator
|
||||||
{
|
{
|
||||||
public GreaterThan (SQLExpression column, Comparable<?> value)
|
public GreaterThan (SQLExpression column, Comparable<?> value)
|
||||||
{
|
{
|
||||||
@@ -37,13 +37,13 @@ public class GreaterThan extends SQLOperator.BinaryOperator
|
|||||||
super(column, value);
|
super(column, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // from SQLOperator.BinaryOperator
|
@Override // from BinaryOperator
|
||||||
public String operator()
|
public String operator()
|
||||||
{
|
{
|
||||||
return ">";
|
return ">";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // from SQLOperator.BinaryOperator
|
@Override // from BinaryOperator
|
||||||
public Object evaluate (Object left, Object right)
|
public Object evaluate (Object left, Object right)
|
||||||
{
|
{
|
||||||
if (all(NUMERICAL, left, right)) {
|
if (all(NUMERICAL, left, right)) {
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ import com.samskivert.depot.expression.SQLExpression;
|
|||||||
/**
|
/**
|
||||||
* The SQL '>=' operator.
|
* The SQL '>=' operator.
|
||||||
*/
|
*/
|
||||||
public class GreaterThanEquals extends SQLOperator.BinaryOperator
|
public class GreaterThanEquals extends BinaryOperator
|
||||||
{
|
{
|
||||||
public GreaterThanEquals (SQLExpression column, Comparable<?> value)
|
public GreaterThanEquals (SQLExpression column, Comparable<?> value)
|
||||||
{
|
{
|
||||||
@@ -37,13 +37,13 @@ public class GreaterThanEquals extends SQLOperator.BinaryOperator
|
|||||||
super(column, value);
|
super(column, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // from SQLOperator.BinaryOperator
|
@Override // from BinaryOperator
|
||||||
public String operator()
|
public String operator()
|
||||||
{
|
{
|
||||||
return ">=";
|
return ">=";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // from SQLOperator.BinaryOperator
|
@Override // from BinaryOperator
|
||||||
public Object evaluate (Object left, Object right)
|
public Object evaluate (Object left, Object right)
|
||||||
{
|
{
|
||||||
if (all(NUMERICAL, left, right)) {
|
if (all(NUMERICAL, left, right)) {
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ import com.samskivert.depot.expression.SQLExpression;
|
|||||||
/**
|
/**
|
||||||
* The SQL '<' operator.
|
* The SQL '<' operator.
|
||||||
*/
|
*/
|
||||||
public class LessThan extends SQLOperator.BinaryOperator
|
public class LessThan extends BinaryOperator
|
||||||
{
|
{
|
||||||
public LessThan (SQLExpression column, Comparable<?> value)
|
public LessThan (SQLExpression column, Comparable<?> value)
|
||||||
{
|
{
|
||||||
@@ -37,13 +37,13 @@ public class LessThan extends SQLOperator.BinaryOperator
|
|||||||
super(column, value);
|
super(column, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // from SQLOperator.BinaryOperator
|
@Override // from BinaryOperator
|
||||||
public String operator()
|
public String operator()
|
||||||
{
|
{
|
||||||
return "<";
|
return "<";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // from SQLOperator.BinaryOperator
|
@Override // from BinaryOperator
|
||||||
public Object evaluate (Object left, Object right)
|
public Object evaluate (Object left, Object right)
|
||||||
{
|
{
|
||||||
if (all(NUMERICAL, left, right)) {
|
if (all(NUMERICAL, left, right)) {
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ import com.samskivert.depot.expression.SQLExpression;
|
|||||||
/**
|
/**
|
||||||
* The SQL '<=' operator.
|
* The SQL '<=' operator.
|
||||||
*/
|
*/
|
||||||
public class LessThanEquals extends SQLOperator.BinaryOperator
|
public class LessThanEquals extends BinaryOperator
|
||||||
{
|
{
|
||||||
public LessThanEquals (SQLExpression column, Comparable<?> value)
|
public LessThanEquals (SQLExpression column, Comparable<?> value)
|
||||||
{
|
{
|
||||||
@@ -37,13 +37,13 @@ public class LessThanEquals extends SQLOperator.BinaryOperator
|
|||||||
super(column, value);
|
super(column, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // from SQLOperator.BinaryOperator
|
@Override // from BinaryOperator
|
||||||
public String operator()
|
public String operator()
|
||||||
{
|
{
|
||||||
return "<=";
|
return "<=";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // from SQLOperator.BinaryOperator
|
@Override // from BinaryOperator
|
||||||
public Object evaluate (Object left, Object right)
|
public Object evaluate (Object left, Object right)
|
||||||
{
|
{
|
||||||
if (all(NUMERICAL, left, right)) {
|
if (all(NUMERICAL, left, right)) {
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ import com.samskivert.depot.expression.SQLExpression;
|
|||||||
/**
|
/**
|
||||||
* The SQL 'like' operator.
|
* The SQL 'like' operator.
|
||||||
*/
|
*/
|
||||||
public class Like extends SQLOperator.BinaryOperator
|
public class Like extends BinaryOperator
|
||||||
{
|
{
|
||||||
public Like (SQLExpression column, Comparable<?> value)
|
public Like (SQLExpression column, Comparable<?> value)
|
||||||
{
|
{
|
||||||
@@ -37,13 +37,13 @@ public class Like extends SQLOperator.BinaryOperator
|
|||||||
super(column, value);
|
super(column, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // from SQLOperator.BinaryOperator
|
@Override // from BinaryOperator
|
||||||
public String operator()
|
public String operator()
|
||||||
{
|
{
|
||||||
return " like ";
|
return " like ";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // from SQLOperator.BinaryOperator
|
@Override // from BinaryOperator
|
||||||
public Object evaluate (Object left, Object right)
|
public Object evaluate (Object left, Object right)
|
||||||
{
|
{
|
||||||
return new NoValue("Like operator not implemented");
|
return new NoValue("Like operator not implemented");
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
//
|
||||||
|
// $Id$
|
||||||
|
|
||||||
|
package com.samskivert.depot.impl.operator;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import com.samskivert.depot.PersistentRecord;
|
||||||
|
import com.samskivert.depot.expression.SQLExpression;
|
||||||
|
import com.samskivert.depot.impl.ExpressionVisitor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an operator with any number of operands.
|
||||||
|
*/
|
||||||
|
public abstract class MultiOperator extends BaseOperator
|
||||||
|
{
|
||||||
|
public MultiOperator (SQLExpression ... operands)
|
||||||
|
{
|
||||||
|
super(operands);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the text infix to be used to join expressions together.
|
||||||
|
*/
|
||||||
|
public abstract String operator ();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates our value.
|
||||||
|
*/
|
||||||
|
public abstract Object evaluate (Object[] values);
|
||||||
|
|
||||||
|
// from SQLExpression
|
||||||
|
public Object accept (ExpressionVisitor<?> builder)
|
||||||
|
{
|
||||||
|
return builder.visit(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override // from SQLExpression
|
||||||
|
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet)
|
||||||
|
{
|
||||||
|
for (SQLExpression operand : _args) {
|
||||||
|
operand.addClasses(classSet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override // from Object
|
||||||
|
public String toString ()
|
||||||
|
{
|
||||||
|
StringBuilder builder = new StringBuilder("(");
|
||||||
|
for (SQLExpression operand : _args) {
|
||||||
|
if (builder.length() > 1) {
|
||||||
|
builder.append(operator());
|
||||||
|
}
|
||||||
|
builder.append(operand);
|
||||||
|
}
|
||||||
|
return builder.append(")").toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -25,7 +25,7 @@ import com.samskivert.depot.expression.SQLExpression;
|
|||||||
/**
|
/**
|
||||||
* The SQL '!=' operator.
|
* The SQL '!=' operator.
|
||||||
*/
|
*/
|
||||||
public class NotEquals extends SQLOperator.BinaryOperator
|
public class NotEquals extends BinaryOperator
|
||||||
{
|
{
|
||||||
public NotEquals (SQLExpression column, Comparable<?> value)
|
public NotEquals (SQLExpression column, Comparable<?> value)
|
||||||
{
|
{
|
||||||
@@ -37,13 +37,13 @@ public class NotEquals extends SQLOperator.BinaryOperator
|
|||||||
super(column, value);
|
super(column, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // from SQLOperator.BinaryOperator
|
@Override // from BinaryOperator
|
||||||
public String operator()
|
public String operator()
|
||||||
{
|
{
|
||||||
return "!=";
|
return "!=";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // from SQLOperator.BinaryOperator
|
@Override // from BinaryOperator
|
||||||
public Object evaluate (Object left, Object right)
|
public Object evaluate (Object left, Object right)
|
||||||
{
|
{
|
||||||
if (left == null || right == null) {
|
if (left == null || right == null) {
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ import com.samskivert.depot.expression.SQLExpression;
|
|||||||
/**
|
/**
|
||||||
* Represents a condition that is false iff all its subconditions are false.
|
* Represents a condition that is false iff all its subconditions are false.
|
||||||
*/
|
*/
|
||||||
public class Or extends SQLOperator.MultiOperator
|
public class Or extends MultiOperator
|
||||||
{
|
{
|
||||||
public Or (Collection<? extends SQLExpression> conditions)
|
public Or (Collection<? extends SQLExpression> conditions)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -20,18 +20,8 @@
|
|||||||
|
|
||||||
package com.samskivert.depot.impl.operator;
|
package com.samskivert.depot.impl.operator;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
|
||||||
import com.google.common.base.Predicates;
|
|
||||||
import com.google.common.collect.Iterables;
|
|
||||||
import com.samskivert.depot.PersistentRecord;
|
|
||||||
import com.samskivert.depot.expression.SQLExpression;
|
import com.samskivert.depot.expression.SQLExpression;
|
||||||
import com.samskivert.depot.impl.ExpressionVisitor;
|
|
||||||
import com.samskivert.depot.impl.expression.ArgumentExp;
|
|
||||||
import com.samskivert.depot.impl.expression.ValueExp;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A common interface for operator hierarchies in SQL. The main purpose of breaking this out from
|
* A common interface for operator hierarchies in SQL. The main purpose of breaking this out from
|
||||||
@@ -40,153 +30,4 @@ import com.samskivert.depot.impl.expression.ValueExp;
|
|||||||
*/
|
*/
|
||||||
public interface SQLOperator extends SQLExpression
|
public interface SQLOperator extends SQLExpression
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Represents an operator with any number of operands.
|
|
||||||
*/
|
|
||||||
public abstract static class MultiOperator extends BaseOperator
|
|
||||||
{
|
|
||||||
public MultiOperator (SQLExpression ... operands)
|
|
||||||
{
|
|
||||||
super(operands);
|
|
||||||
}
|
|
||||||
|
|
||||||
// from SQLExpression
|
|
||||||
public Object accept (ExpressionVisitor<?> builder)
|
|
||||||
{
|
|
||||||
return builder.visit(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override // from SQLExpression
|
|
||||||
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet)
|
|
||||||
{
|
|
||||||
for (SQLExpression operand : _args) {
|
|
||||||
operand.addClasses(classSet);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the text infix to be used to join expressions together.
|
|
||||||
*/
|
|
||||||
public abstract String operator ();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculates a value
|
|
||||||
*/
|
|
||||||
public abstract Object evaluate (Object[] values);
|
|
||||||
|
|
||||||
@Override // from Object
|
|
||||||
public String toString ()
|
|
||||||
{
|
|
||||||
StringBuilder builder = new StringBuilder("(");
|
|
||||||
for (SQLExpression operand : _args) {
|
|
||||||
if (builder.length() > 1) {
|
|
||||||
builder.append(operator());
|
|
||||||
}
|
|
||||||
builder.append(operand);
|
|
||||||
}
|
|
||||||
return builder.append(")").toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Does the real work for simple binary operators such as Equals.
|
|
||||||
*/
|
|
||||||
public abstract static class BinaryOperator extends BaseOperator
|
|
||||||
{
|
|
||||||
public BinaryOperator (SQLExpression lhs, SQLExpression rhs)
|
|
||||||
{
|
|
||||||
super(lhs, rhs);
|
|
||||||
}
|
|
||||||
|
|
||||||
public BinaryOperator (SQLExpression lhs, Comparable<?> rhs)
|
|
||||||
{
|
|
||||||
this(lhs, new ValueExp(rhs));
|
|
||||||
}
|
|
||||||
|
|
||||||
// from SQLExpression
|
|
||||||
public Object accept (ExpressionVisitor<?> builder)
|
|
||||||
{
|
|
||||||
return builder.visit(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract Object evaluate (Object left, Object right);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the string representation of the operator.
|
|
||||||
*/
|
|
||||||
public abstract String operator();
|
|
||||||
|
|
||||||
public SQLExpression getLeftHandSide ()
|
|
||||||
{
|
|
||||||
return _args[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
public SQLExpression getRightHandSide ()
|
|
||||||
{
|
|
||||||
return _args[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override // from Object
|
|
||||||
public String toString ()
|
|
||||||
{
|
|
||||||
return "(" + _args[0] + operator() + _args[1] + ")";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static abstract class BaseOperator extends ArgumentExp
|
|
||||||
implements SQLOperator
|
|
||||||
{
|
|
||||||
public static Function<Object, Long> INTEGRAL = new Function<Object, Long>() {
|
|
||||||
public Long apply (Object o) {
|
|
||||||
if ((o instanceof Integer) || (o instanceof Long)) {
|
|
||||||
return ((Number) o).longValue();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static Function<Object, Double> NUMERICAL = new Function<Object, Double>() {
|
|
||||||
public Double apply (Object o) {
|
|
||||||
return (o instanceof Number) ? ((Number) o).doubleValue() : null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static Function<Object, String> STRING = new Function<Object, String>() {
|
|
||||||
public String apply (Object o) {
|
|
||||||
return (o instanceof String) ? (String) o : null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static Function<Object, Date> DATE = new Function<Object, Date>() {
|
|
||||||
public Date apply (Object o) {
|
|
||||||
return (o instanceof Date) ? (Date) o : null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static <S, T> boolean all (Function<S, T> fun, S... obj) {
|
|
||||||
return Iterables.all(Arrays.asList(obj), Predicates.compose(Predicates.isNull(), fun));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <S, T extends Comparable<T>> int compare (Function<S, T> fun, S lhs, S rhs) {
|
|
||||||
return fun.apply(lhs).compareTo(fun.apply(rhs));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <S, T> T accumulate (Function<S, T> fun, S[] ops, T v, Accumulator<T> acc) {
|
|
||||||
for (S op : ops) {
|
|
||||||
v = acc.accumulate(v, fun.apply(op));
|
|
||||||
}
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected BaseOperator (SQLExpression... operands)
|
|
||||||
{
|
|
||||||
super(operands);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static interface Accumulator<T>
|
|
||||||
{
|
|
||||||
T accumulate (T left, T right);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user