forked from oracle/python-cx_Oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.c
More file actions
1440 lines (1255 loc) · 54.5 KB
/
Connection.c
File metadata and controls
1440 lines (1255 loc) · 54.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
//-----------------------------------------------------------------------------
// Copyright 2016, 2017, Oracle and/or its affiliates. All rights reserved.
//
// Portions Copyright 2007-2015, Anthony Tuininga. All rights reserved.
//
// Portions Copyright 2001-2007, Computronix (Canada) Ltd., Edmonton, Alberta,
// Canada. All rights reserved.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Connection.c
// Definition of the Python type OracleConnection.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// structure for the Python type "Connection"
//-----------------------------------------------------------------------------
typedef struct {
PyObject_HEAD
dpiConn *handle;
udt_SessionPool *sessionPool;
PyObject *inputTypeHandler;
PyObject *outputTypeHandler;
PyObject *username;
PyObject *dsn;
PyObject *version;
dpiEncodingInfo encodingInfo;
int autocommit;
} udt_Connection;
//-----------------------------------------------------------------------------
// functions for the Python type "Connection"
//-----------------------------------------------------------------------------
static void Connection_Free(udt_Connection*);
static PyObject *Connection_New(PyTypeObject*, PyObject*, PyObject*);
static int Connection_Init(udt_Connection*, PyObject*, PyObject*);
static PyObject *Connection_Repr(udt_Connection*);
static PyObject *Connection_Close(udt_Connection*, PyObject*);
static PyObject *Connection_Commit(udt_Connection*, PyObject*);
static PyObject *Connection_Begin(udt_Connection*, PyObject*);
static PyObject *Connection_Prepare(udt_Connection*, PyObject*);
static PyObject *Connection_Rollback(udt_Connection*, PyObject*);
static PyObject *Connection_NewCursor(udt_Connection*, PyObject*, PyObject*);
static PyObject *Connection_Cancel(udt_Connection*, PyObject*);
static PyObject *Connection_GetVersion(udt_Connection*, void*);
static PyObject *Connection_GetEncoding(udt_Connection*, void*);
static PyObject *Connection_GetNationalEncoding(udt_Connection*, void*);
static PyObject *Connection_GetMaxBytesPerCharacter(udt_Connection*, void*);
static PyObject *Connection_ContextManagerEnter(udt_Connection*, PyObject*);
static PyObject *Connection_ContextManagerExit(udt_Connection*, PyObject*);
static PyObject *Connection_ChangePassword(udt_Connection*, PyObject*);
static PyObject *Connection_GetType(udt_Connection*, PyObject*);
static PyObject *Connection_GetStmtCacheSize(udt_Connection*, void*);
static PyObject *Connection_NewEnqueueOptions(udt_Connection*, PyObject*);
static PyObject *Connection_NewDequeueOptions(udt_Connection*, PyObject*);
static PyObject *Connection_NewMessageProperties(udt_Connection*, PyObject*);
static PyObject *Connection_Dequeue(udt_Connection*, PyObject*, PyObject*);
static PyObject *Connection_Enqueue(udt_Connection*, PyObject*, PyObject*);
static PyObject *Connection_Ping(udt_Connection*, PyObject*);
static PyObject *Connection_Shutdown(udt_Connection*, PyObject*, PyObject*);
static PyObject *Connection_Startup(udt_Connection*, PyObject*, PyObject*);
static PyObject *Connection_Subscribe(udt_Connection*, PyObject*, PyObject*);
static PyObject *Connection_GetLTXID(udt_Connection*, void*);
static PyObject *Connection_GetHandle(udt_Connection*, void*);
static PyObject *Connection_GetCurrentSchema(udt_Connection*, void*);
static PyObject *Connection_GetEdition(udt_Connection*, void*);
static PyObject *Connection_GetExternalName(udt_Connection*, void*);
static PyObject *Connection_GetInternalName(udt_Connection*, void*);
static int Connection_SetStmtCacheSize(udt_Connection*, PyObject*, void*);
static int Connection_SetAction(udt_Connection*, PyObject*, void*);
static int Connection_SetClientIdentifier(udt_Connection*, PyObject*, void*);
static int Connection_SetClientInfo(udt_Connection*, PyObject*, void*);
static int Connection_SetCurrentSchema(udt_Connection*, PyObject*, void*);
static int Connection_SetDbOp(udt_Connection*, PyObject*, void*);
static int Connection_SetExternalName(udt_Connection*, PyObject*, void*);
static int Connection_SetInternalName(udt_Connection*, PyObject*, void*);
static int Connection_SetModule(udt_Connection*, PyObject*, void*);
//-----------------------------------------------------------------------------
// declaration of methods for Python type "Connection"
//-----------------------------------------------------------------------------
static PyMethodDef g_ConnectionMethods[] = {
{ "cursor", (PyCFunction) Connection_NewCursor,
METH_VARARGS | METH_KEYWORDS },
{ "commit", (PyCFunction) Connection_Commit, METH_NOARGS },
{ "rollback", (PyCFunction) Connection_Rollback, METH_NOARGS },
{ "begin", (PyCFunction) Connection_Begin, METH_VARARGS },
{ "prepare", (PyCFunction) Connection_Prepare, METH_NOARGS },
{ "close", (PyCFunction) Connection_Close, METH_NOARGS },
{ "cancel", (PyCFunction) Connection_Cancel, METH_NOARGS },
{ "__enter__", (PyCFunction) Connection_ContextManagerEnter, METH_NOARGS },
{ "__exit__", (PyCFunction) Connection_ContextManagerExit, METH_VARARGS },
{ "ping", (PyCFunction) Connection_Ping, METH_NOARGS },
{ "shutdown", (PyCFunction) Connection_Shutdown,
METH_VARARGS | METH_KEYWORDS},
{ "startup", (PyCFunction) Connection_Startup,
METH_VARARGS | METH_KEYWORDS},
{ "subscribe", (PyCFunction) Connection_Subscribe,
METH_VARARGS | METH_KEYWORDS},
{ "changepassword", (PyCFunction) Connection_ChangePassword,
METH_VARARGS },
{ "gettype", (PyCFunction) Connection_GetType, METH_VARARGS },
{ "deqoptions", (PyCFunction) Connection_NewDequeueOptions, METH_NOARGS },
{ "enqoptions", (PyCFunction) Connection_NewEnqueueOptions, METH_NOARGS },
{ "msgproperties", (PyCFunction) Connection_NewMessageProperties,
METH_NOARGS },
{ "deq", (PyCFunction) Connection_Dequeue, METH_VARARGS | METH_KEYWORDS },
{ "enq", (PyCFunction) Connection_Enqueue, METH_VARARGS | METH_KEYWORDS },
{ NULL }
};
//-----------------------------------------------------------------------------
// declaration of members for Python type "Connection"
//-----------------------------------------------------------------------------
static PyMemberDef g_ConnectionMembers[] = {
{ "username", T_OBJECT, offsetof(udt_Connection, username), READONLY },
{ "dsn", T_OBJECT, offsetof(udt_Connection, dsn), READONLY },
{ "tnsentry", T_OBJECT, offsetof(udt_Connection, dsn), READONLY },
{ "autocommit", T_INT, offsetof(udt_Connection, autocommit), 0 },
{ "inputtypehandler", T_OBJECT,
offsetof(udt_Connection, inputTypeHandler), 0 },
{ "outputtypehandler", T_OBJECT,
offsetof(udt_Connection, outputTypeHandler), 0 },
{ NULL }
};
//-----------------------------------------------------------------------------
// declaration of calculated members for Python type "Connection"
//-----------------------------------------------------------------------------
static PyGetSetDef g_ConnectionCalcMembers[] = {
{ "version", (getter) Connection_GetVersion, 0, 0, 0 },
{ "encoding", (getter) Connection_GetEncoding, 0, 0, 0 },
{ "nencoding", (getter) Connection_GetNationalEncoding, 0, 0, 0 },
{ "maxBytesPerCharacter", (getter) Connection_GetMaxBytesPerCharacter,
0, 0, 0 },
{ "stmtcachesize", (getter) Connection_GetStmtCacheSize,
(setter) Connection_SetStmtCacheSize, 0, 0 },
{ "module", 0, (setter) Connection_SetModule, 0, 0 },
{ "action", 0, (setter) Connection_SetAction, 0, 0 },
{ "clientinfo", 0, (setter) Connection_SetClientInfo, 0, 0 },
{ "client_identifier", 0, (setter) Connection_SetClientIdentifier, 0, 0 },
{ "current_schema", (getter) Connection_GetCurrentSchema,
(setter) Connection_SetCurrentSchema, 0, 0 },
{ "external_name", (getter) Connection_GetExternalName,
(setter) Connection_SetExternalName, 0, 0 },
{ "internal_name", (getter) Connection_GetInternalName,
(setter) Connection_SetInternalName, 0, 0 },
{ "dbop", 0, (setter) Connection_SetDbOp, 0, 0 },
{ "edition", (getter) Connection_GetEdition, 0, 0, 0 },
{ "ltxid", (getter) Connection_GetLTXID, 0, 0, 0 },
{ "handle", (getter) Connection_GetHandle, 0, 0, 0 },
{ NULL }
};
//-----------------------------------------------------------------------------
// declaration of Python type "Connection"
//-----------------------------------------------------------------------------
static PyTypeObject g_ConnectionType = {
PyVarObject_HEAD_INIT(NULL, 0)
"cx_Oracle.Connection", // tp_name
sizeof(udt_Connection), // tp_basicsize
0, // tp_itemsize
(destructor) Connection_Free, // tp_dealloc
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_compare
(reprfunc) Connection_Repr, // 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
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
// tp_flags
0, // tp_doc
0, // tp_traverse
0, // tp_clear
0, // tp_richcompare
0, // tp_weaklistoffset
0, // tp_iter
0, // tp_iternext
g_ConnectionMethods, // tp_methods
g_ConnectionMembers, // tp_members
g_ConnectionCalcMembers, // tp_getset
0, // tp_base
0, // tp_dict
0, // tp_descr_get
0, // tp_descr_set
0, // tp_dictoffset
(initproc) Connection_Init, // tp_init
0, // tp_alloc
(newfunc) Connection_New, // tp_new
0, // tp_free
0, // tp_is_gc
0 // tp_bases
};
//-----------------------------------------------------------------------------
// structure used to help in establishing a connection
//-----------------------------------------------------------------------------
typedef struct {
udt_Buffer userNameBuffer;
udt_Buffer passwordBuffer;
udt_Buffer newPasswordBuffer;
udt_Buffer dsnBuffer;
udt_Buffer connectionClassBuffer;
udt_Buffer editionBuffer;
udt_Buffer tagBuffer;
uint32_t numAppContext;
dpiAppContext *appContext;
udt_Buffer *ctxNamespaceBuffers;
udt_Buffer *ctxNameBuffers;
udt_Buffer *ctxValueBuffers;
} udt_ConnectionParams;
//-----------------------------------------------------------------------------
// ConnectionParams_Initialize()
// Initialize the parameters to default values.
//-----------------------------------------------------------------------------
static void ConnectionParams_Initialize(udt_ConnectionParams *params)
{
cxBuffer_Init(¶ms->userNameBuffer);
cxBuffer_Init(¶ms->passwordBuffer);
cxBuffer_Init(¶ms->newPasswordBuffer);
cxBuffer_Init(¶ms->dsnBuffer);
cxBuffer_Init(¶ms->connectionClassBuffer);
cxBuffer_Init(¶ms->editionBuffer);
cxBuffer_Init(¶ms->tagBuffer);
params->numAppContext = 0;
params->appContext = NULL;
params->ctxNamespaceBuffers = NULL;
params->ctxNameBuffers = NULL;
params->ctxValueBuffers = NULL;
}
//-----------------------------------------------------------------------------
// ConnectionParams_ProcessContext()
// Process context for the connection parameters. This validates that the
// context passed in is a list of 3-tuples (namespace, name, value) and
// populates the parametrs with buffers for each of these.
//-----------------------------------------------------------------------------
static int ConnectionParams_ProcessContext(udt_ConnectionParams *params,
PyObject *context, const char *encoding)
{
uint32_t numEntries, i;
dpiAppContext *entry;
PyObject *entryObj;
size_t memorySize;
// validate context is a list with at least one entry in it
if (!context)
return 0;
if (!PyList_Check(context)) {
PyErr_SetString(PyExc_TypeError,
"appcontext should be a list of 3-tuples");
return -1;
}
numEntries = (uint32_t) PyList_GET_SIZE(context);
if (numEntries == 0)
return 0;
// allocate memory for the buffers used to communicate with DPI
params->appContext = PyMem_Malloc(numEntries * sizeof(dpiAppContext));
memorySize = numEntries * sizeof(udt_Buffer);
params->ctxNamespaceBuffers = PyMem_Malloc(memorySize);
params->ctxNameBuffers = PyMem_Malloc(memorySize);
params->ctxValueBuffers = PyMem_Malloc(memorySize);
if (!params->appContext || !params->ctxNamespaceBuffers ||
!params->ctxNameBuffers || !params->ctxValueBuffers) {
PyErr_NoMemory();
return -1;
}
// initialize buffers
for (i = 0; i < numEntries; i++) {
cxBuffer_Init(¶ms->ctxNamespaceBuffers[i]);
cxBuffer_Init(¶ms->ctxNameBuffers[i]);
cxBuffer_Init(¶ms->ctxValueBuffers[i]);
}
params->numAppContext = numEntries;
// process each entry
for (i = 0; i < numEntries; i++) {
entryObj = PyList_GET_ITEM(context, i);
if (!PyTuple_Check(entryObj) || PyTuple_GET_SIZE(entryObj) != 3) {
PyErr_SetString(PyExc_TypeError,
"appcontext should be a list of 3-tuples");
return -1;
}
if (cxBuffer_FromObject(¶ms->ctxNamespaceBuffers[i],
PyTuple_GET_ITEM(entryObj, 0), encoding) < 0)
return -1;
if (cxBuffer_FromObject(¶ms->ctxNameBuffers[i],
PyTuple_GET_ITEM(entryObj, 1), encoding) < 0)
return -1;
if (cxBuffer_FromObject(¶ms->ctxValueBuffers[i],
PyTuple_GET_ITEM(entryObj, 2), encoding) < 0)
return -1;
entry = ¶ms->appContext[i];
entry->namespaceName = params->ctxNamespaceBuffers[i].ptr;
entry->namespaceNameLength = params->ctxNamespaceBuffers[i].size;
entry->name = params->ctxNameBuffers[i].ptr;
entry->nameLength = params->ctxNameBuffers[i].size;
entry->value = params->ctxValueBuffers[i].ptr;
entry->valueLength = params->ctxValueBuffers[i].size;
}
return 0;
}
//-----------------------------------------------------------------------------
// ConnectionParams_Finalize()
// Finalize the parameters, freeing any resources that were allocated. The
// return value is a convenience to the caller.
//-----------------------------------------------------------------------------
static int ConnectionParams_Finalize(udt_ConnectionParams *params)
{
uint32_t i;
cxBuffer_Clear(¶ms->userNameBuffer);
cxBuffer_Clear(¶ms->passwordBuffer);
cxBuffer_Clear(¶ms->newPasswordBuffer);
cxBuffer_Clear(¶ms->dsnBuffer);
cxBuffer_Clear(¶ms->connectionClassBuffer);
cxBuffer_Clear(¶ms->editionBuffer);
cxBuffer_Clear(¶ms->tagBuffer);
for (i = 0; i < params->numAppContext; i++) {
cxBuffer_Clear(¶ms->ctxNamespaceBuffers[i]);
cxBuffer_Clear(¶ms->ctxNameBuffers[i]);
cxBuffer_Clear(¶ms->ctxValueBuffers[i]);
}
params->numAppContext = 0;
if (params->appContext) {
PyMem_Free(params->appContext);
params->appContext = NULL;
}
if (params->ctxNamespaceBuffers) {
PyMem_Free(params->ctxNamespaceBuffers);
params->ctxNamespaceBuffers = NULL;
}
if (params->ctxNameBuffers) {
PyMem_Free(params->ctxNameBuffers);
params->ctxNameBuffers = NULL;
}
if (params->ctxValueBuffers) {
PyMem_Free(params->ctxValueBuffers);
params->ctxValueBuffers = NULL;
}
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(udt_Connection *self)
{
if (!self->handle) {
PyErr_SetString(g_InterfaceErrorException, "not connected");
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
// Connection_GetAttrText()
// Get the value of the attribute returned from the given function. The value
// is assumed to be a text value.
//-----------------------------------------------------------------------------
static PyObject *Connection_GetAttrText(udt_Connection *self,
int (*func)(dpiConn *conn, const char **value, uint32_t *valueLength))
{
uint32_t valueLength;
const char *value;
if (Connection_IsConnected(self) < 0)
return NULL;
if ((*func)(self->handle, &value, &valueLength) < 0)
return Error_RaiseAndReturnNull();
if (!value)
Py_RETURN_NONE;
return cxString_FromEncodedString(value, valueLength,
self->encodingInfo.encoding);
}
//-----------------------------------------------------------------------------
// Connection_SetAttrText()
// Set the value of the attribute using the given function. The value is
// assumed to be a text value.
//-----------------------------------------------------------------------------
static int Connection_SetAttrText(udt_Connection *self, PyObject *value,
int (*func)(dpiConn *conn, const char *value, uint32_t valueLength))
{
udt_Buffer buffer;
int status;
if (Connection_IsConnected(self) < 0)
return -1;
if (cxBuffer_FromObject(&buffer, value, self->encodingInfo.encoding))
return -1;
status = (*func)(self->handle, buffer.ptr, buffer.size);
cxBuffer_Clear(&buffer);
if (status < 0)
return Error_RaiseAndReturnInt();
return 0;
}
//-----------------------------------------------------------------------------
// Connection_ChangePassword()
// Change the password for the given connection.
//-----------------------------------------------------------------------------
static PyObject *Connection_ChangePassword(udt_Connection *self,
PyObject *args)
{
udt_Buffer usernameBuffer, oldPasswordBuffer, newPasswordBuffer;
PyObject *oldPasswordObj, *newPasswordObj;
int status;
// parse the arguments
if (!PyArg_ParseTuple(args, "O!O!", cxString_Type, &oldPasswordObj,
cxString_Type, &newPasswordObj))
return NULL;
// populate buffers
cxBuffer_Init(&usernameBuffer);
cxBuffer_Init(&oldPasswordBuffer);
cxBuffer_Init(&newPasswordBuffer);
if (cxBuffer_FromObject(&usernameBuffer, self->username,
self->encodingInfo.encoding) < 0 ||
cxBuffer_FromObject(&oldPasswordBuffer, oldPasswordObj,
self->encodingInfo.encoding) < 0 ||
cxBuffer_FromObject(&newPasswordBuffer, newPasswordObj,
self->encodingInfo.encoding) < 0) {
cxBuffer_Clear(&usernameBuffer);
cxBuffer_Clear(&oldPasswordBuffer);
cxBuffer_Clear(&newPasswordBuffer);
return NULL;
}
// change the password
Py_BEGIN_ALLOW_THREADS
status = dpiConn_changePassword(self->handle, usernameBuffer.ptr,
usernameBuffer.size, oldPasswordBuffer.ptr, oldPasswordBuffer.size,
newPasswordBuffer.ptr, newPasswordBuffer.size);
Py_END_ALLOW_THREADS
cxBuffer_Clear(&usernameBuffer);
cxBuffer_Clear(&oldPasswordBuffer);
cxBuffer_Clear(&newPasswordBuffer);
if (status < 0)
return Error_RaiseAndReturnNull();
Py_RETURN_NONE;
}
#include "Cursor.c"
#include "Subscription.c"
#include "DeqOptions.c"
#include "EnqOptions.c"
#include "MsgProps.c"
//-----------------------------------------------------------------------------
// Connection_New()
// Create a new connection object and return it.
//-----------------------------------------------------------------------------
static PyObject* Connection_New(PyTypeObject *type, PyObject *args,
PyObject *keywordArgs)
{
udt_Connection *self;
// create the object
self = (udt_Connection*) type->tp_alloc(type, 0);
if (!self)
return NULL;
return (PyObject*) self;
}
//-----------------------------------------------------------------------------
// 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,
PyObject **targetObj, const char *splitString)
{
PyObject *temp, *posObj;
Py_ssize_t size, pos;
if (!*sourceObj || *targetObj)
return 0;
posObj = PyObject_CallMethod(*sourceObj, "find", "s", splitString);
if (!posObj)
return -1;
pos = PyInt_AsLong(posObj);
Py_DECREF(posObj);
if (PyErr_Occurred())
return -1;
if (pos >= 0) {
size = PySequence_Size(*sourceObj);
if (PyErr_Occurred())
return -1;
*targetObj = PySequence_GetSlice(*sourceObj, pos + 1, size);
if (!*targetObj)
return -1;
temp = PySequence_GetSlice(*sourceObj, 0, pos);
if (!temp)
return -1;
*sourceObj = temp;
}
return 0;
}
//-----------------------------------------------------------------------------
// Connection_Init()
// Initialize the connection members.
//-----------------------------------------------------------------------------
static int Connection_Init(udt_Connection *self, PyObject *args,
PyObject *keywordArgs)
{
PyObject *tagObj, *matchAnyTagObj, *threadedObj, *eventsObj, *contextObj;
PyObject *usernameObj, *passwordObj, *dsnObj, *cclassObj, *editionObj;
dpiCommonCreateParams dpiCommonParams;
dpiConnCreateParams dpiCreateParams;
udt_ConnectionParams params;
PyObject *newPasswordObj;
udt_SessionPool *pool;
const char *encoding;
int status, temp;
// define keyword arguments
static char *keywordList[] = { "user", "password", "dsn", "mode",
"handle", "pool", "threaded", "events", "cclass", "purity",
"newpassword", "encoding", "nencoding", "edition", "appcontext",
"tag", "matchanytag", NULL };
// parse arguments
pool = NULL;
threadedObj = eventsObj = newPasswordObj = usernameObj = NULL;
passwordObj = dsnObj = cclassObj = editionObj = tagObj = NULL;
matchAnyTagObj = contextObj = NULL;
if (dpiContext_initCommonCreateParams(g_DpiContext, &dpiCommonParams) < 0)
return Error_RaiseAndReturnInt();
dpiCommonParams.driverName = DRIVER_NAME;
dpiCommonParams.driverNameLength =
(uint32_t) strlen(dpiCommonParams.driverName);
if (dpiContext_initConnCreateParams(g_DpiContext, &dpiCreateParams) < 0)
return Error_RaiseAndReturnInt();
if (!PyArg_ParseTupleAndKeywords(args, keywordArgs,
"|OOOikO!OOOiOssOOOO", keywordList, &usernameObj, &passwordObj,
&dsnObj, &dpiCreateParams.authMode,
&dpiCreateParams.externalHandle, &g_SessionPoolType, &pool,
&threadedObj, &eventsObj, &cclassObj, &dpiCreateParams.purity,
&newPasswordObj, &dpiCommonParams.encoding,
&dpiCommonParams.nencoding, &editionObj, &contextObj, &tagObj,
&matchAnyTagObj))
return -1;
if (GetBooleanValue(threadedObj, 0, &temp) < 0)
return -1;
if (temp)
dpiCommonParams.createMode |= DPI_MODE_CREATE_THREADED;
if (GetBooleanValue(eventsObj, 0, &temp) < 0)
return -1;
if (temp)
dpiCommonParams.createMode |= DPI_MODE_CREATE_EVENTS;
if (GetBooleanValue(matchAnyTagObj, 0, &dpiCreateParams.matchAnyTag) < 0)
return -1;
// keep a copy of the user name and connect string (DSN)
Py_XINCREF(usernameObj);
self->username = usernameObj;
Py_XINCREF(dsnObj);
self->dsn = dsnObj;
// perform some parsing, if necessary
if (Connection_SplitComponent(&self->username, &passwordObj, "/") < 0)
return -1;
if (Connection_SplitComponent(&passwordObj, &self->dsn, "@") < 0)
return -1;
// setup parameters
if (pool) {
dpiCreateParams.pool = pool->handle;
encoding = pool->encodingInfo.encoding;
} else encoding = GetAdjustedEncoding(dpiCommonParams.encoding);
ConnectionParams_Initialize(¶ms);
if (ConnectionParams_ProcessContext(¶ms, contextObj, encoding) < 0)
return ConnectionParams_Finalize(¶ms);
if (cxBuffer_FromObject(¶ms.userNameBuffer, self->username,
encoding) < 0 ||
cxBuffer_FromObject(¶ms.passwordBuffer, passwordObj,
encoding) < 0 ||
cxBuffer_FromObject(¶ms.dsnBuffer, self->dsn, encoding) < 0 ||
cxBuffer_FromObject(¶ms.connectionClassBuffer, cclassObj,
encoding) < 0 ||
cxBuffer_FromObject(¶ms.newPasswordBuffer, newPasswordObj,
encoding) < 0 ||
cxBuffer_FromObject(¶ms.editionBuffer, editionObj,
encoding) < 0 ||
cxBuffer_FromObject(¶ms.tagBuffer, tagObj, encoding) < 0)
return ConnectionParams_Finalize(¶ms);
if (params.userNameBuffer.size == 0 && params.passwordBuffer.size == 0)
dpiCreateParams.externalAuth = 1;
dpiCreateParams.connectionClass = params.connectionClassBuffer.ptr;
dpiCreateParams.connectionClassLength = params.connectionClassBuffer.size;
dpiCreateParams.newPassword = params.newPasswordBuffer.ptr;
dpiCreateParams.newPasswordLength = params.newPasswordBuffer.size;
dpiCommonParams.edition = params.editionBuffer.ptr;
dpiCommonParams.editionLength = params.editionBuffer.size;
dpiCreateParams.tag = params.tagBuffer.ptr;
dpiCreateParams.tagLength = params.tagBuffer.size;
dpiCreateParams.appContext = params.appContext;
dpiCreateParams.numAppContext = params.numAppContext;
if (pool && !pool->homogeneous && pool->username && self->username) {
temp = PyObject_RichCompareBool(self->username, pool->username, Py_EQ);
if (temp < 0)
return ConnectionParams_Finalize(¶ms);
if (temp)
params.userNameBuffer.size = 0;
}
// create connection
Py_BEGIN_ALLOW_THREADS
status = dpiConn_create(g_DpiContext, params.userNameBuffer.ptr,
params.userNameBuffer.size, params.passwordBuffer.ptr,
params.passwordBuffer.size, params.dsnBuffer.ptr,
params.dsnBuffer.size, &dpiCommonParams, &dpiCreateParams,
&self->handle);
Py_END_ALLOW_THREADS
ConnectionParams_Finalize(¶ms);
if (status < 0)
return Error_RaiseAndReturnInt();
// determine encodings to use
if (pool)
self->encodingInfo = pool->encodingInfo;
else {
if (dpiConn_getEncodingInfo(self->handle, &self->encodingInfo) < 0)
return Error_RaiseAndReturnInt();
self->encodingInfo.encoding =
GetAdjustedEncoding(self->encodingInfo.encoding);
self->encodingInfo.nencoding =
GetAdjustedEncoding(self->encodingInfo.nencoding);
}
return 0;
}
//-----------------------------------------------------------------------------
// Connection_Free()
// Deallocate the connection, disconnecting from the database if necessary.
//-----------------------------------------------------------------------------
static void Connection_Free(udt_Connection *self)
{
if (self->handle) {
Py_BEGIN_ALLOW_THREADS
dpiConn_release(self->handle);
Py_END_ALLOW_THREADS
self->handle = NULL;
}
Py_CLEAR(self->sessionPool);
Py_CLEAR(self->username);
Py_CLEAR(self->dsn);
Py_CLEAR(self->version);
Py_CLEAR(self->inputTypeHandler);
Py_CLEAR(self->outputTypeHandler);
Py_TYPE(self)->tp_free((PyObject*) self);
}
//-----------------------------------------------------------------------------
// Connection_Repr()
// Return a string representation of the connection.
//-----------------------------------------------------------------------------
static PyObject *Connection_Repr(udt_Connection *connection)
{
PyObject *module, *name, *result, *format, *formatArgs = NULL;
if (GetModuleAndName(Py_TYPE(connection), &module, &name) < 0)
return NULL;
if (connection->username && connection->username != Py_None &&
connection->dsn && connection->dsn != Py_None) {
format = cxString_FromAscii("<%s.%s to %s@%s>");
if (format)
formatArgs = PyTuple_Pack(4, module, name, connection->username,
connection->dsn);
} else if (connection->username && connection->username != Py_None) {
format = cxString_FromAscii("<%s.%s to user %s@local>");
if (format)
formatArgs = PyTuple_Pack(3, module, name, connection->username);
} else {
format = cxString_FromAscii("<%s.%s to externally identified user>");
if (format)
formatArgs = PyTuple_Pack(2, module, name);
}
Py_DECREF(module);
Py_DECREF(name);
if (!format)
return NULL;
if (!formatArgs) {
Py_DECREF(format);
return NULL;
}
result = cxString_Format(format, formatArgs);
Py_DECREF(format);
Py_DECREF(formatArgs);
return result;
}
//-----------------------------------------------------------------------------
// Connection_GetStmtCacheSize()
// Return the Oracle statement cache size.
//-----------------------------------------------------------------------------
static PyObject *Connection_GetStmtCacheSize(udt_Connection* self, void* arg)
{
uint32_t cacheSize;
if (Connection_IsConnected(self) < 0)
return NULL;
if (dpiConn_getStmtCacheSize(self->handle, &cacheSize) < 0)
return Error_RaiseAndReturnNull();
return PyInt_FromLong(cacheSize);
}
//-----------------------------------------------------------------------------
// Connection_SetStmtCacheSize()
// Set the Oracle statement cache size.
//-----------------------------------------------------------------------------
static int Connection_SetStmtCacheSize(udt_Connection* self, PyObject *value,
void* arg)
{
uint32_t cacheSize;
if (Connection_IsConnected(self) < 0)
return -1;
if (!PyInt_Check(value)) {
PyErr_SetString(PyExc_TypeError, "value must be an integer");
return -1;
}
cacheSize = (uint32_t) PyInt_AsLong(value);
if (dpiConn_setStmtCacheSize(self->handle, cacheSize) < 0)
return Error_RaiseAndReturnInt();
return 0;
}
//-----------------------------------------------------------------------------
// Connection_GetType()
// Return a type object given its name.
//-----------------------------------------------------------------------------
static PyObject *Connection_GetType(udt_Connection *self, PyObject *args)
{
PyObject *nameObj = NULL;
// parse the arguments
if (!PyArg_ParseTuple(args, "O", &nameObj))
return NULL;
return (PyObject*) ObjectType_NewByName(self, nameObj);
}
//-----------------------------------------------------------------------------
// Connection_GetVersion()
// Retrieve the version of the database and return it. Note that this
// function also places the result in the associated dictionary so it is only
// calculated once.
//-----------------------------------------------------------------------------
static PyObject *Connection_GetVersion(udt_Connection *self, void *unused)
{
int versionNum, releaseNum, updateNum, portReleaseNum, portUpdateNum;
const char *releaseString;
uint32_t releaseStringLength;
char buffer[25];
if (dpiConn_getServerVersion(self->handle, &releaseString,
&releaseStringLength, &versionNum, &releaseNum, &updateNum,
&portReleaseNum, &portUpdateNum) < 0)
return Error_RaiseAndReturnNull();
snprintf(buffer, sizeof(buffer), "%d.%d.%d.%d.%d", versionNum, releaseNum,
updateNum, portReleaseNum, portUpdateNum);
return cxString_FromAscii(buffer);
}
//-----------------------------------------------------------------------------
// Connection_GetEncoding()
// Return the encoding associated with the environment of the connection.
//-----------------------------------------------------------------------------
static PyObject *Connection_GetEncoding(udt_Connection *self, void *unused)
{
return cxString_FromAscii(self->encodingInfo.encoding);
}
//-----------------------------------------------------------------------------
// Connection_GetLTXID()
// Return the logical transaction id used with Transaction Guard.
//-----------------------------------------------------------------------------
static PyObject *Connection_GetLTXID(udt_Connection *self, void *unused)
{
uint32_t ltxidLength;
const char *ltxid;
if (Connection_IsConnected(self) < 0)
return NULL;
if (dpiConn_getLTXID(self->handle, <xid, <xidLength) < 0)
return Error_RaiseAndReturnNull();
return PyBytes_FromStringAndSize(ltxid, ltxidLength);
}
//-----------------------------------------------------------------------------
// Connection_GetHandle()
// Return the OCI handle used by the connection.
//-----------------------------------------------------------------------------
static PyObject *Connection_GetHandle(udt_Connection *self, void *unused)
{
void *handle;
if (Connection_IsConnected(self) < 0)
return NULL;
if (dpiConn_getHandle(self->handle, &handle) < 0)
return Error_RaiseAndReturnNull();
return PyInt_FromLong((long) handle);
}
//-----------------------------------------------------------------------------
// Connection_GetNationalEncoding()
// Return the national encoding associated with the environment of the
// connection.
//-----------------------------------------------------------------------------
static PyObject *Connection_GetNationalEncoding(udt_Connection *self,
void *unused)
{
return cxString_FromAscii(self->encodingInfo.nencoding);
}
//-----------------------------------------------------------------------------
// Connection_GetMaxBytesPerCharacter()
// Return the maximum number of bytes per character.
//-----------------------------------------------------------------------------
static PyObject *Connection_GetMaxBytesPerCharacter(udt_Connection *self,
void *unused)
{
return PyInt_FromLong(self->encodingInfo.maxBytesPerCharacter);
}
//-----------------------------------------------------------------------------
// Connection_Close()
// Close the connection, disconnecting from the database.
//-----------------------------------------------------------------------------
static PyObject *Connection_Close(udt_Connection *self, PyObject *args)
{
int status;
if (Connection_IsConnected(self) < 0)
return NULL;
Py_BEGIN_ALLOW_THREADS
status = dpiConn_close(self->handle, DPI_MODE_CONN_CLOSE_DEFAULT, NULL, 0);
Py_END_ALLOW_THREADS
if (status < 0)
return Error_RaiseAndReturnNull();
Py_RETURN_NONE;
}
//-----------------------------------------------------------------------------
// Connection_Commit()
// Commit the transaction on the connection.
//-----------------------------------------------------------------------------
static PyObject *Connection_Commit(udt_Connection *self, PyObject *args)
{
int status;
if (Connection_IsConnected(self) < 0)
return NULL;
Py_BEGIN_ALLOW_THREADS
status = dpiConn_commit(self->handle);
Py_END_ALLOW_THREADS
if (status < 0)
return Error_RaiseAndReturnNull();
Py_RETURN_NONE;
}
//-----------------------------------------------------------------------------
// Connection_Begin()
// Begin a new transaction on the connection.
//-----------------------------------------------------------------------------
static PyObject *Connection_Begin(udt_Connection *self, PyObject *args)
{
uint32_t transactionIdLength, branchIdLength;
const char *transactionId, *branchId;
int formatId, status;
// parse the arguments
formatId = -1;
transactionId = branchId = NULL;
transactionIdLength = branchIdLength = 0;
if (!PyArg_ParseTuple(args, "|is#s#", &formatId, &transactionId,
&transactionIdLength, &branchId, &branchIdLength))
return NULL;
// make sure we are actually connected
if (Connection_IsConnected(self) < 0)
return NULL;
// begin the distributed transaction
Py_BEGIN_ALLOW_THREADS
status = dpiConn_beginDistribTrans(self->handle, formatId, transactionId,
transactionIdLength, branchId, branchIdLength);
Py_END_ALLOW_THREADS
if (status < 0)
return Error_RaiseAndReturnNull();
Py_RETURN_NONE;
}
//-----------------------------------------------------------------------------
// Connection_Prepare()
// Commit the transaction on the connection.
//-----------------------------------------------------------------------------
static PyObject *Connection_Prepare(udt_Connection *self, PyObject *args)
{
int status, commitNeeded;
// make sure we are actually connected
if (Connection_IsConnected(self) < 0)
return NULL;
// perform the prepare
Py_BEGIN_ALLOW_THREADS
status = dpiConn_prepareDistribTrans(self->handle, &commitNeeded);
Py_END_ALLOW_THREADS
if (status < 0)
return Error_RaiseAndReturnNull();
// return whether a commit is needed in order to allow for avoiding the
// call to commit() which will fail with ORA-24756 (transaction does not
// exist)
return PyBool_FromLong(commitNeeded);
}
//-----------------------------------------------------------------------------
// Connection_Rollback()
// Rollback the transaction on the connection.
//-----------------------------------------------------------------------------
static PyObject *Connection_Rollback(udt_Connection *self, PyObject *args)
{
int status;
if (Connection_IsConnected(self) < 0)
return NULL;
Py_BEGIN_ALLOW_THREADS
status = dpiConn_rollback(self->handle);
Py_END_ALLOW_THREADS
if (status < 0)
return Error_RaiseAndReturnNull();
Py_RETURN_NONE;
}
//-----------------------------------------------------------------------------
// Connection_NewCursor()
// Create a new cursor (statement) referencing the connection.