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: SQLSetConnectOptionW.c,v 1.10 2009/02/18 17:59:08 lurcher Exp $
31 *
32 * $Log: SQLSetConnectOptionW.c,v $
33 * Revision 1.10 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.9 2007/02/28 15:37:48 lurcher
37 * deal with drivers that call internal W functions and end up in the driver manager. controlled by the --enable-handlemap configure arg
38 *
39 * Revision 1.8 2006/04/18 10:24:47 lurcher
40 * Add a couple of changes from Mark Vanderwiel
41 *
42 * Revision 1.7 2003/10/30 18:20:46 lurcher
43 *
44 * Fix broken thread protection
45 * Remove SQLNumResultCols after execute, lease S4/S% to driver
46 * Fix string overrun in SQLDriverConnect
47 * Add initial support for Interix
48 *
49 * Revision 1.6 2003/03/05 09:48:45 lurcher
50 *
51 * Add some 64 bit fixes
52 *
53 * Revision 1.5 2002/12/05 17:44:31 lurcher
54 *
55 * Display unknown return values in return logging
56 *
57 * Revision 1.4 2002/07/24 08:49:52 lurcher
58 *
59 * Alter UNICODE support to use iconv for UNICODE-ANSI conversion
60 *
61 * Revision 1.3 2002/01/30 12:20:02 lurcher
62 *
63 * Add MyODBC 3 driver source
64 *
65 * Revision 1.2 2001/12/13 13:00:32 lurcher
66 *
67 * Remove most if not all warnings on 64 bit platforms
68 * Add support for new MS 3.52 64 bit changes
69 * Add override to disable the stopping of tracing
70 * Add MAX_ROWS support in postgres driver
71 *
72 * Revision 1.1.1.1 2001/10/17 16:40:07 lurcher
73 *
74 * First upload to SourceForge
75 *
76 * Revision 1.4 2001/08/08 17:05:17 nick
77 *
78 * Add support for attribute setting in the ini files
79 *
80 * Revision 1.3 2001/07/03 09:30:41 nick
81 *
82 * Add ability to alter size of displayed message in the log
83 *
84 * Revision 1.2 2001/04/12 17:43:36 nick
85 *
86 * Change logging and added autotest to odbctest
87 *
88 * Revision 1.1 2000/12/31 20:30:54 nick
89 *
90 * Add UNICODE support
91 *
92 *
93 **********************************************************************/
94
95#include <config.h>
96#include "drivermanager.h"
97
98static char const rcsid[]= "$RCSfile: SQLSetConnectOptionW.c,v $";
99
100SQLRETURN SQLSetConnectOptionW( SQLHDBC connection_handle,
101 SQLUSMALLINT option,
102 SQLULEN value )
103{
104 DMHDBC connection = (DMHDBC)connection_handle;
105 SQLRETURN ret;
106 SQLCHAR s1[ 100 + LOG_MESSAGE_LEN ];
107 SQLWCHAR buffer[ 512 ];
108
109 /*
110 * doesn't require a handle
111 */
112
113 if ( option == SQL_ATTR_TRACE )
114 {
115 if ((SQLLEN) value != SQL_OPT_TRACE_OFF &&
116 (SQLLEN) value != SQL_OPT_TRACE_ON )
117 {
118 if ( __validate_dbc( connection ))
119 {
120 thread_protect( SQL_HANDLE_DBC, connection );
121 function_entry( connection );
122 dm_log_write( __FILE__,
123 __LINE__,
124 LOG_INFO,
125 LOG_INFO,
126 "Error: HY024" );
127
128 __post_internal_error( &connection -> error,
129 ERROR_HY024, NULL,
130 connection -> environment -> requested_version );
131
132 return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_ERROR );
133 }
134 else
135 {
136 return SQL_INVALID_HANDLE;
137 }
138 }
139
140 if ( value == SQL_OPT_TRACE_OFF )
141 {
142 log_info.log_flag = 0;
143 }
144 else
145 {
146 log_info.log_flag = 1;
147 }
148
149 return SQL_SUCCESS;
150 }
151 else if ( option == SQL_ATTR_TRACEFILE )
152 {
153 if ( value )
154 {
155 if (((SQLWCHAR*)value)[ 0 ] == 0 )
156 {
157 if ( __validate_dbc( connection ))
158 {
159 thread_protect( SQL_HANDLE_DBC, connection );
160 function_entry( connection );
161 dm_log_write( __FILE__,
162 __LINE__,
163 LOG_INFO,
164 LOG_INFO,
165 "Error: HY024" );
166
167 __post_internal_error( &connection -> error,
168 ERROR_HY024, NULL,
169 connection -> environment -> requested_version );
170
171 return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_ERROR );
172 }
173 else
174 {
175 return SQL_INVALID_HANDLE;
176 }
177 }
178 else
179 {
180 if ( log_info.log_file_name )
181 {
182 free( log_info.log_file_name );
183 }
184 log_info.log_file_name = unicode_to_ansi_alloc((SQLWCHAR *) value, SQL_NTS, connection, NULL );
185 }
186 }
187 else
188 {
189 if ( __validate_dbc( connection ))
190 {
191 thread_protect( SQL_HANDLE_DBC, connection );
192 function_entry( connection );
193 dm_log_write( __FILE__,
194 __LINE__,
195 LOG_INFO,
196 LOG_INFO,
197 "Error: HY024" );
198
199 __post_internal_error( &connection -> error,
200 ERROR_HY024, NULL,
201 connection -> environment -> requested_version );
202
203 return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_ERROR );
204 }
205 else
206 {
207 return SQL_INVALID_HANDLE;
208 }
209 }
210 return SQL_SUCCESS;
211 }
212
213 /*
214 * check connection
215 */
216
217 if ( !__validate_dbc( connection ))
218 {
219 dm_log_write( __FILE__,
220 __LINE__,
221 LOG_INFO,
222 LOG_INFO,
223 "Error: SQL_INVALID_HANDLE" );
224
225#ifdef WITH_HANDLE_REDIRECT
226 {
227 DMHDBC parent_connection;
228
229 parent_connection = find_parent_handle( connection, SQL_HANDLE_DBC );
230
231 if ( parent_connection ) {
232 dm_log_write( __FILE__,
233 __LINE__,
234 LOG_INFO,
235 LOG_INFO,
236 "Info: found parent handle" );
237
238 if ( CHECK_SQLSETCONNECTOPTIONW( parent_connection ))
239 {
240 dm_log_write( __FILE__,
241 __LINE__,
242 LOG_INFO,
243 LOG_INFO,
244 "Info: calling redirected driver function" );
245
246 return SQLSETCONNECTOPTIONW( parent_connection,
247 connection_handle,
248 option,
249 value );
250 }
251 }
252 }
253#endif
254 return SQL_INVALID_HANDLE;
255 }
256
257 function_entry( connection );
258
259 if ( log_info.log_flag )
260 {
261 sprintf( connection -> msg, "\n\t\tEntry:\
262\n\t\t\tConnection = %p\
263\n\t\t\tOption = %s\
264\n\t\t\tValue = %d",
265 connection,
266 __con_attr_as_string( s1, option ),
267 (int)value );
268
269 dm_log_write( __FILE__,
270 __LINE__,
271 LOG_INFO,
272 LOG_INFO,
273 connection -> msg );
274 }
275
276 thread_protect( SQL_HANDLE_DBC, connection );
277
278 if ( connection -> state == STATE_C2 )
279 {
280 if ( option == SQL_TRANSLATE_OPTION ||
281 option == SQL_TRANSLATE_DLL )
282 {
283 dm_log_write( __FILE__,
284 __LINE__,
285 LOG_INFO,
286 LOG_INFO,
287 "Error: 08003" );
288
289 __post_internal_error( &connection -> error,
290 ERROR_08003, NULL,
291 connection -> environment -> requested_version );
292
293 return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_ERROR );
294 }
295 }
296 else if ( connection -> state == STATE_C3 )
297 {
298 dm_log_write( __FILE__,
299 __LINE__,
300 LOG_INFO,
301 LOG_INFO,
302 "Error: HY010" );
303
304 __post_internal_error( &connection -> error,
305 ERROR_HY010, NULL,
306 connection -> environment -> requested_version );
307
308 return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_ERROR );
309 }
310 else if ( connection -> state == STATE_C4 ||
311 connection -> state == STATE_C5 )
312 {
313 if ( option == SQL_ODBC_CURSORS )
314 {
315 dm_log_write( __FILE__,
316 __LINE__,
317 LOG_INFO,
318 LOG_INFO,
319 "Error: 08002" );
320
321 __post_internal_error( &connection -> error,
322 ERROR_08002, NULL,
323 connection -> environment -> requested_version );
324
325 return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_ERROR );
326 }
327 }
328 else if ( connection -> state == STATE_C6 )
329 {
330 if ( option == SQL_ODBC_CURSORS )
331 {
332 dm_log_write( __FILE__,
333 __LINE__,
334 LOG_INFO,
335 LOG_INFO,
336 "Error: 08002" );
337
338 __post_internal_error( &connection -> error,
339 ERROR_08002, NULL,
340 connection -> environment -> requested_version );
341
342 return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_ERROR );
343 }
344 else if ( option == SQL_TXN_ISOLATION )
345 {
346 dm_log_write( __FILE__,
347 __LINE__,
348 LOG_INFO,
349 LOG_INFO,
350 "Error: S1011" );
351
352 __post_internal_error( &connection -> error,
353 ERROR_S1011, NULL,
354 connection -> environment -> requested_version );
355
356 return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_ERROR );
357 }
358 }
359
360 /*
361 * is it a legitimate value
362 */
363 ret = dm_check_connection_attrs( connection, option, (SQLPOINTER)value );
364
365 if ( ret != SQL_SUCCESS )
366 {
367 dm_log_write( __FILE__,
368 __LINE__,
369 LOG_INFO,
370 LOG_INFO,
371 "Error: HY024" );
372
373 __post_internal_error( &connection -> error,
374 ERROR_HY024, NULL,
375 connection -> environment -> requested_version );
376
377 return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_ERROR );
378 }
379
380 /*
381 * is it something overridden
382 */
383
384 value = (SQLULEN) __attr_override_wide( connection, SQL_HANDLE_DBC, option, (void*) value, NULL, buffer );
385
386 if ( option == SQL_LOGIN_TIMEOUT )
387 {
388 connection -> login_timeout_set = 1;
389 connection -> login_timeout = value;
390 ret = SQL_SUCCESS;
391 }
392 else if ( option == SQL_ATTR_ACCESS_MODE )
393 {
394 connection -> access_mode = ( SQLLEN ) value;
395 connection -> access_mode_set = 1;
396 }
397 else if ( option == SQL_AUTOCOMMIT )
398 {
399 connection -> auto_commit = ( SQLINTEGER ) value;
400 connection -> auto_commit_set = 1;
401 }
402
403 if ( option == SQL_ODBC_CURSORS )
404 {
405 connection -> cursors = value;
406 ret = SQL_SUCCESS;
407 }
408 else if ( connection -> state == STATE_C2 )
409 {
410 if ( option == SQL_AUTOCOMMIT )
411 {
412 connection -> auto_commit = ( SQLINTEGER ) value;
413 connection -> auto_commit_set = 1;
414 }
415 else if ( option == SQL_ATTR_QUIET_MODE )
416 {
417 connection -> quite_mode = ( SQLLEN ) value;
418 connection -> quite_mode_set = 1;
419 }
420 else if ( option == SQL_ATTR_ACCESS_MODE )
421 {
422 connection -> access_mode = ( SQLLEN ) value;
423 connection -> access_mode_set = 1;
424 }
425 else
426 {
427 /*
428 * save any unknown attributes untill connect
429 */
430
431 struct save_attr *sa = calloc( 1, sizeof( struct save_attr ));
432
433 sa -> attr_type = option;
434 sa -> int_attr = ( SQLINTEGER ) value;
435 sa -> next = connection -> save_attr;
436 connection -> save_attr = sa;
437 }
438
439 if ( log_info.log_flag )
440 {
441 sprintf( connection -> msg,
442 "\n\t\tExit:[%s]",
443 __get_return_status( SQL_SUCCESS, s1 ));
444
445 dm_log_write( __FILE__,
446 __LINE__,
447 LOG_INFO,
448 LOG_INFO,
449 connection -> msg );
450 }
451
452 return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_SUCCESS );
453 }
454 else
455 {
456 /*
457 * call the driver
458 */
459 if ( CHECK_SQLSETCONNECTOPTIONW( connection ))
460 {
461 ret = SQLSETCONNECTOPTIONW( connection,
462 connection -> driver_dbc,
463 option,
464 value );
465 }
466 else if ( CHECK_SQLSETCONNECTATTRW( connection ))
467 {
468 SQLINTEGER string_length;
469
470 switch( option )
471 {
472 case SQL_ATTR_CURRENT_CATALOG:
473 case SQL_ATTR_TRACEFILE:
474 case SQL_ATTR_TRANSLATE_LIB:
475 string_length = SQL_NTS;
476 break;
477
478 default:
479 string_length = 0;
480 break;
481 }
482 ret = SQLSETCONNECTATTRW( connection,
483 connection -> driver_dbc,
484 option,
485 value,
486 string_length );
487 }
488 else
489 {
490 dm_log_write( __FILE__,
491 __LINE__,
492 LOG_INFO,
493 LOG_INFO,
494 "Error: IM001" );
495
496 __post_internal_error( &connection -> error,
497 ERROR_IM001, NULL,
498 connection -> environment -> requested_version );
499
500 return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_ERROR );
501 }
502
503 if ( log_info.log_flag )
504 {
505 sprintf( connection -> msg,
506 "\n\t\tExit:[%s]",
507 __get_return_status( ret, s1 ));
508
509 dm_log_write( __FILE__,
510 __LINE__,
511 LOG_INFO,
512 LOG_INFO,
513 connection -> msg );
514 }
515 }
516
517 /*
518 * catch this
519 */
520
521 if ( option == SQL_ATTR_USE_BOOKMARKS && SQL_SUCCEEDED( ret ))
522 {
523 connection -> bookmarks_on = (SQLUINTEGER) value;
524 }
525
526 return function_return( SQL_HANDLE_DBC, connection, ret );
527}
528