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;
019:
020: import java.io.InputStream;
021: import java.io.Reader;
022: import java.io.Serializable;
023: import java.math.BigDecimal;
024: import java.sql.Array;
025: import java.sql.Blob;
026: import java.sql.Clob;
027: import java.sql.Connection;
028: import java.sql.Date;
029: import java.sql.Ref;
030: import java.sql.ResultSet;
031: import java.sql.SQLException;
032: import java.sql.Time;
033: import java.sql.Timestamp;
034: import java.util.Calendar;
035: import java.util.Hashtable;
036: import java.util.Map;
037: import java.util.Vector;
038:
039: import javax.sql.RowSet;
040: import javax.sql.RowSetEvent;
041: import javax.sql.RowSetListener;
042: import javax.sql.rowset.serial.SerialArray;
043: import javax.sql.rowset.serial.SerialBlob;
044: import javax.sql.rowset.serial.SerialClob;
045: import javax.sql.rowset.serial.SerialRef;
046:
047: public abstract class BaseRowSet implements Cloneable, Serializable {
048: private static final long serialVersionUID = 4886719666485113312L;
049:
050: public static final int UNICODE_STREAM_PARAM = 0;
051:
052: public static final int BINARY_STREAM_PARAM = 1;
053:
054: public static final int ASCII_STREAM_PARAM = 2;
055:
056: protected InputStream binaryStream;
057:
058: protected InputStream unicodeStream;
059:
060: protected InputStream asciiStream;
061:
062: protected Reader charStream;
063:
064: private String command;
065:
066: private String URL;
067:
068: private String dataSource;
069:
070: private int rowSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
071:
072: private boolean showDeleted;
073:
074: private int queryTimeout;
075:
076: private int maxRows;
077:
078: private int maxFieldSize;
079:
080: private int concurrency = ResultSet.CONCUR_UPDATABLE;
081:
082: //compatiable with RI, default: true
083: private boolean readOnly = true;
084:
085: private boolean escapeProcessing;
086:
087: private int isolation;
088:
089: private int fetchDir = ResultSet.FETCH_FORWARD;
090:
091: private int fetchSize;
092:
093: private Map<String, Class<?>> map;
094:
095: private Vector<RowSetListener> listeners;
096:
097: private Hashtable<Object, Object> params;
098:
099: private transient String username;
100:
101: private transient String password;
102:
103: public BaseRowSet() {
104: super ();
105: listeners = new Vector<RowSetListener>();
106: }
107:
108: protected void initParams() {
109: params = new Hashtable<Object, Object>();
110: }
111:
112: public void addRowSetListener(RowSetListener listener) {
113: if (listener == null) {
114: return;
115: }
116: listeners.add(listener);
117: }
118:
119: public void removeRowSetListener(RowSetListener listener) {
120: if (listener == null) {
121: return;
122: }
123: listeners.remove(listener);
124: }
125:
126: protected void notifyCursorMoved() throws SQLException {
127: if (!(this instanceof RowSet)) {
128: throw new SQLException();
129: }
130: if (listeners.isEmpty()) {
131: return;
132: }
133: for (RowSetListener listener : listeners) {
134: listener.cursorMoved(new RowSetEvent((RowSet) this ));
135: }
136: }
137:
138: protected void notifyRowChanged() throws SQLException {
139: if (!(this instanceof RowSet)) {
140: throw new SQLException();
141: }
142: if (listeners.isEmpty()) {
143: return;
144: }
145: for (RowSetListener listener : listeners) {
146: listener.rowChanged(new RowSetEvent((RowSet) this ));
147: }
148: }
149:
150: protected void notifyRowSetChanged() throws SQLException {
151: if (!(this instanceof RowSet)) {
152: throw new SQLException();
153: }
154: if (listeners.isEmpty()) {
155: return;
156: }
157: for (RowSetListener listener : listeners) {
158: listener.rowSetChanged(new RowSetEvent((RowSet) this ));
159: }
160: }
161:
162: public String getCommand() {
163: return command;
164: }
165:
166: public void setCommand(String cmd) throws SQLException {
167: // null is allowed, but empty is not
168: if (cmd != null && cmd.length() == 0) {
169: throw new SQLException();
170: }
171: this .command = cmd;
172: }
173:
174: public String getUrl() throws SQLException {
175: // TODO interrogate the DataSource
176: return URL;
177: }
178:
179: public void setUrl(String url) throws SQLException {
180: // null is allowed, but empty is not
181: if (url != null && url.length() == 0) {
182: throw new SQLException();
183: }
184: this .URL = url;
185: this .dataSource = null;
186: }
187:
188: public String getDataSourceName() {
189: return dataSource;
190: }
191:
192: public void setDataSourceName(String name) throws SQLException {
193: // null is allowed, but empty is not
194: if (name != null && name.length() == 0) {
195: throw new SQLException();
196: }
197: this .dataSource = name;
198: this .URL = null;
199: }
200:
201: public String getUsername() {
202: return username;
203: }
204:
205: public void setUsername(String username) {
206: this .username = username;
207: }
208:
209: public String getPassword() {
210: return password;
211: }
212:
213: public void setPassword(String password) {
214: this .password = password;
215: }
216:
217: public void setType(int type) throws SQLException {
218: switch (type) {
219: case ResultSet.TYPE_FORWARD_ONLY:
220: case ResultSet.TYPE_SCROLL_INSENSITIVE:
221: case ResultSet.TYPE_SCROLL_SENSITIVE: {
222: this .rowSetType = type;
223: return;
224: }
225: default: {
226: throw new SQLException();
227: }
228: }
229: }
230:
231: public int getType() throws SQLException {
232: return rowSetType;
233: }
234:
235: public void setConcurrency(int concurrency) throws SQLException {
236: switch (concurrency) {
237: case ResultSet.CONCUR_READ_ONLY:
238: case ResultSet.CONCUR_UPDATABLE: {
239: this .concurrency = concurrency;
240: return;
241: }
242: default: {
243: throw new SQLException();
244: }
245: }
246: }
247:
248: public int getConcurrency() throws SQLException {
249: return concurrency;
250: }
251:
252: public boolean isReadOnly() {
253: return readOnly;
254: }
255:
256: public void setReadOnly(boolean value) {
257: this .readOnly = value;
258: }
259:
260: public int getTransactionIsolation() {
261: return isolation;
262: }
263:
264: public void setTransactionIsolation(int level) throws SQLException {
265: switch (level) {
266: case Connection.TRANSACTION_NONE:
267: case Connection.TRANSACTION_READ_UNCOMMITTED:
268: case Connection.TRANSACTION_READ_COMMITTED:
269: case Connection.TRANSACTION_REPEATABLE_READ:
270: case Connection.TRANSACTION_SERIALIZABLE: {
271: this .isolation = level;
272: return;
273: }
274: default: {
275: throw new SQLException();
276: }
277: }
278: }
279:
280: public Map<String, Class<?>> getTypeMap() {
281: return map;
282: }
283:
284: public void setTypeMap(Map<String, Class<?>> map) {
285: this .map = map;
286: }
287:
288: public int getMaxFieldSize() throws SQLException {
289: return maxFieldSize;
290: }
291:
292: public void setMaxFieldSize(int max) throws SQLException {
293: // TODO test maximum based on field type
294: this .maxFieldSize = max;
295: }
296:
297: public int getMaxRows() throws SQLException {
298: return maxRows;
299: }
300:
301: public void setMaxRows(int max) throws SQLException {
302: this .maxRows = max;
303: }
304:
305: public void setEscapeProcessing(boolean enable) throws SQLException {
306: this .escapeProcessing = enable;
307: }
308:
309: public boolean getEscapeProcessing() throws SQLException {
310: return escapeProcessing;
311: }
312:
313: public int getQueryTimeout() throws SQLException {
314: return queryTimeout;
315: }
316:
317: public void setQueryTimeout(int seconds) throws SQLException {
318: if (seconds < 0) {
319: throw new SQLException();
320: }
321: this .queryTimeout = seconds;
322: }
323:
324: public boolean getShowDeleted() throws SQLException {
325: return showDeleted;
326: }
327:
328: public void setShowDeleted(boolean value) throws SQLException {
329: this .showDeleted = value;
330: }
331:
332: public void setFetchDirection(int direction) throws SQLException {
333: switch (direction) {
334: case ResultSet.FETCH_REVERSE:
335: case ResultSet.FETCH_UNKNOWN: {
336: if (rowSetType == ResultSet.TYPE_FORWARD_ONLY) {
337: throw new SQLException();
338: }
339: }
340: case ResultSet.FETCH_FORWARD: {
341: this .fetchDir = direction;
342: return;
343: }
344: default: {
345: throw new SQLException();
346: }
347: }
348: }
349:
350: public int getFetchDirection() throws SQLException {
351: return fetchDir;
352: }
353:
354: public void setFetchSize(int rows) throws SQLException {
355: if (rows < 0) {
356: throw new SQLException();
357: }
358: if (rows > maxRows) {
359: throw new SQLException();
360: }
361: this .fetchSize = rows;
362: }
363:
364: public int getFetchSize() throws SQLException {
365: return fetchSize;
366: }
367:
368: public void setNull(int parameterIndex, int sqlType)
369: throws SQLException {
370: if (parameterIndex < 1) {
371: throw new SQLException();
372: }
373: if (params == null) {
374: throw new SQLException();
375: }
376: Object[] value = new Object[2];
377: value[1] = Integer.valueOf(sqlType);
378: params.put(Integer.valueOf(parameterIndex - 1), value);
379: }
380:
381: public void setNull(int parameterIndex, int sqlType, String typeName)
382: throws SQLException {
383: if (parameterIndex < 1) {
384: throw new SQLException();
385: }
386: if (params == null) {
387: throw new SQLException();
388: }
389: Object[] value = new Object[3];
390: value[1] = Integer.valueOf(sqlType);
391: value[2] = typeName;
392: params.put(Integer.valueOf(parameterIndex - 1), value);
393: }
394:
395: public void setBoolean(int parameterIndex, boolean x)
396: throws SQLException {
397: if (parameterIndex < 1) {
398: throw new SQLException();
399: }
400: if (params == null) {
401: throw new SQLException();
402: }
403: params.put(Integer.valueOf(parameterIndex - 1), Boolean
404: .valueOf(x));
405: }
406:
407: public void setByte(int parameterIndex, byte x) throws SQLException {
408: if (parameterIndex < 1) {
409: throw new SQLException();
410: }
411: if (params == null) {
412: throw new SQLException();
413: }
414: params
415: .put(Integer.valueOf(parameterIndex - 1), Byte
416: .valueOf(x));
417: }
418:
419: public void setShort(int parameterIndex, short x)
420: throws SQLException {
421: if (parameterIndex < 1) {
422: throw new SQLException();
423: }
424: if (params == null) {
425: throw new SQLException();
426: }
427: params.put(Integer.valueOf(parameterIndex - 1), Short
428: .valueOf(x));
429: }
430:
431: public void setInt(int parameterIndex, int x) throws SQLException {
432: if (parameterIndex < 1) {
433: throw new SQLException();
434: }
435: if (params == null) {
436: throw new SQLException();
437: }
438: params.put(Integer.valueOf(parameterIndex - 1), Integer
439: .valueOf(x));
440: }
441:
442: public void setLong(int parameterIndex, long x) throws SQLException {
443: if (parameterIndex < 1) {
444: throw new SQLException();
445: }
446: if (params == null) {
447: throw new SQLException();
448: }
449: params
450: .put(Integer.valueOf(parameterIndex - 1), Long
451: .valueOf(x));
452: }
453:
454: public void setFloat(int parameterIndex, float x)
455: throws SQLException {
456: if (parameterIndex < 1) {
457: throw new SQLException();
458: }
459: if (params == null) {
460: throw new SQLException();
461: }
462: params.put(Integer.valueOf(parameterIndex - 1), Float
463: .valueOf(x));
464: }
465:
466: public void setDouble(int parameterIndex, double x)
467: throws SQLException {
468: if (parameterIndex < 1) {
469: throw new SQLException();
470: }
471: if (params == null) {
472: throw new SQLException();
473: }
474: params.put(Integer.valueOf(parameterIndex - 1), Double
475: .valueOf(x));
476: }
477:
478: public void setBigDecimal(int parameterIndex, BigDecimal x)
479: throws SQLException {
480: if (parameterIndex < 1) {
481: throw new SQLException();
482: }
483: if (params == null) {
484: throw new SQLException();
485: }
486: params.put(Integer.valueOf(parameterIndex - 1), x);
487: }
488:
489: public void setString(int parameterIndex, String x)
490: throws SQLException {
491: if (parameterIndex < 1) {
492: throw new SQLException();
493: }
494: if (params == null) {
495: throw new SQLException();
496: }
497: params.put(Integer.valueOf(parameterIndex - 1), x);
498: }
499:
500: public void setBytes(int parameterIndex, byte[] x)
501: throws SQLException {
502: if (parameterIndex < 1) {
503: throw new SQLException();
504: }
505: if (params == null) {
506: throw new SQLException();
507: }
508: params.put(Integer.valueOf(parameterIndex - 1), x);
509: }
510:
511: public void setDate(int parameterIndex, Date x) throws SQLException {
512: if (parameterIndex < 1) {
513: throw new SQLException();
514: }
515: if (params == null) {
516: throw new SQLException();
517: }
518: params.put(Integer.valueOf(parameterIndex - 1), x);
519: }
520:
521: public void setTime(int parameterIndex, Time x) throws SQLException {
522: if (parameterIndex < 1) {
523: throw new SQLException();
524: }
525: if (params == null) {
526: throw new SQLException();
527: }
528: params.put(Integer.valueOf(parameterIndex - 1), x);
529: }
530:
531: public void setTimestamp(int parameterIndex, Timestamp x)
532: throws SQLException {
533: if (parameterIndex < 1) {
534: throw new SQLException();
535: }
536: if (params == null) {
537: throw new SQLException();
538: }
539: params.put(Integer.valueOf(parameterIndex - 1), x);
540: }
541:
542: public void setAsciiStream(int parameterIndex, InputStream x,
543: int length) throws SQLException {
544: if (parameterIndex < 1) {
545: throw new SQLException();
546: }
547: if (params == null) {
548: throw new SQLException();
549: }
550: Object[] value = new Object[3];
551: value[0] = x;
552: value[1] = Integer.valueOf(length);
553: value[2] = Integer.valueOf(ASCII_STREAM_PARAM);
554: params.put(Integer.valueOf(parameterIndex - 1), value);
555: }
556:
557: public void setBinaryStream(int parameterIndex, InputStream x,
558: int length) throws SQLException {
559: if (parameterIndex < 1) {
560: throw new SQLException();
561: }
562: if (params == null) {
563: throw new SQLException();
564: }
565: Object[] value = new Object[3];
566: value[0] = x;
567: value[1] = Integer.valueOf(length);
568: value[2] = Integer.valueOf(BINARY_STREAM_PARAM);
569: params.put(Integer.valueOf(parameterIndex - 1), value);
570: }
571:
572: @Deprecated
573: public void setUnicodeStream(int parameterIndex, InputStream x,
574: int length) throws SQLException {
575: if (parameterIndex < 1) {
576: throw new SQLException();
577: }
578: if (params == null) {
579: throw new SQLException();
580: }
581: Object[] value = new Object[3];
582: value[0] = x;
583: value[1] = Integer.valueOf(length);
584: value[2] = Integer.valueOf(UNICODE_STREAM_PARAM);
585: params.put(Integer.valueOf(parameterIndex - 1), value);
586: }
587:
588: public void setCharacterStream(int parameterIndex, Reader reader,
589: int length) throws SQLException {
590: if (parameterIndex < 1) {
591: throw new SQLException();
592: }
593: if (params == null) {
594: throw new SQLException();
595: }
596: Object[] value = new Object[2];
597: value[0] = reader;
598: value[1] = Integer.valueOf(length);
599: params.put(Integer.valueOf(parameterIndex - 1), value);
600: }
601:
602: public void setObject(int parameterIndex, Object x,
603: int targetSqlType, int scale) throws SQLException {
604: if (parameterIndex < 1) {
605: throw new SQLException();
606: }
607: if (params == null) {
608: throw new SQLException();
609: }
610: Object[] value = new Object[3];
611: value[0] = x;
612: value[1] = Integer.valueOf(targetSqlType);
613: value[2] = Integer.valueOf(scale);
614: params.put(Integer.valueOf(parameterIndex - 1), value);
615: }
616:
617: public void setObject(int parameterIndex, Object x,
618: int targetSqlType) throws SQLException {
619: if (parameterIndex < 1) {
620: throw new SQLException();
621: }
622: if (params == null) {
623: throw new SQLException();
624: }
625: Object[] value = new Object[2];
626: value[0] = x;
627: value[1] = Integer.valueOf(targetSqlType);
628: params.put(Integer.valueOf(parameterIndex - 1), value);
629: }
630:
631: public void setObject(int parameterIndex, Object x)
632: throws SQLException {
633: if (parameterIndex < 1) {
634: throw new SQLException();
635: }
636: if (params == null) {
637: throw new SQLException();
638: }
639: params.put(Integer.valueOf(parameterIndex - 1), x);
640: }
641:
642: public void setRef(int parameterIndex, Ref ref) throws SQLException {
643: if (parameterIndex < 1) {
644: throw new SQLException();
645: }
646: if (params == null) {
647: throw new SQLException();
648: }
649: params.put(Integer.valueOf(parameterIndex - 1), new SerialRef(
650: ref));
651: }
652:
653: public void setBlob(int parameterIndex, Blob x) throws SQLException {
654: if (parameterIndex < 1) {
655: throw new SQLException();
656: }
657: if (params == null) {
658: throw new SQLException();
659: }
660: params.put(Integer.valueOf(parameterIndex - 1), new SerialBlob(
661: x));
662: }
663:
664: public void setClob(int parameterIndex, Clob x) throws SQLException {
665: if (parameterIndex < 1) {
666: throw new SQLException();
667: }
668: if (params == null) {
669: throw new SQLException();
670: }
671: params.put(Integer.valueOf(parameterIndex - 1), new SerialClob(
672: x));
673: }
674:
675: public void setArray(int parameterIndex, Array array)
676: throws SQLException {
677: if (parameterIndex < 1) {
678: throw new SQLException();
679: }
680: if (params == null) {
681: throw new SQLException();
682: }
683: params.put(Integer.valueOf(parameterIndex - 1),
684: new SerialArray(array));
685: }
686:
687: public void setDate(int parameterIndex, Date x, Calendar cal)
688: throws SQLException {
689: if (parameterIndex < 1) {
690: throw new SQLException();
691: }
692: if (params == null) {
693: throw new SQLException();
694: }
695: Object[] value = new Object[2];
696: value[0] = x;
697: value[1] = cal;
698: params.put(Integer.valueOf(parameterIndex - 1), value);
699: }
700:
701: public void setTime(int parameterIndex, Time x, Calendar cal)
702: throws SQLException {
703: if (parameterIndex < 1) {
704: throw new SQLException();
705: }
706: if (params == null) {
707: throw new SQLException();
708: }
709: Object[] value = new Object[2];
710: value[0] = x;
711: value[1] = cal;
712: params.put(Integer.valueOf(parameterIndex - 1), value);
713: }
714:
715: public void setTimestamp(int parameterIndex, Timestamp x,
716: Calendar cal) throws SQLException {
717: if (parameterIndex < 1) {
718: throw new SQLException();
719: }
720: if (params == null) {
721: throw new SQLException();
722: }
723: Object[] value = new Object[2];
724: value[0] = x;
725: value[1] = cal;
726: params.put(Integer.valueOf(parameterIndex - 1), value);
727: }
728:
729: public void clearParameters() throws SQLException {
730: if (params == null) {
731: return;
732: }
733: params.clear();
734: }
735:
736: public Object[] getParams() throws SQLException {
737: if (params == null) {
738: return new Object[0];
739: }
740: Object[] result = new Object[params.size()];
741: for (int i = 0; i < result.length; i++) {
742: Object param = params.get(Integer.valueOf(i));
743: if (param == null) {
744: throw new SQLException();
745: }
746: result[i] = param;
747: }
748: return result;
749: }
750:
751: public BaseRowSet clone() throws CloneNotSupportedException {
752: BaseRowSet result = (BaseRowSet) super.clone();
753: return result;
754:
755: }
756: }
|