]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
fd8f882a6102a909e2c1bd433d2b96101236f096
[packages/trusty/mysql-wsrep-5.6.git] /
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 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.jdbc;
19
20 import java.math.BigDecimal;
21 import java.sql.Date;
22 import java.sql.SQLException;
23 import java.sql.Time;
24 import java.sql.Timestamp;
25 import java.util.Calendar;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import com.mysql.clusterj.core.spi.SessionSPI;
30 import com.mysql.clusterj.core.store.ResultData;
31 import com.mysql.clusterj.core.util.I18NHelper;
32 import com.mysql.clusterj.core.util.Logger;
33 import com.mysql.clusterj.core.util.LoggerFactoryService;
34
35 /** This class is part of the statement interceptor contract with the MySQL JDBC connection.
36  * When a statement is intercepted and executed, an instance of this class is returned if there
37  * is a real result to be iterated. A sibling class, ResultSetInternalMethodsUpdateCount, is
38  * returned if only an insert/delete/update count is returned.
39  * This class in turn delegates to the clusterj ResultData to retrieve data from the cluster.
40  */
41 public class ResultSetInternalMethodsImpl extends AbstractResultSetInternalMethods {
42
43     /** My message translator */
44     static final I18NHelper local = I18NHelper.getInstance(ResultSetInternalMethodsImpl.class);
45
46     /** My logger */
47     static final Logger logger = LoggerFactoryService.getFactory().getInstance(ResultSetInternalMethodsImpl.class);
48
49     private ResultData resultData;
50
51     private SessionSPI session;
52
53     private int[] columnIndexToFieldNumberMap;
54
55     private Map<String, Integer> columnNameToFieldNumberMap = new HashMap<String, Integer>();
56
57     private boolean autotransaction = true;
58
59     public ResultSetInternalMethodsImpl(ResultData resultData, int[] columnIndexToFieldNumberMap, 
60             Map<String, Integer> columnNameToFieldNumberMap, SessionSPI session) {
61         this.columnIndexToFieldNumberMap = columnIndexToFieldNumberMap;
62         this.columnNameToFieldNumberMap = columnNameToFieldNumberMap;
63         this.resultData = resultData;
64         this.session = session;
65     }
66
67     @Override
68     public boolean reallyResult() {
69         return true;
70     }
71
72     @Override
73     public boolean next() {
74         boolean hasNext = resultData.next();
75         // startAutoTransaction was called in SQLExecutor.Select.execute and 
76         // endAutoTransaction must be called exactly once after all results have been read
77         if (autotransaction & !hasNext) {
78             session.endAutoTransaction();
79             autotransaction = false;
80         }
81         if (logger.isDetailEnabled()) logger.detail("ResultSetInternalMethods.next returned: " + hasNext);
82         return hasNext;
83     }
84
85     @Override
86     public long getUpdateID() {
87         return 0;
88     }
89
90     @Override
91     public boolean getBoolean(int columnIndex) throws SQLException {
92         boolean result = resultData.getBoolean(columnIndexToFieldNumberMap[columnIndex]);
93         return result;
94     }
95
96     @Override
97     public boolean getBoolean(String columnName) throws SQLException {
98         boolean result = resultData.getBoolean(getFieldNumberForColumnName(columnName));
99         return result;
100     }
101
102     @Override
103     public byte getByte(int columnIndex) throws SQLException {
104         byte result = resultData.getByte(columnIndexToFieldNumberMap[columnIndex]);
105         return result;
106     }
107
108     @Override
109     public byte getByte(String columnName) throws SQLException {
110         byte result = resultData.getByte(getFieldNumberForColumnName(columnName));
111         return result;
112     }
113
114     @Override
115     public byte[] getBytes(int columnIndex) throws SQLException {
116         byte[] result = resultData.getBytes(columnIndexToFieldNumberMap[columnIndex]);
117         return result;
118     }
119
120     @Override
121     public byte[] getBytes(String columnName) throws SQLException {
122         byte[] result = resultData.getBytes(getFieldNumberForColumnName(columnName));
123         return result;
124     }
125
126     @Override
127     public Date getDate(int columnIndex) throws SQLException {
128         throw new SQLException(local.message("ERR_Should_Not_Occur"));
129     }
130
131     @Override
132     public Date getDate(String columnName) throws SQLException {
133         throw new SQLException(local.message("ERR_Should_Not_Occur"));
134     }
135
136     @Override
137     public Date getDate(int columnIndex, Calendar cal) throws SQLException {
138         throw new SQLException(local.message("ERR_Should_Not_Occur"));
139     }
140
141     @Override
142     public Date getDate(String columnName, Calendar cal) throws SQLException {
143         throw new SQLException(local.message("ERR_Should_Not_Occur"));
144     }
145
146     @Override
147     public double getDouble(int columnIndex) throws SQLException {
148         double result = resultData.getDouble(columnIndexToFieldNumberMap[columnIndex]);
149         return result;
150     }
151
152     @Override
153     public double getDouble(String columnName) throws SQLException {
154         double result = resultData.getDouble(getFieldNumberForColumnName(columnName));
155         return result;
156     }
157
158     @Override
159     public float getFloat(int columnIndex) throws SQLException {
160         float result = resultData.getFloat(columnIndexToFieldNumberMap[columnIndex]);
161         return result;
162     }
163
164     @Override
165     public float getFloat(String columnName) throws SQLException {
166         float result = resultData.getFloat(getFieldNumberForColumnName(columnName));
167         return result;
168     }
169
170     @Override
171     public long getLong(int columnIndex) throws SQLException {
172         long result = resultData.getLong(columnIndexToFieldNumberMap[columnIndex]);
173         return result;
174     }
175
176     @Override
177     public long getLong(String columnName) throws SQLException {
178         long result = resultData.getLong(getFieldNumberForColumnName(columnName));
179         return result;
180     }
181
182     @Override
183     public int getInt(int columnIndex) throws SQLException {
184         int result = resultData.getInt(columnIndexToFieldNumberMap[columnIndex]);
185         return result;
186     }
187
188     @Override
189     public int getInt(String columnName) throws SQLException {
190         int result = resultData.getInt(getFieldNumberForColumnName(columnName));
191         return result;
192     }
193
194     @Override
195     public short getShort(int columnIndex) throws SQLException {
196         short result = resultData.getShort(columnIndexToFieldNumberMap[columnIndex]);
197         return result;
198     }
199
200     @Override
201     public short getShort(String columnName) throws SQLException {
202         short result = resultData.getShort(getFieldNumberForColumnName(columnName));
203         return result;
204     }
205
206     @Override
207     public String getString(int columnIndex) throws SQLException {
208         String result = resultData.getString(columnIndexToFieldNumberMap[columnIndex]);
209         return result;
210     }
211
212     @Override
213     public String getString(String columnName) throws SQLException {
214         String result = resultData.getString(getFieldNumberForColumnName(columnName));
215         return result;
216     }
217
218     @Override
219     public Time getTime(int columnIndex) throws SQLException {
220         throw new SQLException(local.message("ERR_Should_Not_Occur")); 
221     }
222
223     @Override
224     public Time getTime(String columnName) throws SQLException {
225         throw new SQLException(local.message("ERR_Should_Not_Occur")); 
226     }
227
228     @Override
229     public Time getTime(int columnIndex, Calendar cal) throws SQLException {
230         throw new SQLException(local.message("ERR_Should_Not_Occur")); 
231     }
232
233     @Override
234     public Time getTime(String columnName, Calendar cal) throws SQLException {
235         throw new SQLException(local.message("ERR_Should_Not_Occur")); 
236     }
237
238     @Override
239     public Timestamp getTimestamp(int columnIndex) throws SQLException {
240         throw new SQLException(local.message("ERR_Should_Not_Occur")); 
241     }
242
243     @Override
244     public Timestamp getTimestamp(String columnName) throws SQLException {
245         throw new SQLException(local.message("ERR_Should_Not_Occur")); 
246     }
247
248     @Override
249     public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
250         throw new SQLException(local.message("ERR_Should_Not_Occur")); 
251     }
252
253     @Override
254     public Timestamp getTimestamp(String columnName, Calendar cal) throws SQLException {
255         throw new SQLException(local.message("ERR_Should_Not_Occur")); 
256     }
257
258     @Override
259     public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
260         BigDecimal result = resultData.getDecimal(columnIndexToFieldNumberMap[columnIndex]);
261         return result;
262     }
263
264     @Override
265     public BigDecimal getBigDecimal(String columnName) throws SQLException {
266         BigDecimal result = resultData.getDecimal(getFieldNumberForColumnName(columnName));
267         return result;
268     }
269
270     @Override
271     public void realClose(boolean arg0) throws SQLException {
272         // if next() was never called to end the autotransaction, do so now
273         if (autotransaction) {
274             session.endAutoTransaction();
275             autotransaction = false;
276         }
277     }
278
279     private int getFieldNumberForColumnName(String columnName) throws SQLException {
280         Integer fieldNumber = columnNameToFieldNumberMap.get(columnName);
281         if (fieldNumber != null) {
282             return fieldNumber.intValue();
283         }
284         throw new SQLException(local.message("ERR_Column_Name_Not_In_Result", columnName));
285     }
286
287 }