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 []
 
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 255 of file plpy_resultobject.c.

References PLyResultObject::rows.

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

◆ PLy_result_colnames()

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

Definition at line 111 of file plpy_resultobject.c.

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

112 {
113  PLyResultObject *ob = (PLyResultObject *) self;
114  PyObject *list;
115  int i;
116 
117  if (!ob->tupdesc)
118  {
119  PLy_exception_set(PLy_exc_error, "command did not produce a result set");
120  return NULL;
121  }
122 
123  list = PyList_New(ob->tupdesc->natts);
124  if (!list)
125  return NULL;
126  for (i = 0; i < ob->tupdesc->natts; i++)
127  {
128  Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i);
129 
130  PyList_SET_ITEM(list, i, PyString_FromString(NameStr(attr->attname)));
131  }
132 
133  return list;
134 }
#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:487
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
int i
#define NameStr(name)
Definition: c.h:616

◆ PLy_result_coltypes()

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

Definition at line 137 of file plpy_resultobject.c.

References i, sort-test::list, TupleDescData::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  if (!list)
151  return NULL;
152  for (i = 0; i < ob->tupdesc->natts; i++)
153  {
154  Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i);
155 
156  PyList_SET_ITEM(list, i, PyInt_FromLong(attr->atttypid));
157  }
158 
159  return list;
160 }
#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:487
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 163 of file plpy_resultobject.c.

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

164 {
165  PLyResultObject *ob = (PLyResultObject *) self;
166  PyObject *list;
167  int i;
168 
169  if (!ob->tupdesc)
170  {
171  PLy_exception_set(PLy_exc_error, "command did not produce a result set");
172  return NULL;
173  }
174 
175  list = PyList_New(ob->tupdesc->natts);
176  if (!list)
177  return NULL;
178  for (i = 0; i < ob->tupdesc->natts; i++)
179  {
180  Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i);
181 
182  PyList_SET_ITEM(list, i, PyInt_FromLong(attr->atttypmod));
183  }
184 
185  return list;
186 }
#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:487
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 94 of file plpy_resultobject.c.

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

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

References elog, ERROR, and PLy_ResultType.

Referenced by PLy_init_plpy().

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

◆ PLy_result_item()

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

Definition at line 215 of file plpy_resultobject.c.

References PLyResultObject::rows.

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

◆ PLy_result_length()

static Py_ssize_t PLy_result_length ( PyObject *  arg)
static

Definition at line 207 of file plpy_resultobject.c.

References PLyResultObject::rows.

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

◆ PLy_result_new()

PyObject* PLy_result_new ( void  )

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

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

◆ PLy_result_nrows()

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

Definition at line 189 of file plpy_resultobject.c.

References PLyResultObject::nrows.

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

◆ PLy_result_status()

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

Definition at line 198 of file plpy_resultobject.c.

References PLyResultObject::status.

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

◆ PLy_result_str()

static PyObject * PLy_result_str ( PyObject *  arg)
static

Definition at line 227 of file plpy_resultobject.c.

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

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

◆ PLy_result_subscript()

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

Definition at line 247 of file plpy_resultobject.c.

References PLyResultObject::rows.

248 {
250 
251  return PyObject_GetItem(ob->rows, item);
252 }
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 34 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 29 of file plpy_resultobject.c.

◆ PLy_result_doc

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

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

Referenced by PLy_result_init_type(), and PLy_result_new().