PostgreSQL Source Code  git master
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

◆ PLy_result_ass_slice()

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

Definition at line 266 of file plpy_resultobject.c.

References PLyResultObject::rows.

267 {
268  int rv;
270 
271  rv = PyList_SetSlice(ob->rows, lidx, hidx, slice);
272  return rv;
273 }
void * arg

◆ PLy_result_ass_subscript()

static int PLy_result_ass_subscript ( PyObject *  self,
PyObject *  item,
PyObject *  value 
)
static

Definition at line 304 of file plpy_resultobject.c.

References PLyResultObject::rows.

305 {
307 
308  return PyObject_SetItem(ob->rows, item, value);
309 }
static struct @121 value
void * arg

◆ PLy_result_colnames()

static PyObject * PLy_result_colnames ( PyObject *  self,
PyObject *  unused 
)
static

Definition at line 142 of file plpy_resultobject.c.

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

143 {
144  PLyResultObject *ob = (PLyResultObject *) self;
145  PyObject *list;
146  int i;
147 
148  if (!ob->tupdesc)
149  {
150  PLy_exception_set(PLy_exc_error, "command did not produce a result set");
151  return NULL;
152  }
153 
154  list = PyList_New(ob->tupdesc->natts);
155  if (!list)
156  return NULL;
157  for (i = 0; i < ob->tupdesc->natts; i++)
158  {
159  Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i);
160 
161  PyList_SET_ITEM(list, i, PyString_FromString(NameStr(attr->attname)));
162  }
163 
164  return list;
165 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
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:79
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
int i
#define NameStr(name)
Definition: c.h:547

◆ PLy_result_coltypes()

static PyObject * PLy_result_coltypes ( PyObject *  self,
PyObject *  unused 
)
static

Definition at line 168 of file plpy_resultobject.c.

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

169 {
170  PLyResultObject *ob = (PLyResultObject *) self;
171  PyObject *list;
172  int i;
173 
174  if (!ob->tupdesc)
175  {
176  PLy_exception_set(PLy_exc_error, "command did not produce a result set");
177  return NULL;
178  }
179 
180  list = PyList_New(ob->tupdesc->natts);
181  if (!list)
182  return NULL;
183  for (i = 0; i < ob->tupdesc->natts; i++)
184  {
185  Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i);
186 
187  PyList_SET_ITEM(list, i, PyInt_FromLong(attr->atttypid));
188  }
189 
190  return list;
191 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
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:79
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
int i

◆ PLy_result_coltypmods()

static PyObject * PLy_result_coltypmods ( PyObject *  self,
PyObject *  unused 
)
static

Definition at line 194 of file plpy_resultobject.c.

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

195 {
196  PLyResultObject *ob = (PLyResultObject *) self;
197  PyObject *list;
198  int i;
199 
200  if (!ob->tupdesc)
201  {
202  PLy_exception_set(PLy_exc_error, "command did not produce a result set");
203  return NULL;
204  }
205 
206  list = PyList_New(ob->tupdesc->natts);
207  if (!list)
208  return NULL;
209  for (i = 0; i < ob->tupdesc->natts; i++)
210  {
211  Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i);
212 
213  PyList_SET_ITEM(list, i, PyInt_FromLong(attr->atttypmod));
214  }
215 
216  return list;
217 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
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:79
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
int i

◆ PLy_result_dealloc()

static void PLy_result_dealloc ( PyObject *  arg)
static

Definition at line 125 of file plpy_resultobject.c.

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

126 {
128 
129  Py_XDECREF(ob->nrows);
130  Py_XDECREF(ob->rows);
131  Py_XDECREF(ob->status);
132  if (ob->tupdesc)
133  {
134  FreeTupleDesc(ob->tupdesc);
135  ob->tupdesc = NULL;
136  }
137 
138  arg->ob_type->tp_free(arg);
139 }
void FreeTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:251
void * arg
PyObject_HEAD PyObject * nrows

◆ PLy_result_init_type()

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

◆ PLy_result_item()

static PyObject * PLy_result_item ( PyObject *  arg,
Py_ssize_t  idx 
)
static

Definition at line 246 of file plpy_resultobject.c.

References PLyResultObject::rows.

247 {
248  PyObject *rv;
250 
251  rv = PyList_GetItem(ob->rows, idx);
252  if (rv != NULL)
253  Py_INCREF(rv);
254  return rv;
255 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
void * arg

◆ PLy_result_length()

static Py_ssize_t PLy_result_length ( PyObject *  arg)
static

Definition at line 238 of file plpy_resultobject.c.

References PLyResultObject::rows.

239 {
241 
242  return PyList_Size(ob->rows);
243 }
void * arg

◆ PLy_result_new()

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  if (!ob->rows)
116  {
117  Py_DECREF(ob);
118  return NULL;
119  }
120 
121  return (PyObject *) ob;
122 }
static PyTypeObject PLy_ResultType
PyObject_HEAD PyObject * nrows

◆ PLy_result_nrows()

static PyObject * PLy_result_nrows ( PyObject *  self,
PyObject *  args 
)
static

Definition at line 220 of file plpy_resultobject.c.

References PLyResultObject::nrows.

221 {
222  PLyResultObject *ob = (PLyResultObject *) self;
223 
224  Py_INCREF(ob->nrows);
225  return ob->nrows;
226 }
PyObject_HEAD PyObject * nrows

◆ PLy_result_slice()

static PyObject * PLy_result_slice ( PyObject *  arg,
Py_ssize_t  lidx,
Py_ssize_t  hidx 
)
static

Definition at line 258 of file plpy_resultobject.c.

References PLyResultObject::rows.

259 {
261 
262  return PyList_GetSlice(ob->rows, lidx, hidx);
263 }
void * arg

◆ PLy_result_status()

static PyObject * PLy_result_status ( PyObject *  self,
PyObject *  args 
)
static

Definition at line 229 of file plpy_resultobject.c.

References PLyResultObject::status.

230 {
231  PLyResultObject *ob = (PLyResultObject *) self;
232 
233  Py_INCREF(ob->status);
234  return ob->status;
235 }

◆ PLy_result_str()

static PyObject * PLy_result_str ( PyObject *  arg)
static

Definition at line 276 of file plpy_resultobject.c.

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

277 {
279 
280 #if PY_MAJOR_VERSION >= 3
281  return PyUnicode_FromFormat("<%s status=%S nrows=%S rows=%S>",
282  Py_TYPE(ob)->tp_name,
283  ob->status,
284  ob->nrows,
285  ob->rows);
286 #else
287  return PyString_FromFormat("<%s status=%ld nrows=%ld rows=%s>",
288  ob->ob_type->tp_name,
289  PyInt_AsLong(ob->status),
290  PyInt_AsLong(ob->nrows),
291  PyString_AsString(PyObject_Str(ob->rows)));
292 #endif
293 }
void * arg
PyObject_HEAD PyObject * nrows

◆ PLy_result_subscript()

static PyObject * PLy_result_subscript ( PyObject *  arg,
PyObject *  item 
)
static

Definition at line 296 of file plpy_resultobject.c.

References PLyResultObject::rows.

297 {
299 
300  return PyObject_GetItem(ob->rows, item);
301 }
void * arg

Variable Documentation

◆ PLy_result_as_mapping

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.

◆ PLy_result_as_sequence

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.

◆ PLy_result_doc

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

Definition at line 29 of file plpy_resultobject.c.

◆ PLy_result_methods

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.

◆ PLy_ResultType

PyTypeObject PLy_ResultType
static

Definition at line 58 of file plpy_resultobject.c.

Referenced by PLy_result_init_type(), and PLy_result_new().