aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com/healthmarketscience/jackcess/impl/BaseEvalContext.java
blob: 80c63aa375a47ad746e6b70035d38a7c15e1bf2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
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;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.EnumMap;
import java.util.Map;
import javax.script.Bindings;

import com.healthmarketscience.jackcess.DataType;
import com.healthmarketscience.jackcess.JackcessException;
import com.healthmarketscience.jackcess.expr.EvalContext;
import com.healthmarketscience.jackcess.expr.EvalException;
import com.healthmarketscience.jackcess.expr.Expression;
import com.healthmarketscience.jackcess.expr.Identifier;
import com.healthmarketscience.jackcess.expr.LocaleContext;
import com.healthmarketscience.jackcess.expr.NumericConfig;
import com.healthmarketscience.jackcess.expr.TemporalConfig;
import com.healthmarketscience.jackcess.expr.Value;
import com.healthmarketscience.jackcess.impl.expr.Expressionator;
import com.healthmarketscience.jackcess.impl.expr.ValueSupport;

/**
 *
 * @author James Ahlborn
 */
public abstract class BaseEvalContext implements EvalContext
{
  /** map of all non-string data types */
  private static final Map<DataType,Value.Type> TYPE_MAP =
    new EnumMap<DataType,Value.Type>(DataType.class);

  static {
    TYPE_MAP.put(DataType.BOOLEAN,Value.Type.LONG);
    TYPE_MAP.put(DataType.BYTE,Value.Type.LONG);
    TYPE_MAP.put(DataType.INT,Value.Type.LONG);
    TYPE_MAP.put(DataType.LONG,Value.Type.LONG);
    TYPE_MAP.put(DataType.MONEY,Value.Type.DOUBLE);
    TYPE_MAP.put(DataType.FLOAT,Value.Type.DOUBLE);
    TYPE_MAP.put(DataType.DOUBLE,Value.Type.DOUBLE);
    TYPE_MAP.put(DataType.SHORT_DATE_TIME,Value.Type.DATE_TIME);
    TYPE_MAP.put(DataType.NUMERIC,Value.Type.BIG_DEC);
    TYPE_MAP.put(DataType.BIG_INT,Value.Type.BIG_DEC);
  }

  private final DBEvalContext _dbCtx;
  private Expression _expr;

  protected BaseEvalContext(DBEvalContext dbCtx) {
    _dbCtx = dbCtx;
  }

  void setExpr(Expressionator.Type exprType, String exprStr) {
    _expr = new RawExpr(exprType, exprStr);
  }

  protected DatabaseImpl getDatabase() {
    return _dbCtx.getDatabase();
  }

  @Override
  public TemporalConfig getTemporalConfig() {
    return _dbCtx.getTemporalConfig();
  }

  @Override
  public DateTimeFormatter createDateFormatter(String formatStr) {
    return _dbCtx.createDateFormatter(formatStr);
  }

  @Override
  public ZoneId getZoneId() {
    return _dbCtx.getZoneId();
  }

  @Override
  public NumericConfig getNumericConfig() {
    return _dbCtx.getNumericConfig();
  }

  @Override
  public DecimalFormat createDecimalFormat(String formatStr) {
    return _dbCtx.createDecimalFormat(formatStr);
  }

  @Override
  public float getRandom(Integer seed) {
    return _dbCtx.getRandom(seed);
  }

  @Override
  public Value.Type getResultType() {
    return null;
  }

  @Override
  public Value getThisColumnValue() {
    throw new UnsupportedOperationException();
  }

  @Override
  public Value getIdentifierValue(Identifier identifier) {
    throw new UnsupportedOperationException();
  }

  @Override
  public Bindings getBindings() {
    return _dbCtx.getBindings();
  }

  @Override
  public Object get(String key) {
    return _dbCtx.getBindings().get(key);
  }

  @Override
  public void put(String key, Object value) {
    _dbCtx.getBindings().put(key, value);
  }

  public Object eval() throws IOException {
    try {
      return _expr.eval(this);
    } catch(Exception e) {
      String msg = withErrorContext(e.getMessage());
      throw new JackcessException(msg, e);
    }
  }

  public void collectIdentifiers(Collection<Identifier> identifiers) {
    _expr.collectIdentifiers(identifiers);
  }

  @Override
  public String toString() {
    return _expr.toString();
  }

  protected Value toValue(Object val, DataType dType) {
    try {
      // expression engine always uses LocalDateTime, so force that date/time
      // type
      val = ColumnImpl.toInternalValue(dType, val, getDatabase(),
                                       ColumnImpl.LDT_DATE_TIME_FACTORY);
      if(val == null) {
        return ValueSupport.NULL_VAL;
      }

      Value.Type vType = toValueType(dType);
      switch(vType) {
      case STRING:
        return ValueSupport.toValue(val.toString());
      case DATE:
      case TIME:
      case DATE_TIME:
        return ValueSupport.toValue(vType, (LocalDateTime)val);
      case LONG:
        Integer i = ((val instanceof Integer) ? (Integer)val :
                     ((Number)val).intValue());
        return ValueSupport.toValue(i);
      case DOUBLE:
        Double d = ((val instanceof Double) ? (Double)val :
                    ((Number)val).doubleValue());
        return ValueSupport.toValue(d);
      case BIG_DEC:
        BigDecimal bd = ColumnImpl.toBigDecimal(val, getDatabase());
        return ValueSupport.toValue(bd);
      default:
        throw new RuntimeException("Unexpected type " + vType);
      }
    } catch(IOException e) {
      throw new EvalException("Failed converting value to type " + dType, e);
    }
  }

  public static Value.Type toValueType(DataType dType) {
    Value.Type type = TYPE_MAP.get(dType);
    return ((type == null) ? Value.Type.STRING : type);
  }

  protected abstract String withErrorContext(String msg);

  private class RawExpr implements Expression
  {
    private final Expressionator.Type _exprType;
    private final String _exprStr;

    private RawExpr(Expressionator.Type exprType, String exprStr) {
      _exprType = exprType;
      _exprStr = exprStr;
    }

    private Expression getExpr() {
      // when the expression is parsed we replace the raw version
      Expression expr = Expressionator.parse(
          _exprType, _exprStr, getResultType(), _dbCtx);
      _expr = expr;
      return expr;
    }

    @Override
    public Object eval(EvalContext ctx) {
      return getExpr().eval(ctx);
    }

    @Override
    public String toDebugString(LocaleContext ctx) {
      return getExpr().toDebugString(ctx);
    }

    @Override
    public String toRawString() {
      return _exprStr;
    }

    @Override
    public String toCleanString(LocaleContext ctx) {
      return getExpr().toCleanString(ctx);
    }

    @Override
    public boolean isConstant() {
      return getExpr().isConstant();
    }

    @Override
    public void collectIdentifiers(Collection<Identifier> identifiers) {
      getExpr().collectIdentifiers(identifiers);
    }

    @Override
    public String toString() {
      return toRawString();
    }
  }
}