KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > in > co > daffodil > db > jdbc > TempResultSet


1 package in.co.daffodil.db.jdbc;
2
3 import java.sql.*;
4 import com.daffodilwoods.database.resource.*;
5 import java.util.HashMap JavaDoc;
6 import java.util.TreeMap JavaDoc;
7 import java.util.Calendar JavaDoc;
8 import java.math.BigDecimal JavaDoc;
9 import java.io.*;
10 import com.daffodilwoods.database.general.*;
11 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
12 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.Datatype;
13 import com.daffodilwoods.daffodildb.server.serversystem.*;
14 import com.daffodilwoods.daffodildb.client.*;
15 import in.co.daffodil.db.general.*;
16
17 public class TempResultSet implements ResultSet{
18
19     private Object JavaDoc[][] objs;
20     TreeMap JavaDoc columnNamesMap;
21     private ResultSetMetaData metaData;
22     DaffodilDBConnection connection;
23     private Navigator navigator;
24    int concurrency = CONCUR_READ_ONLY;
25    int type = TYPE_FORWARD_ONLY ;
26    int fetchSize = 0 ;
27    int fetchDirection = FETCH_FORWARD;
28    SQLWarning sqlWarning;
29    private DaffodilDBStatement statement;
30
31    public TempResultSet(Object JavaDoc[][] obj,String JavaDoc[] columnNames){
32           this.objs = obj;
33         columnNamesMap = new TreeMap JavaDoc(String.CASE_INSENSITIVE_ORDER);
34         for(int g = 0 ; g < columnNames.length ; g++)
35             columnNamesMap.put(columnNames[g],new Integer JavaDoc(g+1));
36    }
37
38    public boolean next() throws SQLException{
39         return navigator.next();
40    }
41
42    public void close() throws SQLException{
43       if( statement != null ) {
44          statement.resultSet = null;
45          statement=null;
46       }
47     }
48
49    public boolean wasNull() throws SQLException{
50       if( navigator.lastRetrievedColumn == -1 ) {
51          DException dex = new DException("DSE725", null);
52          throw dex.getSqlException(connection.getLocale());
53       }
54       return navigator.lastRetrievedColumnValue == null;
55    }
56
57    public String JavaDoc getString(int columnIndex) throws SQLException{
58       checkValidRow();
59       checkValidColumn(columnIndex);
60       navigator.lastRetrievedColumn = columnIndex;
61       Object JavaDoc columnValue = getObject(columnIndex,Types.VARCHAR);
62       navigator.lastRetrievedColumnValue = columnValue == null ? null : columnValue;
63       return columnValue==null ? null : columnValue.toString();
64    }
65
66    public boolean getBoolean(int columnIndex) throws SQLException{
67       checkValidRow();
68       checkValidColumn(columnIndex);
69       navigator.lastRetrievedColumn = columnIndex;
70       Boolean JavaDoc columnValue = (Boolean JavaDoc)getObject(columnIndex,16);
71       navigator.lastRetrievedColumnValue = columnValue == null ? null : columnValue;
72       return columnValue == null ? false : columnValue.booleanValue();
73    }
74
75    public byte getByte(int columnIndex) throws SQLException{
76       checkValidRow();
77       checkValidColumn(columnIndex);
78       navigator.lastRetrievedColumn = columnIndex;
79       Integer JavaDoc columnValue = (Integer JavaDoc)getObject(columnIndex,Types.TINYINT );
80       navigator.lastRetrievedColumnValue = columnValue == null ? null : columnValue;
81       return columnValue == null ? (byte)0 : columnValue.byteValue();
82    }
83
84    public short getShort(int columnIndex) throws SQLException{
85       checkValidRow();
86       checkValidColumn(columnIndex);
87       navigator.lastRetrievedColumn = columnIndex;
88       Integer JavaDoc shortValue = (Integer JavaDoc)getObject(columnIndex,Types.SMALLINT);
89       navigator.lastRetrievedColumnValue = shortValue == null ? null : shortValue;
90       return shortValue == null ? (short)0 : shortValue.shortValue();
91    }
92
93    public int getInt(int columnIndex) throws SQLException{
94       checkValidRow();
95       checkValidColumn(columnIndex);
96       navigator.lastRetrievedColumn = columnIndex;
97       Integer JavaDoc intValue = (Integer JavaDoc)getObject(columnIndex,Types.INTEGER);
98       navigator.lastRetrievedColumnValue = intValue == null ? null : intValue;
99       return intValue == null ? 0 : intValue.intValue();
100    }
101
102    public long getLong(int columnIndex) throws SQLException{
103       checkValidRow();
104       checkValidColumn(columnIndex);
105       navigator.lastRetrievedColumn = columnIndex;
106       Long JavaDoc longValue = (Long JavaDoc)getObject(columnIndex,Types.BIGINT);
107       navigator.lastRetrievedColumnValue = longValue == null ? null : longValue;
108       return longValue == null ? 0l : longValue.longValue();
109    }
110
111    public float getFloat(int columnIndex) throws SQLException{
112       checkValidRow();
113       checkValidColumn(columnIndex);
114       navigator.lastRetrievedColumn = columnIndex;
115       Float JavaDoc floatValue = (Float JavaDoc)getObject(columnIndex,Types.REAL);
116       navigator.lastRetrievedColumnValue = floatValue == null ? null : floatValue;
117       return floatValue== null ? 0f : floatValue.floatValue();
118    }
119
120    public double getDouble(int columnIndex) throws SQLException{
121       checkValidRow();
122       checkValidColumn(columnIndex);
123       navigator.lastRetrievedColumn = columnIndex;
124       Double JavaDoc doubleValue = (Double JavaDoc)getObject(columnIndex,Types.DOUBLE);
125       navigator.lastRetrievedColumnValue = doubleValue == null ? null : doubleValue;
126       return doubleValue == null ? 0.0 : doubleValue.doubleValue();
127    }
128
129    public BigDecimal JavaDoc getBigDecimal(int columnIndex, int scale) throws SQLException{
130        checkValidRow();
131        checkValidColumn(columnIndex);
132        navigator.lastRetrievedColumn = columnIndex;
133        BigDecimal JavaDoc columnValue = (BigDecimal JavaDoc)getObject(columnIndex,Types.DECIMAL);
134        navigator.lastRetrievedColumnValue = columnValue == null ? null : columnValue;
135        if( columnValue == null || scale == -1 )
136            return columnValue;
137        return columnValue.setScale(scale);
138    }
139
140    public byte[] getBytes(int columnIndex) throws SQLException{
141       checkValidRow();
142       checkValidColumn(columnIndex);
143       navigator.lastRetrievedColumn = columnIndex;
144       Object JavaDoc columnValue = navigator.getObject(columnIndex);
145       navigator.lastRetrievedColumnValue = columnValue == null ? null : columnValue;
146       if(columnValue == null)
147           return null;
148       try{
149          byte[] bytes = (byte[])Utilities.convertObject(columnValue,Types.BINARY);
150          return bytes;
151       }catch(DException de){
152          throw de.getSqlException(connection.getLocale());
153       }
154    }
155
156    public java.sql.Date JavaDoc getDate(int columnIndex) throws SQLException{
157       return getDate(columnIndex,null);
158    }
159
160    public Time getTime(int columnIndex) throws SQLException{
161       return getTime(columnIndex,null);
162    }
163
164    public Timestamp getTimestamp(int columnIndex) throws SQLException{
165       return getTimestamp(columnIndex,null );
166    }
167
168    public InputStream getAsciiStream(int columnIndex) throws SQLException{
169       checkValidRow();
170       checkValidColumn(columnIndex);
171       navigator.lastRetrievedColumn = columnIndex;
172       byte[] bytes = getBytes(columnIndex);
173       if( bytes == null )
174          return null;
175       try{
176          byte[] unicodeBytes = Utilities.convertBytesToAsciiBytes(bytes);
177          ByteArrayInputStream in = new ByteArrayInputStream(unicodeBytes);
178          return in;
179       }catch(sun.io.MalformedInputException be){
180          be.printStackTrace();
181          DException dex = new DException("DSE297", null);
182          throw dex.getSqlException(connection.getLocale());
183       }
184    }
185
186    public java.io.InputStream JavaDoc getUnicodeStream(int columnIndex) throws SQLException{
187       checkValidRow();
188       checkValidColumn(columnIndex);
189       navigator.lastRetrievedColumn = columnIndex;
190       byte[] bytes = getBytes(columnIndex);
191       navigator.lastRetrievedColumnValue = bytes == null ? null : bytes;
192       if( bytes == null )
193           return null;
194       try{
195          byte[] unicodeBytes = Utilities.convertBytesToUnicodeBytes(bytes);
196          ByteArrayInputStream in = new ByteArrayInputStream(unicodeBytes);
197          return in;
198       }catch(sun.io.MalformedInputException e){
199          e.printStackTrace();
200          DException dex = new DException("DSE297", null);
201          throw dex.getSqlException(connection.getLocale());
202       }
203    }
204
205    public InputStream getBinaryStream(int columnIndex) throws SQLException{
206       checkValidRow();
207       checkValidColumn(columnIndex);
208       navigator.lastRetrievedColumn = columnIndex;
209       byte[] bytes = getBytes(columnIndex);
210       navigator.lastRetrievedColumnValue = bytes == null ? null : bytes;
211       if(bytes == null)
212           return null;
213       ByteArrayInputStream in = new ByteArrayInputStream(bytes);
214       return in;
215    }
216
217
218    public String JavaDoc getString(String JavaDoc columnName) throws SQLException{
219       return getString(findColumn(columnName) );
220    }
221
222    public boolean getBoolean(String JavaDoc columnName) throws SQLException{
223         return getBoolean( findColumn(columnName) );
224    }
225
226    public byte getByte(String JavaDoc columnName) throws SQLException{
227         return getByte( findColumn(columnName) );
228    }
229
230    public short getShort(String JavaDoc columnName) throws SQLException{
231         return getShort(findColumn(columnName));
232    }
233
234    public int getInt(String JavaDoc columnName) throws SQLException{
235         return getInt( findColumn(columnName) );
236    }
237
238    public long getLong(String JavaDoc columnName) throws SQLException{
239         return getLong( findColumn( columnName) );
240    }
241
242    public float getFloat(String JavaDoc columnName) throws SQLException{
243         return getFloat( findColumn(columnName) );
244    }
245
246    public double getDouble(String JavaDoc columnName) throws SQLException{
247         return getDouble( findColumn(columnName) );
248    }
249
250    public BigDecimal JavaDoc getBigDecimal(String JavaDoc columnName, int scale) throws SQLException{
251         return getBigDecimal(findColumn(columnName),scale);
252    }
253
254    public byte[] getBytes(String JavaDoc columnName) throws SQLException{
255         return getBytes( findColumn(columnName) );
256    }
257
258    public java.sql.Date JavaDoc getDate(String JavaDoc columnName) throws SQLException{
259       return getDate(findColumn(columnName),null);
260    }
261
262    public Time getTime(String JavaDoc columnName) throws SQLException{
263      return getTime( findColumn(columnName),null);
264    }
265
266    public java.sql.Timestamp JavaDoc getTimestamp(String JavaDoc columnName) throws SQLException{
267      return getTimestamp(findColumn(columnName),null);
268    }
269
270    public java.io.InputStream JavaDoc getAsciiStream(String JavaDoc columnName) throws SQLException{
271         return getAsciiStream(findColumn(columnName) );
272    }
273
274     public java.io.InputStream JavaDoc getUnicodeStream(String JavaDoc columnName) throws SQLException{
275         return getUnicodeStream(findColumn(columnName));
276     }
277
278     public InputStream getBinaryStream(String JavaDoc columnName)throws SQLException{
279         return getBinaryStream( findColumn(columnName) );
280     }
281
282     public SQLWarning getWarnings() throws SQLException{
283         return sqlWarning;
284     }
285
286     public void clearWarnings() throws SQLException{
287         sqlWarning = null;
288     }
289
290     public String JavaDoc getCursorName() throws SQLException{
291        DException dex = new DException("DSE16",new Object JavaDoc[]{"getCursorName"});
292        throw dex.getSqlException(connection.getLocale());
293     }
294
295    public ResultSetMetaData getMetaData() throws SQLException{
296       return metaData;
297    }
298
299    public Object JavaDoc getObject(int columnIndex) throws SQLException{
300       return getObject(columnIndex,null);
301    }
302
303    public Object JavaDoc getObject(String JavaDoc columnName) throws SQLException{
304       return getObject(findColumn(columnName),null);
305    }
306
307    public int findColumn(String JavaDoc columnName) throws SQLException{
308       Object JavaDoc obj = columnNamesMap.get(columnName);
309       if(obj == null)
310          throw new SQLException("ColumnName "+columnName +" Not Found "+columnNamesMap);
311       return obj.hashCode();
312    }
313
314    public Reader getCharacterStream(int columnIndex) throws SQLException{
315       checkValidRow();
316       checkValidColumn(columnIndex);
317       navigator.lastRetrievedColumn = columnIndex;
318       byte[] columnBytes = getBytes(columnIndex);
319       navigator.lastRetrievedColumnValue = columnBytes == null ? null : columnBytes;
320       if( columnBytes == null)
321           return null;
322       ByteArrayInputStream in = new ByteArrayInputStream(columnBytes);
323       return new InputStreamReader(in);
324    }
325
326     public Reader getCharacterStream(String JavaDoc columnName) throws SQLException{
327         return getCharacterStream(findColumn(columnName) );
328     }
329
330     public BigDecimal JavaDoc getBigDecimal(int columnIndex) throws SQLException{
331         return getBigDecimal(columnIndex,-1);
332     }
333
334     public BigDecimal JavaDoc getBigDecimal(String JavaDoc columnName) throws SQLException{
335         return getBigDecimal(findColumn(columnName),-1);
336     }
337
338     public boolean isBeforeFirst() throws SQLException{
339         return navigator.isBeforeFirst();
340     }
341
342     public boolean isAfterLast() throws SQLException{
343         return navigator.isAfterLast();
344     }
345
346     public boolean isFirst() throws SQLException{
347         return navigator.isFirst();
348     }
349
350     public boolean isLast() throws SQLException{
351         return navigator.isLast();
352     }
353
354     public void beforeFirst() throws SQLException{
355         navigator.beforeFirst();
356     }
357
358     public void afterLast() throws SQLException{
359         navigator.afterLast();
360     }
361
362     public boolean first() throws SQLException{
363         return navigator.first();
364     }
365
366     public boolean last() throws SQLException{
367         return navigator.last();
368     }
369
370     public int getRow() throws SQLException{
371         return navigator.getRow();
372     }
373
374     public boolean absolute( int row ) throws SQLException{
375         return navigator.absolute(row);
376     }
377
378     public boolean relative( int rows ) throws SQLException{
379         return navigator.relative(rows);
380     }
381     /**
382      * Moves the cursor to the previous row in this
383      * <code>ResultSet</code> object.
384      *
385      * @return <code>true</code> if the cursor is on a valid row;
386      * <code>false</code> if it is off the result set
387      * @exception SQLException if a database access error
388      * occurs or the result set type is <code>TYPE_FORWARD_ONLY</code>
389      * @since 1.2
390      */

391
392     public boolean previous() throws SQLException{
393         return navigator.previous();
394     }
395
396    public void setFetchDirection(int direction) throws SQLException{
397       /*SQLException if result set type is TYPE_FORWARD_ONLY and the fetch direction is not FETCH_FORWARD*/
398       if(direction != FETCH_FORWARD && direction != FETCH_REVERSE && direction != FETCH_UNKNOWN){
399           DException dex = new DException("DSE738", new Object JavaDoc[]{new Integer JavaDoc(direction)});
400           throw dex.getSqlException(connection.getLocale());
401       }
402       if(type == TYPE_FORWARD_ONLY && direction != FETCH_FORWARD ){
403           DException dex = new DException("DSE415", null);
404           throw dex.getSqlException(connection.getLocale());
405       }
406       fetchDirection = direction;
407    }
408
409     public int getFetchDirection() throws SQLException{
410       return fetchDirection;
411     }
412
413     public void setFetchSize(int rows) throws SQLException{
414         /*Exception in case 0 <= rows <= this.getMaxRows() */
415       if( rows < 0 || (statement.getMaxRows()!=0 && rows > statement.getMaxRows())) {
416          DException dex = new DException("DSE518", null);
417          throw dex.getSqlException(connection.getLocale());
418       }
419       fetchSize = rows;
420     }
421
422     public int getFetchSize() throws SQLException{
423       return fetchSize;
424     }
425
426     public int getType() throws SQLException{
427         return type;
428     }
429
430    public int getConcurrency() throws SQLException{
431       return concurrency;
432    }
433
434    public boolean rowUpdated() throws SQLException{
435       checkUpdatable();
436         return false;
437    }
438
439     public boolean rowInserted() throws SQLException{
440         checkUpdatable();
441         return false;
442     }
443
444     public boolean rowDeleted() throws SQLException{
445         checkUpdatable();
446         return false;
447     }
448
449     public void updateNull(int columnIndex) throws SQLException{
450          checkValidRow();
451          checkUpdatable();
452          checkValidColumn(columnIndex);
453     }
454
455     public void updateBoolean(int columnIndex, boolean x) throws SQLException{
456          checkValidRow();
457          checkUpdatable();
458          checkValidColumn(columnIndex);
459     }
460
461     public void updateByte(int columnIndex, byte x) throws SQLException{
462          checkValidRow();
463          checkUpdatable();
464          checkValidColumn(columnIndex);
465     }
466
467    public void updateShort(int columnIndex, short x) throws SQLException{
468       checkValidRow();
469       checkUpdatable();
470       checkValidColumn(columnIndex);
471    }
472
473    public void updateInt(int columnIndex, int x) throws SQLException{
474       checkValidRow();
475       checkUpdatable();
476       checkValidColumn(columnIndex);
477    }
478
479    public void updateLong(int columnIndex, long x) throws SQLException{
480       checkValidRow();
481       checkUpdatable();
482       checkValidColumn(columnIndex);
483    }
484
485    public void updateFloat(int columnIndex, float x) throws SQLException{
486       checkValidRow();
487       checkUpdatable();
488       checkValidColumn(columnIndex);
489    }
490
491    public void updateDouble(int columnIndex, double x) throws SQLException{
492       checkValidRow();
493       checkUpdatable();
494       checkValidColumn(columnIndex);
495    }
496
497    public void updateBigDecimal(int columnIndex, BigDecimal JavaDoc x) throws SQLException{
498       checkValidRow();
499       checkUpdatable();
500       checkValidColumn(columnIndex);
501    }
502
503    public void updateString(int columnIndex, String JavaDoc x) throws SQLException{
504       checkValidRow();
505       checkUpdatable();
506       checkValidColumn(columnIndex);
507    }
508
509    public void updateBytes(int columnIndex, byte x[]) throws SQLException{
510       checkValidRow();
511       checkUpdatable();
512       checkValidColumn(columnIndex);
513       try{
514          Object JavaDoc columnValue = Utilities.convertObject(x,getMetaData().getColumnType(columnIndex));
515       }catch(DException de){
516          throw de.getSqlException(connection.getLocale());
517       }
518    }
519
520    public void updateDate(int columnIndex, java.sql.Date JavaDoc x) throws SQLException{
521       checkValidRow();
522       checkUpdatable();
523       checkValidColumn(columnIndex);
524    }
525
526    public void updateTime(int columnIndex, java.sql.Time JavaDoc x) throws SQLException{
527       checkValidRow();
528       checkUpdatable();
529       checkValidColumn(columnIndex);
530    }
531
532    public void updateTimestamp(int columnIndex, java.sql.Timestamp JavaDoc x) throws SQLException{
533       checkValidRow();
534       checkUpdatable();
535       checkValidColumn(columnIndex);
536    }
537
538    public void updateAsciiStream(int columnIndex,InputStream x,int length) throws SQLException{
539       checkValidRow();
540       checkUpdatable();
541       checkValidColumn(columnIndex);
542       byte[] bytes = new byte[length];
543       try{
544           x.read(bytes);
545       }catch(IOException ex){
546           DException dex = new DException("DSE146", null);
547           throw dex.getSqlException(connection.getLocale());
548       }
549    }
550
551    public void updateBinaryStream(int columnIndex,java.io.InputStream JavaDoc x,int length) throws SQLException{
552       checkValidRow();
553       checkUpdatable();
554       checkValidColumn(columnIndex);
555       byte[] bytes = new byte[length];
556       try{
557           x.read(bytes);
558       }catch(IOException ex){
559           DException dex = new DException("DSE146", null);
560           throw dex.getSqlException(connection.getLocale());
561       }
562    }
563
564    public void updateCharacterStream(int columnIndex,java.io.Reader JavaDoc x,int length) throws SQLException{
565       checkValidRow();
566       checkUpdatable();
567       checkValidColumn(columnIndex);
568       char[] chars = new char[length];
569       byte[] bytes = null;
570       try{
571           x.read(chars);
572           bytes = Utilities.convertCharsToBytes(chars);
573       }catch(java.io.IOException JavaDoc ie){
574           DException dex = new DException("DSE297", null);
575           throw dex.getSqlException(connection.getLocale());
576       }
577    }
578
579    public void updateObject(int columnIndex, Object JavaDoc x, int scale) throws SQLException{
580       checkValidRow();
581       checkUpdatable();
582       checkValidColumn(columnIndex);
583       int type = getMetaData().getColumnType(columnIndex);
584       if( (type == Types.NUMERIC || type == Datatype.DECIMAL) && scale != -1 ){
585           try{
586             x = Utilities.convertObject(x,Types.DECIMAL);
587          }catch(DException de){
588             throw de.getSqlException(connection.getLocale());
589          }
590           x = ((java.math.BigDecimal JavaDoc)x).setScale(scale);
591       }
592    }
593
594     public void updateObject(int columnIndex, Object JavaDoc x) throws SQLException{
595         updateObject(columnIndex,x,-1);
596     }
597
598     public void updateNull(String JavaDoc columnName) throws SQLException{
599         updateNull( findColumn(columnName));
600     }
601
602     public void updateBoolean(String JavaDoc columnName, boolean x) throws SQLException{
603         updateObject(findColumn(columnName), Utilities.getBooleanValue(x));
604     }
605
606     public void updateByte(String JavaDoc columnName, byte x) throws SQLException{
607         updateObject( findColumn(columnName),new Byte JavaDoc(x) );
608     }
609
610     public void updateShort(String JavaDoc columnName, short x) throws SQLException{
611         updateShort( findColumn(columnName),x);
612     }
613
614     public void updateInt(String JavaDoc columnName, int x) throws SQLException{
615         updateInt( findColumn(columnName),x);
616     }
617
618     public void updateLong(String JavaDoc columnName, long x) throws SQLException{
619         updateLong( findColumn(columnName),x);
620     }
621
622     public void updateFloat(String JavaDoc columnName, float x) throws SQLException{
623         updateFloat( findColumn(columnName),x);
624     }
625
626     public void updateDouble(String JavaDoc columnName, double x) throws SQLException{
627         updateDouble( findColumn(columnName), x );
628     }
629
630     public void updateBigDecimal(String JavaDoc columnName, BigDecimal JavaDoc x) throws SQLException{
631         updateBigDecimal(findColumn(columnName), x);
632     }
633
634     public void updateString(String JavaDoc columnName, String JavaDoc x) throws SQLException{
635         updateString( findColumn(columnName),x);
636     }
637
638     public void updateBytes(String JavaDoc columnName, byte x[]) throws SQLException{
639         updateBytes(findColumn(columnName), x );
640     }
641
642     public void updateDate(String JavaDoc columnName, java.sql.Date JavaDoc x) throws SQLException{
643         updateDate(findColumn(columnName),x);
644     }
645
646     public void updateTime(String JavaDoc columnName, java.sql.Time JavaDoc x) throws SQLException{
647         updateTime( findColumn(columnName),x);
648     }
649
650     public void updateTimestamp(String JavaDoc columnName, java.sql.Timestamp JavaDoc x) throws SQLException{
651         updateTimestamp( findColumn(columnName),x);
652     }
653
654     public void updateAsciiStream(String JavaDoc columnName,InputStream x,int length) throws SQLException{
655         updateAsciiStream(findColumn(columnName),x,length);
656     }
657
658     public void updateBinaryStream(String JavaDoc columnName,java.io.InputStream JavaDoc x,int length) throws SQLException{
659         updateBinaryStream(findColumn(columnName),x,length);
660     }
661
662     public void updateCharacterStream(String JavaDoc columnName,java.io.Reader JavaDoc reader,int length) throws SQLException{
663         updateCharacterStream(findColumn(columnName),reader,length);
664     }
665
666     public void updateObject(String JavaDoc columnName, Object JavaDoc x, int scale) throws SQLException{
667         updateObject(findColumn(columnName),x,scale);
668     }
669
670     public void updateObject(String JavaDoc columnName, Object JavaDoc x) throws SQLException{
671         updateObject( findColumn(columnName),x);
672     }
673
674    public void insertRow() throws SQLException{
675       checkUpdatable();
676    }
677
678    public void updateRow() throws SQLException{
679       checkUpdatable();
680    }
681
682    public void deleteRow() throws SQLException{
683       checkUpdatable();
684    }
685
686    public void refreshRow() throws SQLException{
687         checkUpdatable();
688    }
689
690     public void cancelRowUpdates() throws SQLException{
691       checkUpdatable();
692     }
693
694     public void moveToInsertRow() throws SQLException{
695       checkUpdatable();
696       navigator.storeCurrentPosition();
697     }
698
699     public void moveToCurrentRow() throws SQLException{
700         navigator.moveToCurrentRow();
701     }
702
703     public Statement getStatement() throws SQLException{
704         return statement;
705     }
706
707    public Object JavaDoc getObject(int columnIndex, java.util.Map JavaDoc map) throws SQLException{
708       checkValidRow();
709       checkValidColumn(columnIndex);
710       navigator.lastRetrievedColumn = columnIndex;
711       Object JavaDoc columnValue = getObject(columnIndex,-1);
712       navigator.lastRetrievedColumnValue = columnValue == null ? null : columnValue;
713       if( columnValue == null)
714          return columnValue;
715       int typew = getMetaData().getColumnType(columnIndex);
716       try{
717          Object JavaDoc newvalue = Utilities.convertObject(columnValue,typew);
718          if(typew == Types.NUMERIC || typew == Types.DECIMAL){
719             int scale = getMetaData().getScale(columnIndex);
720             if(scale != -1)
721                newvalue = ((java.math.BigDecimal JavaDoc)newvalue).setScale(scale,BigDecimal.ROUND_DOWN);
722          }
723          return newvalue;
724       }catch(DException de){
725          throw de.getSqlException(connection.getLocale());
726       }
727    }
728
729     public Ref getRef(int columnIndex) throws SQLException{
730        DException dex = new DException("DSE22",new Object JavaDoc[]{"User-defined type"});
731        throw dex.getSqlException(connection.getLocale());
732     }
733
734    public Blob getBlob(int columnIndex) throws SQLException{
735       Blob clob = (Blob)getObject(columnIndex);
736       navigator.lastRetrievedColumnValue = clob == null ? null : clob;
737       return clob;
738    }
739
740    public Clob getClob(int columnIndex) throws SQLException{
741       Clob clob = (Clob)getObject(columnIndex,Types.CLOB);
742       navigator.lastRetrievedColumnValue = clob == null ? null : clob;
743       return clob;
744    }
745
746    public Array getArray(int columnIndex) throws SQLException{
747        DException dex = new DException("DSE22",new Object JavaDoc[]{"User-defined type"});
748        throw dex.getSqlException(connection.getLocale());
749    }
750
751    public Object JavaDoc getObject(String JavaDoc colName, java.util.Map JavaDoc map) throws SQLException{
752         return getObject(findColumn(colName),map);
753    }
754
755     public Ref getRef(String JavaDoc colName) throws SQLException{
756         return getRef(findColumn(colName) );
757     }
758
759     public Blob getBlob(String JavaDoc colName) throws SQLException{
760         return getBlob( findColumn(colName) );
761     }
762
763     public Clob getClob(String JavaDoc colName) throws SQLException{
764         return getClob( findColumn(colName) );
765     }
766
767    public Array getArray(String JavaDoc colName) throws SQLException{
768       return getArray(findColumn(colName) );
769    }
770
771    public java.sql.Date JavaDoc getDate(int columnIndex, Calendar JavaDoc cal) throws SQLException{
772       checkValidRow();
773       checkValidColumn(columnIndex);
774       navigator.lastRetrievedColumn = columnIndex;
775       java.sql.Date JavaDoc columnValue = (java.sql.Date JavaDoc)getObject(columnIndex,Types.DATE);
776       navigator.lastRetrievedColumnValue = columnValue == null ? null : columnValue;
777       if( columnValue == null || cal == null )
778           return columnValue;
779       cal.setTime(columnValue);
780       return new com.daffodilwoods.daffodildb.utils.DBDate(cal.getTime().getTime()/*cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DATE)*/);
781    }
782
783     public java.sql.Date JavaDoc getDate(String JavaDoc columnName, Calendar JavaDoc cal) throws SQLException{
784         return getDate(findColumn(columnName),cal);
785     }
786
787    public Time getTime(int columnIndex, Calendar JavaDoc cal) throws SQLException{
788       checkValidRow();
789       checkValidColumn(columnIndex);
790       navigator.lastRetrievedColumn = columnIndex;
791       Time timeObj = (Time)getObject(columnIndex,Types.TIME);
792       navigator.lastRetrievedColumnValue = null;
793       if( timeObj == null )
794           return null;
795       Date timeObject = new com.daffodilwoods.daffodildb.utils.DBDate(timeObj.getTime());
796       if(cal == null){
797           Time time = new Time(timeObject.getTime());
798           navigator.lastRetrievedColumnValue = time;
799           return time;
800       }
801       cal.setTime(timeObject);
802       Time calTime = new Time(timeObject.getTime()/*cal.get(Calendar.HOUR),cal.get(Calendar.MINUTE),
803                                cal.get(Calendar.SECOND) */
);
804       navigator.lastRetrievedColumnValue = calTime;
805       return calTime;
806    }
807
808     public Time getTime(String JavaDoc columnName, Calendar JavaDoc cal) throws SQLException{
809         return getTime( findColumn(columnName),cal);
810     }
811
812     public java.sql.Timestamp JavaDoc getTimestamp(int columnIndex, Calendar JavaDoc cal)throws SQLException{
813          checkValidRow();
814          checkValidColumn(columnIndex);
815          navigator.lastRetrievedColumn = columnIndex;
816          Timestamp timestamp = (Timestamp)getObject(columnIndex,Types.TIMESTAMP);
817          if(timestamp == null){
818              return null;
819          }
820          Date timeObject = new com.daffodilwoods.daffodildb.utils.DBDate(timestamp.getTime());
821          navigator.lastRetrievedColumnValue = null;
822          if( timeObject == null )
823              return null;
824          if(cal == null){
825              Timestamp time = new Timestamp(timeObject.getTime());
826              navigator.lastRetrievedColumnValue = time;
827              return time;
828          }
829          cal.setTime(timeObject);
830          Timestamp calTime = new Timestamp( cal.getTime().getTime() );
831          navigator.lastRetrievedColumnValue = calTime;
832          return calTime;
833     }
834
835     public java.sql.Timestamp JavaDoc getTimestamp(String JavaDoc columnName, Calendar JavaDoc cal)throws SQLException{
836         return getTimestamp(findColumn(columnName),cal);
837     }
838
839    public java.net.URL JavaDoc getURL(int columnIndex) throws SQLException{
840       checkValidRow();
841       checkValidColumn(columnIndex);
842       navigator.lastRetrievedColumn = columnIndex;
843       try{
844           Object JavaDoc urlString = getObject(columnIndex,Types.CHAR);
845           navigator.lastRetrievedColumnValue = urlString == null ? null : urlString;
846           if (urlString == null )
847               return null;
848           java.net.URL JavaDoc url = new java.net.URL JavaDoc(urlString.toString());
849           return url;
850       }catch(java.net.MalformedURLException JavaDoc N){
851           DException dex = new DException("DSE525", null);
852           throw dex.getSqlException(((DaffodilDBConnection)statement.getConnection()).getLocale());
853       }
854    }
855
856     public java.net.URL JavaDoc getURL(String JavaDoc columnName) throws SQLException{
857         return getURL(findColumn(columnName) );
858     }
859
860     public void updateRef(int columnIndex, java.sql.Ref JavaDoc x) throws SQLException{
861          checkValidRow();
862          checkUpdatable();
863          checkValidColumn(columnIndex);
864          DException dex = new DException("DSE22",new Object JavaDoc[]{"User-defined type"});
865          throw dex.getSqlException(connection.getLocale());
866     }
867
868     public void updateRef(String JavaDoc columnName, java.sql.Ref JavaDoc x) throws SQLException{
869         updateRef(findColumn(columnName),x);
870     }
871
872    public void updateBlob(int columnIndex, Blob x) throws SQLException{
873       checkValidRow();
874       checkUpdatable();
875       checkValidColumn(columnIndex);
876    }
877
878     public void updateBlob(String JavaDoc columnName, Blob x) throws SQLException{
879         updateBlob( findColumn(columnName), x );
880     }
881
882    public void updateClob(int columnIndex, java.sql.Clob JavaDoc x) throws SQLException{
883       checkValidRow();
884       checkUpdatable();
885       checkValidColumn(columnIndex);
886    }
887
888    public void updateClob(String JavaDoc columnName, java.sql.Clob JavaDoc x) throws SQLException{
889         updateClob(findColumn(columnName),x);
890    }
891
892    public void updateArray(int columnIndex, java.sql.Array JavaDoc x) throws SQLException{
893       checkValidRow();
894       checkUpdatable();
895       checkValidColumn(columnIndex);
896       DException dex = new DException("DSE22",new Object JavaDoc[]{"User-defined type"});
897       throw dex.getSqlException(connection.getLocale());
898    }
899
900    public void updateArray(String JavaDoc columnName, java.sql.Array JavaDoc x) throws SQLException{
901         updateArray(findColumn(columnName), x);
902    }
903
904    public void setConcurreny(int concurrency)throws SQLException{
905       if(concurrency != CONCUR_READ_ONLY){
906           DException dex = new DException("DSE511", new Object JavaDoc[] {new Integer JavaDoc(concurrency)});
907           throw dex.getSqlException(connection.getLocale());
908       }
909       this.concurrency = concurrency;
910    }
911
912    public int getRowCount()throws SQLException{
913       return objs.length;
914    }
915
916    public void setType(int type)throws SQLException{
917       if(! (type == TYPE_FORWARD_ONLY || type == TYPE_SCROLL_INSENSITIVE || type == TYPE_SCROLL_SENSITIVE) ){
918           DException dex = new DException("DSE1024", null);
919           throw dex.getSqlException(connection.getLocale());
920       }
921       this.type = type;
922       navigator = new Navigator();
923    }
924
925    private void checkValidRow() throws SQLException{
926       if(isBeforeFirst() || isAfterLast()){
927           DException dex = new DException("DSE294", null);
928           throw dex.getSqlException(connection.getLocale());
929       }
930    }
931
932    private void checkValidColumn(int columnIndex) throws SQLException{
933       if( columnIndex < 1 || columnIndex > getMetaData().getColumnCount()) {
934          DException dex = new DException("DSE255",new Object JavaDoc[]{getMetaData().getColumnName(columnIndex),getMetaData().getTableName(columnIndex)});
935          throw dex.getSqlException(connection.getLocale());
936       }
937    }
938
939    private void checkUpdatable() throws SQLException{
940      if(concurrency != CONCUR_UPDATABLE ) {
941           DException dex = new DException("DSE870", null);
942           throw dex.getSqlException(connection.getLocale());
943      }
944    }
945
946
947    class Navigator{
948       boolean beforeFirst = true;
949       boolean afterLast = false;
950       int currentRow = -1;
951       int rowCount;
952       int lastCurrentRow = -1 ; // used in case of updatable resultSet
953
int lastRetrievedColumn = -1;
954       Object JavaDoc lastRetrievedColumnValue = null;
955
956       Navigator(){
957          rowCount = objs.length;
958       }
959
960       private void checkScrollable() throws SQLException{
961          if( type == TYPE_FORWARD_ONLY ) {
962              DException dex = new DException("DSE871", null);
963              throw dex.getSqlException(connection.getLocale());
964          }
965       }
966
967       Object JavaDoc getObject(int columnIndex)throws SQLException{
968          if(currentRow > 0)
969             return lastRetrievedColumnValue = objs[currentRow-1][columnIndex-1];
970          throw new SQLException("ColumnIndex Passed Is Invalid : "+columnIndex);
971       }
972
973       boolean absolute( int row ) throws SQLException{
974          checkScrollable();
975          clearUpdateBuffer();
976          if(row == 0)
977              return false;
978          if(row < 0 && Math.abs(row) > rowCount){
979              beforeFirst();
980              return false;
981          }
982          if(row > rowCount){
983              afterLast();
984              return false;
985          }
986          row = row < 0 ? rowCount - row + 1 : row;
987          moveToRow(row);
988          lastRetrievedColumn = -1;
989          return true;
990       }
991
992       void afterLast() throws SQLException{
993          checkScrollable();
994          clearUpdateBuffer();
995          beforeFirst = false;
996          afterLast = true;
997          currentRow = objs.length;
998          lastRetrievedColumn = -1;
999       }
1000
1001      void beforeFirst() throws SQLException{
1002         checkScrollable();
1003         clearUpdateBuffer();
1004         beforeFirst = true;
1005         afterLast = false;
1006         currentRow = -1;
1007         lastRetrievedColumn = -1;
1008      }
1009
1010      boolean first() throws SQLException{
1011         checkScrollable();
1012         clearUpdateBuffer();
1013         if( rowCount > 0){
1014             currentRow = 1;
1015             lastRetrievedColumn = -1;
1016             beforeFirst = false;
1017             afterLast = false;
1018             return true;
1019          }
1020          return false;
1021      }
1022
1023      int getRow() throws SQLException{
1024         if(beforeFirst || afterLast)
1025             return 0;
1026         return currentRow;
1027      }
1028
1029      boolean isAfterLast() throws SQLException{
1030         return afterLast;
1031      }
1032
1033      boolean isBeforeFirst() throws SQLException{
1034         return beforeFirst;
1035      }
1036
1037      boolean isFirst() throws SQLException{
1038         return currentRow == 1;
1039      }
1040
1041      boolean isLast() throws SQLException{
1042         return currentRow == rowCount;
1043      }
1044
1045      boolean last() throws SQLException{
1046         checkScrollable();
1047         clearUpdateBuffer();
1048         if( objs.length > 0 ){
1049             currentRow = rowCount;
1050             lastRetrievedColumn = -1;
1051             beforeFirst = false;
1052             afterLast = false;
1053             return true;
1054         }
1055         return false;
1056      }
1057
1058      void moveToCurrentRow() throws SQLException{
1059         clearUpdateBuffer();
1060         lastRetrievedColumn = -1;
1061         if( lastCurrentRow != -1 ){
1062             if( currentRow != lastCurrentRow )
1063                 moveToRow(lastCurrentRow);
1064             lastCurrentRow = -1;
1065             beforeFirst = false;
1066             afterLast = false;
1067         }
1068      }
1069
1070      boolean next() throws SQLException{
1071         clearUpdateBuffer();
1072         lastRetrievedColumn = -1;
1073         if(afterLast)
1074            return false;
1075         if( beforeFirst ){
1076            beforeFirst = false;
1077            currentRow = 0;
1078            if( rowCount > 0 ){
1079               currentRow =1;
1080               clearWarnings();
1081               return true;
1082            }
1083            return false;
1084         }
1085         boolean next = (currentRow < rowCount);
1086         currentRow++;
1087         if( next ){
1088            clearWarnings();
1089            return true;
1090         }
1091         afterLast = true;
1092         return false;
1093      }
1094
1095      boolean previous() throws SQLException{
1096         checkScrollable();
1097         clearUpdateBuffer();
1098         lastRetrievedColumn = -1;
1099         if( beforeFirst )
1100             return false;
1101         if( afterLast ){
1102            afterLast = false;
1103            currentRow--;
1104            if ( rowCount > 0 ){
1105               currentRow = rowCount;
1106               clearWarnings();
1107               return true;
1108            }
1109            return false;
1110         }
1111         boolean previous = currentRow > 0;
1112         currentRow--;
1113         if( previous ){
1114            clearWarnings();
1115            return true;
1116         }
1117         currentRow = -1;
1118         beforeFirst = true;
1119         return false;
1120      }
1121
1122      boolean relative( int rows ) throws SQLException{
1123         checkScrollable();
1124         clearUpdateBuffer();
1125         lastRetrievedColumn = -1;
1126         int newRowIndex = currentRow + rows;
1127         if( newRowIndex <= 0 ){
1128            beforeFirst = true;currentRow =-1;
1129            return false;
1130         }
1131         else if( newRowIndex > rowCount ){
1132            afterLast = true;currentRow = rowCount +1;
1133            return false;
1134         }
1135         moveToRow(newRowIndex);
1136         return true;
1137      }
1138
1139      private void moveToRow(int rowIndex)throws SQLException{
1140         clearUpdateBuffer();
1141         lastRetrievedColumn = -1;
1142         if( currentRow < rowIndex ){
1143             while( currentRow != rowIndex ){
1144                 if(currentRow > rowCount){
1145                     break;
1146                 }
1147                 currentRow++;
1148             }
1149             if( currentRow == rowCount ){
1150                 beforeFirst = false;
1151                 afterLast = true;
1152             }
1153         }
1154         else if( currentRow > rowIndex ){
1155            while( currentRow != rowIndex ){
1156               if(currentRow < 1)
1157                  break;
1158               currentRow--;
1159            }
1160            if( currentRow == -1 ){
1161               beforeFirst = true;
1162               afterLast = false;
1163            }
1164         }
1165      }
1166
1167      public void storeCurrentPosition()throws SQLException{
1168         clearUpdateBuffer();
1169         lastCurrentRow = currentRow;
1170         currentRow = -1;
1171      }
1172
1173      private void clearUpdateBuffer()throws SQLException{
1174      }
1175   }
1176
1177   void addWarning(String JavaDoc reason){
1178      SQLWarning warning = new SQLWarning(reason);
1179      if( sqlWarning == null )
1180         sqlWarning = warning;
1181      sqlWarning.setNextWarning(warning);
1182   }
1183
1184   private Object JavaDoc getObject(int columnIndex,int requiredType) throws SQLException{
1185      checkValidRow();
1186      checkValidColumn(columnIndex);
1187      Object JavaDoc columnValue = navigator.getObject(columnIndex);
1188      if (columnValue == null || requiredType == -1 )
1189          return columnValue;
1190      try{
1191         return Utilities.convertObject(columnValue,requiredType);
1192      }catch(DException d){
1193         throw d.getSqlException(connection.getLocale());
1194      }
1195   }
1196
1197   public void setConnection(java.sql.Connection JavaDoc connection) throws SQLException{
1198      this.connection = (DaffodilDBConnection)connection;
1199      statement = (DaffodilDBStatement)connection.createStatement();
1200      setType(TYPE_FORWARD_ONLY);
1201   }
1202
1203   public void setMetaData(ResultSetMetaData resultSetMetaData) throws SQLException{
1204      this.metaData = resultSetMetaData;
1205   }
1206}
1207
1208
Popular Tags