|
|
|
@ -10,7 +10,7 @@ |
|
|
|
|
#include "plpy_resultobject.h" |
|
|
|
|
#include "plpython.h" |
|
|
|
|
|
|
|
|
|
static void PLy_result_dealloc(PyObject *arg); |
|
|
|
|
static void PLy_result_dealloc(PLyResultObject *self); |
|
|
|
|
static PyObject *PLy_result_colnames(PyObject *self, PyObject *unused); |
|
|
|
|
static PyObject *PLy_result_coltypes(PyObject *self, PyObject *unused); |
|
|
|
|
static PyObject *PLy_result_coltypmods(PyObject *self, PyObject *unused); |
|
|
|
@ -24,17 +24,6 @@ static int PLy_result_ass_subscript(PyObject *arg, PyObject *item, PyObject *val |
|
|
|
|
|
|
|
|
|
static char PLy_result_doc[] = "Results of a PostgreSQL query"; |
|
|
|
|
|
|
|
|
|
static PySequenceMethods PLy_result_as_sequence = { |
|
|
|
|
.sq_length = PLy_result_length, |
|
|
|
|
.sq_item = PLy_result_item, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
static PyMappingMethods PLy_result_as_mapping = { |
|
|
|
|
.mp_length = PLy_result_length, |
|
|
|
|
.mp_subscript = PLy_result_subscript, |
|
|
|
|
.mp_ass_subscript = PLy_result_ass_subscript, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
static PyMethodDef PLy_result_methods[] = { |
|
|
|
|
{"colnames", PLy_result_colnames, METH_NOARGS, NULL}, |
|
|
|
|
{"coltypes", PLy_result_coltypes, METH_NOARGS, NULL}, |
|
|
|
@ -44,23 +33,55 @@ static PyMethodDef PLy_result_methods[] = { |
|
|
|
|
{NULL, NULL, 0, NULL} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
static PyTypeObject PLy_ResultType = { |
|
|
|
|
PyVarObject_HEAD_INIT(NULL, 0) |
|
|
|
|
.tp_name = "PLyResult", |
|
|
|
|
.tp_basicsize = sizeof(PLyResultObject), |
|
|
|
|
.tp_dealloc = PLy_result_dealloc, |
|
|
|
|
.tp_as_sequence = &PLy_result_as_sequence, |
|
|
|
|
.tp_as_mapping = &PLy_result_as_mapping, |
|
|
|
|
.tp_str = &PLy_result_str, |
|
|
|
|
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
|
|
|
|
.tp_doc = PLy_result_doc, |
|
|
|
|
.tp_methods = PLy_result_methods, |
|
|
|
|
static PyType_Slot PLyResult_slots[] = |
|
|
|
|
{ |
|
|
|
|
{ |
|
|
|
|
Py_tp_dealloc, PLy_result_dealloc |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Py_sq_length, PLy_result_length |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Py_sq_item, PLy_result_item |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Py_mp_length, PLy_result_length |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Py_mp_subscript, PLy_result_subscript |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Py_mp_ass_subscript, PLy_result_ass_subscript |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Py_tp_str, PLy_result_str |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Py_tp_doc, (char *) PLy_result_doc |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Py_tp_methods, PLy_result_methods |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
0, NULL |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
static PyType_Spec PLyResult_spec = |
|
|
|
|
{ |
|
|
|
|
.name = "PLyResult", |
|
|
|
|
.basicsize = sizeof(PLyResultObject), |
|
|
|
|
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
|
|
|
|
.slots = PLyResult_slots, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
static PyTypeObject *PLy_ResultType; |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
PLy_result_init_type(void) |
|
|
|
|
{ |
|
|
|
|
if (PyType_Ready(&PLy_ResultType) < 0) |
|
|
|
|
PLy_ResultType = (PyTypeObject *) PyType_FromSpec(&PLyResult_spec); |
|
|
|
|
if (!PLy_ResultType) |
|
|
|
|
elog(ERROR, "could not initialize PLy_ResultType"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -69,7 +90,7 @@ PLy_result_new(void) |
|
|
|
|
{ |
|
|
|
|
PLyResultObject *ob; |
|
|
|
|
|
|
|
|
|
if ((ob = PyObject_New(PLyResultObject, &PLy_ResultType)) == NULL) |
|
|
|
|
if ((ob = PyObject_New(PLyResultObject, PLy_ResultType)) == NULL) |
|
|
|
|
return NULL; |
|
|
|
|
|
|
|
|
|
/* ob->tuples = NULL; */ |
|
|
|
@ -89,20 +110,21 @@ PLy_result_new(void) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void |
|
|
|
|
PLy_result_dealloc(PyObject *arg) |
|
|
|
|
PLy_result_dealloc(PLyResultObject *self) |
|
|
|
|
{ |
|
|
|
|
PLyResultObject *ob = (PLyResultObject *) arg; |
|
|
|
|
PyTypeObject *tp = Py_TYPE(self); |
|
|
|
|
|
|
|
|
|
Py_XDECREF(ob->nrows); |
|
|
|
|
Py_XDECREF(ob->rows); |
|
|
|
|
Py_XDECREF(ob->status); |
|
|
|
|
if (ob->tupdesc) |
|
|
|
|
Py_XDECREF(self->nrows); |
|
|
|
|
Py_XDECREF(self->rows); |
|
|
|
|
Py_XDECREF(self->status); |
|
|
|
|
if (self->tupdesc) |
|
|
|
|
{ |
|
|
|
|
FreeTupleDesc(ob->tupdesc); |
|
|
|
|
ob->tupdesc = NULL; |
|
|
|
|
FreeTupleDesc(self->tupdesc); |
|
|
|
|
self->tupdesc = NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
arg->ob_type->tp_free(arg); |
|
|
|
|
PyObject_Free(self); |
|
|
|
|
Py_DECREF(tp); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static PyObject * |
|
|
|
@ -125,7 +147,7 @@ PLy_result_colnames(PyObject *self, PyObject *unused) |
|
|
|
|
{ |
|
|
|
|
Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i); |
|
|
|
|
|
|
|
|
|
PyList_SET_ITEM(list, i, PLyUnicode_FromString(NameStr(attr->attname))); |
|
|
|
|
PyList_SetItem(list, i, PLyUnicode_FromString(NameStr(attr->attname))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return list; |
|
|
|
@ -151,7 +173,7 @@ PLy_result_coltypes(PyObject *self, PyObject *unused) |
|
|
|
|
{ |
|
|
|
|
Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i); |
|
|
|
|
|
|
|
|
|
PyList_SET_ITEM(list, i, PyLong_FromLong(attr->atttypid)); |
|
|
|
|
PyList_SetItem(list, i, PyLong_FromLong(attr->atttypid)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return list; |
|
|
|
@ -177,7 +199,7 @@ PLy_result_coltypmods(PyObject *self, PyObject *unused) |
|
|
|
|
{ |
|
|
|
|
Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i); |
|
|
|
|
|
|
|
|
|
PyList_SET_ITEM(list, i, PyLong_FromLong(attr->atttypmod)); |
|
|
|
|
PyList_SetItem(list, i, PyLong_FromLong(attr->atttypmod)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return list; |
|
|
|
@ -227,7 +249,7 @@ PLy_result_str(PyObject *arg) |
|
|
|
|
PLyResultObject *ob = (PLyResultObject *) arg; |
|
|
|
|
|
|
|
|
|
return PyUnicode_FromFormat("<%s status=%S nrows=%S rows=%S>", |
|
|
|
|
Py_TYPE(ob)->tp_name, |
|
|
|
|
"PLyResult", |
|
|
|
|
ob->status, |
|
|
|
|
ob->nrows, |
|
|
|
|
ob->rows); |
|
|
|
|