-
Notifications
You must be signed in to change notification settings - Fork 2
/
ObjectType.c
609 lines (539 loc) · 23.4 KB
/
ObjectType.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
//-----------------------------------------------------------------------------
// ObjectType.c
// Defines the routines for handling Oracle type information.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// structures used for handling object types
//-----------------------------------------------------------------------------
typedef struct {
PyObject_HEAD
udt_Environment *environment;
OCIType *tdo;
PyObject *schema;
PyObject *name;
PyObject *attributes;
PyObject *attributesByName;
OCITypeCode collectionTypeCode;
OCITypeCode elementTypeCode;
PyObject *elementType;
int isCollection;
} udt_ObjectType;
typedef struct {
PyObject_HEAD
PyObject *name;
OCITypeCode typeCode;
udt_ObjectType *subType;
} udt_ObjectAttribute;
//-----------------------------------------------------------------------------
// Declaration of type variable functions.
//-----------------------------------------------------------------------------
static udt_ObjectType *ObjectType_New(udt_Connection*, OCIParam*, ub4);
static void ObjectType_Free(udt_ObjectType*);
static PyObject *ObjectType_Repr(udt_ObjectType*);
static udt_ObjectAttribute *ObjectAttribute_New(udt_Connection*, OCIParam*);
static void ObjectAttribute_Free(udt_ObjectAttribute*);
static PyObject *ObjectAttribute_Repr(udt_ObjectAttribute*);
//-----------------------------------------------------------------------------
// declaration of members for Python type "ObjectType"
//-----------------------------------------------------------------------------
static PyMemberDef g_ObjectTypeMembers[] = {
{ "schema", T_OBJECT, offsetof(udt_ObjectType, schema), READONLY },
{ "name", T_OBJECT, offsetof(udt_ObjectType, name), READONLY },
{ "attributes", T_OBJECT, offsetof(udt_ObjectType, attributes), READONLY },
{ NULL }
};
//-----------------------------------------------------------------------------
// declaration of members for Python type "ObjectAttribute"
//-----------------------------------------------------------------------------
static PyMemberDef g_ObjectAttributeMembers[] = {
{ "name", T_OBJECT, offsetof(udt_ObjectAttribute, name), READONLY },
{ NULL }
};
//-----------------------------------------------------------------------------
// Python type declarations
//-----------------------------------------------------------------------------
static PyTypeObject g_ObjectTypeType = {
PyVarObject_HEAD_INIT(NULL, 0)
"cx_Oracle.ObjectType", // tp_name
sizeof(udt_ObjectType), // tp_basicsize
0, // tp_itemsize
(destructor) ObjectType_Free, // tp_dealloc
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_compare
(reprfunc) ObjectType_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
0, // tp_methods
g_ObjectTypeMembers, // tp_members
0, // tp_getset
0, // tp_base
0, // tp_dict
0, // tp_descr_get
0, // tp_descr_set
0, // tp_dictoffset
0, // tp_init
0, // tp_alloc
0, // tp_new
0, // tp_free
0, // tp_is_gc
0 // tp_bases
};
static PyTypeObject g_ObjectAttributeType = {
PyVarObject_HEAD_INIT(NULL, 0)
"cx_Oracle.ObjectAttribute", // tp_name
sizeof(udt_ObjectAttribute), // tp_basicsize
0, // tp_itemsize
(destructor) ObjectAttribute_Free, // tp_dealloc
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_compare
(reprfunc) ObjectAttribute_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
0, // tp_methods
g_ObjectAttributeMembers, // tp_members
0, // tp_getset
0, // tp_base
0, // tp_dict
0, // tp_descr_get
0, // tp_descr_set
0, // tp_dictoffset
0, // tp_init
0, // tp_alloc
0, // tp_new
0, // tp_free
0, // tp_is_gc
0 // tp_bases
};
//-----------------------------------------------------------------------------
// ObjectType_Describe()
// Describe the type and store information about it as needed.
//-----------------------------------------------------------------------------
static int ObjectType_Describe(
udt_ObjectType *self, // type to populate
udt_Connection *connection, // connection for type information
OCIDescribe *describeHandle) // describe handle
{
OCIParam *topLevelParam, *attributeListParam, *attributeParam;
udt_ObjectAttribute *attribute;
OCIParam *collectionParam;
OCITypeCode typeCode;
ub2 numAttributes;
sword status;
int i;
// describe the type
status = OCIDescribeAny(connection->handle, self->environment->errorHandle,
(dvoid*) self->tdo, 0, OCI_OTYPE_PTR, OCI_DEFAULT, OCI_PTYPE_TYPE,
describeHandle);
if (Environment_CheckForError(self->environment, status,
"ObjectType_Describe(): describe type") < 0)
return -1;
// get top level parameter descriptor
status = OCIAttrGet(describeHandle, OCI_HTYPE_DESCRIBE, &topLevelParam, 0,
OCI_ATTR_PARAM, self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"ObjectType_Describe(): get top level parameter descriptor") < 0)
return -1;
// determine type of type
status = OCIAttrGet(topLevelParam, OCI_DTYPE_PARAM, &typeCode, 0,
OCI_ATTR_TYPECODE, self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"ObjectType_Describe(): get type code") < 0)
return -1;
// if a collection, need to determine the sub type
if (typeCode == OCI_TYPECODE_NAMEDCOLLECTION) {
self->isCollection = 1;
// determine type of collection
status = OCIAttrGet(topLevelParam, OCI_DTYPE_PARAM,
&self->collectionTypeCode, 0, OCI_ATTR_COLLECTION_TYPECODE,
self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"ObjectType_Describe(): get collection type code") < 0)
return -1;
// acquire collection parameter descriptor
status = OCIAttrGet(topLevelParam, OCI_DTYPE_PARAM, &collectionParam,
0, OCI_ATTR_COLLECTION_ELEMENT,
self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"ObjectType_Describe(): get collection descriptor") < 0)
return -1;
// determine type of element
status = OCIAttrGet(collectionParam, OCI_DTYPE_PARAM,
&self->elementTypeCode, 0, OCI_ATTR_TYPECODE,
self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"ObjectType_Describe(): get element type code") < 0)
return -1;
// if element type is an object type get its type
if (self->elementTypeCode == OCI_TYPECODE_OBJECT) {
self->elementType = (PyObject*)
ObjectType_New(connection, collectionParam,
OCI_ATTR_TYPE_NAME);
if (!self->elementType)
return -1;
}
}
// determine the number of attributes
status = OCIAttrGet(topLevelParam, OCI_DTYPE_PARAM,
(dvoid*) &numAttributes, 0, OCI_ATTR_NUM_TYPE_ATTRS,
self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"ObjectType_Describe(): get number of attributes") < 0)
return -1;
// allocate the attribute list and dictionary
self->attributes = PyList_New(numAttributes);
if (!self->attributes)
return -1;
self->attributesByName = PyDict_New();
if (!self->attributesByName)
return -1;
// acquire the list parameter descriptor
status = OCIAttrGet(topLevelParam, OCI_DTYPE_PARAM,
(dvoid*) &attributeListParam, 0, OCI_ATTR_LIST_TYPE_ATTRS,
self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"ObjectType_Describe(): get list parameter descriptor") < 0)
return -1;
// create attribute information for each attribute
for (i = 0; i < numAttributes; i++) {
status = OCIParamGet(attributeListParam, OCI_DTYPE_PARAM,
self->environment->errorHandle, (dvoid**) &attributeParam,
(ub4) i + 1);
if (Environment_CheckForError(self->environment, status,
"ObjectType_Describe(): get attribute param descriptor") < 0)
return -1;
attribute = ObjectAttribute_New(connection, attributeParam);
if (!attribute)
return -1;
PyList_SET_ITEM(self->attributes, i, (PyObject*) attribute);
if (PyDict_SetItem(self->attributesByName, attribute->name,
(PyObject*) attribute) < 0)
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
// ObjectType_Initialize()
// Initialize the object type with the information that is required.
//-----------------------------------------------------------------------------
static int ObjectType_Initialize(
udt_ObjectType *self, // type to initialize
udt_Connection *connection, // connection for type information
OCIParam *param, // parameter descriptor
ub4 nameAttribute) // value for the name attribute
{
OCIDescribe *describeHandle;
OCIRef *tdoReference;
sword status;
char *name;
ub4 size;
// determine the schema of the type
status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE, (dvoid*) &name, &size,
OCI_ATTR_SCHEMA_NAME, self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"ObjectType_Initialize(): get schema name") < 0)
return -1;
self->schema = cxString_FromEncodedString(name, size,
self->environment->encoding);
if (!self->schema)
return -1;
// determine the name of the type
status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE, (dvoid*) &name, &size,
nameAttribute, self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"ObjectType_Initialize(): get name") < 0)
return -1;
self->name = cxString_FromEncodedString(name, size,
self->environment->encoding);
if (!self->name)
return -1;
// retrieve TDO of the parameter
status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE, (dvoid*) &tdoReference, 0,
OCI_ATTR_REF_TDO, self->environment->errorHandle);
if (Environment_CheckForError(self->environment, status,
"ObjectType_Initialize(): get TDO reference") < 0)
return -1;
status = OCIObjectPin(self->environment->handle,
self->environment->errorHandle, tdoReference, NULL, OCI_PIN_ANY,
OCI_DURATION_SESSION, OCI_LOCK_NONE, (dvoid**) &self->tdo);
if (Environment_CheckForError(self->environment, status,
"ObjectType_Initialize(): pin TDO reference") < 0)
return -1;
// acquire a describe handle
status = OCIHandleAlloc(self->environment->handle,
(dvoid**) &describeHandle, OCI_HTYPE_DESCRIBE, 0, 0);
if (Environment_CheckForError(self->environment, status,
"ObjectType_Initialize(): allocate describe handle") < 0)
return -1;
// describe the type
if (ObjectType_Describe(self, connection, describeHandle) < 0)
return -1;
// free the describe handle
status = OCIHandleFree(describeHandle, OCI_HTYPE_DESCRIBE);
if (Environment_CheckForError(self->environment, status,
"ObjectType_Initialize(): free describe handle") < 0)
return -1;
return 0;
}
//-----------------------------------------------------------------------------
// ObjectType_New()
// Allocate a new object type.
//-----------------------------------------------------------------------------
static udt_ObjectType *ObjectType_New(
udt_Connection *connection, // connection for type information
OCIParam *param, // parameter descriptor
ub4 nameAttribute) // value for the name attribute
{
udt_ObjectType *self;
self = (udt_ObjectType*) g_ObjectTypeType.tp_alloc(&g_ObjectTypeType, 0);
if (!self)
return NULL;
Py_INCREF(connection->environment);
self->environment = connection->environment;
self->tdo = NULL;
self->schema = NULL;
self->name = NULL;
self->attributes = NULL;
self->attributesByName = NULL;
self->elementType = NULL;
self->isCollection = 0;
if (ObjectType_Initialize(self, connection, param, nameAttribute) < 0) {
Py_DECREF(self);
return NULL;
}
return self;
}
//-----------------------------------------------------------------------------
// ObjectType_NewByName()
// Create a new object type given its name.
//-----------------------------------------------------------------------------
static udt_ObjectType *ObjectType_NewByName(
udt_Connection *connection, // connection for type information
PyObject *name) // name of object type to describe
{
OCIDescribe *describeHandle;
udt_ObjectType *result;
udt_Buffer buffer;
OCIParam *param;
sword status;
// allocate describe handle
status = OCIHandleAlloc(connection->environment->handle,
(dvoid**) &describeHandle, OCI_HTYPE_DESCRIBE, 0, 0);
if (Environment_CheckForError(connection->environment, status,
"ObjectType_NewByName(): allocate describe handle") < 0)
return NULL;
// describe the object
if (cxBuffer_FromObject(&buffer, name,
connection->environment->encoding) < 0) {
OCIHandleFree(describeHandle, OCI_HTYPE_DESCRIBE);
return NULL;
}
status = OCIDescribeAny(connection->handle,
connection->environment->errorHandle, (dvoid*) buffer.ptr,
buffer.size, OCI_OTYPE_NAME, 0, OCI_PTYPE_TYPE, describeHandle);
cxBuffer_Clear(&buffer);
if (Environment_CheckForError(connection->environment, status,
"ObjectType_NewByName(): describe type") < 0) {
OCIHandleFree(describeHandle, OCI_HTYPE_DESCRIBE);
return NULL;
}
// get the parameter handle
status = OCIAttrGet(describeHandle, OCI_HTYPE_DESCRIBE, ¶m, 0,
OCI_ATTR_PARAM, connection->environment->errorHandle);
if (Environment_CheckForError(connection->environment, status,
"ObjectType_NewByName(): get parameter handle") < 0) {
OCIHandleFree(describeHandle, OCI_HTYPE_DESCRIBE);
return NULL;
}
// get object type
result = ObjectType_New(connection, param, OCI_ATTR_NAME);
if (!result) {
OCIHandleFree(describeHandle, OCI_HTYPE_DESCRIBE);
return NULL;
}
// free the describe handle
status = OCIHandleFree(describeHandle, OCI_HTYPE_DESCRIBE);
if (Environment_CheckForError(connection->environment, status,
"ObjectType_NewByName(): free describe handle") < 0)
return NULL;
return result;
}
//-----------------------------------------------------------------------------
// ObjectType_Free()
// Free the memory associated with an object type.
//-----------------------------------------------------------------------------
static void ObjectType_Free(
udt_ObjectType *self) // object type to free
{
if (self->tdo)
OCIObjectUnpin(self->environment->handle,
self->environment->errorHandle, self->tdo);
Py_CLEAR(self->environment);
Py_CLEAR(self->schema);
Py_CLEAR(self->name);
Py_CLEAR(self->attributes);
Py_CLEAR(self->attributesByName);
Py_CLEAR(self->elementType);
Py_TYPE(self)->tp_free((PyObject*) self);
}
//-----------------------------------------------------------------------------
// ObjectType_Repr()
// Return a string representation of the object type.
//-----------------------------------------------------------------------------
static PyObject *ObjectType_Repr(
udt_ObjectType *self) // object type to return the string for
{
PyObject *module, *name, *result, *format, *formatArgs;
if (GetModuleAndName(Py_TYPE(self), &module, &name) < 0)
return NULL;
format = cxString_FromAscii("<%s.%s %s.%s>");
if (!format) {
Py_DECREF(module);
Py_DECREF(name);
return NULL;
}
formatArgs = PyTuple_Pack(4, module, name, self->schema, self->name);
Py_DECREF(module);
Py_DECREF(name);
if (!formatArgs) {
Py_DECREF(format);
return NULL;
}
result = cxString_Format(format, formatArgs);
Py_DECREF(format);
Py_DECREF(formatArgs);
return result;
}
//-----------------------------------------------------------------------------
// ObjectAttribute_Initialize()
// Initialize the new object attribute.
//-----------------------------------------------------------------------------
static int ObjectAttribute_Initialize(
udt_ObjectAttribute *self, // object attribute to initialize
udt_Connection *connection, // connection in use
OCIParam *param) // parameter descriptor
{
sword status;
char *name;
ub4 size;
// determine the name of the attribute
status = OCIAttrGet(param, OCI_DTYPE_PARAM, (dvoid*) &name, &size,
OCI_ATTR_NAME, connection->environment->errorHandle);
if (Environment_CheckForError(connection->environment, status,
"ObjectAttribute_Initialize(): get name") < 0)
return -1;
self->name = cxString_FromEncodedString(name, size,
connection->environment->encoding);
if (!self->name)
return -1;
// determine the type of the attribute
status = OCIAttrGet(param, OCI_DTYPE_PARAM, (dvoid*) &self->typeCode, 0,
OCI_ATTR_TYPECODE, connection->environment->errorHandle);
if (Environment_CheckForError(connection->environment, status,
"ObjectAttribute_Initialize(): get type code") < 0)
return -1;
// if the type of the attribute is object, recurse
switch (self->typeCode) {
case OCI_TYPECODE_NAMEDCOLLECTION:
case OCI_TYPECODE_OBJECT:
self->subType = ObjectType_New(connection, param,
OCI_ATTR_TYPE_NAME);
if (!self->subType)
return -1;
break;
};
return 0;
}
//-----------------------------------------------------------------------------
// ObjectAttribute_New()
// Allocate a new object attribute.
//-----------------------------------------------------------------------------
static udt_ObjectAttribute *ObjectAttribute_New(
udt_Connection *connection, // connection information
OCIParam *param) // parameter descriptor
{
udt_ObjectAttribute *self;
self = (udt_ObjectAttribute*)
g_ObjectAttributeType.tp_alloc(&g_ObjectAttributeType, 0);
if (!self)
return NULL;
self->name = NULL;
self->subType = NULL;
if (ObjectAttribute_Initialize(self, connection, param) < 0) {
Py_DECREF(self);
return NULL;
}
return self;
}
//-----------------------------------------------------------------------------
// ObjectAttribute_Free()
// Free the memory associated with an object attribute.
//-----------------------------------------------------------------------------
static void ObjectAttribute_Free(
udt_ObjectAttribute *self) // object attribute to free
{
Py_CLEAR(self->name);
Py_CLEAR(self->subType);
Py_TYPE(self)->tp_free((PyObject*) self);
}
//-----------------------------------------------------------------------------
// ObjectAttribute_Repr()
// Return a string representation of the object attribute.
//-----------------------------------------------------------------------------
static PyObject *ObjectAttribute_Repr(
udt_ObjectAttribute *self) // attribute to return the string for
{
PyObject *module, *name, *result, *format, *formatArgs;
if (GetModuleAndName(Py_TYPE(self), &module, &name) < 0)
return NULL;
format = cxString_FromAscii("<%s.%s %s>");
if (!format) {
Py_DECREF(module);
Py_DECREF(name);
return NULL;
}
formatArgs = PyTuple_Pack(3, module, name, self->name);
Py_DECREF(module);
Py_DECREF(name);
if (!formatArgs) {
Py_DECREF(format);
return NULL;
}
result = cxString_Format(format, formatArgs);
Py_DECREF(format);
Py_DECREF(formatArgs);
return result;
}