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