forked from oracle/python-cx_Oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcxoCursor.c
2274 lines (1969 loc) · 77.6 KB
/
cxoCursor.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
//-----------------------------------------------------------------------------
// Copyright (c) 2016, 2020, 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.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// cxoCursor.c
// Definition of the Python type Cursor.
//-----------------------------------------------------------------------------
#include "cxoModule.h"
//-----------------------------------------------------------------------------
// cxoCursor_new()
// Create a new cursor object.
//-----------------------------------------------------------------------------
static PyObject *cxoCursor_new(PyTypeObject *type, PyObject *args,
PyObject *keywordArgs)
{
return type->tp_alloc(type, 0);
}
//-----------------------------------------------------------------------------
// cxoCursor_init()
// Create a new cursor object.
//-----------------------------------------------------------------------------
static int cxoCursor_init(cxoCursor *cursor, PyObject *args,
PyObject *keywordArgs)
{
static char *keywordList[] = { "connection", "scrollable", NULL };
cxoConnection *connection;
PyObject *scrollableObj;
int isScrollable;
// parse arguments
scrollableObj = NULL;
if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "O!|O", keywordList,
&cxoPyTypeConnection, &connection, &scrollableObj))
return -1;
if (cxoUtils_getBooleanValue(scrollableObj, 0, &isScrollable) < 0)
return -1;
cursor->isScrollable = (char) isScrollable;
// initialize members
Py_INCREF(connection);
cursor->connection = connection;
cursor->arraySize = 100;
cursor->fetchArraySize = 100;
cursor->prefetchRows = DPI_DEFAULT_PREFETCH_ROWS;
cursor->bindArraySize = 1;
cursor->isOpen = 1;
return 0;
}
//-----------------------------------------------------------------------------
// cxoCursor_repr()
// Return a string representation of the cursor.
//-----------------------------------------------------------------------------
static PyObject *cxoCursor_repr(cxoCursor *cursor)
{
PyObject *connectionRepr, *module, *name, *result;
connectionRepr = PyObject_Repr((PyObject*) cursor->connection);
if (!connectionRepr)
return NULL;
if (cxoUtils_getModuleAndName(Py_TYPE(cursor), &module, &name) < 0) {
Py_DECREF(connectionRepr);
return NULL;
}
result = cxoUtils_formatString("<%s.%s on %s>",
PyTuple_Pack(3, module, name, connectionRepr));
Py_DECREF(module);
Py_DECREF(name);
Py_DECREF(connectionRepr);
return result;
}
//-----------------------------------------------------------------------------
// cxoCursor_free()
// Deallocate the cursor.
//-----------------------------------------------------------------------------
static void cxoCursor_free(cxoCursor *cursor)
{
Py_CLEAR(cursor->statement);
Py_CLEAR(cursor->statementTag);
Py_CLEAR(cursor->bindVariables);
Py_CLEAR(cursor->fetchVariables);
if (cursor->handle) {
dpiStmt_release(cursor->handle);
cursor->handle = NULL;
}
Py_CLEAR(cursor->connection);
Py_CLEAR(cursor->rowFactory);
Py_CLEAR(cursor->inputTypeHandler);
Py_CLEAR(cursor->outputTypeHandler);
Py_TYPE(cursor)->tp_free((PyObject*) cursor);
}
//-----------------------------------------------------------------------------
// cxoCursor_isOpen()
// Determines if the cursor object is open. Since the same cursor can be
// used to execute multiple statements, simply checking for the DPI statement
// handle is insufficient.
//-----------------------------------------------------------------------------
static int cxoCursor_isOpen(cxoCursor *cursor)
{
if (!cursor->isOpen) {
cxoError_raiseFromString(cxoInterfaceErrorException, "not open");
return -1;
}
return cxoConnection_isConnected(cursor->connection);
}
//-----------------------------------------------------------------------------
// cxoCursor_fetchRow()
// Fetch a single row from the cursor. Internally the number of rows left in
// the buffer is managed in order to minimize calls to Py_BEGIN_ALLOW_THREADS
// and Py_END_ALLOW_THREADS which have a significant overhead.
//-----------------------------------------------------------------------------
static int cxoCursor_fetchRow(cxoCursor *cursor, int *found,
uint32_t *bufferRowIndex)
{
int status;
// if the number of rows in the fetch buffer is zero and there are more
// rows to fetch, call DPI with threading enabled in order to perform any
// fetch requiring a network round trip
if (cursor->numRowsInFetchBuffer == 0 && cursor->moreRowsToFetch) {
Py_BEGIN_ALLOW_THREADS
status = dpiStmt_fetchRows(cursor->handle, cursor->fetchArraySize,
&cursor->fetchBufferRowIndex, &cursor->numRowsInFetchBuffer,
&cursor->moreRowsToFetch);
Py_END_ALLOW_THREADS
if (status < 0)
return cxoError_raiseAndReturnInt();
}
// keep track of where we are in the fetch buffer
if (cursor->numRowsInFetchBuffer == 0)
*found = 0;
else {
*found = 1;
*bufferRowIndex = cursor->fetchBufferRowIndex++;
cursor->numRowsInFetchBuffer--;
}
return 0;
}
//-----------------------------------------------------------------------------
// cxoCursor_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 cxoCursor_performDefine(cxoCursor *cursor, uint32_t numQueryColumns)
{
PyObject *outputTypeHandler, *result;
cxoTransformNum transformNum;
cxoObjectType *objectType;
dpiQueryInfo queryInfo;
uint32_t pos, size;
cxoDbType *dbType;
char message[120];
cxoVar *var;
// initialize fetching variables; these are used to reduce the number of
// times that Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS is called as
// there is a significant amount of overhead in making these calls
cursor->numRowsInFetchBuffer = 0;
cursor->moreRowsToFetch = 1;
// if fetch variables already exist, nothing more to do (we are executing
// the same statement and therefore all defines have already been
// performed)
if (cursor->fetchVariables)
return 0;
// create a list corresponding to the number of items
cursor->fetchVariables = PyList_New(numQueryColumns);
if (!cursor->fetchVariables)
return -1;
// create a variable for each of the query columns
cursor->fetchArraySize = cursor->arraySize;
for (pos = 1; pos <= numQueryColumns; pos++) {
// get query information for the column position
if (dpiStmt_getQueryInfo(cursor->handle, pos, &queryInfo) < 0)
return cxoError_raiseAndReturnInt();
if (queryInfo.typeInfo.sizeInChars)
size = queryInfo.typeInfo.sizeInChars;
else size = queryInfo.typeInfo.clientSizeInBytes;
// determine object type, if applicable
objectType = NULL;
if (queryInfo.typeInfo.objectType) {
objectType = cxoObjectType_new(cursor->connection,
queryInfo.typeInfo.objectType);
if (!objectType)
return -1;
}
// determine the default types to use
transformNum =
cxoTransform_getNumFromDataTypeInfo(&queryInfo.typeInfo);
if (transformNum == CXO_TRANSFORM_UNSUPPORTED) {
snprintf(message, sizeof(message), "Oracle type %d not supported.",
queryInfo.typeInfo.oracleTypeNum);
cxoError_raiseFromString(cxoNotSupportedErrorException, message);
return -1;
}
dbType = cxoDbType_fromTransformNum(transformNum);
if (!dbType)
return -1;
// see if an output type handler should be used
var = NULL;
outputTypeHandler = NULL;
if (cursor->outputTypeHandler && cursor->outputTypeHandler != Py_None)
outputTypeHandler = cursor->outputTypeHandler;
else if (cursor->connection->outputTypeHandler &&
cursor->connection->outputTypeHandler != Py_None)
outputTypeHandler = cursor->connection->outputTypeHandler;
// if using an output type handler, None implies default behavior
if (outputTypeHandler) {
result = PyObject_CallFunction(outputTypeHandler, "Os#Oiii",
cursor, queryInfo.name, (Py_ssize_t) queryInfo.nameLength,
dbType, size, queryInfo.typeInfo.precision,
queryInfo.typeInfo.scale);
if (!result) {
Py_XDECREF(objectType);
return -1;
} else if (result == Py_None)
Py_DECREF(result);
else if (!cxoVar_check(result)) {
Py_DECREF(result);
Py_XDECREF(objectType);
PyErr_SetString(PyExc_TypeError,
"expecting variable from output type handler");
return -1;
} else {
var = (cxoVar*) result;
if (var->allocatedElements < cursor->fetchArraySize) {
Py_DECREF(result);
Py_XDECREF(objectType);
PyErr_SetString(PyExc_TypeError,
"expecting variable with array size large "
"enough for fetch");
return -1;
}
}
}
// if no variable created yet, use the database metadata
if (!var) {
var = cxoVar_new(cursor, cursor->fetchArraySize, transformNum,
size, 0, objectType);
if (!var) {
Py_XDECREF(objectType);
return -1;
}
}
// add the variable to the fetch variables and perform define
Py_XDECREF(objectType);
PyList_SET_ITEM(cursor->fetchVariables, pos - 1, (PyObject *) var);
if (dpiStmt_define(cursor->handle, pos, var->handle) < 0)
return cxoError_raiseAndReturnInt();
}
return 0;
}
//-----------------------------------------------------------------------------
// cxoCursor_verifyFetch()
// Verify that fetching may happen from this cursor.
//-----------------------------------------------------------------------------
static int cxoCursor_verifyFetch(cxoCursor *cursor)
{
uint32_t numQueryColumns;
// make sure the cursor is open
if (cxoCursor_isOpen(cursor) < 0)
return -1;
// fixup REF cursor, if applicable
if (cursor->fixupRefCursor) {
cursor->fetchArraySize = cursor->arraySize;
if (dpiStmt_setFetchArraySize(cursor->handle,
cursor->fetchArraySize) < 0)
return cxoError_raiseAndReturnInt();
if (dpiStmt_getNumQueryColumns(cursor->handle, &numQueryColumns) < 0)
return cxoError_raiseAndReturnInt();
if (cxoCursor_performDefine(cursor, numQueryColumns) < 0)
return cxoError_raiseAndReturnInt();
cursor->fixupRefCursor = 0;
}
// make sure the cursor is for a query
if (!cursor->fetchVariables) {
cxoError_raiseFromString(cxoInterfaceErrorException, "not a query");
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
// cxoCursor_itemDescription()
// Return a tuple describing the item at the given position.
//-----------------------------------------------------------------------------
static PyObject *cxoCursor_itemDescription(cxoCursor *cursor, uint32_t pos)
{
int displaySize, index;
dpiQueryInfo queryInfo;
PyObject *tuple, *temp;
cxoDbType *dbType;
// get information about the column position
if (dpiStmt_getQueryInfo(cursor->handle, pos, &queryInfo) < 0)
return NULL;
dbType = cxoDbType_fromDataTypeInfo(&queryInfo.typeInfo);
if (!dbType)
return NULL;
// set display size based on data type
switch (queryInfo.typeInfo.oracleTypeNum) {
case DPI_ORACLE_TYPE_VARCHAR:
case DPI_ORACLE_TYPE_NVARCHAR:
case DPI_ORACLE_TYPE_CHAR:
case DPI_ORACLE_TYPE_NCHAR:
case DPI_ORACLE_TYPE_ROWID:
displaySize = (int) queryInfo.typeInfo.sizeInChars;
break;
case DPI_ORACLE_TYPE_RAW:
displaySize = (int) queryInfo.typeInfo.clientSizeInBytes;
break;
case DPI_ORACLE_TYPE_NATIVE_FLOAT:
case DPI_ORACLE_TYPE_NATIVE_DOUBLE:
case DPI_ORACLE_TYPE_NATIVE_INT:
case DPI_ORACLE_TYPE_NUMBER:
if (queryInfo.typeInfo.precision) {
displaySize = queryInfo.typeInfo.precision + 1;
if (queryInfo.typeInfo.scale > 0)
displaySize += queryInfo.typeInfo.scale + 1;
}
else displaySize = 127;
break;
case DPI_ORACLE_TYPE_DATE:
case DPI_ORACLE_TYPE_TIMESTAMP:
displaySize = 23;
break;
default:
displaySize = 0;
}
// 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, PyUnicode_Decode(queryInfo.name,
queryInfo.nameLength, cursor->connection->encodingInfo.encoding,
NULL));
Py_INCREF(dbType);
PyTuple_SET_ITEM(tuple, 1, (PyObject*) dbType);
if (displaySize)
PyTuple_SET_ITEM(tuple, 2, PyLong_FromLong(displaySize));
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(tuple, 2, Py_None);
}
if (queryInfo.typeInfo.clientSizeInBytes)
PyTuple_SET_ITEM(tuple, 3,
PyLong_FromLong(queryInfo.typeInfo.clientSizeInBytes));
else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(tuple, 3, Py_None);
}
if (queryInfo.typeInfo.precision || queryInfo.typeInfo.scale ||
queryInfo.typeInfo.fsPrecision) {
PyTuple_SET_ITEM(tuple, 4,
PyLong_FromLong(queryInfo.typeInfo.precision));
PyTuple_SET_ITEM(tuple, 5,
PyLong_FromLong(queryInfo.typeInfo.scale +
queryInfo.typeInfo.fsPrecision));
} else {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(tuple, 4, Py_None);
Py_INCREF(Py_None);
PyTuple_SET_ITEM(tuple, 5, Py_None);
}
PyTuple_SET_ITEM(tuple, 6, PyLong_FromLong(queryInfo.nullOk != 0));
// make sure the tuple is ok
for (index = 0; index < 7; index++) {
temp = PyTuple_GET_ITEM(tuple, index);
if (!temp) {
Py_DECREF(tuple);
return NULL;
} else if (temp == Py_None)
Py_INCREF(temp);
}
return tuple;
}
//-----------------------------------------------------------------------------
// cxoCursor_getDescription()
// Return a list of 7-tuples consisting of the description of the define
// variables.
//-----------------------------------------------------------------------------
static PyObject *cxoCursor_getDescription(cxoCursor *cursor, void *unused)
{
uint32_t numQueryColumns, i;
PyObject *results, *tuple;
// make sure the cursor is open
if (cxoCursor_isOpen(cursor) < 0)
return NULL;
// determine the number of query columns; if not a query return None
if (!cursor->handle)
Py_RETURN_NONE;
if (dpiStmt_getNumQueryColumns(cursor->handle, &numQueryColumns) < 0)
return cxoError_raiseAndReturnNull();
if (numQueryColumns == 0)
Py_RETURN_NONE;
// create a list of the required length
results = PyList_New(numQueryColumns);
if (!results)
return NULL;
// create tuples corresponding to the select-items
for (i = 0; i < numQueryColumns; i++) {
tuple = cxoCursor_itemDescription(cursor, i + 1);
if (!tuple) {
Py_DECREF(results);
return NULL;
}
PyList_SET_ITEM(results, i, tuple);
}
return results;
}
//-----------------------------------------------------------------------------
// cxoCursor_getLastRowid()
// Return the rowid of the last modified row if applicable. If no row was
// modified the value None is returned.
//-----------------------------------------------------------------------------
static PyObject *cxoCursor_getLastRowid(cxoCursor *cursor, void *unused)
{
uint32_t rowidStrLength;
const char *rowidStr;
dpiRowid *rowid;
// make sure the cursor is open
if (cxoCursor_isOpen(cursor) < 0)
return NULL;
// get the value, if applicable
if (cursor->handle) {
if (dpiStmt_getLastRowid(cursor->handle, &rowid) < 0)
return cxoError_raiseAndReturnNull();
if (rowid) {
if (dpiRowid_getStringValue(rowid, &rowidStr, &rowidStrLength) < 0)
return cxoError_raiseAndReturnNull();
return PyUnicode_Decode(rowidStr, rowidStrLength,
cursor->connection->encodingInfo.encoding, NULL);
}
}
Py_RETURN_NONE;
}
//-----------------------------------------------------------------------------
// cxoCursor_getOciAttr()
// Return the value of the OCI attribute. This is intended to be used for
// testing attributes which are not currently exposed directly and should only
// be used for that purpose.
//-----------------------------------------------------------------------------
static PyObject *cxoCursor_getOciAttr(cxoCursor *cursor, PyObject *args,
PyObject *keywordArgs)
{
static char *keywordList[] = { "attr_num", "attr_type", NULL };
unsigned attrNum, attrType;
uint32_t valueLength;
dpiDataBuffer value;
// validate parameters
if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "II", keywordList,
&attrNum, &attrType))
return NULL;
if (cxoCursor_isOpen(cursor) < 0)
return NULL;
// get value and convert it to the appropriate Python value
if (dpiStmt_getOciAttr(cursor->handle, attrNum, &value, &valueLength) < 0)
return cxoError_raiseAndReturnNull();
return cxoUtils_convertOciAttrToPythonValue(attrType, &value, valueLength,
cursor->connection->encodingInfo.encoding);
}
//-----------------------------------------------------------------------------
// cxoCursor_getPrefetchRows()
// Return an integer providing the number of rows that are prefetched by the
// Oracle Client library.
//-----------------------------------------------------------------------------
static PyObject *cxoCursor_getPrefetchRows(cxoCursor *cursor, void *unused)
{
if (cxoCursor_isOpen(cursor) < 0)
return NULL;
return PyLong_FromUnsignedLong(cursor->prefetchRows);
}
//-----------------------------------------------------------------------------
// cxoCursor_close()
// Close the cursor. Any action taken on this cursor from this point forward
// results in an exception being raised.
//-----------------------------------------------------------------------------
static PyObject *cxoCursor_close(cxoCursor *cursor, PyObject *args)
{
if (cxoCursor_isOpen(cursor) < 0)
return NULL;
Py_CLEAR(cursor->bindVariables);
Py_CLEAR(cursor->fetchVariables);
if (cursor->handle) {
if (dpiStmt_close(cursor->handle, NULL, 0) < 0)
return cxoError_raiseAndReturnNull();
dpiStmt_release(cursor->handle);
cursor->handle = NULL;
}
cursor->isOpen = 0;
Py_RETURN_NONE;
}
//-----------------------------------------------------------------------------
// cxoCursor_setBindVariableHelper()
// Helper for setting a bind variable.
//-----------------------------------------------------------------------------
static int cxoCursor_setBindVariableHelper(cxoCursor *cursor,
unsigned numElements, unsigned arrayPos, PyObject *value,
cxoVar *origVar, cxoVar **newVar, int deferTypeAssignment)
{
cxoVar *varToSet;
int isValueVar;
// initialization
*newVar = NULL;
isValueVar = cxoVar_check(value);
// handle case where variable is already bound, either from a prior
// execution or a call to setinputsizes()
if (origVar) {
// if the value is a variable object, rebind it if necessary
if (isValueVar) {
if ( (PyObject*) origVar != value) {
Py_INCREF(value);
*newVar = (cxoVar*) value;
}
// otherwise, attempt to set the value, but if this fails, simply
// ignore the original bind variable and create a new one; this is
// intended for cases where the type changes between executions of a
// statement or where setinputsizes() has been called with the wrong
// type (as mandated by the DB API)
} else {
varToSet = origVar;
// first check to see if the variable transform is for None (which
// can happen if all of the values in a previous invocation of
// executemany() were None) and there is now a value; in this case,
// discard the original variable and have a new one created
if (origVar->transformNum == CXO_TRANSFORM_NONE &&
value != Py_None) {
origVar = NULL;
varToSet = NULL;
// otherwise, 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 = cxoVar_new(cursor, numElements,
origVar->transformNum, origVar->size, origVar->isArray,
origVar->objectType);
if (!*newVar)
return -1;
varToSet = *newVar;
}
// attempt to set the value
if (varToSet && cxoVar_setValue(varToSet, arrayPos, value) < 0) {
// executemany() should simply fail after the first element
if (arrayPos > 0)
return -1;
// clear the exception and try to create a new variable
PyErr_Clear();
Py_CLEAR(*newVar);
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 = (cxoVar*) value;
// otherwise, create a new variable, unless the value is None and
// we wish to defer type assignment
} else if (value != Py_None || !deferTypeAssignment) {
*newVar = cxoVar_newByValue(cursor, value, numElements);
if (!*newVar)
return -1;
if (cxoVar_setValue(*newVar, arrayPos, value) < 0) {
Py_CLEAR(*newVar);
return -1;
}
}
}
return 0;
}
//-----------------------------------------------------------------------------
// cxoCursor_setBindVariables()
// Create or set bind variables.
//-----------------------------------------------------------------------------
int cxoCursor_setBindVariables(cxoCursor *cursor, PyObject *parameters,
unsigned numElements, unsigned arrayPos, int deferTypeAssignment)
{
uint32_t i, origBoundByPos, origNumParams, boundByPos, numParams;
PyObject *key, *value, *origVar;
cxoVar *newVar;
Py_ssize_t pos, temp;
// make sure positional and named binds are not being intermixed
origNumParams = numParams = 0;
boundByPos = PySequence_Check(parameters);
if (boundByPos) {
temp = PySequence_Size(parameters);
if (temp < 0)
return -1;
numParams = (uint32_t) temp;
}
if (cursor->bindVariables) {
origBoundByPos = PyList_Check(cursor->bindVariables);
if (boundByPos != origBoundByPos) {
cxoError_raiseFromString(cxoProgrammingErrorException,
"positional and named binds cannot be intermixed");
return -1;
}
if (origBoundByPos)
origNumParams = (uint32_t) PyList_GET_SIZE(cursor->bindVariables);
// otherwise, create the list or dictionary if needed
} else {
if (boundByPos)
cursor->bindVariables = PyList_New(numParams);
else cursor->bindVariables = PyDict_New();
if (!cursor->bindVariables)
return -1;
}
// handle positional binds
if (boundByPos) {
for (i = 0; i < numParams; i++) {
value = PySequence_GetItem(parameters, i);
if (!value)
return -1;
Py_DECREF(value);
if (i < origNumParams) {
origVar = PyList_GET_ITEM(cursor->bindVariables, i);
if (origVar == Py_None)
origVar = NULL;
} else origVar = NULL;
if (cxoCursor_setBindVariableHelper(cursor, numElements, arrayPos,
value, (cxoVar*) origVar, &newVar,
deferTypeAssignment) < 0)
return -1;
if (newVar) {
if (i < (uint32_t) PyList_GET_SIZE(cursor->bindVariables)) {
if (PyList_SetItem(cursor->bindVariables, i,
(PyObject*) newVar) < 0) {
Py_DECREF(newVar);
return -1;
}
} else {
if (PyList_Append(cursor->bindVariables,
(PyObject*) newVar) < 0) {
Py_DECREF(newVar);
return -1;
}
Py_DECREF(newVar);
}
}
}
// handle named binds
} else {
pos = 0;
while (PyDict_Next(parameters, &pos, &key, &value)) {
origVar = PyDict_GetItem(cursor->bindVariables, key);
if (cxoCursor_setBindVariableHelper(cursor, numElements, arrayPos,
value, (cxoVar*) origVar, &newVar,
deferTypeAssignment) < 0)
return -1;
if (newVar) {
if (PyDict_SetItem(cursor->bindVariables, key,
(PyObject*) newVar) < 0) {
Py_DECREF(newVar);
return -1;
}
Py_DECREF(newVar);
}
}
}
return 0;
}
//-----------------------------------------------------------------------------
// cxoCursor_performBind()
// Perform the binds on the cursor.
//-----------------------------------------------------------------------------
int cxoCursor_performBind(cxoCursor *cursor)
{
PyObject *key, *var;
Py_ssize_t pos;
int i;
// ensure that input sizes are reset
// this is done before binding is attempted so that if binding fails and
// a new statement is prepared, the bind variables will be reset and
// spurious errors will not occur
cursor->setInputSizes = 0;
// set values and perform binds for all bind variables
if (cursor->bindVariables) {
if (PyDict_Check(cursor->bindVariables)) {
pos = 0;
while (PyDict_Next(cursor->bindVariables, &pos, &key, &var)) {
if (cxoVar_bind((cxoVar*) var, cursor, key, 0) < 0)
return -1;
}
} else {
for (i = 0; i < PyList_GET_SIZE(cursor->bindVariables); i++) {
var = PyList_GET_ITEM(cursor->bindVariables, i);
if (var != Py_None) {
if (cxoVar_bind((cxoVar*) var, cursor, NULL,
i + 1) < 0)
return -1;
}
}
}
}
return 0;
}
//-----------------------------------------------------------------------------
// cxoCursor_createRow()
// Create an object for the row. The object created is a tuple unless a row
// factory function has been defined in which case it is the result of the
// row factory function called with the argument tuple that would otherwise be
// returned.
//-----------------------------------------------------------------------------
static PyObject *cxoCursor_createRow(cxoCursor *cursor, uint32_t pos)
{
PyObject *tuple, *item, *result;
Py_ssize_t numItems, i;
cxoVar *var;
// bump row count as a new row has been found
cursor->rowCount++;
// create a new tuple
numItems = PyList_GET_SIZE(cursor->fetchVariables);
tuple = PyTuple_New(numItems);
if (!tuple)
return NULL;
// acquire the value for each item
for (i = 0; i < numItems; i++) {
var = (cxoVar*) PyList_GET_ITEM(cursor->fetchVariables, i);
item = cxoVar_getSingleValue(var, var->data, pos);
if (!item) {
Py_DECREF(tuple);
return NULL;
}
PyTuple_SET_ITEM(tuple, i, item);
}
// if a row factory is defined, call it
if (cursor->rowFactory && cursor->rowFactory != Py_None) {
result = PyObject_CallObject(cursor->rowFactory, tuple);
Py_DECREF(tuple);
return result;
}
return tuple;
}
//-----------------------------------------------------------------------------
// cxoCursor_internalPrepare()
// Internal method for preparing a statement for execution.
//-----------------------------------------------------------------------------
static int cxoCursor_internalPrepare(cxoCursor *cursor, PyObject *statement,
PyObject *statementTag)
{
cxoBuffer statementBuffer, tagBuffer;
int status;
// make sure we don't get a situation where nothing is to be executed
if (statement == Py_None && !cursor->statement) {
cxoError_raiseFromString(cxoProgrammingErrorException,
"no statement specified and no prior statement prepared");
return -1;
}
// nothing to do if the statement is identical to the one already stored
// but go ahead and prepare anyway for create, alter and drop statments
if (statement == Py_None || statement == cursor->statement) {
if (cursor->handle && !cursor->stmtInfo.isDDL)
return 0;
statement = cursor->statement;
}
// keep track of the statement
Py_XDECREF(cursor->statement);
Py_INCREF(statement);
cursor->statement = statement;
// keep track of the tag
Py_XDECREF(cursor->statementTag);
Py_XINCREF(statementTag);
cursor->statementTag = statementTag;
// clear fetch and bind variables if applicable
Py_CLEAR(cursor->fetchVariables);
if (!cursor->setInputSizes)
Py_CLEAR(cursor->bindVariables);
// prepare statement
if (cxoBuffer_fromObject(&statementBuffer, statement,
cursor->connection->encodingInfo.encoding) < 0)
return -1;
if (cxoBuffer_fromObject(&tagBuffer, statementTag,
cursor->connection->encodingInfo.encoding) < 0) {
cxoBuffer_clear(&statementBuffer);
return -1;
}
Py_BEGIN_ALLOW_THREADS
if (cursor->handle)
dpiStmt_release(cursor->handle);
status = dpiConn_prepareStmt(cursor->connection->handle,
cursor->isScrollable, (const char*) statementBuffer.ptr,
statementBuffer.size, (const char*) tagBuffer.ptr, tagBuffer.size,
&cursor->handle);
Py_END_ALLOW_THREADS
cxoBuffer_clear(&statementBuffer);
cxoBuffer_clear(&tagBuffer);
if (status < 0)
return cxoError_raiseAndReturnInt();
// get statement information
if (dpiStmt_getInfo(cursor->handle, &cursor->stmtInfo) < 0)
return cxoError_raiseAndReturnInt();
// set the fetch array size, if applicable
if (cursor->stmtInfo.statementType == DPI_STMT_TYPE_SELECT) {
if (dpiStmt_setFetchArraySize(cursor->handle, cursor->arraySize) < 0)
return cxoError_raiseAndReturnInt();
}
// set number of rows to prefetch on execute, if applicable
if (cursor->prefetchRows != DPI_DEFAULT_PREFETCH_ROWS) {
if (dpiStmt_setPrefetchRows(cursor->handle, cursor->prefetchRows) < 0)
return cxoError_raiseAndReturnInt();
}
// clear row factory, if applicable
Py_CLEAR(cursor->rowFactory);
return 0;
}
//-----------------------------------------------------------------------------
// cxoCursor_parse()
// Parse the statement without executing it. This also retrieves information
// about the select list for select statements.
//-----------------------------------------------------------------------------
static PyObject *cxoCursor_parse(cxoCursor *cursor, PyObject *statement)
{
uint32_t mode, numQueryColumns;
dpiStmtInfo stmtInfo;
int status;
// make sure the cursor is open
if (cxoCursor_isOpen(cursor) < 0)
return NULL;
// prepare the statement and get statement information
if (cxoCursor_internalPrepare(cursor, statement, NULL) < 0)
return NULL;
if (dpiStmt_getInfo(cursor->handle, &stmtInfo) < 0)
return cxoError_raiseAndReturnNull();
// parse the statement
if (stmtInfo.isQuery)
mode = DPI_MODE_EXEC_DESCRIBE_ONLY;
else mode = DPI_MODE_EXEC_PARSE_ONLY;
Py_BEGIN_ALLOW_THREADS
status = dpiStmt_execute(cursor->handle, mode, &numQueryColumns);
Py_END_ALLOW_THREADS
if (status < 0)
return cxoError_raiseAndReturnNull();
Py_RETURN_NONE;
}
//-----------------------------------------------------------------------------
// cxoCursor_prepare()
// Prepare the statement for execution.
//-----------------------------------------------------------------------------
static PyObject *cxoCursor_prepare(cxoCursor *cursor, PyObject *args)
{
PyObject *statement, *statementTag;
// statement text and optional tag is expected
statementTag = NULL;
if (!PyArg_ParseTuple(args, "O|O", &statement, &statementTag))
return NULL;
// make sure the cursor is open
if (cxoCursor_isOpen(cursor) < 0)
return NULL;
// prepare the statement
if (cxoCursor_internalPrepare(cursor, statement, statementTag) < 0)
return NULL;
Py_RETURN_NONE;
}
//-----------------------------------------------------------------------------
// cxoCursor_callCalculateSize()
// Calculate the size of the statement that is to be executed.
//-----------------------------------------------------------------------------
static int cxoCursor_callCalculateSize(PyObject *name,
cxoVar *returnValue, PyObject *listOfArguments,
PyObject *keywordArguments, int *size)
{
Py_ssize_t numPositionalArgs, numKeywordArgs;
// set base size without any arguments
*size = 17;