-
Notifications
You must be signed in to change notification settings - Fork 191
/
ibm_db.c
15740 lines (14888 loc) · 732 KB
/
ibm_db.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+--------------------------------------------------------------------------+
| Licensed Materials - Property of IBM |
| |
| (C) Copyright IBM Corporation 2006-2020 |
+--------------------------------------------------------------------------+
| This module complies with SQLAlchemy 0.4 and is |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable |
| law or agreed to in writing, software distributed under the License is |
| distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| KIND, either express or implied. See the License for the specific |
| language governing permissions and limitations under the License. |
+--------------------------------------------------------------------------+
| Authors: Manas Dadarkar, Salvador Ledezma, Sushant Koduru, |
| Lynh Nguyen, Kanchana Padmanabhan, Dan Scott, Helmut Tessarek, |
| Sam Ruby, Kellen Bombardier, Tony Cairns, Abhigyan Agrawal, |
| Tarun Pasrija, Rahul Priyadarshi, Akshay Anand, Saba Kauser , |
| Hemlata Bhatt |
+--------------------------------------------------------------------------+
*/
#define MODULE_RELEASE "3.2.3"
#include <Python.h>
#include <datetime.h>
#include "ibm_db.h"
#include <ctype.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#ifdef _LP64
#ifdef __MVS__
#define BIGINT_IS_SHORTER_THAN_LONG 0
#else
#define BIGINT_IS_SHORTER_THAN_LONG 1
#endif
#else
#define BIGINT_IS_SHORTER_THAN_LONG 1
#endif
/* MAX length for DECFLOAT */
#define MAX_DECFLOAT_LENGTH 44
/* True global resources - no need for thread safety here */
static struct _ibm_db_globals *ibm_db_globals;
static void _python_ibm_db_check_sql_errors( SQLHANDLE handle, SQLSMALLINT hType, int rc, int cpy_to_global, char* ret_str, int API, SQLSMALLINT recno );
static int _python_ibm_db_assign_options( void* handle, int type, long opt_key, PyObject *data );
static SQLWCHAR* getUnicodeDataAsSQLWCHAR(PyObject *pyobj, int *isNewBuffer);
static SQLCHAR* getUnicodeDataAsSQLCHAR(PyObject *pyobj, int *isNewBuffer);
static PyObject* getSQLWCharAsPyUnicodeObject(SQLWCHAR* sqlwcharData, int sqlwcharBytesLen);
const int _check_i = 1;
#define is_bigendian() ( (*(char*)&_check_i) == 0 )
static int is_systemi, is_informix; /* 1 == TRUE; 0 == FALSE; */
#ifdef _WIN32
#define DLOPEN LoadLibrary
#define DLSYM GetProcAddress
#define DLCLOSE FreeLibrary
#ifdef _WIN64
#define LIBDB2 "db2cli64.dll"
#else
#define LIBDB2 "db2cli.dll"
#endif
#elif _AIX
#define DLOPEN dlopen
#define DLSYM dlsym
#define DLCLOSE dlclose
#ifdef __64BIT__
/*64-bit library in the archive libdb2.a*/
#define LIBDB2 "libdb2.a(shr_64.o)"
#else
/*32-bit library in the archive libdb2.a*/
#define LIBDB2 "libdb2.a(shr.o)"
#endif
#elif __APPLE__
#define DLOPEN dlopen
#define DLSYM dlsym
#define DLCLOSE dlclose
#define LIBDB2 "libdb2.dylib"
#else
#define DLOPEN dlopen
#define DLSYM dlsym
#define DLCLOSE dlclose
#define LIBDB2 "libdb2.so.1"
#endif
// Define macros for log levels
#define DEBUG "DEBUG"
#define INFO "INFO"
#define WARNING "WARNING"
#define ERROR "ERROR"
#define EXCEPTION "EXCEPTION"
static int debug_mode = 0;
static char* fileName = NULL;
static char messageStr[2024];
// Function to get the current timestamp
static void get_timestamp(char *buffer, size_t size) {
time_t now = time(NULL);
struct tm *tm_info = localtime(&now);
strftime(buffer, size, "%Y-%m-%d %H:%M:%S", tm_info);
}
// LogMsg function
static void LogMsg(const char *log_level, const char *message, const char *file_name) {
// logging if in debug mode
if (debug_mode)
{
char timestamp[20];
get_timestamp(timestamp, sizeof(timestamp));
// Print formatted log message
if (file_name == NULL) {
printf("%s - %s - %s\n", timestamp, log_level, message);
} else {
FILE *file = fopen(file_name, "a");
if (file != NULL) {
fprintf(file, "%s - %s - %s\n", timestamp, log_level, message);
fclose(file);
} else {
printf("Failed to open log file: %s\n", file_name);
}
}
} else {
return;
}
}
/* Defines a linked list structure for error messages */
typedef struct _error_msg_node {
char err_msg[DB2_MAX_ERR_MSG_LEN];
struct _error_msg_node *next;
} error_msg_node;
/* Defines a linked list structure for caching param data */
typedef struct _param_cache_node {
SQLSMALLINT data_type; /* Datatype */
SQLUINTEGER param_size; /* param size */
SQLSMALLINT nullable; /* is Nullable */
SQLSMALLINT scale; /* Decimal scale */
SQLUINTEGER file_options; /* File options if PARAM_FILE */
SQLINTEGER bind_indicator; /* indicator variable for SQLBindParameter */
int param_num; /* param number in stmt */
int param_type; /* Type of param - INP/OUT/INP-OUT/FILE */
int size; /* Size of param */
char *varname; /* bound variable name */
PyObject *var_pyvalue; /* bound variable value */
SQLINTEGER ivalue; /* Temp storage value */
double fvalue; /* Temp storage value */
char *svalue; /* Temp storage value */
SQLWCHAR *uvalue; /* Temp storage value */
DATE_STRUCT *date_value; /* Temp storage value */
TIME_STRUCT *time_value; /* Temp storage value */
TIMESTAMP_STRUCT *ts_value; /* Temp storage value */
SQLINTEGER *ivalueArray; /* Temp storage array of values */
double *fvalueArray; /* Temp storage array of values */
SQLINTEGER *bind_indicator_array; /* Temp storage array of values */
struct _param_cache_node *next; /* Pointer to next node */
} param_node;
typedef struct _conn_handle_struct {
PyObject_HEAD
SQLHANDLE henv;
SQLHANDLE hdbc;
long auto_commit;
long c_bin_mode;
long c_case_mode;
long c_cursor_type;
long c_use_wchar;
int handle_active;
SQLSMALLINT error_recno_tracker;
SQLSMALLINT errormsg_recno_tracker;
int flag_pconnect; /* Indicates that this connection is persistent */
} conn_handle;
static void _python_ibm_db_free_conn_struct(conn_handle *handle);
static PyTypeObject conn_handleType = {
PyVarObject_HEAD_INIT(NULL, 0)
/* tp_name */ "ibm_db.IBM_DBConnection",
/* tp_basicsize */ sizeof(conn_handle),
/* tp_itemsize */ 0,
/* tp_dealloc */ (destructor)_python_ibm_db_free_conn_struct,
/* tp_print */ 0,
/* tp_getattr */ 0,
/* tp_setattr */ 0,
/* tp_compare */ 0,
/* tp_repr */ 0,
/* tp_as_number */ 0,
/* tp_as_sequence */ 0,
/* tp_as_mapping */ 0,
/* tp_hash */ 0,
/* tp_call */ 0,
/* tp_str */ 0,
/* tp_getattro */ 0,
/* tp_setattro */ 0,
/* tp_as_buffer */ 0,
/* tp_flags */ Py_TPFLAGS_DEFAULT,
/* tp_doc */ "IBM DataServer connection object",
/* tp_traverse */ 0,
/* tp_clear */ 0,
/* tp_richcompare */ 0,
/* tp_weaklistoffset */ 0,
/* tp_iter */ 0,
/* tp_iternext */ 0,
/* tp_methods */ 0,
/* tp_members */ 0,
/* tp_getset */ 0,
/* tp_base */ 0,
/* tp_dict */ 0,
/* tp_descr_get */ 0,
/* tp_descr_set */ 0,
/* tp_dictoffset */ 0,
/* tp_init */ 0,
};
typedef union {
SQLINTEGER i_val;
SQLDOUBLE d_val;
SQLFLOAT f_val;
SQLSMALLINT s_val;
SQLCHAR *str_val;
SQLREAL r_val;
SQLWCHAR *w_val;
TIMESTAMP_STRUCT *ts_val;
DATE_STRUCT *date_val;
TIME_STRUCT *time_val;
} ibm_db_row_data_type;
typedef struct {
SQLINTEGER out_length;
ibm_db_row_data_type data;
} ibm_db_row_type;
typedef struct _ibm_db_result_set_info_struct {
SQLCHAR *name;
SQLSMALLINT type;
SQLUINTEGER size;
SQLSMALLINT scale;
SQLSMALLINT nullable;
unsigned char *mem_alloc; /* Mem free */
} ibm_db_result_set_info;
typedef struct _row_hash_struct {
PyObject *hash;
} row_hash_struct;
typedef struct _stmt_handle_struct {
PyObject_HEAD
SQLHANDLE hdbc;
SQLHANDLE hstmt;
long s_bin_mode;
long cursor_type;
long s_case_mode;
long s_use_wchar;
SQLSMALLINT error_recno_tracker;
SQLSMALLINT errormsg_recno_tracker;
/* Parameter Caching variables */
param_node *head_cache_list;
param_node *current_node;
int num_params; /* Number of Params */
int file_param; /* if option passed in is FILE_PARAM */
int num_columns;
ibm_db_result_set_info *column_info;
ibm_db_row_type *row_data;
} stmt_handle;
static void _python_ibm_db_free_stmt_struct(stmt_handle *handle);
static PyTypeObject stmt_handleType = {
PyVarObject_HEAD_INIT(NULL, 0)
/* tp_name */ "ibm_db.IBM_DBStatement",
/* tp_basicsize */ sizeof(stmt_handle),
/* tp_itemsize */ 0,
/* tp_dealloc */ (destructor)_python_ibm_db_free_stmt_struct,
/* tp_print */ 0,
/* tp_getattr */ 0,
/* tp_setattr */ 0,
/* tp_compare */ 0,
/* tp_repr */ 0,
/* tp_as_number */ 0,
/* tp_as_sequence */ 0,
/* tp_as_mapping */ 0,
/* tp_hash */ 0,
/* tp_call */ 0,
/* tp_str */ 0,
/* tp_getattro */ 0,
/* tp_setattro */ 0,
/* tp_as_buffer */ 0,
/* tp_flags */ Py_TPFLAGS_DEFAULT,
/* tp_doc */ "IBM DataServer cursor object",
/* tp_traverse */ 0,
/* tp_clear */ 0,
/* tp_richcompare */ 0,
/* tp_weaklistoffset */ 0,
/* tp_iter */ 0,
/* tp_iternext */ 0,
/* tp_methods */ 0,
/* tp_members */ 0,
/* tp_getset */ 0,
/* tp_base */ 0,
/* tp_dict */ 0,
/* tp_descr_get */ 0,
/* tp_descr_set */ 0,
/* tp_dictoffset */ 0,
/* tp_init */ 0,
};
/* equivalent functions on different platforms */
#ifdef _WIN32
#define STRCASECMP stricmp
#else
#define STRCASECMP strcasecmp
#endif
static void python_ibm_db_init_globals(struct _ibm_db_globals *ibm_db_globals) {
/* env handle */
ibm_db_globals->bin_mode = 1;
memset(ibm_db_globals->__python_conn_err_msg, 0, DB2_MAX_ERR_MSG_LEN);
memset(ibm_db_globals->__python_stmt_err_msg, 0, DB2_MAX_ERR_MSG_LEN);
memset(ibm_db_globals->__python_conn_err_state, 0, SQL_SQLSTATE_SIZE + 1);
memset(ibm_db_globals->__python_stmt_err_state, 0, SQL_SQLSTATE_SIZE + 1);
memset(ibm_db_globals->__python_err_code, 0, SQL_SQLCODE_SIZE + 1);
}
static PyObject *persistent_list;
static PyObject *os_getpid;
char *estrdup(char *data) {
int len = strlen(data);
char *dup = ALLOC_N(char, len+1);
if ( dup == NULL ) {
PyErr_SetString(PyExc_Exception, "Failed to Allocate Memory");
return NULL;
}
strcpy(dup, data);
return dup;
}
char *estrndup(char *data, int max) {
int len = strlen(data);
char *dup;
if (len > max){
len = max;
}
dup = ALLOC_N(char, len+1);
if ( dup == NULL ) {
PyErr_SetString(PyExc_Exception, "Failed to Allocate Memory");
return NULL;
}
strcpy(dup, data);
return dup;
}
char *strtolower(char *data, int max) {
while (max--){
data[max] = tolower(data[max]);
}
return data;
}
char *strtoupper(char *data, int max) {
while (max--){
data[max] = toupper(data[max]);
}
return data;
}
/* static void _python_ibm_db_free_conn_struct */
static void _python_ibm_db_free_conn_struct(conn_handle *handle) {
LogMsg(INFO, "entry _python_ibm_db_free_conn_struct", fileName);
/* Disconnect from DB. If stmt is allocated, it is freed automatically */
snprintf(messageStr, sizeof(messageStr), "Handle details: handle_active=%d, flag_pconnect=%d, auto_commit=%d",
handle->handle_active, handle->flag_pconnect, handle->auto_commit);
LogMsg(DEBUG, messageStr, fileName);
if ( handle->handle_active && !handle->flag_pconnect) {
if(handle->auto_commit == 0){
Py_BEGIN_ALLOW_THREADS;
SQLEndTran(SQL_HANDLE_DBC, (SQLHDBC)handle->hdbc, SQL_ROLLBACK);
Py_END_ALLOW_THREADS;
snprintf(messageStr, sizeof(messageStr), "SQLEndTran called with SQL_HANDLE_DBC=%d, hdbc=%p, SQL_ROLLBACK=%d",
SQL_HANDLE_DBC, (void *)handle->hdbc, SQL_ROLLBACK);
LogMsg(DEBUG, messageStr, fileName);
}
Py_BEGIN_ALLOW_THREADS;
SQLDisconnect((SQLHDBC)handle->hdbc);
snprintf(messageStr, sizeof(messageStr), "SQLDisconnect called with hdbc=%p", (void *)handle->hdbc);
LogMsg(DEBUG, messageStr, fileName);
SQLFreeHandle(SQL_HANDLE_DBC, handle->hdbc);
snprintf(messageStr, sizeof(messageStr), "SQLFreeHandle called with SQL_HANDLE_DBC=%d, handle_hdbc=%p", SQL_HANDLE_DBC, (void *)handle->hdbc);
LogMsg(DEBUG, messageStr, fileName);
SQLFreeHandle(SQL_HANDLE_ENV, handle->henv);
snprintf(messageStr, sizeof(messageStr), "SQLFreeHandle called with SQL_HANDLE_ENV=%d, handle->henv=%p", SQL_HANDLE_ENV, (void *)handle->henv);
LogMsg(DEBUG, messageStr, fileName);
Py_END_ALLOW_THREADS;
} else {
snprintf(messageStr, sizeof(messageStr), "Connection not active or is a persistent connection. No disconnect needed.");
LogMsg(INFO, messageStr, fileName);
}
LogMsg(INFO, "exit _python_ibm_db_free_conn_struct", fileName);
Py_TYPE(handle)->tp_free((PyObject*)handle);
}
/* static void _python_ibm_db_free_row_struct */
/*
* static void _python_ibm_db_free_row_struct(row_hash_struct *handle) {
* free(handle);
* }
*/
static void _python_ibm_db_clear_param_cache( stmt_handle *stmt_res )
{
LogMsg(INFO, "entry _python_ibm_db_clear_param_cache()", fileName);
snprintf(messageStr, sizeof(messageStr), "Initial state: head_cache_list=%p, num_params=%d",
stmt_res->head_cache_list, stmt_res->num_params);
LogMsg(DEBUG, messageStr, fileName);
param_node *temp_ptr, *curr_ptr;
/* Free param cache list */
curr_ptr = stmt_res->head_cache_list;
while (curr_ptr != NULL) {
snprintf(messageStr, sizeof(messageStr), "Freeing node: var_pyvalue=%p, varname=%p, svalue=%p, uvalue=%p, date_value=%p, time_value=%p, ts_value=%p, ivalueArray=%p, fvalueArray=%p, bind_indicator_array=%p",
curr_ptr->var_pyvalue, curr_ptr->varname, curr_ptr->svalue, curr_ptr->uvalue,
curr_ptr->date_value, curr_ptr->time_value, curr_ptr->ts_value,
curr_ptr->ivalueArray, curr_ptr->fvalueArray, curr_ptr->bind_indicator_array);
LogMsg(DEBUG, messageStr, fileName);
/* Decrement refcount on Python handle */
/* NOTE: Py_XDECREF checks NULL value */
Py_XDECREF(curr_ptr->var_pyvalue);
/* Free Values */
/* NOTE: PyMem_Free checks NULL value */
PyMem_Free(curr_ptr->varname);
PyMem_Free(curr_ptr->svalue);
PyMem_Free(curr_ptr->uvalue);
PyMem_Free(curr_ptr->date_value);
PyMem_Free(curr_ptr->time_value);
PyMem_Free(curr_ptr->ts_value);
PyMem_Free(curr_ptr->ivalueArray);
PyMem_Free(curr_ptr->fvalueArray);
PyMem_Free(curr_ptr->bind_indicator_array);
temp_ptr = curr_ptr;
curr_ptr = curr_ptr->next;
PyMem_Free(temp_ptr);
}
stmt_res->head_cache_list = NULL;
stmt_res->num_params = 0;
snprintf(messageStr, sizeof(messageStr), "Final state: head_cache_list=%p, num_params=%d",
stmt_res->head_cache_list, stmt_res->num_params);
LogMsg(DEBUG, messageStr, fileName);
LogMsg(INFO, "exit _python_ibm_db_clear_param_cache()", fileName);
}
/* static void _python_ibm_db_free_result_struct(stmt_handle* handle) */
static void _python_ibm_db_free_result_struct(stmt_handle* handle) {
LogMsg(INFO, "entry _python_ibm_db_free_result_struct()", fileName);
int i;
if ( handle != NULL ) {
snprintf(messageStr, sizeof(messageStr), "handle=%p, num_columns=%d", (void *)handle, handle->num_columns);
LogMsg(DEBUG, messageStr, fileName);
_python_ibm_db_clear_param_cache(handle);
/* free row data cache */
if (handle->row_data) {
for (i = 0; i<handle->num_columns; i++) {
switch (handle->column_info[i].type) {
case SQL_CHAR:
case SQL_VARCHAR:
case SQL_LONGVARCHAR:
case SQL_WCHAR:
case SQL_WVARCHAR:
case SQL_GRAPHIC:
case SQL_VARGRAPHIC:
case SQL_LONGVARGRAPHIC:
case SQL_BIGINT:
case SQL_DECIMAL:
case SQL_NUMERIC:
case SQL_XML:
case SQL_DECFLOAT:
if ( handle->row_data[i].data.str_val != NULL ) {
snprintf(messageStr, sizeof(messageStr), "Freeing row_data[%d].data.str_val=%p", i, handle->row_data[i].data.str_val);
LogMsg(DEBUG, messageStr, fileName);
PyMem_Del(handle->row_data[i].data.str_val);
handle->row_data[i].data.str_val = NULL;
}
if ( handle->row_data[i].data.w_val != NULL ) {
snprintf(messageStr, sizeof(messageStr), "Freeing row_data[%d].data.w_val=%p", i, handle->row_data[i].data.w_val);
LogMsg(DEBUG, messageStr, fileName);
PyMem_Del(handle->row_data[i].data.w_val);
handle->row_data[i].data.w_val = NULL;
}
break;
case SQL_TYPE_TIMESTAMP:
if ( handle->row_data[i].data.ts_val != NULL ) {
snprintf(messageStr, sizeof(messageStr), "Freeing row_data[%d].data.ts_val=%p", i, handle->row_data[i].data.ts_val);
LogMsg(DEBUG, messageStr, fileName);
PyMem_Del(handle->row_data[i].data.ts_val);
handle->row_data[i].data.ts_val = NULL;
}
break;
case SQL_TYPE_DATE:
if ( handle->row_data[i].data.date_val != NULL ) {
snprintf(messageStr, sizeof(messageStr), "Freeing row_data[%d].data.date_val=%p", i, handle->row_data[i].data.date_val);
LogMsg(DEBUG, messageStr, fileName);
PyMem_Del(handle->row_data[i].data.date_val);
handle->row_data[i].data.date_val = NULL;
}
break;
case SQL_TYPE_TIME:
if ( handle->row_data[i].data.time_val != NULL ) {
snprintf(messageStr, sizeof(messageStr), "Freeing row_data[%d].data.time_val=%p", i, handle->row_data[i].data.time_val);
LogMsg(DEBUG, messageStr, fileName);
PyMem_Del(handle->row_data[i].data.time_val);
handle->row_data[i].data.time_val = NULL;
}
break;
}
}
PyMem_Del(handle->row_data);
handle->row_data = NULL;
}
/* free column info cache */
if ( handle->column_info ) {
for (i = 0; i<handle->num_columns; i++) {
snprintf(messageStr, sizeof(messageStr), "Freeing column_info[%d].name=%p", i, handle->column_info[i].name);
LogMsg(DEBUG, messageStr, fileName);
PyMem_Del(handle->column_info[i].name);
/* Mem free */
if(handle->column_info[i].mem_alloc){
snprintf(messageStr, sizeof(messageStr), "Freeing column_info[%d].mem_alloc=%p", i, handle->column_info[i].mem_alloc);
LogMsg(DEBUG, messageStr, fileName);
PyMem_Del(handle->column_info[i].mem_alloc);
}
}
PyMem_Del(handle->column_info);
handle->column_info = NULL;
handle->num_columns = 0;
}
}
LogMsg(INFO, "exit _python_ibm_db_free_result_struct()", fileName);
}
/* static stmt_handle *_ibm_db_new_stmt_struct(conn_handle* conn_res) */
static stmt_handle *_ibm_db_new_stmt_struct(conn_handle* conn_res) {
LogMsg(INFO, "entry _ibm_db_new_stmt_struct()", fileName);
snprintf(messageStr, sizeof(messageStr), "Initializing stmt_handle: hdbc=%p, c_bin_mode=%d, c_cursor_type=%d, c_case_mode=%d, c_use_wchar=%d",
conn_res->hdbc, conn_res->c_bin_mode, conn_res->c_cursor_type, conn_res->c_case_mode, conn_res->c_use_wchar);
LogMsg(DEBUG, messageStr, fileName);
stmt_handle *stmt_res;
stmt_res = PyObject_NEW(stmt_handle, &stmt_handleType);
/* memset(stmt_res, 0, sizeof(stmt_handle)); */
/* Initialize stmt resource so parsing assigns updated options if needed */
stmt_res->hdbc = conn_res->hdbc;
stmt_res->s_bin_mode = conn_res->c_bin_mode;
stmt_res->cursor_type = conn_res->c_cursor_type;
stmt_res->s_case_mode = conn_res->c_case_mode;
stmt_res->s_use_wchar = conn_res->c_use_wchar;
snprintf(messageStr, sizeof(messageStr), "New stmt_handle initialized: hdbc=%p, s_bin_mode=%d, cursor_type=%d, s_case_mode=%d, s_use_wchar=%d",
stmt_res->hdbc, stmt_res->s_bin_mode, stmt_res->cursor_type, stmt_res->s_case_mode, stmt_res->s_use_wchar);
LogMsg(DEBUG, messageStr, fileName);
stmt_res->head_cache_list = NULL;
stmt_res->current_node = NULL;
stmt_res->num_params = 0;
stmt_res->file_param = 0;
stmt_res->column_info = NULL;
stmt_res->num_columns = 0;
stmt_res->error_recno_tracker = 1;
stmt_res->errormsg_recno_tracker = 1;
stmt_res->row_data = NULL;
snprintf(messageStr, sizeof(messageStr), "Final stmt_handle state: head_cache_list=%p, current_node=%p, num_params=%d, file_param=%d, column_info=%p, num_columns=%d, error_recno_tracker=%d, errormsg_recno_tracker=%d, row_data=%p",
stmt_res->head_cache_list, stmt_res->current_node, stmt_res->num_params, stmt_res->file_param, stmt_res->column_info, stmt_res->num_columns, stmt_res->error_recno_tracker, stmt_res->errormsg_recno_tracker, stmt_res->row_data);
LogMsg(DEBUG, messageStr, fileName);
LogMsg(INFO, "exit _ibm_db_new_stmt_struct()", fileName);
return stmt_res;
}
/* static _python_ibm_db_free_stmt_struct */
static void _python_ibm_db_free_stmt_struct(stmt_handle *handle) {
LogMsg(INFO, "entry _python_ibm_db_free_stmt_struct()", fileName);
if ( handle->hstmt != -1 ) {
snprintf(messageStr, sizeof(messageStr), "handle->hstmt=%p, preparing to call SQLFreeHandle", (void *)handle->hstmt);
LogMsg(DEBUG, messageStr, fileName);
Py_BEGIN_ALLOW_THREADS;
SQLFreeHandle( SQL_HANDLE_STMT, handle->hstmt);
Py_END_ALLOW_THREADS;
snprintf(messageStr, sizeof(messageStr), "SQLFreeHandle called with handle=%p", (void *)handle->hstmt);
LogMsg(DEBUG, messageStr, fileName);
if ( handle ) {
_python_ibm_db_free_result_struct(handle);
}
}
snprintf(messageStr, sizeof(messageStr), "Py_TYPE(handle)->tp_free called for handle=%p", (void *)handle);
LogMsg(DEBUG, messageStr, fileName);
Py_TYPE(handle)->tp_free((PyObject*)handle);
LogMsg(INFO, "exit _python_ibm_db_free_stmt_struct()", fileName);
}
/* static void _python_ibm_db_init_error_info(stmt_handle *stmt_res) */
static void _python_ibm_db_init_error_info(stmt_handle *stmt_res) {
LogMsg(INFO, "entry _python_ibm_db_init_error_info()", fileName);
snprintf(messageStr, sizeof(messageStr), "Initial state: error_recno_tracker = %d, errormsg_recno_tracker = %d",
stmt_res->error_recno_tracker, stmt_res->errormsg_recno_tracker);
LogMsg(DEBUG, messageStr, fileName);
stmt_res->error_recno_tracker = 1;
stmt_res->errormsg_recno_tracker = 1;
snprintf(messageStr, sizeof(messageStr), "Modified state: error_recno_tracker = %d, errormsg_recno_tracker = %d",
stmt_res->error_recno_tracker, stmt_res->errormsg_recno_tracker);
LogMsg(DEBUG, messageStr, fileName);
LogMsg(INFO, "exit _python_ibm_db_init_error_info()", fileName);
}
/* static void _python_ibm_db_check_sql_errors( SQLHANDLE handle, SQLSMALLINT hType, int rc, int cpy_to_global, char* ret_str, int API SQLSMALLINT recno)
*/
static void _python_ibm_db_check_sql_errors( SQLHANDLE handle, SQLSMALLINT hType, int rc, int cpy_to_global, char* ret_str, int API, SQLSMALLINT recno )
{
LogMsg(INFO, "entry _python_ibm_db_check_sql_errors", fileName);
SQLCHAR msg[SQL_MAX_MESSAGE_LENGTH + 1] = {0};
SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1] = {0};
SQLCHAR errMsg[DB2_MAX_ERR_MSG_LEN] = {0};
SQLCHAR errcode[SQL_SQLCODE_SIZE + 1] = {0};
SQLINTEGER sqlcode = 0;
SQLSMALLINT length = 0;
char *p= NULL;
SQLINTEGER rc1 = SQL_SUCCESS;
int i = 0;
snprintf(messageStr, sizeof(messageStr), "handle=%p, hType=%d, rc=%d, cpy_to_global=%d, API=%d, recno=%d",
handle, hType, rc, cpy_to_global, API, recno);
LogMsg(DEBUG, messageStr, fileName);
memset(errMsg, '\0', DB2_MAX_ERR_MSG_LEN);
memset(msg, '\0', SQL_MAX_MESSAGE_LENGTH + 1);
rc1 = SQLGetDiagRec( hType, handle, recno, sqlstate, &sqlcode, msg,
SQL_MAX_MESSAGE_LENGTH + 1, &length );
snprintf(messageStr, sizeof(messageStr), "SQLGetDiagRec returned rc1=%d, sqlstate=%s, sqlcode=%d, msg=%s, length=%d",
rc1, sqlstate, sqlcode, msg, length);
sprintf((char*)errcode, "SQLCODE=%d",(int)sqlcode);
LogMsg(DEBUG, messageStr, fileName);
if ( rc1 == SQL_SUCCESS )
{
while ((p = strchr( (char *)msg, '\n' ))) {
*p = '\0';
}
sprintf((char*)errMsg, "%s SQLCODE=%d", (char*)msg, (int)sqlcode);
#ifdef _WIN32
for(i = 0; i < strlen(errMsg); i++)
{
if(errMsg[i] == '\r')
{
errMsg[i] = ' ';
}
}
#endif
LogMsg(ERROR, errMsg, fileName);
if (cpy_to_global != 0 && rc != 1 ) {
PyErr_SetString(PyExc_Exception, (char *) errMsg);
}
switch (rc) {
case SQL_ERROR:
/* Need to copy the error msg and sqlstate into the symbol Table
* to cache these results */
if ( cpy_to_global ) {
switch (hType) {
case SQL_HANDLE_DBC:
snprintf(messageStr, sizeof(messageStr),
"Copying to global: SQL_HANDLE_DBC, sqlstate=%s, errMsg=%s",
sqlstate, errMsg);
LogMsg(DEBUG, messageStr, fileName);
strncpy( IBM_DB_G(__python_conn_err_state),
(char*)sqlstate, SQL_SQLSTATE_SIZE+1);
strncpy( IBM_DB_G(__python_conn_err_msg),
(char*)errMsg, DB2_MAX_ERR_MSG_LEN);
strncpy( IBM_DB_G(__python_err_code),
(char*)errcode, SQL_SQLCODE_SIZE);
break;
case SQL_HANDLE_STMT:
snprintf(messageStr, sizeof(messageStr),
"Copying to global: SQL_HANDLE_STMT, sqlstate=%s, errMsg=%s",
sqlstate, errMsg);
LogMsg(DEBUG, messageStr, fileName);
strncpy( IBM_DB_G(__python_stmt_err_state),
(char*)sqlstate, SQL_SQLSTATE_SIZE+1);
strncpy( IBM_DB_G(__python_stmt_err_msg),
(char*)errMsg, DB2_MAX_ERR_MSG_LEN);
strncpy( IBM_DB_G(__python_err_code),
(char*)errcode, SQL_SQLCODE_SIZE);
break;
}
}
/* This call was made from ibm_db_errmsg or ibm_db_error or ibm_db_warn */
/* Check for error and return */
switch (API) {
case DB2_ERR:
if ( ret_str != NULL ) {
snprintf(messageStr, sizeof(messageStr), "Returning SQLSTATE for DB2_ERR: %s", sqlstate);
LogMsg(DEBUG, messageStr, fileName);
strncpy(ret_str, (char*)sqlstate, SQL_SQLSTATE_SIZE+1);
}
return;
case DB2_ERRMSG:
if ( ret_str != NULL ) {
snprintf(messageStr, sizeof(messageStr), "Returning error message for DB2_ERRMSG: %s", errMsg);
LogMsg(DEBUG, messageStr, fileName);
strncpy(ret_str, (char*)errMsg, DB2_MAX_ERR_MSG_LEN);
}
return;
default:
break;
}
break;
case SQL_SUCCESS_WITH_INFO:
/* Need to copy the warning msg and sqlstate into the symbol Table
* to cache these results */
if ( cpy_to_global ) {
switch ( hType ) {
case SQL_HANDLE_DBC:
snprintf(messageStr, sizeof(messageStr),
"Copying warning to global: SQL_HANDLE_DBC, sqlstate=%s, errMsg=%s",
sqlstate, errMsg);
LogMsg(DEBUG, messageStr, fileName);
strncpy(IBM_DB_G(__python_conn_warn_state),
(char*)sqlstate, SQL_SQLSTATE_SIZE+1);
strncpy(IBM_DB_G(__python_conn_warn_msg),
(char*)errMsg, DB2_MAX_ERR_MSG_LEN);
break;
case SQL_HANDLE_STMT:
snprintf(messageStr, sizeof(messageStr),
"Copying warning to global: SQL_HANDLE_STMT, sqlstate=%s, errMsg=%s",
sqlstate, errMsg);
LogMsg(DEBUG, messageStr, fileName);
strncpy(IBM_DB_G(__python_stmt_warn_state),
(char*)sqlstate, SQL_SQLSTATE_SIZE+1);
strncpy(IBM_DB_G(__python_stmt_warn_msg),
(char*)errMsg, DB2_MAX_ERR_MSG_LEN);
break;
}
}
/* This call was made from ibm_db_errmsg or ibm_db_error or ibm_db_warn */
/* Check for error and return */
if ( (API == DB2_WARNMSG) && (ret_str != NULL) ) {
snprintf(messageStr, sizeof(messageStr), "Returning warning message for DB2_WARNMSG: %s", errMsg);
LogMsg(DEBUG, messageStr, fileName);
strncpy(ret_str, (char*)errMsg, DB2_MAX_ERR_MSG_LEN);
}
return;
default:
break;
}
}
}
/* static int _python_ibm_db_assign_options( void *handle, int type, long opt_key, PyObject *data ) */
static int _python_ibm_db_assign_options( void *handle, int type, long opt_key, PyObject *data )
{
LogMsg(INFO, "entry _python_ibm_db_assign_options()", fileName);
int rc = 0;
long option_num = 0;
SQLINTEGER value_int = 0;
#ifdef __MVS__
SQLCHAR *option_str = NULL;
#else
SQLWCHAR *option_str = NULL;
#endif
int isNewBuffer;
snprintf(messageStr, sizeof(messageStr), "Parameters - Handle: %p, type: %d, opt_key: %ld, data: %p", handle, type, opt_key, data);
LogMsg(DEBUG, messageStr, fileName);
/* First check to see if it is a non-cli attribut */
if (opt_key == ATTR_CASE) {
option_num = NUM2LONG(data);
snprintf(messageStr, sizeof(messageStr), "ATTR_CASE option_num: %ld", option_num);
LogMsg(DEBUG, messageStr, fileName);
if (type == SQL_HANDLE_STMT) {
switch (option_num) {
case CASE_LOWER:
((stmt_handle*)handle)->s_case_mode = CASE_LOWER;
snprintf(messageStr, sizeof(messageStr), "Setting s_case_mode to CASE_LOWER");
LogMsg(DEBUG, messageStr, fileName);
break;
case CASE_UPPER:
((stmt_handle*)handle)->s_case_mode = CASE_UPPER;
snprintf(messageStr, sizeof(messageStr), "Setting s_case_mode to CASE_UPPER");
LogMsg(DEBUG, messageStr, fileName);
break;
case CASE_NATURAL:
((stmt_handle*)handle)->s_case_mode = CASE_NATURAL;
snprintf(messageStr, sizeof(messageStr), "Setting s_case_mode to CASE_UPPER");
LogMsg(DEBUG, messageStr, fileName);
break;
default:
LogMsg(EXCEPTION, "ATTR_CASE attribute must be one of CASE_LOWER, CASE_UPPER, or CASE_NATURAL", fileName);
PyErr_SetString(PyExc_Exception, "ATTR_CASE attribute must be one of CASE_LOWER, CASE_UPPER, or CASE_NATURAL");
return -1;
}
} else if (type == SQL_HANDLE_DBC) {
switch (option_num) {
case CASE_LOWER:
((conn_handle*)handle)->c_case_mode = CASE_LOWER;
snprintf(messageStr, sizeof(messageStr), "Setting c_case_mode to CASE_LOWER");
LogMsg(DEBUG, messageStr, fileName);
break;
case CASE_UPPER:
((conn_handle*)handle)->c_case_mode = CASE_UPPER;
snprintf(messageStr, sizeof(messageStr), "Setting c_case_mode to CASE_UPPER");
LogMsg(DEBUG, messageStr, fileName);
break;
case CASE_NATURAL:
((conn_handle*)handle)->c_case_mode = CASE_NATURAL;
snprintf(messageStr, sizeof(messageStr), "Setting c_case_mode to CASE_NATURAL");
LogMsg(DEBUG, messageStr, fileName);
break;
default:
LogMsg(EXCEPTION, "ATTR_CASE attribute must be one of CASE_LOWER, CASE_UPPER, or CASE_NATURAL", fileName);
PyErr_SetString(PyExc_Exception, "ATTR_CASE attribute must be one of CASE_LOWER, CASE_UPPER, or CASE_NATURAL");
return -1;
}
} else {
LogMsg(ERROR, "Connection or statement handle must be passed in.", fileName);
PyErr_SetString(PyExc_Exception, "Connection or statement handle must be passed in.");
return -1;
}
} else if (opt_key == USE_WCHAR) {
option_num = NUM2LONG(data);
snprintf(messageStr, sizeof(messageStr), "USE_WCHAR option_num: %ld", option_num);
LogMsg(DEBUG, messageStr, fileName);
if (type == SQL_HANDLE_STMT) {
switch (option_num) {
case WCHAR_YES:
((stmt_handle*)handle)->s_use_wchar = WCHAR_YES;
snprintf(messageStr, sizeof(messageStr), "Setting s_use_wchar to WCHAR_YES");
LogMsg(DEBUG, messageStr, fileName);
break;
case WCHAR_NO:
((stmt_handle*)handle)->s_use_wchar = WCHAR_NO;
snprintf(messageStr, sizeof(messageStr), "Setting s_use_wchar to WCHAR_NO");
LogMsg(DEBUG, messageStr, fileName);
break;
default:
LogMsg(EXCEPTION, "USE_WCHAR attribute must be one of WCHAR_YES or WCHAR_NO", fileName);
PyErr_SetString(PyExc_Exception, "USE_WCHAR attribute must be one of WCHAR_YES or WCHAR_NO");
return -1;
}
}
else if (type == SQL_HANDLE_DBC) {
switch (option_num) {
case WCHAR_YES:
((conn_handle*)handle)->c_use_wchar = WCHAR_YES;
snprintf(messageStr, sizeof(messageStr), "Setting c_use_wchar to WCHAR_YES");
LogMsg(DEBUG, messageStr, fileName);
break;
case WCHAR_NO:
((conn_handle*)handle)->c_use_wchar = WCHAR_NO;
snprintf(messageStr, sizeof(messageStr), "Setting c_use_wchar to WCHAR_NO");
LogMsg(DEBUG, messageStr, fileName);
break;
default:
LogMsg(EXCEPTION, "USE_WCHAR attribute must be one of WCHAR_YES or WCHAR_NO", fileName);
PyErr_SetString(PyExc_Exception, "USE_WCHAR attribute must be one of WCHAR_YES or WCHAR_NO");
return -1;
}
}
} else if (type == SQL_HANDLE_STMT) {
if (PyString_Check(data)|| PyUnicode_Check(data)) {
data = PyUnicode_FromObject(data);
snprintf(messageStr, sizeof(messageStr), "Converted data to Unicode: %p", data);
LogMsg(DEBUG, messageStr, fileName);
#ifdef __MVS__
option_str = getUnicodeDataAsSQLCHAR(data, &isNewBuffer);
snprintf(messageStr, sizeof(messageStr), "Option string (SQLCHAR) pointer: %p, isNewBuffer: %d", option_str, isNewBuffer);
LogMsg(DEBUG, messageStr, fileName);
Py_BEGIN_ALLOW_THREADS;
rc = SQLSetStmtAttr((SQLHSTMT)((stmt_handle *)handle)->hstmt, opt_key, (SQLPOINTER)option_str, SQL_IS_INTEGER );
Py_END_ALLOW_THREADS;
snprintf(messageStr, sizeof(messageStr), "SQLSetStmtAttr called with hstmt: %p, opt_key: %ld, option_str: %p, SQL_IS_INTEGER, and returned rc: %d",
((stmt_handle*)handle)->hstmt, opt_key, option_str, rc);
LogMsg(DEBUG, messageStr, fileName);
#else
option_str = getUnicodeDataAsSQLWCHAR(data, &isNewBuffer);
snprintf(messageStr, sizeof(messageStr), "Option string (SQLWCHAR) pointer: %p, isNewBuffer: %d", option_str, isNewBuffer);
LogMsg(DEBUG, messageStr, fileName);
Py_BEGIN_ALLOW_THREADS;
rc = SQLSetStmtAttrW((SQLHSTMT)((stmt_handle *)handle)->hstmt, opt_key, (SQLPOINTER)option_str, SQL_IS_INTEGER );
Py_END_ALLOW_THREADS;
snprintf(messageStr, sizeof(messageStr), "SQLSetStmtAttrW called with hstmt: %p, opt_key: %ld, option_str: %p, SQL_IS_INTEGER, and returned rc: %d",
((stmt_handle*)handle)->hstmt, opt_key, option_str, rc);
LogMsg(DEBUG, messageStr, fileName);
#endif
if ( rc == SQL_ERROR ) {
_python_ibm_db_check_sql_errors((SQLHSTMT)((stmt_handle *)handle)->hstmt, SQL_HANDLE_STMT, rc, 1, NULL, -1, 1);
}
if (isNewBuffer)
PyMem_Del(option_str);
} else {
option_num = NUM2LONG(data);
snprintf(messageStr, sizeof(messageStr), "Option number: %ld", option_num);
LogMsg(DEBUG, messageStr, fileName);
if (opt_key == SQL_ATTR_AUTOCOMMIT && option_num == SQL_AUTOCOMMIT_OFF) {
((conn_handle*)handle)->auto_commit = 0;
snprintf(messageStr, sizeof(messageStr), "Setting auto_commit to OFF");
LogMsg(DEBUG, messageStr, fileName);
}
else if (opt_key == SQL_ATTR_AUTOCOMMIT && option_num == SQL_AUTOCOMMIT_ON) {
((conn_handle*)handle)->auto_commit = 1;
snprintf(messageStr, sizeof(messageStr), "Setting auto_commit to ON");
LogMsg(DEBUG, messageStr, fileName);
}
Py_BEGIN_ALLOW_THREADS;
rc = SQLSetStmtAttr((SQLHSTMT)((stmt_handle *)handle)->hstmt, opt_key, (SQLPOINTER)option_num, SQL_IS_INTEGER );
Py_END_ALLOW_THREADS;
snprintf(messageStr, sizeof(messageStr), "SQLSetStmtAttr called with hstmt: %p, opt_key: %ld, option_num: %ld, SQL_IS_INTEGER, and returned rc: %d",
((stmt_handle*)handle)->hstmt, opt_key, option_num, rc);
LogMsg(DEBUG, messageStr, fileName);
if ( rc == SQL_ERROR ) {
_python_ibm_db_check_sql_errors((SQLHSTMT)((stmt_handle *)handle)->hstmt, SQL_HANDLE_STMT, rc, 1, NULL, -1, 1);
}
if (opt_key == SQL_ATTR_CURSOR_TYPE){
((stmt_handle *)handle)->cursor_type = option_num;
snprintf(messageStr, sizeof(messageStr), "Cursor type set to %ld", option_num);
LogMsg(DEBUG, messageStr, fileName);
if ( rc == SQL_SUCCESS_WITH_INFO ) {
Py_BEGIN_ALLOW_THREADS;
rc = SQLGetStmtAttr( ((stmt_handle *)handle)->hstmt, opt_key, &value_int, SQL_IS_INTEGER, NULL);
Py_END_ALLOW_THREADS;
snprintf(messageStr, sizeof(messageStr), "SQLGetStmtAttr called with hstmt: %p, opt_key: %ld, value_int: %d, SQL_IS_INTEGER, and retured rc: %d",
((stmt_handle*)handle)->hstmt, opt_key, value_int, rc);
LogMsg(DEBUG, messageStr, fileName);
if (rc == SQL_ERROR) {
_python_ibm_db_check_sql_errors(((stmt_handle *)handle)->hstmt, SQL_HANDLE_STMT, rc, 1, NULL, -1, 1);
PyErr_Clear();
return -1;
}
((stmt_handle *)handle)->cursor_type = value_int;
}
}
}
} else if (type == SQL_HANDLE_DBC) {
if (PyString_Check(data)|| PyUnicode_Check(data)) {
data = PyUnicode_FromObject(data);
snprintf(messageStr, sizeof(messageStr), "Converted data to Unicode: %p", data);
LogMsg(DEBUG, messageStr, fileName);
#ifdef __MVS__
option_str = getUnicodeDataAsSQLCHAR(data, &isNewBuffer);
snprintf(messageStr, sizeof(messageStr), "Option string (SQLCHAR) pointer: %p, isNewBuffer: %d", option_str, isNewBuffer);
LogMsg(DEBUG, messageStr, fileName);
Py_BEGIN_ALLOW_THREADS;
rc = SQLSetConnectAttr((SQLHSTMT)((conn_handle*)handle)->hdbc, opt_key, (SQLPOINTER)option_str, SQL_NTS);
snprintf(messageStr, sizeof(messageStr),"SQLSetConnectAttr called with hdbc: %p, opt_key: %ld, option_str: %p, SQL_NTS, and retured rc: %d",
((conn_handle*)handle)->hdbc, opt_key, (void*)option_str, rc);
LogMsg(DEBUG, messageStr, fileName);
Py_END_ALLOW_THREADS;
#else
option_str = getUnicodeDataAsSQLWCHAR(data, &isNewBuffer);
snprintf(messageStr, sizeof(messageStr), "Option string (SQLWCHAR) pointer: %p, isNewBuffer: %d", option_str, isNewBuffer);
LogMsg(DEBUG, messageStr, fileName);
Py_BEGIN_ALLOW_THREADS;
rc = SQLSetConnectAttrW((SQLHSTMT)((conn_handle*)handle)->hdbc, opt_key, (SQLPOINTER)option_str, SQL_NTS);
snprintf(messageStr, sizeof(messageStr), "SQLSetConnectAttrW called with hdbc: %p, opt_key: %ld, option_str: %p, SQL_NTS, and retured rc: %d",
((conn_handle*)handle)->hdbc, opt_key, (void*)option_str, rc);
LogMsg(INFO, messageStr, fileName);
Py_END_ALLOW_THREADS;
#endif
if ( rc == SQL_ERROR ) {
_python_ibm_db_check_sql_errors((SQLHSTMT)((stmt_handle *)handle)->hstmt, SQL_HANDLE_STMT, rc, 1, NULL, -1, 1);
}
if (isNewBuffer)
PyMem_Del(option_str);
} else {