1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.healthmarketscience.jackcess.impl;
18
19 import java.text.DecimalFormat;
20 import java.time.ZoneId;
21 import java.time.format.DateTimeFormatter;
22 import java.util.Map;
23 import javax.script.Bindings;
24 import javax.script.SimpleBindings;
25
26 import com.healthmarketscience.jackcess.expr.EvalConfig;
27 import com.healthmarketscience.jackcess.expr.FunctionLookup;
28 import com.healthmarketscience.jackcess.expr.NumericConfig;
29 import com.healthmarketscience.jackcess.expr.TemporalConfig;
30 import com.healthmarketscience.jackcess.impl.expr.DefaultFunctions;
31 import com.healthmarketscience.jackcess.impl.expr.Expressionator;
32 import com.healthmarketscience.jackcess.impl.expr.NumberFormatter;
33 import com.healthmarketscience.jackcess.impl.expr.RandomContext;
34
35
36
37
38
39 public class DBEvalContext implements Expressionator.ParseContext, EvalConfig
40 {
41 private static final int MAX_CACHE_SIZE = 10;
42
43 private final DatabaseImpl _db;
44 private FunctionLookup _funcs = DefaultFunctions.LOOKUP;
45 private Map<String,DateTimeFormatter> _sdfs;
46 private Map<String,DecimalFormat> _dfs;
47 private TemporalConfig _temporal = TemporalConfig.US_TEMPORAL_CONFIG;
48 private NumericConfig _numeric = NumericConfig.US_NUMERIC_CONFIG;
49 private final RandomContextl/expr/RandomContext.html#RandomContext">RandomContext _rndCtx = new RandomContext();
50 private Bindings _bindings = new SimpleBindings();
51
52 public DBEvalContext(DatabaseImpl db) {
53 _db = db;
54 }
55
56 protected DatabaseImpl getDatabase() {
57 return _db;
58 }
59
60 @Override
61 public TemporalConfig getTemporalConfig() {
62 return _temporal;
63 }
64
65 @Override
66 public void setTemporalConfig(TemporalConfig temporal) {
67 if(_temporal != temporal) {
68 _temporal = temporal;
69 _sdfs = null;
70 }
71 }
72
73 @Override
74 public ZoneId getZoneId() {
75 return _db.getZoneId();
76 }
77
78 @Override
79 public NumericConfig getNumericConfig() {
80 return _numeric;
81 }
82
83 @Override
84 public void setNumericConfig(NumericConfig numeric) {
85 if(_numeric != numeric) {
86 _numeric = numeric;
87 _dfs = null;
88 }
89 }
90
91 @Override
92 public FunctionLookup getFunctionLookup() {
93 return _funcs;
94 }
95
96 @Override
97 public void setFunctionLookup(FunctionLookup lookup) {
98 _funcs = lookup;
99 }
100
101 @Override
102 public Bindings getBindings() {
103 return _bindings;
104 }
105
106 @Override
107 public void setBindings(Bindings bindings) {
108 _bindings = bindings;
109 }
110
111 @Override
112 public DateTimeFormatter createDateFormatter(String formatStr) {
113 if(_sdfs == null) {
114 _sdfs = new SimpleCache<String,DateTimeFormatter>(MAX_CACHE_SIZE);
115 }
116 DateTimeFormatter sdf = _sdfs.get(formatStr);
117 if(sdf == null) {
118 sdf = DateTimeFormatter.ofPattern(formatStr, _temporal.getLocale());
119 _sdfs.put(formatStr, sdf);
120 }
121 return sdf;
122 }
123
124 @Override
125 public DecimalFormat createDecimalFormat(String formatStr) {
126 if(_dfs == null) {
127 _dfs = new SimpleCache<String,DecimalFormat>(MAX_CACHE_SIZE);
128 }
129 DecimalFormat df = _dfs.get(formatStr);
130 if(df == null) {
131 df = new DecimalFormat(formatStr, _numeric.getDecimalFormatSymbols());
132 df.setRoundingMode(NumberFormatter.ROUND_MODE);
133 _dfs.put(formatStr, df);
134 }
135 return df;
136 }
137
138 public float getRandom(Integer seed) {
139 return _rndCtx.getRandom(seed);
140 }
141 }