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 243 of file plpy_resultobject.c.

References PLyResultObject::rows.

244 {
245  int rv;
247 
248  rv = PyList_SetSlice(ob->rows, lidx, hidx, slice);
249  return rv;
250 }
void * arg
static int PLy_result_ass_subscript ( PyObject *  self,
PyObject *  item,
PyObject *  value 
)
static

Definition at line 281 of file plpy_resultobject.c.

References PLyResultObject::rows.

282 {
284 
285  return PyObject_SetItem(ob->rows, item, value);
286 }
static struct @76 value
void * arg
static PyObject * PLy_result_colnames ( PyObject *  self,
PyObject *  unused 
)
static

Definition at line 137 of file plpy_resultobject.c.

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

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  PyList_SET_ITEM(list, i, PyString_FromString(NameStr(ob->tupdesc->attrs[i]->attname)));
152 
153  return list;
154 }
Form_pg_attribute * attrs
Definition: tupdesc.h:74
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
#define NULL
Definition: c.h:226
tuple list
Definition: sort-test.py:11
int i
#define NameStr(name)
Definition: c.h:495
static PyObject * PLy_result_coltypes ( PyObject *  self,
PyObject *  unused 
)
static

Definition at line 157 of file plpy_resultobject.c.

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

158 {
159  PLyResultObject *ob = (PLyResultObject *) self;
160  PyObject *list;
161  int i;
162 
163  if (!ob->tupdesc)
164  {
165  PLy_exception_set(PLy_exc_error, "command did not produce a result set");
166  return NULL;
167  }
168 
169  list = PyList_New(ob->tupdesc->natts);
170  for (i = 0; i < ob->tupdesc->natts; i++)
171  PyList_SET_ITEM(list, i, PyInt_FromLong(ob->tupdesc->attrs[i]->atttypid));
172 
173  return list;
174 }
Form_pg_attribute * attrs
Definition: tupdesc.h:74
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
#define NULL
Definition: c.h:226
tuple list
Definition: sort-test.py:11
int i
static PyObject * PLy_result_coltypmods ( PyObject *  self,
PyObject *  unused 
)
static

Definition at line 177 of file plpy_resultobject.c.

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

178 {
179  PLyResultObject *ob = (PLyResultObject *) self;
180  PyObject *list;
181  int i;
182 
183  if (!ob->tupdesc)
184  {
185  PLy_exception_set(PLy_exc_error, "command did not produce a result set");
186  return NULL;
187  }
188 
189  list = PyList_New(ob->tupdesc->natts);
190  for (i = 0; i < ob->tupdesc->natts; i++)
191  PyList_SET_ITEM(list, i, PyInt_FromLong(ob->tupdesc->attrs[i]->atttypmod));
192 
193  return list;
194 }
Form_pg_attribute * attrs
Definition: tupdesc.h:74
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
#define NULL
Definition: c.h:226
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, NULL, 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 }
#define NULL
Definition: c.h:226
void FreeTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:266
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 223 of file plpy_resultobject.c.

References NULL, and PLyResultObject::rows.

224 {
225  PyObject *rv;
227 
228  rv = PyList_GetItem(ob->rows, idx);
229  if (rv != NULL)
230  Py_INCREF(rv);
231  return rv;
232 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
#define NULL
Definition: c.h:226
void * arg
static Py_ssize_t PLy_result_length ( PyObject *  arg)
static

Definition at line 215 of file plpy_resultobject.c.

References PLyResultObject::rows.

216 {
218 
219  return PyList_Size(ob->rows);
220 }
void * arg
PyObject* PLy_result_new ( void  )

Definition at line 101 of file plpy_resultobject.c.

References PLyResultObject::nrows, NULL, 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
#define NULL
Definition: c.h:226
PyObject_HEAD PyObject * nrows
static PyObject * PLy_result_nrows ( PyObject *  self,
PyObject *  args 
)
static

Definition at line 197 of file plpy_resultobject.c.

References PLyResultObject::nrows.

198 {
199  PLyResultObject *ob = (PLyResultObject *) self;
200 
201  Py_INCREF(ob->nrows);
202  return ob->nrows;
203 }
PyObject_HEAD PyObject * nrows
static PyObject * PLy_result_slice ( PyObject *  arg,
Py_ssize_t  lidx,
Py_ssize_t  hidx 
)
static

Definition at line 235 of file plpy_resultobject.c.

References PLyResultObject::rows.

236 {
238 
239  return PyList_GetSlice(ob->rows, lidx, hidx);
240 }
void * arg
static PyObject * PLy_result_status ( PyObject *  self,
PyObject *  args 
)
static

Definition at line 206 of file plpy_resultobject.c.

References PLyResultObject::status.

207 {
208  PLyResultObject *ob = (PLyResultObject *) self;
209 
210  Py_INCREF(ob->status);
211  return ob->status;
212 }
static PyObject * PLy_result_str ( PyObject *  arg)
static

Definition at line 253 of file plpy_resultobject.c.

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

254 {
256 
257 #if PY_MAJOR_VERSION >= 3
258  return PyUnicode_FromFormat("<%s status=%S nrows=%S rows=%S>",
259  Py_TYPE(ob)->tp_name,
260  ob->status,
261  ob->nrows,
262  ob->rows);
263 #else
264  return PyString_FromFormat("<%s status=%ld nrows=%ld rows=%s>",
265  ob->ob_type->tp_name,
266  PyInt_AsLong(ob->status),
267  PyInt_AsLong(ob->nrows),
268  PyString_AsString(PyObject_Str(ob->rows)));
269 #endif
270 }
void * arg
PyObject_HEAD PyObject * nrows
static PyObject * PLy_result_subscript ( PyObject *  arg,
PyObject *  item 
)
static

Definition at line 273 of file plpy_resultobject.c.

References PLyResultObject::rows.

274 {
276 
277  return PyObject_GetItem(ob->rows, item);
278 }
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)
#define NULL
Definition: c.h:226
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)
#define NULL
Definition: c.h:226
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().