From 4a5f749ee5702e5408687013595d03bb9f13eb5e Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Thu, 26 Jan 2017 02:11:33 +0000 Subject: [PATCH] add date time Value accessor git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/exprs@1081 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../jackcess/expr/EvalContext.java | 4 ++-- .../jackcess/expr/Value.java | 2 +- .../jackcess/impl/expr/BaseDateValue.java | 6 ++++++ .../jackcess/impl/expr/BaseDelayedValue.java | 5 +++-- .../jackcess/impl/expr/BaseNumericValue.java | 14 +++++++++++++ .../jackcess/impl/expr/BaseValue.java | 3 ++- .../jackcess/impl/expr/BuiltinOperators.java | 4 +++- .../jackcess/impl/expr/Expressionator.java | 21 ++++++++++++++++--- 8 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/healthmarketscience/jackcess/expr/EvalContext.java b/src/main/java/com/healthmarketscience/jackcess/expr/EvalContext.java index 01a27b0..a4e21d1 100644 --- a/src/main/java/com/healthmarketscience/jackcess/expr/EvalContext.java +++ b/src/main/java/com/healthmarketscience/jackcess/expr/EvalContext.java @@ -24,10 +24,10 @@ import java.text.SimpleDateFormat; */ public interface EvalContext { - public TemporalConfig getTemporalConfig(); - public Value.Type getResultType(); + public TemporalConfig getTemporalConfig(); + public SimpleDateFormat createDateFormat(String formatStr); public Value getThisColumnValue(); diff --git a/src/main/java/com/healthmarketscience/jackcess/expr/Value.java b/src/main/java/com/healthmarketscience/jackcess/expr/Value.java index 2996772..04844ae 100644 --- a/src/main/java/com/healthmarketscience/jackcess/expr/Value.java +++ b/src/main/java/com/healthmarketscience/jackcess/expr/Value.java @@ -62,7 +62,7 @@ public interface Value public String getAsString(); - public Date getAsDateTime(); + public Date getAsDateTime(EvalContext ctx); public Long getAsLong(); diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseDateValue.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseDateValue.java index 1c96ba8..4bf03af 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseDateValue.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseDateValue.java @@ -21,6 +21,7 @@ import java.text.DateFormat; import java.util.Date; import com.healthmarketscience.jackcess.impl.ColumnImpl; +import com.healthmarketscience.jackcess.expr.EvalContext; /** * @@ -60,6 +61,11 @@ public abstract class BaseDateValue extends BaseValue return _fmt.format(_val); } + @Override + public Date getAsDateTime(EvalContext ctx) { + return _val; + } + @Override public Long getAsLong() { return getNumber().longValue(); diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseDelayedValue.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseDelayedValue.java index 5ee037c..c34a914 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseDelayedValue.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseDelayedValue.java @@ -19,6 +19,7 @@ package com.healthmarketscience.jackcess.impl.expr; import java.math.BigDecimal; import java.util.Date; +import com.healthmarketscience.jackcess.expr.EvalContext; import com.healthmarketscience.jackcess.expr.Value; /** @@ -59,8 +60,8 @@ public abstract class BaseDelayedValue implements Value return getDelegate().getAsString(); } - public Date getAsDateTime() { - return getDelegate().getAsDateTime(); + public Date getAsDateTime(EvalContext ctx) { + return getDelegate().getAsDateTime(ctx); } public Long getAsLong() { diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseNumericValue.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseNumericValue.java index fc136bf..585c71e 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseNumericValue.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseNumericValue.java @@ -16,6 +16,11 @@ limitations under the License. package com.healthmarketscience.jackcess.impl.expr; +import java.text.SimpleDateFormat; +import java.util.Date; + +import com.healthmarketscience.jackcess.expr.EvalContext; +import com.healthmarketscience.jackcess.impl.ColumnImpl; /** * @@ -43,5 +48,14 @@ public abstract class BaseNumericValue extends BaseValue return getNumber().doubleValue(); } + @Override + public Date getAsDateTime(EvalContext ctx) { + double d = getNumber().doubleValue(); + + SimpleDateFormat sdf = ctx.createDateFormat( + ctx.getTemporalConfig().getDefaultDateTimeFormat()); + return new Date(ColumnImpl.fromDateDouble(d, sdf.getCalendar())); + } + protected abstract Number getNumber(); } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseValue.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseValue.java index 109412b..e30c303 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseValue.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseValue.java @@ -20,6 +20,7 @@ import java.math.BigDecimal; import java.util.Date; import com.healthmarketscience.jackcess.expr.Value; +import com.healthmarketscience.jackcess.expr.EvalContext; /** * @@ -39,7 +40,7 @@ public abstract class BaseValue implements Value throw invalidConversion(Value.Type.STRING); } - public Date getAsDateTime() { + public Date getAsDateTime(EvalContext ctx) { throw invalidConversion(Value.Type.DATE_TIME); } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BuiltinOperators.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BuiltinOperators.java index 5bd6ac9..e456e8b 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BuiltinOperators.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BuiltinOperators.java @@ -154,6 +154,8 @@ public class BuiltinOperators return NULL_VAL; } + // FIXME, string will convert to number if one is number and one is a string parseable as a number!!! + Value.Type mathType = getGeneralMathTypePrecedence(param1, param2); switch(mathType) { @@ -696,7 +698,7 @@ public class BuiltinOperators return ((t1.compareTo(t2) > 0) ? t1 : t2); } - private static boolean isIntegral(double d) { + static boolean isIntegral(double d) { return ((d == Math.rint(d)) && !Double.isInfinite(d) && !Double.isNaN(d)); } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/Expressionator.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/Expressionator.java index d7e5190..f92ad8c 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/Expressionator.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/Expressionator.java @@ -1877,9 +1877,24 @@ public class Expressionator return null; } - // FIXME, booleans seem to go to -1 (true),0 (false) ...? - - return val.get(); + // FIXME possibly do some type coercion. are there conversions here which don't work elsewhere? (string -> date, string -> number)? + switch(ctx.getResultType()) { + case STRING: + return val.getAsString(); + case DATE: + case TIME: + case DATE_TIME: + return val.getAsDateTime(ctx); + case LONG: + return val.getAsLong(); + case DOUBLE: + return val.getAsDouble(); + case BIG_DEC: + return val.getAsBigDecimal(); + default: + throw new IllegalStateException("unexpected result type " + + ctx.getResultType()); + } } private Boolean evalCondition(EvalContext ctx) { -- 2.39.5