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
|
/*
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.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.EnumMap;
import java.util.Map;
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.TemporalConfig;
import com.healthmarketscience.jackcess.expr.Value;
import com.healthmarketscience.jackcess.impl.expr.BuiltinOperators;
import com.healthmarketscience.jackcess.impl.expr.Expressionator;
/**
*
* @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();
}
public TemporalConfig getTemporalConfig() {
return _dbCtx.getTemporalConfig();
}
public SimpleDateFormat createDateFormat(String formatStr) {
return _dbCtx.createDateFormat(formatStr);
}
public float getRandom(Integer seed) {
return _dbCtx.getRandom(seed);
}
public Value.Type getResultType() {
throw new UnsupportedOperationException();
}
public Value getThisColumnValue() {
throw new UnsupportedOperationException();
}
public Value getIdentifierValue(Identifier identifier) {
throw new UnsupportedOperationException();
}
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 {
val = ColumnImpl.toInternalValue(dType, val, getDatabase());
if(val == null) {
return BuiltinOperators.NULL_VAL;
}
Value.Type vType = toValueType(dType);
switch(vType) {
case STRING:
return BuiltinOperators.toValue(val.toString());
case DATE:
case TIME:
case DATE_TIME:
return BuiltinOperators.toValue(this, vType, (Date)val);
case LONG:
Integer i = ((val instanceof Integer) ? (Integer)val :
((Number)val).intValue());
return BuiltinOperators.toValue(i);
case DOUBLE:
Double d = ((val instanceof Double) ? (Double)val :
((Number)val).doubleValue());
return BuiltinOperators.toValue(d);
case BIG_DEC:
BigDecimal bd = ColumnImpl.toBigDecimal(val, getDatabase());
return BuiltinOperators.toValue(bd);
default:
throw new RuntimeException("Unexpected type " + vType);
}
} catch(IOException e) {
throw new EvalException("Failed converting value to type " + dType, e);
}
}
protected 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, _dbCtx);
_expr = expr;
return expr;
}
public Object eval(EvalContext ctx) {
return getExpr().eval(ctx);
}
public String toDebugString() {
return "<raw>{" + _exprStr + "}";
}
public boolean isConstant() {
return getExpr().isConstant();
}
public void collectIdentifiers(Collection<Identifier> identifiers) {
getExpr().collectIdentifiers(identifiers);
}
@Override
public String toString() {
return _exprStr;
}
}
}
|