diff --git a/src/java/com/samskivert/depot/expression/IntervalExp.java b/src/java/com/samskivert/depot/expression/IntervalExp.java new file mode 100644 index 0000000..85820ae --- /dev/null +++ b/src/java/com/samskivert/depot/expression/IntervalExp.java @@ -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.expression; + +import java.util.Collection; + +import com.samskivert.depot.PersistentRecord; +import com.samskivert.depot.impl.ExpressionVisitor; + +/** + * A code for representing a date interval. + */ +public class IntervalExp + implements SQLExpression +{ + /** The units that can be used for an interval. */ + public enum Unit { YEAR, MONTH, DAY, HOUR, MINUTE, SECOND }; + + /** The unit for this interval. */ + public final Unit unit; + + /** The number of units for this interval. */ + public final int amount; + + public IntervalExp (Unit unit, int amount) + { + this.unit = unit; + this.amount = amount; + } + + // from SQLExpression + public Object accept (ExpressionVisitor builder) + { + return builder.visit(this); + } + + // from SQLExpression + public void addClasses (Collection> classSet) + { + } + + @Override + public String toString () + { + return amount + " " + unit; + } +} diff --git a/src/java/com/samskivert/depot/impl/BuildVisitor.java b/src/java/com/samskivert/depot/impl/BuildVisitor.java index 433fb3a..34d6cec 100644 --- a/src/java/com/samskivert/depot/impl/BuildVisitor.java +++ b/src/java/com/samskivert/depot/impl/BuildVisitor.java @@ -51,6 +51,7 @@ import com.samskivert.depot.clause.WhereClause; import com.samskivert.depot.expression.ColumnExp; import com.samskivert.depot.expression.EpochSeconds; import com.samskivert.depot.expression.FunctionExp; +import com.samskivert.depot.expression.IntervalExp; import com.samskivert.depot.expression.LiteralExp; import com.samskivert.depot.expression.SQLExpression; import com.samskivert.depot.expression.ValueExp; @@ -320,6 +321,12 @@ public abstract class BuildVisitor implements ExpressionVisitor return null; } + public Void visit (IntervalExp interval) + { + _builder.append("interval ").append(interval.amount).append(" ").append(interval.unit); + return null; + } + public Void visit (Exists exists) { _builder.append("exists "); diff --git a/src/java/com/samskivert/depot/impl/ExpressionEvaluator.java b/src/java/com/samskivert/depot/impl/ExpressionEvaluator.java index 7df0ae7..d735255 100644 --- a/src/java/com/samskivert/depot/impl/ExpressionEvaluator.java +++ b/src/java/com/samskivert/depot/impl/ExpressionEvaluator.java @@ -40,6 +40,7 @@ import com.samskivert.depot.clause.WhereClause; import com.samskivert.depot.expression.ColumnExp; import com.samskivert.depot.expression.EpochSeconds; import com.samskivert.depot.expression.FunctionExp; +import com.samskivert.depot.expression.IntervalExp; import com.samskivert.depot.expression.LiteralExp; import com.samskivert.depot.expression.SQLExpression; import com.samskivert.depot.expression.SQLExpression.NoValue; @@ -214,6 +215,11 @@ public class ExpressionEvaluator return valueExp.getValue(); } + public Object visit (IntervalExp interval) + { + return new NoValue("Cannot evaluate IntervalExp: " + interval); + } + public Object visit (WhereClause where) { Object result = where.getWhereExpression().accept(this); diff --git a/src/java/com/samskivert/depot/impl/ExpressionVisitor.java b/src/java/com/samskivert/depot/impl/ExpressionVisitor.java index 9b38e76..d82d81a 100644 --- a/src/java/com/samskivert/depot/impl/ExpressionVisitor.java +++ b/src/java/com/samskivert/depot/impl/ExpressionVisitor.java @@ -37,6 +37,7 @@ import com.samskivert.depot.clause.WhereClause; import com.samskivert.depot.expression.ColumnExp; import com.samskivert.depot.expression.EpochSeconds; import com.samskivert.depot.expression.FunctionExp; +import com.samskivert.depot.expression.IntervalExp; import com.samskivert.depot.expression.LiteralExp; import com.samskivert.depot.expression.ValueExp; @@ -76,8 +77,9 @@ public interface ExpressionVisitor public T visit (OrderBy orderBy); public T visit (Join join); public T visit (Limit limit); - public T visit (LiteralExp literalExp); - public T visit (ValueExp valueExp); + public T visit (LiteralExp literal); + public T visit (ValueExp value); + public T visit (IntervalExp interval); public T visit (WhereClause where); public T visit (Key.Expression key); public T visit (Exists exists); diff --git a/src/java/com/samskivert/depot/impl/PostgreSQLBuilder.java b/src/java/com/samskivert/depot/impl/PostgreSQLBuilder.java index 776e401..2574d86 100644 --- a/src/java/com/samskivert/depot/impl/PostgreSQLBuilder.java +++ b/src/java/com/samskivert/depot/impl/PostgreSQLBuilder.java @@ -40,6 +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.expression.IntervalExp; import com.samskivert.depot.operator.FullText; import static com.samskivert.Log.log; @@ -53,6 +54,12 @@ public class PostgreSQLBuilder public class PGBuildVisitor extends BuildVisitor { + @Override public Void visit (IntervalExp interval) { + _builder.append("interval '").append(interval.amount); + _builder.append(" ").append(interval.unit).append("'"); + return null; + } + @Override public Void visit (FullText.Match match) { appendIdentifier("ftsCol_" + match.getDefinition().getName()); _builder.append(" @@ to_tsquery('").