001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.sql.rowset.serial;
019:
020: import java.io.InputStream;
021: import java.io.Reader;
022: import java.math.BigDecimal;
023: import java.net.URL;
024: import java.sql.Array;
025: import java.sql.Blob;
026: import java.sql.Clob;
027: import java.sql.Date;
028: import java.sql.Ref;
029: import java.sql.SQLData;
030: import java.sql.SQLException;
031: import java.sql.SQLInput;
032: import java.sql.Struct;
033: import java.sql.Time;
034: import java.sql.Timestamp;
035: import java.util.Map;
036:
037: import org.apache.harmony.luni.util.NotImplementedException;
038: import org.apache.harmony.sql.internal.nls.Messages;
039:
040: /**
041: * A concrete implementation of SQLInput. The readXXX methods will be called by
042: * SQLData.readSQL, which read different objects such as Array, BigDecimal from
043: * this SQLInputImpl object.
044: *
045: * Different JDBC drivers may have their own implementation of SQLInput and
046: * won't use this class.
047: */
048: public class SQLInputImpl implements SQLInput {
049:
050: private Object[] attributes;
051:
052: private Map<String, Class<?>> map;
053:
054: private int readPosition = 0;
055:
056: /**
057: * Constructs a new SQLInputImpl object using an array of attributes and a
058: * custom name-type map.
059: *
060: * @param attributes -
061: * the array of given attribute objects.
062: * @param map -
063: * the UDT(user defined type) name-type map
064: * @throws SQLException -
065: * if the attributes or the map is null
066: */
067: public SQLInputImpl(Object[] attributes, Map<String, Class<?>> map)
068: throws SQLException {
069: if (null == attributes || null == map) {
070: throw new SQLException(Messages.getString("sql.34")); //$NON-NLS-1$
071: }
072: this .attributes = attributes;
073: this .map = map;
074: }
075:
076: /**
077: * {@inheritDoc}
078: *
079: * @see java.sql.SQLInput#readArray()
080: */
081: public Array readArray() throws SQLException {
082: if (readPosition >= attributes.length) {
083: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
084: }
085: Object o = attributes[readPosition++];
086: return (Array) o;
087: }
088:
089: /**
090: * {@inheritDoc}
091: *
092: * @see java.sql.SQLInput#readAsciiStream()
093: */
094: public InputStream readAsciiStream() throws SQLException {
095: if (readPosition >= attributes.length) {
096: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
097: }
098: Object o = attributes[readPosition++];
099: return (InputStream) o;
100: }
101:
102: /**
103: * {@inheritDoc}
104: *
105: * @see java.sql.SQLInput#readBigDecimal()
106: */
107: public BigDecimal readBigDecimal() throws SQLException {
108: if (readPosition >= attributes.length) {
109: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
110: }
111: Object o = attributes[readPosition++];
112: return (BigDecimal) o;
113: }
114:
115: /**
116: * {@inheritDoc}
117: *
118: * @see java.sql.SQLInput#readBinaryStream()
119: */
120: public InputStream readBinaryStream() throws SQLException {
121: if (readPosition >= attributes.length) {
122: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
123: }
124: Object o = attributes[readPosition++];
125: return (InputStream) o;
126: }
127:
128: /**
129: * {@inheritDoc}
130: *
131: * @see java.sql.SQLInput#readBlob()
132: */
133: public Blob readBlob() throws SQLException {
134: if (readPosition >= attributes.length) {
135: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
136: }
137: Object o = attributes[readPosition++];
138: return (Blob) o;
139: }
140:
141: /**
142: * {@inheritDoc}
143: *
144: * @see java.sql.SQLInput#readBoolean()
145: */
146: public boolean readBoolean() throws SQLException {
147: if (readPosition >= attributes.length) {
148: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
149: }
150: Object o = attributes[readPosition++];
151: return o == null ? false : ((Boolean) o).booleanValue();
152: }
153:
154: /**
155: * {@inheritDoc}
156: *
157: * @see java.sql.SQLInput#readByte()
158: */
159: public byte readByte() throws SQLException {
160: if (readPosition >= attributes.length) {
161: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
162: }
163: Object o = attributes[readPosition++];
164: return o == null ? (byte) 0 : ((Byte) o).byteValue();
165: }
166:
167: /**
168: * {@inheritDoc}
169: *
170: * @see java.sql.SQLInput#readBytes()
171: */
172: public byte[] readBytes() throws SQLException {
173: if (readPosition >= attributes.length) {
174: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
175: }
176: Object o = attributes[readPosition++];
177: return (byte[]) o;
178: }
179:
180: /**
181: * {@inheritDoc}
182: *
183: * @see java.sql.SQLInput#readCharacterStream()
184: */
185: public Reader readCharacterStream() throws SQLException {
186: if (readPosition >= attributes.length) {
187: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
188: }
189: Object o = attributes[readPosition++];
190: return (Reader) o;
191: }
192:
193: /**
194: * {@inheritDoc}
195: *
196: * @see java.sql.SQLInput#readClob()
197: */
198: public Clob readClob() throws SQLException {
199: if (readPosition >= attributes.length) {
200: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
201: }
202: Object o = attributes[readPosition++];
203: return (Clob) o;
204: }
205:
206: /**
207: * {@inheritDoc}
208: *
209: * @see java.sql.SQLInput#readDate()
210: */
211: public Date readDate() throws SQLException {
212: if (readPosition >= attributes.length) {
213: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
214: }
215: Object o = attributes[readPosition++];
216: return (Date) o;
217: }
218:
219: /**
220: * {@inheritDoc}
221: *
222: * @see java.sql.SQLInput#readDouble()
223: */
224: public double readDouble() throws SQLException {
225: if (readPosition >= attributes.length) {
226: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
227: }
228: Object o = attributes[readPosition++];
229: return o == null ? 0 : ((Double) o).doubleValue();
230: }
231:
232: /**
233: * {@inheritDoc}
234: *
235: * @see java.sql.SQLInput#readFloat()
236: */
237: public float readFloat() throws SQLException {
238: if (readPosition >= attributes.length) {
239: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
240: }
241: Object o = attributes[readPosition++];
242: return o == null ? 0f : ((Float) o).floatValue();
243: }
244:
245: /**
246: * {@inheritDoc}
247: *
248: * @see java.sql.SQLInput#readInt()
249: */
250: public int readInt() throws SQLException {
251: if (readPosition >= attributes.length) {
252: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
253: }
254: Object o = attributes[readPosition++];
255: return o == null ? 0 : ((Integer) o).intValue();
256: }
257:
258: /**
259: * {@inheritDoc}
260: *
261: * @see java.sql.SQLInput#readLong()
262: */
263: public long readLong() throws SQLException {
264: if (readPosition >= attributes.length) {
265: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
266: }
267: Object o = attributes[readPosition++];
268: return o == null ? 0 : ((Long) o).longValue();
269: }
270:
271: /**
272: * {@inheritDoc}
273: *
274: * @see java.sql.SQLInput#readObject()
275: */
276: public Object readObject() throws SQLException {
277: if (readPosition >= attributes.length) {
278: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
279: }
280: Object o = attributes[readPosition++];
281: if (o instanceof Struct) {
282: Struct structuredType = (Struct) o;
283: String typeName = structuredType.getSQLTypeName();
284: Class<?> c = map.get(typeName);
285: if (c != null) {
286: try {
287: SQLData data = (SQLData) c.newInstance();
288: SQLInputImpl input = new SQLInputImpl(
289: structuredType.getAttributes(), map);
290: data.readSQL(input, typeName);
291: return data;
292: } catch (IllegalAccessException e) {
293: throw new SQLException(e.getMessage());
294: } catch (InstantiationException e) {
295: throw new SQLException(e.getMessage());
296: }
297:
298: }
299: }
300: return o;
301: }
302:
303: /**
304: * {@inheritDoc}
305: *
306: * @see java.sql.SQLInput#readRef()
307: */
308: public Ref readRef() throws SQLException {
309: if (readPosition >= attributes.length) {
310: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
311: }
312: Object o = attributes[readPosition++];
313: return (Ref) o;
314: }
315:
316: /**
317: * {@inheritDoc}
318: *
319: * @see java.sql.SQLInput#readShort()
320: */
321: public short readShort() throws SQLException {
322: if (readPosition >= attributes.length) {
323: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
324: }
325: Object o = attributes[readPosition++];
326: return o == null ? (short) 0 : ((Short) o).shortValue();
327: }
328:
329: /**
330: * {@inheritDoc}
331: *
332: * @see java.sql.SQLInput#readString()
333: */
334: public String readString() throws SQLException {
335: if (readPosition >= attributes.length) {
336: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
337: }
338: Object o = attributes[readPosition++];
339: return (String) o;
340: }
341:
342: /**
343: * {@inheritDoc}
344: *
345: * @see java.sql.SQLInput#readTime()
346: */
347: public Time readTime() throws SQLException {
348: if (readPosition >= attributes.length) {
349: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
350: }
351: Object o = attributes[readPosition++];
352: return (Time) o;
353: }
354:
355: /**
356: * {@inheritDoc}
357: *
358: * @see java.sql.SQLInput#readTimestamp()
359: */
360: public Timestamp readTimestamp() throws SQLException {
361: if (readPosition >= attributes.length) {
362: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
363: }
364: Object o = attributes[readPosition++];
365: return (Timestamp) o;
366: }
367:
368: /**
369: * {@inheritDoc}
370: *
371: * @see java.sql.SQLInput#readURL()
372: */
373: public URL readURL() throws SQLException {
374: if (readPosition >= attributes.length) {
375: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
376: }
377: throw new SQLException(Messages.getString("sql.37")); //$NON-NLS-1$
378: }
379:
380: /**
381: * {@inheritDoc}
382: *
383: * @see java.sql.SQLInput#wasNull()
384: */
385: public boolean wasNull() throws SQLException {
386: if (readPosition > attributes.length) {
387: throw new SQLException(Messages.getString("sql.35")); //$NON-NLS-1$
388: }
389: return readPosition == 0 ? false
390: : attributes[readPosition - 1] == null;
391: }
392:
393: }
|