2 * Copyright (c) 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.jdbc;
20 import java.math.BigDecimal;
22 import java.sql.SQLException;
24 import java.sql.Timestamp;
25 import java.util.Calendar;
26 import java.util.HashMap;
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;
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.
41 public class ResultSetInternalMethodsImpl extends AbstractResultSetInternalMethods {
43 /** My message translator */
44 static final I18NHelper local = I18NHelper.getInstance(ResultSetInternalMethodsImpl.class);
47 static final Logger logger = LoggerFactoryService.getFactory().getInstance(ResultSetInternalMethodsImpl.class);
49 private ResultData resultData;
51 private SessionSPI session;
53 private int[] columnIndexToFieldNumberMap;
55 private Map<String, Integer> columnNameToFieldNumberMap = new HashMap<String, Integer>();
57 private boolean autotransaction = true;
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;
68 public boolean reallyResult() {
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;
81 if (logger.isDetailEnabled()) logger.detail("ResultSetInternalMethods.next returned: " + hasNext);
86 public long getUpdateID() {
91 public boolean getBoolean(int columnIndex) throws SQLException {
92 boolean result = resultData.getBoolean(columnIndexToFieldNumberMap[columnIndex]);
97 public boolean getBoolean(String columnName) throws SQLException {
98 boolean result = resultData.getBoolean(getFieldNumberForColumnName(columnName));
103 public byte getByte(int columnIndex) throws SQLException {
104 byte result = resultData.getByte(columnIndexToFieldNumberMap[columnIndex]);
109 public byte getByte(String columnName) throws SQLException {
110 byte result = resultData.getByte(getFieldNumberForColumnName(columnName));
115 public byte[] getBytes(int columnIndex) throws SQLException {
116 byte[] result = resultData.getBytes(columnIndexToFieldNumberMap[columnIndex]);
121 public byte[] getBytes(String columnName) throws SQLException {
122 byte[] result = resultData.getBytes(getFieldNumberForColumnName(columnName));
127 public Date getDate(int columnIndex) throws SQLException {
128 throw new SQLException(local.message("ERR_Should_Not_Occur"));
132 public Date getDate(String columnName) throws SQLException {
133 throw new SQLException(local.message("ERR_Should_Not_Occur"));
137 public Date getDate(int columnIndex, Calendar cal) throws SQLException {
138 throw new SQLException(local.message("ERR_Should_Not_Occur"));
142 public Date getDate(String columnName, Calendar cal) throws SQLException {
143 throw new SQLException(local.message("ERR_Should_Not_Occur"));
147 public double getDouble(int columnIndex) throws SQLException {
148 double result = resultData.getDouble(columnIndexToFieldNumberMap[columnIndex]);
153 public double getDouble(String columnName) throws SQLException {
154 double result = resultData.getDouble(getFieldNumberForColumnName(columnName));
159 public float getFloat(int columnIndex) throws SQLException {
160 float result = resultData.getFloat(columnIndexToFieldNumberMap[columnIndex]);
165 public float getFloat(String columnName) throws SQLException {
166 float result = resultData.getFloat(getFieldNumberForColumnName(columnName));
171 public long getLong(int columnIndex) throws SQLException {
172 long result = resultData.getLong(columnIndexToFieldNumberMap[columnIndex]);
177 public long getLong(String columnName) throws SQLException {
178 long result = resultData.getLong(getFieldNumberForColumnName(columnName));
183 public int getInt(int columnIndex) throws SQLException {
184 int result = resultData.getInt(columnIndexToFieldNumberMap[columnIndex]);
189 public int getInt(String columnName) throws SQLException {
190 int result = resultData.getInt(getFieldNumberForColumnName(columnName));
195 public short getShort(int columnIndex) throws SQLException {
196 short result = resultData.getShort(columnIndexToFieldNumberMap[columnIndex]);
201 public short getShort(String columnName) throws SQLException {
202 short result = resultData.getShort(getFieldNumberForColumnName(columnName));
207 public String getString(int columnIndex) throws SQLException {
208 String result = resultData.getString(columnIndexToFieldNumberMap[columnIndex]);
213 public String getString(String columnName) throws SQLException {
214 String result = resultData.getString(getFieldNumberForColumnName(columnName));
219 public Time getTime(int columnIndex) throws SQLException {
220 throw new SQLException(local.message("ERR_Should_Not_Occur"));
224 public Time getTime(String columnName) throws SQLException {
225 throw new SQLException(local.message("ERR_Should_Not_Occur"));
229 public Time getTime(int columnIndex, Calendar cal) throws SQLException {
230 throw new SQLException(local.message("ERR_Should_Not_Occur"));
234 public Time getTime(String columnName, Calendar cal) throws SQLException {
235 throw new SQLException(local.message("ERR_Should_Not_Occur"));
239 public Timestamp getTimestamp(int columnIndex) throws SQLException {
240 throw new SQLException(local.message("ERR_Should_Not_Occur"));
244 public Timestamp getTimestamp(String columnName) throws SQLException {
245 throw new SQLException(local.message("ERR_Should_Not_Occur"));
249 public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
250 throw new SQLException(local.message("ERR_Should_Not_Occur"));
254 public Timestamp getTimestamp(String columnName, Calendar cal) throws SQLException {
255 throw new SQLException(local.message("ERR_Should_Not_Occur"));
259 public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
260 BigDecimal result = resultData.getDecimal(columnIndexToFieldNumberMap[columnIndex]);
265 public BigDecimal getBigDecimal(String columnName) throws SQLException {
266 BigDecimal result = resultData.getDecimal(getFieldNumberForColumnName(columnName));
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;
279 private int getFieldNumberForColumnName(String columnName) throws SQLException {
280 Integer fieldNumber = columnNameToFieldNumberMap.get(columnName);
281 if (fieldNumber != null) {
282 return fieldNumber.intValue();
284 throw new SQLException(local.message("ERR_Column_Name_Not_In_Result", columnName));