diff --git a/src/java/com/samskivert/jdbc/depot/BindVisitor.java b/src/java/com/samskivert/jdbc/depot/BindVisitor.java index 1274c07..eab550a 100644 --- a/src/java/com/samskivert/jdbc/depot/BindVisitor.java +++ b/src/java/com/samskivert/jdbc/depot/BindVisitor.java @@ -38,6 +38,7 @@ import com.samskivert.jdbc.depot.clause.SelectClause; import com.samskivert.jdbc.depot.clause.UpdateClause; import com.samskivert.jdbc.depot.clause.Where; import com.samskivert.jdbc.depot.expression.ColumnExp; +import com.samskivert.jdbc.depot.expression.EpochSeconds; import com.samskivert.jdbc.depot.expression.ExpressionVisitor; import com.samskivert.jdbc.depot.expression.FunctionExp; import com.samskivert.jdbc.depot.expression.LiteralExp; @@ -105,6 +106,12 @@ public class BindVisitor implements ExpressionVisitor visit(functionExp.getArguments()); } + public void visit (EpochSeconds epochSeconds) + throws Exception + { + epochSeconds.getArgument().accept(this); + } + public void visit (MultiOperator multiOperator) throws Exception { diff --git a/src/java/com/samskivert/jdbc/depot/MySQLBuilder.java b/src/java/com/samskivert/jdbc/depot/MySQLBuilder.java index 59887b8..89abd5e 100644 --- a/src/java/com/samskivert/jdbc/depot/MySQLBuilder.java +++ b/src/java/com/samskivert/jdbc/depot/MySQLBuilder.java @@ -47,6 +47,7 @@ import com.samskivert.jdbc.depot.FieldMarshaller.ShortMarshaller; import com.samskivert.jdbc.depot.annotation.FullTextIndex; import com.samskivert.jdbc.depot.clause.DeleteClause; import com.samskivert.jdbc.depot.expression.ColumnExp; +import com.samskivert.jdbc.depot.expression.EpochSeconds; import com.samskivert.jdbc.depot.operator.Conditionals.FullTextMatch; public class MySQLBuilder @@ -89,6 +90,14 @@ public class MySQLBuilder } } + public void visit (EpochSeconds epochSeconds) + throws Exception + { + _builder.append("unix_timestamp("); + epochSeconds.getArgument().accept(this); + _builder.append(")"); + } + protected MSBuildVisitor (DepotTypes types) { super(types); diff --git a/src/java/com/samskivert/jdbc/depot/PostgreSQLBuilder.java b/src/java/com/samskivert/jdbc/depot/PostgreSQLBuilder.java index c14218f..1c20476 100644 --- a/src/java/com/samskivert/jdbc/depot/PostgreSQLBuilder.java +++ b/src/java/com/samskivert/jdbc/depot/PostgreSQLBuilder.java @@ -46,6 +46,7 @@ import com.samskivert.jdbc.depot.FieldMarshaller.LongMarshaller; import com.samskivert.jdbc.depot.FieldMarshaller.ObjectMarshaller; import com.samskivert.jdbc.depot.FieldMarshaller.ShortMarshaller; import com.samskivert.jdbc.depot.annotation.FullTextIndex; +import com.samskivert.jdbc.depot.expression.EpochSeconds; import com.samskivert.jdbc.depot.operator.Conditionals.FullTextMatch; import com.samskivert.util.ArrayUtil; import com.samskivert.util.StringUtil; @@ -65,6 +66,14 @@ public class PostgreSQLBuilder _builder.append(" @@ TO_TSQUERY('default', ?)"); } + public void visit (EpochSeconds epochSeconds) + throws Exception + { + _builder.append("date_part('epoch', "); + epochSeconds.getArgument().accept(this); + _builder.append(")"); + } + protected PGBuildVisitor (DepotTypes types) { super(types); diff --git a/src/java/com/samskivert/jdbc/depot/expression/EpochSeconds.java b/src/java/com/samskivert/jdbc/depot/expression/EpochSeconds.java new file mode 100644 index 0000000..e9d164b --- /dev/null +++ b/src/java/com/samskivert/jdbc/depot/expression/EpochSeconds.java @@ -0,0 +1,60 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2006-2007 Michael Bayne, 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.jdbc.depot.expression; + +import java.util.Collection; + +import com.samskivert.jdbc.depot.PersistentRecord; + +/** + * An expression for extracting the seconds since the epoch from a date expression. + */ +public class EpochSeconds implements SQLExpression +{ + /** + * Create a new EpochSeconds with the given argument. + */ + public EpochSeconds (SQLExpression arg) + { + _arg = arg; + } + + // from SQLExpression + public void accept (ExpressionVisitor builder) + throws Exception + { + builder.visit(this); + } + + // from SQLExpression + public void addClasses (Collection> classSet) + { + _arg.addClasses(classSet); + } + + public SQLExpression getArgument () + { + return _arg; + } + + /** The argument. */ + protected SQLExpression _arg; +} diff --git a/src/java/com/samskivert/jdbc/depot/expression/ExpressionVisitor.java b/src/java/com/samskivert/jdbc/depot/expression/ExpressionVisitor.java index 3245ea0..9fa59fb 100644 --- a/src/java/com/samskivert/jdbc/depot/expression/ExpressionVisitor.java +++ b/src/java/com/samskivert/jdbc/depot/expression/ExpressionVisitor.java @@ -61,6 +61,8 @@ public interface ExpressionVisitor throws Exception; public void visit (FunctionExp functionExp) throws Exception; + public void visit (EpochSeconds epochSeconds) + throws Exception; public void visit (FromOverride fromOverride) throws Exception; public void visit (MultiOperator multiOperator)