I'm not going all the way down this path today, but now that we've hidden the
implementation of operators and expressions, we can go the whole nine yards and eliminate the named classes entirely (except in cases where we need them for multiple dispatch by the visitors). Demonstrated this neatness with And and Or.
This commit is contained in:
@@ -40,7 +40,6 @@ import com.samskivert.depot.impl.DepotUtil;
|
|||||||
import com.samskivert.depot.impl.ExpressionVisitor;
|
import com.samskivert.depot.impl.ExpressionVisitor;
|
||||||
import com.samskivert.depot.impl.expression.LiteralExp;
|
import com.samskivert.depot.impl.expression.LiteralExp;
|
||||||
import com.samskivert.depot.impl.operator.In;
|
import com.samskivert.depot.impl.operator.In;
|
||||||
import com.samskivert.depot.impl.operator.Or;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains a set of primary keys that match a set of persistent records. This is used internally
|
* Contains a set of primary keys that match a set of persistent records. This is used internally
|
||||||
@@ -225,7 +224,7 @@ public abstract class KeySet<T extends PersistentRecord> extends WhereClause
|
|||||||
for (Comparable<?>[] kvals : _keys) {
|
for (Comparable<?>[] kvals : _keys) {
|
||||||
keyexps[ii++] = new Key.Expression(_pClass, kvals);
|
keyexps[ii++] = new Key.Expression(_pClass, kvals);
|
||||||
}
|
}
|
||||||
return new Or(keyexps);
|
return Ops.or(keyexps);
|
||||||
}
|
}
|
||||||
|
|
||||||
// from Iterable<Key<T>>
|
// from Iterable<Key<T>>
|
||||||
|
|||||||
@@ -26,12 +26,11 @@ import com.samskivert.depot.clause.SelectClause;
|
|||||||
import com.samskivert.depot.expression.FluentExp;
|
import com.samskivert.depot.expression.FluentExp;
|
||||||
import com.samskivert.depot.expression.SQLExpression;
|
import com.samskivert.depot.expression.SQLExpression;
|
||||||
import com.samskivert.depot.impl.operator.Add;
|
import com.samskivert.depot.impl.operator.Add;
|
||||||
import com.samskivert.depot.impl.operator.And;
|
|
||||||
import com.samskivert.depot.impl.operator.Exists;
|
import com.samskivert.depot.impl.operator.Exists;
|
||||||
import com.samskivert.depot.impl.operator.Like;
|
import com.samskivert.depot.impl.operator.Like;
|
||||||
import com.samskivert.depot.impl.operator.Mul;
|
import com.samskivert.depot.impl.operator.Mul;
|
||||||
|
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.Or;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides static methods for operator construction that don't fit nicely into the fluent style.
|
* Provides static methods for operator construction that don't fit nicely into the fluent style.
|
||||||
@@ -52,7 +51,7 @@ public class Ops
|
|||||||
*/
|
*/
|
||||||
public static FluentExp and (Collection<? extends SQLExpression> conditions)
|
public static FluentExp and (Collection<? extends SQLExpression> conditions)
|
||||||
{
|
{
|
||||||
return new And(conditions);
|
return and(conditions.toArray(new SQLExpression[conditions.size()]));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -60,7 +59,28 @@ public class Ops
|
|||||||
*/
|
*/
|
||||||
public static FluentExp and (SQLExpression... conditions)
|
public static FluentExp and (SQLExpression... conditions)
|
||||||
{
|
{
|
||||||
return new And(conditions);
|
return new MultiOperator(conditions) {
|
||||||
|
@Override public String operator() {
|
||||||
|
return " and ";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public Object evaluate (Object[] values) {
|
||||||
|
// if all operands are true, return true, else if all operands are boolean, return
|
||||||
|
// false, else return NO_VALUE
|
||||||
|
boolean allTrue = true;
|
||||||
|
for (Object value : values) {
|
||||||
|
if (value instanceof NoValue) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
if (Boolean.FALSE.equals(value)) {
|
||||||
|
allTrue = false;
|
||||||
|
} else if (!Boolean.TRUE.equals(value)) {
|
||||||
|
return new NoValue("Non-boolean operand to AND: " + value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return allTrue;
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -68,7 +88,7 @@ public class Ops
|
|||||||
*/
|
*/
|
||||||
public static FluentExp or (Collection<? extends SQLExpression> conditions)
|
public static FluentExp or (Collection<? extends SQLExpression> conditions)
|
||||||
{
|
{
|
||||||
return new Or(conditions);
|
return or(conditions.toArray(new SQLExpression[conditions.size()]));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -76,7 +96,26 @@ public class Ops
|
|||||||
*/
|
*/
|
||||||
public static FluentExp or (SQLExpression... conditions)
|
public static FluentExp or (SQLExpression... conditions)
|
||||||
{
|
{
|
||||||
return new Or(conditions);
|
return new MultiOperator(conditions) {
|
||||||
|
@Override public String operator() {
|
||||||
|
return " or ";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public Object evaluate (Object[] values) {
|
||||||
|
boolean anyTrue = false;
|
||||||
|
for (Object value : values) {
|
||||||
|
if (value instanceof NoValue) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
if (Boolean.TRUE.equals(value)) {
|
||||||
|
anyTrue = true;
|
||||||
|
} else if (!Boolean.FALSE.equals(value)) {
|
||||||
|
return new NoValue("Non-boolean operand to OR: " + value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return anyTrue;
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -22,12 +22,12 @@ package com.samskivert.depot.clause;
|
|||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import com.samskivert.depot.Ops;
|
||||||
import com.samskivert.depot.PersistentRecord;
|
import com.samskivert.depot.PersistentRecord;
|
||||||
import com.samskivert.depot.expression.ColumnExp;
|
import com.samskivert.depot.expression.ColumnExp;
|
||||||
import com.samskivert.depot.expression.SQLExpression;
|
import com.samskivert.depot.expression.SQLExpression;
|
||||||
import com.samskivert.depot.impl.ExpressionVisitor;
|
import com.samskivert.depot.impl.ExpressionVisitor;
|
||||||
import com.samskivert.depot.impl.expression.ValueExp;
|
import com.samskivert.depot.impl.expression.ValueExp;
|
||||||
import com.samskivert.depot.impl.operator.And;
|
|
||||||
import com.samskivert.depot.impl.operator.Equals;
|
import com.samskivert.depot.impl.operator.Equals;
|
||||||
import com.samskivert.depot.impl.operator.IsNull;
|
import com.samskivert.depot.impl.operator.IsNull;
|
||||||
|
|
||||||
@@ -97,7 +97,7 @@ public class Where extends WhereClause
|
|||||||
comparisons[ii] = (values[ii] == null) ? new IsNull(columns[ii]) :
|
comparisons[ii] = (values[ii] == null) ? new IsNull(columns[ii]) :
|
||||||
new Equals(columns[ii], new ValueExp(values[ii]));
|
new Equals(columns[ii], new ValueExp(values[ii]));
|
||||||
}
|
}
|
||||||
return new And(comparisons);
|
return Ops.and(comparisons);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected SQLExpression _condition;
|
protected SQLExpression _condition;
|
||||||
|
|||||||
@@ -20,8 +20,8 @@
|
|||||||
|
|
||||||
package com.samskivert.depot.expression;
|
package com.samskivert.depot.expression;
|
||||||
|
|
||||||
|
import com.samskivert.depot.Ops;
|
||||||
import com.samskivert.depot.impl.operator.Add;
|
import com.samskivert.depot.impl.operator.Add;
|
||||||
import com.samskivert.depot.impl.operator.And;
|
|
||||||
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.Div;
|
import com.samskivert.depot.impl.operator.Div;
|
||||||
@@ -32,7 +32,6 @@ import com.samskivert.depot.impl.operator.LessThan;
|
|||||||
import com.samskivert.depot.impl.operator.LessThanEquals;
|
import com.samskivert.depot.impl.operator.LessThanEquals;
|
||||||
import com.samskivert.depot.impl.operator.Mul;
|
import com.samskivert.depot.impl.operator.Mul;
|
||||||
import com.samskivert.depot.impl.operator.NotEquals;
|
import com.samskivert.depot.impl.operator.NotEquals;
|
||||||
import com.samskivert.depot.impl.operator.Or;
|
|
||||||
import com.samskivert.depot.impl.operator.Sub;
|
import com.samskivert.depot.impl.operator.Sub;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -117,13 +116,13 @@ public abstract class FluentExp
|
|||||||
/** Returns an {@link And} with this expression and the supplied target. */
|
/** Returns an {@link And} with this expression and the supplied target. */
|
||||||
public FluentExp and (SQLExpression expr)
|
public FluentExp and (SQLExpression expr)
|
||||||
{
|
{
|
||||||
return new And(this, expr);
|
return Ops.and(this, expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns an {@link Or} with this expression and the supplied target. */
|
/** Returns an {@link Or} with this expression and the supplied target. */
|
||||||
public FluentExp or (SQLExpression expr)
|
public FluentExp or (SQLExpression expr)
|
||||||
{
|
{
|
||||||
return new Or(this, expr);
|
return Ops.or(this, expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns an {@link BitAnd} with this expression and the supplied target. */
|
/** Returns an {@link BitAnd} with this expression and the supplied target. */
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ import com.samskivert.jdbc.ColumnDefinition;
|
|||||||
import com.samskivert.util.ArrayUtil;
|
import com.samskivert.util.ArrayUtil;
|
||||||
import com.samskivert.util.Tuple;
|
import com.samskivert.util.Tuple;
|
||||||
|
|
||||||
|
import com.samskivert.depot.Ops;
|
||||||
import com.samskivert.depot.PersistentRecord;
|
import com.samskivert.depot.PersistentRecord;
|
||||||
import com.samskivert.depot.annotation.FullTextIndex;
|
import com.samskivert.depot.annotation.FullTextIndex;
|
||||||
import com.samskivert.depot.annotation.GeneratedValue;
|
import com.samskivert.depot.annotation.GeneratedValue;
|
||||||
@@ -47,15 +48,14 @@ import com.samskivert.depot.expression.*;
|
|||||||
import com.samskivert.depot.operator.FullText;
|
import com.samskivert.depot.operator.FullText;
|
||||||
|
|
||||||
import com.samskivert.depot.impl.clause.CreateIndexClause;
|
import com.samskivert.depot.impl.clause.CreateIndexClause;
|
||||||
|
import com.samskivert.depot.impl.expression.DateFun.DatePart.Part;
|
||||||
import com.samskivert.depot.impl.expression.DateFun.DatePart;
|
import com.samskivert.depot.impl.expression.DateFun.DatePart;
|
||||||
import com.samskivert.depot.impl.expression.DateFun.DateTruncate;
|
import com.samskivert.depot.impl.expression.DateFun.DateTruncate;
|
||||||
import com.samskivert.depot.impl.expression.DateFun.DatePart.Part;
|
|
||||||
import com.samskivert.depot.impl.expression.StringFun.Lower;
|
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.MultiOperator;
|
||||||
import com.samskivert.depot.impl.operator.Or;
|
|
||||||
|
|
||||||
public class HSQLBuilder
|
public class HSQLBuilder
|
||||||
extends SQLBuilder
|
extends SQLBuilder
|
||||||
@@ -90,7 +90,7 @@ public class HSQLBuilder
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// then just OR them all together and we have our query
|
// then just OR them all together and we have our query
|
||||||
_ftsCondition = new Or(bits);
|
_ftsCondition = Ops.or(bits);
|
||||||
_ftsCondition.accept(this);
|
_ftsCondition.accept(this);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,65 +0,0 @@
|
|||||||
//
|
|
||||||
// $Id$
|
|
||||||
//
|
|
||||||
// Depot library - a Java relational persistence library
|
|
||||||
// Copyright (C) 2006-2009 Michael Bayne and Pär Winzell
|
|
||||||
//
|
|
||||||
// This library is free software; you can redistribute it and/or modify it
|
|
||||||
// under the terms of the GNU Lesser General Public License as published
|
|
||||||
// by the Free Software Foundation; either version 2.1 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// This library is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
// Lesser General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Lesser General Public
|
|
||||||
// License along with this library; if not, write to the Free Software
|
|
||||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
||||||
|
|
||||||
package com.samskivert.depot.impl.operator;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
import com.samskivert.depot.expression.SQLExpression;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a condition that is true iff all its subconditions are true.
|
|
||||||
*/
|
|
||||||
public class And extends MultiOperator
|
|
||||||
{
|
|
||||||
public And (Collection<? extends SQLExpression> conditions)
|
|
||||||
{
|
|
||||||
super(conditions.toArray(new SQLExpression[conditions.size()]));
|
|
||||||
}
|
|
||||||
|
|
||||||
public And (SQLExpression... conditions)
|
|
||||||
{
|
|
||||||
super(conditions);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public String operator()
|
|
||||||
{
|
|
||||||
return " and ";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object evaluate (Object[] values)
|
|
||||||
{
|
|
||||||
// if all operands are true, return true, else if all operands are boolean, return
|
|
||||||
// false, else return NO_VALUE
|
|
||||||
boolean allTrue = true;
|
|
||||||
for (Object value : values) {
|
|
||||||
if (value instanceof NoValue) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
if (Boolean.FALSE.equals(value)) {
|
|
||||||
allTrue = false;
|
|
||||||
} else if (!Boolean.TRUE.equals(value)) {
|
|
||||||
return new NoValue("Non-boolean operand to AND: " + value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return allTrue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
//
|
|
||||||
// $Id$
|
|
||||||
//
|
|
||||||
// Depot library - a Java relational persistence library
|
|
||||||
// Copyright (C) 2006-2009 Michael Bayne and Pär Winzell
|
|
||||||
//
|
|
||||||
// This library is free software; you can redistribute it and/or modify it
|
|
||||||
// under the terms of the GNU Lesser General Public License as published
|
|
||||||
// by the Free Software Foundation; either version 2.1 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// This library is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
// Lesser General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Lesser General Public
|
|
||||||
// License along with this library; if not, write to the Free Software
|
|
||||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
||||||
|
|
||||||
package com.samskivert.depot.impl.operator;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
import com.samskivert.depot.expression.SQLExpression;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a condition that is false iff all its subconditions are false.
|
|
||||||
*/
|
|
||||||
public class Or extends MultiOperator
|
|
||||||
{
|
|
||||||
public Or (Collection<? extends SQLExpression> conditions)
|
|
||||||
{
|
|
||||||
super(conditions.toArray(new SQLExpression[conditions.size()]));
|
|
||||||
}
|
|
||||||
|
|
||||||
public Or (SQLExpression... conditions)
|
|
||||||
{
|
|
||||||
super(conditions);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public String operator()
|
|
||||||
{
|
|
||||||
return " or ";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object evaluate (Object[] values)
|
|
||||||
{
|
|
||||||
boolean anyTrue = false;
|
|
||||||
for (Object value : values) {
|
|
||||||
if (value instanceof NoValue) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
if (Boolean.TRUE.equals(value)) {
|
|
||||||
anyTrue = true;
|
|
||||||
} else if (!Boolean.FALSE.equals(value)) {
|
|
||||||
return new NoValue("Non-boolean operand to OR: " + value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return anyTrue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user