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_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_subscript()

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

Definition at line 257 of file plpy_resultobject.c.

References PLyResultObject::rows.

258 {
260 
261  return PyObject_SetItem(ob->rows, item, value);
262 }
static struct @145 value
void * arg

◆ PLy_result_colnames()

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

Definition at line 113 of file plpy_resultobject.c.

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

114 {
115  PLyResultObject *ob = (PLyResultObject *) self;
116  PyObject *list;
117  int i;
118 
119  if (!ob->tupdesc)
120  {
121  PLy_exception_set(PLy_exc_error, "command did not produce a result set");
122  return NULL;
123  }
124 
125  list = PyList_New(ob->tupdesc->natts);
126  if (!list)
127  return NULL;
128  for (i = 0; i < ob->tupdesc->natts; i++)
129  {
130  Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i);
131 
132  PyList_SET_ITEM(list, i, PyString_FromString(NameStr(attr->attname)));
133  }
134 
135  return list;
136 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
PyObject * PLy_exc_error
Definition: plpy_elog.c:19
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:502
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
int i
#define NameStr(name)
Definition: c.h:609

◆ PLy_result_coltypes()

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

Definition at line 139 of file plpy_resultobject.c.

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

140 {
141  PLyResultObject *ob = (PLyResultObject *) self;
142  PyObject *list;
143  int i;
144 
145  if (!ob->tupdesc)
146  {
147  PLy_exception_set(PLy_exc_error, "command did not produce a result set");
148  return NULL;
149  }
150 
151  list = PyList_New(ob->tupdesc->natts);
152  if (!list)
153  return NULL;
154  for (i = 0; i < ob->tupdesc->natts; i++)
155  {
156  Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i);
157 
158  PyList_SET_ITEM(list, i, PyInt_FromLong(attr->atttypid));
159  }
160 
161  return list;
162 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
PyObject * PLy_exc_error
Definition: plpy_elog.c:19
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:502
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
int i

◆ PLy_result_coltypmods()

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

Definition at line 165 of file plpy_resultobject.c.

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

166 {
167  PLyResultObject *ob = (PLyResultObject *) self;
168  PyObject *list;
169  int i;
170 
171  if (!ob->tupdesc)
172  {
173  PLy_exception_set(PLy_exc_error, "command did not produce a result set");
174  return NULL;
175  }
176 
177  list = PyList_New(ob->tupdesc->natts);
178  if (!list)
179  return NULL;
180  for (i = 0; i < ob->tupdesc->natts; i++)
181  {
182  Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i);
183 
184  PyList_SET_ITEM(list, i, PyInt_FromLong(attr->atttypmod));
185  }
186 
187  return list;
188 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
PyObject * PLy_exc_error
Definition: plpy_elog.c:19
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:502
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
int i

◆ PLy_result_dealloc()

static void PLy_result_dealloc ( PyObject *  arg)
static

Definition at line 96 of file plpy_resultobject.c.

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

97 {
99 
100  Py_XDECREF(ob->nrows);
101  Py_XDECREF(ob->rows);
102  Py_XDECREF(ob->status);
103  if (ob->tupdesc)
104  {
105  FreeTupleDesc(ob->tupdesc);
106  ob->tupdesc = NULL;
107  }
108 
109  arg->ob_type->tp_free(arg);
110 }
void FreeTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:313
void * arg
PyObject_HEAD PyObject * nrows

◆ PLy_result_init_type()

void PLy_result_init_type ( void  )

Definition at line 65 of file plpy_resultobject.c.

References elog, ERROR, and PLy_ResultType.

Referenced by PLy_init_plpy().

66 {
67  if (PyType_Ready(&PLy_ResultType) < 0)
68  elog(ERROR, "could not initialize PLy_ResultType");
69 }
#define ERROR
Definition: elog.h:43
static PyTypeObject PLy_ResultType
#define elog(elevel,...)
Definition: elog.h:226

◆ PLy_result_item()

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

Definition at line 217 of file plpy_resultobject.c.

References PLyResultObject::rows.

218 {
219  PyObject *rv;
221 
222  rv = PyList_GetItem(ob->rows, idx);
223  if (rv != NULL)
224  Py_INCREF(rv);
225  return rv;
226 }
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 209 of file plpy_resultobject.c.

References PLyResultObject::rows.

210 {
212 
213  return PyList_Size(ob->rows);
214 }
void * arg

◆ PLy_result_new()

PyObject* PLy_result_new ( void  )

Definition at line 72 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().

73 {
74  PLyResultObject *ob;
75 
76  if ((ob = PyObject_New(PLyResultObject, &PLy_ResultType)) == NULL)
77  return NULL;
78 
79  /* ob->tuples = NULL; */
80 
81  Py_INCREF(Py_None);
82  ob->status = Py_None;
83  ob->nrows = PyInt_FromLong(-1);
84  ob->rows = PyList_New(0);
85  ob->tupdesc = NULL;
86  if (!ob->rows)
87  {
88  Py_DECREF(ob);
89  return NULL;
90  }
91 
92  return (PyObject *) ob;
93 }
static PyTypeObject PLy_ResultType
PyObject_HEAD PyObject * nrows

◆ PLy_result_nrows()

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

Definition at line 191 of file plpy_resultobject.c.

References PLyResultObject::nrows.

192 {
193  PLyResultObject *ob = (PLyResultObject *) self;
194 
195  Py_INCREF(ob->nrows);
196  return ob->nrows;
197 }
PyObject_HEAD PyObject * nrows

◆ PLy_result_status()

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

Definition at line 200 of file plpy_resultobject.c.

References PLyResultObject::status.

201 {
202  PLyResultObject *ob = (PLyResultObject *) self;
203 
204  Py_INCREF(ob->status);
205  return ob->status;
206 }

◆ PLy_result_str()

static PyObject * PLy_result_str ( PyObject *  arg)
static

Definition at line 229 of file plpy_resultobject.c.

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

230 {
232 
233 #if PY_MAJOR_VERSION >= 3
234  return PyUnicode_FromFormat("<%s status=%S nrows=%S rows=%S>",
235  Py_TYPE(ob)->tp_name,
236  ob->status,
237  ob->nrows,
238  ob->rows);
239 #else
240  return PyString_FromFormat("<%s status=%ld nrows=%ld rows=%s>",
241  ob->ob_type->tp_name,
242  PyInt_AsLong(ob->status),
243  PyInt_AsLong(ob->nrows),
244  PyString_AsString(PyObject_Str(ob->rows)));
245 #endif
246 }
void * arg
PyObject_HEAD PyObject * nrows

◆ PLy_result_subscript()

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

Definition at line 249 of file plpy_resultobject.c.

References PLyResultObject::rows.

250 {
252 
253  return PyObject_GetItem(ob->rows, item);
254 }
void * arg

Variable Documentation

◆ PLy_result_as_mapping

PyMappingMethods PLy_result_as_mapping
static
Initial value:
= {
.mp_length = PLy_result_length,
.mp_subscript = PLy_result_subscript,
.mp_ass_subscript = PLy_result_ass_subscript,
}
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 36 of file plpy_resultobject.c.

◆ PLy_result_as_sequence

PySequenceMethods PLy_result_as_sequence
static
Initial value:
= {
.sq_length = PLy_result_length,
.sq_item = PLy_result_item,
}
static Py_ssize_t PLy_result_length(PyObject *arg)
static PyObject * PLy_result_item(PyObject *arg, Py_ssize_t idx)

Definition at line 31 of file plpy_resultobject.c.

◆ PLy_result_doc

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

Definition at line 27 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 42 of file plpy_resultobject.c.

◆ PLy_ResultType

PyTypeObject PLy_ResultType
static
Initial value:
= {
.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,
}
#define PyVarObject_HEAD_INIT(type, size)
Definition: plpython.h:113
static PyMethodDef PLy_result_methods[]
struct PLyResultObject PLyResultObject
static PySequenceMethods PLy_result_as_sequence
static char PLy_result_doc[]
static void PLy_result_dealloc(PyObject *arg)
static PyMappingMethods PLy_result_as_mapping
static PyObject * PLy_result_str(PyObject *arg)

Definition at line 51 of file plpy_resultobject.c.

Referenced by PLy_result_init_type(), and PLy_result_new().