This is going to break everything, but I couldn't stand our operator inner

classes any more, so I promoted them all to standalone classes.
This commit is contained in:
Michael Bayne
2009-05-27 23:30:19 +00:00
parent 601b9eaba1
commit ed6eaae7b7
36 changed files with 1418 additions and 917 deletions
+9 -9
View File
@@ -34,13 +34,13 @@ import com.samskivert.util.Logger;
import com.samskivert.util.StringUtil;
import com.samskivert.depot.clause.WhereClause;
import com.samskivert.depot.operator.In;
import com.samskivert.depot.operator.Or;
import com.samskivert.depot.expression.ColumnExp;
import com.samskivert.depot.expression.LiteralExp;
import com.samskivert.depot.expression.SQLExpression;
import com.samskivert.depot.impl.DepotUtil;
import com.samskivert.depot.impl.ExpressionVisitor;
import com.samskivert.depot.operator.Conditionals;
import com.samskivert.depot.operator.Logic;
/**
* Contains a set of primary keys that match a set of persistent records. This is used internally
@@ -162,9 +162,9 @@ public abstract class KeySet<T extends PersistentRecord> extends WhereClause
public SingleKeySet (Class<T> pClass, Comparable<?>[] keys) {
super(pClass);
// TODO: remove when we update to 1.6 and change Postgres In handling
if (keys.length > Conditionals.In.MAX_KEYS) {
if (keys.length > In.MAX_KEYS) {
throw new IllegalArgumentException("Cannot create where clause for more than " +
Conditionals.In.MAX_KEYS + " at a time.");
In.MAX_KEYS + " at a time.");
}
_keys = keys;
}
@@ -172,7 +172,7 @@ public abstract class KeySet<T extends PersistentRecord> extends WhereClause
@Override public SQLExpression getWhereExpression () {
// Single-column keys result in the compact IN(keyVal1, keyVal2, ...)
ColumnExp column = new ColumnExp(_pClass, DepotUtil.getKeyFields(_pClass)[0]);
return new Conditionals.In(column, _keys);
return new In(column, _keys);
}
// from Iterable<Key<T>>
@@ -226,16 +226,16 @@ public abstract class KeySet<T extends PersistentRecord> extends WhereClause
for (Comparable<?>[] kvals : _keys) {
keyexps[ii++] = new Key.Expression<T>(_pClass, kvals);
}
return new Logic.Or(keyexps);
return new Or(keyexps);
}
// from Iterable<Key<T>>
public Iterator<Key<T>> iterator () {
return Iterators.transform(
Iterators.forArray(_keys, 0, _keys.length), new Function<Comparable<?>[], Key<T>>() {
return Iterators.transform(Iterators.forArray(_keys, 0, _keys.length),
new Function<Comparable<?>[], Key<T>>() {
public Key<T> apply (Comparable<?>[] key) {
return new Key<T>(_pClass, key);
}
}
});
}
@@ -27,7 +27,7 @@ import com.samskivert.depot.expression.ColumnExp;
import com.samskivert.depot.expression.SQLExpression;
import com.samskivert.depot.impl.DepotUtil;
import com.samskivert.depot.impl.ExpressionVisitor;
import com.samskivert.depot.operator.Conditionals.Equals;
import com.samskivert.depot.operator.Equals;
/**
* Represents a JOIN.
@@ -27,9 +27,9 @@ import com.samskivert.depot.expression.ColumnExp;
import com.samskivert.depot.expression.SQLExpression;
import com.samskivert.depot.expression.ValueExp;
import com.samskivert.depot.impl.ExpressionVisitor;
import com.samskivert.depot.operator.Conditionals.Equals;
import com.samskivert.depot.operator.Conditionals.IsNull;
import com.samskivert.depot.operator.Logic.And;
import com.samskivert.depot.operator.And;
import com.samskivert.depot.operator.Equals;
import com.samskivert.depot.operator.IsNull;
/**
* Represents a where clause: the condition can be any comparison operator or logical combination
@@ -44,22 +44,22 @@ import com.samskivert.depot.clause.GroupBy;
import com.samskivert.depot.clause.InsertClause;
import com.samskivert.depot.clause.Join;
import com.samskivert.depot.clause.Limit;
import com.samskivert.depot.clause.OrderBy.Order;
import com.samskivert.depot.clause.OrderBy;
import com.samskivert.depot.clause.SelectClause;
import com.samskivert.depot.clause.WhereClause;
import com.samskivert.depot.clause.OrderBy.Order;
import com.samskivert.depot.expression.ColumnExp;
import com.samskivert.depot.expression.EpochSeconds;
import com.samskivert.depot.expression.FunctionExp;
import com.samskivert.depot.expression.LiteralExp;
import com.samskivert.depot.expression.SQLExpression;
import com.samskivert.depot.expression.ValueExp;
import com.samskivert.depot.operator.Conditionals.Case;
import com.samskivert.depot.operator.Conditionals.Exists;
import com.samskivert.depot.operator.Conditionals.FullText;
import com.samskivert.depot.operator.Conditionals.In;
import com.samskivert.depot.operator.Conditionals.IsNull;
import com.samskivert.depot.operator.Logic.Not;
import com.samskivert.depot.operator.Case;
import com.samskivert.depot.operator.Exists;
import com.samskivert.depot.operator.FullText;
import com.samskivert.depot.operator.In;
import com.samskivert.depot.operator.IsNull;
import com.samskivert.depot.operator.Not;
import com.samskivert.depot.operator.SQLOperator.BinaryOperator;
import com.samskivert.depot.operator.SQLOperator.MultiOperator;
@@ -45,12 +45,12 @@ import com.samskivert.depot.expression.SQLExpression;
import com.samskivert.depot.expression.SQLExpression.NoValue;
import com.samskivert.depot.expression.ValueExp;
import com.samskivert.depot.operator.Conditionals.Case;
import com.samskivert.depot.operator.Conditionals.Exists;
import com.samskivert.depot.operator.Conditionals.FullText;
import com.samskivert.depot.operator.Conditionals.In;
import com.samskivert.depot.operator.Conditionals.IsNull;
import com.samskivert.depot.operator.Logic.Not;
import com.samskivert.depot.operator.Case;
import com.samskivert.depot.operator.Exists;
import com.samskivert.depot.operator.FullText;
import com.samskivert.depot.operator.In;
import com.samskivert.depot.operator.IsNull;
import com.samskivert.depot.operator.Not;
import com.samskivert.depot.operator.SQLOperator.BinaryOperator;
import com.samskivert.depot.operator.SQLOperator.MultiOperator;
@@ -40,12 +40,12 @@ import com.samskivert.depot.expression.FunctionExp;
import com.samskivert.depot.expression.LiteralExp;
import com.samskivert.depot.expression.ValueExp;
import com.samskivert.depot.operator.Conditionals.Case;
import com.samskivert.depot.operator.Conditionals.Exists;
import com.samskivert.depot.operator.Conditionals.FullText;
import com.samskivert.depot.operator.Conditionals.In;
import com.samskivert.depot.operator.Conditionals.IsNull;
import com.samskivert.depot.operator.Logic.Not;
import com.samskivert.depot.operator.Case;
import com.samskivert.depot.operator.Exists;
import com.samskivert.depot.operator.FullText;
import com.samskivert.depot.operator.In;
import com.samskivert.depot.operator.IsNull;
import com.samskivert.depot.operator.Not;
import com.samskivert.depot.operator.SQLOperator.BinaryOperator;
import com.samskivert.depot.operator.SQLOperator.MultiOperator;
@@ -38,7 +38,9 @@ import com.google.common.collect.Maps;
import com.samskivert.jdbc.DatabaseLiaison;
import com.samskivert.jdbc.JDBCUtil;
import com.samskivert.depot.CacheAdapter.CacheCategory;
import com.samskivert.depot.DatabaseException;
import com.samskivert.depot.DepotRepository.CacheStrategy;
import com.samskivert.depot.DepotRepository;
import com.samskivert.depot.Key;
import com.samskivert.depot.KeySet;
@@ -46,12 +48,10 @@ import com.samskivert.depot.PersistenceContext;
import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.SimpleCacheKey;
import com.samskivert.depot.Stats;
import com.samskivert.depot.CacheAdapter.CacheCategory;
import com.samskivert.depot.DepotRepository.CacheStrategy;
import com.samskivert.depot.clause.FieldOverride;
import com.samskivert.depot.clause.QueryClause;
import com.samskivert.depot.clause.SelectClause;
import com.samskivert.depot.operator.Conditionals.*;
import com.samskivert.depot.operator.In;
import static com.samskivert.depot.Log.log;
@@ -47,11 +47,11 @@ import com.samskivert.depot.expression.ColumnExp;
import com.samskivert.depot.expression.EpochSeconds;
import com.samskivert.depot.expression.FunctionExp;
import com.samskivert.depot.expression.SQLExpression;
import com.samskivert.depot.operator.Arithmetic.BitAnd;
import com.samskivert.depot.operator.Arithmetic.BitOr;
import com.samskivert.depot.operator.Conditionals.FullText;
import com.samskivert.depot.operator.Conditionals.Like;
import com.samskivert.depot.operator.Logic.Or;
import com.samskivert.depot.operator.BitAnd;
import com.samskivert.depot.operator.BitOr;
import com.samskivert.depot.operator.FullText;
import com.samskivert.depot.operator.Like;
import com.samskivert.depot.operator.Or;
import com.samskivert.depot.operator.SQLOperator.MultiOperator;
import com.samskivert.depot.impl.clause.CreateIndexClause;
@@ -39,7 +39,7 @@ import com.samskivert.depot.clause.OrderBy.Order;
import com.samskivert.depot.expression.ColumnExp;
import com.samskivert.depot.expression.EpochSeconds;
import com.samskivert.depot.expression.SQLExpression;
import com.samskivert.depot.operator.Conditionals.FullText;
import com.samskivert.depot.operator.FullText;
import com.samskivert.depot.impl.FieldMarshaller.BooleanMarshaller;
import com.samskivert.depot.impl.FieldMarshaller.ByteArrayMarshaller;
@@ -29,7 +29,7 @@ import java.sql.Timestamp;
import com.samskivert.depot.ByteEnum;
import com.samskivert.depot.DatabaseException;
import com.samskivert.depot.expression.ValueExp;
import com.samskivert.depot.operator.Conditionals.In;
import com.samskivert.depot.operator.In;
/**
* Specializes our PostgreSQL builder for JDBC4.
@@ -40,7 +40,7 @@ import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.annotation.FullTextIndex.Configuration;
import com.samskivert.depot.annotation.FullTextIndex;
import com.samskivert.depot.expression.EpochSeconds;
import com.samskivert.depot.operator.Conditionals.FullText;
import com.samskivert.depot.operator.FullText;
import static com.samskivert.Log.log;
@@ -0,0 +1,59 @@
//
// $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.operator;
import com.samskivert.depot.expression.SQLExpression;
/**
* The SQL '+' operator.
*/
public class Add extends Arithmetic
{
public Add (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public Add (SQLExpression... values)
{
super(values);
}
@Override // from Arithmetic
public String operator()
{
return "+";
}
@Override // from Arithmetic
public Object evaluate (Object[] operands)
{
return evaluate(operands, "+", new Accumulator<Double>() {
public Double accumulate (Double left, Double right) {
return left + right;
}
}, new Accumulator<Long>() {
public Long accumulate (Long left, Long right) {
return left + right;
}
});
}
}
@@ -0,0 +1,65 @@
//
// $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.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 SQLOperator.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;
}
}
@@ -23,199 +23,12 @@ package com.samskivert.depot.operator;
import com.samskivert.depot.expression.SQLExpression;
import com.samskivert.depot.expression.ValueExp;
import com.samskivert.depot.operator.SQLOperator.MultiOperator;
import com.samskivert.util.StringUtil;
/**
* A convenient container for implementations of arithmetic operators. Classes that value brevity
* that feel otherwise will use Arithmetic.Add() and Arithmetic.Sub().
* A convenient container for implementations of arithmetic operators.
*/
public abstract class Arithmetic extends MultiOperator
{
/** The SQL '+' operator. */
public static class Add extends Arithmetic
{
public Add (SQLExpression column, Comparable<?> value)
{
super(column, new ValueExp(value));
}
public Add (SQLExpression... values)
{
super(values);
}
@Override public String operator()
{
return "+";
}
@Override
public Object evaluate (Object[] operands)
{
return evaluate(operands, "+", new Accumulator<Double>() {
public Double accumulate (Double left, Double right) {
return left + right;
}}, new Accumulator<Long>() {
public Long accumulate (Long left, Long right) {
return left + right;
}});
}
}
/** The SQL '-' operator. */
public static class Sub extends Arithmetic
{
public Sub (SQLExpression column, Comparable<?> value)
{
super(column, new ValueExp(value));
}
public Sub (SQLExpression... values)
{
super(values);
}
@Override public String operator()
{
return "-";
}
@Override
public Object evaluate (Object[] operands)
{
return evaluate(operands, "-", new Accumulator<Double>() {
public Double accumulate (Double left, Double right) {
return left - right;
}}, new Accumulator<Long>() {
public Long accumulate (Long left, Long right) {
return left - right;
}});
}
}
/** The SQL '*' operator. */
public static class Mul extends Arithmetic
{
public Mul (SQLExpression column, Comparable<?> value)
{
super(column, new ValueExp(value));
}
public Mul (SQLExpression... values)
{
super(values);
}
@Override public String operator()
{
return "*";
}
@Override
public Object evaluate (Object[] operands)
{
return evaluate(operands, "*", new Accumulator<Double>() {
public Double accumulate (Double left, Double right) {
return left * right;
}}, new Accumulator<Long>() {
public Long accumulate (Long left, Long right) {
return left * right;
}});
}
}
/** The SQL '/' operator. */
public static class Div extends Arithmetic
{
public Div (SQLExpression column, Comparable<?> value)
{
super(column, new ValueExp(value));
}
public Div (SQLExpression... values)
{
super(values);
}
@Override public String operator()
{
return " / "; // Pad with spaces to work-around a MySQL bug.
}
@Override
public Object evaluate (Object[] operands)
{
for (int ii = 1; ii < operands.length; ii ++) {
if (Double.valueOf(0).equals(NUMERICAL.apply(operands[ii]))) {
return new NoValue("Division by zero in: " + StringUtil.toString(operands));
}
}
return evaluate(operands, "/", new Accumulator<Double>() {
public Double accumulate (Double left, Double right) {
return left / right;
}}, new Accumulator<Long>() {
public Long accumulate (Long left, Long right) {
return left / right;
}});
}
}
/** The SQL '&' operator. */
public static class BitAnd extends Arithmetic
{
public BitAnd (SQLExpression column, Comparable<?> value)
{
super(column, new ValueExp(value));
}
public BitAnd (SQLExpression... values)
{
super(values);
}
@Override public String operator()
{
return "&";
}
@Override
public Object evaluate (Object[] operands)
{
return evaluate(operands, "&", null, new Accumulator<Long>() {
public Long accumulate (Long left, Long right) {
return left & right;
}});
}
}
/** The SQL '|' operator. */
public static class BitOr extends Arithmetic
{
public BitOr (SQLExpression column, Comparable<?> value)
{
super(column, new ValueExp(value));
}
public BitOr (SQLExpression... values)
{
super(values);
}
@Override public String operator()
{
return "|";
}
@Override
public Object evaluate (Object[] operands)
{
return evaluate(operands, "|", null, new Accumulator<Long>() {
public Long accumulate (Long left, Long right) {
return left | right;
}});
}
}
public Arithmetic (SQLExpression column, Comparable<?> value)
{
super(column, new ValueExp(value));
@@ -0,0 +1,55 @@
//
// $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.operator;
import com.samskivert.depot.expression.SQLExpression;
/**
* The SQL '&' operator.
*/
public class BitAnd extends Arithmetic
{
public BitAnd (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public BitAnd (SQLExpression... values)
{
super(values);
}
@Override // from Arithmetic
public String operator()
{
return "&";
}
@Override // from Arithmetic
public Object evaluate (Object[] operands)
{
return evaluate(operands, "&", null, new Accumulator<Long>() {
public Long accumulate (Long left, Long right) {
return left & right;
}
});
}
}
@@ -0,0 +1,55 @@
//
// $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.operator;
import com.samskivert.depot.expression.SQLExpression;
/**
* The SQL '|' operator.
*/
public class BitOr extends Arithmetic
{
public BitOr (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public BitOr (SQLExpression... values)
{
super(values);
}
@Override // from Arithmetic
public String operator()
{
return "|";
}
@Override // from Arithmetic
public Object evaluate (Object[] operands)
{
return evaluate(operands, "|", null, new Accumulator<Long>() {
public Long accumulate (Long left, Long right) {
return left | right;
}
});
}
}
@@ -0,0 +1,93 @@
//
// $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.operator;
import java.util.Collection;
import java.util.List;
import com.google.common.collect.Lists;
import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.expression.SQLExpression;
import com.samskivert.depot.impl.ExpressionVisitor;
import com.samskivert.util.Tuple;
/**
* The SQL 'case' operator.
*/
public class Case implements SQLOperator
{
public Case (SQLExpression... exps)
{
int i = 0;
while (i+1 < exps.length) {
_whenExps.add(Tuple.newTuple(exps[i], exps[i+1]));
i += 2;
}
_elseExp = (i < exps.length) ? exps[i] : null;
}
public List<Tuple<SQLExpression, SQLExpression>> getWhenExps ()
{
return _whenExps;
}
public SQLExpression getElseExp ()
{
return _elseExp;
}
// from SQLExpression
public Object accept (ExpressionVisitor<?> builder)
{
return builder.visit(this);
}
// from SQLExpression
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet)
{
for (Tuple<SQLExpression, SQLExpression> tuple : _whenExps) {
tuple.left.addClasses(classSet);
tuple.right.addClasses(classSet);
}
if (_elseExp != null) {
_elseExp.addClasses(classSet);
}
}
@Override // from Object
public String toString ()
{
StringBuilder builder = new StringBuilder();
builder.append("Case(");
for (Tuple<SQLExpression, SQLExpression> tuple : _whenExps) {
builder.append(tuple.left.toString()).append("->");
builder.append(tuple.right.toString()).append(",");
}
if (_elseExp != null) {
builder.append(_elseExp.toString()).append(")");
}
return builder.toString();
}
protected List<Tuple<SQLExpression, SQLExpression>> _whenExps = Lists.newArrayList();
protected SQLExpression _elseExp;
}
@@ -1,533 +0,0 @@
//
// $Id$
//
// Depot library - a Java relational persistence library
// Copyright (C) 2006-2008 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.operator;
import java.util.Collection;
import java.util.List;
import com.google.common.collect.Lists;
import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.clause.SelectClause;
import com.samskivert.depot.expression.ColumnExp;
import com.samskivert.depot.expression.SQLExpression;
import com.samskivert.depot.impl.ExpressionVisitor;
import com.samskivert.util.Tuple;
import static com.samskivert.Log.log;
/**
* A convenient container for implementations of conditional operators. Classes that value brevity
* classes that feel otherwise will use Conditionals.Equals() and Conditionals.In().
*/
public abstract class Conditionals
{
/** The SQL 'is null' operator. */
public static class IsNull
implements SQLOperator
{
public IsNull (ColumnExp column)
{
_column = column;
}
public ColumnExp getColumn()
{
return _column;
}
// from SQLExpression
public Object accept (ExpressionVisitor<?> builder)
{
return builder.visit(this);
}
// from SQLExpression
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet)
{
}
@Override // from Object
public String toString ()
{
return "IsNull(" + _column + ")";
}
protected ColumnExp _column;
}
/** The SQL '=' operator. */
public static class Equals extends SQLOperator.BinaryOperator
{
public Equals (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public Equals (SQLExpression column, SQLExpression value)
{
super(column, value);
}
@Override public String operator()
{
return "=";
}
@Override
public Object evaluate (Object left, Object right)
{
if (left == null || right == null) {
return new NoValue("Null operand to '=': (" + left + ", " + right + ")");
}
return left.equals(right);
}
}
/** The SQL '!=' operator. */
public static class NotEquals extends SQLOperator.BinaryOperator
{
public NotEquals (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public NotEquals (SQLExpression column, SQLExpression value)
{
super(column, value);
}
@Override public String operator()
{
return "!=";
}
@Override
public Object evaluate (Object left, Object right)
{
if (left == null || right == null) {
return new NoValue("Null operand to '!=': (" + left + ", " + right + ")");
}
return !left.equals(right);
}
}
/** The SQL '<' operator. */
public static class LessThan extends SQLOperator.BinaryOperator
{
public LessThan (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public LessThan (SQLExpression column, SQLExpression value)
{
super(column, value);
}
@Override public String operator()
{
return "<";
}
@Override
public Object evaluate (Object left, Object right)
{
if (all(NUMERICAL, left, right)) {
return NUMERICAL.apply(left) < NUMERICAL.apply(right);
}
if (all(STRING, left, right) || all(DATE, left, right)) {
return compare(STRING, left, right) < 0;
}
return new NoValue("Non-comparable operand to '<': (" + left + ", " + right + ")");
}
}
/** The SQL '<=' operator. */
public static class LessThanEquals extends SQLOperator.BinaryOperator
{
public LessThanEquals (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public LessThanEquals (SQLExpression column, SQLExpression value)
{
super(column, value);
}
@Override public String operator()
{
return "<=";
}
@Override
public Object evaluate (Object left, Object right)
{
if (all(NUMERICAL, left, right)) {
return NUMERICAL.apply(left) <= NUMERICAL.apply(right);
}
if (all(STRING, left, right) || all(DATE, left, right)) {
return compare(STRING, left, right) <= 0;
}
return new NoValue("Non-comparable operand to '<=': (" + left + ", " + right + ")");
}
}
/** The SQL '>' operator. */
public static class GreaterThan extends SQLOperator.BinaryOperator
{
public GreaterThan (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public GreaterThan (SQLExpression column, SQLExpression value)
{
super(column, value);
}
@Override public String operator()
{
return ">";
}
@Override
public Object evaluate (Object left, Object right)
{
if (all(NUMERICAL, left, right)) {
return NUMERICAL.apply(left) > NUMERICAL.apply(right);
}
if (all(STRING, left, right) || all(DATE, left, right)) {
return compare(STRING, left, right) > 0;
}
return new NoValue("Non-comparable operand to '>': (" + left + ", " + right + ")");
}
}
/** The SQL '>=' operator. */
public static class GreaterThanEquals extends SQLOperator.BinaryOperator
{
public GreaterThanEquals (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public GreaterThanEquals (SQLExpression column, SQLExpression value)
{
super(column, value);
}
@Override public String operator()
{
return ">=";
}
@Override
public Object evaluate (Object left, Object right)
{
if (all(NUMERICAL, left, right)) {
return NUMERICAL.apply(left) >= NUMERICAL.apply(right);
}
if (all(STRING, left, right) || all(DATE, left, right)) {
return compare(STRING, left, right) >= 0;
}
return new NoValue("Non-comparable operand to '>=': (" + left + ", " + right + ")");
}
}
/** The SQL 'in (...)' operator. */
public static class In
implements SQLOperator
{
/** 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)
{
if (values.length == 0) {
log.warning("Grouchily allowing empty In() operator", "column", column.name,
new Exception());
}
_column = column;
_values = values;
}
public In (ColumnExp pColumn, Collection<? extends Comparable<?>> values)
{
this(pColumn, values.toArray(new Comparable<?>[values.size()]));
}
public ColumnExp getColumn ()
{
return _column;
}
public Comparable<?>[] getValues ()
{
return _values;
}
// from SQLExpression
public Object accept (ExpressionVisitor<?> builder)
{
return builder.visit(this);
}
// from SQLExpression
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet)
{
_column.addClasses(classSet);
}
@Override // from Object
public String toString ()
{
StringBuilder builder = new StringBuilder();
builder.append(_column).append(" in (");
for (int ii = 0; ii < _values.length; ii++) {
if (ii > 0) {
builder.append(", ");
}
builder.append(_values[ii]);
}
return builder.append(")").toString();
}
protected ColumnExp _column;
protected Comparable<?>[] _values;
}
/** The SQL ' like ' operator. */
public static class Like extends SQLOperator.BinaryOperator
{
public Like (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public Like (SQLExpression column, SQLExpression value)
{
super(column, value);
}
@Override public String operator()
{
return " like ";
}
@Override
public Object evaluate (Object left, Object right)
{
return new NoValue("Like operator not implemented");
}
}
/** The SQL ' exists' operator. */
public static class Exists<T extends PersistentRecord> implements SQLOperator
{
public Exists (SelectClause<T> clause)
{
_clause = clause;
}
public Object accept (ExpressionVisitor<?> builder)
{
return builder.visit(this);
}
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet)
{
_clause.addClasses(classSet);
}
public SelectClause<T> getSubClause ()
{
return _clause;
}
@Override // from Object
public String toString ()
{
return "Exists(" + _clause + ")";
}
protected SelectClause<T> _clause;
}
public static class Case implements SQLOperator
{
public Case (SQLExpression... exps)
{
int i = 0;
while (i+1 < exps.length) {
_whenExps.add(Tuple.newTuple(exps[i], exps[i+1]));
i += 2;
}
_elseExp = (i < exps.length) ? exps[i] : null;
}
// from SQLExpression
public Object accept (ExpressionVisitor<?> builder)
{
return builder.visit(this);
}
// from SQLExpression
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet)
{
for (Tuple<SQLExpression, SQLExpression> tuple : _whenExps) {
tuple.left.addClasses(classSet);
tuple.right.addClasses(classSet);
}
if (_elseExp != null) {
_elseExp.addClasses(classSet);
}
}
public List<Tuple<SQLExpression, SQLExpression>> getWhenExps ()
{
return _whenExps;
}
public SQLExpression getElseExp ()
{
return _elseExp;
}
@Override // from Object
public String toString ()
{
StringBuilder builder = new StringBuilder();
builder.append("Case(");
for (Tuple<SQLExpression, SQLExpression> tuple : _whenExps) {
builder.append(tuple.left.toString()).append("->");
builder.append(tuple.right.toString()).append(",");
}
if (_elseExp != null) {
builder.append(_elseExp.toString()).append(")");
}
return builder.toString();
}
protected List<Tuple<SQLExpression, SQLExpression>> _whenExps = Lists.newArrayList();
protected SQLExpression _elseExp;
}
/**
* An attempt at a dialect-agnostic full-text search condition, such as MySQL's MATCH() and
* PostgreSQL's @@ TO_TSQUERY(...) abilities.
*/
public static class FullText
{
public class Rank
implements SQLOperator
{
// from SQLExpression
public Object accept (ExpressionVisitor<?> builder)
{
return builder.visit(this);
}
// from SQLExpression
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet)
{
}
@Override // from Object
public String toString ()
{
return FullText.this.toString("Rank");
}
public FullText getDefinition ()
{
return FullText.this;
}
}
public class Match
implements SQLOperator
{
// from SQLExpression
public Object accept (ExpressionVisitor<?> builder)
{
return builder.visit(this);
}
public FullText getDefinition ()
{
return FullText.this;
}
// from SQLExpression
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet)
{
}
@Override // from Object
public String toString ()
{
return FullText.this.toString("Match");
}
}
public FullText (Class<? extends PersistentRecord> pClass, String name, String query)
{
_pClass = pClass;
_name = name;
_query = query;
}
public SQLOperator match ()
{
return new Match();
}
public SQLOperator rank ()
{
return new Rank();
}
public Class<? extends PersistentRecord> getPersistentClass ()
{
return _pClass;
}
public String getName ()
{
return _name;
}
public String getQuery ()
{
return _query;
}
protected String toString (String subType)
{
return "FullText." + subType + "(" + _name + "=" + _query + ")";
}
protected Class<? extends PersistentRecord> _pClass;
protected String _name;
protected String _query;
}
}
@@ -0,0 +1,66 @@
//
// $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.operator;
import com.samskivert.util.StringUtil;
import com.samskivert.depot.expression.SQLExpression;
/**
* The SQL '/' operator.
*/
public class Div extends Arithmetic
{
public Div (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public Div (SQLExpression... values)
{
super(values);
}
@Override // from Arithmetic
public String operator()
{
return " / "; // Pad with spaces to work-around a MySQL bug.
}
@Override // from Arithmetic
public Object evaluate (Object[] operands)
{
for (int ii = 1; ii < operands.length; ii ++) {
if (Double.valueOf(0).equals(NUMERICAL.apply(operands[ii]))) {
return new NoValue("Division by zero in: " + StringUtil.toString(operands));
}
}
return evaluate(operands, "/", new Accumulator<Double>() {
public Double accumulate (Double left, Double right) {
return left / right;
}
}, new Accumulator<Long>() {
public Long accumulate (Long left, Long right) {
return left / right;
}
});
}
}
@@ -0,0 +1,54 @@
//
// $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.operator;
import com.samskivert.depot.expression.SQLExpression;
/**
* The SQL '=' operator.
*/
public class Equals extends SQLOperator.BinaryOperator
{
public Equals (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public Equals (SQLExpression column, SQLExpression value)
{
super(column, value);
}
@Override // from SQLOperator.BinaryOperator
public String operator()
{
return "=";
}
@Override // from SQLOperator.BinaryOperator
public Object evaluate (Object left, Object right)
{
if (left == null || right == null) {
return new NoValue("Null operand to '=': (" + left + ", " + right + ")");
}
return left.equals(right);
}
}
@@ -0,0 +1,62 @@
//
// $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.operator;
import java.util.Collection;
import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.clause.SelectClause;
import com.samskivert.depot.impl.ExpressionVisitor;
/**
* The SQL 'exists' operator.
*/
public class Exists<T extends PersistentRecord>
implements SQLOperator
{
public Exists (SelectClause<T> clause)
{
_clause = clause;
}
public Object accept (ExpressionVisitor<?> builder)
{
return builder.visit(this);
}
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet)
{
_clause.addClasses(classSet);
}
public SelectClause<T> getSubClause ()
{
return _clause;
}
@Override // from Object
public String toString ()
{
return "Exists(" + _clause + ")";
}
protected SelectClause<T> _clause;
}
@@ -0,0 +1,126 @@
//
// $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.operator;
import java.util.Collection;
import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.impl.ExpressionVisitor;
/**
* An attempt at a dialect-agnostic full-text search condition, such as MySQL's MATCH() and
* PostgreSQL's @@ TO_TSQUERY(...) abilities.
*/
public class FullText
{
public class Rank
implements SQLOperator
{
// from SQLExpression
public Object accept (ExpressionVisitor<?> builder)
{
return builder.visit(this);
}
// from SQLExpression
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet)
{
}
@Override // from Object
public String toString ()
{
return FullText.this.toString("Rank");
}
public FullText getDefinition ()
{
return FullText.this;
}
}
public class Match
implements SQLOperator
{
// from SQLExpression
public Object accept (ExpressionVisitor<?> builder)
{
return builder.visit(this);
}
public FullText getDefinition ()
{
return FullText.this;
}
// from SQLExpression
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet)
{
}
@Override // from Object
public String toString ()
{
return FullText.this.toString("Match");
}
}
public FullText (Class<? extends PersistentRecord> pClass, String name, String query)
{
_pClass = pClass;
_name = name;
_query = query;
}
public SQLOperator match ()
{
return new Match();
}
public SQLOperator rank ()
{
return new Rank();
}
public Class<? extends PersistentRecord> getPersistentClass ()
{
return _pClass;
}
public String getName ()
{
return _name;
}
public String getQuery ()
{
return _query;
}
protected String toString (String subType)
{
return "FullText." + subType + "(" + _name + "=" + _query + ")";
}
protected Class<? extends PersistentRecord> _pClass;
protected String _name;
protected String _query;
}
@@ -0,0 +1,57 @@
//
// $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.operator;
import com.samskivert.depot.expression.SQLExpression;
/**
* The SQL '>' operator.
*/
public class GreaterThan extends SQLOperator.BinaryOperator
{
public GreaterThan (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public GreaterThan (SQLExpression column, SQLExpression value)
{
super(column, value);
}
@Override // from SQLOperator.BinaryOperator
public String operator()
{
return ">";
}
@Override // from SQLOperator.BinaryOperator
public Object evaluate (Object left, Object right)
{
if (all(NUMERICAL, left, right)) {
return NUMERICAL.apply(left) > NUMERICAL.apply(right);
}
if (all(STRING, left, right) || all(DATE, left, right)) {
return compare(STRING, left, right) > 0;
}
return new NoValue("Non-comparable operand to '>': (" + left + ", " + right + ")");
}
}
@@ -0,0 +1,57 @@
//
// $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.operator;
import com.samskivert.depot.expression.SQLExpression;
/**
* The SQL '>=' operator.
*/
public class GreaterThanEquals extends SQLOperator.BinaryOperator
{
public GreaterThanEquals (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public GreaterThanEquals (SQLExpression column, SQLExpression value)
{
super(column, value);
}
@Override // from SQLOperator.BinaryOperator
public String operator()
{
return ">=";
}
@Override // from SQLOperator.BinaryOperator
public Object evaluate (Object left, Object right)
{
if (all(NUMERICAL, left, right)) {
return NUMERICAL.apply(left) >= NUMERICAL.apply(right);
}
if (all(STRING, left, right) || all(DATE, left, right)) {
return compare(STRING, left, right) >= 0;
}
return new NoValue("Non-comparable operand to '>=': (" + left + ", " + right + ")");
}
}
@@ -0,0 +1,93 @@
//
// $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.operator;
import java.util.Collection;
import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.expression.ColumnExp;
import com.samskivert.depot.impl.ExpressionVisitor;
import static com.samskivert.depot.Log.log;
/**
* The SQL 'in (...)' operator.
*/
public class In
implements SQLOperator
{
/** 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)
{
if (values.length == 0) {
log.warning("Grouchily allowing empty In() operator", "column", column.name,
new Exception());
}
_column = column;
_values = values;
}
public In (ColumnExp pColumn, Collection<? extends Comparable<?>> values)
{
this(pColumn, values.toArray(new Comparable<?>[values.size()]));
}
public ColumnExp getColumn ()
{
return _column;
}
public Comparable<?>[] getValues ()
{
return _values;
}
// from SQLExpression
public Object accept (ExpressionVisitor<?> builder)
{
return builder.visit(this);
}
// from SQLExpression
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet)
{
_column.addClasses(classSet);
}
@Override // from Object
public String toString ()
{
StringBuilder builder = new StringBuilder();
builder.append(_column).append(" in (");
for (int ii = 0; ii < _values.length; ii++) {
if (ii > 0) {
builder.append(", ");
}
builder.append(_values[ii]);
}
return builder.append(")").toString();
}
protected ColumnExp _column;
protected Comparable<?>[] _values;
}
@@ -0,0 +1,63 @@
//
// $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.operator;
import java.util.Collection;
import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.expression.ColumnExp;
import com.samskivert.depot.impl.ExpressionVisitor;
/**
* The SQL 'is null' operator.
*/
public class IsNull
implements SQLOperator
{
public IsNull (ColumnExp column)
{
_column = column;
}
public ColumnExp getColumn()
{
return _column;
}
// from SQLExpression
public Object accept (ExpressionVisitor<?> builder)
{
return builder.visit(this);
}
// from SQLExpression
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet)
{
}
@Override // from Object
public String toString ()
{
return "IsNull(" + _column + ")";
}
protected ColumnExp _column;
}
@@ -0,0 +1,57 @@
//
// $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.operator;
import com.samskivert.depot.expression.SQLExpression;
/**
* The SQL '<' operator.
*/
public class LessThan extends SQLOperator.BinaryOperator
{
public LessThan (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public LessThan (SQLExpression column, SQLExpression value)
{
super(column, value);
}
@Override // from SQLOperator.BinaryOperator
public String operator()
{
return "<";
}
@Override // from SQLOperator.BinaryOperator
public Object evaluate (Object left, Object right)
{
if (all(NUMERICAL, left, right)) {
return NUMERICAL.apply(left) < NUMERICAL.apply(right);
}
if (all(STRING, left, right) || all(DATE, left, right)) {
return compare(STRING, left, right) < 0;
}
return new NoValue("Non-comparable operand to '<': (" + left + ", " + right + ")");
}
}
@@ -0,0 +1,57 @@
//
// $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.operator;
import com.samskivert.depot.expression.SQLExpression;
/**
* The SQL '<=' operator.
*/
public class LessThanEquals extends SQLOperator.BinaryOperator
{
public LessThanEquals (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public LessThanEquals (SQLExpression column, SQLExpression value)
{
super(column, value);
}
@Override // from SQLOperator.BinaryOperator
public String operator()
{
return "<=";
}
@Override // from SQLOperator.BinaryOperator
public Object evaluate (Object left, Object right)
{
if (all(NUMERICAL, left, right)) {
return NUMERICAL.apply(left) <= NUMERICAL.apply(right);
}
if (all(STRING, left, right) || all(DATE, left, right)) {
return compare(STRING, left, right) <= 0;
}
return new NoValue("Non-comparable operand to '<=': (" + left + ", " + right + ")");
}
}
@@ -0,0 +1,51 @@
//
// $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.operator;
import com.samskivert.depot.expression.SQLExpression;
/**
* The SQL 'like' operator.
*/
public class Like extends SQLOperator.BinaryOperator
{
public Like (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public Like (SQLExpression column, SQLExpression value)
{
super(column, value);
}
@Override // from SQLOperator.BinaryOperator
public String operator()
{
return " like ";
}
@Override // from SQLOperator.BinaryOperator
public Object evaluate (Object left, Object right)
{
return new NoValue("Like operator not implemented");
}
}
@@ -1,149 +0,0 @@
//
// $Id$
//
// Depot library - a Java relational persistence library
// Copyright (C) 2006-2008 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.operator;
import java.util.Collection;
import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.expression.SQLExpression;
import com.samskivert.depot.impl.ExpressionVisitor;
/**
* A convenient container for implementations of logical operators. Classes that value brevity
* feel otherwise will use Logic.And() and Logic.Not().
*/
public abstract class Logic
{
/**
* Represents a condition that is false iff all its subconditions are false.
*/
public static class Or extends SQLOperator.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;
}
}
/**
* Represents a condition that is true iff all its subconditions are true.
*/
public static class And extends SQLOperator.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;
}
}
/**
* Represents the truth negation of another conditon.
*/
public static class Not
implements SQLOperator
{
public Not (SQLExpression condition)
{
_condition = condition;
}
public SQLExpression getCondition ()
{
return _condition;
}
// from SQLExpression
public Object accept (ExpressionVisitor<?> builder)
{
return builder.visit(this);
}
// from SQLExpression
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet)
{
_condition.addClasses(classSet);
}
@Override // from Object
public String toString ()
{
return "Not(" + _condition + ")";
}
protected SQLExpression _condition;
}
}
@@ -0,0 +1,59 @@
//
// $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.operator;
import com.samskivert.depot.expression.SQLExpression;
/**
* The SQL '*' operator.
*/
public class Mul extends Arithmetic
{
public Mul (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public Mul (SQLExpression... values)
{
super(values);
}
@Override // from Arithmetic
public String operator()
{
return "*";
}
@Override // from Arithmetic
public Object evaluate (Object[] operands)
{
return evaluate(operands, "*", new Accumulator<Double>() {
public Double accumulate (Double left, Double right) {
return left * right;
}
}, new Accumulator<Long>() {
public Long accumulate (Long left, Long right) {
return left * right;
}
});
}
}
@@ -0,0 +1,64 @@
//
// $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.operator;
import java.util.Collection;
import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.expression.SQLExpression;
import com.samskivert.depot.impl.ExpressionVisitor;
/**
* Represents the truth negation of another conditon.
*/
public class Not
implements SQLOperator
{
public Not (SQLExpression condition)
{
_condition = condition;
}
public SQLExpression getCondition ()
{
return _condition;
}
// from SQLExpression
public Object accept (ExpressionVisitor<?> builder)
{
return builder.visit(this);
}
// from SQLExpression
public void addClasses (Collection<Class<? extends PersistentRecord>> classSet)
{
_condition.addClasses(classSet);
}
@Override // from Object
public String toString ()
{
return "Not(" + _condition + ")";
}
protected SQLExpression _condition;
}
@@ -0,0 +1,54 @@
//
// $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.operator;
import com.samskivert.depot.expression.SQLExpression;
/**
* The SQL '!=' operator.
*/
public class NotEquals extends SQLOperator.BinaryOperator
{
public NotEquals (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public NotEquals (SQLExpression column, SQLExpression value)
{
super(column, value);
}
@Override // from SQLOperator.BinaryOperator
public String operator()
{
return "!=";
}
@Override // from SQLOperator.BinaryOperator
public Object evaluate (Object left, Object right)
{
if (left == null || right == null) {
return new NoValue("Null operand to '!=': (" + left + ", " + right + ")");
}
return !left.equals(right);
}
}
@@ -0,0 +1,63 @@
//
// $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.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 SQLOperator.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;
}
}
@@ -0,0 +1,59 @@
//
// $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.operator;
import com.samskivert.depot.expression.SQLExpression;
/**
* The SQL '-' operator.
*/
public class Sub extends Arithmetic
{
public Sub (SQLExpression column, Comparable<?> value)
{
super(column, value);
}
public Sub (SQLExpression... values)
{
super(values);
}
@Override // from Arithmetic
public String operator()
{
return "-";
}
@Override // from Arithmetic
public Object evaluate (Object[] operands)
{
return evaluate(operands, "-", new Accumulator<Double>() {
public Double accumulate (Double left, Double right) {
return left - right;
}
}, new Accumulator<Long>() {
public Long accumulate (Long left, Long right) {
return left - right;
}
});
}
}
@@ -39,7 +39,8 @@ import com.samskivert.depot.SchemaMigration;
import com.samskivert.depot.annotation.Computed;
import com.samskivert.depot.clause.Where;
import com.samskivert.depot.expression.LiteralExp;
import com.samskivert.depot.operator.Conditionals;
import com.samskivert.depot.operator.GreaterThan;
import com.samskivert.depot.operator.LessThan;
/**
* A test tool for the Depot repository services.
@@ -120,7 +121,7 @@ public class TestRepository extends DepotRepository
System.out.println("Delete none " + repo.deleteAll(TestRecord.class, none) + ".");
// test collection caching
Where where = new Where(new Conditionals.GreaterThan(TestRecord.RECORD_ID, 100));
Where where = new Where(new GreaterThan(TestRecord.RECORD_ID, 100));
System.out.println("100 and up: " + repo.findAll(TestRecord.class, where).size());
System.out.println("100 and up again: " + repo.findAll(TestRecord.class, where).size());
@@ -131,13 +132,13 @@ public class TestRepository extends DepotRepository
System.out.println("Names " + repo.findAll(TestNameRecord.class) + ".");
System.out.println("Have " + repo.findAll(TestRecord.class).size() + " records.");
repo.deleteAll(TestRecord.class, new Where(new Conditionals.LessThan(
repo.deleteAll(TestRecord.class, new Where(new LessThan(
TestRecord.RECORD_ID, CREATE_RECORDS/2)));
System.out.println("Now have " + repo.findAll(TestRecord.class).size() + " records.");
repo.deleteAll(TestRecord.class, new Where(new LiteralExp("true")));
// // TODO: try to break our In() clause
// Set<Integer> ids = Sets.newHashSet();
// for (int ii = 1; ii <= Conditionals.In.MAX_KEYS*2+3; ii++) {
// for (int ii = 1; ii <= In.MAX_KEYS*2+3; ii++) {
// ids.add(ii);
// }
// repo.deleteAll(TestRecord.class, KeySet.newSimpleKeySet(TestRecord.class, ids));