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: SQLFetch.c,v 1.4 2009/02/18 17:59:08 lurcher Exp $
31 *
32 * $Log: SQLFetch.c,v $
33 * Revision 1.4 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.3 2003/10/30 18:20:45 lurcher
37 *
38 * Fix broken thread protection
39 * Remove SQLNumResultCols after execute, lease S4/S% to driver
40 * Fix string overrun in SQLDriverConnect
41 * Add initial support for Interix
42 *
43 * Revision 1.2 2002/12/05 17:44:30 lurcher
44 *
45 * Display unknown return values in return logging
46 *
47 * Revision 1.1.1.1 2001/10/17 16:40:05 lurcher
48 *
49 * First upload to SourceForge
50 *
51 * Revision 1.4 2001/04/12 17:43:36 nick
52 *
53 * Change logging and added autotest to odbctest
54 *
55 * Revision 1.3 2001/01/14 10:09:39 nick
56 *
57 * Add sqi/test to build
58 *
59 * Revision 1.2 2001/01/09 22:33:13 nick
60 *
61 * Stop passing NULL into SQLExtendedFetch
62 * Further fixes to unicode to ansi conversions
63 *
64 * Revision 1.1.1.1 2000/09/04 16:42:52 nick
65 * Imported Sources
66 *
67 * Revision 1.9 2000/02/11 00:41:46 ngorham
68 *
69 * Added a couple of fixes for drivers without SQLExtendedFetch
70 *
71 * Revision 1.8 2000/02/02 07:55:21 ngorham
72 *
73 * Add flag to disable SQLFetch -> SQLExtendedFetch mapping
74 *
75 * Revision 1.7 1999/11/13 23:40:59 ngorham
76 *
77 * Alter the way DM logging works
78 * Upgrade the Postgres driver to 6.4.6
79 *
80 * Revision 1.6 1999/10/24 23:54:18 ngorham
81 *
82 * First part of the changes to the error reporting
83 *
84 * Revision 1.5 1999/09/21 22:34:24 ngorham
85 *
86 * Improve performance by removing unneeded logging calls when logging is
87 * disabled
88 *
89 * Revision 1.4 1999/07/10 21:10:16 ngorham
90 *
91 * Adjust error sqlstate from driver manager, depending on requested
92 * version (ODBC2/3)
93 *
94 * Revision 1.3 1999/07/04 21:05:07 ngorham
95 *
96 * Add LGPL Headers to code
97 *
98 * Revision 1.2 1999/06/30 23:56:54 ngorham
99 *
100 * Add initial thread safety code
101 *
102 * Revision 1.1.1.1 1999/05/29 13:41:06 sShandyb
103 * first go at it
104 *
105 * Revision 1.2 1999/06/02 23:48:45 ngorham
106 *
107 * Added more 3-2 mapping
108 *
109 * Revision 1.1.1.1 1999/05/27 18:23:17 pharvey
110 * Imported sources
111 *
112 * Revision 1.4 1999/05/03 19:50:43 nick
113 * Another check point
114 *
115 * Revision 1.3 1999/04/30 16:22:47 nick
116 * Another checkpoint
117 *
118 * Revision 1.2 1999/04/29 20:47:37 nick
119 * Another checkpoint
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: SQLFetch.c,v $ $Revision: 1.4 $";
131
132SQLRETURN SQLFetch( 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 /*
169 * check states
170 */
171
172 thread_protect( SQL_HANDLE_STMT, statement );
173
174 if ( statement -> state == STATE_S1 ||
175 statement -> state == STATE_S2 ||
176 statement -> state == STATE_S3 )
177 {
178 dm_log_write( __FILE__,
179 __LINE__,
180 LOG_INFO,
181 LOG_INFO,
182 "Error: HY010" );
183
184 __post_internal_error( &statement -> error,
185 ERROR_HY010, NULL,
186 statement -> connection -> environment -> requested_version );
187
188 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
189 }
190
191 if ( statement -> state == STATE_S4 )
192 {
193 dm_log_write( __FILE__,
194 __LINE__,
195 LOG_INFO,
196 LOG_INFO,
197 "Error: 24000" );
198
199 __post_internal_error( &statement -> error,
200 ERROR_24000, NULL,
201 statement -> connection -> environment -> requested_version );
202
203 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
204 }
205
206 if ( statement -> state == STATE_S7 ||
207 statement -> state == STATE_S8 ||
208 statement -> state == STATE_S9 ||
209 statement -> state == STATE_S10 ||
210 statement -> state == STATE_S13 ||
211 statement -> state == STATE_S14 ||
212 statement -> state == STATE_S15 )
213 {
214 dm_log_write( __FILE__,
215 __LINE__,
216 LOG_INFO,
217 LOG_INFO,
218 "Error: HY010" );
219
220 __post_internal_error( &statement -> error,
221 ERROR_HY010, NULL,
222 statement -> connection -> environment -> requested_version );
223
224 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
225 }
226
227 if ( statement -> state == STATE_S11 ||
228 statement -> state == STATE_S12 )
229 {
230 if ( statement -> interupted_func != SQL_API_SQLFETCH )
231 {
232 dm_log_write( __FILE__,
233 __LINE__,
234 LOG_INFO,
235 LOG_INFO,
236 "Error: HY010" );
237
238 __post_internal_error( &statement -> error,
239 ERROR_HY010, NULL,
240 statement -> connection -> environment -> requested_version );
241
242 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
243 }
244 }
245
246 if ( CHECK_SQLFETCH( statement -> connection ))
247 {
248 /*
249 * this is odd, but its in the book
250 */
251
252 if ( statement -> connection -> driver_act_ver == SQL_OV_ODBC2 &&
253 CHECK_SQLEXTENDEDFETCH( statement -> connection ) &&
254 statement -> connection -> ex_fetch_mapping )
255 {
256 /*
257 * the row_st_arr can't be null
258 */
259
260 if ( statement -> row_st_arr )
261 {
262 ret = SQLEXTENDEDFETCH( statement -> connection,
263 statement -> driver_stmt,
264 SQL_FETCH_NEXT,
265 NULL,
266 statement -> row_ct_ptr,
267 statement -> row_st_arr );
268 }
269 else
270 {
271 SQLUSMALLINT *row_st_arr;
272 int row_count;
273 SQLUSMALLINT row_status;
274
275 if ( statement -> row_array_size <= 1 )
276 {
277 row_count = 1;
278 row_st_arr = &row_status;
279 }
280 else
281 {
282 row_count = statement -> row_array_size;
283 row_st_arr = malloc( sizeof( SQLUSMALLINT ) * row_count );
284 }
285
286 ret = SQLEXTENDEDFETCH( statement -> connection,
287 statement -> driver_stmt,
288 SQL_FETCH_NEXT,
289 NULL,
290 statement -> row_ct_ptr,
291 row_st_arr );
292
293 if ( row_count > 1 )
294 {
295 free( row_st_arr );
296 }
297 }
298 }
299 else
300 {
301 ret = SQLFETCH( statement -> connection,
302 statement -> driver_stmt );
303
304 if( statement -> connection -> driver_act_ver == SQL_OV_ODBC2 &&
305 statement -> row_ct_ptr ) {
306 if( ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO ) {
307 *statement -> row_ct_ptr = 1;
308 } else {
309 *statement -> row_ct_ptr = 0;
310 }
311 }
312 }
313 }
314 else
315 {
316 dm_log_write( __FILE__,
317 __LINE__,
318 LOG_INFO,
319 LOG_INFO,
320 "Error: IM001" );
321
322 __post_internal_error( &statement -> error,
323 ERROR_IM001, NULL,
324 statement -> connection -> environment -> requested_version );
325
326 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
327 }
328
329 if ( ret == SQL_STILL_EXECUTING )
330 {
331 statement -> interupted_func = SQL_API_SQLFETCH;
332 if ( statement -> state != STATE_S11 &&
333 statement -> state != STATE_S12 )
334 statement -> state = STATE_S11;
335 }
336 else if ( SQL_SUCCEEDED( ret ))
337 {
338 statement -> state = STATE_S6;
339 statement -> eod = 0;
340 }
341 else if ( ret == SQL_NO_DATA ) {
342 statement -> eod = 1;
343 }
344
345 if ( log_info.log_flag )
346 {
347 sprintf( statement -> msg,
348 "\n\t\tExit:[%s]",
349 __get_return_status( ret, s1 ));
350
351 dm_log_write( __FILE__,
352 __LINE__,
353 LOG_INFO,
354 LOG_INFO,
355 statement -> msg );
356 }
357
358 return function_return( SQL_HANDLE_STMT, statement, ret );
359}
360