9
9
* Copyright (c) 2003, PostgreSQL Global Development Group
10
10
*
11
11
* IDENTIFICATION
12
- * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1ResultSet.java,v 1.21 2003/09/22 04:54:59 barry Exp $
12
+ * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1ResultSet.java,v 1.22 2003/10/29 02:39:09 davec Exp $
13
13
*
14
14
*-------------------------------------------------------------------------
15
15
*/
@@ -61,6 +61,9 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet
61
61
private SimpleDateFormat m_tstzFormat = null ;
62
62
private SimpleDateFormat m_dateFormat = null ;
63
63
64
+ private int fetchSize ; // Fetch size for next read (might be 0).
65
+ private int lastFetchSize ; // Fetch size of last read (might be 0).
66
+
64
67
public abstract ResultSetMetaData getMetaData () throws SQLException ;
65
68
66
69
public AbstractJdbc1ResultSet (BaseStatement statement ,
@@ -82,6 +85,8 @@ public AbstractJdbc1ResultSet(BaseStatement statement,
82
85
this .this_row = null ;
83
86
this .current_row = -1 ;
84
87
this .binaryCursor = binaryCursor ;
88
+
89
+ this .lastFetchSize = this .fetchSize = (statement == null ? 0 : statement .getFetchSize ());
85
90
}
86
91
87
92
public BaseStatement getPGStatement () {
@@ -111,7 +116,21 @@ public void reInit (Field[] fields, Vector tuples, String status,
111
116
this .current_row = -1 ;
112
117
this .binaryCursor = binaryCursor ;
113
118
}
119
+
120
+ //
121
+ // Part of the JDBC2 support, but convenient to implement here.
122
+ //
114
123
124
+ public void setFetchSize (int rows ) throws SQLException
125
+ {
126
+ fetchSize = rows ;
127
+ }
128
+
129
+
130
+ public int getFetchSize () throws SQLException
131
+ {
132
+ return fetchSize ;
133
+ }
115
134
116
135
public boolean next () throws SQLException
117
136
{
@@ -120,30 +139,32 @@ public boolean next() throws SQLException
120
139
121
140
if (++current_row >= rows .size ())
122
141
{
123
- int fetchSize = statement .getFetchSize ();
124
- // Must be false if we weren't batching.
125
- if (fetchSize == 0 )
126
- return false ;
127
- // Use the ref to the statement to get
128
- // the details we need to do another cursor
129
- // query - it will use reinit() to repopulate this
130
- // with the right data.
131
- String [] sql = new String [1 ];
132
- String [] binds = new String [0 ];
133
- // Is this the correct query???
134
- String cursorName = statement .getStatementName ();
135
- //if cursorName is null, we are not batching (likely because the
136
- //query itself can't be batched)
137
- if (cursorName == null )
138
- return false ;
139
- sql [0 ] = "FETCH FORWARD " + fetchSize + " FROM " + cursorName ;
140
- QueryExecutor .execute (sql ,
141
- binds ,
142
- this );
143
-
144
- // Test the new rows array.
145
- if (rows .size () == 0 )
146
- return false ;
142
+ String cursorName = statement .getFetchingCursorName ();
143
+ if (cursorName == null || lastFetchSize == 0 || rows .size () < lastFetchSize )
144
+ return false ; // Not doing a cursor-based fetch or the last fetch was the end of the query
145
+
146
+ // Use the ref to the statement to get
147
+ // the details we need to do another cursor
148
+ // query - it will use reinit() to repopulate this
149
+ // with the right data.
150
+
151
+ // NB: We can reach this point with fetchSize == 0
152
+ // if the fetch size is changed halfway through reading results.
153
+ // Use "FETCH FORWARD ALL" in that case to complete the query.
154
+ String [] sql = new String [] {
155
+ fetchSize == 0 ? ("FETCH FORWARD ALL FROM " + cursorName ) :
156
+ ("FETCH FORWARD " + fetchSize + " FROM " + cursorName )
157
+ };
158
+
159
+ QueryExecutor .execute (sql ,
160
+ new String [0 ],
161
+ this );
162
+
163
+ // Test the new rows array.
164
+ lastFetchSize = fetchSize ;
165
+ if (rows .size () == 0 )
166
+ return false ;
167
+
147
168
// Otherwise reset the counter and let it go on...
148
169
current_row = 0 ;
149
170
}
0 commit comments