From 9c68929213e0995bcf533c1111cd733ffd472bb9 Mon Sep 17 00:00:00 2001 From: Par Winzell Date: Wed, 9 Sep 2009 19:51:33 +0000 Subject: [PATCH] It turns out PostgreSQL supports LN(exp) and LOG(exp) in double precision, but not LOG(base, exp). So we're adding an explicit LOG10. I wonder how useful the two-argument one is... maybe we should delete it. --- src/java/com/samskivert/depot/Funs.java | 13 ++++++++----- .../com/samskivert/depot/function/NumericalFun.java | 9 +++++++++ .../com/samskivert/depot/impl/BuildVisitor.java | 6 ++++++ .../samskivert/depot/impl/ExpressionVisitor.java | 2 ++ .../samskivert/depot/impl/PostgreSQLBuilder.java | 3 ++- 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/java/com/samskivert/depot/Funs.java b/src/java/com/samskivert/depot/Funs.java index 9677d7d..d27dbc8 100644 --- a/src/java/com/samskivert/depot/Funs.java +++ b/src/java/com/samskivert/depot/Funs.java @@ -68,7 +68,8 @@ public class Funs } /** - * Creates an expression that computes the natural logarithm of the supplied expression. + * Creates an expression that computes the natural logarithm of the supplied expression, + * which may be double-precision. */ public static Ln ln (SQLExpression exp) { @@ -76,16 +77,18 @@ public class Funs } /** - * Creates an expression that computes the base-10 logarithm of the supplied expression. + * Creates an expression that computes the base-10 logarithm of the supplied expression, + * which may be double-precision. */ - public static LogN log10 (SQLExpression value) + public static Log10 log10 (SQLExpression value) { - return new LogN(Exps.value(10), value); + return new Log10(value); } /** * Creates an expression that computes the logarithm of the supplied value expression - * in the supplied base expression. + * in the supplied base expression. Note: Under PostgreSQL, this must evaluate to a simple + * numerical, not double precision. */ public static LogN logN (SQLExpression base, SQLExpression value) { diff --git a/src/java/com/samskivert/depot/function/NumericalFun.java b/src/java/com/samskivert/depot/function/NumericalFun.java index 1cd2179..e3c9540 100644 --- a/src/java/com/samskivert/depot/function/NumericalFun.java +++ b/src/java/com/samskivert/depot/function/NumericalFun.java @@ -70,6 +70,15 @@ public abstract class NumericalFun } } + public static class Log10 extends OneArgFun { + public Log10 (SQLExpression value) { + super(value); + } + public Object accept (ExpressionVisitor visitor) { + return visitor.visit(this); + } + } + public static class LogN extends TwoArgFun { public LogN (SQLExpression base, SQLExpression value) { super(base, value); diff --git a/src/java/com/samskivert/depot/impl/BuildVisitor.java b/src/java/com/samskivert/depot/impl/BuildVisitor.java index d770c15..a20753e 100644 --- a/src/java/com/samskivert/depot/impl/BuildVisitor.java +++ b/src/java/com/samskivert/depot/impl/BuildVisitor.java @@ -68,6 +68,7 @@ import com.samskivert.depot.function.NumericalFun.Ceil; import com.samskivert.depot.function.NumericalFun.Exp; import com.samskivert.depot.function.NumericalFun.Floor; import com.samskivert.depot.function.NumericalFun.Ln; +import com.samskivert.depot.function.NumericalFun.Log10; import com.samskivert.depot.function.NumericalFun.LogN; import com.samskivert.depot.function.NumericalFun.Pi; import com.samskivert.depot.function.NumericalFun.Power; @@ -617,6 +618,11 @@ public abstract class BuildVisitor implements ExpressionVisitor return appendFunctionCall("ln", exp.getArg()); } + public Void visit (Log10 exp) + { + return appendFunctionCall("log", exp.getArg()); + } + public Void visit (LogN exp) { return appendFunctionCall("log", exp.getBase(), exp.getValue()); diff --git a/src/java/com/samskivert/depot/impl/ExpressionVisitor.java b/src/java/com/samskivert/depot/impl/ExpressionVisitor.java index dcd9b19..98da311 100644 --- a/src/java/com/samskivert/depot/impl/ExpressionVisitor.java +++ b/src/java/com/samskivert/depot/impl/ExpressionVisitor.java @@ -51,6 +51,7 @@ import com.samskivert.depot.function.NumericalFun.Ceil; import com.samskivert.depot.function.NumericalFun.Exp; import com.samskivert.depot.function.NumericalFun.Floor; import com.samskivert.depot.function.NumericalFun.Ln; +import com.samskivert.depot.function.NumericalFun.Log10; import com.samskivert.depot.function.NumericalFun.LogN; import com.samskivert.depot.function.NumericalFun.Pi; import com.samskivert.depot.function.NumericalFun.Power; @@ -123,6 +124,7 @@ public interface ExpressionVisitor public T visit (Exp exp); public T visit (Floor exp); public T visit (Ln exp); + public T visit (Log10 exp); public T visit (LogN exp); public T visit (Pi exp); public T visit (Power exp); diff --git a/src/java/com/samskivert/depot/impl/PostgreSQLBuilder.java b/src/java/com/samskivert/depot/impl/PostgreSQLBuilder.java index a13f0f6..218bf28 100644 --- a/src/java/com/samskivert/depot/impl/PostgreSQLBuilder.java +++ b/src/java/com/samskivert/depot/impl/PostgreSQLBuilder.java @@ -86,7 +86,8 @@ public class PostgreSQLBuilder return null; } - @Override public Void visit (DatePart exp) { + @Override public Void visit (DatePart exp) + { return appendFunctionCall( "date_part", Exps.value(translateDatePart(exp.getPart())), exp.getArg()); }