Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5295fff

Browse files
author
Barry Lind
committed
Patch to fix up LONGVARBINARY support submitted by Amit Gollapudi
(agollapudi@demandsolutions.com). Also applied the RefCursor support patch by Nic Ferrier. This patch allows you too return a get a result set from a function that returns a refcursor. For example: call.registerOutParameter(1, Types.OTHER); call.execute(); ResultSet rs = (ResultSet) call.getObject(1); Modified Files: jdbc/org/postgresql/core/BaseStatement.java jdbc/org/postgresql/jdbc1/AbstractJdbc1ResultSet.java jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java jdbc/org/postgresql/jdbc1/Jdbc1CallableStatement.java jdbc/org/postgresql/jdbc1/Jdbc1PreparedStatement.java jdbc/org/postgresql/jdbc1/Jdbc1Statement.java jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java jdbc/org/postgresql/jdbc2/Jdbc2CallableStatement.java jdbc/org/postgresql/jdbc2/Jdbc2PreparedStatement.java jdbc/org/postgresql/jdbc2/Jdbc2Statement.java jdbc/org/postgresql/jdbc3/Jdbc3CallableStatement.java jdbc/org/postgresql/jdbc3/Jdbc3PreparedStatement.java jdbc/org/postgresql/jdbc3/Jdbc3Statement.java Added Files: jdbc/org/postgresql/PGRefCursorResultSet.java jdbc/org/postgresql/jdbc1/Jdbc1RefCursorResultSet.java jdbc/org/postgresql/jdbc2/Jdbc2RefCursorResultSet.java jdbc/org/postgresql/jdbc3/Jdbc3RefCursorResultSet.java jdbc/org/postgresql/test/jdbc2/RefCursorTest.java
1 parent 721996d commit 5295fff

18 files changed

+340
-11
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* PGRefCursorResultSet.java
4+
* Describes a PLPGSQL refcursor type.
5+
*
6+
* Copyright (c) 2003, PostgreSQL Global Development Group
7+
*
8+
* IDENTIFICATION
9+
* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/Attic/PGRefCursorResultSet.java,v 1.1 2003/05/03 20:40:45 barry Exp $
10+
*
11+
*-------------------------------------------------------------------------
12+
*/
13+
package org.postgresql;
14+
15+
16+
/** A ref cursor based result set.
17+
*/
18+
public interface PGRefCursorResultSet
19+
{
20+
21+
/** return the name of the cursor.
22+
*/
23+
public String getRefCursor ();
24+
25+
}

src/interfaces/jdbc/org/postgresql/core/BaseStatement.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66
* Copyright (c) 2003, PostgreSQL Global Development Group
77
*
88
* IDENTIFICATION
9-
* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/core/Attic/BaseStatement.java,v 1.1 2003/03/07 18:39:41 barry Exp $
9+
* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/core/Attic/BaseStatement.java,v 1.2 2003/05/03 20:40:45 barry Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
1313
package org.postgresql.core;
1414

15-
15+
import org.postgresql.PGRefCursorResultSet;
1616
import java.sql.*;
1717
import java.util.Vector;
1818

1919
public interface BaseStatement extends org.postgresql.PGStatement
2020
{
21-
public BaseResultSet createResultSet(Field[] fields, Vector tuples, String status, int updateCount, long insertOID, boolean binaryCursor) throws SQLException;
21+
public BaseResultSet createResultSet(Field[] fields, Vector tuples, String status, int updateCount, long insertOID, boolean binaryCursor) throws SQLException;
22+
public PGRefCursorResultSet createRefCursorResultSet(String cursorName) throws SQLException;
2223

2324
public BaseConnection getPGConnection();
2425

src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1ResultSet.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Copyright (c) 2003, PostgreSQL Global Development Group
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1ResultSet.java,v 1.11 2003/03/08 06:06:55 barry Exp $
12+
* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1ResultSet.java,v 1.12 2003/05/03 20:40:45 barry Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -575,11 +575,18 @@ public Object getObject(int columnIndex) throws SQLException
575575
return getBytes(columnIndex);
576576
default:
577577
String type = field.getPGType();
578+
578579
// if the backend doesn't know the type then coerce to String
579580
if (type.equals("unknown"))
580581
{
581582
return getString(columnIndex);
582583
}
584+
// Specialized support for ref cursors is neater.
585+
else if (type.equals("refcursor"))
586+
{
587+
String cursorName = getString(columnIndex);
588+
return statement.createRefCursorResultSet(cursorName);
589+
}
583590
else
584591
{
585592
return connection.getObject(field.getPGType(), getString(columnIndex));

src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import org.postgresql.largeobject.*;
1414
import org.postgresql.util.*;
1515

16-
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.20 2003/04/17 04:37:07 barry Exp $
16+
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.21 2003/05/03 20:40:45 barry Exp $
1717
* This class defines methods of the jdbc1 specification. This class is
1818
* extended by org.postgresql.jdbc2.AbstractJdbc2Statement which adds the jdbc2
1919
* methods. The real Statement class (for jdbc1) is org.postgresql.jdbc1.Jdbc1Statement
@@ -871,6 +871,7 @@ public void setNull(int parameterIndex, int sqlType) throws SQLException
871871
break;
872872
case Types.BINARY:
873873
case Types.VARBINARY:
874+
case Types.LONGVARBINARY:
874875
l_pgType = PG_BYTEA;
875876
break;
876877
case Types.OTHER:
@@ -1490,6 +1491,7 @@ public void setObject(int parameterIndex, Object x, int targetSqlType, int scale
14901491
break;
14911492
case Types.BINARY:
14921493
case Types.VARBINARY:
1494+
case Types.LONGVARBINARY:
14931495
setObject(parameterIndex, x);
14941496
break;
14951497
case Types.OTHER:

src/interfaces/jdbc/org/postgresql/jdbc1/Jdbc1CallableStatement.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import java.sql.*;
55
import java.util.Vector;
6+
import org.postgresql.PGRefCursorResultSet;
67
import org.postgresql.core.BaseResultSet;
78
import org.postgresql.core.Field;
89

@@ -18,5 +19,10 @@ public BaseResultSet createResultSet (Field[] fields, java.util.Vector tuples, S
1819
{
1920
return new Jdbc1ResultSet(this, fields, tuples, status, updateCount, insertOID, binaryCursor);
2021
}
22+
23+
public PGRefCursorResultSet createRefCursorResultSet (String cursorName) throws SQLException
24+
{
25+
return new Jdbc1RefCursorResultSet(this, cursorName);
26+
}
2127
}
2228

src/interfaces/jdbc/org/postgresql/jdbc1/Jdbc1PreparedStatement.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import java.sql.*;
5+
import org.postgresql.PGRefCursorResultSet;
56
import org.postgresql.core.BaseResultSet;
67
import org.postgresql.core.Field;
78

@@ -17,4 +18,9 @@ public BaseResultSet createResultSet (Field[] fields, java.util.Vector tuples, S
1718
{
1819
return new Jdbc1ResultSet(this, fields, tuples, status, updateCount, insertOID, binaryCursor);
1920
}
21+
22+
public PGRefCursorResultSet createRefCursorResultSet (String cursorName) throws SQLException
23+
{
24+
return new Jdbc1RefCursorResultSet(this, cursorName);
25+
}
2026
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.postgresql.jdbc1;
2+
3+
import java.sql.SQLException;
4+
import org.postgresql.core.QueryExecutor;
5+
import org.postgresql.core.BaseStatement;
6+
import org.postgresql.PGRefCursorResultSet;
7+
8+
/** A real result set based on a ref cursor.
9+
*
10+
* @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
11+
*/
12+
public class Jdbc1RefCursorResultSet extends Jdbc1ResultSet
13+
implements PGRefCursorResultSet
14+
{
15+
16+
// The name of the cursor being used.
17+
String refCursorHandle;
18+
19+
// Indicates when the result set has activaly bound to the cursor.
20+
boolean isInitialized = false;
21+
22+
23+
Jdbc1RefCursorResultSet(BaseStatement statement, String refCursorName)
24+
{
25+
super(statement, null, null, null, -1, 0L, false);
26+
this.refCursorHandle = refCursorName;
27+
}
28+
29+
public String getRefCursor ()
30+
{
31+
return refCursorHandle;
32+
}
33+
34+
public boolean next () throws SQLException
35+
{
36+
if (isInitialized)
37+
return super.next();
38+
// Initialize this res set with the rows from the cursor.
39+
String[] toExec = { "FETCH ALL IN \"" + refCursorHandle + "\";" };
40+
QueryExecutor.execute(toExec, new String[0], this);
41+
isInitialized = true;
42+
return super.next();
43+
}
44+
}

src/interfaces/jdbc/org/postgresql/jdbc1/Jdbc1Statement.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
import java.sql.*;
55
import java.util.Vector;
6+
import org.postgresql.PGRefCursorResultSet;
67
import org.postgresql.core.BaseResultSet;
78
import org.postgresql.core.Field;
89

9-
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/Jdbc1Statement.java,v 1.5 2003/03/07 18:39:44 barry Exp $
10+
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/Jdbc1Statement.java,v 1.6 2003/05/03 20:40:45 barry Exp $
1011
* This class implements the java.sql.Statement interface for JDBC1.
1112
* However most of the implementation is really done in
1213
* org.postgresql.jdbc1.AbstractJdbc1Statement
@@ -23,4 +24,9 @@ public BaseResultSet createResultSet (Field[] fields, Vector tuples, String stat
2324
{
2425
return new Jdbc1ResultSet(this, fields, tuples, status, updateCount, insertOID, binaryCursor);
2526
}
27+
28+
public PGRefCursorResultSet createRefCursorResultSet (String cursorName) throws SQLException
29+
{
30+
return new Jdbc1RefCursorResultSet(this, cursorName);
31+
}
2632
}

src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Copyright (c) 2003, PostgreSQL Global Development Group
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/AbstractJdbc2ResultSet.java,v 1.18 2003/03/25 02:24:07 davec Exp $
12+
* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/AbstractJdbc2ResultSet.java,v 1.19 2003/05/03 20:40:45 barry Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -148,11 +148,18 @@ public Object getObject(int columnIndex) throws SQLException
148148

149149
default:
150150
String type = field.getPGType();
151+
151152
// if the backend doesn't know the type then coerce to String
152153
if (type.equals("unknown"))
153154
{
154155
return getString(columnIndex);
155156
}
157+
// Specialized support for ref cursors is neater.
158+
else if (type.equals("refcursor"))
159+
{
160+
String cursorName = getString(columnIndex);
161+
return statement.createRefCursorResultSet(cursorName);
162+
}
156163
else
157164
{
158165
return connection.getObject(field.getPGType(), getString(columnIndex));

src/interfaces/jdbc/org/postgresql/jdbc2/Jdbc2CallableStatement.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import java.sql.*;
55
import java.util.Vector;
6+
import org.postgresql.PGRefCursorResultSet;
67
import org.postgresql.core.BaseResultSet;
78
import org.postgresql.core.Field;
89

@@ -18,5 +19,10 @@ public BaseResultSet createResultSet (Field[] fields, Vector tuples, String stat
1819
{
1920
return new Jdbc2ResultSet(this, fields, tuples, status, updateCount, insertOID, binaryCursor);
2021
}
22+
23+
public PGRefCursorResultSet createRefCursorResultSet (String cursorName) throws SQLException
24+
{
25+
return new Jdbc2RefCursorResultSet(this, cursorName);
26+
}
2127
}
2228

src/interfaces/jdbc/org/postgresql/jdbc2/Jdbc2PreparedStatement.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import java.sql.*;
55
import java.util.Vector;
6+
import org.postgresql.PGRefCursorResultSet;
67
import org.postgresql.core.BaseResultSet;
78
import org.postgresql.core.Field;
89

@@ -18,5 +19,11 @@ public BaseResultSet createResultSet (Field[] fields, Vector tuples, String stat
1819
{
1920
return new Jdbc2ResultSet(this, fields, tuples, status, updateCount, insertOID, binaryCursor);
2021
}
22+
23+
24+
public PGRefCursorResultSet createRefCursorResultSet (String cursorName) throws SQLException
25+
{
26+
return new Jdbc2RefCursorResultSet(this, cursorName);
27+
}
2128
}
2229

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.postgresql.jdbc2;
2+
3+
4+
import org.postgresql.core.QueryExecutor;
5+
import org.postgresql.core.BaseStatement;
6+
import org.postgresql.PGRefCursorResultSet;
7+
8+
9+
/** A real result set based on a ref cursor.
10+
*
11+
* @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
12+
*/
13+
public class Jdbc2RefCursorResultSet extends Jdbc2ResultSet
14+
implements PGRefCursorResultSet
15+
{
16+
17+
String refCursorHandle;
18+
19+
// Indicates when the result set has activaly bound to the cursor.
20+
boolean isInitialized = false;
21+
22+
Jdbc2RefCursorResultSet(BaseStatement statement, String refCursorName) throws java.sql.SQLException
23+
{
24+
super(statement, null, null, null, -1, 0L, false);
25+
this.refCursorHandle = refCursorName;
26+
}
27+
28+
public String getRefCursor ()
29+
{
30+
return refCursorHandle;
31+
}
32+
33+
public boolean next () throws java.sql.SQLException
34+
{
35+
if (isInitialized)
36+
return super.next();
37+
// Initialize this res set with the rows from the cursor.
38+
String[] toExec = { "FETCH ALL IN \"" + refCursorHandle + "\";" };
39+
QueryExecutor.execute(toExec, new String[0], this);
40+
isInitialized = true;
41+
return super.next();
42+
}
43+
}

src/interfaces/jdbc/org/postgresql/jdbc2/Jdbc2Statement.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
import java.sql.*;
55
import java.util.Vector;
6+
import org.postgresql.PGRefCursorResultSet;
67
import org.postgresql.core.BaseResultSet;
78
import org.postgresql.core.Field;
89

9-
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/Jdbc2Statement.java,v 1.5 2003/03/07 18:39:45 barry Exp $
10+
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/Jdbc2Statement.java,v 1.6 2003/05/03 20:40:45 barry Exp $
1011
* This class implements the java.sql.Statement interface for JDBC2.
1112
* However most of the implementation is really done in
1213
* org.postgresql.jdbc2.AbstractJdbc2Statement or one of it's parents
@@ -23,4 +24,9 @@ public BaseResultSet createResultSet (Field[] fields, Vector tuples, String stat
2324
{
2425
return new Jdbc2ResultSet(this, fields, tuples, status, updateCount, insertOID, binaryCursor);
2526
}
27+
28+
public PGRefCursorResultSet createRefCursorResultSet (String cursorName) throws SQLException
29+
{
30+
return new Jdbc2RefCursorResultSet(this, cursorName);
31+
}
2632
}

src/interfaces/jdbc/org/postgresql/jdbc3/Jdbc3CallableStatement.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import java.sql.*;
55
import java.util.Vector;
6+
import org.postgresql.PGRefCursorResultSet;
67
import org.postgresql.core.BaseResultSet;
78
import org.postgresql.core.Field;
89

@@ -19,5 +20,9 @@ public BaseResultSet createResultSet (Field[] fields, Vector tuples, String stat
1920
return new Jdbc3ResultSet(this, fields, tuples, status, updateCount, insertOID, binaryCursor);
2021
}
2122

23+
public PGRefCursorResultSet createRefCursorResultSet (String cursorName) throws SQLException
24+
{
25+
return new Jdbc3RefCursorResultSet(this, cursorName);
26+
}
2227
}
2328

src/interfaces/jdbc/org/postgresql/jdbc3/Jdbc3PreparedStatement.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33

44
import java.sql.*;
5+
import org.postgresql.PGRefCursorResultSet;
56
import org.postgresql.core.BaseResultSet;
7+
import org.postgresql.core.BaseStatement;
68
import org.postgresql.core.Field;
79

810
public class Jdbc3PreparedStatement extends org.postgresql.jdbc3.AbstractJdbc3Statement implements java.sql.PreparedStatement
@@ -15,8 +17,12 @@ public Jdbc3PreparedStatement(Jdbc3Connection connection, String sql) throws SQL
1517

1618
public BaseResultSet createResultSet (Field[] fields, java.util.Vector tuples, String status, int updateCount, long insertOID, boolean binaryCursor) throws SQLException
1719
{
18-
return new Jdbc3ResultSet(this, fields, tuples, status, updateCount, insertOID, binaryCursor);
20+
return new Jdbc3ResultSet((BaseStatement)this, fields, tuples, status, updateCount, insertOID, binaryCursor);
21+
}
22+
23+
public PGRefCursorResultSet createRefCursorResultSet (String cursorName) throws SQLException
24+
{
25+
return new Jdbc3RefCursorResultSet(this, cursorName);
1926
}
20-
2127
}
2228

0 commit comments

Comments
 (0)