]> source.dussan.org Git - jackcess.git/commitdiff
beginnings of general format support
authorJames Ahlborn <jtahlborn@yahoo.com>
Tue, 13 Nov 2018 23:34:37 +0000 (23:34 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Tue, 13 Nov 2018 23:34:37 +0000 (23:34 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1224 f203690c-595d-4dc9-a70b-905162fa7fd2

src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultDateFunctions.java
src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultTextFunctions.java
src/main/java/com/healthmarketscience/jackcess/impl/expr/FormatUtil.java [new file with mode: 0644]

index cbc2f074e70a54ea5aab93850a854ff17a3baa55..a19ab0afa37ad9cd03b23fcebb328c97de71246c 100644 (file)
@@ -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
index f8c4a880311b56974eb8fe75a34bc5b024369092..564376659229a3e64d0e2702f2b3f72b30b480a0 100644 (file)
@@ -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 (file)
index 0000000..730961d
--- /dev/null
@@ -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);
+    }
+  }
+
+  
+}