Skip to content

Commit 16a3034

Browse files
author
Barry Lind
committed
Patch from Nic Ferrier to add support for result sets being cursor based
so that rows can be fetched incrementally. This is enabled by using setFetchSize()
1 parent 2d1f940 commit 16a3034

26 files changed

+428
-231
lines changed

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

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,80 @@
66
import java.sql.*;
77
import org.postgresql.*;
88
import org.postgresql.util.PSQLException;
9+
import org.postgresql.jdbc1.AbstractJdbc1Connection;
10+
import org.postgresql.jdbc1.AbstractJdbc1ResultSet;
11+
import org.postgresql.jdbc1.AbstractJdbc1Statement;
912

1013
/*
1114
* Executes a query on the backend.
1215
*
1316
* <p>The lifetime of a QueryExecutor object is from sending the query
1417
* until the response has been received from the backend.
1518
*
16-
* $Id: QueryExecutor.java,v 1.17 2002/11/14 05:35:45 barry Exp $
19+
* $Id: QueryExecutor.java,v 1.18 2003/02/04 09:20:08 barry Exp $
1720
*/
1821

1922
public class QueryExecutor
2023
{
24+
//This version of execute does not take an existing result set, but
25+
//creates a new one for the results of the query
26+
public static ResultSet execute (String[] p_sqlFrags,
27+
Object[] p_binds,
28+
java.sql.Statement statement)
29+
throws SQLException
30+
{
31+
QueryExecutor qe = new QueryExecutor();
32+
qe.m_sqlFrags = p_sqlFrags;
33+
qe.m_binds = p_binds;
34+
qe.statement = statement;
35+
if (statement != null)
36+
qe.maxRows = statement.getMaxRows();
37+
else
38+
qe.maxRows = 0;
2139

22-
private final String[] m_sqlFrags;
23-
private final Object[] m_binds;
24-
private final java.sql.Statement statement;
25-
private final PG_Stream pg_stream;
26-
private final org.postgresql.jdbc1.AbstractJdbc1Connection connection;
40+
qe.connection = (AbstractJdbc1Connection)((AbstractJdbc1Statement)statement).getPGConnection();
41+
qe.pg_stream = qe.connection.getPGStream();
2742

28-
public QueryExecutor(String[] p_sqlFrags, Object[] p_binds,
29-
java.sql.Statement statement,
30-
PG_Stream pg_stream,
31-
java.sql.Connection connection)
43+
return qe.execute();
44+
}
45+
46+
//This version of execute reuses an existing result set for the query
47+
//results, this is used when a result set is backed by a cursor and
48+
//more results are fetched
49+
public static void execute (String[] p_sqlFrags,
50+
Object[] p_binds,
51+
java.sql.ResultSet rs)
3252
throws SQLException
3353
{
34-
this.m_sqlFrags = p_sqlFrags;
35-
this.m_binds = p_binds;
36-
this.statement = statement;
37-
this.pg_stream = pg_stream;
38-
this.connection = (org.postgresql.jdbc1.AbstractJdbc1Connection)connection;
39-
40-
if (statement != null)
41-
maxRows = statement.getMaxRows();
54+
QueryExecutor qe = new QueryExecutor();
55+
qe.m_sqlFrags = p_sqlFrags;
56+
qe.m_binds = p_binds;
57+
qe.rs = rs;
58+
qe.statement = (java.sql.Statement)((AbstractJdbc1ResultSet)qe.rs).getPGStatement();
59+
if (qe.statement != null)
60+
qe.maxRows = qe.statement.getMaxRows();
4261
else
43-
maxRows = 0;
62+
qe.maxRows = 0;
63+
64+
qe.connection = (AbstractJdbc1Connection)((AbstractJdbc1Statement)qe.statement).getPGConnection();
65+
qe.pg_stream = qe.connection.getPGStream();
66+
67+
qe.execute();
4468
}
4569

70+
71+
private QueryExecutor ()
72+
{
73+
}
74+
75+
private String[] m_sqlFrags;
76+
private Object[] m_binds;
77+
private java.sql.Statement statement;
78+
private java.sql.ResultSet rs;
79+
80+
private AbstractJdbc1Connection connection;
81+
private PG_Stream pg_stream;
82+
4683
private Field[] fields = null;
4784
private Vector tuples = new Vector();
4885
private boolean binaryCursor = false;
@@ -51,10 +88,12 @@ public QueryExecutor(String[] p_sqlFrags, Object[] p_binds,
5188
private long insert_oid = 0;
5289
private int maxRows;
5390

91+
5492
/*
5593
* Execute a query on the backend.
94+
*
5695
*/
57-
public java.sql.ResultSet execute() throws SQLException
96+
private java.sql.ResultSet execute() throws SQLException
5897
{
5998

6099
StringBuffer errorMessage = null;
@@ -130,7 +169,18 @@ public java.sql.ResultSet execute() throws SQLException
130169
if ( errorMessage != null )
131170
throw new SQLException( errorMessage.toString() );
132171

133-
return connection.getResultSet(statement, fields, tuples, status, update_count, insert_oid, binaryCursor);
172+
173+
//if an existing result set was passed in reuse it, else
174+
//create a new one
175+
if (rs != null)
176+
{
177+
((org.postgresql.jdbc1.AbstractJdbc1ResultSet)rs).reInit(fields, tuples, status, update_count, insert_oid, binaryCursor);
178+
}
179+
else
180+
{
181+
rs = ((AbstractJdbc1Statement)statement).createResultSet(fields, tuples, status, update_count, insert_oid, binaryCursor);
182+
}
183+
return rs;
134184
}
135185
}
136186

@@ -145,10 +195,12 @@ private void sendQuery() throws SQLException
145195
for (int i = 0 ; i < m_binds.length ; ++i)
146196
{
147197
if (m_binds[i] == null)
148-
throw new PSQLException("postgresql.prep.param", new Integer(i + 1));
198+
throw new PSQLException("postgresql.prep.param (" + i + ")", new Integer(i + 1));
199+
149200
pg_stream.Send(connection.getEncoding().encode(m_sqlFrags[i]));
150201
pg_stream.Send(connection.getEncoding().encode(m_binds[i].toString()));
151202
}
203+
152204
pg_stream.Send(connection.getEncoding().encode(m_sqlFrags[m_binds.length]));
153205
pg_stream.SendChar(0);
154206
pg_stream.flush();

0 commit comments

Comments
 (0)