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: SQLSetStmtOptionW.c,v 1.6 2009/02/18 17:59:08 lurcher Exp $
31 *
32 * $Log: SQLSetStmtOptionW.c,v $
33 * Revision 1.6 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.5 2009/02/04 09:30:02 lurcher
37 * Fix some SQLINTEGER/SQLLEN conflicts
38 *
39 * Revision 1.4 2007/11/29 12:00:31 lurcher
40 * Add 64 bit type changes to SQLExtendedFetch etc
41 *
42 * Revision 1.3 2007/02/28 15:37:48 lurcher
43 * deal with drivers that call internal W functions and end up in the driver manager. controlled by the --enable-handlemap configure arg
44 *
45 * Revision 1.2 2006/04/18 10:24:47 lurcher
46 * Add a couple of changes from Mark Vanderwiel
47 *
48 * Revision 1.1 2005/05/02 20:37:31 lurcher
49 * Add missing SQLSetStmtOptionW.c
50 *
51 * Revision 1.5 2003/10/30 18:20:46 lurcher
52 *
53 * Fix broken thread protection
54 * Remove SQLNumResultCols after execute, lease S4/S% to driver
55 * Fix string overrun in SQLDriverConnect
56 * Add initial support for Interix
57 *
58 * Revision 1.4 2003/03/05 09:48:45 lurcher
59 *
60 * Add some 64 bit fixes
61 *
62 * Revision 1.3 2002/12/05 17:44:31 lurcher
63 *
64 * Display unknown return values in return logging
65 *
66 * Revision 1.2 2001/12/13 13:00:32 lurcher
67 *
68 * Remove most if not all warnings on 64 bit platforms
69 * Add support for new MS 3.52 64 bit changes
70 * Add override to disable the stopping of tracing
71 * Add MAX_ROWS support in postgres driver
72 *
73 * Revision 1.1.1.1 2001/10/17 16:40:07 lurcher
74 *
75 * First upload to SourceForge
76 *
77 * Revision 1.4 2001/08/08 17:05:17 nick
78 *
79 * Add support for attribute setting in the ini files
80 *
81 * Revision 1.3 2001/07/03 09:30:41 nick
82 *
83 * Add ability to alter size of displayed message in the log
84 *
85 * Revision 1.2 2001/04/12 17:43:36 nick
86 *
87 * Change logging and added autotest to odbctest
88 *
89 * Revision 1.1.1.1 2000/09/04 16:42:52 nick
90 * Imported Sources
91 *
92 * Revision 1.8 2000/06/20 13:30:12 ngorham
93 *
94 * Fix problems when using bookmarks
95 *
96 * Revision 1.7 1999/11/13 23:41:01 ngorham
97 *
98 * Alter the way DM logging works
99 * Upgrade the Postgres driver to 6.4.6
100 *
101 * Revision 1.6 1999/10/24 23:54:19 ngorham
102 *
103 * First part of the changes to the error reporting
104 *
105 * Revision 1.5 1999/09/21 22:34:26 ngorham
106 *
107 * Improve performance by removing unneeded logging calls when logging is
108 * disabled
109 *
110 * Revision 1.4 1999/07/10 21:10:17 ngorham
111 *
112 * Adjust error sqlstate from driver manager, depending on requested
113 * version (ODBC2/3)
114 *
115 * Revision 1.3 1999/07/04 21:05:08 ngorham
116 *
117 * Add LGPL Headers to code
118 *
119 * Revision 1.2 1999/06/30 23:56:55 ngorham
120 *
121 * Add initial thread safety code
122 *
123 * Revision 1.1.1.1 1999/05/29 13:41:09 sShandyb
124 * first go at it
125 *
126 * Revision 1.4 1999/06/03 22:20:25 ngorham
127 *
128 * Finished off the ODBC3-2 mapping
129 *
130 * Revision 1.3 1999/06/02 20:12:10 ngorham
131 *
132 * Fixed botched log entry, and removed the dos \r from the sql header files.
133 *
134 * Revision 1.2 1999/06/02 19:57:21 ngorham
135 *
136 * Added code to check if a attempt is being made to compile with a C++
137 * Compiler, and issue a message.
138 * Start work on the ODBC2-3 conversions.
139 *
140 * Revision 1.1.1.1 1999/05/27 18:23:18 pharvey
141 * Imported sources
142 *
143 * Revision 1.2 1999/05/09 23:27:11 nick
144 * All the API done now
145 *
146 * Revision 1.1 1999/04/25 23:06:11 nick
147 * Initial revision
148 *
149 *
150 **********************************************************************/
151
152#include <config.h>
153#include "drivermanager.h"
154
155static char const rcsid[]= "$RCSfile: SQLSetStmtOptionW.c,v $ $Revision: 1.6 $";
156
157SQLRETURN SQLSetStmtOptionW( SQLHSTMT statement_handle,
158 SQLUSMALLINT option,
159 SQLULEN value )
160{
161 DMHSTMT statement = (DMHSTMT) statement_handle;
162 SQLRETURN ret;
163 SQLCHAR s1[ 100 + LOG_MESSAGE_LEN ];
164 SQLWCHAR buffer[ 512 ];
165
166 /*
167 * check statement
168 */
169
170 if ( !__validate_stmt( statement ))
171 {
172 dm_log_write( __FILE__,
173 __LINE__,
174 LOG_INFO,
175 LOG_INFO,
176 "Error: SQL_INVALID_HANDLE" );
177
178#ifdef WITH_HANDLE_REDIRECT
179 {
180 DMHSTMT parent_statement;
181
182 parent_statement = find_parent_handle( statement, SQL_HANDLE_STMT );
183
184 if ( parent_statement ) {
185 dm_log_write( __FILE__,
186 __LINE__,
187 LOG_INFO,
188 LOG_INFO,
189 "Info: found parent handle" );
190
191 if ( CHECK_SQLSETSTMTOPTIONW( parent_statement -> connection ))
192 {
193 dm_log_write( __FILE__,
194 __LINE__,
195 LOG_INFO,
196 LOG_INFO,
197 "Info: calling redirected driver function" );
198
199 return SQLSETSTMTOPTIONW( parent_statement -> connection,
200 statement_handle,
201 option,
202 value );
203 }
204 }
205 }
206#endif
207 return SQL_INVALID_HANDLE;
208 }
209
210 function_entry( statement );
211
212 if ( log_info.log_flag )
213 {
214 sprintf( statement -> msg, "\n\t\tEntry:\
215\n\t\t\tStatement = %p\
216\n\t\t\tOption = %s\
217\n\t\t\tValue = %d",
218 statement,
219 __stmt_attr_as_string( s1, option ),
220 (int)value );
221
222 dm_log_write( __FILE__,
223 __LINE__,
224 LOG_INFO,
225 LOG_INFO,
226 statement -> msg );
227 }
228
229 thread_protect( SQL_HANDLE_STMT, statement );
230
231 /*
232 * check states
233 */
234
235 if ( option == SQL_CONCURRENCY ||
236 option == SQL_CURSOR_TYPE ||
237 option == SQL_SIMULATE_CURSOR ||
238 option == SQL_USE_BOOKMARKS )
239 {
240 if ( statement -> state == STATE_S2 ||
241 statement -> state == STATE_S3 )
242 {
243 dm_log_write( __FILE__,
244 __LINE__,
245 LOG_INFO,
246 LOG_INFO,
247 "Error: S1011" );
248
249 __post_internal_error( &statement -> error,
250 ERROR_S1011, NULL,
251 statement -> connection -> environment -> requested_version );
252
253 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
254 }
255 else if ( statement -> state == STATE_S4 ||
256 statement -> state == STATE_S5 ||
257 statement -> state == STATE_S6 ||
258 statement -> state == STATE_S7 )
259 {
260 dm_log_write( __FILE__,
261 __LINE__,
262 LOG_INFO,
263 LOG_INFO,
264 "Error: 24000" );
265
266 __post_internal_error( &statement -> error,
267 ERROR_24000, NULL,
268 statement -> connection -> environment -> requested_version );
269
270 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
271 }
272 else if ( statement -> state == STATE_S8 ||
273 statement -> state == STATE_S9 ||
274 statement -> state == STATE_S10 ||
275 statement -> state == STATE_S11 ||
276 statement -> state == STATE_S12 ||
277 statement -> state == STATE_S13 ||
278 statement -> state == STATE_S14 ||
279 statement -> state == STATE_S15 )
280 {
281 if ( statement -> prepared )
282 {
283 dm_log_write( __FILE__,
284 __LINE__,
285 LOG_INFO,
286 LOG_INFO,
287 "Error: S1011" );
288
289 __post_internal_error( &statement -> error,
290 ERROR_S1011, NULL,
291 statement -> connection -> environment -> requested_version );
292
293 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
294 }
295 else
296 {
297 dm_log_write( __FILE__,
298 __LINE__,
299 LOG_INFO,
300 LOG_INFO,
301 "Error: S1010" );
302
303 __post_internal_error( &statement -> error,
304 ERROR_S1010, NULL,
305 statement -> connection -> environment -> requested_version );
306
307 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
308 }
309 }
310 }
311 else
312 {
313 if ( statement -> state == STATE_S8 ||
314 statement -> state == STATE_S9 ||
315 statement -> state == STATE_S10 ||
316 statement -> state == STATE_S11 ||
317 statement -> state == STATE_S12 )
318 {
319 dm_log_write( __FILE__,
320 __LINE__,
321 LOG_INFO,
322 LOG_INFO,
323 "Error: S1010" );
324
325 __post_internal_error( &statement -> error,
326 ERROR_S1010, NULL,
327 statement -> connection -> environment -> requested_version );
328
329 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
330 }
331 }
332
333 if ( option == SQL_ATTR_IMP_ROW_DESC ||
334 option == SQL_ATTR_IMP_PARAM_DESC )
335 {
336 dm_log_write( __FILE__,
337 __LINE__,
338 LOG_INFO,
339 LOG_INFO,
340 "Error: HY017" );
341
342 __post_internal_error( &statement -> error,
343 ERROR_HY017, NULL,
344 statement -> connection -> environment -> requested_version );
345
346 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
347 }
348
349 /*
350 * is it a legitimate value
351 */
352 ret = dm_check_statement_attrs( statement, option, (SQLPOINTER)value );
353
354 if ( ret != SQL_SUCCESS )
355 {
356 dm_log_write( __FILE__,
357 __LINE__,
358 LOG_INFO,
359 LOG_INFO,
360 "Error: HY011" );
361
362 __post_internal_error( &statement -> error,
363 ERROR_HY024, NULL,
364 statement -> connection -> environment -> requested_version );
365
366 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
367 }
368
369 /*
370 * is it something overridden
371 */
372
373 value = (SQLULEN) __attr_override_wide( statement, SQL_HANDLE_STMT, option, (void*) value, NULL, buffer );
374
375 if ( CHECK_SQLSETSTMTOPTIONW( statement -> connection ))
376 {
377 ret = SQLSETSTMTOPTIONW( statement -> connection,
378 statement -> driver_stmt,
379 option,
380 value );
381 }
382 else if ( CHECK_SQLSETSTMTATTRW( statement -> connection ))
383 {
384 switch ( option )
385 {
386 case SQL_ATTR_APP_PARAM_DESC:
387 if ( value )
388 memcpy( &statement -> apd, (void*)value,
389 sizeof( statement -> apd ));
390
391 ret = SQL_SUCCESS;
392 break;
393
394 case SQL_ATTR_APP_ROW_DESC:
395 if ( value )
396 memcpy( &statement -> ard, (void*)value,
397 sizeof( statement -> ard ));
398
399 ret = SQL_SUCCESS;
400 break;
401
402 case SQL_ATTR_IMP_PARAM_DESC:
403 if ( value )
404 memcpy( &statement -> ipd, (void*)value,
405 sizeof( statement -> ipd ));
406
407 ret = SQL_SUCCESS;
408 break;
409
410 case SQL_ATTR_IMP_ROW_DESC:
411 if ( value )
412 memcpy( &statement -> ird, (void*)value,
413 sizeof( statement -> ird ));
414
415 ret = SQL_SUCCESS;
416 break;
417
418 default:
419 ret = SQLSETSTMTATTRW( statement -> connection,
420 statement -> driver_stmt,
421 option,
422 value,
423 SQL_NTS );
424 break;
425 }
426 }
427 else
428 {
429 dm_log_write( __FILE__,
430 __LINE__,
431 LOG_INFO,
432 LOG_INFO,
433 "Error: IM001" );
434
435 __post_internal_error( &statement -> error,
436 ERROR_IM001, NULL,
437 statement -> connection -> environment -> requested_version );
438
439 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
440 }
441
442 /*
443 * take notice of this
444 */
445
446 if ( option == SQL_USE_BOOKMARKS && SQL_SUCCEEDED( ret ))
447 {
448 statement -> bookmarks_on = (SQLULEN) value;
449 }
450
451 if ( log_info.log_flag )
452 {
453 sprintf( statement -> msg,
454 "\n\t\tExit:[%s]",
455 __get_return_status( ret, s1 ));
456
457 dm_log_write( __FILE__,
458 __LINE__,
459 LOG_INFO,
460 LOG_INFO,
461 statement -> msg );
462 }
463
464 return function_return( SQL_HANDLE_STMT, statement, ret );
465}
466