More import relief: add builder classes Ops and Exps which have methods for

creating common operators and expressions that don't fit nicely into the fluent
style. For example: Ops.and(expr1, expr2) is preferable to expr1.and(expr2)
and Ops.not(expr) is definitely nicer than expr.not() (which we don't even
provide).
This commit is contained in:
Michael Bayne
2009-06-10 20:58:12 +00:00
parent 03ac710d4f
commit f57795c48f
3 changed files with 167 additions and 48 deletions
+107
View File
@@ -0,0 +1,107 @@
//
// $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;
import com.samskivert.depot.expression.EpochSeconds;
import com.samskivert.depot.expression.IntervalExp;
import com.samskivert.depot.expression.LiteralExp;
import com.samskivert.depot.expression.SQLExpression;
import com.samskivert.depot.expression.ValueExp;
/**
* Provides static methods for expression construction. For example: {@link #literal}, {@link
* #value} and {@link #years}.
*/
public class Exps
{
/**
* Wraps the supplied object in a {@link ValueExp}.
*/
public ValueExp value (Object value)
{
return new ValueExp(value);
}
/**
* Creates a {@link LiteralExp} with the supplied SQL snippet. Note: you're probably breaking
* cross platform compatibility by using this construction.
*/
public LiteralExp literal (String text)
{
return new LiteralExp(text);
}
/**
* Creates an interval for the specified number of years.
*/
public static IntervalExp years (int amount)
{
return new IntervalExp(IntervalExp.Unit.YEAR, amount);
}
/**
* Creates an interval for the specified number of months.
*/
public static IntervalExp months (int amount)
{
return new IntervalExp(IntervalExp.Unit.MONTH, amount);
}
/**
* Creates an interval for the specified number of days.
*/
public static IntervalExp days (int amount)
{
return new IntervalExp(IntervalExp.Unit.DAY, amount);
}
/**
* Creates an interval for the specified number of hours.
*/
public static IntervalExp hours (int amount)
{
return new IntervalExp(IntervalExp.Unit.HOUR, amount);
}
/**
* Creates an interval for the specified number of minutes.
*/
public static IntervalExp minutes (int amount)
{
return new IntervalExp(IntervalExp.Unit.MINUTE, amount);
}
/**
* Creates an interval for the specified number of seconds.
*/
public static IntervalExp seconds (int amount)
{
return new IntervalExp(IntervalExp.Unit.SECOND, amount);
}
/**
* Creates an expression that converts the supplied expression into seconds since the epoch.
*/
public static EpochSeconds epochSeconds (SQLExpression expr)
{
return new EpochSeconds(expr);
}
}
+60
View File
@@ -0,0 +1,60 @@
//
// $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;
import java.util.Collection;
import com.samskivert.depot.expression.SQLExpression;
import com.samskivert.depot.operator.And;
import com.samskivert.depot.operator.Not;
import com.samskivert.depot.operator.Or;
/**
* Provides static methods for operator construction that don't fit nicely into the fluent style.
* For example: Ops.and(), Ops.or() and Ops.not().
*/
public class Ops
{
public static Not not (SQLExpression expr)
{
return new Not(expr);
}
public static And and (Collection<? extends SQLExpression> conditions)
{
return new And(conditions);
}
public static And and (SQLExpression... conditions)
{
return new And(conditions);
}
public static Or or (Collection<? extends SQLExpression> conditions)
{
return new Or(conditions);
}
public static Or or (SQLExpression... conditions)
{
return new Or(conditions);
}
}
@@ -40,54 +40,6 @@ public class IntervalExp
/** The number of units for this interval. */
public final int amount;
/**
* Creates an interval for the specified number of years.
*/
public static IntervalExp years (int amount)
{
return new IntervalExp(Unit.YEAR, amount);
}
/**
* Creates an interval for the specified number of months.
*/
public static IntervalExp months (int amount)
{
return new IntervalExp(Unit.MONTH, amount);
}
/**
* Creates an interval for the specified number of days.
*/
public static IntervalExp days (int amount)
{
return new IntervalExp(Unit.DAY, amount);
}
/**
* Creates an interval for the specified number of hours.
*/
public static IntervalExp hours (int amount)
{
return new IntervalExp(Unit.HOUR, amount);
}
/**
* Creates an interval for the specified number of minutes.
*/
public static IntervalExp minutes (int amount)
{
return new IntervalExp(Unit.MINUTE, amount);
}
/**
* Creates an interval for the specified number of seconds.
*/
public static IntervalExp seconds (int amount)
{
return new IntervalExp(Unit.SECOND, amount);
}
public IntervalExp (Unit unit, int amount)
{
this.unit = unit;