-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConnection.c
More file actions
2377 lines (1981 loc) · 75.5 KB
/
Connection.c
File metadata and controls
2377 lines (1981 loc) · 75.5 KB
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
//-----------------------------------------------------------------------------
// Connection.c
// Definition of the Python type DmConnection.
//-----------------------------------------------------------------------------
#include "py_Dameng.h"
#include "Error.h"
#include "Buffer.h"
#include "trc.h"
#define DMPY_ATTR_MAX_IDENTIFIER_LENGTH 11111
#define DMPY_ATTR_STMT_CACHE_SIZE 11112
#define DMPY_ATTR_OUTPUT_TYPE_HANDLER 11113
//-----------------------------------------------------------------------------
// Constants for the Python type "Connection" Attributes
//-----------------------------------------------------------------------------
#define DMPYTHON_STR_MAXLEN 2048
static udint4 gc_attr_access_mode = DSQL_ATTR_ACCESS_MODE;
static udint4 gc_attr_async_enalbe = DSQL_ATTR_ASYNC_ENABLE;
static udint4 gc_attr_auto_ipd = DSQL_ATTR_AUTO_IPD;
static udint4 gc_attr_autocommit = DSQL_ATTR_AUTOCOMMIT;
static udint4 gc_attr_conn_dead = DSQL_ATTR_CONNECTION_DEAD;
static udint4 gc_attr_conn_timeout = DSQL_ATTR_CONNECTION_TIMEOUT;
static udint4 gc_attr_login_timeout = DSQL_ATTR_LOGIN_TIMEOUT;
static udint4 gc_attr_packet_size = DSQL_ATTR_PACKET_SIZE;
static udint4 gc_attr_txn_isolation = DSQL_ATTR_TXN_ISOLATION;
static udint4 gc_attr_login_port = DSQL_ATTR_LOGIN_PORT;
static udint4 gc_attr_str_case_sencitive = DSQL_ATTR_STR_CASE_SENSITIVE;
static udint4 gc_attr_max_row_size = DSQL_ATTR_MAX_ROW_SIZE;
static udint4 gc_attr_login_user = DSQL_ATTR_LOGIN_USER;
static udint4 gc_attr_login_server = DSQL_ATTR_LOGIN_SERVER;
static udint4 gc_attr_instance_name = DSQL_ATTR_INSTANCE_NAME;
static udint4 gc_attr_current_schema = DSQL_ATTR_CURRENT_SCHEMA;
static udint4 gc_attr_server_code = DSQL_ATTR_SERVER_CODE;
static udint4 gc_attr_local_code = DSQL_ATTR_LOCAL_CODE;
static udint4 gc_attr_lang_id = DSQL_ATTR_LANG_ID;
static udint4 gc_attr_app_name = DSQL_ATTR_APP_NAME;
static udint4 gc_attr_compres_msg = DSQL_ATTR_COMPRESS_MSG;
static udint4 gc_attr_rwseparate = DSQL_ATTR_RWSEPARATE;
static udint4 gc_attr_rwseparate_percent = DSQL_ATTR_RWSEPARATE_PERCENT;
static udint4 gc_attr_current_catalog = DSQL_ATTR_CURRENT_CATALOG;
static udint4 gc_attr_trx_state = DSQL_ATTR_TRX_STATE;
static udint4 gc_attr_use_stmt_pool = DSQL_ATTR_USE_STMT_POOL;
static udint4 gc_attr_ssl_path = DSQL_ATTR_SSL_PATH;
static udint4 gc_attr_mpp_login = DSQL_ATTR_MPP_LOGIN;
static udint4 gc_attr_server_version = DSQL_ATTR_SERVER_VERSION;
static udint4 gc_attr_cursor_rollback_behavior = DSQL_ATTR_CURSOR_ROLLBACK_BEHAVIOR;
static udint4 gc_attr_max_identifier_length = DMPY_ATTR_MAX_IDENTIFIER_LENGTH;
static udint4 gc_attr_stmt_cache_size = DMPY_ATTR_STMT_CACHE_SIZE;
static udint4 gc_attr_output_type_handler = DMPY_ATTR_OUTPUT_TYPE_HANDLER;
/* 不可读取,只允许设置 */
static udint4 gc_attr_ssl_pwd = DSQL_ATTR_SSL_PWD;
static udint4 gc_attr_ukey_name = DSQL_ATTR_UKEY_NAME;
static udint4 gc_attr_ukey_pin = DSQL_ATTR_UKEY_PIN;
/* dpi未支持 */
static udint4 gc_attr_trace = DSQL_ATTR_TRACE;
static udint4 gc_attr_trace_file = DSQL_ATTR_TRACEFILE;
//-----------------------------------------------------------------------------
// functions for the Python type "Connection"
//-----------------------------------------------------------------------------
static
void
Connection_init_inner(
dm_Connection* self
)
{
Py_INCREF(Py_None);
self->environment = (dm_Environment *)Py_None;
Py_INCREF(Py_None);
self->username = Py_None;
Py_INCREF(Py_None);
self->schema = Py_None;
Py_INCREF(Py_None);
self->password = Py_None;
Py_INCREF(Py_None);
self->server = Py_None;
Py_INCREF(Py_None);
self->port = Py_None;
Py_INCREF(Py_None);
self->dsn = Py_None;
Py_INCREF(Py_None);
self->inputTypeHandler = Py_None;
Py_INCREF(Py_None);
self->outputTypeHandler = Py_None;
Py_INCREF(Py_None);
self->version = Py_None;
Py_INCREF(Py_None);
self->server_status = Py_None;
Py_INCREF(Py_None);
self->warning = Py_None;
Py_INCREF(Py_None);
self->catalog = Py_None;
Py_INCREF(Py_None);
self->parse_type = Py_None;
self->isConnected = 0;
}
static
void
Connection_Free_inner(
dm_Connection* self
)
{
Py_CLEAR(self->username);
Py_CLEAR(self->password);
Py_CLEAR(self->server);
Py_CLEAR(self->port);
Py_CLEAR(self->dsn);
Py_CLEAR(self->inputTypeHandler);
Py_CLEAR(self->outputTypeHandler);
Py_CLEAR(self->environment);
Py_CLEAR(self->server_status);
Py_CLEAR(self->warning);
Py_CLEAR(self->catalog);
Py_CLEAR(self->version);
Py_CLEAR(self->parse_type);
}
//-----------------------------------------------------------------------------
// Connection_SplitComponent()
// Split the component out of the source and replace the source with the
// characters up to the split string and put the characters after the split
// string in to the target.
//-----------------------------------------------------------------------------
static
int
Connection_SplitComponent(
PyObject** sourceObj, // source object to split
PyObject** targetObj, // target object (for component)
const char* splitString // split string (assume one character)
)
{
char* source_str = NULL;
char* target_str = NULL;
char* pos = NULL;
if (*sourceObj == Py_None || *targetObj != Py_None || splitString == NULL)
return 0;
Py_INCREF(*sourceObj);
source_str = py_String_asString(*sourceObj);
if (PyErr_Occurred())
{
return -1;
}
if (source_str == NULL || target_str != NULL)
{
return 0;
}
if (strcmp(splitString, ":") == 0)
{
pos = strstr(source_str, "]");
if (pos == NULL)
pos = strstr(source_str, splitString);
else
pos = strstr(pos, splitString);
}
else
pos = strstr(source_str, splitString);
if (pos == NULL)
{
return 0;
}
*pos = 0;
*sourceObj = Py_BuildValue("s", source_str);
if(strcmp(splitString,"?catalog=") == 0)
*targetObj = Py_BuildValue("s", pos + 9);
else
*targetObj = Py_BuildValue("s", pos + 1);
//还原pos处的内容
*pos = *splitString;
return 1;
}
//-----------------------------------------------------------------------------
// Connection_IsConnected()
// Determines if the connection object is connected to the database. If not,
// a Python exception is raised.
//-----------------------------------------------------------------------------
static int Connection_IsConnected_without_err(
dm_Connection* self // connection to check
)
{
if (!self->hcon)
{
return -1;
}
return 0;
}
static int Connection_IsConnected(
dm_Connection* self // connection to check
)
{
if (Connection_IsConnected_without_err(self) < 0)
{
PyErr_SetString(g_InterfaceErrorException, "not connected");
return -1;
}
return 0;
}
static int Connection_IsLogin(
dm_Connection* self, // connection to check
sdint2 isRaiseExcepiton
)
{
if (!self->isConnected)
{
if (isRaiseExcepiton)
PyErr_SetString(g_InterfaceErrorException, "not login");
return -1;
}
return 0;
}
static
PyObject*
Connection_Debug_inner(
dm_Connection* self,
PyObject* args
)
{
udint4 debug_type;
dhstmt hstmt;
sdbyte sql_txt[128];
DPIRETURN rt = DSQL_SUCCESS;
Py_ssize_t num = 0;
// make sure we are connected
if (Connection_IsConnected(self) < 0)
{
PyErr_SetString(g_ErrorException, "not connected");
Py_INCREF(Py_None);
return Py_None;
}
// 若未登录,则直接返回
if (Connection_IsLogin(self, 0) < 0)
{
PyErr_SetString(g_ErrorException, "not login");
Py_INCREF(Py_None);
return Py_None;
}
num = PyTuple_Size(args);
if (num == 0)
{
//没有指定参数,使用默认值
debug_type = DEBUG_OPEN;
}
else
{
// parse the arguments
if (!PyArg_ParseTuple(args, "i", &debug_type))
{
PyErr_SetString(g_ErrorException, "invalid arguments");
return NULL;
}
}
if (debug_type < 0 || debug_type > 3)
{
PyErr_SetString(g_ErrorException, "invalid arguments");
return NULL;
}
// 申请语句句柄
Py_BEGIN_ALLOW_THREADS
rt = dpi_alloc_stmt(self->hcon, &hstmt);
Py_END_ALLOW_THREADS
if (Environment_CheckForError(self->environment, self->hcon, DSQL_HANDLE_DBC, rt, "Connection_Debug():dpi_alloc_stmt") < 0)
return NULL;
aq_sprintf(sql_txt, 128, "SP_SET_PARA_VALUE(1, 'SVR_LOG', %d)", debug_type);
Py_BEGIN_ALLOW_THREADS
rt = dpi_exec_direct(hstmt, sql_txt);
dpi_free_stmt(hstmt);
Py_END_ALLOW_THREADS
if (Environment_CheckForError(self->environment, self->hcon, DSQL_HANDLE_DBC, rt, "Connection_Debug():dpi_exec_direct") < 0)
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
static
PyObject*
Connection_Debug(
dm_Connection* self,
PyObject* args
)
{
PyObject* rt_obj;
DMPYTHON_TRACE_INFO(dpy_trace(NULL, args, "ENTER Connect_Debug\n"));
rt_obj = Connection_Debug_inner(self, args);
DMPYTHON_TRACE_INFO(dpy_trace(NULL, args, "EXIT Connect_Debug, %s\n", rt_obj == NULL ? "FAILED" : "SUCCESS"));
return rt_obj;
}
static
PyObject*
Connection_Shutdown_inner(
dm_Connection* self,
PyObject* args
)
{
char* shutdown_type;
dhstmt hstmt;
sdbyte sql_txt[128];
DPIRETURN rt = DSQL_SUCCESS;
Py_ssize_t num = 0;
// make sure we are connected
if (Connection_IsConnected(self) < 0)
{
PyErr_SetString(g_ErrorException, "not connected");
Py_INCREF(Py_None);
return Py_None;
}
// 若未登录,则直接返回
if (Connection_IsLogin(self, 0) < 0)
{
PyErr_SetString(g_ErrorException, "not login");
Py_INCREF(Py_None);
return Py_None;
}
num = PyTuple_Size(args);
if (num == 0)
{
//没有指定参数,使用default值
shutdown_type = SHUTDOWN_DEFAULT;
}
else
{
// parse the arguments
if (!PyArg_ParseTuple(args, "s", &shutdown_type))
{
PyErr_SetString(g_ErrorException, "invalid arguments");
return NULL;
}
if(strlen(shutdown_type) > 15)
{
PyErr_SetString(g_ErrorException, "invalid arguments");
return NULL;
}
}
// 申请语句句柄
Py_BEGIN_ALLOW_THREADS
rt = dpi_alloc_stmt(self->hcon, &hstmt);
Py_END_ALLOW_THREADS
if (Environment_CheckForError(self->environment, self->hcon, DSQL_HANDLE_DBC, rt, "Connection_Debug():dpi_alloc_stmt") < 0)
return NULL;
aq_sprintf(sql_txt, 128, "SHUTDOWN %s", shutdown_type);
Py_BEGIN_ALLOW_THREADS
rt = dpi_exec_direct(hstmt, sql_txt);
dpi_free_stmt(hstmt);
Py_END_ALLOW_THREADS
if (Environment_CheckForError(self->environment, self->hcon, DSQL_HANDLE_DBC, rt, "Connection_Debug():dpi_exec_direct") < 0)
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
static
PyObject*
Connection_Shutdown(
dm_Connection* self,
PyObject* args
)
{
PyObject* rt_obj;
DMPYTHON_TRACE_INFO(dpy_trace(NULL, args, "ENTER Connect_Shutdown\n"));
rt_obj = Connection_Shutdown_inner(self, args);
DMPYTHON_TRACE_INFO(dpy_trace(NULL, args, "Exit Connect_Shutdown, %s\n", rt_obj == NULL ? "FAILED" : "SUCCESS"));
return rt_obj;
}
static
PyObject*
Connection_Close_inner(
dm_Connection* self
)
{
DPIRETURN status = DSQL_SUCCESS;
sdint4 is_active;
// make sure we are connected
if (Connection_IsConnected(self) < 0)
{
PyErr_Clear();
goto fun_end;
}
// 若未登录,则直接返回
if (Connection_IsLogin(self, 0) < 0)
{
goto fun_end;
}
// logout of the server
status = dpi_logout(self->hcon);
if (Environment_CheckForError(self->environment, self->hcon, DSQL_HANDLE_DBC, status,
"Connection_Close(): logout") < 0)
return NULL;
fun_end:
// free handle
if (self->hcon)
{
dpi_free_con(self->hcon);
self->hcon = NULL;
}
Connection_Free_inner(self);
/** 便于最后一个引用释放之前访问 **/
Connection_init_inner(self);
Py_INCREF(Py_None);
return Py_None;
}
static
PyObject*
Connection_Close(
dm_Connection* self
)
{
PyObject* rt_obj;
DMPYTHON_TRACE_INFO(dpy_trace(NULL, NULL, "ENTER Connect_Close\n"));
rt_obj = Connection_Close_inner(self);
DMPYTHON_TRACE_INFO(dpy_trace(NULL, NULL, "ENTER Connect_Close, %s\n", rt_obj == NULL ? "FAILED" : "SUCCESS"));
return rt_obj;
}
static
PyObject*
Connection_Commit_inner(
dm_Connection* self,
PyObject* args
)
{
DPIRETURN rt = DSQL_SUCCESS;
// make sure we are acturally connected
if (Connection_IsConnected(self) < 0)
return NULL;
// 若未登录,则直接返回
if (Connection_IsLogin(self, 0) < 0){
Py_INCREF(Py_None);
return Py_None;
}
// perform a commit operation
Py_BEGIN_ALLOW_THREADS
rt = dpi_commit(self->hcon);
Py_END_ALLOW_THREADS
if (Environment_CheckForError(self->environment, self->hcon, DSQL_HANDLE_DBC, rt,
"Connection_Commit()") < 0)
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
static
PyObject*
Connection_Commit(
dm_Connection* self,
PyObject* args
)
{
PyObject* rt_obj;
DMPYTHON_TRACE_INFO(dpy_trace(NULL, args, "ENTER Connect_Commit\n"));
rt_obj = Connection_Commit_inner(self, args);
DMPYTHON_TRACE_INFO(dpy_trace(NULL, args, "EXIT Connect_Commit, %s\n", rt_obj == NULL ? "FAILED" : "SUCCESS"));
return rt_obj;
}
static
PyObject*
Connection_Rollback_inner(
dm_Connection* self,
PyObject* args
)
{
DPIRETURN rt = DSQL_SUCCESS;
// make sure we are acturally connected
if (Connection_IsConnected(self) < 0)
return NULL;
// 若未登录,则直接返回
if (Connection_IsLogin(self, 0) < 0){
Py_INCREF(Py_None);
return Py_None;
}
// perform the rollback operation
Py_BEGIN_ALLOW_THREADS
rt = dpi_rollback(self->hcon);
Py_END_ALLOW_THREADS
if (Environment_CheckForError(self->environment, self->hcon, DSQL_HANDLE_DBC, rt,
"Connection_Rollback") < 0)
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
static
PyObject*
Connection_Rollback(
dm_Connection* self,
PyObject* args
)
{
PyObject* rt_obj;
DMPYTHON_TRACE_INFO(dpy_trace(NULL, args, "ENTER Connect_Rollback\n"));
rt_obj = Connection_Rollback_inner(self, args);
DMPYTHON_TRACE_INFO(dpy_trace(NULL, args, "EXIT Connect_Rollback, %s\n", rt_obj == NULL ? "FAILED" : "SUCCESS"));
return rt_obj;
}
static PyObject *Connection_GetConAttr(
dm_Connection* self, // connection
sdint4* attr_id // attribute type
)
{
DPIRETURN rt = DSQL_SUCCESS;
sdint4 len;
sdint4 int4Value;
sdint2 int2Value;
udint4 uint4Value;
sdbyte strValue[500];
PyObject* retObj = NULL;
// check if connected
if (Connection_IsConnected(self) < 0)
{
return NULL;
}
switch(*attr_id){
case DSQL_ATTR_ACCESS_MODE:
case DSQL_ATTR_ASYNC_ENABLE:
case DSQL_ATTR_AUTO_IPD:
case DSQL_ATTR_AUTOCOMMIT:
case DSQL_ATTR_CONNECTION_DEAD:
case DSQL_ATTR_CONNECTION_TIMEOUT:
case DSQL_ATTR_LOGIN_TIMEOUT:
case DSQL_ATTR_PACKET_SIZE:
case DSQL_ATTR_TXN_ISOLATION:
case DSQL_ATTR_MAX_ROW_SIZE:
case DSQL_ATTR_LANG_ID:
case DSQL_ATTR_LOCAL_CODE:
case DSQL_ATTR_SERVER_CODE:
case DSQL_ATTR_USE_STMT_POOL:
case DSQL_ATTR_COMPRESS_MSG:
case DSQL_ATTR_RWSEPARATE:
case DSQL_ATTR_RWSEPARATE_PERCENT:
case DSQL_ATTR_TRX_STATE:
case DSQL_ATTR_MPP_LOGIN:
case DSQL_ATTR_CURSOR_ROLLBACK_BEHAVIOR:
Py_BEGIN_ALLOW_THREADS
rt = dpi_get_con_attr(self->hcon, *attr_id, (dpointer)(&int4Value), 0, &len);
Py_END_ALLOW_THREADS
if (DSQL_SUCCEEDED(rt))
{
return Py_BuildValue("i", int4Value);
}
break;
case DSQL_ATTR_LOGIN_PORT:
Py_BEGIN_ALLOW_THREADS
rt = dpi_get_con_attr(self->hcon, *attr_id, (dpointer)(&int2Value), 0, &len);
Py_END_ALLOW_THREADS
if (DSQL_SUCCEEDED(rt))
{
return Py_BuildValue("i", int2Value);
}
break;
case DSQL_ATTR_STR_CASE_SENSITIVE:
Py_BEGIN_ALLOW_THREADS
rt = dpi_get_con_attr(self->hcon, *attr_id, (dpointer)(&uint4Value), 0, &len);
Py_END_ALLOW_THREADS
if (DSQL_SUCCEEDED(rt))
{
return Py_BuildValue("i", uint4Value);
}
break;
case DMPY_ATTR_MAX_IDENTIFIER_LENGTH:
return Py_BuildValue("i", 128);
case DMPY_ATTR_STMT_CACHE_SIZE:
return Py_BuildValue("i", 20);
case DMPY_ATTR_OUTPUT_TYPE_HANDLER:
return Py_None;
default:
Py_BEGIN_ALLOW_THREADS
rt = dpi_get_con_attr(self->hcon, *attr_id, strValue, 500, &len);
Py_END_ALLOW_THREADS
if (DSQL_SUCCEEDED(rt))
{
return dmString_FromEncodedString(strValue, strlen(strValue), self->environment->encoding);
}
break;
}
Environment_CheckForError(self->environment, self->hcon, DSQL_HANDLE_DBC, rt, "Connection_GetConAttr()");
return NULL;
}
static int Connection_SetConAttr(
dm_Connection* self, // connection
PyObject* value, // attribute value to set
sdint4* attr_id // attribute type
){
DPIRETURN rt = DSQL_SUCCESS;
long numValue;
char* strValue;
sdint4 val_len = 0;
sdint2 isNumVal = 0; // 是否为数字值,0 否 1 是
dm_Buffer buffer;
sdint4 lang_id;
// check attributes
switch(*attr_id){
case DSQL_ATTR_ACCESS_MODE:
case DSQL_ATTR_ASYNC_ENABLE:
case DSQL_ATTR_AUTO_IPD:
case DSQL_ATTR_AUTOCOMMIT:
case DSQL_ATTR_CONNECTION_DEAD:
case DSQL_ATTR_CONNECTION_TIMEOUT:
case DSQL_ATTR_LOGIN_TIMEOUT:
case DSQL_ATTR_PACKET_SIZE:
case DSQL_ATTR_TXN_ISOLATION:
case DSQL_ATTR_LOGIN_PORT:
case DSQL_ATTR_STR_CASE_SENSITIVE:
case DSQL_ATTR_MAX_ROW_SIZE:
case DSQL_ATTR_LOCAL_CODE:
case DSQL_ATTR_LANG_ID:
case DSQL_ATTR_SERVER_CODE:
case DSQL_ATTR_USE_STMT_POOL:
case DSQL_ATTR_COMPRESS_MSG:
case DSQL_ATTR_RWSEPARATE:
case DSQL_ATTR_RWSEPARATE_PERCENT:
isNumVal = 1;
break;
default:
isNumVal = 0;
break;
}
// parse arguements by attribute type
if (isNumVal) // 数字
{
#if PY_MAJOR_VERSION >= 3
if (!PyLong_Check(value))
{
PyErr_SetString(PyExc_TypeError, "Invalid attribute value to set, expecting integer value");
return -1;
}
numValue = PyLong_AsUnsignedLong(value);
if (numValue < 0)
{
return -1;
}
if (numValue > INT_MAX)
{
PyErr_SetString(PyExc_OverflowError, "Invalid attribute value to set, the value is overflow");
return -1;
}
#else
if (!PyInt_Check(value))
{
PyErr_SetString(PyExc_TypeError, "Invalid attribute value to set, expecting integer value");
return -1;
}
numValue = PyInt_AsUnsignedLongMask(value);
if (numValue < 0 || numValue > INT_MAX)
{
PyErr_SetString(PyExc_OverflowError, "Invalid attribute value to set, the value is overflow");
return -1;
}
#endif
if (*attr_id == DSQL_ATTR_LOCAL_CODE)
{
if (numValue != PG_UTF8 && numValue != PG_GBK && numValue != PG_BIG5 && numValue != PG_GB18030)
{
lang_id = LANGUAGE_EN;
if (lang_id != self->environment->local_langid)
{
PyErr_SetString(PyExc_TypeError, "Invalid attribute value to set, this value only can be used when lang_id = LANGUAGE_EN");
return -1;
}
}
}
Py_BEGIN_ALLOW_THREADS
rt = dpi_set_con_attr(self->hcon, *attr_id, (dpointer)numValue, val_len);
Py_END_ALLOW_THREADS
}
else
{
if (!py_String_Check(value))
{
PyErr_SetString(PyExc_TypeError, "Invalid attribute value to set, expecting string value");
return -1;
}
if(dmBuffer_FromObject(&buffer, value, self->environment->encoding) < 0)
return -1;
strValue = PyMem_Malloc(buffer.size + 1);
strcpy(strValue, buffer.ptr);
dmBuffer_Clear(&buffer);
Py_BEGIN_ALLOW_THREADS
rt = dpi_set_con_attr(self->hcon, *attr_id, strValue, (sdint4)strlen(strValue));
Py_END_ALLOW_THREADS
PyMem_Free(strValue);
}
if(Environment_CheckForError(self->environment, self->hcon, DSQL_HANDLE_DBC, rt,
"Connection_SetConAttr()") < 0)
return -1;
/** 若设置LOCAL_CODE或者LANG_ID成功,则更新当前使用的环境对象的上编码方式 **/
if (*attr_id == DSQL_ATTR_LOCAL_CODE)
{
Environment_refresh_local_code(self->environment, self->hcon, self->environment->local_code);
}
if (*attr_id == DSQL_ATTR_LANG_ID)
{
Environment_refresh_local_langid(self->environment, self->hcon, self->environment->local_langid);
}
return 0;
}
static
int
Connection_Free(
dm_Connection* self // connection itself
)
{
if (Connection_IsConnected_without_err(self) >= 0)
Connection_Close(self);
Connection_Free_inner(self);
Py_TYPE(self)->tp_free((PyObject*) self);
return 0;
}
static
PyObject*
Connection_New(
PyTypeObject* type, // object type
PyObject* args, // arguments
PyObject* keywords // keywords
)
{
dm_Connection *self;
// create the object
self = (dm_Connection*) type->tp_alloc(type, 0);
if (!self)
return NULL;
self->environment = NULL;
self->isConnected = 0;
return (PyObject*) self;
}
/* 获取server_status字符串 */
static
void
Connection_make_svrstat(
dm_Connection* self
)
{
char print_info[1024];
PyObject* format = NULL;
PyObject* formatArgs = NULL;
dpi_get_diag_field(DSQL_HANDLE_DBC, self->hcon, 0, DSQL_DIAG_SERVER_STAT, print_info, sizeof(print_info), NULL);
self->server_status = dmString_FromEncodedString(print_info, strlen(print_info), self->environment->encoding);
}
static
int
Connection_connect_inner(
dm_Connection* self
)
{
DPIRETURN rt = DSQL_SUCCESS; // 返回值
dm_Buffer buffer;
//char *server, *username, *password;
char server[NAMELEN + 1];
char username[NAMELEN + 1];
char password[NAMELEN + 1];
char catalog[NAMELEN + 1];
char server_str[NAMELEN * 2 + 1];
sdint4 attr_id;
if(self->schema != Py_None)
{
if (dmBuffer_FromObject(&buffer, self->schema, self->environment->encoding) < 0)
return -1;
if (buffer.size > NAMELEN)
{
PyErr_SetString(g_ErrorException, "invalid schema");
dmBuffer_Clear(&buffer);
return -1;
}
dmBuffer_Clear(&buffer);
}
if(dmBuffer_FromObject(&buffer, self->server, self->environment->encoding) < 0)
return -1;
if(buffer.size > NAMELEN)
{
PyErr_SetString(g_ErrorException, "invalid server");
dmBuffer_Clear(&buffer);
return -1;
}
strcpy(server, buffer.ptr);
dmBuffer_Clear(&buffer);
if(dmBuffer_FromObject(&buffer, self->username, self->environment->encoding) < 0)
{
return -1;
}
if (buffer.size > NAMELEN)
{
PyErr_SetString(g_ErrorException, "invalid username");
dmBuffer_Clear(&buffer);
return -1;
}
strcpy(username, buffer.ptr);
dmBuffer_Clear(&buffer);
if(dmBuffer_FromObject(&buffer, self->password, self->environment->encoding) < 0)
{
return -1;
}
if (buffer.size > NAMELEN)
{
PyErr_SetString(g_ErrorException, "invalid password");
dmBuffer_Clear(&buffer);
return -1;
}
strcpy(password, buffer.ptr);
dmBuffer_Clear(&buffer);
if(self->catalog != Py_None)
{
if (dmBuffer_FromObject(&buffer, self->catalog, self->environment->encoding) < 0)
{
return -1;
}
if (buffer.size > NAMELEN)
{
PyErr_SetString(g_ErrorException, "invalid catalog");
dmBuffer_Clear(&buffer);
return -1;
}
strcpy(catalog, buffer.ptr);
dmBuffer_Clear(&buffer);
aq_sprintf(server_str, NAMELEN * 2 + 1, "%s/%s", server, catalog);
// 连接数据库服务器
Py_BEGIN_ALLOW_THREADS
rt = dpi_login(self->hcon, server_str, username, password);
Py_END_ALLOW_THREADS
}
else
{
// 连接数据库服务器
Py_BEGIN_ALLOW_THREADS
rt = dpi_login(self->hcon, server, username, password);
Py_END_ALLOW_THREADS
}
if (Environment_CheckForError(self->environment, self->hcon, DSQL_HANDLE_DBC, rt,
"Connection_connect():Connect to db server") < 0)
return -1;
// 读取是否自动提交属性,以及访问模式属性
attr_id = DSQL_ATTR_AUTOCOMMIT;
if (!Connection_GetConAttr(self, &attr_id))
return -1;
attr_id = DSQL_ATTR_ACCESS_MODE;
if (!Connection_GetConAttr(self, &attr_id))
return -1;
/* 连接成功后,获取server_status串 */