-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cursor.c
2685 lines (2344 loc) · 91.9 KB
/
Cursor.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
//-----------------------------------------------------------------------------
// Cursor.c
// Definition of the Python type OracleCursor.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// structure for the Python type "Cursor"
//-----------------------------------------------------------------------------
typedef struct {
PyObject_HEAD
OCIStmt *handle;
udt_Connection *connection;
udt_Environment *environment;
PyObject *statement;
PyObject *statementTag;
PyObject *bindVariables;
PyObject *fetchVariables;
PyObject *rowFactory;
PyObject *inputTypeHandler;
PyObject *outputTypeHandler;
ub4 arraySize;
ub4 bindArraySize;
ub4 fetchArraySize;
int numbersAsStrings;
int setInputSizes;
int outputSize;
int outputSizeColumn;
ub8 rowCount;
ub4 bufferRowCount;
ub4 bufferRowIndex;
int statementType;
int hasRowsToFetch;
int isOpen;
int isOwned;
boolean isScrollable;
} udt_Cursor;
//-----------------------------------------------------------------------------
// dependent function defintions
//-----------------------------------------------------------------------------
static void Cursor_Free(udt_Cursor*);
//-----------------------------------------------------------------------------
// functions for the Python type "Cursor"
//-----------------------------------------------------------------------------
static PyObject *Cursor_GetIter(udt_Cursor*);
static PyObject *Cursor_GetNext(udt_Cursor*);
static PyObject *Cursor_Close(udt_Cursor*, PyObject*);
static PyObject *Cursor_CallFunc(udt_Cursor*, PyObject*, PyObject*);
static PyObject *Cursor_CallProc(udt_Cursor*, PyObject*, PyObject*);
static PyObject *Cursor_Execute(udt_Cursor*, PyObject*, PyObject*);
static PyObject *Cursor_ExecuteMany(udt_Cursor*, PyObject*, PyObject*);
static PyObject *Cursor_ExecuteManyPrepared(udt_Cursor*, PyObject*);
static PyObject *Cursor_FetchOne(udt_Cursor*, PyObject*);
static PyObject *Cursor_FetchMany(udt_Cursor*, PyObject*, PyObject*);
static PyObject *Cursor_FetchAll(udt_Cursor*, PyObject*);
static PyObject *Cursor_FetchRaw(udt_Cursor*, PyObject*, PyObject*);
static PyObject *Cursor_Parse(udt_Cursor*, PyObject*);
static PyObject *Cursor_Prepare(udt_Cursor*, PyObject*);
static PyObject *Cursor_Scroll(udt_Cursor*, PyObject*, PyObject*);
static PyObject *Cursor_SetInputSizes(udt_Cursor*, PyObject*, PyObject*);
static PyObject *Cursor_SetOutputSize(udt_Cursor*, PyObject*);
static PyObject *Cursor_Var(udt_Cursor*, PyObject*, PyObject*);
static PyObject *Cursor_ArrayVar(udt_Cursor*, PyObject*);
static PyObject *Cursor_BindNames(udt_Cursor*, PyObject*);
static PyObject *Cursor_GetDescription(udt_Cursor*, void*);
static PyObject *Cursor_New(PyTypeObject*, PyObject*, PyObject*);
static int Cursor_Init(udt_Cursor*, PyObject*, PyObject*);
static PyObject *Cursor_Repr(udt_Cursor*);
static PyObject* Cursor_GetBatchErrors(udt_Cursor*);
#if ORACLE_VERSION_HEX >= ORACLE_VERSION(12,1)
static PyObject *Cursor_GetArrayDMLRowCounts(udt_Cursor*);
static PyObject *Cursor_GetImplicitResults(udt_Cursor*);
#endif
//-----------------------------------------------------------------------------
// declaration of methods for Python type "Cursor"
//-----------------------------------------------------------------------------
static PyMethodDef g_CursorMethods[] = {
{ "execute", (PyCFunction) Cursor_Execute, METH_VARARGS | METH_KEYWORDS },
{ "fetchall", (PyCFunction) Cursor_FetchAll, METH_NOARGS },
{ "fetchone", (PyCFunction) Cursor_FetchOne, METH_NOARGS },
{ "fetchmany", (PyCFunction) Cursor_FetchMany,
METH_VARARGS | METH_KEYWORDS },
{ "fetchraw", (PyCFunction) Cursor_FetchRaw,
METH_VARARGS | METH_KEYWORDS },
{ "prepare", (PyCFunction) Cursor_Prepare, METH_VARARGS },
{ "parse", (PyCFunction) Cursor_Parse, METH_VARARGS },
{ "setinputsizes", (PyCFunction) Cursor_SetInputSizes,
METH_VARARGS | METH_KEYWORDS },
{ "executemany", (PyCFunction) Cursor_ExecuteMany,
METH_VARARGS | METH_KEYWORDS },
{ "callproc", (PyCFunction) Cursor_CallProc,
METH_VARARGS | METH_KEYWORDS },
{ "callfunc", (PyCFunction) Cursor_CallFunc,
METH_VARARGS | METH_KEYWORDS },
{ "executemanyprepared", (PyCFunction) Cursor_ExecuteManyPrepared,
METH_VARARGS },
{ "setoutputsize", (PyCFunction) Cursor_SetOutputSize, METH_VARARGS },
{ "scroll", (PyCFunction) Cursor_Scroll, METH_VARARGS | METH_KEYWORDS },
{ "var", (PyCFunction) Cursor_Var, METH_VARARGS | METH_KEYWORDS },
{ "arrayvar", (PyCFunction) Cursor_ArrayVar, METH_VARARGS },
{ "bindnames", (PyCFunction) Cursor_BindNames, METH_NOARGS },
{ "close", (PyCFunction) Cursor_Close, METH_NOARGS },
{ "getbatcherrors", (PyCFunction) Cursor_GetBatchErrors, METH_NOARGS },
#if ORACLE_VERSION_HEX >= ORACLE_VERSION(12,1)
{ "getarraydmlrowcounts", (PyCFunction) Cursor_GetArrayDMLRowCounts,
METH_NOARGS },
{ "getimplicitresults", (PyCFunction) Cursor_GetImplicitResults,
METH_NOARGS },
#endif
{ NULL, NULL }
};
//-----------------------------------------------------------------------------
// declaration of members for Python type "Cursor"
//-----------------------------------------------------------------------------
static PyMemberDef g_CursorMembers[] = {
{ "arraysize", T_UINT, offsetof(udt_Cursor, arraySize), 0 },
{ "bindarraysize", T_UINT, offsetof(udt_Cursor, bindArraySize), 0 },
{ "rowcount", T_ULONGLONG, offsetof(udt_Cursor, rowCount), READONLY },
{ "statement", T_OBJECT, offsetof(udt_Cursor, statement), READONLY },
{ "connection", T_OBJECT_EX, offsetof(udt_Cursor, connection), READONLY },
{ "numbersAsStrings", T_INT, offsetof(udt_Cursor, numbersAsStrings), 0 },
{ "rowfactory", T_OBJECT, offsetof(udt_Cursor, rowFactory), 0 },
{ "bindvars", T_OBJECT, offsetof(udt_Cursor, bindVariables), READONLY },
{ "fetchvars", T_OBJECT, offsetof(udt_Cursor, fetchVariables), READONLY },
{ "inputtypehandler", T_OBJECT, offsetof(udt_Cursor, inputTypeHandler),
0 },
{ "outputtypehandler", T_OBJECT, offsetof(udt_Cursor, outputTypeHandler),
0 },
{ "scrollable", T_BOOL, offsetof(udt_Cursor, isScrollable), 0 },
{ NULL }
};
//-----------------------------------------------------------------------------
// declaration of calculated members for Python type "Connection"
//-----------------------------------------------------------------------------
static PyGetSetDef g_CursorCalcMembers[] = {
{ "description", (getter) Cursor_GetDescription, 0, 0, 0 },
{ NULL }
};
//-----------------------------------------------------------------------------
// declaration of Python type "Cursor"
//-----------------------------------------------------------------------------
static PyTypeObject g_CursorType = {
PyVarObject_HEAD_INIT(NULL, 0)
"cx_Oracle.Cursor", // tp_name
sizeof(udt_Cursor), // tp_basicsize
0, // tp_itemsize
(destructor) Cursor_Free, // tp_dealloc
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_compare
(reprfunc) Cursor_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
(getiterfunc) Cursor_GetIter, // tp_iter
(iternextfunc) Cursor_GetNext, // tp_iternext
g_CursorMethods, // tp_methods
g_CursorMembers, // tp_members
g_CursorCalcMembers, // tp_getset
0, // tp_base
0, // tp_dict
0, // tp_descr_get
0, // tp_descr_set
0, // tp_dictoffset
(initproc) Cursor_Init, // tp_init
0, // tp_alloc
Cursor_New, // tp_new
0, // tp_free
0, // tp_is_gc
0 // tp_bases
};
//-----------------------------------------------------------------------------
// Cursor_AllocateHandle()
// Allocate a new handle.
//-----------------------------------------------------------------------------
static int Cursor_AllocateHandle(
udt_Cursor *self) // cursor object
{
sword status;
self->isOwned = 1;
status = OCIHandleAlloc(self->environment->handle,
(dvoid**) &self->handle, OCI_HTYPE_STMT, 0, 0);
if (Environment_CheckForError(self->environment, status,
"Cursor_New()") < 0)
return -1;
return 0;
}
//-----------------------------------------------------------------------------
// Cursor_FreeHandle()
// Free the handle which may be reallocated if necessary.
//-----------------------------------------------------------------------------
static int Cursor_FreeHandle(
udt_Cursor *self, // cursor object
int raiseException) // raise an exception, if necesary?
{
udt_Buffer buffer;
sword status;
if (self->handle) {
if (self->isOwned) {
status = OCIHandleFree(self->handle, OCI_HTYPE_STMT);
if (raiseException && Environment_CheckForError(
self->environment, status, "Cursor_FreeHandle()") < 0)
return -1;
} else if (self->connection->handle != 0) {
if (cxBuffer_FromObject(&buffer, self->statementTag,
self->environment->encoding) < 0)
return (raiseException) ? -1 : 0;
status = OCIStmtRelease(self->handle,
self->environment->errorHandle, (text*) buffer.ptr,
buffer.size, OCI_DEFAULT);
cxBuffer_Clear(&buffer);
if (raiseException && Environment_CheckForError(
self->environment, status, "Cursor_FreeHandle()") < 0)
return -1;
}
self->handle = NULL;
}
return 0;
}
#include "Variable.c"
//-----------------------------------------------------------------------------
// Cursor_IsOpen()
// Determines if the cursor object is open and if so, if the connection is
// also open.
//-----------------------------------------------------------------------------
static int Cursor_IsOpen(
udt_Cursor *self) // cursor to check
{
if (!self->isOpen) {
PyErr_SetString(g_InterfaceErrorException, "not open");
return -1;
}
return Connection_IsConnected(self->connection);
}
//-----------------------------------------------------------------------------
// Cursor_New()
// Create a new cursor object.
//-----------------------------------------------------------------------------
static PyObject *Cursor_New(
PyTypeObject *type, // type object
PyObject *args, // arguments
PyObject *keywordArgs) // keyword arguments
{
return type->tp_alloc(type, 0);
}
//-----------------------------------------------------------------------------
// Cursor_Init()
// Create a new cursor object.
//-----------------------------------------------------------------------------
static int Cursor_Init(
udt_Cursor *self, // cursor object
PyObject *args, // arguments
PyObject *keywordArgs) // keyword arguments
{
static char *keywordList[] = { "connection", "scrollable", NULL };
udt_Connection *connection;
PyObject *scrollableObj;
// parse arguments
scrollableObj = NULL;
if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "O!|O", keywordList,
&g_ConnectionType, &connection, &scrollableObj))
return -1;
// initialize members
Py_INCREF(connection);
self->connection = connection;
self->environment = connection->environment;
self->arraySize = 50;
self->fetchArraySize = 50;
self->bindArraySize = 1;
self->statementType = -1;
self->outputSize = -1;
self->outputSizeColumn = -1;
self->isOpen = 1;
if (scrollableObj) {
self->isScrollable = PyObject_IsTrue(scrollableObj);
if (self->isScrollable < 0)
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
// Cursor_Repr()
// Return a string representation of the cursor.
//-----------------------------------------------------------------------------
static PyObject *Cursor_Repr(
udt_Cursor *cursor) // cursor to return the string for
{
PyObject *connectionRepr, *module, *name, *result, *format, *formatArgs;
format = cxString_FromAscii("<%s.%s on %s>");
if (!format)
return NULL;
connectionRepr = PyObject_Repr((PyObject*) cursor->connection);
if (!connectionRepr) {
Py_DECREF(format);
return NULL;
}
if (GetModuleAndName(Py_TYPE(cursor), &module, &name) < 0) {
Py_DECREF(format);
Py_DECREF(connectionRepr);
return NULL;
}
formatArgs = PyTuple_Pack(3, module, name, connectionRepr);
Py_DECREF(module);
Py_DECREF(name);
Py_DECREF(connectionRepr);
if (!formatArgs) {
Py_DECREF(format);
return NULL;
}
result = cxString_Format(format, formatArgs);
Py_DECREF(format);
Py_DECREF(formatArgs);
return result;
}
//-----------------------------------------------------------------------------
// Cursor_Free()
// Deallocate the cursor.
//-----------------------------------------------------------------------------
static void Cursor_Free(
udt_Cursor *self) // cursor object
{
Cursor_FreeHandle(self, 0);
Py_CLEAR(self->statement);
Py_CLEAR(self->statementTag);
Py_CLEAR(self->bindVariables);
Py_CLEAR(self->fetchVariables);
Py_CLEAR(self->connection);
Py_CLEAR(self->rowFactory);
Py_CLEAR(self->inputTypeHandler);
Py_CLEAR(self->outputTypeHandler);
Py_TYPE(self)->tp_free((PyObject*) self);
}
//-----------------------------------------------------------------------------
// Cursor_GetBindNames()
// Return a list of bind variable names. At this point the cursor must have
// already been prepared.
//-----------------------------------------------------------------------------
static int Cursor_GetBindNames(
udt_Cursor *self, // cursor to get information from
int numElements, // number of elements (IN/OUT)
PyObject **names) // list of names (OUT)
{
ub1 *bindNameLengths, *indicatorNameLengths, *duplicate;
char *buffer, **bindNames, **indicatorNames;
OCIBind **bindHandles;
int elementSize, i;
sb4 foundElements;
PyObject *temp;
sword status;
// ensure that a statement has already been prepared
if (!self->statement) {
PyErr_SetString(g_ProgrammingErrorException,
"statement must be prepared first");
return -1;
}
// avoid bus errors on 64-bit platforms
numElements = numElements + (sizeof(void*) - numElements % sizeof(void*));
// initialize the buffers
elementSize = sizeof(char*) + sizeof(ub1) + sizeof(char*) + sizeof(ub1) +
sizeof(ub1) + sizeof(OCIBind*);
buffer = (char*) PyMem_Malloc(numElements * elementSize);
if (!buffer) {
PyErr_NoMemory();
return -1;
}
bindNames = (char**) buffer;
bindNameLengths = (ub1*) (((char*) bindNames) +
sizeof(char*) * numElements);
indicatorNames = (char**) (((char*) bindNameLengths) +
sizeof(ub1) * numElements);
indicatorNameLengths = (ub1*) (((char*) indicatorNames) +
sizeof(char*) * numElements);
duplicate = (ub1*) (((char*) indicatorNameLengths) +
sizeof(ub1) * numElements);
bindHandles = (OCIBind**) (((char*) duplicate) +
sizeof(ub1) * numElements);
// get the bind information
status = OCIStmtGetBindInfo(self->handle,
self->environment->errorHandle, numElements, 1, &foundElements,
(text**) bindNames, bindNameLengths, (text**) indicatorNames,
indicatorNameLengths, duplicate, bindHandles);
if (status != OCI_NO_DATA &&
Environment_CheckForError(self->environment, status,
"Cursor_GetBindNames()") < 0) {
PyMem_Free(buffer);
return -1;
}
if (foundElements < 0) {
*names = NULL;
PyMem_Free(buffer);
return abs(foundElements);
}
// create the list which is to be returned
*names = PyList_New(0);
if (!*names) {
PyMem_Free(buffer);
return -1;
}
// process the bind information returned
for (i = 0; i < foundElements; i++) {
if (!duplicate[i]) {
temp = cxString_FromEncodedString(bindNames[i],
bindNameLengths[i],
self->connection->environment->encoding);
if (!temp) {
Py_DECREF(*names);
PyMem_Free(buffer);
return -1;
}
if (PyList_Append(*names, temp) < 0) {
Py_DECREF(*names);
Py_DECREF(temp);
PyMem_Free(buffer);
return -1;
}
Py_DECREF(temp);
}
}
PyMem_Free(buffer);
return 0;
}
//-----------------------------------------------------------------------------
// Cursor_PerformDefine()
// Perform the defines for the cursor. At this point it is assumed that the
// statement being executed is in fact a query.
//-----------------------------------------------------------------------------
static int Cursor_PerformDefine(
udt_Cursor *self) // cursor to perform define on
{
int numParams, pos;
udt_Variable *var;
sword status;
// determine number of items in select-list
status = OCIAttrGet(self->handle, OCI_HTYPE_STMT, (dvoid*) &numParams, 0,
OCI_ATTR_PARAM_COUNT, self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"Cursor_PerformDefine()") < 0)
return -1;
// create a list corresponding to the number of items
self->fetchVariables = PyList_New(numParams);
if (!self->fetchVariables)
return -1;
// define a variable for each select-item
self->fetchArraySize = self->arraySize;
for (pos = 1; pos <= numParams; pos++) {
var = Variable_Define(self, self->fetchArraySize, pos);
if (!var)
return -1;
PyList_SET_ITEM(self->fetchVariables, pos - 1, (PyObject *) var);
}
return 0;
}
//-----------------------------------------------------------------------------
// Cursor_GetRowCount()
// Get the row count from the statement handle. This is a 64-bit value in
// Oracle Database 12.1 and later and a 32-bit value prior to that.
//-----------------------------------------------------------------------------
static int Cursor_GetRowCount(
udt_Cursor *self, // cursor to get the row count on
ub8* value) // the row count (OUT)
{
#if ORACLE_VERSION_HEX >= ORACLE_VERSION(12,1)
ub8 rowCount;
ub4 attribute = OCI_ATTR_UB8_ROW_COUNT;
#else
ub4 rowCount;
ub4 attribute = OCI_ATTR_ROW_COUNT;
#endif
sword status;
status = OCIAttrGet(self->handle, OCI_HTYPE_STMT, &rowCount, 0,
attribute, self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"Cursor_GetRowCount()") < 0)
return -1;
*value = rowCount;
return 0;
}
//-----------------------------------------------------------------------------
// Cursor_SetRowCount()
// Set the rowcount variable.
//-----------------------------------------------------------------------------
static int Cursor_SetRowCount(
udt_Cursor *self) // cursor to set the rowcount on
{
self->rowCount = 0;
self->hasRowsToFetch = 0;
if (self->statementType == OCI_STMT_SELECT) {
self->bufferRowCount = 0;
self->bufferRowIndex = self->fetchArraySize;
self->hasRowsToFetch = 1;
} else if (self->statementType == OCI_STMT_INSERT ||
self->statementType == OCI_STMT_UPDATE ||
self->statementType == OCI_STMT_DELETE ||
self->statementType == OCI_STMT_BEGIN ||
self->statementType == OCI_STMT_DECLARE) {
if (Cursor_GetRowCount(self, &self->rowCount) < 0)
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
// Cursor_SetErrorOffset()
// Set the error offset on the error object, if applicable.
//-----------------------------------------------------------------------------
static void Cursor_SetErrorOffset(
udt_Cursor *self) // cursor to get the error offset from
{
PyObject *type, *value, *traceback, *args;
udt_Error *error;
ub2 offset = 0;
PyErr_Fetch(&type, &value, &traceback);
if (type == g_DatabaseErrorException) {
PyErr_NormalizeException(&type, &value, &traceback);
OCIAttrGet(self->handle, OCI_HTYPE_STMT, &offset, 0,
OCI_ATTR_PARSE_ERROR_OFFSET, self->environment->errorHandle);
args = PyObject_GetAttrString(value, "args");
error = (udt_Error*) PyTuple_GET_ITEM(args, 0);
error->offset = offset;
Py_DECREF(args);
}
PyErr_Restore(type, value, traceback);
}
//-----------------------------------------------------------------------------
// Cursor_InternalExecute()
// Perform the work of executing a cursor and set the rowcount appropriately
// regardless of whether an error takes place.
//-----------------------------------------------------------------------------
static int Cursor_InternalExecute(
udt_Cursor *self, // cursor to perform the execute on
ub4 numIters, // number of iterations to execute
ub4 additionalMode) // additional modes to set
{
sword status;
ub4 mode;
if (self->connection->autocommit)
mode = OCI_COMMIT_ON_SUCCESS;
else mode = OCI_DEFAULT;
mode |= additionalMode;
Py_BEGIN_ALLOW_THREADS
status = OCIStmtExecute(self->connection->handle, self->handle,
self->environment->errorHandle, numIters, 0, 0, 0, mode);
Py_END_ALLOW_THREADS
if (Environment_CheckForError(self->environment, status,
"Cursor_InternalExecute()") < 0) {
Cursor_SetErrorOffset(self);
if (Cursor_SetRowCount(self) < 0)
PyErr_Clear();
return -1;
}
return Cursor_SetRowCount(self);
}
//-----------------------------------------------------------------------------
// Cursor_GetStatementType()
// Determine if the cursor is executing a select statement.
//-----------------------------------------------------------------------------
static int Cursor_GetStatementType(
udt_Cursor *self) // cursor to perform binds on
{
ub2 statementType;
sword status;
status = OCIAttrGet(self->handle, OCI_HTYPE_STMT,
(dvoid*) &statementType, 0, OCI_ATTR_STMT_TYPE,
self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"Cursor_GetStatementType()") < 0)
return -1;
self->statementType = statementType;
if (self->fetchVariables) {
Py_DECREF(self->fetchVariables);
self->fetchVariables = NULL;
}
return 0;
}
//-----------------------------------------------------------------------------
// Cursor_FixupBoundCursor()
// Fixup a cursor so that fetching and returning cursor descriptions are
// successful after binding a cursor to another cursor.
//-----------------------------------------------------------------------------
static int Cursor_FixupBoundCursor(
udt_Cursor *self) // cursor that may have been bound
{
if (self->handle && self->statementType < 0) {
if (Cursor_GetStatementType(self) < 0)
return -1;
if (self->statementType == OCI_STMT_SELECT &&
Cursor_PerformDefine(self) < 0)
return -1;
if (Cursor_SetRowCount(self) < 0)
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
// Cursor_ItemDescriptionHelper()
// Helper for Cursor_ItemDescription() used so that it is not necessary to
// constantly free the descriptor when an error takes place.
//-----------------------------------------------------------------------------
static PyObject *Cursor_ItemDescriptionHelper(
udt_Cursor *self, // cursor object
unsigned pos, // position in description
OCIParam *param) // parameter to use for description
{
ub2 internalSize, charSize;
udt_VariableType *varType;
int displaySize, index;
PyObject *tuple, *type;
ub4 nameLength;
sb2 precision;
sword status;
char *name;
ub1 nullOk;
sb1 scale;
// acquire usable type of item
varType = Variable_TypeByOracleDescriptor(param, self->environment);
if (!varType)
return NULL;
// acquire internal size of item
status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE, (dvoid*) &internalSize, 0,
OCI_ATTR_DATA_SIZE, self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"Cursor_ItemDescription(): internal size") < 0)
return NULL;
// acquire character size of item
status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE, (dvoid*) &charSize, 0,
OCI_ATTR_CHAR_SIZE, self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"Cursor_ItemDescription(): character size") < 0)
return NULL;
// aquire name of item
status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE, (dvoid*) &name,
&nameLength, OCI_ATTR_NAME, self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"Cursor_ItemDescription(): name") < 0)
return NULL;
// lookup precision and scale
scale = 0;
precision = 0;
if (varType->pythonType == &g_NumberVarType) {
status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE, (dvoid*) &scale, 0,
OCI_ATTR_SCALE, self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"Cursor_ItemDescription(): scale") < 0)
return NULL;
status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE, (dvoid*) &precision, 0,
OCI_ATTR_PRECISION, self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"Cursor_ItemDescription(): precision") < 0)
return NULL;
}
// lookup whether null is permitted for the attribute
status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE, (dvoid*) &nullOk, 0,
OCI_ATTR_IS_NULL, self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"Cursor_ItemDescription(): nullable") < 0)
return NULL;
// set display size based on data type
type = (PyObject*) varType->pythonType;
if (type == (PyObject*) &g_StringVarType)
displaySize = charSize;
else if (type == (PyObject*) &g_NCharVarType)
displaySize = charSize;
else if (type == (PyObject*) &g_BinaryVarType)
displaySize = internalSize;
else if (type == (PyObject*) &g_FixedCharVarType)
displaySize = charSize;
else if (type == (PyObject*) &g_FixedNCharVarType)
displaySize = charSize;
else if (type == (PyObject*) &g_NumberVarType) {
if (precision) {
displaySize = precision + 1;
if (scale > 0)
displaySize += scale + 1;
}
else displaySize = 127;
} else if (type == (PyObject*) &g_DateTimeVarType) {
displaySize = 23;
} else {
displaySize = -1;
}
// create the tuple and populate it
tuple = PyTuple_New(7);
if (!tuple)
return NULL;
// set each of the items in the tuple
PyTuple_SET_ITEM(tuple, 0, cxString_FromEncodedString(name, nameLength,
self->connection->environment->encoding));
Py_INCREF(type);
PyTuple_SET_ITEM(tuple, 1, type);
PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(displaySize));
PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(internalSize));
PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong(precision));
PyTuple_SET_ITEM(tuple, 5, PyInt_FromLong(scale));
PyTuple_SET_ITEM(tuple, 6, PyInt_FromLong(nullOk != 0));
// make sure the tuple is ok
for (index = 0; index < 7; index++) {
if (!PyTuple_GET_ITEM(tuple, index)) {
Py_DECREF(tuple);
return NULL;
}
}
return tuple;
}
//-----------------------------------------------------------------------------
// Cursor_ItemDescription()
// Return a tuple describing the item at the given position.
//-----------------------------------------------------------------------------
static PyObject *Cursor_ItemDescription(
udt_Cursor *self, // cursor object
unsigned pos) // position
{
PyObject *tuple;
OCIParam *param;
sword status;
// acquire parameter descriptor
status = OCIParamGet(self->handle, OCI_HTYPE_STMT,
self->environment->errorHandle, (void**) ¶m, pos);
if (Environment_CheckForError(self->environment, status,
"Cursor_ItemDescription(): parameter") < 0)
return NULL;
// use helper routine to get tuple
tuple = Cursor_ItemDescriptionHelper(self, pos, param);
OCIDescriptorFree(param, OCI_DTYPE_PARAM);
return tuple;
}
//-----------------------------------------------------------------------------
// Cursor_GetDescription()
// Return a list of 7-tuples consisting of the description of the define
// variables.
//-----------------------------------------------------------------------------
static PyObject *Cursor_GetDescription(
udt_Cursor *self, // cursor object
void *arg) // optional argument (ignored)
{
PyObject *results, *tuple;
int numItems, index;
sword status;
// make sure the cursor is open
if (Cursor_IsOpen(self) < 0)
return NULL;
// fixup bound cursor, if necessary
if (Cursor_FixupBoundCursor(self) < 0)
return NULL;
// if not a query, return None
if (self->statementType != OCI_STMT_SELECT) {
Py_INCREF(Py_None);
return Py_None;
}
// determine number of items in select-list
status = OCIAttrGet(self->handle, OCI_HTYPE_STMT, (dvoid*) &numItems, 0,
OCI_ATTR_PARAM_COUNT, self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"Cursor_GetDescription()") < 0)
return NULL;
// create a list of the required length
results = PyList_New(numItems);
if (!results)
return NULL;
// create tuples corresponding to the select-items
for (index = 0; index < numItems; index++) {
tuple = Cursor_ItemDescription(self, index + 1);
if (!tuple) {
Py_DECREF(results);
return NULL;
}
PyList_SET_ITEM(results, index, tuple);
}
return results;
}
//-----------------------------------------------------------------------------
// Cursor_Close()
// Close the cursor.
//-----------------------------------------------------------------------------
static PyObject *Cursor_Close(
udt_Cursor *self, // cursor to close
PyObject *args) // arguments
{
// make sure we are actually open
if (Cursor_IsOpen(self) < 0)
return NULL;
// close the cursor
if (Cursor_FreeHandle(self, 1) < 0)
return NULL;
self->isOpen = 0;
Py_INCREF(Py_None);
return Py_None;
}
//-----------------------------------------------------------------------------
// Cursor_SetBindVariableHelper()
// Helper for setting a bind variable.
//-----------------------------------------------------------------------------
static int Cursor_SetBindVariableHelper(
udt_Cursor *self, // cursor to perform bind on
unsigned numElements, // number of elements to create
unsigned arrayPos, // array position to set
PyObject *value, // value to bind
udt_Variable *origVar, // original variable bound
udt_Variable **newVar, // new variable to be bound
int deferTypeAssignment) // defer type assignment if null?
{
int isValueVar;
// initialization
*newVar = NULL;
isValueVar = Variable_Check(value);
// handle case where variable is already bound
if (origVar) {
// if the value is a variable object, rebind it if necessary
if (isValueVar) {
if ( (PyObject*) origVar != value) {
Py_INCREF(value);
*newVar = (udt_Variable*) value;
}
// if the number of elements has changed, create a new variable
// this is only necessary for executemany() since execute() always
// passes a value of 1 for the number of elements
} else if (numElements > origVar->allocatedElements) {
*newVar = Variable_New(self, numElements, origVar->type,
origVar->size);
if (!*newVar)
return -1;
if (Variable_SetValue(*newVar, arrayPos, value) < 0)
return -1;
// otherwise, attempt to set the value
} else if (Variable_SetValue(origVar, arrayPos, value) < 0) {
// executemany() should simply fail after the first element
if (arrayPos > 0)
return -1;
// anything other than index error or type error should fail
if (!PyErr_ExceptionMatches(PyExc_IndexError) &&
!PyErr_ExceptionMatches(PyExc_TypeError))
return -1;
// clear the exception and try to create a new variable
PyErr_Clear();
origVar = NULL;
}
}
// if no original variable used, create a new one
if (!origVar) {
// if the value is a variable object, bind it directly
if (isValueVar) {
Py_INCREF(value);
*newVar = (udt_Variable*) value;
(*newVar)->boundPos = 0;
Py_XDECREF((*newVar)->boundName);
(*newVar)->boundName = NULL;
// otherwise, create a new variable, unless the value is None and
// we wish to defer type assignment
} else if (value != Py_None || !deferTypeAssignment) {
*newVar = Variable_NewByValue(self, value, numElements);
if (!*newVar)
return -1;
if (Variable_SetValue(*newVar, arrayPos, value) < 0)
return -1;
}
}
return 0;
}
//-----------------------------------------------------------------------------
// Cursor_SetBindVariables()
// Create or set bind variables.
//-----------------------------------------------------------------------------
static int Cursor_SetBindVariables(
udt_Cursor *self, // cursor to perform binds on
PyObject *parameters, // parameters to bind
unsigned numElements, // number of elements to create
unsigned arrayPos, // array position to set
int deferTypeAssignment) // defer type assignment if null?
{
int i, origBoundByPos, origNumParams, boundByPos, numParams;
PyObject *key, *value, *origVar;
udt_Variable *newVar;