Codebase list python-cx-oracle / f6f53092-631a-4f23-a503-f9ea6dae6db6/main src / cxoObject.c
f6f53092-631a-4f23-a503-f9ea6dae6db6/main

Tree @f6f53092-631a-4f23-a503-f9ea6dae6db6/main (Download .tar.gz)

cxoObject.c @f6f53092-631a-4f23-a503-f9ea6dae6db6/mainraw · history · blame

  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
//-----------------------------------------------------------------------------
// Copyright (c) 2016, 2018, 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.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// cxoObject.c
//   Defines the routines for handling objects in Oracle.
//-----------------------------------------------------------------------------

#include "cxoModule.h"

//-----------------------------------------------------------------------------
// functions for the Python type "Object"
//-----------------------------------------------------------------------------
static void cxoObject_free(cxoObject*);
static PyObject *cxoObject_getAttr(cxoObject*, PyObject*);
static PyObject *cxoObject_repr(cxoObject*);
static int cxoObject_setAttr(cxoObject*, PyObject*, PyObject*);
static PyObject *cxoObject_append(cxoObject*, PyObject*);
static PyObject *cxoObject_asDict(cxoObject*, PyObject*);
static PyObject *cxoObject_asList(cxoObject*, PyObject*);
static PyObject *cxoObject_copy(cxoObject*, PyObject*);
static PyObject *cxoObject_delete(cxoObject*, PyObject*);
static PyObject *cxoObject_exists(cxoObject*, PyObject*);
static PyObject *cxoObject_extend(cxoObject*, PyObject*);
static PyObject *cxoObject_getElement(cxoObject*, PyObject*);
static PyObject *cxoObject_getFirstIndex(cxoObject*, PyObject*);
static PyObject *cxoObject_getLastIndex(cxoObject*, PyObject*);
static PyObject *cxoObject_getNextIndex(cxoObject*, PyObject*);
static PyObject *cxoObject_getPrevIndex(cxoObject*, PyObject*);
static PyObject *cxoObject_getSize(cxoObject*, PyObject*);
static PyObject *cxoObject_setElement(cxoObject*, PyObject*);
static PyObject *cxoObject_trim(cxoObject*, PyObject*);


//-----------------------------------------------------------------------------
// declaration of methods for Python type "Object"
//-----------------------------------------------------------------------------
static PyMethodDef cxoObjectMethods[] = {
    { "append", (PyCFunction) cxoObject_append, METH_O },
    { "asdict", (PyCFunction) cxoObject_asDict, METH_NOARGS },
    { "aslist", (PyCFunction) cxoObject_asList, METH_NOARGS },
    { "copy", (PyCFunction) cxoObject_copy, METH_NOARGS },
    { "delete", (PyCFunction) cxoObject_delete, METH_VARARGS },
    { "exists", (PyCFunction) cxoObject_exists, METH_VARARGS },
    { "extend", (PyCFunction) cxoObject_extend, METH_O },
    { "first", (PyCFunction) cxoObject_getFirstIndex, METH_NOARGS },
    { "getelement", (PyCFunction) cxoObject_getElement, METH_VARARGS },
    { "last", (PyCFunction) cxoObject_getLastIndex, METH_NOARGS },
    { "next", (PyCFunction) cxoObject_getNextIndex, METH_VARARGS },
    { "prev", (PyCFunction) cxoObject_getPrevIndex, METH_VARARGS },
    { "setelement", (PyCFunction) cxoObject_setElement, METH_VARARGS },
    { "size", (PyCFunction) cxoObject_getSize, METH_NOARGS },
    { "trim", (PyCFunction) cxoObject_trim, METH_VARARGS },
    { NULL, NULL }
};


//-----------------------------------------------------------------------------
// Declaration of members for Python type "Object".
//-----------------------------------------------------------------------------
static PyMemberDef cxoObjectMembers[] = {
    { "type", T_OBJECT, offsetof(cxoObject, objectType), READONLY },
    { NULL }
};


//-----------------------------------------------------------------------------
// Python type declaration
//-----------------------------------------------------------------------------
PyTypeObject cxoPyTypeObject = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "cx_Oracle.Object",                 // tp_name
    sizeof(cxoObject),                  // tp_basicsize
    0,                                  // tp_itemsize
    (destructor) cxoObject_free,        // tp_dealloc
    0,                                  // tp_print
    0,                                  // tp_getattr
    0,                                  // tp_setattr
    0,                                  // tp_compare
    (reprfunc) cxoObject_repr,          // tp_repr
    0,                                  // tp_as_number
    0,                                  // tp_as_sequence
    0,                                  // tp_as_mapping
    0,                                  // tp_hash
    0,                                  // tp_call
    0,                                  // tp_str
    (getattrofunc) cxoObject_getAttr,   // tp_getattro
    (setattrofunc) cxoObject_setAttr,   // 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
    cxoObjectMethods,                   // tp_methods
    cxoObjectMembers                    // tp_members
};


//-----------------------------------------------------------------------------
// cxoObject_new()
//   Create a new object.
//-----------------------------------------------------------------------------
PyObject *cxoObject_new(cxoObjectType *objectType, dpiObject *handle)
{
    cxoObject *obj;

    obj = (cxoObject*) cxoPyTypeObject.tp_alloc(&cxoPyTypeObject, 0);
    if (!obj)
        return NULL;
    Py_INCREF(objectType);
    obj->objectType = objectType;
    obj->handle = handle;
    return (PyObject*) obj;
}


//-----------------------------------------------------------------------------
// cxoObject_free()
//   Free an object.
//-----------------------------------------------------------------------------
static void cxoObject_free(cxoObject *obj)
{
    if (obj->handle) {
        dpiObject_release(obj->handle);
        obj->handle = NULL;
    }
    Py_CLEAR(obj->objectType);
    Py_TYPE(obj)->tp_free((PyObject*) obj);
}


//-----------------------------------------------------------------------------
// cxoObject_repr()
//   Return a string representation of the object.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_repr(cxoObject *obj)
{
    PyObject *module, *name, *result;

    if (cxoUtils_getModuleAndName(Py_TYPE(obj), &module, &name) < 0)
        return NULL;
    result = cxoUtils_formatString("<%s.%s %s.%s at %#x>",
            Py_BuildValue("(OOOOl)", module, name, obj->objectType->schema,
                    obj->objectType->name, obj));
    Py_DECREF(module);
    Py_DECREF(name);
    return result;
}


//-----------------------------------------------------------------------------
// cxoObject_convertFromPython()
//   Convert a Python value to an Oracle value.
//-----------------------------------------------------------------------------
static int cxoObject_convertFromPython(cxoObject *obj, PyObject *value,
        cxoTransformNum transformNum, dpiNativeTypeNum *nativeTypeNum,
        dpiData *data, cxoBuffer *buffer)
{
    dpiOracleTypeNum oracleTypeNum;

    // None is treated as null
    if (value == Py_None) {
        data->isNull = 1;
        return 0;
    }

    // convert the different Python types
    cxoTransform_getTypeInfo(transformNum, &oracleTypeNum, nativeTypeNum);
    if (cxoTransform_fromPython(transformNum, value, &data->value, buffer,
            obj->objectType->connection->encodingInfo.encoding,
            obj->objectType->connection->encodingInfo.nencoding, NULL, 0) < 0)
        return -1;
    data->isNull = 0;
    return 0;
}


//-----------------------------------------------------------------------------
// cxoObject_convertToPython()
//   Convert an Oracle value to a Python value.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_convertToPython(cxoObject *obj,
        cxoTransformNum transformNum, dpiData *data, cxoObjectType *objType)
{
    if (data->isNull)
        Py_RETURN_NONE;
    return cxoTransform_toPython(transformNum, obj->objectType->connection,
            objType, &data->value, NULL);
}


//-----------------------------------------------------------------------------
// cxoObject_getAttributeValue()
//   Retrieve an attribute on the object.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_getAttributeValue(cxoObject *obj,
        cxoObjectAttr *attribute)
{
    char numberAsStringBuffer[200], message[120];
    dpiOracleTypeNum oracleTypeNum;
    dpiNativeTypeNum nativeTypeNum;
    dpiData data;

    if (attribute->transformNum == CXO_TRANSFORM_UNSUPPORTED) {
        snprintf(message, sizeof(message), "Oracle type %d not supported.",
                attribute->oracleTypeNum);
        return cxoError_raiseFromString(cxoNotSupportedErrorException,
                message);
    }
    cxoTransform_getTypeInfo(attribute->transformNum, &oracleTypeNum,
            &nativeTypeNum);
    if (oracleTypeNum == DPI_ORACLE_TYPE_NUMBER &&
            nativeTypeNum == DPI_NATIVE_TYPE_BYTES) {
        data.value.asBytes.ptr = numberAsStringBuffer;
        data.value.asBytes.length = sizeof(numberAsStringBuffer);
        data.value.asBytes.encoding = NULL;
    }
    if (dpiObject_getAttributeValue(obj->handle, attribute->handle,
            nativeTypeNum, &data) < 0)
        return cxoError_raiseAndReturnNull();
    return cxoObject_convertToPython(obj, attribute->transformNum, &data,
            attribute->type);
}


//-----------------------------------------------------------------------------
// cxoObject_setAttributeValue()
//   Set an attribute on the object.
//-----------------------------------------------------------------------------
static int cxoObject_setAttributeValue(cxoObject *obj,
        cxoObjectAttr *attribute, PyObject *value)
{
    dpiNativeTypeNum nativeTypeNum = 0;
    cxoBuffer buffer;
    dpiData data;
    int status;

    cxoBuffer_init(&buffer);
    if (cxoObject_convertFromPython(obj, value, attribute->transformNum,
            &nativeTypeNum, &data, &buffer) < 0)
        return -1;
    status = dpiObject_setAttributeValue(obj->handle, attribute->handle,
            nativeTypeNum, &data);
    cxoBuffer_clear(&buffer);
    if (status < 0)
        return cxoError_raiseAndReturnInt();
    return 0;
}


//-----------------------------------------------------------------------------
// cxoObject_getAttr()
//   Retrieve an attribute on an object.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_getAttr(cxoObject *obj, PyObject *nameObject)
{
    cxoObjectAttr *attribute;

    attribute = (cxoObjectAttr*)
            PyDict_GetItem(obj->objectType->attributesByName, nameObject);
    if (attribute)
        return cxoObject_getAttributeValue(obj, attribute);

    return PyObject_GenericGetAttr( (PyObject*) obj, nameObject);
}


//-----------------------------------------------------------------------------
// cxoObject_setAttr()
//   Set an attribute on an object.
//-----------------------------------------------------------------------------
static int cxoObject_setAttr(cxoObject *obj, PyObject *nameObject,
        PyObject *value)
{
    cxoObjectAttr *attribute;

    attribute = (cxoObjectAttr*)
            PyDict_GetItem(obj->objectType->attributesByName, nameObject);
    if (attribute)
        return cxoObject_setAttributeValue(obj, attribute, value);

    return PyObject_GenericSetAttr( (PyObject*) obj, nameObject, value);
}


//-----------------------------------------------------------------------------
// cxoObject_internalAppend()
//   Append an item to the collection.
//-----------------------------------------------------------------------------
static int cxoObject_internalAppend(cxoObject *obj, PyObject *value)
{
    dpiNativeTypeNum nativeTypeNum = 0;
    cxoBuffer buffer;
    dpiData data;
    int status;

    cxoBuffer_init(&buffer);
    if (cxoObject_convertFromPython(obj, value,
            obj->objectType->elementTransformNum, &nativeTypeNum, &data,
            &buffer) < 0)
        return -1;
    status = dpiObject_appendElement(obj->handle, nativeTypeNum, &data);
    cxoBuffer_clear(&buffer);
    if (status < 0)
        return cxoError_raiseAndReturnInt();
    return 0;
}


//-----------------------------------------------------------------------------
// cxoObject_internalExtend()
//   Extend the collection by appending each of the items in the sequence.
//-----------------------------------------------------------------------------
int cxoObject_internalExtend(cxoObject *obj, PyObject *sequence)
{
    PyObject *fastSequence, *element;
    Py_ssize_t size, i;

    fastSequence = PySequence_Fast(sequence, "expecting sequence");
    if (!fastSequence)
        return -1;
    size = PySequence_Fast_GET_SIZE(fastSequence);
    for (i = 0; i < size; i++) {
        element = PySequence_Fast_GET_ITEM(fastSequence, i);
        if (cxoObject_internalAppend(obj, element) < 0) {
            Py_DECREF(fastSequence);
            return -1;
        }
    }
    Py_DECREF(fastSequence);
    
    return 0;
}


//-----------------------------------------------------------------------------
// cxoObject_append()
//   Append an item to the collection.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_append(cxoObject *obj, PyObject *value)
{
    if (cxoObject_internalAppend(obj, value) < 0)
        return NULL;
    Py_RETURN_NONE;
}


//-----------------------------------------------------------------------------
// cxoObject_internalGetElementByIndex()
//   Internal method used for getting an element value for a particular index.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_internalGetElementByIndex(cxoObject *obj,
        int32_t index)
{
    char numberAsStringBuffer[200], message[120];
    dpiOracleTypeNum oracleTypeNum;
    dpiNativeTypeNum nativeTypeNum;
    dpiData data;

    if (obj->objectType->elementTransformNum == CXO_TRANSFORM_UNSUPPORTED) {
        snprintf(message, sizeof(message), "Oracle type %d not supported.",
                obj->objectType->elementOracleTypeNum);
        return cxoError_raiseFromString(cxoNotSupportedErrorException,
                message);
    }
    cxoTransform_getTypeInfo(obj->objectType->elementTransformNum,
            &oracleTypeNum, &nativeTypeNum);
    if (oracleTypeNum == DPI_ORACLE_TYPE_NUMBER &&
            nativeTypeNum == DPI_NATIVE_TYPE_BYTES) {
        data.value.asBytes.ptr = numberAsStringBuffer;
        data.value.asBytes.length = sizeof(numberAsStringBuffer);
        data.value.asBytes.encoding = NULL;
    }
    if (dpiObject_getElementValueByIndex(obj->handle, index, nativeTypeNum,
                &data) < 0)
        return cxoError_raiseAndReturnNull();
    return cxoObject_convertToPython(obj, obj->objectType->elementTransformNum,
            &data, (cxoObjectType*) obj->objectType->elementType);
}


//-----------------------------------------------------------------------------
// cxoObject_asDict()
//   Returns a collection as a dictionary. If the object is not a collection,
// an error is returned.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_asDict(cxoObject *obj, PyObject *args)
{
    PyObject *dict, *key, *value;
    int32_t index, nextIndex;
    int exists;

    // create the result dictionary
    dict = PyDict_New();
    if (!dict)
        return NULL;

    // populate it with each of the elements in the collection
    if (dpiObject_getFirstIndex(obj->handle, &index, &exists) < 0) {
        Py_DECREF(dict);
        return cxoError_raiseAndReturnNull();
    }
    while (exists) {
        value = cxoObject_internalGetElementByIndex(obj, index);
        if (!value) {
            Py_DECREF(dict);
            return NULL;
        }
        key = PyInt_FromLong(index);
        if (!key) {
            Py_DECREF(value);
            Py_DECREF(dict);
            return NULL;
        }
        if (PyDict_SetItem(dict, key, value) < 0) {
            Py_DECREF(key);
            Py_DECREF(value);
            Py_DECREF(dict);
            return NULL;
        }
        Py_DECREF(key);
        Py_DECREF(value);
        if (dpiObject_getNextIndex(obj->handle, index, &nextIndex,
                &exists) < 0) {
            Py_DECREF(dict);
            return cxoError_raiseAndReturnNull();
        }
        index = nextIndex;
    }

    return dict;
}


//-----------------------------------------------------------------------------
// cxoObject_asList()
//   Returns a collection as a list of elements. If the object is not a
// collection, an error is returned.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_asList(cxoObject *obj, PyObject *args)
{
    PyObject *list, *elementValue;
    int32_t index, nextIndex;
    int exists;

    // create the result list
    list = PyList_New(0);
    if (!list)
        return NULL;

    // populate it with each of the elements in the list
    if (dpiObject_getFirstIndex(obj->handle, &index, &exists) < 0) {
        Py_DECREF(list);
        return cxoError_raiseAndReturnNull();
    }
    while (exists) {
        elementValue = cxoObject_internalGetElementByIndex(obj, index);
        if (!elementValue) {
            Py_DECREF(list);
            return NULL;
        }
        if (PyList_Append(list, elementValue) < 0) {
            Py_DECREF(elementValue);
            Py_DECREF(list);
            return NULL;
        }
        Py_DECREF(elementValue);
        if (dpiObject_getNextIndex(obj->handle, index, &nextIndex,
                &exists) < 0) {
            Py_DECREF(list);
            return cxoError_raiseAndReturnNull();
        }
        index = nextIndex;
    }

    return list;
}


//-----------------------------------------------------------------------------
// cxoObject_copy()
//   Return a copy of the object.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_copy(cxoObject *obj, PyObject *args)
{
    PyObject *copiedObj;
    dpiObject *handle;

    if (dpiObject_copy(obj->handle, &handle) < 0)
        return cxoError_raiseAndReturnNull();
    copiedObj = cxoObject_new(obj->objectType, handle);
    if (!copiedObj) {
        dpiObject_release(handle);
        return NULL;
    }
    return copiedObj;
}


//-----------------------------------------------------------------------------
// cxoObject_delete()
//   Delete the element at the specified index in the collection.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_delete(cxoObject *obj, PyObject *args)
{
    int32_t index;

    if (!PyArg_ParseTuple(args, "i", &index))
        return NULL;
    if (dpiObject_deleteElementByIndex(obj->handle, index) < 0)
        return cxoError_raiseAndReturnNull();
    Py_RETURN_NONE;
}


//-----------------------------------------------------------------------------
// cxoObject_exists()
//   Return true or false indicating if an element exists in the collection at
// the specified index.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_exists(cxoObject *obj, PyObject *args)
{
    int32_t index;
    int exists;

    if (!PyArg_ParseTuple(args, "i", &index))
        return NULL;
    if (dpiObject_getElementExistsByIndex(obj->handle, index, &exists) < 0)
        return cxoError_raiseAndReturnNull();
    if (exists)
        Py_RETURN_TRUE;
    Py_RETURN_FALSE;
}


//-----------------------------------------------------------------------------
// cxoObject_extend()
//   Extend the collection by appending each of the items in the sequence.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_extend(cxoObject *obj, PyObject *sequence)
{
    if (cxoObject_internalExtend(obj, sequence) < 0)
        return NULL;
    Py_RETURN_NONE;
}


//-----------------------------------------------------------------------------
// cxoObject_getElement()
//   Return the element at the given position in the collection.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_getElement(cxoObject *obj, PyObject *args)
{
    int32_t index;

    if (!PyArg_ParseTuple(args, "i", &index))
        return NULL;
    return cxoObject_internalGetElementByIndex(obj, index);
}


//-----------------------------------------------------------------------------
// cxoObject_getFirstIndex()
//   Return the index of the first entry in the collection.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_getFirstIndex(cxoObject *obj, PyObject *args)
{
    int32_t index;
    int exists;

    if (dpiObject_getFirstIndex(obj->handle, &index, &exists) < 0)
        return cxoError_raiseAndReturnNull();
    if (exists)
        return PyInt_FromLong(index);
    Py_RETURN_NONE;
}


//-----------------------------------------------------------------------------
// cxoObject_getLastIndex()
//   Return the index of the last entry in the collection.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_getLastIndex(cxoObject *obj, PyObject *args)
{
    int32_t index;
    int exists;

    if (dpiObject_getLastIndex(obj->handle, &index, &exists) < 0)
        return cxoError_raiseAndReturnNull();
    if (exists)
        return PyInt_FromLong(index);
    Py_RETURN_NONE;
}


//-----------------------------------------------------------------------------
// cxoObject_getNextIndex()
//   Return the index of the next entry in the collection following the index
// specified. If there is no next entry, None is returned.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_getNextIndex(cxoObject *obj, PyObject *args)
{
    int32_t index, nextIndex;
    int exists;

    if (!PyArg_ParseTuple(args, "i", &index))
        return NULL;
    if (dpiObject_getNextIndex(obj->handle, index, &nextIndex, &exists) < 0)
        return cxoError_raiseAndReturnNull();
    if (exists)
        return PyInt_FromLong(nextIndex);
    Py_RETURN_NONE;
}


//-----------------------------------------------------------------------------
// cxoObject_getPrevIndex()
//   Return the index of the previous entry in the collection preceding the
// index specified. If there is no previous entry, None is returned.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_getPrevIndex(cxoObject *obj, PyObject *args)
{
    int32_t index, prevIndex;
    int exists;

    if (!PyArg_ParseTuple(args, "i", &index))
        return NULL;
    if (dpiObject_getPrevIndex(obj->handle, index, &prevIndex, &exists) < 0)
        return cxoError_raiseAndReturnNull();
    if (exists)
        return PyInt_FromLong(prevIndex);
    Py_RETURN_NONE;
}


//-----------------------------------------------------------------------------
// cxoObject_getSize()
//   Return the size of a collection. If the object is not a collection, an
// error is returned.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_getSize(cxoObject *obj, PyObject *args)
{
    int32_t size;

    if (dpiObject_getSize(obj->handle, &size) < 0)
        return cxoError_raiseAndReturnNull();
    return PyInt_FromLong(size);
}


//-----------------------------------------------------------------------------
// cxoObject_setElement()
//   Set the element at the specified location to the given value.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_setElement(cxoObject *obj, PyObject *args)
{
    dpiNativeTypeNum nativeTypeNum = 0;
    cxoBuffer buffer;
    PyObject *value;
    int32_t index;
    dpiData data;
    int status;

    if (!PyArg_ParseTuple(args, "iO", &index, &value))
        return NULL;
    cxoBuffer_init(&buffer);
    if (cxoObject_convertFromPython(obj, value,
            obj->objectType->elementTransformNum, &nativeTypeNum, &data,
            &buffer) < 0)
        return NULL;
    status = dpiObject_setElementValueByIndex(obj->handle, index,
            nativeTypeNum, &data);
    cxoBuffer_clear(&buffer);
    if (status < 0)
        return cxoError_raiseAndReturnNull();
    Py_RETURN_NONE;
}


//-----------------------------------------------------------------------------
// cxoObject_trim()
//   Trim a number of elements from the end of the collection.
//-----------------------------------------------------------------------------
static PyObject *cxoObject_trim(cxoObject *obj, PyObject *args)
{
    int32_t numToTrim;

    if (!PyArg_ParseTuple(args, "i", &numToTrim))
        return NULL;
    if (dpiObject_trim(obj->handle, numToTrim) < 0)
        return cxoError_raiseAndReturnNull();
    Py_RETURN_NONE;
}