You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseEvalContext.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. Copyright (c) 2018 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl;
  14. import java.io.IOException;
  15. import java.math.BigDecimal;
  16. import java.text.DecimalFormat;
  17. import java.time.LocalDateTime;
  18. import java.time.ZoneId;
  19. import java.time.format.DateTimeFormatter;
  20. import java.util.Collection;
  21. import java.util.Date;
  22. import java.util.EnumMap;
  23. import java.util.Map;
  24. import javax.script.Bindings;
  25. import com.healthmarketscience.jackcess.DataType;
  26. import com.healthmarketscience.jackcess.DateTimeType;
  27. import com.healthmarketscience.jackcess.JackcessException;
  28. import com.healthmarketscience.jackcess.expr.EvalContext;
  29. import com.healthmarketscience.jackcess.expr.EvalException;
  30. import com.healthmarketscience.jackcess.expr.Expression;
  31. import com.healthmarketscience.jackcess.expr.Identifier;
  32. import com.healthmarketscience.jackcess.expr.LocaleContext;
  33. import com.healthmarketscience.jackcess.expr.NumericConfig;
  34. import com.healthmarketscience.jackcess.expr.TemporalConfig;
  35. import com.healthmarketscience.jackcess.expr.Value;
  36. import com.healthmarketscience.jackcess.impl.expr.Expressionator;
  37. import com.healthmarketscience.jackcess.impl.expr.ValueSupport;
  38. /**
  39. *
  40. * @author James Ahlborn
  41. */
  42. public abstract class BaseEvalContext implements EvalContext
  43. {
  44. /** map of all non-string data types */
  45. private static final Map<DataType,Value.Type> TYPE_MAP =
  46. new EnumMap<DataType,Value.Type>(DataType.class);
  47. static {
  48. TYPE_MAP.put(DataType.BOOLEAN,Value.Type.LONG);
  49. TYPE_MAP.put(DataType.BYTE,Value.Type.LONG);
  50. TYPE_MAP.put(DataType.INT,Value.Type.LONG);
  51. TYPE_MAP.put(DataType.LONG,Value.Type.LONG);
  52. TYPE_MAP.put(DataType.MONEY,Value.Type.DOUBLE);
  53. TYPE_MAP.put(DataType.FLOAT,Value.Type.DOUBLE);
  54. TYPE_MAP.put(DataType.DOUBLE,Value.Type.DOUBLE);
  55. TYPE_MAP.put(DataType.SHORT_DATE_TIME,Value.Type.DATE_TIME);
  56. TYPE_MAP.put(DataType.NUMERIC,Value.Type.BIG_DEC);
  57. TYPE_MAP.put(DataType.BIG_INT,Value.Type.BIG_DEC);
  58. }
  59. private final DBEvalContext _dbCtx;
  60. private Expression _expr;
  61. protected BaseEvalContext(DBEvalContext dbCtx) {
  62. _dbCtx = dbCtx;
  63. }
  64. void setExpr(Expressionator.Type exprType, String exprStr) {
  65. _expr = new RawExpr(exprType, exprStr);
  66. }
  67. protected DatabaseImpl getDatabase() {
  68. return _dbCtx.getDatabase();
  69. }
  70. @Override
  71. public TemporalConfig getTemporalConfig() {
  72. return _dbCtx.getTemporalConfig();
  73. }
  74. @Override
  75. public DateTimeFormatter createDateFormatter(String formatStr) {
  76. return _dbCtx.createDateFormatter(formatStr);
  77. }
  78. @Override
  79. public ZoneId getZoneId() {
  80. return _dbCtx.getZoneId();
  81. }
  82. @Override
  83. public NumericConfig getNumericConfig() {
  84. return _dbCtx.getNumericConfig();
  85. }
  86. @Override
  87. public DecimalFormat createDecimalFormat(String formatStr) {
  88. return _dbCtx.createDecimalFormat(formatStr);
  89. }
  90. @Override
  91. public float getRandom(Integer seed) {
  92. return _dbCtx.getRandom(seed);
  93. }
  94. @Override
  95. public Value.Type getResultType() {
  96. return null;
  97. }
  98. @Override
  99. public Value getThisColumnValue() {
  100. throw new UnsupportedOperationException();
  101. }
  102. @Override
  103. public Value getIdentifierValue(Identifier identifier) {
  104. throw new UnsupportedOperationException();
  105. }
  106. @Override
  107. public Bindings getBindings() {
  108. return _dbCtx.getBindings();
  109. }
  110. @Override
  111. public Object get(String key) {
  112. return _dbCtx.getBindings().get(key);
  113. }
  114. @Override
  115. public void put(String key, Object value) {
  116. _dbCtx.getBindings().put(key, value);
  117. }
  118. public Object eval() throws IOException {
  119. try {
  120. return _expr.eval(this);
  121. } catch(Exception e) {
  122. String msg = withErrorContext(e.getMessage());
  123. throw new JackcessException(msg, e);
  124. }
  125. }
  126. public void collectIdentifiers(Collection<Identifier> identifiers) {
  127. _expr.collectIdentifiers(identifiers);
  128. }
  129. @Override
  130. public String toString() {
  131. return _expr.toString();
  132. }
  133. protected Value toValue(Object val, DataType dType) {
  134. try {
  135. // expression engine always uses LocalDateTime, so force that date/time
  136. // type
  137. val = ColumnImpl.toInternalValue(dType, val, getDatabase(),
  138. ColumnImpl.LDT_DATE_TIME_FACTORY);
  139. if(val == null) {
  140. return ValueSupport.NULL_VAL;
  141. }
  142. Value.Type vType = toValueType(dType);
  143. switch(vType) {
  144. case STRING:
  145. return ValueSupport.toValue(val.toString());
  146. case DATE:
  147. case TIME:
  148. case DATE_TIME:
  149. return ValueSupport.toValue(vType, (LocalDateTime)val);
  150. case LONG:
  151. Integer i = ((val instanceof Integer) ? (Integer)val :
  152. ((Number)val).intValue());
  153. return ValueSupport.toValue(i);
  154. case DOUBLE:
  155. Double d = ((val instanceof Double) ? (Double)val :
  156. ((Number)val).doubleValue());
  157. return ValueSupport.toValue(d);
  158. case BIG_DEC:
  159. BigDecimal bd = ColumnImpl.toBigDecimal(val, getDatabase());
  160. return ValueSupport.toValue(bd);
  161. default:
  162. throw new RuntimeException("Unexpected type " + vType);
  163. }
  164. } catch(IOException e) {
  165. throw new EvalException("Failed converting value to type " + dType, e);
  166. }
  167. }
  168. public static Value.Type toValueType(DataType dType) {
  169. Value.Type type = TYPE_MAP.get(dType);
  170. return ((type == null) ? Value.Type.STRING : type);
  171. }
  172. protected abstract String withErrorContext(String msg);
  173. private class RawExpr implements Expression
  174. {
  175. private final Expressionator.Type _exprType;
  176. private final String _exprStr;
  177. private RawExpr(Expressionator.Type exprType, String exprStr) {
  178. _exprType = exprType;
  179. _exprStr = exprStr;
  180. }
  181. private Expression getExpr() {
  182. // when the expression is parsed we replace the raw version
  183. Expression expr = Expressionator.parse(
  184. _exprType, _exprStr, getResultType(), _dbCtx);
  185. _expr = expr;
  186. return expr;
  187. }
  188. @Override
  189. public Object eval(EvalContext ctx) {
  190. return getExpr().eval(ctx);
  191. }
  192. @Override
  193. public String toDebugString(LocaleContext ctx) {
  194. return getExpr().toDebugString(ctx);
  195. }
  196. @Override
  197. public String toRawString() {
  198. return _exprStr;
  199. }
  200. @Override
  201. public String toCleanString(LocaleContext ctx) {
  202. return getExpr().toCleanString(ctx);
  203. }
  204. @Override
  205. public boolean isConstant() {
  206. return getExpr().isConstant();
  207. }
  208. @Override
  209. public void collectIdentifiers(Collection<Identifier> identifiers) {
  210. getExpr().collectIdentifiers(identifiers);
  211. }
  212. @Override
  213. public String toString() {
  214. return toRawString();
  215. }
  216. }
  217. }