-
Notifications
You must be signed in to change notification settings - Fork 2
/
Variable.c
1521 lines (1355 loc) · 53 KB
/
Variable.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
//-----------------------------------------------------------------------------
// Variable.c
// Defines Python types for Oracle variables.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// define structure common to all variables
//-----------------------------------------------------------------------------
struct _udt_VariableType;
#define Variable_HEAD \
PyObject_HEAD \
OCIBind *bindHandle; \
OCIDefine *defineHandle; \
OCIStmt *boundCursorHandle; \
PyObject *boundName; \
PyObject *inConverter; \
PyObject *outConverter; \
ub4 boundPos; \
udt_Environment *environment; \
ub4 allocatedElements; \
ub4 actualElements; \
unsigned internalFetchNum; \
int isArray; \
int isAllocatedInternally; \
sb2 *indicator; \
ub2 *returnCode; \
ub2 *actualLength; \
ub4 size; \
ub4 bufferSize; \
struct _udt_VariableType *type;
typedef struct {
Variable_HEAD
void *data;
} udt_Variable;
//-----------------------------------------------------------------------------
// define function types for the common actions that take place on a variable
//-----------------------------------------------------------------------------
typedef int (*InitializeProc)(udt_Variable*, udt_Cursor*);
typedef void (*FinalizeProc)(udt_Variable*);
typedef int (*PreDefineProc)(udt_Variable*, OCIParam*);
typedef int (*PostDefineProc)(udt_Variable*);
typedef int (*PreFetchProc)(udt_Variable*);
typedef int (*IsNullProc)(udt_Variable*, unsigned);
typedef int (*SetValueProc)(udt_Variable*, unsigned, PyObject*);
typedef PyObject * (*GetValueProc)(udt_Variable*, unsigned);
typedef ub4 (*GetBufferSizeProc)(udt_Variable*);
//-----------------------------------------------------------------------------
// define structure for the common actions that take place on a variable
//-----------------------------------------------------------------------------
typedef struct _udt_VariableType {
InitializeProc initializeProc;
FinalizeProc finalizeProc;
PreDefineProc preDefineProc;
PostDefineProc postDefineProc;
PreFetchProc preFetchProc;
IsNullProc isNullProc;
SetValueProc setValueProc;
GetValueProc getValueProc;
GetBufferSizeProc getBufferSizeProc;
PyTypeObject *pythonType;
ub2 oracleType;
ub1 charsetForm;
ub4 size;
int isCharacterData;
int isVariableLength;
int canBeCopied;
int canBeInArray;
} udt_VariableType;
//-----------------------------------------------------------------------------
// Declaration of common variable functions.
//-----------------------------------------------------------------------------
static void Variable_Free(udt_Variable *);
static PyObject *Variable_Repr(udt_Variable *);
static PyObject *Variable_ExternalCopy(udt_Variable *, PyObject *);
static PyObject *Variable_ExternalSetValue(udt_Variable *, PyObject *);
static PyObject *Variable_ExternalGetValue(udt_Variable *, PyObject *,
PyObject *);
static int Variable_InternalBind(udt_Variable *);
static int Variable_Resize(udt_Variable *, unsigned);
//-----------------------------------------------------------------------------
// declaration of members for variables
//-----------------------------------------------------------------------------
static PyMemberDef g_VariableMembers[] = {
{ "bufferSize", T_INT, offsetof(udt_Variable, bufferSize), READONLY },
{ "inconverter", T_OBJECT, offsetof(udt_Variable, inConverter), 0 },
{ "numElements", T_INT, offsetof(udt_Variable, allocatedElements),
READONLY },
{ "outconverter", T_OBJECT, offsetof(udt_Variable, outConverter), 0 },
{ "size", T_INT, offsetof(udt_Variable, size), READONLY },
{ "maxlength", T_INT, offsetof(udt_Variable, bufferSize), READONLY },
{ "allocelems", T_INT, offsetof(udt_Variable, allocatedElements),
READONLY },
{ NULL }
};
//-----------------------------------------------------------------------------
// declaration of methods for variables
//-----------------------------------------------------------------------------
static PyMethodDef g_VariableMethods[] = {
{ "copy", (PyCFunction) Variable_ExternalCopy, METH_VARARGS },
{ "setvalue", (PyCFunction) Variable_ExternalSetValue, METH_VARARGS },
{ "getvalue", (PyCFunction) Variable_ExternalGetValue,
METH_VARARGS | METH_KEYWORDS },
{ NULL }
};
//-----------------------------------------------------------------------------
// The base variable type
//-----------------------------------------------------------------------------
static PyTypeObject g_BaseVarType = {
PyVarObject_HEAD_INIT(NULL, 0)
"cx_Oracle._BASEVARTYPE", // tp_name
sizeof(udt_Variable), // tp_basicsize
0, // tp_itemsize
(destructor) Variable_Free, // tp_dealloc
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_compare
(reprfunc) Variable_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, // tp_flags
0, // tp_doc
0, // tp_traverse
0, // tp_clear
0, // tp_richcompare
0, // tp_weaklistoffset
0, // tp_iter
0, // tp_iternext
g_VariableMethods, // tp_methods
g_VariableMembers // tp_members
};
#include "Transforms.c"
#include "StringVar.c"
#include "LongVar.c"
#include "NumberVar.c"
#include "DateTimeVar.c"
#include "TimestampVar.c"
#include "LobVar.c"
#include "CursorVar.c"
#include "ObjectVar.c"
#include "IntervalVar.c"
//-----------------------------------------------------------------------------
// Variable_AllocateData()
// Allocate the data for the variable.
//-----------------------------------------------------------------------------
static int Variable_AllocateData(
udt_Variable *self) // variable to allocate data for
{
unsigned PY_LONG_LONG dataLength;
// set the buffer size for the variable
if (self->type->getBufferSizeProc)
self->bufferSize = (*self->type->getBufferSizeProc)(self);
else self->bufferSize = self->size;
// allocate the data as long as it is small enough
dataLength = (unsigned PY_LONG_LONG) self->allocatedElements *
(unsigned PY_LONG_LONG) self->bufferSize;
if (dataLength > INT_MAX) {
PyErr_SetString(PyExc_ValueError, "array size too large");
return -1;
}
self->data = PyMem_Malloc((size_t) dataLength);
if (!self->data) {
PyErr_NoMemory();
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
// Variable_New()
// Allocate a new variable.
//-----------------------------------------------------------------------------
static udt_Variable *Variable_New(
udt_Cursor *cursor, // cursor to associate variable with
unsigned numElements, // number of elements to allocate
udt_VariableType *type, // variable type
ub4 size) // used only for variable length types
{
udt_Variable *self;
ub4 i;
// attempt to allocate the object
self = (udt_Variable*) type->pythonType->tp_alloc(type->pythonType, 0);
if (!self)
return NULL;
// perform basic initialization
Py_INCREF(cursor->connection->environment);
self->environment = cursor->connection->environment;
self->boundCursorHandle = NULL;
self->bindHandle = NULL;
self->defineHandle = NULL;
self->boundName = NULL;
self->inConverter = NULL;
self->outConverter = NULL;
self->boundPos = 0;
if (numElements < 1)
self->allocatedElements = 1;
else self->allocatedElements = numElements;
self->actualElements = 0;
self->internalFetchNum = 0;
self->isArray = 0;
self->isAllocatedInternally = 1;
self->type = type;
self->indicator = NULL;
self->data = NULL;
self->actualLength = NULL;
self->returnCode = NULL;
// set the maximum length of the variable, ensure that a minimum of
// 2 bytes is allocated to ensure that the array size check works
self->size = type->size;
if (type->isVariableLength) {
if (size < sizeof(ub2))
size = sizeof(ub2);
self->size = size;
}
// allocate the data for the variable
if (Variable_AllocateData(self) < 0) {
Py_DECREF(self);
return NULL;
}
// allocate the indicator for the variable
self->indicator = PyMem_Malloc(self->allocatedElements * sizeof(sb2));
if (!self->indicator) {
PyErr_NoMemory();
Py_DECREF(self);
return NULL;
}
// ensure that all variable values start out NULL
for (i = 0; i < self->allocatedElements; i++)
self->indicator[i] = OCI_IND_NULL;
// for variable length data, also allocate the return code
if (type->isVariableLength) {
self->returnCode = PyMem_Malloc(self->allocatedElements * sizeof(ub2));
if (!self->returnCode) {
PyErr_NoMemory();
Py_DECREF(self);
return NULL;
}
}
// perform extended initialization
if (self->type->initializeProc) {
if ((*self->type->initializeProc)(self, cursor) < 0) {
Py_DECREF(self);
return NULL;
}
}
return self;
}
//-----------------------------------------------------------------------------
// Variable_Free()
// Free an existing variable.
//-----------------------------------------------------------------------------
static void Variable_Free(
udt_Variable *self) // variable to free
{
if (self->isAllocatedInternally) {
if (self->type->finalizeProc)
(*self->type->finalizeProc)(self);
if (self->indicator)
PyMem_Free(self->indicator);
if (self->data)
PyMem_Free(self->data);
if (self->actualLength)
PyMem_Free(self->actualLength);
if (self->returnCode)
PyMem_Free(self->returnCode);
}
Py_CLEAR(self->environment);
Py_CLEAR(self->boundName);
Py_CLEAR(self->inConverter);
Py_CLEAR(self->outConverter);
Py_TYPE(self)->tp_free((PyObject*) self);
}
//-----------------------------------------------------------------------------
// Variable_Resize()
// Resize the variable.
//-----------------------------------------------------------------------------
static int Variable_Resize(
udt_Variable *self, // variable to resize
unsigned size) // new size to use
{
ub4 origBufferSize, i;
char *origData;
// allocate the data for the new array
origData = self->data;
origBufferSize = self->bufferSize;
self->size = size;
if (Variable_AllocateData(self) < 0)
return -1;
// copy the data from the original array to the new array
for (i = 0; i < self->allocatedElements; i++)
memcpy( (char*) self->data + self->bufferSize * i,
(void*) ( (char*) origData + origBufferSize * i ),
origBufferSize);
PyMem_Free(origData);
// force rebinding
if (self->boundName || self->boundPos > 0) {
if (Variable_InternalBind(self) < 0)
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
// Variable_Check()
// Returns a boolean indicating if the object is a variable.
//-----------------------------------------------------------------------------
static int Variable_Check(
PyObject *object) // Python object to check
{
return (Py_TYPE(object) == &g_CursorVarType ||
Py_TYPE(object) == &g_DateTimeVarType ||
Py_TYPE(object) == &g_BFILEVarType ||
Py_TYPE(object) == &g_BLOBVarType ||
Py_TYPE(object) == &g_CLOBVarType ||
Py_TYPE(object) == &g_LongStringVarType ||
Py_TYPE(object) == &g_LongBinaryVarType ||
Py_TYPE(object) == &g_NumberVarType ||
Py_TYPE(object) == &g_StringVarType ||
Py_TYPE(object) == &g_FixedCharVarType ||
Py_TYPE(object) == &g_NCLOBVarType ||
#if PY_MAJOR_VERSION < 3
Py_TYPE(object) == &g_UnicodeVarType ||
Py_TYPE(object) == &g_FixedUnicodeVarType ||
#endif
Py_TYPE(object) == &g_RowidVarType ||
Py_TYPE(object) == &g_BinaryVarType ||
Py_TYPE(object) == &g_TimestampVarType ||
Py_TYPE(object) == &g_IntervalVarType
#ifdef SQLT_BFLOAT
|| Py_TYPE(object) == &g_NativeFloatVarType
#endif
);
}
//-----------------------------------------------------------------------------
// Variable_TypeByPythonType()
// Return a variable type given a Python type object or NULL if the Python
// type does not have a corresponding variable type.
//-----------------------------------------------------------------------------
static udt_VariableType *Variable_TypeByPythonType(
udt_Cursor* cursor, // cursor variable created for
PyObject* type) // Python type
{
if (type == (PyObject*) &g_StringVarType)
return &vt_String;
if (type == (PyObject*) cxString_Type)
return &vt_String;
if (type == (PyObject*) &g_FixedCharVarType)
return &vt_FixedChar;
#if PY_MAJOR_VERSION < 3
if (type == (PyObject*) &g_UnicodeVarType)
return &vt_NationalCharString;
if (type == (PyObject*) &PyUnicode_Type)
return &vt_NationalCharString;
if (type == (PyObject*) &g_FixedUnicodeVarType)
return &vt_FixedNationalChar;
#endif
if (type == (PyObject*) &g_NCLOBVarType)
return &vt_NCLOB;
if (type == (PyObject*) &g_RowidVarType)
return &vt_Rowid;
if (type == (PyObject*) &g_BinaryVarType)
return &vt_Binary;
if (type == (PyObject*) &cxBinary_Type)
return &vt_Binary;
if (type == (PyObject*) &g_LongStringVarType)
return &vt_LongString;
if (type == (PyObject*) &g_LongBinaryVarType)
return &vt_LongBinary;
if (type == (PyObject*) &g_BFILEVarType)
return &vt_BFILE;
if (type == (PyObject*) &g_BLOBVarType)
return &vt_BLOB;
if (type == (PyObject*) &g_CLOBVarType)
return &vt_CLOB;
if (type == (PyObject*) &g_NumberVarType) {
if (cursor->numbersAsStrings)
return &vt_NumberAsString;
return &vt_Float;
}
if (type == (PyObject*) &PyFloat_Type)
return &vt_Float;
#if PY_MAJOR_VERSION < 3
if (type == (PyObject*) &PyInt_Type)
return &vt_Integer;
#endif
if (type == (PyObject*) &PyLong_Type)
return &vt_LongInteger;
if (type == (PyObject*) &PyBool_Type)
return &vt_Boolean;
if (type == (PyObject*) &g_DateTimeVarType)
return &vt_DateTime;
if (type == (PyObject*) PyDateTimeAPI->DateType)
return &vt_Date;
if (type == (PyObject*) PyDateTimeAPI->DateTimeType)
return &vt_DateTime;
if (type == (PyObject*) &g_IntervalVarType)
return &vt_Interval;
if (type == (PyObject*) PyDateTimeAPI->DeltaType)
return &vt_Interval;
if (type == (PyObject*) &g_TimestampVarType)
return &vt_Timestamp;
if (type == (PyObject*) &g_CursorVarType)
return &vt_Cursor;
#ifdef SQLT_BFLOAT
if (type == (PyObject*) &g_NativeFloatVarType)
return &vt_NativeFloat;
#endif
if (type == (PyObject*) &g_ObjectVarType)
return &vt_Object;
PyErr_SetString(g_NotSupportedErrorException,
"Variable_TypeByPythonType(): unhandled data type");
return NULL;
}
//-----------------------------------------------------------------------------
// Variable_TypeByValue()
// Return a variable type given a Python object or NULL if the Python
// object does not have a corresponding variable type.
//-----------------------------------------------------------------------------
static udt_VariableType *Variable_TypeByValue(
PyObject* value, // Python type
ub4* size, // size to use (OUT)
unsigned *numElements) // number of elements (OUT)
{
udt_VariableType *varType;
PyObject *elementValue;
char buffer[200];
int i, result;
// handle scalars
if (value == Py_None) {
*size = 1;
return &vt_String;
}
if (cxString_Check(value)) {
*size = cxString_GetSize(value);
if (*size > MAX_STRING_CHARS)
return &vt_LongString;
return &vt_String;
}
#if PY_MAJOR_VERSION < 3
if (PyUnicode_Check(value)) {
*size = PyUnicode_GET_SIZE(value);
if (*size > MAX_STRING_CHARS)
return &vt_LongString;
return &vt_NationalCharString;
}
if (PyInt_Check(value))
return &vt_Integer;
#else
if (PyBytes_Check(value)) {
*size = PyBytes_GET_SIZE(value);
if (*size > MAX_BINARY_BYTES)
return &vt_LongBinary;
return &vt_Binary;
}
#endif
if (PyLong_Check(value))
return &vt_LongInteger;
if (PyFloat_Check(value))
return &vt_Float;
if (cxBinary_Check(value)) {
udt_Buffer temp;
if (cxBuffer_FromObject(&temp, value, NULL) < 0)
return NULL;
*size = temp.size;
cxBuffer_Clear(&temp);
if (*size > MAX_BINARY_BYTES)
return &vt_LongBinary;
return &vt_Binary;
}
if (PyBool_Check(value))
return &vt_Boolean;
if (PyDateTime_Check(value))
return &vt_DateTime;
if (PyDate_Check(value))
return &vt_DateTime;
if (PyDelta_Check(value))
return &vt_Interval;
result = PyObject_IsInstance(value, (PyObject*) &g_CursorType);
if (result < 0)
return NULL;
if (result)
return &vt_Cursor;
if (Py_TYPE(value) == g_DateTimeType)
return &vt_DateTime;
if (Py_TYPE(value) == g_DecimalType)
return &vt_NumberAsString;
// handle arrays
if (PyList_Check(value)) {
elementValue = Py_None;
for (i = 0; i < PyList_GET_SIZE(value); i++) {
elementValue = PyList_GET_ITEM(value, i);
if (elementValue != Py_None)
break;
}
varType = Variable_TypeByValue(elementValue, size, numElements);
if (!varType)
return NULL;
*numElements = PyList_GET_SIZE(value);
*size = varType->size;
return varType;
}
sprintf(buffer, "Variable_TypeByValue(): unhandled data type %.*s", 150,
Py_TYPE(value)->tp_name);
PyErr_SetString(g_NotSupportedErrorException, buffer);
return NULL;
}
//-----------------------------------------------------------------------------
// Variable_TypeByOracleDataType()
// Return a variable type given an Oracle data type or NULL if the Oracle
// data type does not have a corresponding variable type.
//-----------------------------------------------------------------------------
static udt_VariableType *Variable_TypeByOracleDataType (
ub2 oracleDataType, // Oracle data type
ub1 charsetForm) // character set form
{
char buffer[100];
switch(oracleDataType) {
case SQLT_LNG:
return &vt_LongString;
case SQLT_AFC:
#if PY_MAJOR_VERSION < 3
if (charsetForm == SQLCS_NCHAR)
return &vt_FixedNationalChar;
#endif
return &vt_FixedChar;
case SQLT_CHR:
#if PY_MAJOR_VERSION < 3
if (charsetForm == SQLCS_NCHAR)
return &vt_NationalCharString;
#endif
return &vt_String;
case SQLT_RDD:
return &vt_Rowid;
case SQLT_BIN:
return &vt_Binary;
case SQLT_LBI:
return &vt_LongBinary;
#ifdef SQLT_BFLOAT
case SQLT_BFLOAT:
case SQLT_IBFLOAT:
case SQLT_BDOUBLE:
case SQLT_IBDOUBLE:
return &vt_NativeFloat;
#endif
case SQLT_NUM:
case SQLT_VNU:
return &vt_Float;
case SQLT_DAT:
case SQLT_ODT:
return &vt_DateTime;
case SQLT_DATE:
case SQLT_TIMESTAMP:
case SQLT_TIMESTAMP_TZ:
case SQLT_TIMESTAMP_LTZ:
return &vt_Timestamp;
case SQLT_INTERVAL_DS:
return &vt_Interval;
case SQLT_CLOB:
if (charsetForm == SQLCS_NCHAR)
return &vt_NCLOB;
return &vt_CLOB;
case SQLT_BLOB:
return &vt_BLOB;
case SQLT_BFILE:
return &vt_BFILE;
case SQLT_RSET:
return &vt_Cursor;
case SQLT_NTY:
return &vt_Object;
}
sprintf(buffer, "Variable_TypeByOracleDataType: unhandled data type %d",
oracleDataType);
PyErr_SetString(g_NotSupportedErrorException, buffer);
return NULL;
}
//-----------------------------------------------------------------------------
// Variable_TypeByOracleDescriptor()
// Return a variable type given an Oracle descriptor.
//-----------------------------------------------------------------------------
static udt_VariableType *Variable_TypeByOracleDescriptor(
OCIParam *param, // parameter to get type from
udt_Environment *environment) // environment to use
{
ub1 charsetForm;
ub2 dataType;
sword status;
// retrieve datatype of the parameter
status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE, (dvoid*) &dataType, 0,
OCI_ATTR_DATA_TYPE, environment->errorHandle);
if (Environment_CheckForError(environment, status,
"Variable_TypeByOracleDescriptor(): data type") < 0)
return NULL;
// retrieve character set form of the parameter
if (dataType != SQLT_CHR && dataType != SQLT_AFC &&
dataType != SQLT_CLOB) {
charsetForm = SQLCS_IMPLICIT;
} else {
status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE, (dvoid*) &charsetForm,
0, OCI_ATTR_CHARSET_FORM, environment->errorHandle);
if (Environment_CheckForError(environment, status,
"Variable_TypeByOracleDescriptor(): charset form") < 0)
return NULL;
}
return Variable_TypeByOracleDataType(dataType, charsetForm);
}
//-----------------------------------------------------------------------------
// Variable_MakeArray()
// Make the variable an array, ensuring that the type supports arrays.
//-----------------------------------------------------------------------------
static int Variable_MakeArray(
udt_Variable *var) // variable to make an array
{
if (!var->type->canBeInArray) {
PyErr_SetString(g_NotSupportedErrorException,
"Variable_MakeArray(): type does not support arrays");
return -1;
}
var->isArray = 1;
return 0;
}
//-----------------------------------------------------------------------------
// Variable_DefaultNewByValue()
// Default method for determining the type of variable to use for the data.
//-----------------------------------------------------------------------------
static udt_Variable *Variable_DefaultNewByValue(
udt_Cursor *cursor, // cursor to associate variable with
PyObject *value, // Python value to associate
unsigned numElements) // number of elements to allocate
{
udt_VariableType *varType;
udt_Variable *var;
ub4 size = 0;
varType = Variable_TypeByValue(value, &size, &numElements);
if (!varType)
return NULL;
var = Variable_New(cursor, numElements, varType, size);
if (!var)
return NULL;
if (PyList_Check(value)) {
if (Variable_MakeArray(var) < 0) {
Py_DECREF(var);
return NULL;
}
}
return var;
}
//-----------------------------------------------------------------------------
// Variable_NewByInputTypeHandler()
// Allocate a new variable by looking at the type of the data.
//-----------------------------------------------------------------------------
static udt_Variable *Variable_NewByInputTypeHandler(
udt_Cursor *cursor, // cursor to associate variable with
PyObject *inputTypeHandler, // input type handler
PyObject *value, // Python value to associate
unsigned numElements) // number of elements to allocate
{
PyObject *var;
var = PyObject_CallFunction(inputTypeHandler, "OOi", cursor, value,
numElements);
if (!var)
return NULL;
if (var != Py_None) {
if (!Variable_Check(var)) {
Py_DECREF(var);
PyErr_SetString(PyExc_TypeError,
"expecting variable from input type handler");
return NULL;
}
return (udt_Variable*) var;
}
Py_DECREF(var);
return Variable_DefaultNewByValue(cursor, value, numElements);
}
//-----------------------------------------------------------------------------
// Variable_NewByValue()
// Allocate a new variable by looking at the type of the data.
//-----------------------------------------------------------------------------
static udt_Variable *Variable_NewByValue(
udt_Cursor *cursor, // cursor to associate variable with
PyObject *value, // Python value to associate
unsigned numElements) // number of elements to allocate
{
if (cursor->inputTypeHandler && cursor->inputTypeHandler != Py_None)
return Variable_NewByInputTypeHandler(cursor, cursor->inputTypeHandler,
value, numElements);
if (cursor->connection->inputTypeHandler &&
cursor->connection->inputTypeHandler != Py_None)
return Variable_NewByInputTypeHandler(cursor,
cursor->connection->inputTypeHandler, value, numElements);
return Variable_DefaultNewByValue(cursor, value, numElements);
}
//-----------------------------------------------------------------------------
// Variable_NewArrayByType()
// Allocate a new PL/SQL array by looking at the Python data type.
//-----------------------------------------------------------------------------
static udt_Variable *Variable_NewArrayByType(
udt_Cursor *cursor, // cursor to bind variable to
PyObject *value) // value to bind
{
PyObject *typeObj, *numElementsObj;
udt_VariableType *varType;
unsigned numElements;
udt_Variable *var;
if (PyList_GET_SIZE(value) != 2) {
PyErr_SetString(g_ProgrammingErrorException,
"expecting an array of two elements [type, numelems]");
return NULL;
}
typeObj = PyList_GET_ITEM(value, 0);
numElementsObj = PyList_GET_ITEM(value, 1);
if (!PyInt_Check(numElementsObj)) {
PyErr_SetString(g_ProgrammingErrorException,
"number of elements must be an integer");
return NULL;
}
varType = Variable_TypeByPythonType(cursor, typeObj);
if (!varType)
return NULL;
numElements = PyInt_AsLong(numElementsObj);
if (PyErr_Occurred())
return NULL;
var = Variable_New(cursor, numElements, varType, varType->size);
if (!var)
return NULL;
if (Variable_MakeArray(var) < 0) {
Py_DECREF(var);
return NULL;
}
return var;
}
//-----------------------------------------------------------------------------
// Variable_NewByType()
// Allocate a new variable by looking at the Python data type.
//-----------------------------------------------------------------------------
static udt_Variable *Variable_NewByType(
udt_Cursor *cursor, // cursor to associate variable with
PyObject *value, // Python data type to associate
unsigned numElements) // number of elements to allocate
{
udt_VariableType *varType;
int size;
// passing an integer is assumed to be a string
if (PyInt_Check(value)) {
size = PyInt_AsLong(value);
if (PyErr_Occurred())
return NULL;
if (size > MAX_STRING_CHARS)
varType = &vt_LongString;
else varType = &vt_String;
return Variable_New(cursor, numElements, varType, size);
}
// passing an array of two elements to define an array
if (PyList_Check(value))
return Variable_NewArrayByType(cursor, value);
// handle directly bound variables
if (Variable_Check(value)) {
Py_INCREF(value);
return (udt_Variable*) value;
}
// everything else ought to be a Python type
varType = Variable_TypeByPythonType(cursor, value);
if (!varType)
return NULL;
return Variable_New(cursor, numElements, varType, varType->size);
}
//-----------------------------------------------------------------------------
// Variable_NewByOutputTypeHandler()
// Create a new variable by calling the output type handler.
//-----------------------------------------------------------------------------
static udt_Variable *Variable_NewByOutputTypeHandler(
udt_Cursor *cursor, // cursor to associate variable with
OCIParam *param, // parameter descriptor
PyObject *outputTypeHandler, // method to call to get type
udt_VariableType *varType, // variable type already chosen
ub4 size, // maximum size of variable
unsigned numElements) // number of elements
{
udt_Variable *var;
PyObject *result;
ub4 nameLength;
sb2 precision;
sword status;
char *name;
sb1 scale;
// determine name of variable
status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE, (dvoid*) &name,
&nameLength, OCI_ATTR_NAME, cursor->environment->errorHandle);
if (Environment_CheckForError(cursor->environment, status,
"Variable_NewByOutputTypeHandler(): get name") < 0)
return NULL;
// retrieve scale and precision of the parameter, if applicable
precision = scale = 0;
if (varType->pythonType == &g_NumberVarType) {
status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE, (dvoid*) &scale, 0,
OCI_ATTR_SCALE, cursor->environment->errorHandle);
if (Environment_CheckForError(cursor->environment, status,
"Variable_NewByOutputTypeHandler(): get scale") < 0)
return NULL;
status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE, (dvoid*) &precision, 0,
OCI_ATTR_PRECISION, cursor->environment->errorHandle);
if (Environment_CheckForError(cursor->environment, status,
"Variable_NewByOutputTypeHandler(): get precision") < 0)
return NULL;
}
// call method, passing parameters
result = PyObject_CallFunction(outputTypeHandler, "Os#Oiii", cursor, name,
nameLength, varType->pythonType, size, precision, scale);
if (!result)
return NULL;
// if result is None, assume default behavior
if (result == Py_None) {
Py_DECREF(result);
return Variable_New(cursor, numElements, varType, size);
}
// otherwise, verify that the result is an actual variable
if (!Variable_Check(result)) {
Py_DECREF(result);
PyErr_SetString(PyExc_TypeError,
"expecting variable from output type handler");
return NULL;
}
// verify that the array size is sufficient to handle the fetch
var = (udt_Variable*) result;
if (var->allocatedElements < cursor->fetchArraySize) {
Py_DECREF(result);
PyErr_SetString(PyExc_TypeError,
"expecting variable with array size large enough for fetch");
return NULL;
}
return var;
}
//-----------------------------------------------------------------------------
// Variable_DefineHelper()
// Helper routine for Variable_Define() used so that constant calls to
// OCIDescriptorFree() is not necessary.
//-----------------------------------------------------------------------------
static udt_Variable *Variable_DefineHelper(
udt_Cursor *cursor, // cursor in use
OCIParam *param, // parameter descriptor
unsigned position, // position in define list
unsigned numElements) // number of elements to create
{
udt_VariableType *varType;
ub2 sizeFromOracle;
udt_Variable *var;
sword status;
ub4 size;
// determine data type
varType = Variable_TypeByOracleDescriptor(param, cursor->environment);
if (!varType)
return NULL;
if (cursor->numbersAsStrings && varType == &vt_Float)
varType = &vt_NumberAsString;
// retrieve size of the parameter
size = varType->size;
if (varType->isVariableLength) {
// determine the maximum length from Oracle
status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE,
(dvoid*) &sizeFromOracle, 0, OCI_ATTR_DATA_SIZE,
cursor->environment->errorHandle);
if (Environment_CheckForError(cursor->environment, status,
"Variable_Define(): data size") < 0)
return NULL;
// use the length from Oracle directly if available
if (sizeFromOracle)
size = sizeFromOracle;
// otherwise, use the value set with the setoutputsize() parameter
else if (cursor->outputSize >= 0) {
if (cursor->outputSizeColumn < 0 ||
(int) position == cursor->outputSizeColumn)
size = cursor->outputSize;
}
}
// create a variable of the correct type
if (cursor->outputTypeHandler && cursor->outputTypeHandler != Py_None)
var = Variable_NewByOutputTypeHandler(cursor, param,
cursor->outputTypeHandler, varType, size, numElements);
else if (cursor->connection->outputTypeHandler &&
cursor->connection->outputTypeHandler != Py_None)
var = Variable_NewByOutputTypeHandler(cursor, param,
cursor->connection->outputTypeHandler, varType, size,
numElements);
else var = Variable_New(cursor, numElements, varType, size);
if (!var)
return NULL;
// call the procedure to set values prior to define
if (var->type->preDefineProc) {
if ((*var->type->preDefineProc)(var, param) < 0) {
Py_DECREF(var);
return NULL;
}
}
// perform the define
status = OCIDefineByPos(cursor->handle, &var->defineHandle,
var->environment->errorHandle, position, var->data,