2 Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 package com.mysql.clusterj.core.query;
20 import com.mysql.clusterj.ClusterJFatalInternalException;
21 import com.mysql.clusterj.ClusterJUserException;
22 import com.mysql.clusterj.core.spi.QueryExecutionContext;
23 import com.mysql.clusterj.core.spi.SessionSPI;
24 import com.mysql.clusterj.core.store.ResultData;
25 import com.mysql.clusterj.core.store.ScanFilter;
26 import com.mysql.clusterj.core.util.I18NHelper;
27 import com.mysql.clusterj.core.util.Logger;
28 import com.mysql.clusterj.core.util.LoggerFactoryService;
29 import com.mysql.clusterj.query.QueryDomainType;
31 import java.math.BigDecimal;
32 import java.math.BigInteger;
35 import java.sql.Timestamp;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
41 /** This is the execution context for a query. It contains the
42 * parameter bindings so as to make query execution thread-safe.
45 public class QueryExecutionContextImpl implements QueryExecutionContext {
47 /** My message translator */
48 static final I18NHelper local = I18NHelper.getInstance(BetweenPredicateImpl.class);
51 static final Logger logger = LoggerFactoryService.getFactory().getInstance(BetweenPredicateImpl.class);
53 protected Map<String, Object> boundParameters =
54 new HashMap<String, Object>();
56 /** The session for this query */
57 protected SessionSPI session;
59 /** The filters used in the query */
60 private List<ScanFilter> filters = new ArrayList<ScanFilter>();
62 /** The explain for this query; will be null until executed or explained */
63 protected Map<String, Object> explain = null;
65 /** Create a new execution context with an empty map of parameters.
66 * @param session the session for this context
68 public QueryExecutionContextImpl(SessionSPI session) {
69 if (session == null) {
70 throw new ClusterJFatalInternalException(
71 local.message("ERR_Session_Must_Not_Be_Null"));
73 this.session = session;
76 /** Create a new execution context copying the bound parameter values.
77 * This allows a new execution of a query only modifying some parameters.
78 * @param context an existing execution context
80 protected QueryExecutionContextImpl(QueryExecutionContextImpl context) {
81 this.session = context.getSession();
82 boundParameters = new HashMap<String, Object>(context.boundParameters);
85 /** Create a new execution context with specific map of parameters.
86 * @param session the session for this context
87 * @param parameterMap the parameter map for this context
89 public QueryExecutionContextImpl(SessionSPI session, Map<String, Object> parameterMap) {
90 this.session = session;
91 this.boundParameters = parameterMap;
94 /** Bind the value of a parameter for this query execution.
96 * @param parameterName the name of the parameter
97 * @param value the value for the parameter
99 public void bindParameterValue(String parameterName, Object value) {
100 if (parameterName == null) {
101 throw new ClusterJUserException(
102 local.message("ERR_Parameter_Null"));
104 boundParameters.put(parameterName, value);
106 /** Get the value of a parameter by name.
108 public Object getParameterValue(String parameterName) {
109 if (!isBound(parameterName)) {
110 throw new ClusterJUserException(
111 local.message("ERR_Parameter_Not_Bound", parameterName));
113 return boundParameters.get(parameterName);
116 /** Return whether the parameter has a value for this execution context.
118 * @param parameterName the name of the parameter
119 * @return whether the parameter has a value
121 public boolean isBound(String parameterName) {
122 return boundParameters.containsKey(parameterName);
125 public SessionSPI getSession() {
129 public ResultData getResultData(QueryDomainType<?> queryDomainType) {
130 return ((QueryDomainTypeImpl<?>)queryDomainType).getResultData(this);
133 /** Add a filter to the list of filters created for this query.
134 * @param scanFilter the filter
136 public void addFilter(ScanFilter scanFilter) {
137 filters.add(scanFilter);
140 /** Delete all the filters created for this query.
142 public void deleteFilters() {
143 for (ScanFilter filter: filters) {
149 public void setExplain(Map<String, Object> explain) {
150 this.explain = explain;
153 public Map<String, Object> getExplain() {
157 public Byte getByte(String index) {
158 Object result = boundParameters.get(index);
159 if (result == null) {
162 if (result instanceof Byte) {
165 throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "Byte"));
169 public BigDecimal getBigDecimal(String index) {
170 Object result = boundParameters.get(index);
171 if (result == null) {
174 if (result instanceof BigDecimal) {
175 return (BigDecimal)result;
177 throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "BigDecimal"));
181 public BigInteger getBigInteger(String index) {
182 Object result = boundParameters.get(index);
183 if (result == null) {
186 if (result instanceof BigInteger) {
187 return (BigInteger)result;
189 throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "BigInteger"));
193 public Boolean getBoolean(String index) {
194 Object result = boundParameters.get(index);
195 if (result == null) {
198 if (result instanceof Boolean) {
199 return (Boolean)result;
201 throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "Boolean"));
205 public byte[] getBytes(String index) {
206 Object result = boundParameters.get(index);
207 if (result == null) {
210 if (result instanceof byte[]) {
211 return (byte[])result;
213 throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "byte[]"));
217 public Double getDouble(String index) {
218 Object result = boundParameters.get(index);
219 if (result == null) {
222 if (result instanceof Double) {
223 return (Double)result;
225 throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "Double"));
229 public Float getFloat(String index) {
230 Object result = boundParameters.get(index);
231 if (result == null) {
234 if (result instanceof Float) {
235 return (Float)result;
237 throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "Float"));
241 public Integer getInt(String index) {
242 Object result = boundParameters.get(index);
243 if (result == null) {
246 if (result instanceof Integer) {
247 return (Integer)result;
249 throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "Integer"));
253 public Date getJavaSqlDate(String index) {
254 Object result = boundParameters.get(index);
255 if (result == null) {
258 if (result instanceof java.sql.Date) {
259 return (java.sql.Date)result;
261 throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "java.sql.Date"));
265 public Time getJavaSqlTime(String index) {
266 Object result = boundParameters.get(index);
267 if (result == null) {
270 if (result instanceof java.sql.Time) {
271 return (java.sql.Time)result;
273 throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "java.sql.Time"));
277 public Timestamp getJavaSqlTimestamp(String index) {
278 Object result = boundParameters.get(index);
279 if (result == null) {
282 if (result instanceof java.sql.Timestamp) {
283 return (java.sql.Timestamp)result;
285 throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "java.sql.Timestamp"));
289 public java.util.Date getJavaUtilDate(String index) {
290 Object result = boundParameters.get(index);
291 if (result == null) {
294 if (result instanceof java.util.Date) {
295 return (java.util.Date)result;
297 throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "java.util.Date"));
301 public Long getLong(String index) {
302 Object result = boundParameters.get(index);
303 if (result == null) {
306 if (result instanceof Long) {
309 throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "Long"));
313 public Short getShort(String index) {
314 Object result = boundParameters.get(index);
315 if (result == null) {
318 if (result instanceof Short) {
319 return (Short)result;
321 throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "Short"));
325 public String getString(String index) {
326 Object result = boundParameters.get(index);
327 if (result == null) {
330 if (result instanceof String) {
331 return (String)result;
333 throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "String"));
337 public Object getObject(String index) {
338 return boundParameters.get(index);