diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2018-11-13 23:34:37 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2018-11-13 23:34:37 +0000 |
commit | dad3be567a30fa6b0f9ad03a5c3a66180b820311 (patch) | |
tree | ecfa099de7122ca5fb69f838dc52b1f75f3d92ba /src/main | |
parent | ea8a75596cecfe3479b08847a163912a77382d7b (diff) | |
download | jackcess-dad3be567a30fa6b0f9ad03a5c3a66180b820311.tar.gz jackcess-dad3be567a30fa6b0f9ad03a5c3a66180b820311.zip |
beginnings of general format support
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1224 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'src/main')
3 files changed, 129 insertions, 2 deletions
diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultDateFunctions.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultDateFunctions.java index cbc2f07..a19ab0a 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultDateFunctions.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultDateFunctions.java @@ -537,14 +537,14 @@ public class DefaultDateFunctions return (((firstDay - 1) + (weekday - 1)) % 7) + 1; } - private static int getFirstDayParam( + static int getFirstDayParam( LocaleContext ctx, Value[] params, int idx) { // vbSunday (default) 1 // vbUseSystem 0 return getOptionalIntParam(ctx, params, idx, 1, 0); } - private static int getFirstWeekTypeParam( + static int getFirstWeekTypeParam( LocaleContext ctx, Value[] params, int idx) { // vbFirstJan1 (default) 1 // vbUseSystem 0 diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultTextFunctions.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultTextFunctions.java index f8c4a88..5643766 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultTextFunctions.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultTextFunctions.java @@ -425,7 +425,27 @@ public class DefaultTextFunctions } }); + public static final Function FORMAT = registerFunc(new FuncVar("Format", 1, 4) { + @Override + protected Value evalVar(EvalContext ctx, Value[] params) { + Value expr = params[0]; + if(params.length < 2) { + // no formatting, do simple string conversion + if(expr.isNull()) { + return ValueSupport.NULL_VAL; + } + return ValueSupport.toValue(expr.getAsString(ctx)); + } + + String fmtStr = params[1].getAsString(ctx); + int firstDay = DefaultDateFunctions.getFirstDayParam(ctx, params, 2); + int firstWeekType = DefaultDateFunctions.getFirstWeekTypeParam(ctx, params, 3); + + return FormatUtil.format(ctx, expr, fmtStr, firstDay, firstWeekType); + } + }); + private static String nchars(int num, char c) { StringBuilder sb = new StringBuilder(num); nchars(sb, num, c); diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/FormatUtil.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/FormatUtil.java new file mode 100644 index 0000000..730961d --- /dev/null +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/FormatUtil.java @@ -0,0 +1,107 @@ +/* +Copyright (c) 2018 James Ahlborn + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package com.healthmarketscience.jackcess.impl.expr; + +import java.text.DateFormat; +import java.util.HashMap; +import java.util.Map; + +import com.healthmarketscience.jackcess.expr.EvalContext; +import com.healthmarketscience.jackcess.expr.TemporalConfig; +import com.healthmarketscience.jackcess.expr.Value; + +/** + * + * @author James Ahlborn + */ +public class FormatUtil +{ + private static final Map<String,Fmt> PREDEF_FMTS = new HashMap<String,Fmt>(); + + static { + PREDEF_FMTS.put("General Date", + new PredefDateFmt(TemporalConfig.Type.GENERAL_DATE)); + PREDEF_FMTS.put("Long Date", + new PredefDateFmt(TemporalConfig.Type.LONG_DATE)); + PREDEF_FMTS.put("Medium Date", + new PredefDateFmt(TemporalConfig.Type.MEDIUM_DATE)); + PREDEF_FMTS.put("Short Date", + new PredefDateFmt(TemporalConfig.Type.SHORT_DATE)); + PREDEF_FMTS.put("Long Time", + new PredefDateFmt(TemporalConfig.Type.LONG_TIME)); + PREDEF_FMTS.put("Medium Time", + new PredefDateFmt(TemporalConfig.Type.MEDIUM_TIME)); + PREDEF_FMTS.put("Short Time", + new PredefDateFmt(TemporalConfig.Type.SHORT_TIME)); + + } + + private FormatUtil() {} + + + public static Value format(EvalContext ctx, Value expr, String fmtStr, + int firstDay, int firstWeekType) { + + + // FIXME, + throw new UnsupportedOperationException(); + } + + private static abstract class Fmt + { + // FIXME, no null + public abstract Value format(EvalContext ctx, Value expr, String fmtStr, + int firstDay, int firstWeekType); + } + + private static class PredefDateFmt extends Fmt + { + private final TemporalConfig.Type _type; + + private PredefDateFmt(TemporalConfig.Type type) { + _type = type; + } + + @Override + public Value format(EvalContext ctx, Value expr, String fmtStr, + int firstDay, int firstWeekType) { + DateFormat sdf = ctx.createDateFormat( + ctx.getTemporalConfig().getDateTimeFormat(_type)); + return ValueSupport.toValue(sdf.format(expr.getAsDateTime(ctx))); + } + } + + private static class PredefBoolFmt extends Fmt + { + private final Value _trueVal; + private final Value _falseVal; + + private PredefBoolFmt(String trueStr, String falseStr) { + _trueVal = ValueSupport.toValue(trueStr); + _falseVal = ValueSupport.toValue(falseStr); + } + + @Override + public Value format(EvalContext ctx, Value expr, String fmtStr, + int firstDay, int firstWeekType) { + // FIXME, handle null? + return(expr.getAsBoolean(ctx) ? _trueVal : _falseVal); + } + } + + +} |