*
* @author James Ahlborn
*/
-public class TemporalConfig
+public class TemporalConfig
{
public static final String US_DATE_FORMAT = "M/d/yyyy";
- public static final String US_TIME_FORMAT_12 = "hh:mm:ss a";
- public static final String US_TIME_FORMAT_24 = "HH:mm:ss";
+ public static final String US_TIME_FORMAT_12 = "h:mm:ss a";
+ public static final String US_TIME_FORMAT_24 = "H:mm:ss";
public static final TemporalConfig US_TEMPORAL_CONFIG = new TemporalConfig(
US_DATE_FORMAT, US_TIME_FORMAT_12, US_TIME_FORMAT_24, '/', ':');
private final String _dateTimeFormat12;
private final String _dateTimeFormat24;
- public TemporalConfig(String dateFormat, String timeFormat12,
- String timeFormat24, char dateSeparator,
+ public TemporalConfig(String dateFormat, String timeFormat12,
+ String timeFormat24, char dateSeparator,
char timeSeparator)
{
_dateFormat = dateFormat;
package com.healthmarketscience.jackcess;
import java.util.List;
-
+import javax.script.Bindings;
+import javax.script.SimpleBindings;
+
+import com.healthmarketscience.jackcess.expr.EvalConfig;
+import com.healthmarketscience.jackcess.expr.EvalContext;
+import com.healthmarketscience.jackcess.expr.Function;
+import com.healthmarketscience.jackcess.expr.FunctionLookup;
+import com.healthmarketscience.jackcess.expr.TemporalConfig;
+import com.healthmarketscience.jackcess.expr.Value;
+import com.healthmarketscience.jackcess.impl.expr.BuiltinOperators;
+import com.healthmarketscience.jackcess.impl.expr.DefaultFunctions;
import junit.framework.TestCase;
import static com.healthmarketscience.jackcess.Database.*;
}
}
+ public static void testCustomEvalConfig() throws Exception
+ {
+ TemporalConfig tempConf = new TemporalConfig("yyyy/M/d",
+ "hh.mm.ss a",
+ "HH.mm.ss", '/', '.');
+
+ FunctionLookup lookup = new FunctionLookup() {
+ public Function getFunction(String name) {
+ if("FooFunc".equalsIgnoreCase(name)) {
+ return FOO;
+ }
+ return DefaultFunctions.LOOKUP.getFunction(name);
+ }
+ };
+
+ Bindings bindings = new SimpleBindings();
+ bindings.put("someKey", "someVal");
+
+ for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
+ Database db = create(fileFormat);
+
+ EvalConfig ec = db.getEvalConfig();
+ ec.setTemporalConfig(tempConf);
+ ec.setFunctionLookup(lookup);
+ ec.setBindings(bindings);
+
+ db.setEvaluateExpressions(true);
+
+ Table t = new TableBuilder("test")
+ .addColumn(new ColumnBuilder("id", DataType.LONG).setAutoNumber(true))
+ .addColumn(new ColumnBuilder("data1", DataType.TEXT)
+ .putProperty(PropertyMap.DEFAULT_VALUE_PROP,
+ "=FooFunc()"))
+ .addColumn(new ColumnBuilder("data2", DataType.TEXT)
+ .putProperty(PropertyMap.DEFAULT_VALUE_PROP,
+ "=Date()"))
+ .addColumn(new ColumnBuilder("data3", DataType.TEXT)
+ .putProperty(PropertyMap.DEFAULT_VALUE_PROP,
+ "=Time()"))
+ .toTable(db);
+
+ t.addRow(Column.AUTO_NUMBER, null, null);
+
+ Row row = t.iterator().next();
+
+ assertEquals(1, row.get("id"));
+ assertEquals("FOO_someVal", row.get("data1"));
+ assertTrue(((String)row.get("data2"))
+ .matches("\\d{4}/\\d{1,2}/\\d{1,2}"));
+ assertTrue(((String)row.get("data3"))
+ .matches("\\d{2}.\\d{2}.\\d{2} (AM|PM)"));
+
+ db.close();
+ }
+ }
+
private static void setProp(Table t, String colName, String propName,
String propVal) throws Exception {
PropertyMap props = t.getColumn(colName).getProperties();
}
props.save();
}
+
+ private static final Function FOO = new DefaultFunctions.Func0("FooFunc") {
+ @Override
+ public boolean isPure() { return false; }
+ @Override
+ protected Value eval0(EvalContext ctx) {
+ Object val = ctx.get("someKey");
+ return BuiltinOperators.toValue("FOO_" + val);
+ }
+ };
}
public void testDateFuncs() throws Exception
{
assertEquals("1/2/2003", eval("=CStr(DateValue(#01/02/2003 7:00:00 AM#))"));
- assertEquals("07:00:00 AM", eval("=CStr(TimeValue(#01/02/2003 7:00:00 AM#))"));
+ assertEquals("7:00:00 AM", eval("=CStr(TimeValue(#01/02/2003 7:00:00 AM#))"));
assertEquals(2003, eval("=Year(#01/02/2003 7:00:00 AM#)"));
assertEquals(1, eval("=Month(#01/02/2003 7:00:00 AM#)"));
assertTrue(((String)eval("=CStr(Date())"))
.matches("\\d{1,2}/\\d{1,2}/\\d{4}"));
assertTrue(((String)eval("=CStr(Time())"))
- .matches("\\d{1,2}:\\d{1,2}:\\d{2} (AM|PM)"));
+ .matches("\\d{1,2}:\\d{2}:\\d{2} (AM|PM)"));
+
+ assertEquals("3:57:34 AM", eval("=CStr(TimeSerial(3,57,34))"));
+ assertEquals("3:57:34 PM", eval("=CStr(TimeSerial(15,57,34))"));
}
public void testFinancialFuncs() throws Exception