]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
71c95024ecdb00a5509cfa6f971ea04ee875da34
[packages/trusty/mysql-wsrep-5.6.git] /
1 /*
2    Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
3
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.
7
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.
12
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
16 */
17
18 package com.mysql.clusterj.core.query;
19
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;
30
31 import java.math.BigDecimal;
32 import java.math.BigInteger;
33 import java.sql.Date;
34 import java.sql.Time;
35 import java.sql.Timestamp;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40
41 /** This is the execution context for a query. It contains the
42  * parameter bindings so as to make query execution thread-safe.
43  *
44  */
45 public class QueryExecutionContextImpl implements QueryExecutionContext {
46
47     /** My message translator */
48     static final I18NHelper local = I18NHelper.getInstance(BetweenPredicateImpl.class);
49
50     /** My logger */
51     static final Logger logger = LoggerFactoryService.getFactory().getInstance(BetweenPredicateImpl.class);
52
53     protected Map<String, Object> boundParameters = 
54             new HashMap<String, Object>();
55
56     /** The session for this query */
57     protected SessionSPI session;
58
59     /** The filters used in the query */
60     private List<ScanFilter> filters = new ArrayList<ScanFilter>();
61
62     /** The explain for this query; will be null until executed or explained */
63     protected Map<String, Object> explain = null;
64
65     /** Create a new execution context with an empty map of parameters.
66      * @param session the session for this context
67      */
68     public QueryExecutionContextImpl(SessionSPI session) {
69         if (session == null) {
70             throw new ClusterJFatalInternalException(
71                     local.message("ERR_Session_Must_Not_Be_Null"));
72         }
73         this.session = session;
74     }
75
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
79      */
80     protected QueryExecutionContextImpl(QueryExecutionContextImpl context) {
81         this.session = context.getSession();
82         boundParameters = new HashMap<String, Object>(context.boundParameters);
83     }
84
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
88      */
89     public QueryExecutionContextImpl(SessionSPI session, Map<String, Object> parameterMap) {
90         this.session = session;
91         this.boundParameters = parameterMap;
92     }
93
94     /** Bind the value of a parameter for this query execution.
95      *
96      * @param parameterName the name of the parameter
97      * @param value the value for the parameter
98      */
99     public void bindParameterValue(String parameterName, Object value) {
100         if (parameterName == null) {
101             throw new ClusterJUserException(
102                     local.message("ERR_Parameter_Null"));
103         }
104         boundParameters.put(parameterName, value);
105     }
106     /** Get the value of a parameter by name.
107      */
108     public Object getParameterValue(String parameterName) {
109         if (!isBound(parameterName)) {
110             throw new ClusterJUserException(
111                     local.message("ERR_Parameter_Not_Bound", parameterName));
112         }
113         return boundParameters.get(parameterName);
114     }
115
116     /** Return whether the parameter has a value for this execution context.
117      *
118      * @param parameterName the name of the parameter
119      * @return whether the parameter has a value
120      */
121     public boolean isBound(String parameterName) {
122         return boundParameters.containsKey(parameterName);
123     }
124
125     public SessionSPI getSession() {
126         return session;
127     }
128
129     public ResultData getResultData(QueryDomainType<?> queryDomainType) {
130         return ((QueryDomainTypeImpl<?>)queryDomainType).getResultData(this);
131     }
132
133     /** Add a filter to the list of filters created for this query.
134      * @param scanFilter the filter
135      */
136     public void addFilter(ScanFilter scanFilter) {
137         filters.add(scanFilter);
138     }
139
140     /** Delete all the filters created for this query.
141      */
142     public void deleteFilters() {
143         for (ScanFilter filter: filters) {
144             filter.delete();
145         }
146         filters.clear();
147     }
148
149     public void setExplain(Map<String, Object> explain) {
150         this.explain = explain;
151     }
152
153     public Map<String, Object> getExplain() {
154         return explain;
155     }
156
157     public Byte getByte(String index) {
158         Object result = boundParameters.get(index);
159         if (result == null) {
160             return null;
161         }
162         if (result instanceof Byte) {
163             return (Byte)result;
164         } else {
165             throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "Byte"));
166         }
167     }
168
169     public BigDecimal getBigDecimal(String index) {
170         Object result = boundParameters.get(index);
171         if (result == null) {
172             return null;
173         }
174         if (result instanceof BigDecimal) {
175             return (BigDecimal)result;
176         } else {
177             throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "BigDecimal"));
178         }
179     }
180
181     public BigInteger getBigInteger(String index) {
182         Object result = boundParameters.get(index);
183         if (result == null) {
184             return null;
185         }
186         if (result instanceof BigInteger) {
187             return (BigInteger)result;
188         } else {
189             throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "BigInteger"));
190         }
191     }
192
193     public Boolean getBoolean(String index) {
194         Object result = boundParameters.get(index);
195         if (result == null) {
196             return null;
197         }
198         if (result instanceof Boolean) {
199             return (Boolean)result;
200         } else {
201             throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "Boolean"));
202         }
203     }
204
205     public byte[] getBytes(String index) {
206         Object result = boundParameters.get(index);
207         if (result == null) {
208             return null;
209         }
210         if (result instanceof byte[]) {
211             return (byte[])result;
212         } else {
213             throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "byte[]"));
214         }
215     }
216
217     public Double getDouble(String index) {
218         Object result = boundParameters.get(index);
219         if (result == null) {
220             return null;
221         }
222         if (result instanceof Double) {
223             return (Double)result;
224         } else {
225             throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "Double"));
226         }
227     }
228
229     public Float getFloat(String index) {
230         Object result = boundParameters.get(index);
231         if (result == null) {
232             return null;
233         }
234         if (result instanceof Float) {
235             return (Float)result;
236         } else {
237             throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "Float"));
238         }
239     }
240
241     public Integer getInt(String index) {
242         Object result = boundParameters.get(index);
243         if (result == null) {
244             return null;
245         }
246         if (result instanceof Integer) {
247             return (Integer)result;
248         } else {
249             throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "Integer"));
250         }
251     }
252
253     public Date getJavaSqlDate(String index) {
254         Object result = boundParameters.get(index);
255         if (result == null) {
256             return null;
257         }
258         if (result instanceof java.sql.Date) {
259             return (java.sql.Date)result;
260         } else {
261             throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "java.sql.Date"));
262         }
263     }
264
265     public Time getJavaSqlTime(String index) {
266         Object result = boundParameters.get(index);
267         if (result == null) {
268             return null;
269         }
270         if (result instanceof java.sql.Time) {
271             return (java.sql.Time)result;
272         } else {
273             throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "java.sql.Time"));
274         }
275     }
276
277     public Timestamp getJavaSqlTimestamp(String index) {
278         Object result = boundParameters.get(index);
279         if (result == null) {
280             return null;
281         }
282         if (result instanceof java.sql.Timestamp) {
283             return (java.sql.Timestamp)result;
284         } else {
285             throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "java.sql.Timestamp"));
286         }
287     }
288
289     public java.util.Date getJavaUtilDate(String index) {
290         Object result = boundParameters.get(index);
291         if (result == null) {
292             return null;
293         }
294         if (result instanceof java.util.Date) {
295             return (java.util.Date)result;
296         } else {
297             throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "java.util.Date"));
298         }
299     }
300
301     public Long getLong(String index) {
302         Object result = boundParameters.get(index);
303         if (result == null) {
304             return null;
305         }
306         if (result instanceof Long) {
307             return (Long)result;
308         } else {
309             throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "Long"));
310         }
311     }
312
313     public Short getShort(String index) {
314         Object result = boundParameters.get(index);
315         if (result == null) {
316             return null;
317         }
318         if (result instanceof Short) {
319             return (Short)result;
320         } else {
321             throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "Short"));
322         }
323     }
324
325     public String getString(String index) {
326         Object result = boundParameters.get(index);
327         if (result == null) {
328             return null;
329         }
330         if (result instanceof String) {
331             return (String)result;
332         } else {
333             throw new ClusterJUserException(local.message("ERR_Parameter_Wrong_Type", index, result.getClass(), "String"));
334         }
335     }
336
337     public Object getObject(String index) {
338         return boundParameters.get(index);
339     }
340
341 }