PostgreSQL Source Code  git master
plpy_resultobject.c File Reference
#include "postgres.h"
#include "plpy_elog.h"
#include "plpy_resultobject.h"
#include "plpython.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 [] = "Results of a PostgreSQL query"
 
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 253 of file plpy_resultobject.c.

References PLyResultObject::rows.

254 {
256 
257  return PyObject_SetItem(ob->rows, item, value);
258 }
static struct @143 value
void * arg

◆ PLy_result_colnames()

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

Definition at line 109 of file plpy_resultobject.c.

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

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

◆ PLy_result_coltypes()

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

Definition at line 135 of file plpy_resultobject.c.

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

136 {
137  PLyResultObject *ob = (PLyResultObject *) self;
138  PyObject *list;
139  int i;
140 
141  if (!ob->tupdesc)
142  {
143  PLy_exception_set(PLy_exc_error, "command did not produce a result set");
144  return NULL;
145  }
146 
147  list = PyList_New(ob->tupdesc->natts);
148  if (!list)
149  return NULL;
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, PyInt_FromLong(attr->atttypid));
155  }
156 
157  return list;
158 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
PyObject * PLy_exc_error
Definition: plpy_elog.c:15
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:479
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
int i

◆ PLy_result_coltypmods()

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

Definition at line 161 of file plpy_resultobject.c.

References i, sort-test::list, TupleDescData::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  if (!list)
175  return NULL;
176  for (i = 0; i < ob->tupdesc->natts; i++)
177  {
178  Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i);
179 
180  PyList_SET_ITEM(list, i, PyInt_FromLong(attr->atttypmod));
181  }
182 
183  return list;
184 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
PyObject * PLy_exc_error
Definition: plpy_elog.c:15
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:479
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
int i

◆ PLy_result_dealloc()

static void PLy_result_dealloc ( PyObject *  arg)
static

Definition at line 92 of file plpy_resultobject.c.

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

93 {
95 
96  Py_XDECREF(ob->nrows);
97  Py_XDECREF(ob->rows);
98  Py_XDECREF(ob->status);
99  if (ob->tupdesc)
100  {
101  FreeTupleDesc(ob->tupdesc);
102  ob->tupdesc = NULL;
103  }
104 
105  arg->ob_type->tp_free(arg);
106 }
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 61 of file plpy_resultobject.c.

References elog, ERROR, and PLy_ResultType.

Referenced by PLy_init_plpy().

62 {
63  if (PyType_Ready(&PLy_ResultType) < 0)
64  elog(ERROR, "could not initialize PLy_ResultType");
65 }
#define ERROR
Definition: elog.h:43
static PyTypeObject PLy_ResultType
#define elog(elevel,...)
Definition: elog.h:214

◆ PLy_result_item()

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

Definition at line 213 of file plpy_resultobject.c.

References PLyResultObject::rows.

214 {
215  PyObject *rv;
217 
218  rv = PyList_GetItem(ob->rows, idx);
219  if (rv != NULL)
220  Py_INCREF(rv);
221  return rv;
222 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
void * arg

◆ PLy_result_length()

static Py_ssize_t PLy_result_length ( PyObject *  arg)
static

Definition at line 205 of file plpy_resultobject.c.

References PLyResultObject::rows.

206 {
208 
209  return PyList_Size(ob->rows);
210 }
void * arg

◆ PLy_result_new()

PyObject* PLy_result_new ( void  )

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

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

◆ PLy_result_nrows()

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

Definition at line 187 of file plpy_resultobject.c.

References PLyResultObject::nrows.

188 {
189  PLyResultObject *ob = (PLyResultObject *) self;
190 
191  Py_INCREF(ob->nrows);
192  return ob->nrows;
193 }
PyObject_HEAD PyObject * nrows

◆ PLy_result_status()

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

Definition at line 196 of file plpy_resultobject.c.

References PLyResultObject::status.

197 {
198  PLyResultObject *ob = (PLyResultObject *) self;
199 
200  Py_INCREF(ob->status);
201  return ob->status;
202 }

◆ PLy_result_str()

static PyObject * PLy_result_str ( PyObject *  arg)
static

Definition at line 225 of file plpy_resultobject.c.

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

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

◆ PLy_result_subscript()

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

Definition at line 245 of file plpy_resultobject.c.

References PLyResultObject::rows.

246 {
248 
249  return PyObject_GetItem(ob->rows, item);
250 }
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 32 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 27 of file plpy_resultobject.c.

◆ PLy_result_doc

char PLy_result_doc[] = "Results of a PostgreSQL query"
static

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

◆ PLy_ResultType

PyTypeObject PLy_ResultType
static
Initial value:
= {
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 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 47 of file plpy_resultobject.c.

Referenced by PLy_result_init_type(), and PLy_result_new().