1 /*
2  *  Copyright (c) 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, version 2.0,
6  *  as published by the Free Software Foundation.
7  *
8  *  This program is also distributed with certain software (including
9  *  but not limited to OpenSSL) that is licensed under separate terms,
10  *  as designated in a particular file or component or in included license
11  *  documentation.  The authors of MySQL hereby grant you an additional
12  *  permission to link the program and your derivative works with the
13  *  separately licensed software that they have included with MySQL.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License, version 2.0, for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23  */
24 
25 package com.mysql.clusterj.jdbc;
26 
27 import java.math.BigDecimal;
28 import java.math.BigInteger;
29 import java.sql.Date;
30 import java.sql.SQLException;
31 import java.sql.Time;
32 import java.sql.Timestamp;
33 
34 import com.mysql.clusterj.ClusterJUserException;
35 import com.mysql.clusterj.core.query.QueryExecutionContextImpl;
36 import com.mysql.clusterj.core.spi.SessionSPI;
37 import com.mysql.clusterj.core.util.I18NHelper;
38 import com.mysql.clusterj.core.util.Logger;
39 import com.mysql.clusterj.core.util.LoggerFactoryService;
40 import com.mysql.jdbc.ParameterBindings;
41 
42 /** This class handles retrieving parameter values from the parameterBindings
43  * associated with a PreparedStatement.
44  */
45 public class QueryExecutionContextJDBCImpl extends QueryExecutionContextImpl {
46 
47     /** My message translator */
48     static final I18NHelper local = I18NHelper.getInstance(QueryExecutionContextJDBCImpl.class);
49 
50     /** My logger */
51     static final Logger logger = LoggerFactoryService.getFactory().getInstance(QueryExecutionContextJDBCImpl.class);
52 
53     /** The wrapped ParameterBindings */
54     ParameterBindings parameterBindings;
55 
56     /** The current offset */
57     int offset = 0;
58 
59     /** The number of parameters */
60     int numberOfParameters;
61 
62     /** Create a new execution context with parameter bindings.
63      * @param parameterBindings the jdbc parameter bindings for the statement
64      * @param session the session for this context
65      * @param numberOfParameters the number of parameters per statement
66      */
QueryExecutionContextJDBCImpl(SessionSPI session, ParameterBindings parameterBindings, int numberOfParameters)67     public QueryExecutionContextJDBCImpl(SessionSPI session,
68             ParameterBindings parameterBindings, int numberOfParameters) {
69         super(session);
70         this.parameterBindings = parameterBindings;
71         this.numberOfParameters = numberOfParameters;
72     }
73 
74     /** Advance to the next statement (and next number of affected rows).
75      */
nextStatement()76     public void nextStatement() {
77         offset += numberOfParameters;
78     }
79 
getByte(String index)80     public Byte getByte(String index) {
81         try {
82             int parameterIndex = Integer.valueOf(index) + offset;
83             Byte result = parameterBindings.getByte(parameterIndex);
84             return result;
85         } catch (SQLException ex) {
86                 throw new ClusterJUserException(local.message("ERR_Getting_Parameter_Value", offset, index), ex);
87         }
88     }
89 
getBigDecimal(String index)90     public BigDecimal getBigDecimal(String index) {
91         try {
92             int parameterIndex = Integer.valueOf(index) + offset;
93             BigDecimal result = parameterBindings.getBigDecimal(parameterIndex);
94             return result;
95         } catch (SQLException ex) {
96                 throw new ClusterJUserException(local.message("ERR_Getting_Parameter_Value", offset, index), ex);
97         }
98     }
99 
getBigInteger(String index)100     public BigInteger getBigInteger(String index) {
101         try {
102             int parameterIndex = Integer.valueOf(index) + offset;
103             BigInteger result = parameterBindings.getBigDecimal(parameterIndex).toBigInteger();
104             return result;
105         } catch (SQLException ex) {
106                 throw new ClusterJUserException(local.message("ERR_Getting_Parameter_Value", offset, index), ex);
107         }
108     }
109 
getBoolean(String index)110     public Boolean getBoolean(String index) {
111         try {
112             int parameterIndex = Integer.valueOf(index) + offset;
113             Boolean result = parameterBindings.getBoolean(parameterIndex);
114             return result;
115         } catch (SQLException ex) {
116                 throw new ClusterJUserException(local.message("ERR_Getting_Parameter_Value", offset, index), ex);
117         }
118     }
119 
getBytes(String index)120     public byte[] getBytes(String index) {
121         try {
122             int parameterIndex = Integer.valueOf(index) + offset;
123             byte[] result = parameterBindings.getBytes(parameterIndex);
124             return result;
125         } catch (SQLException ex) {
126                 throw new ClusterJUserException(local.message("ERR_Getting_Parameter_Value", offset, index), ex);
127         }
128     }
129 
getDouble(String index)130     public Double getDouble(String index) {
131         try {
132             int parameterIndex = Integer.valueOf(index) + offset;
133             Double result = parameterBindings.getDouble(parameterIndex);
134             return result;
135         } catch (SQLException ex) {
136                 throw new ClusterJUserException(local.message("ERR_Getting_Parameter_Value", offset, index), ex);
137         }
138     }
139 
getFloat(String index)140     public Float getFloat(String index) {
141         try {
142             int parameterIndex = Integer.valueOf(index) + offset;
143             Float result = parameterBindings.getFloat(parameterIndex);
144             return result;
145         } catch (SQLException ex) {
146                 throw new ClusterJUserException(local.message("ERR_Getting_Parameter_Value", offset, index), ex);
147         }
148     }
149 
getInt(String index)150     public Integer getInt(String index) {
151         try {
152             int parameterIndex = Integer.valueOf(index) + offset;
153             Integer result = parameterBindings.getInt(parameterIndex);
154             return result;
155         } catch (SQLException ex) {
156                 throw new ClusterJUserException(local.message("ERR_Getting_Parameter_Value", offset, index), ex);
157         }
158     }
159 
getJavaSqlDate(String index)160     public Date getJavaSqlDate(String index) {
161         try {
162             int parameterIndex = Integer.valueOf(index) + offset;
163             java.sql.Date result = parameterBindings.getDate(parameterIndex);
164             return result;
165         } catch (SQLException ex) {
166                 throw new ClusterJUserException(local.message("ERR_Getting_Parameter_Value", offset, index), ex);
167         }
168     }
169 
getJavaSqlTime(String index)170     public Time getJavaSqlTime(String index) {
171         try {
172             int parameterIndex = Integer.valueOf(index) + offset;
173             Time result = parameterBindings.getTime(parameterIndex);
174             return result;
175         } catch (SQLException ex) {
176                 throw new ClusterJUserException(local.message("ERR_Getting_Parameter_Value", offset, index), ex);
177         }
178     }
179 
getJavaSqlTimestamp(String index)180     public Timestamp getJavaSqlTimestamp(String index) {
181         try {
182             int parameterIndex = Integer.valueOf(index) + offset;
183             java.sql.Timestamp result = parameterBindings.getTimestamp(parameterIndex);
184             return result;
185         } catch (SQLException ex) {
186                 throw new ClusterJUserException(local.message("ERR_Getting_Parameter_Value", offset, index), ex);
187         }
188     }
189 
getJavaUtilDate(String index)190     public java.util.Date getJavaUtilDate(String index) {
191         try {
192             int parameterIndex = Integer.valueOf(index) + offset;
193             java.util.Date result = parameterBindings.getDate(parameterIndex);
194             return result;
195         } catch (SQLException ex) {
196                 throw new ClusterJUserException(local.message("ERR_Getting_Parameter_Value", offset, index), ex);
197         }
198     }
199 
getLong(String index)200     public Long getLong(String index) {
201         try {
202             int parameterIndex = Integer.valueOf(index) + offset;
203             Long result = parameterBindings.getLong(parameterIndex);
204             return result;
205         } catch (SQLException ex) {
206                 throw new ClusterJUserException(local.message("ERR_Getting_Parameter_Value", offset, index), ex);
207         }
208     }
209 
getShort(String index)210     public Short getShort(String index) {
211         try {
212             int parameterIndex = Integer.valueOf(index) + offset;
213             Short result = parameterBindings.getShort(parameterIndex);
214             return result;
215         } catch (SQLException ex) {
216                 throw new ClusterJUserException(local.message("ERR_Getting_Parameter_Value", offset, index), ex);
217         }
218     }
219 
getString(String index)220     public String getString(String index) {
221         try {
222             int parameterIndex = Integer.valueOf(index) + offset;
223             String result = parameterBindings.getString(parameterIndex);
224             return result;
225         } catch (SQLException ex) {
226                 throw new ClusterJUserException(local.message("ERR_Getting_Parameter_Value", offset, index), ex);
227         }
228     }
229 
getObject(String index)230     public Object getObject(String index) {
231         try {
232             int parameterIndex = Integer.valueOf(index) + offset;
233             Object result = parameterBindings.getObject(parameterIndex);
234             return result;
235         } catch (SQLException ex) {
236                 throw new ClusterJUserException(local.message("ERR_Getting_Parameter_Value", offset, index), ex);
237         }
238     }
239 
240 }
241