1/*********************************************************************
2 *
3 * This is based on code created by Peter Harvey,
4 * (pharvey@codebydesign.com).
5 *
6 * Modified and extended by Nick Gorham
7 * (nick@lurcher.org).
8 *
9 * Any bugs or problems should be considered the fault of Nick and not
10 * Peter.
11 *
12 * copyright (c) 1999 Nick Gorham
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 **********************************************************************
29 *
30 * $Id: SQLMoreResults.c,v 1.8 2009/02/18 17:59:08 lurcher Exp $
31 *
32 * $Log: SQLMoreResults.c,v $
33 * Revision 1.8 2009/02/18 17:59:08 lurcher
34 * Shift to using config.h, the compile lines were making it hard to spot warnings
35 *
36 * Revision 1.7 2006/03/08 09:18:41 lurcher
37 * fix silly typo that was using sizeof( SQL_WCHAR ) instead of SQLWCHAR
38 *
39 * Revision 1.6 2003/12/19 16:25:38 lurcher
40 *
41 * Fix incorrect state in SQLMoreResults.c
42 *
43 * Revision 1.5 2003/10/30 18:20:46 lurcher
44 *
45 * Fix broken thread protection
46 * Remove SQLNumResultCols after execute, lease S4/S% to driver
47 * Fix string overrun in SQLDriverConnect
48 * Add initial support for Interix
49 *
50 * Revision 1.4 2003/01/27 15:01:01 lurcher
51 *
52 * On error from SQLMoreResults DONT change to S1
53 *
54 * Revision 1.3 2002/12/05 17:44:31 lurcher
55 *
56 * Display unknown return values in return logging
57 *
58 * Revision 1.2 2002/08/15 08:10:33 lurcher
59 *
60 * Couple of small fixes from John L Miller
61 *
62 * Revision 1.1.1.1 2001/10/17 16:40:06 lurcher
63 *
64 * First upload to SourceForge
65 *
66 * Revision 1.2 2001/04/12 17:43:36 nick
67 *
68 * Change logging and added autotest to odbctest
69 *
70 * Revision 1.1.1.1 2000/09/04 16:42:52 nick
71 * Imported Sources
72 *
73 * Revision 1.9 2000/06/20 12:44:00 ngorham
74 *
75 * Fix bug that caused a success with info message from SQLExecute or
76 * SQLExecDirect to be lost if used with a ODBC 3 driver and the application
77 * called SQLGetDiagRec
78 *
79 * Revision 1.8 2000/06/16 16:52:18 ngorham
80 *
81 * Stop info messages being lost when calling SQLExecute etc on ODBC 3
82 * drivers, the SQLNumResultCols were clearing the error before
83 * function return had a chance to get to them
84 *
85 * Revision 1.7 1999/11/13 23:41:00 ngorham
86 *
87 * Alter the way DM logging works
88 * Upgrade the Postgres driver to 6.4.6
89 *
90 * Revision 1.6 1999/10/24 23:54:18 ngorham
91 *
92 * First part of the changes to the error reporting
93 *
94 * Revision 1.5 1999/09/21 22:34:25 ngorham
95 *
96 * Improve performance by removing unneeded logging calls when logging is
97 * disabled
98 *
99 * Revision 1.4 1999/07/10 21:10:16 ngorham
100 *
101 * Adjust error sqlstate from driver manager, depending on requested
102 * version (ODBC2/3)
103 *
104 * Revision 1.3 1999/07/04 21:05:08 ngorham
105 *
106 * Add LGPL Headers to code
107 *
108 * Revision 1.2 1999/06/30 23:56:55 ngorham
109 *
110 * Add initial thread safety code
111 *
112 * Revision 1.1.1.1 1999/05/29 13:41:07 sShandyb
113 * first go at it
114 *
115 * Revision 1.1.1.1 1999/05/27 18:23:18 pharvey
116 * Imported sources
117 *
118 * Revision 1.2 1999/05/03 19:50:43 nick
119 * Another check point
120 *
121 * Revision 1.1 1999/04/25 23:06:11 nick
122 * Initial revision
123 *
124 *
125 **********************************************************************/
126
127#include <config.h>
128#include "drivermanager.h"
129
130static char const rcsid[]= "$RCSfile: SQLMoreResults.c,v $ $Revision: 1.8 $";
131
132SQLRETURN SQLMoreResults( SQLHSTMT statement_handle )
133{
134 DMHSTMT statement = (DMHSTMT) statement_handle;
135 SQLRETURN ret;
136 SQLCHAR s1[ 100 + LOG_MESSAGE_LEN ];
137
138 /*
139 * check statement
140 */
141
142 if ( !__validate_stmt( statement ))
143 {
144 dm_log_write( __FILE__,
145 __LINE__,
146 LOG_INFO,
147 LOG_INFO,
148 "Error: SQL_INVALID_HANDLE" );
149
150 return SQL_INVALID_HANDLE;
151 }
152
153 function_entry( statement );
154
155 if ( log_info.log_flag )
156 {
157 sprintf( statement -> msg, "\n\t\tEntry:\
158\n\t\t\tStatement = %p",
159 statement );
160
161 dm_log_write( __FILE__,
162 __LINE__,
163 LOG_INFO,
164 LOG_INFO,
165 statement -> msg );
166 }
167
168 thread_protect( SQL_HANDLE_STMT, statement );
169
170 /*
171 * check states
172 */
173 if ( statement -> state == STATE_S1 ||
174 /* statement -> state == STATE_S2 || */
175 statement -> state == STATE_S3 )
176 {
177 sprintf( statement -> msg,
178 "\n\t\tExit:[%s]",
179 __get_return_status( SQL_NO_DATA, s1 ));
180
181 dm_log_write( __FILE__,
182 __LINE__,
183 LOG_INFO,
184 LOG_INFO,
185 statement -> msg );
186
187 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_NO_DATA );
188 }
189 else if ( statement -> state == STATE_S8 ||
190 statement -> state == STATE_S9 ||
191 statement -> state == STATE_S10 )
192 {
193 dm_log_write( __FILE__,
194 __LINE__,
195 LOG_INFO,
196 LOG_INFO,
197 "Error: HY010" );
198
199 __post_internal_error( &statement -> error,
200 ERROR_HY010, NULL,
201 statement -> connection -> environment -> requested_version );
202
203 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
204 }
205 else if ( statement -> state == STATE_S11 ||
206 statement -> state == STATE_S12 )
207 {
208 if ( statement -> interupted_func != SQL_API_SQLMORERESULTS )
209 {
210 dm_log_write( __FILE__,
211 __LINE__,
212 LOG_INFO,
213 LOG_INFO,
214 "Error: HY010" );
215
216 __post_internal_error( &statement -> error,
217 ERROR_HY010, NULL,
218 statement -> connection -> environment -> requested_version );
219
220 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
221 }
222 }
223
224#ifdef NR_PROBE
225 if ( !CHECK_SQLMORERESULTS( statement -> connection ) ||
226 !CHECK_SQLNUMRESULTCOLS( statement -> connection ))
227#else
228 if ( !CHECK_SQLMORERESULTS( statement -> connection ))
229#endif
230 {
231 dm_log_write( __FILE__,
232 __LINE__,
233 LOG_INFO,
234 LOG_INFO,
235 "Error: IM001" );
236
237 __post_internal_error( &statement -> error,
238 ERROR_IM001, NULL,
239 statement -> connection -> environment -> requested_version );
240
241 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
242 }
243
244 ret = SQLMORERESULTS( statement -> connection ,
245 statement -> driver_stmt );
246
247 if ( SQL_SUCCEEDED( ret ))
248 {
249#ifdef NR_PROBE
250 /*
251 * grab any errors
252 */
253
254 if ( ret == SQL_SUCCESS_WITH_INFO )
255 {
256 function_return_ex( IGNORE_THREAD, statement, ret, TRUE );
257 }
258
259 SQLNUMRESULTCOLS( statement -> connection,
260 statement -> driver_stmt, &statement -> numcols );
261
262 if ( statement -> numcols == 0 )
263 {
264 statement -> state = STATE_S4;
265 }
266 else
267 {
268 statement -> state = STATE_S5;
269 }
270#else
271 /*
272 * We don't know for sure
273 */
274 statement -> hascols = 0;
275 statement -> state = STATE_S5;
276#endif
277 }
278 else if ( ret == SQL_STILL_EXECUTING )
279 {
280 statement -> interupted_func = SQL_API_SQLEXECUTE;
281 if ( statement -> state != STATE_S11 &&
282 statement -> state != STATE_S12 )
283 statement -> state = STATE_S11;
284 }
285 else if ( ret == SQL_NO_DATA )
286 {
287 if ( statement -> prepared )
288 {
289 if ( statement -> state == STATE_S4 )
290 {
291 statement -> state = STATE_S2;
292 }
293 else
294 {
295 statement -> state = STATE_S3;
296 }
297 }
298 else
299 {
300 statement -> state = STATE_S1;
301 }
302 }
303 else if ( ret == SQL_NEED_DATA )
304 {
305 statement -> interupted_func = SQL_API_SQLMORERESULTS;
306 statement -> interupted_state = statement -> state;
307 statement -> state = STATE_S8;
308 }
309 else if ( ret == SQL_PARAM_DATA_AVAILABLE )
310 {
311 statement -> interupted_func = SQL_API_SQLMORERESULTS;
312 statement -> interupted_state = statement -> state;
313 statement -> state = STATE_S13;
314 }
315 else
316 {
317 /*
318 * Leave the state where it is
319 */
320 }
321
322 if ( log_info.log_flag )
323 {
324 sprintf( statement -> msg,
325 "\n\t\tExit:[%s]",
326 __get_return_status( ret, s1 ));
327
328 dm_log_write( __FILE__,
329 __LINE__,
330 LOG_INFO,
331 LOG_INFO,
332 statement -> msg );
333 }
334
335 return function_return( SQL_HANDLE_STMT, statement, ret );
336}
337