PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
plpy_resultobject.c File Reference
#include "postgres.h"
#include "plpython.h"
#include "plpy_resultobject.h"
#include "plpy_elog.h"
Include dependency graph for plpy_resultobject.c:

Go to the source code of this file.

Functions

static void PLy_result_dealloc (PyObject *arg)
 
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)
 
static PyObject * PLy_result_nrows (PyObject *self, PyObject *args)
 
static PyObject * PLy_result_status (PyObject *self, PyObject *args)
 
static Py_ssize_t PLy_result_length (PyObject *arg)
 
static PyObject * PLy_result_item (PyObject *arg, Py_ssize_t idx)
 
static PyObject * PLy_result_slice (PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx)
 
static int PLy_result_ass_slice (PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx, PyObject *slice)
 
static PyObject * PLy_result_str (PyObject *arg)
 
static PyObject * PLy_result_subscript (PyObject *arg, PyObject *item)
 
static int PLy_result_ass_subscript (PyObject *self, PyObject *item, PyObject *value)
 
void PLy_result_init_type (void)
 
PyObject * PLy_result_new (void)
 

Variables

static char PLy_result_doc []
 
static PySequenceMethods PLy_result_as_sequence
 
static PyMappingMethods PLy_result_as_mapping
 
static PyMethodDef PLy_result_methods []
 
static PyTypeObject PLy_ResultType
 

Function Documentation

static int PLy_result_ass_slice ( PyObject *  arg,
Py_ssize_t  lidx,
Py_ssize_t  hidx,
PyObject *  slice 
)
static

Definition at line 255 of file plpy_resultobject.c.

References PLyResultObject::rows.

256 {
257  int rv;
259 
260  rv = PyList_SetSlice(ob->rows, lidx, hidx, slice);
261  return rv;
262 }
void * arg
static int PLy_result_ass_subscript ( PyObject *  self,
PyObject *  item,
PyObject *  value 
)
static

Definition at line 293 of file plpy_resultobject.c.

References PLyResultObject::rows.

294 {
296 
297  return PyObject_SetItem(ob->rows, item, value);
298 }
static struct @121 value
void * arg
static PyObject * PLy_result_colnames ( PyObject *  self,
PyObject *  unused 
)
static

Definition at line 137 of file plpy_resultobject.c.

References i, sort-test::list, NameStr, tupleDesc::natts, PLy_exc_error, PLy_exception_set(), PLyResultObject::tupdesc, and TupleDescAttr.

138 {
139  PLyResultObject *ob = (PLyResultObject *) self;
140  PyObject *list;
141  int i;
142 
143  if (!ob->tupdesc)
144  {
145  PLy_exception_set(PLy_exc_error, "command did not produce a result set");
146  return NULL;
147  }
148 
149  list = PyList_New(ob->tupdesc->natts);
150  for (i = 0; i < ob->tupdesc->natts; i++)
151  {
152  Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i);
153 
154  PyList_SET_ITEM(list, i, PyString_FromString(NameStr(attr->attname)));
155  }
156 
157  return list;
158 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
PyObject * PLy_exc_error
Definition: plpy_elog.c:19
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:500
int natts
Definition: tupdesc.h:73
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
tuple list
Definition: sort-test.py:11
int i
#define NameStr(name)
Definition: c.h:493
static PyObject * PLy_result_coltypes ( PyObject *  self,
PyObject *  unused 
)
static

Definition at line 161 of file plpy_resultobject.c.

References i, sort-test::list, tupleDesc::natts, PLy_exc_error, PLy_exception_set(), PLyResultObject::tupdesc, and TupleDescAttr.

162 {
163  PLyResultObject *ob = (PLyResultObject *) self;
164  PyObject *list;
165  int i;
166 
167  if (!ob->tupdesc)
168  {
169  PLy_exception_set(PLy_exc_error, "command did not produce a result set");
170  return NULL;
171  }
172 
173  list = PyList_New(ob->tupdesc->natts);
174  for (i = 0; i < ob->tupdesc->natts; i++)
175  {
176  Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i);
177 
178  PyList_SET_ITEM(list, i, PyInt_FromLong(attr->atttypid));
179  }
180 
181  return list;
182 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
PyObject * PLy_exc_error
Definition: plpy_elog.c:19
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:500
int natts
Definition: tupdesc.h:73
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
tuple list
Definition: sort-test.py:11
int i
static PyObject * PLy_result_coltypmods ( PyObject *  self,
PyObject *  unused 
)
static

Definition at line 185 of file plpy_resultobject.c.

References i, sort-test::list, tupleDesc::natts, PLy_exc_error, PLy_exception_set(), PLyResultObject::tupdesc, and TupleDescAttr.

186 {
187  PLyResultObject *ob = (PLyResultObject *) self;
188  PyObject *list;
189  int i;
190 
191  if (!ob->tupdesc)
192  {
193  PLy_exception_set(PLy_exc_error, "command did not produce a result set");
194  return NULL;
195  }
196 
197  list = PyList_New(ob->tupdesc->natts);
198  for (i = 0; i < ob->tupdesc->natts; i++)
199  {
200  Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i);
201 
202  PyList_SET_ITEM(list, i, PyInt_FromLong(attr->atttypmod));
203  }
204 
205  return list;
206 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
PyObject * PLy_exc_error
Definition: plpy_elog.c:19
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:500
int natts
Definition: tupdesc.h:73
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
tuple list
Definition: sort-test.py:11
int i
static void PLy_result_dealloc ( PyObject *  arg)
static

Definition at line 120 of file plpy_resultobject.c.

References FreeTupleDesc(), PLyResultObject::nrows, PLyResultObject::rows, PLyResultObject::status, and PLyResultObject::tupdesc.

121 {
123 
124  Py_XDECREF(ob->nrows);
125  Py_XDECREF(ob->rows);
126  Py_XDECREF(ob->status);
127  if (ob->tupdesc)
128  {
129  FreeTupleDesc(ob->tupdesc);
130  ob->tupdesc = NULL;
131  }
132 
133  arg->ob_type->tp_free(arg);
134 }
void FreeTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:251
void * arg
PyObject_HEAD PyObject * nrows
void PLy_result_init_type ( void  )

Definition at line 94 of file plpy_resultobject.c.

References elog, ERROR, and PLy_ResultType.

Referenced by PLy_init_plpy().

95 {
96  if (PyType_Ready(&PLy_ResultType) < 0)
97  elog(ERROR, "could not initialize PLy_ResultType");
98 }
#define ERROR
Definition: elog.h:43
static PyTypeObject PLy_ResultType
#define elog
Definition: elog.h:219
static PyObject * PLy_result_item ( PyObject *  arg,
Py_ssize_t  idx 
)
static

Definition at line 235 of file plpy_resultobject.c.

References PLyResultObject::rows.

236 {
237  PyObject *rv;
239 
240  rv = PyList_GetItem(ob->rows, idx);
241  if (rv != NULL)
242  Py_INCREF(rv);
243  return rv;
244 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
void * arg
static Py_ssize_t PLy_result_length ( PyObject *  arg)
static

Definition at line 227 of file plpy_resultobject.c.

References PLyResultObject::rows.

228 {
230 
231  return PyList_Size(ob->rows);
232 }
void * arg
PyObject* PLy_result_new ( void  )

Definition at line 101 of file plpy_resultobject.c.

References PLyResultObject::nrows, PLy_ResultType, PLyResultObject::rows, PLyResultObject::status, and PLyResultObject::tupdesc.

Referenced by PLy_cursor_fetch(), and PLy_spi_execute_fetch_result().

102 {
103  PLyResultObject *ob;
104 
105  if ((ob = PyObject_New(PLyResultObject, &PLy_ResultType)) == NULL)
106  return NULL;
107 
108  /* ob->tuples = NULL; */
109 
110  Py_INCREF(Py_None);
111  ob->status = Py_None;
112  ob->nrows = PyInt_FromLong(-1);
113  ob->rows = PyList_New(0);
114  ob->tupdesc = NULL;
115 
116  return (PyObject *) ob;
117 }
static PyTypeObject PLy_ResultType
PyObject_HEAD PyObject * nrows
static PyObject * PLy_result_nrows ( PyObject *  self,
PyObject *  args 
)
static

Definition at line 209 of file plpy_resultobject.c.

References PLyResultObject::nrows.

210 {
211  PLyResultObject *ob = (PLyResultObject *) self;
212 
213  Py_INCREF(ob->nrows);
214  return ob->nrows;
215 }
PyObject_HEAD PyObject * nrows
static PyObject * PLy_result_slice ( PyObject *  arg,
Py_ssize_t  lidx,
Py_ssize_t  hidx 
)
static

Definition at line 247 of file plpy_resultobject.c.

References PLyResultObject::rows.

248 {
250 
251  return PyList_GetSlice(ob->rows, lidx, hidx);
252 }
void * arg
static PyObject * PLy_result_status ( PyObject *  self,
PyObject *  args 
)
static

Definition at line 218 of file plpy_resultobject.c.

References PLyResultObject::status.

219 {
220  PLyResultObject *ob = (PLyResultObject *) self;
221 
222  Py_INCREF(ob->status);
223  return ob->status;
224 }
static PyObject * PLy_result_str ( PyObject *  arg)
static

Definition at line 265 of file plpy_resultobject.c.

References PLyResultObject::nrows, PLyResultObject::rows, and PLyResultObject::status.

266 {
268 
269 #if PY_MAJOR_VERSION >= 3
270  return PyUnicode_FromFormat("<%s status=%S nrows=%S rows=%S>",
271  Py_TYPE(ob)->tp_name,
272  ob->status,
273  ob->nrows,
274  ob->rows);
275 #else
276  return PyString_FromFormat("<%s status=%ld nrows=%ld rows=%s>",
277  ob->ob_type->tp_name,
278  PyInt_AsLong(ob->status),
279  PyInt_AsLong(ob->nrows),
280  PyString_AsString(PyObject_Str(ob->rows)));
281 #endif
282 }
void * arg
PyObject_HEAD PyObject * nrows
static PyObject * PLy_result_subscript ( PyObject *  arg,
PyObject *  item 
)
static

Definition at line 285 of file plpy_resultobject.c.

References PLyResultObject::rows.

286 {
288 
289  return PyObject_GetItem(ob->rows, item);
290 }
void * arg

Variable Documentation

PyMappingMethods PLy_result_as_mapping
static
Initial value:
= {
}
static Py_ssize_t PLy_result_length(PyObject *arg)
static PyObject * PLy_result_subscript(PyObject *arg, PyObject *item)
static int PLy_result_ass_subscript(PyObject *self, PyObject *item, PyObject *value)

Definition at line 43 of file plpy_resultobject.c.

PySequenceMethods PLy_result_as_sequence
static
Initial value:
= {
NULL,
NULL,
NULL,
}
static Py_ssize_t PLy_result_length(PyObject *arg)
static PyObject * PLy_result_item(PyObject *arg, Py_ssize_t idx)
static int PLy_result_ass_slice(PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx, PyObject *slice)
static PyObject * PLy_result_slice(PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx)

Definition at line 33 of file plpy_resultobject.c.

char PLy_result_doc[]
static
Initial value:
= {
"Results of a PostgreSQL query"
}

Definition at line 29 of file plpy_resultobject.c.

PyMethodDef PLy_result_methods[]
static
Initial value:
= {
{"colnames", PLy_result_colnames, METH_NOARGS, NULL},
{"coltypes", PLy_result_coltypes, METH_NOARGS, NULL},
{"coltypmods", PLy_result_coltypmods, METH_NOARGS, NULL},
{"nrows", PLy_result_nrows, METH_VARARGS, NULL},
{"status", PLy_result_status, METH_VARARGS, NULL},
{NULL, NULL, 0, NULL}
}
static PyObject * PLy_result_status(PyObject *self, PyObject *args)
static PyObject * PLy_result_nrows(PyObject *self, PyObject *args)
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)

Definition at line 49 of file plpy_resultobject.c.

PyTypeObject PLy_ResultType
static

Definition at line 58 of file plpy_resultobject.c.

Referenced by PLy_result_init_type(), and PLy_result_new().