1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
|
package org.postgresql.jdbc2;
import java.math.BigDecimal;
import java.io.*;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Vector;
import org.postgresql.Field;
import org.postgresql.core.Encoding;
import org.postgresql.largeobject.*;
import org.postgresql.util.PGbytea;
import org.postgresql.util.PSQLException;
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/AbstractJdbc2ResultSet.java,v 1.2 2002/07/24 22:08:42 barry Exp $
* This class defines methods of the jdbc2 specification. This class extends
* org.postgresql.jdbc1.AbstractJdbc1ResultSet which provides the jdbc1
* methods. The real Statement class (for jdbc2) is org.postgresql.jdbc2.Jdbc2ResultSet
*/
public class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.AbstractJdbc1ResultSet
{
protected Statement statement;
protected String sqlQuery=null;
public AbstractJdbc2ResultSet(org.postgresql.PGConnection conn, Field[] fields, Vector tuples, String status, int updateCount, long insertOID, boolean binaryCursor)
{
super(conn, fields, tuples, status, updateCount, insertOID, binaryCursor);
}
public java.net.URL getURL(int columnIndex) throws SQLException
{
return null;
}
public java.net.URL getURL(String columnName) throws SQLException
{
return null;
}
/*
* Get the value of a column in the current row as a Java object
*
* <p>This method will return the value of the given column as a
* Java object. The type of the Java object will be the default
* Java Object type corresponding to the column's SQL type, following
* the mapping specified in the JDBC specification.
*
* <p>This method may also be used to read database specific abstract
* data types.
*
* @param columnIndex the first column is 1, the second is 2...
* @return a Object holding the column value
* @exception SQLException if a database access error occurs
*/
public Object getObject(int columnIndex) throws SQLException
{
Field field;
checkResultSet( columnIndex );
wasNullFlag = (this_row[columnIndex - 1] == null);
if (wasNullFlag)
return null;
field = fields[columnIndex - 1];
// some fields can be null, mainly from those returned by MetaData methods
if (field == null)
{
wasNullFlag = true;
return null;
}
switch (field.getSQLType())
{
case Types.BIT:
return getBoolean(columnIndex) ? Boolean.TRUE : Boolean.FALSE;
case Types.SMALLINT:
return new Short(getShort(columnIndex));
case Types.INTEGER:
return new Integer(getInt(columnIndex));
case Types.BIGINT:
return new Long(getLong(columnIndex));
case Types.NUMERIC:
return getBigDecimal
(columnIndex, (field.getMod() == -1) ? -1 : ((field.getMod() - 4) & 0xffff));
case Types.REAL:
return new Float(getFloat(columnIndex));
case Types.DOUBLE:
return new Double(getDouble(columnIndex));
case Types.CHAR:
case Types.VARCHAR:
return getString(columnIndex);
case Types.DATE:
return getDate(columnIndex);
case Types.TIME:
return getTime(columnIndex);
case Types.TIMESTAMP:
return getTimestamp(columnIndex);
case Types.BINARY:
case Types.VARBINARY:
return getBytes(columnIndex);
case Types.ARRAY:
return getArray(columnIndex);
default:
String type = field.getPGType();
// if the backend doesn't know the type then coerce to String
if (type.equals("unknown"))
{
return getString(columnIndex);
}
else
{
return connection.getObject(field.getPGType(), getString(columnIndex));
}
}
}
public boolean absolute(int index) throws SQLException
{
// index is 1-based, but internally we use 0-based indices
int internalIndex;
if (index == 0)
throw new SQLException("Cannot move to index of 0");
final int rows_size = rows.size();
//if index<0, count from the end of the result set, but check
//to be sure that it is not beyond the first index
if (index < 0)
{
if (index >= -rows_size)
internalIndex = rows_size + index;
else
{
beforeFirst();
return false;
}
}
else
{
//must be the case that index>0,
//find the correct place, assuming that
//the index is not too large
if (index <= rows_size)
internalIndex = index - 1;
else
{
afterLast();
return false;
}
}
current_row = internalIndex;
this_row = (byte [][])rows.elementAt(internalIndex);
return true;
}
public void afterLast() throws SQLException
{
final int rows_size = rows.size();
if (rows_size > 0)
current_row = rows_size;
}
public void beforeFirst() throws SQLException
{
if (rows.size() > 0)
current_row = -1;
}
public void cancelRowUpdates() throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void deleteRow() throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public boolean first() throws SQLException
{
if (rows.size() <= 0)
return false;
current_row = 0;
this_row = (byte [][])rows.elementAt(current_row);
rowBuffer=new byte[this_row.length][];
System.arraycopy(this_row,0,rowBuffer,0,this_row.length);
return true;
}
public java.sql.Array getArray(String colName) throws SQLException
{
return getArray(findColumn(colName));
}
public java.sql.Array getArray(int i) throws SQLException
{
wasNullFlag = (this_row[i - 1] == null);
if (wasNullFlag)
return null;
if (i < 1 || i > fields.length)
throw new PSQLException("postgresql.res.colrange");
return (java.sql.Array) new org.postgresql.jdbc2.Array( connection, i, fields[i - 1], (java.sql.ResultSet)this );
}
public java.math.BigDecimal getBigDecimal(int columnIndex) throws SQLException
{
return getBigDecimal(columnIndex, -1);
}
public java.math.BigDecimal getBigDecimal(String columnName) throws SQLException
{
return getBigDecimal(findColumn(columnName));
}
public Blob getBlob(String columnName) throws SQLException
{
return getBlob(findColumn(columnName));
}
public Blob getBlob(int i) throws SQLException
{
return new org.postgresql.largeobject.PGblob(connection, getInt(i));
}
public java.io.Reader getCharacterStream(String columnName) throws SQLException
{
return getCharacterStream(findColumn(columnName));
}
public java.io.Reader getCharacterStream(int i) throws SQLException
{
checkResultSet( i );
wasNullFlag = (this_row[i - 1] == null);
if (wasNullFlag)
return null;
if (((AbstractJdbc2Connection)connection).haveMinimumCompatibleVersion("7.2"))
{
//Version 7.2 supports AsciiStream for all the PG text types
//As the spec/javadoc for this method indicate this is to be used for
//large text values (i.e. LONGVARCHAR) PG doesn't have a separate
//long string datatype, but with toast the text datatype is capable of
//handling very large values. Thus the implementation ends up calling
//getString() since there is no current way to stream the value from the server
return new CharArrayReader(getString(i).toCharArray());
}
else
{
// In 7.1 Handle as BLOBS so return the LargeObject input stream
Encoding encoding = connection.getEncoding();
InputStream input = getBinaryStream(i);
return encoding.getDecodingReader(input);
}
}
public Clob getClob(String columnName) throws SQLException
{
return getClob(findColumn(columnName));
}
public Clob getClob(int i) throws SQLException
{
return new org.postgresql.largeobject.PGclob(connection, getInt(i));
}
public int getConcurrency() throws SQLException
{
// The standard ResultSet class will now return
// CONCUR_READ_ONLY. A sub-class will overide this if the query was
// updateable.
return java.sql.ResultSet.CONCUR_READ_ONLY;
}
public java.sql.Date getDate(int i, java.util.Calendar cal) throws SQLException
{
// If I read the specs, this should use cal only if we don't
// store the timezone, and if we do, then act just like getDate()?
// for now...
return getDate(i);
}
public Time getTime(int i, java.util.Calendar cal) throws SQLException
{
// If I read the specs, this should use cal only if we don't
// store the timezone, and if we do, then act just like getTime()?
// for now...
return getTime(i);
}
public Timestamp getTimestamp(int i, java.util.Calendar cal) throws SQLException
{
// If I read the specs, this should use cal only if we don't
// store the timezone, and if we do, then act just like getDate()?
// for now...
return getTimestamp(i);
}
public java.sql.Date getDate(String c, java.util.Calendar cal) throws SQLException
{
return getDate(findColumn(c), cal);
}
public Time getTime(String c, java.util.Calendar cal) throws SQLException
{
return getTime(findColumn(c), cal);
}
public Timestamp getTimestamp(String c, java.util.Calendar cal) throws SQLException
{
return getTimestamp(findColumn(c), cal);
}
public int getFetchDirection() throws SQLException
{
//PostgreSQL normally sends rows first->last
return java.sql.ResultSet.FETCH_FORWARD;
}
public int getFetchSize() throws SQLException
{
// In this implementation we return the entire result set, so
// here return the number of rows we have. Sub-classes can return a proper
// value
return rows.size();
}
public Object getObject(String columnName, java.util.Map map) throws SQLException
{
return getObject(findColumn(columnName), map);
}
/*
* This checks against map for the type of column i, and if found returns
* an object based on that mapping. The class must implement the SQLData
* interface.
*/
public Object getObject(int i, java.util.Map map) throws SQLException
{
throw org.postgresql.Driver.notImplemented();
}
public Ref getRef(String columnName) throws SQLException
{
return getRef(findColumn(columnName));
}
public Ref getRef(int i) throws SQLException
{
//The backend doesn't yet have SQL3 REF types
throw new PSQLException("postgresql.psqlnotimp");
}
public int getRow() throws SQLException
{
final int rows_size = rows.size();
if (current_row < 0 || current_row >= rows_size)
return 0;
return current_row + 1;
}
// This one needs some thought, as not all ResultSets come from a statement
public Statement getStatement() throws SQLException
{
return statement;
}
public int getType() throws SQLException
{
// This implementation allows scrolling but is not able to
// see any changes. Sub-classes may overide this to return a more
// meaningful result.
return java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE;
}
public void insertRow() throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public boolean isAfterLast() throws SQLException
{
final int rows_size = rows.size();
return (current_row >= rows_size && rows_size > 0);
}
public boolean isBeforeFirst() throws SQLException
{
return (current_row < 0 && rows.size() > 0);
}
public boolean isFirst() throws SQLException
{
return (current_row == 0 && rows.size() >= 0);
}
public boolean isLast() throws SQLException
{
final int rows_size = rows.size();
return (current_row == rows_size - 1 && rows_size > 0);
}
public boolean last() throws SQLException
{
final int rows_size = rows.size();
if (rows_size <= 0)
return false;
current_row = rows_size - 1;
this_row = (byte [][])rows.elementAt(current_row);
rowBuffer=new byte[this_row.length][];
System.arraycopy(this_row,0,rowBuffer,0,this_row.length);
return true;
}
public void moveToCurrentRow() throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void moveToInsertRow() throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public boolean previous() throws SQLException
{
if (--current_row < 0)
return false;
this_row = (byte [][])rows.elementAt(current_row);
System.arraycopy(this_row,0,rowBuffer,0,this_row.length);
return true;
}
public void refreshRow() throws SQLException
{
throw new PSQLException("postgresql.notsensitive");
}
public boolean relative(int rows) throws SQLException
{
//have to add 1 since absolute expects a 1-based index
return absolute(current_row + 1 + rows);
}
public boolean rowDeleted() throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
return false; // javac complains about not returning a value!
}
public boolean rowInserted() throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
return false; // javac complains about not returning a value!
}
public boolean rowUpdated() throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
return false; // javac complains about not returning a value!
}
public void setFetchDirection(int direction) throws SQLException
{
throw new PSQLException("postgresql.psqlnotimp");
}
public void setFetchSize(int rows) throws SQLException
{
// Sub-classes should implement this as part of their cursor support
throw org.postgresql.Driver.notImplemented();
}
public void updateAsciiStream(int columnIndex,
java.io.InputStream x,
int length
) throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateAsciiStream(String columnName,
java.io.InputStream x,
int length
) throws SQLException
{
updateAsciiStream(findColumn(columnName), x, length);
}
public void updateBigDecimal(int columnIndex,
java.math.BigDecimal x
) throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateBigDecimal(String columnName,
java.math.BigDecimal x
) throws SQLException
{
updateBigDecimal(findColumn(columnName), x);
}
public void updateBinaryStream(int columnIndex,
java.io.InputStream x,
int length
) throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateBinaryStream(String columnName,
java.io.InputStream x,
int length
) throws SQLException
{
updateBinaryStream(findColumn(columnName), x, length);
}
public void updateBoolean(int columnIndex, boolean x) throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateBoolean(String columnName, boolean x) throws SQLException
{
updateBoolean(findColumn(columnName), x);
}
public void updateByte(int columnIndex, byte x) throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateByte(String columnName, byte x) throws SQLException
{
updateByte(findColumn(columnName), x);
}
public void updateBytes(String columnName, byte[] x) throws SQLException
{
updateBytes(findColumn(columnName), x);
}
public void updateBytes(int columnIndex, byte[] x) throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateCharacterStream(int columnIndex,
java.io.Reader x,
int length
) throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateCharacterStream(String columnName,
java.io.Reader x,
int length
) throws SQLException
{
updateCharacterStream(findColumn(columnName), x, length);
}
public void updateDate(int columnIndex, java.sql.Date x) throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateDate(String columnName, java.sql.Date x) throws SQLException
{
updateDate(findColumn(columnName), x);
}
public void updateDouble(int columnIndex, double x) throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateDouble(String columnName, double x) throws SQLException
{
updateDouble(findColumn(columnName), x);
}
public void updateFloat(int columnIndex, float x) throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateFloat(String columnName, float x) throws SQLException
{
updateFloat(findColumn(columnName), x);
}
public void updateInt(int columnIndex, int x) throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateInt(String columnName, int x) throws SQLException
{
updateInt(findColumn(columnName), x);
}
public void updateLong(int columnIndex, long x) throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateLong(String columnName, long x) throws SQLException
{
updateLong(findColumn(columnName), x);
}
public void updateNull(int columnIndex) throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateNull(String columnName) throws SQLException
{
updateNull(findColumn(columnName));
}
public void updateObject(int columnIndex, Object x) throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateObject(String columnName, Object x) throws SQLException
{
updateObject(findColumn(columnName), x);
}
public void updateObject(int columnIndex, Object x, int scale) throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateObject(String columnName, Object x, int scale) throws SQLException
{
updateObject(findColumn(columnName), x, scale);
}
public void updateRow() throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateShort(int columnIndex, short x) throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateShort(String columnName, short x) throws SQLException
{
updateShort(findColumn(columnName), x);
}
public void updateString(int columnIndex, String x) throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateString(String columnName, String x) throws SQLException
{
updateString(findColumn(columnName), x);
}
public void updateTime(int columnIndex, Time x) throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateTime(String columnName, Time x) throws SQLException
{
updateTime(findColumn(columnName), x);
}
public void updateTimestamp(int columnIndex, Timestamp x) throws SQLException
{
// only sub-classes implement CONCUR_UPDATEABLE
notUpdateable();
}
public void updateTimestamp(String columnName, Timestamp x) throws SQLException
{
updateTimestamp(findColumn(columnName), x);
}
// helper method. Throws an SQLException when an update is not possible
public void notUpdateable() throws SQLException
{
throw new PSQLException("postgresql.noupdate");
}
/*
* It's used currently by getStatement() but may also with the new core
* package.
*/
public void setStatement(Statement statement)
{
this.statement = statement;
}
public void setSQLQuery(String sqlQuery) {
this.sqlQuery=sqlQuery;
}
}
|