Browse Source

beginnings of general format support

git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1224 f203690c-595d-4dc9-a70b-905162fa7fd2
tags/jackcess-2.2.1
James Ahlborn 5 years ago
parent
commit
dad3be567a

+ 2
- 2
src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultDateFunctions.java View 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

+ 20
- 0
src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultTextFunctions.java View 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);

+ 107
- 0
src/main/java/com/healthmarketscience/jackcess/impl/expr/FormatUtil.java View File

@@ -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);
}
}

}

Loading…
Cancel
Save