6
6
import java .sql .*;
7
7
import org .postgresql .*;
8
8
import org .postgresql .util .PSQLException ;
9
+ import org .postgresql .jdbc1 .AbstractJdbc1Connection ;
10
+ import org .postgresql .jdbc1 .AbstractJdbc1ResultSet ;
11
+ import org .postgresql .jdbc1 .AbstractJdbc1Statement ;
9
12
10
13
/*
11
14
* Executes a query on the backend.
12
15
*
13
16
* <p>The lifetime of a QueryExecutor object is from sending the query
14
17
* until the response has been received from the backend.
15
18
*
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 $
17
20
*/
18
21
19
22
public class QueryExecutor
20
23
{
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 ;
21
39
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 ();
27
42
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 )
32
52
throws SQLException
33
53
{
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 ();
42
61
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 ();
44
68
}
45
69
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
+
46
83
private Field [] fields = null ;
47
84
private Vector tuples = new Vector ();
48
85
private boolean binaryCursor = false ;
@@ -51,10 +88,12 @@ public QueryExecutor(String[] p_sqlFrags, Object[] p_binds,
51
88
private long insert_oid = 0 ;
52
89
private int maxRows ;
53
90
91
+
54
92
/*
55
93
* Execute a query on the backend.
94
+ *
56
95
*/
57
- public java .sql .ResultSet execute () throws SQLException
96
+ private java .sql .ResultSet execute () throws SQLException
58
97
{
59
98
60
99
StringBuffer errorMessage = null ;
@@ -130,7 +169,18 @@ public java.sql.ResultSet execute() throws SQLException
130
169
if ( errorMessage != null )
131
170
throw new SQLException ( errorMessage .toString () );
132
171
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 ;
134
184
}
135
185
}
136
186
@@ -145,10 +195,12 @@ private void sendQuery() throws SQLException
145
195
for (int i = 0 ; i < m_binds .length ; ++i )
146
196
{
147
197
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
+
149
200
pg_stream .Send (connection .getEncoding ().encode (m_sqlFrags [i ]));
150
201
pg_stream .Send (connection .getEncoding ().encode (m_binds [i ].toString ()));
151
202
}
203
+
152
204
pg_stream .Send (connection .getEncoding ().encode (m_sqlFrags [m_binds .length ]));
153
205
pg_stream .SendChar (0 );
154
206
pg_stream .flush ();
0 commit comments