Added support for intervals which would almost be exactly the same except that

Postgres insists that you quote: interval '7 day' and MySQL insists that you
not quote: interval 7 day. Frawesome!
This commit is contained in:
Michael Bayne
2009-05-28 22:53:30 +00:00
parent 965cd7fd60
commit bde87ac0e6
5 changed files with 89 additions and 2 deletions
@@ -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<Class<? extends PersistentRecord>> classSet)
{
}
@Override
public String toString ()
{
return amount + " " + unit;
}
}
@@ -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<Void>
return null;
}
public Void visit (IntervalExp interval)
{
_builder.append("interval ").append(interval.amount).append(" ").append(interval.unit);
return null;
}
public Void visit (Exists<? extends PersistentRecord> exists)
{
_builder.append("exists ");
@@ -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);
@@ -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<T>
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<? extends PersistentRecord> key);
public T visit (Exists<? extends PersistentRecord> exists);
@@ -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('").