@@ -22,6 +22,7 @@ static PyObject *cxoObject_getAttr(cxoObject*, PyObject*);
2222static PyObject * cxoObject_repr (cxoObject * );
2323static int cxoObject_setAttr (cxoObject * , PyObject * , PyObject * );
2424static PyObject * cxoObject_append (cxoObject * , PyObject * );
25+ static PyObject * cxoObject_asDict (cxoObject * , PyObject * );
2526static PyObject * cxoObject_asList (cxoObject * , PyObject * );
2627static PyObject * cxoObject_copy (cxoObject * , PyObject * );
2728static PyObject * cxoObject_delete (cxoObject * , PyObject * );
@@ -42,6 +43,7 @@ static PyObject *cxoObject_trim(cxoObject*, PyObject*);
4243//-----------------------------------------------------------------------------
4344static PyMethodDef cxoObjectMethods [] = {
4445 { "append" , (PyCFunction ) cxoObject_append , METH_O },
46+ { "asdict" , (PyCFunction ) cxoObject_asDict , METH_NOARGS },
4547 { "aslist" , (PyCFunction ) cxoObject_asList , METH_NOARGS },
4648 { "copy" , (PyCFunction ) cxoObject_copy , METH_NOARGS },
4749 { "delete" , (PyCFunction ) cxoObject_delete , METH_VARARGS },
@@ -384,6 +386,59 @@ static PyObject *cxoObject_internalGetElementByIndex(cxoObject *obj,
384386}
385387
386388
389+ //-----------------------------------------------------------------------------
390+ // cxoObject_asDict()
391+ // Returns a collection as a dictionary. If the object is not a collection,
392+ // an error is returned.
393+ //-----------------------------------------------------------------------------
394+ static PyObject * cxoObject_asDict (cxoObject * obj , PyObject * args )
395+ {
396+ PyObject * dict , * key , * value ;
397+ int32_t index , nextIndex ;
398+ int exists ;
399+
400+ // create the result dictionary
401+ dict = PyDict_New ();
402+ if (!dict )
403+ return NULL ;
404+
405+ // populate it with each of the elements in the collection
406+ if (dpiObject_getFirstIndex (obj -> handle , & index , & exists ) < 0 ) {
407+ Py_DECREF (dict );
408+ return cxoError_raiseAndReturnNull ();
409+ }
410+ while (exists ) {
411+ value = cxoObject_internalGetElementByIndex (obj , index );
412+ if (!value ) {
413+ Py_DECREF (dict );
414+ return NULL ;
415+ }
416+ key = PyInt_FromLong (index );
417+ if (!key ) {
418+ Py_DECREF (value );
419+ Py_DECREF (dict );
420+ return NULL ;
421+ }
422+ if (PyDict_SetItem (dict , key , value ) < 0 ) {
423+ Py_DECREF (key );
424+ Py_DECREF (value );
425+ Py_DECREF (dict );
426+ return NULL ;
427+ }
428+ Py_DECREF (key );
429+ Py_DECREF (value );
430+ if (dpiObject_getNextIndex (obj -> handle , index , & nextIndex ,
431+ & exists ) < 0 ) {
432+ Py_DECREF (dict );
433+ return cxoError_raiseAndReturnNull ();
434+ }
435+ index = nextIndex ;
436+ }
437+
438+ return dict ;
439+ }
440+
441+
387442//-----------------------------------------------------------------------------
388443// cxoObject_asList()
389444// Returns a collection as a list of elements. If the object is not a
0 commit comments