Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

BaseEvalContext.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.SimpleDateFormat;
  17. import java.util.Collection;
  18. import java.util.Date;
  19. import java.util.EnumMap;
  20. import java.util.Map;
  21. import javax.script.Bindings;
  22. import com.healthmarketscience.jackcess.DataType;
  23. import com.healthmarketscience.jackcess.JackcessException;
  24. import com.healthmarketscience.jackcess.expr.EvalContext;
  25. import com.healthmarketscience.jackcess.expr.EvalException;
  26. import com.healthmarketscience.jackcess.expr.Expression;
  27. import com.healthmarketscience.jackcess.expr.Identifier;
  28. import com.healthmarketscience.jackcess.expr.TemporalConfig;
  29. import com.healthmarketscience.jackcess.expr.Value;
  30. import com.healthmarketscience.jackcess.impl.expr.BuiltinOperators;
  31. import com.healthmarketscience.jackcess.impl.expr.Expressionator;
  32. /**
  33. *
  34. * @author James Ahlborn
  35. */
  36. public abstract class BaseEvalContext implements EvalContext
  37. {
  38. /** map of all non-string data types */
  39. private static final Map<DataType,Value.Type> TYPE_MAP =
  40. new EnumMap<DataType,Value.Type>(DataType.class);
  41. static {
  42. TYPE_MAP.put(DataType.BOOLEAN,Value.Type.LONG);
  43. TYPE_MAP.put(DataType.BYTE,Value.Type.LONG);
  44. TYPE_MAP.put(DataType.INT,Value.Type.LONG);
  45. TYPE_MAP.put(DataType.LONG,Value.Type.LONG);
  46. TYPE_MAP.put(DataType.MONEY,Value.Type.DOUBLE);
  47. TYPE_MAP.put(DataType.FLOAT,Value.Type.DOUBLE);
  48. TYPE_MAP.put(DataType.DOUBLE,Value.Type.DOUBLE);
  49. TYPE_MAP.put(DataType.SHORT_DATE_TIME,Value.Type.DATE_TIME);
  50. TYPE_MAP.put(DataType.NUMERIC,Value.Type.BIG_DEC);
  51. TYPE_MAP.put(DataType.BIG_INT,Value.Type.BIG_DEC);
  52. }
  53. private final DBEvalContext _dbCtx;
  54. private Expression _expr;
  55. protected BaseEvalContext(DBEvalContext dbCtx) {
  56. _dbCtx = dbCtx;
  57. }
  58. void setExpr(Expressionator.Type exprType, String exprStr) {
  59. _expr = new RawExpr(exprType, exprStr);
  60. }
  61. protected DatabaseImpl getDatabase() {
  62. return _dbCtx.getDatabase();
  63. }
  64. public TemporalConfig getTemporalConfig() {
  65. return _dbCtx.getTemporalConfig();
  66. }
  67. public SimpleDateFormat createDateFormat(String formatStr) {
  68. return _dbCtx.createDateFormat(formatStr);
  69. }
  70. public float getRandom(Integer seed) {
  71. return _dbCtx.getRandom(seed);
  72. }
  73. public Value.Type getResultType() {
  74. throw new UnsupportedOperationException();
  75. }
  76. public Value getThisColumnValue() {
  77. throw new UnsupportedOperationException();
  78. }
  79. public Value getIdentifierValue(Identifier identifier) {
  80. throw new UnsupportedOperationException();
  81. }
  82. public Bindings getBindings() {
  83. return _dbCtx.getBindings();
  84. }
  85. public Object get(String key) {
  86. return _dbCtx.getBindings().get(key);
  87. }
  88. public void put(String key, Object value) {
  89. _dbCtx.getBindings().put(key, value);
  90. }
  91. public Object eval() throws IOException {
  92. try {
  93. return _expr.eval(this);
  94. } catch(Exception e) {
  95. String msg = withErrorContext(e.getMessage());
  96. throw new JackcessException(msg, e);
  97. }
  98. }
  99. public void collectIdentifiers(Collection<Identifier> identifiers) {
  100. _expr.collectIdentifiers(identifiers);
  101. }
  102. @Override
  103. public String toString() {
  104. return _expr.toString();
  105. }
  106. protected Value toValue(Object val, DataType dType) {
  107. try {
  108. val = ColumnImpl.toInternalValue(dType, val, getDatabase());
  109. if(val == null) {
  110. return BuiltinOperators.NULL_VAL;
  111. }
  112. Value.Type vType = toValueType(dType);
  113. switch(vType) {
  114. case STRING:
  115. return BuiltinOperators.toValue(val.toString());
  116. case DATE:
  117. case TIME:
  118. case DATE_TIME:
  119. return BuiltinOperators.toValue(this, vType, (Date)val);
  120. case LONG:
  121. Integer i = ((val instanceof Integer) ? (Integer)val :
  122. ((Number)val).intValue());
  123. return BuiltinOperators.toValue(i);
  124. case DOUBLE:
  125. Double d = ((val instanceof Double) ? (Double)val :
  126. ((Number)val).doubleValue());
  127. return BuiltinOperators.toValue(d);
  128. case BIG_DEC:
  129. BigDecimal bd = ColumnImpl.toBigDecimal(val, getDatabase());
  130. return BuiltinOperators.toValue(bd);
  131. default:
  132. throw new RuntimeException("Unexpected type " + vType);
  133. }
  134. } catch(IOException e) {
  135. throw new EvalException("Failed converting value to type " + dType, e);
  136. }
  137. }
  138. protected static Value.Type toValueType(DataType dType) {
  139. Value.Type type = TYPE_MAP.get(dType);
  140. return ((type == null) ? Value.Type.STRING : type);
  141. }
  142. protected abstract String withErrorContext(String msg);
  143. private class RawExpr implements Expression
  144. {
  145. private final Expressionator.Type _exprType;
  146. private final String _exprStr;
  147. private RawExpr(Expressionator.Type exprType, String exprStr) {
  148. _exprType = exprType;
  149. _exprStr = exprStr;
  150. }
  151. private Expression getExpr() {
  152. // when the expression is parsed we replace the raw version
  153. Expression expr = Expressionator.parse(_exprType, _exprStr, _dbCtx);
  154. _expr = expr;
  155. return expr;
  156. }
  157. public Object eval(EvalContext ctx) {
  158. return getExpr().eval(ctx);
  159. }
  160. public String toDebugString() {
  161. return "<raw>{" + _exprStr + "}";
  162. }
  163. public boolean isConstant() {
  164. return getExpr().isConstant();
  165. }
  166. public void collectIdentifiers(Collection<Identifier> identifiers) {
  167. getExpr().collectIdentifiers(identifiers);
  168. }
  169. @Override
  170. public String toString() {
  171. return _exprStr;
  172. }
  173. }
  174. }