4 * src/pl/plpython/plpy_resultobject.c
10 #include "plpy_resultobject.h"
13 static void PLy_result_dealloc(PyObject
*arg
);
14 static PyObject
*PLy_result_colnames(PyObject
*self
, PyObject
*unused
);
15 static PyObject
*PLy_result_coltypes(PyObject
*self
, PyObject
*unused
);
16 static PyObject
*PLy_result_coltypmods(PyObject
*self
, PyObject
*unused
);
17 static PyObject
*PLy_result_nrows(PyObject
*self
, PyObject
*args
);
18 static PyObject
*PLy_result_status(PyObject
*self
, PyObject
*args
);
19 static Py_ssize_t
PLy_result_length(PyObject
*arg
);
20 static PyObject
*PLy_result_item(PyObject
*arg
, Py_ssize_t idx
);
21 static PyObject
*PLy_result_str(PyObject
*arg
);
22 static PyObject
*PLy_result_subscript(PyObject
*arg
, PyObject
*item
);
23 static int PLy_result_ass_subscript(PyObject
*arg
, PyObject
*item
, PyObject
*value
);
25 static char PLy_result_doc
[] = "Results of a PostgreSQL query";
27 static PySequenceMethods PLy_result_as_sequence
= {
28 .sq_length
= PLy_result_length
,
29 .sq_item
= PLy_result_item
,
32 static PyMappingMethods PLy_result_as_mapping
= {
33 .mp_length
= PLy_result_length
,
34 .mp_subscript
= PLy_result_subscript
,
35 .mp_ass_subscript
= PLy_result_ass_subscript
,
38 static PyMethodDef PLy_result_methods
[] = {
39 {"colnames", PLy_result_colnames
, METH_NOARGS
, NULL
},
40 {"coltypes", PLy_result_coltypes
, METH_NOARGS
, NULL
},
41 {"coltypmods", PLy_result_coltypmods
, METH_NOARGS
, NULL
},
42 {"nrows", PLy_result_nrows
, METH_VARARGS
, NULL
},
43 {"status", PLy_result_status
, METH_VARARGS
, NULL
},
47 static PyTypeObject PLy_ResultType
= {
48 PyVarObject_HEAD_INIT(NULL
, 0)
49 .tp_name
= "PLyResult",
50 .tp_basicsize
= sizeof(PLyResultObject
),
51 .tp_dealloc
= PLy_result_dealloc
,
52 .tp_as_sequence
= &PLy_result_as_sequence
,
53 .tp_as_mapping
= &PLy_result_as_mapping
,
54 .tp_str
= &PLy_result_str
,
55 .tp_flags
= Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
,
56 .tp_doc
= PLy_result_doc
,
57 .tp_methods
= PLy_result_methods
,
61 PLy_result_init_type(void)
63 if (PyType_Ready(&PLy_ResultType
) < 0)
64 elog(ERROR
, "could not initialize PLy_ResultType");
72 if ((ob
= PyObject_New(PLyResultObject
, &PLy_ResultType
)) == NULL
)
75 /* ob->tuples = NULL; */
79 ob
->nrows
= PyLong_FromLong(-1);
80 ob
->rows
= PyList_New(0);
88 return (PyObject
*) ob
;
92 PLy_result_dealloc(PyObject
*arg
)
94 PLyResultObject
*ob
= (PLyResultObject
*) arg
;
96 Py_XDECREF(ob
->nrows
);
98 Py_XDECREF(ob
->status
);
101 FreeTupleDesc(ob
->tupdesc
);
105 arg
->ob_type
->tp_free(arg
);
109 PLy_result_colnames(PyObject
*self
, PyObject
*unused
)
111 PLyResultObject
*ob
= (PLyResultObject
*) self
;
117 PLy_exception_set(PLy_exc_error
, "command did not produce a result set");
121 list
= PyList_New(ob
->tupdesc
->natts
);
124 for (i
= 0; i
< ob
->tupdesc
->natts
; i
++)
126 Form_pg_attribute attr
= TupleDescAttr(ob
->tupdesc
, i
);
128 PyList_SET_ITEM(list
, i
, PLyUnicode_FromString(NameStr(attr
->attname
)));
135 PLy_result_coltypes(PyObject
*self
, PyObject
*unused
)
137 PLyResultObject
*ob
= (PLyResultObject
*) self
;
143 PLy_exception_set(PLy_exc_error
, "command did not produce a result set");
147 list
= PyList_New(ob
->tupdesc
->natts
);
150 for (i
= 0; i
< ob
->tupdesc
->natts
; i
++)
152 Form_pg_attribute attr
= TupleDescAttr(ob
->tupdesc
, i
);
154 PyList_SET_ITEM(list
, i
, PyLong_FromLong(attr
->atttypid
));
161 PLy_result_coltypmods(PyObject
*self
, PyObject
*unused
)
163 PLyResultObject
*ob
= (PLyResultObject
*) self
;
169 PLy_exception_set(PLy_exc_error
, "command did not produce a result set");
173 list
= PyList_New(ob
->tupdesc
->natts
);
176 for (i
= 0; i
< ob
->tupdesc
->natts
; i
++)
178 Form_pg_attribute attr
= TupleDescAttr(ob
->tupdesc
, i
);
180 PyList_SET_ITEM(list
, i
, PyLong_FromLong(attr
->atttypmod
));
187 PLy_result_nrows(PyObject
*self
, PyObject
*args
)
189 PLyResultObject
*ob
= (PLyResultObject
*) self
;
191 Py_INCREF(ob
->nrows
);
196 PLy_result_status(PyObject
*self
, PyObject
*args
)
198 PLyResultObject
*ob
= (PLyResultObject
*) self
;
200 Py_INCREF(ob
->status
);
205 PLy_result_length(PyObject
*arg
)
207 PLyResultObject
*ob
= (PLyResultObject
*) arg
;
209 return PyList_Size(ob
->rows
);
213 PLy_result_item(PyObject
*arg
, Py_ssize_t idx
)
216 PLyResultObject
*ob
= (PLyResultObject
*) arg
;
218 rv
= PyList_GetItem(ob
->rows
, idx
);
225 PLy_result_str(PyObject
*arg
)
227 PLyResultObject
*ob
= (PLyResultObject
*) arg
;
229 return PyUnicode_FromFormat("<%s status=%S nrows=%S rows=%S>",
230 Py_TYPE(ob
)->tp_name
,
237 PLy_result_subscript(PyObject
*arg
, PyObject
*item
)
239 PLyResultObject
*ob
= (PLyResultObject
*) arg
;
241 return PyObject_GetItem(ob
->rows
, item
);
245 PLy_result_ass_subscript(PyObject
*arg
, PyObject
*item
, PyObject
*value
)
247 PLyResultObject
*ob
= (PLyResultObject
*) arg
;
249 return PyObject_SetItem(ob
->rows
, item
, value
);