001: /*
002: Copyright (C) 2007 MySQL AB
003:
004: This program is free software; you can redistribute it and/or modify
005: it under the terms of version 2 of the GNU General Public License as
006: published by the Free Software Foundation.
007:
008: There are special exceptions to the terms and conditions of the GPL
009: as it is applied to this software. View the full text of the
010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
011: software distribution.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU General Public License for more details.
017:
018: You should have received a copy of the GNU General Public License
019: along with this program; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021:
022: */
023: package com.mysql.jdbc;
024:
025: import java.io.ByteArrayInputStream;
026: import java.io.InputStream;
027: import java.io.InputStreamReader;
028: import java.io.Reader;
029: import java.io.UnsupportedEncodingException;
030: import java.sql.Date;
031: import java.sql.SQLException;
032: import java.sql.Time;
033: import java.sql.Timestamp;
034: import java.util.Calendar;
035: import java.util.TimeZone;
036:
037: /**
038: * A RowHolder implementation that is for cached results (a-la
039: * mysql_store_result()).
040: *
041: * @version $Id: $
042: */
043: public class ByteArrayRow extends ResultSetRow {
044:
045: byte[][] internalRowData;
046:
047: public ByteArrayRow(byte[][] internalRowData) {
048: this .internalRowData = internalRowData;
049: }
050:
051: public byte[] getColumnValue(int index) throws SQLException {
052: return this .internalRowData[index];
053: }
054:
055: public void setColumnValue(int index, byte[] value)
056: throws SQLException {
057: this .internalRowData[index] = value;
058: }
059:
060: public String getString(int index, String encoding,
061: ConnectionImpl conn) throws SQLException {
062: byte[] columnData = this .internalRowData[index];
063:
064: if (columnData == null) {
065: return null;
066: }
067:
068: return getString(encoding, conn, columnData, 0,
069: columnData.length);
070: }
071:
072: public boolean isNull(int index) throws SQLException {
073: return this .internalRowData[index] == null;
074: }
075:
076: public boolean isFloatingPointNumber(int index) throws SQLException {
077: byte[] numAsBytes = this .internalRowData[index];
078:
079: if (this .internalRowData[index] == null
080: || this .internalRowData[index].length == 0) {
081: return false;
082: }
083:
084: for (int i = 0; i < numAsBytes.length; i++) {
085: if (((char) numAsBytes[i] == 'e')
086: || ((char) numAsBytes[i] == 'E')) {
087: return true;
088: }
089: }
090:
091: return false;
092: }
093:
094: public long length(int index) throws SQLException {
095: if (this .internalRowData[index] == null) {
096: return 0;
097: }
098:
099: return this .internalRowData[index].length;
100: }
101:
102: public int getInt(int columnIndex) {
103: if (this .internalRowData[columnIndex] == null) {
104: return 0;
105: }
106:
107: return StringUtils.getInt(this .internalRowData[columnIndex]);
108: }
109:
110: public long getLong(int columnIndex) {
111: if (this .internalRowData[columnIndex] == null) {
112: return 0;
113: }
114:
115: return StringUtils.getLong(this .internalRowData[columnIndex]);
116: }
117:
118: public Timestamp getTimestampFast(int columnIndex,
119: Calendar targetCalendar, TimeZone tz, boolean rollForward,
120: ConnectionImpl conn, ResultSetImpl rs) throws SQLException {
121: byte[] columnValue = this .internalRowData[columnIndex];
122:
123: if (columnValue == null) {
124: return null;
125: }
126:
127: return getTimestampFast(columnIndex,
128: this .internalRowData[columnIndex], 0,
129: columnValue.length, targetCalendar, tz, rollForward,
130: conn, rs);
131: }
132:
133: public double getNativeDouble(int columnIndex) throws SQLException {
134: if (this .internalRowData[columnIndex] == null) {
135: return 0;
136: }
137:
138: return getNativeDouble(this .internalRowData[columnIndex], 0);
139: }
140:
141: public float getNativeFloat(int columnIndex) throws SQLException {
142: if (this .internalRowData[columnIndex] == null) {
143: return 0;
144: }
145:
146: return getNativeFloat(this .internalRowData[columnIndex], 0);
147: }
148:
149: public int getNativeInt(int columnIndex) throws SQLException {
150: if (this .internalRowData[columnIndex] == null) {
151: return 0;
152: }
153:
154: return getNativeInt(this .internalRowData[columnIndex], 0);
155: }
156:
157: public long getNativeLong(int columnIndex) throws SQLException {
158: if (this .internalRowData[columnIndex] == null) {
159: return 0;
160: }
161:
162: return getNativeLong(this .internalRowData[columnIndex], 0);
163: }
164:
165: public short getNativeShort(int columnIndex) throws SQLException {
166: if (this .internalRowData[columnIndex] == null) {
167: return 0;
168: }
169:
170: return getNativeShort(this .internalRowData[columnIndex], 0);
171: }
172:
173: public Timestamp getNativeTimestamp(int columnIndex,
174: Calendar targetCalendar, TimeZone tz, boolean rollForward,
175: ConnectionImpl conn, ResultSetImpl rs) throws SQLException {
176: byte[] bits = this .internalRowData[columnIndex];
177:
178: if (bits == null) {
179: return null;
180: }
181:
182: return getNativeTimestamp(bits, 0, bits.length, targetCalendar,
183: tz, rollForward, conn, rs);
184: }
185:
186: public void closeOpenStreams() {
187: // no-op for this type
188: }
189:
190: public InputStream getBinaryInputStream(int columnIndex)
191: throws SQLException {
192: if (this .internalRowData[columnIndex] == null) {
193: return null;
194: }
195:
196: return new ByteArrayInputStream(
197: this .internalRowData[columnIndex]);
198: }
199:
200: public Reader getReader(int columnIndex) throws SQLException {
201: InputStream stream = getBinaryInputStream(columnIndex);
202:
203: if (stream == null) {
204: return null;
205: }
206:
207: try {
208: return new InputStreamReader(stream,
209: this .metadata[columnIndex].getCharacterSet());
210: } catch (UnsupportedEncodingException e) {
211: SQLException sqlEx = SQLError.createSQLException("");
212:
213: sqlEx.initCause(e);
214:
215: throw sqlEx;
216: }
217: }
218:
219: public Time getTimeFast(int columnIndex, Calendar targetCalendar,
220: TimeZone tz, boolean rollForward, ConnectionImpl conn,
221: ResultSetImpl rs) throws SQLException {
222: byte[] columnValue = this .internalRowData[columnIndex];
223:
224: if (columnValue == null) {
225: return null;
226: }
227:
228: return getTimeFast(columnIndex,
229: this .internalRowData[columnIndex], 0,
230: columnValue.length, targetCalendar, tz, rollForward,
231: conn, rs);
232: }
233:
234: public Date getDateFast(int columnIndex, ConnectionImpl conn,
235: ResultSetImpl rs) throws SQLException {
236: byte[] columnValue = this .internalRowData[columnIndex];
237:
238: if (columnValue == null) {
239: return null;
240: }
241:
242: return getDateFast(columnIndex,
243: this .internalRowData[columnIndex], 0,
244: columnValue.length, conn, rs);
245: }
246:
247: public Object getNativeDateTimeValue(int columnIndex,
248: Calendar targetCalendar, int jdbcType, int mysqlType,
249: TimeZone tz, boolean rollForward, ConnectionImpl conn,
250: ResultSetImpl rs) throws SQLException {
251: byte[] columnValue = this .internalRowData[columnIndex];
252:
253: if (columnValue == null) {
254: return null;
255: }
256:
257: return getNativeDateTimeValue(columnIndex, columnValue, 0,
258: columnValue.length, targetCalendar, jdbcType,
259: mysqlType, tz, rollForward, conn, rs);
260: }
261:
262: public Date getNativeDate(int columnIndex, ConnectionImpl conn,
263: ResultSetImpl rs) throws SQLException {
264: byte[] columnValue = this .internalRowData[columnIndex];
265:
266: if (columnValue == null) {
267: return null;
268: }
269:
270: return getNativeDate(columnIndex, columnValue, 0,
271: columnValue.length, conn, rs);
272: }
273:
274: public Time getNativeTime(int columnIndex, Calendar targetCalendar,
275: TimeZone tz, boolean rollForward, ConnectionImpl conn,
276: ResultSetImpl rs) throws SQLException {
277: byte[] columnValue = this .internalRowData[columnIndex];
278:
279: if (columnValue == null) {
280: return null;
281: }
282:
283: return getNativeTime(columnIndex, columnValue, 0,
284: columnValue.length, targetCalendar, tz, rollForward,
285: conn, rs);
286: }
287: }
|