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: SQLTransact.c,v 1.11 2009/02/18 17:59:08 lurcher Exp $
31 *
32 * $Log: SQLTransact.c,v $
33 * Revision 1.11 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.10 2007/08/30 12:54:17 lurcher
37 * Add -3 option to isql to use ODBC3 calls
38 *
39 * Revision 1.9 2006/05/31 17:35:34 lurcher
40 * Add unicode ODBCINST entry points
41 *
42 * Revision 1.8 2004/06/16 14:42:03 lurcher
43 *
44 *
45 * Fix potential corruption with threaded use and SQLEndTran
46 *
47 * Revision 1.7 2003/10/30 18:20:46 lurcher
48 *
49 * Fix broken thread protection
50 * Remove SQLNumResultCols after execute, lease S4/S% to driver
51 * Fix string overrun in SQLDriverConnect
52 * Add initial support for Interix
53 *
54 * Revision 1.6 2002/12/05 17:44:31 lurcher
55 *
56 * Display unknown return values in return logging
57 *
58 * Revision 1.5 2002/09/18 14:49:32 lurcher
59 *
60 * DataManagerII additions and some more threading fixes
61 *
62 * Revision 1.3 2002/08/20 12:41:07 lurcher
63 *
64 * Fix incorrect return state from SQLEndTran/SQLTransact
65 *
66 * Revision 1.2 2002/08/12 16:20:44 lurcher
67 *
68 * Make it try and find a working iconv set of encodings
69 *
70 * Revision 1.1.1.1 2001/10/17 16:40:07 lurcher
71 *
72 * First upload to SourceForge
73 *
74 * Revision 1.2 2001/04/12 17:43:36 nick
75 *
76 * Change logging and added autotest to odbctest
77 *
78 * Revision 1.1.1.1 2000/09/04 16:42:52 nick
79 * Imported Sources
80 *
81 * Revision 1.10 2000/08/16 15:57:51 ngorham
82 *
83 * Fix bug where it falled if called in state C4
84 *
85 * Revision 1.9 1999/11/13 23:41:01 ngorham
86 *
87 * Alter the way DM logging works
88 * Upgrade the Postgres driver to 6.4.6
89 *
90 * Revision 1.8 1999/10/24 23:54:19 ngorham
91 *
92 * First part of the changes to the error reporting
93 *
94 * Revision 1.7 1999/10/20 19:45:15 ngorham
95 *
96 * Added fix to SQLTransact in the DM
97 *
98 * Revision 1.6 1999/09/21 22:34:26 ngorham
99 *
100 * Improve performance by removing unneeded logging calls when logging is
101 * disabled
102 *
103 * Revision 1.5 1999/07/10 21:10:17 ngorham
104 *
105 * Adjust error sqlstate from driver manager, depending on requested
106 * version (ODBC2/3)
107 *
108 * Revision 1.4 1999/07/04 21:05:08 ngorham
109 *
110 * Add LGPL Headers to code
111 *
112 * Revision 1.3 1999/06/30 23:56:56 ngorham
113 *
114 * Add initial thread safety code
115 *
116 * Revision 1.2 1999/06/19 17:51:40 ngorham
117 *
118 * Applied assorted minor bug fixes
119 *
120 * Revision 1.1.1.1 1999/05/29 13:41:09 sShandyb
121 * first go at it
122 *
123 * Revision 1.3 1999/06/02 20:12:10 ngorham
124 *
125 * Fixed botched log entry, and removed the dos \r from the sql header files.
126 *
127 * Revision 1.2 1999/06/02 19:57:21 ngorham
128 *
129 * Added code to check if a attempt is being made to compile with a C++
130 * Compiler, and issue a message.
131 * Start work on the ODBC2-3 conversions.
132 *
133 * Revision 1.1.1.1 1999/05/27 18:23:18 pharvey
134 * Imported sources
135 *
136 * Revision 1.2 1999/05/09 23:27:11 nick
137 * All the API done now
138 *
139 * Revision 1.1 1999/04/25 23:06:11 nick
140 * Initial revision
141 *
142 *
143 **********************************************************************/
144
145#include <config.h>
146#include "drivermanager.h"
147
148static char const rcsid[]= "$RCSfile: SQLTransact.c,v $ $Revision: 1.11 $";
149
150SQLRETURN SQLTransact( SQLHENV environment_handle,
151 SQLHDBC connection_handle,
152 SQLUSMALLINT completion_type )
153{
154 SQLCHAR s1[ 100 + LOG_MESSAGE_LEN ];
155
156 /*
157 * check either handle first
158 */
159
160 if ( connection_handle != SQL_NULL_HDBC )
161 {
162 DMHDBC connection = (DMHDBC) connection_handle;
163
164 if ( !__validate_dbc( connection ))
165 {
166 dm_log_write( __FILE__,
167 __LINE__,
168 LOG_INFO,
169 LOG_INFO,
170 "Error: SQL_INVALID_HANDLE" );
171
172 return SQL_INVALID_HANDLE;
173 }
174 }
175
176 if ( environment_handle != SQL_NULL_HENV )
177 {
178 DMHENV environment = (DMHENV) environment_handle;
179
180 if ( !__validate_env( environment ))
181 {
182 dm_log_write( __FILE__,
183 __LINE__,
184 LOG_INFO,
185 LOG_INFO,
186 "Error: SQL_INVALID_HANDLE" );
187
188 return SQL_INVALID_HANDLE;
189 }
190 }
191
192 if ( connection_handle != SQL_NULL_HDBC )
193 {
194 DMHDBC connection = (DMHDBC) connection_handle;
195 SQLRETURN ret;
196
197 function_entry( connection );
198
199 if ( log_info.log_flag )
200 {
201 sprintf( connection -> msg, "\n\t\tEntry:\
202\n\t\t\tEnvironment = %p\
203\n\t\t\tConnection = %p\
204\n\t\t\tCompletion Type = %d",
205 (void*)environment_handle,
206 (void*)connection_handle,
207 (int)completion_type );
208
209 dm_log_write( __FILE__,
210 __LINE__,
211 LOG_INFO,
212 LOG_INFO,
213 connection -> msg );
214 }
215
216 thread_protect( SQL_HANDLE_DBC, connection );
217
218 if ( connection -> state == STATE_C1 ||
219 connection -> state == STATE_C2 ||
220 connection -> state == STATE_C3 )
221 {
222 dm_log_write( __FILE__,
223 __LINE__,
224 LOG_INFO,
225 LOG_INFO,
226 "Error: 08003" );
227
228 __post_internal_error( &connection -> error,
229 ERROR_08003, NULL,
230 connection -> environment -> requested_version );
231
232 return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_ERROR );
233 }
234
235 /*
236 * check status of statements belonging to this connection
237 */
238
239 if( __check_stmt_from_dbc_v( connection, 8, STATE_S8, STATE_S9, STATE_S10, STATE_S11, STATE_S12, STATE_S13, STATE_S14, STATE_S15 )) {
240
241 dm_log_write( __FILE__,
242 __LINE__,
243 LOG_INFO,
244 LOG_INFO,
245 "Error: HY010" );
246
247 __post_internal_error( &connection -> error,
248 ERROR_HY010, NULL,
249 connection -> environment -> requested_version );
250
251 return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_ERROR );
252 }
253
254 if ( completion_type != SQL_COMMIT &&
255 completion_type != SQL_ROLLBACK )
256 {
257 dm_log_write( __FILE__,
258 __LINE__,
259 LOG_INFO,
260 LOG_INFO,
261 "Error: HY012" );
262
263 __post_internal_error( &connection -> error,
264 ERROR_HY012, NULL,
265 connection -> environment -> requested_version );
266
267 return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_ERROR );
268 }
269
270 if ( CHECK_SQLTRANSACT( connection ))
271 {
272 ret = SQLTRANSACT( connection,
273 SQL_NULL_HENV,
274 connection -> driver_dbc,
275 completion_type );
276 }
277 else if ( CHECK_SQLENDTRAN( connection ))
278 {
279 ret = SQLENDTRAN( connection,
280 SQL_HANDLE_DBC,
281 connection -> driver_dbc,
282 completion_type );
283 }
284 else
285 {
286 dm_log_write( __FILE__,
287 __LINE__,
288 LOG_INFO,
289 LOG_INFO,
290 "Error: IM001" );
291
292 __post_internal_error( &connection -> error,
293 ERROR_IM001, NULL,
294 connection -> environment -> requested_version );
295
296 return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_ERROR );
297 }
298
299 if( SQL_SUCCEEDED(ret) && connection -> auto_commit == SQL_AUTOCOMMIT_OFF )
300 {
301 SQLSMALLINT cb_value;
302 SQLSMALLINT cb_value_length = sizeof(SQLSMALLINT);
303 SQLRETURN ret1;
304
305 /*
306 * for each statement belonging to this connection set its state
307 * relative to the commit or rollback behavior
308 */
309
310 if ( connection -> cbs_found == 0 )
311 {
312 /* release thread so we can get the info */
313 thread_release( SQL_HANDLE_DBC, connection );
314
315 ret1 = SQLGetInfo(connection,
316 SQL_CURSOR_COMMIT_BEHAVIOR,
317 &connection -> ccb_value,
318 sizeof( SQLSMALLINT ),
319 &cb_value_length);
320
321 if ( SQL_SUCCEEDED( ret1 ))
322 {
323 ret1 = SQLGetInfo(connection,
324 SQL_CURSOR_ROLLBACK_BEHAVIOR,
325 &connection -> crb_value,
326 sizeof( SQLSMALLINT ),
327 &cb_value_length);
328 }
329
330 /* protect thread again */
331 thread_protect( SQL_HANDLE_DBC, connection );
332 if ( SQL_SUCCEEDED( ret1 ))
333 {
334 connection -> cbs_found = 1;
335 }
336 }
337
338 if( completion_type == SQL_COMMIT )
339 {
340 cb_value = connection -> ccb_value;
341 }
342 else
343 {
344 cb_value = connection -> crb_value;
345 }
346
347 if( connection -> cbs_found )
348 {
349 __set_stmt_state( connection, cb_value );
350 }
351 }
352
353 if ( log_info.log_flag )
354 {
355 sprintf( connection -> msg,
356 "\n\t\tExit:[%s]",
357 __get_return_status( ret, s1 ));
358
359 dm_log_write( __FILE__,
360 __LINE__,
361 LOG_INFO,
362 LOG_INFO,
363 connection -> msg );
364 }
365
366 return function_return( SQL_HANDLE_DBC, connection, ret );
367 }
368 else if ( environment_handle != SQL_NULL_HENV )
369 {
370 DMHENV environment = (DMHENV) environment_handle;
371 DMHDBC connection;
372 SQLRETURN ret;
373
374 function_entry( environment );
375
376 if ( log_info.log_flag )
377 {
378 sprintf( environment -> msg, "\n\t\tEntry:\
379\n\t\t\tEnvironment = %p\
380\n\t\t\tConnection = %p\
381\n\t\t\tCompletion Type = %d",
382 (void*)environment_handle,
383 (void*)connection_handle,
384 (int)completion_type );
385
386 dm_log_write( __FILE__,
387 __LINE__,
388 LOG_INFO,
389 LOG_INFO,
390 environment -> msg );
391 }
392
393 thread_protect( SQL_HANDLE_ENV, environment );
394
395 if ( completion_type != SQL_COMMIT &&
396 completion_type != SQL_ROLLBACK )
397 {
398 dm_log_write( __FILE__,
399 __LINE__,
400 LOG_INFO,
401 LOG_INFO,
402 "Error: HY012" );
403
404 __post_internal_error( &environment -> error,
405 ERROR_HY012, NULL,
406 environment -> requested_version );
407
408 return function_return_nodrv( SQL_HANDLE_ENV, environment, SQL_ERROR );
409 }
410
411 if ( environment -> state == STATE_E2 )
412 {
413 /*
414 * check that none of the connections are in a need data state
415 */
416
417 connection = __get_dbc_root();
418
419 while( connection )
420 {
421 if ( connection -> environment == environment &&
422 connection -> state > STATE_C4 )
423 {
424 if( __check_stmt_from_dbc_v( connection, 8, STATE_S8, STATE_S9, STATE_S10, STATE_S11, STATE_S12, STATE_S13, STATE_S14, STATE_S15 )) {
425
426 dm_log_write( __FILE__,
427 __LINE__,
428 LOG_INFO,
429 LOG_INFO,
430 "Error: HY010" );
431
432 __post_internal_error( &environment -> error,
433 ERROR_HY010, NULL,
434 environment -> requested_version );
435
436 return function_return_nodrv( SQL_HANDLE_ENV, environment, SQL_ERROR );
437 }
438 }
439
440 connection = connection -> next_class_list;
441 }
442
443 /*
444 * for each connection on this env
445 */
446
447 connection = __get_dbc_root();
448
449 while( connection )
450 {
451 if ( connection -> environment == environment &&
452 connection -> state > STATE_C4 )
453 {
454 if ( CHECK_SQLTRANSACT( connection ))
455 {
456 ret = SQLTRANSACT( connection,
457 SQL_NULL_HENV,
458 connection -> driver_dbc,
459 completion_type );
460
461 if ( !SQL_SUCCEEDED( ret ))
462 {
463 dm_log_write( __FILE__,
464 __LINE__,
465 LOG_INFO,
466 LOG_INFO,
467 "Error: 24S01" );
468
469 __post_internal_error( &environment -> error,
470 ERROR_25S01, NULL,
471 environment -> requested_version );
472
473 thread_release( SQL_HANDLE_ENV, environment );
474
475 return function_return( SQL_HANDLE_ENV, environment, SQL_ERROR );
476 }
477 }
478 else if ( CHECK_SQLENDTRAN( connection ))
479 {
480 ret = SQLENDTRAN( connection,
481 SQL_HANDLE_DBC,
482 connection -> driver_dbc,
483 completion_type );
484
485 if ( !SQL_SUCCEEDED( ret ))
486 {
487 dm_log_write( __FILE__,
488 __LINE__,
489 LOG_INFO,
490 LOG_INFO,
491 "Error: 24S01" );
492
493 __post_internal_error( &environment -> error,
494 ERROR_25S01, NULL,
495 environment -> requested_version );
496
497 return function_return( SQL_HANDLE_ENV, environment, SQL_ERROR );
498 }
499 }
500 else
501 {
502 dm_log_write( __FILE__,
503 __LINE__,
504 LOG_INFO,
505 LOG_INFO,
506 "Error: IM001" );
507
508 __post_internal_error( &environment -> error,
509 ERROR_IM001, NULL,
510 environment -> requested_version );
511
512 return function_return_nodrv( SQL_HANDLE_ENV, environment, SQL_ERROR );
513 }
514 }
515
516 connection = connection -> next_class_list;
517 }
518 }
519
520 if ( log_info.log_flag )
521 {
522 sprintf( environment -> msg,
523 "\n\t\tExit:[%s]",
524 __get_return_status( SQL_SUCCESS, s1 ));
525
526 dm_log_write( __FILE__,
527 __LINE__,
528 LOG_INFO,
529 LOG_INFO,
530 environment -> msg );
531 }
532
533 thread_release( SQL_HANDLE_ENV, environment );
534
535 return SQL_SUCCESS;
536 }
537 else
538 {
539 dm_log_write( __FILE__,
540 __LINE__,
541 LOG_INFO,
542 LOG_INFO,
543 "Error: SQL_INVALID_HANDLE" );
544
545 return SQL_INVALID_HANDLE;
546 }
547}
548