PostgreSQL Source Code git master
Loading...
Searching...
No Matches
plpy_plpymodule.c
Go to the documentation of this file.
1/*
2 * the plpy module
3 *
4 * src/pl/plpython/plpy_plpymodule.c
5 */
6
7#include "postgres.h"
8
9#include "mb/pg_wchar.h"
10#include "plpy_cursorobject.h"
11#include "plpy_elog.h"
12#include "plpy_planobject.h"
13#include "plpy_plpymodule.h"
14#include "plpy_resultobject.h"
15#include "plpy_spi.h"
16#include "plpy_subxactobject.h"
17#include "plpy_util.h"
18#include "utils/builtins.h"
19
21
22
23static void PLy_add_exceptions(PyObject *plpy);
25 PyObject *base, PyObject *dict,
26 const char *modname, PyObject *mod);
28
29/* module functions */
30static PyObject *PLy_debug(PyObject *self, PyObject *args, PyObject *kw);
31static PyObject *PLy_log(PyObject *self, PyObject *args, PyObject *kw);
32static PyObject *PLy_info(PyObject *self, PyObject *args, PyObject *kw);
33static PyObject *PLy_notice(PyObject *self, PyObject *args, PyObject *kw);
34static PyObject *PLy_warning(PyObject *self, PyObject *args, PyObject *kw);
35static PyObject *PLy_error(PyObject *self, PyObject *args, PyObject *kw);
36static PyObject *PLy_fatal(PyObject *self, PyObject *args, PyObject *kw);
37static PyObject *PLy_quote_literal(PyObject *self, PyObject *args);
38static PyObject *PLy_quote_nullable(PyObject *self, PyObject *args);
39static PyObject *PLy_quote_ident(PyObject *self, PyObject *args);
40
41
42/* A list of all known exceptions, generated from backend/utils/errcodes.txt */
43typedef struct ExceptionMap
44{
45 char *name;
46 char *classname;
49
50static const ExceptionMap exception_map[] = {
51#include "spiexceptions.h"
52 {NULL, NULL, 0}
53};
54
56 /*
57 * logging methods
58 */
66
67 /*
68 * create a stored plan
69 */
70 {"prepare", PLy_spi_prepare, METH_VARARGS, NULL},
71
72 /*
73 * execute a plan or query
74 */
75 {"execute", PLy_spi_execute, METH_VARARGS, NULL},
76
77 /*
78 * escaping strings
79 */
80 {"quote_literal", PLy_quote_literal, METH_VARARGS, NULL},
81 {"quote_nullable", PLy_quote_nullable, METH_VARARGS, NULL},
82 {"quote_ident", PLy_quote_ident, METH_VARARGS, NULL},
83
84 /*
85 * create the subtransaction context manager
86 */
87 {"subtransaction", PLy_subtransaction_new, METH_NOARGS, NULL},
88
89 /*
90 * create a cursor
91 */
92 {"cursor", PLy_cursor, METH_VARARGS, NULL},
93
94 /*
95 * transaction control
96 */
97 {"commit", PLy_commit, METH_NOARGS, NULL},
98 {"rollback", PLy_rollback, METH_NOARGS, NULL},
99
100 {NULL, NULL, 0, NULL}
101};
102
104 {NULL, NULL, 0, NULL}
105};
106
109 .m_name = "plpy",
110 .m_size = -1,
111 .m_methods = PLy_methods,
112};
113
116 .m_name = "spiexceptions",
117 .m_size = -1,
118 .m_methods = PLy_exc_methods,
119};
120
121/*
122 * Must have external linkage, because PyMODINIT_FUNC does dllexport on
123 * Windows-like platforms.
124 */
127{
128 PyObject *m;
129
131 if (m == NULL)
132 return NULL;
133
135
140
141 return m;
142}
143
144static void
146{
149
151 "Error", plpy);
153 "Fatal", plpy);
155 "SPIError", plpy);
156
158 if (excmod == NULL)
159 PLy_elog(ERROR, "could not create the spiexceptions module");
160
161 hash_ctl.keysize = sizeof(int);
162 hash_ctl.entrysize = sizeof(PLyExceptionEntry);
163 PLy_spi_exceptions = hash_create("PL/Python SPI exceptions", 256,
165
167
168 if (PyModule_AddObject(plpy, "spiexceptions", excmod) < 0)
169 {
171 PLy_elog(ERROR, "could not add the spiexceptions module");
172 }
173}
174
175/*
176 * Create an exception object and add it to the module
177 *
178 * The created exception object is also returned.
179 */
180static PyObject *
182 const char *modname, PyObject *mod)
183{
184 PyObject *exc;
185
186 exc = PyErr_NewException(name, base, dict);
187 if (exc == NULL)
189
190 /*
191 * PyModule_AddObject() (below) steals the reference to exc, but we also
192 * want to return the value from this function, so add another ref to
193 * account for that. (The caller will store a pointer to the exception
194 * object in some permanent variable.)
195 */
196 Py_INCREF(exc);
197
198 if (PyModule_AddObject(mod, modname, exc) < 0)
199 {
200 Py_XDECREF(exc);
201 PLy_elog(ERROR, "could not add exception %s", name);
202 }
203
204 return exc;
205}
206
207/*
208 * Add all the autogenerated exceptions as subclasses of SPIError
209 */
210static void
212{
213 int i;
214
215 for (i = 0; exception_map[i].name != NULL; i++)
216 {
217 bool found;
218 PyObject *exc;
219 PLyExceptionEntry *entry;
220 PyObject *sqlstate;
222
223 if (dict == NULL)
225
227 if (sqlstate == NULL)
228 PLy_elog(ERROR, "could not generate SPI exceptions");
229
230 PyDict_SetItemString(dict, "sqlstate", sqlstate);
231 Py_DECREF(sqlstate);
232
234 exception_map[i].classname, mod);
235
236 entry = hash_search(PLy_spi_exceptions, &exception_map[i].sqlstate,
237 HASH_ENTER, &found);
238 Assert(!found);
239 entry->exc = exc;
240 }
241}
242
243
244/*
245 * the python interface to the elog function
246 * don't confuse these with PLy_elog
247 */
248static PyObject *PLy_output(volatile int level, PyObject *self,
249 PyObject *args, PyObject *kw);
250
251static PyObject *
253{
254 return PLy_output(DEBUG2, self, args, kw);
255}
256
257static PyObject *
259{
260 return PLy_output(LOG, self, args, kw);
261}
262
263static PyObject *
265{
266 return PLy_output(INFO, self, args, kw);
267}
268
269static PyObject *
271{
272 return PLy_output(NOTICE, self, args, kw);
273}
274
275static PyObject *
277{
278 return PLy_output(WARNING, self, args, kw);
279}
280
281static PyObject *
283{
284 return PLy_output(ERROR, self, args, kw);
285}
286
287static PyObject *
289{
290 return PLy_output(FATAL, self, args, kw);
291}
292
293static PyObject *
295{
296 const char *str;
297 char *quoted;
298 PyObject *ret;
299
300 if (!PyArg_ParseTuple(args, "s:quote_literal", &str))
301 return NULL;
302
303 quoted = quote_literal_cstr(str);
304 ret = PLyUnicode_FromString(quoted);
305 pfree(quoted);
306
307 return ret;
308}
309
310static PyObject *
312{
313 const char *str;
314 char *quoted;
315 PyObject *ret;
316
317 if (!PyArg_ParseTuple(args, "z:quote_nullable", &str))
318 return NULL;
319
320 if (str == NULL)
321 return PLyUnicode_FromString("NULL");
322
323 quoted = quote_literal_cstr(str);
324 ret = PLyUnicode_FromString(quoted);
325 pfree(quoted);
326
327 return ret;
328}
329
330static PyObject *
332{
333 const char *str;
334 const char *quoted;
335 PyObject *ret;
336
337 if (!PyArg_ParseTuple(args, "s:quote_ident", &str))
338 return NULL;
339
340 quoted = quote_identifier(str);
341 ret = PLyUnicode_FromString(quoted);
342
343 return ret;
344}
345
346/* enforce cast of object to string (returns a palloc'd string or NULL) */
347static char *
349{
350 if (obj)
351 {
352 PyObject *so = PyObject_Str(obj);
353
354 if (so != NULL)
355 {
356 char *str;
357
359 Py_DECREF(so);
360
361 return str;
362 }
363 }
364
365 return NULL;
366}
367
368static PyObject *
369PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw)
370{
371 int sqlstate = 0;
372 char *volatile sqlstatestr = NULL;
373 char *volatile message = NULL;
374 char *volatile detail = NULL;
375 char *volatile hint = NULL;
376 char *volatile column_name = NULL;
377 char *volatile constraint_name = NULL;
378 char *volatile datatype_name = NULL;
379 char *volatile table_name = NULL;
380 char *volatile schema_name = NULL;
381 volatile MemoryContext oldcontext;
382 PyObject *key,
383 *value;
384 PyObject *volatile so;
385 Py_ssize_t pos = 0;
386
387 if (PyTuple_Size(args) == 1)
388 {
389 /*
390 * Treat single argument specially to avoid undesirable ('tuple',)
391 * decoration.
392 */
393 PyObject *o;
394
395 if (!PyArg_UnpackTuple(args, "plpy.elog", 1, 1, &o))
396 PLy_elog(ERROR, "could not unpack arguments in plpy.elog");
397 so = PyObject_Str(o);
398 }
399 else
400 so = PyObject_Str(args);
401
402 if (so == NULL || ((message = PLyUnicode_AsString(so)) == NULL))
403 {
404 level = ERROR;
405 message = dgettext(TEXTDOMAIN, "could not parse error message in plpy.elog");
406 }
407 message = pstrdup(message);
408
409 Py_XDECREF(so);
410
411 if (kw != NULL)
412 {
413 while (PyDict_Next(kw, &pos, &key, &value))
414 {
415 char *keyword = PLyUnicode_AsString(key);
416
417 if (strcmp(keyword, "message") == 0)
418 {
419 /* the message should not be overwritten */
420 if (PyTuple_Size(args) != 0)
421 {
422 PLy_exception_set(PyExc_TypeError, "argument 'message' given by name and position");
423 return NULL;
424 }
425
426 if (message)
427 pfree(message);
428 message = object_to_string(value);
429 }
430 else if (strcmp(keyword, "detail") == 0)
431 detail = object_to_string(value);
432 else if (strcmp(keyword, "hint") == 0)
433 hint = object_to_string(value);
434 else if (strcmp(keyword, "sqlstate") == 0)
436 else if (strcmp(keyword, "schema_name") == 0)
437 schema_name = object_to_string(value);
438 else if (strcmp(keyword, "table_name") == 0)
439 table_name = object_to_string(value);
440 else if (strcmp(keyword, "column_name") == 0)
441 column_name = object_to_string(value);
442 else if (strcmp(keyword, "datatype_name") == 0)
443 datatype_name = object_to_string(value);
444 else if (strcmp(keyword, "constraint_name") == 0)
445 constraint_name = object_to_string(value);
446 else
447 {
449 "'%s' is an invalid keyword argument for this function",
450 keyword);
451 return NULL;
452 }
453 }
454 }
455
456 if (sqlstatestr != NULL)
457 {
458 if (strlen(sqlstatestr) != 5)
459 {
460 PLy_exception_set(PyExc_ValueError, "invalid SQLSTATE code");
461 return NULL;
462 }
463
464 if (strspn(sqlstatestr, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != 5)
465 {
466 PLy_exception_set(PyExc_ValueError, "invalid SQLSTATE code");
467 return NULL;
468 }
469
470 sqlstate = MAKE_SQLSTATE(sqlstatestr[0],
471 sqlstatestr[1],
472 sqlstatestr[2],
473 sqlstatestr[3],
474 sqlstatestr[4]);
475 }
476
477 oldcontext = CurrentMemoryContext;
478 PG_TRY();
479 {
480 if (message != NULL)
481 pg_verifymbstr(message, strlen(message), false);
482 if (detail != NULL)
483 pg_verifymbstr(detail, strlen(detail), false);
484 if (hint != NULL)
485 pg_verifymbstr(hint, strlen(hint), false);
486 if (schema_name != NULL)
487 pg_verifymbstr(schema_name, strlen(schema_name), false);
488 if (table_name != NULL)
489 pg_verifymbstr(table_name, strlen(table_name), false);
490 if (column_name != NULL)
491 pg_verifymbstr(column_name, strlen(column_name), false);
492 if (datatype_name != NULL)
493 pg_verifymbstr(datatype_name, strlen(datatype_name), false);
494 if (constraint_name != NULL)
495 pg_verifymbstr(constraint_name, strlen(constraint_name), false);
496
497 ereport(level,
498 ((sqlstate != 0) ? errcode(sqlstate) : 0,
499 (message != NULL) ? errmsg_internal("%s", message) : 0,
500 (detail != NULL) ? errdetail_internal("%s", detail) : 0,
501 (hint != NULL) ? errhint("%s", hint) : 0,
502 (column_name != NULL) ?
503 err_generic_string(PG_DIAG_COLUMN_NAME, column_name) : 0,
504 (constraint_name != NULL) ?
505 err_generic_string(PG_DIAG_CONSTRAINT_NAME, constraint_name) : 0,
506 (datatype_name != NULL) ?
507 err_generic_string(PG_DIAG_DATATYPE_NAME, datatype_name) : 0,
508 (table_name != NULL) ?
509 err_generic_string(PG_DIAG_TABLE_NAME, table_name) : 0,
510 (schema_name != NULL) ?
511 err_generic_string(PG_DIAG_SCHEMA_NAME, schema_name) : 0));
512 }
513 PG_CATCH();
514 {
516
517 MemoryContextSwitchTo(oldcontext);
520
523
524 return NULL;
525 }
526 PG_END_TRY();
527
528 /*
529 * return a legal object so the interpreter will continue on its merry way
530 */
532}
#define Assert(condition)
Definition c.h:873
#define dgettext(d, x)
Definition c.h:1169
void(* pg_funcptr_t)(void)
Definition c.h:470
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition dynahash.c:952
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition dynahash.c:358
int err_generic_string(int field, const char *str)
Definition elog.c:1546
int errmsg_internal(const char *fmt,...)
Definition elog.c:1170
void FreeErrorData(ErrorData *edata)
Definition elog.c:1835
int errdetail_internal(const char *fmt,...)
Definition elog.c:1243
ErrorData * CopyErrorData(void)
Definition elog.c:1763
void FlushErrorState(void)
Definition elog.c:1884
int errhint(const char *fmt,...)
Definition elog.c:1330
int errcode(int sqlerrcode)
Definition elog.c:863
char * unpack_sql_state(int sql_state)
Definition elog.c:3222
#define LOG
Definition elog.h:31
#define FATAL
Definition elog.h:41
#define PG_TRY(...)
Definition elog.h:372
#define WARNING
Definition elog.h:36
#define DEBUG2
Definition elog.h:29
#define PG_END_TRY(...)
Definition elog.h:397
#define ERROR
Definition elog.h:39
#define PG_CATCH(...)
Definition elog.h:382
#define MAKE_SQLSTATE(ch1, ch2, ch3, ch4, ch5)
Definition elog.h:56
#define TEXTDOMAIN
Definition elog.h:153
#define NOTICE
Definition elog.h:35
#define INFO
Definition elog.h:34
#define ereport(elevel,...)
Definition elog.h:150
const char * str
@ HASH_ENTER
Definition hsearch.h:114
#define HASH_ELEM
Definition hsearch.h:95
#define HASH_BLOBS
Definition hsearch.h:97
static struct @172 value
int i
Definition isn.c:77
#define PLy_elog
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition mbutils.c:1559
char * pstrdup(const char *in)
Definition mcxt.c:1781
void pfree(void *pointer)
Definition mcxt.c:1616
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
PyObject * PLy_cursor(PyObject *self, PyObject *args)
void PLy_cursor_init_type(void)
PyObject * PLy_exc_error
Definition plpy_elog.c:15
PyObject * PLy_exc_spi_error
Definition plpy_elog.c:17
void PLy_exception_set_with_details(PyObject *excclass, ErrorData *edata)
Definition plpy_elog.c:522
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition plpy_elog.c:490
PyObject * PLy_exc_fatal
Definition plpy_elog.c:16
void PLy_plan_init_type(void)
PyMODINIT_FUNC PyInit_plpy(void)
static PyObject * PLy_notice(PyObject *self, PyObject *args, PyObject *kw)
static PyMethodDef PLy_exc_methods[]
static PyObject * PLy_quote_ident(PyObject *self, PyObject *args)
static char * object_to_string(PyObject *obj)
static PyObject * PLy_debug(PyObject *self, PyObject *args, PyObject *kw)
static PyObject * PLy_quote_nullable(PyObject *self, PyObject *args)
static void PLy_generate_spi_exceptions(PyObject *mod, PyObject *base)
static PyModuleDef PLy_module
static PyObject * PLy_error(PyObject *self, PyObject *args, PyObject *kw)
static PyObject * PLy_log(PyObject *self, PyObject *args, PyObject *kw)
static PyObject * PLy_warning(PyObject *self, PyObject *args, PyObject *kw)
static PyObject * PLy_create_exception(char *name, PyObject *base, PyObject *dict, const char *modname, PyObject *mod)
static PyObject * PLy_quote_literal(PyObject *self, PyObject *args)
static PyMethodDef PLy_methods[]
static PyModuleDef PLy_exc_module
static PyObject * PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw)
static PyObject * PLy_info(PyObject *self, PyObject *args, PyObject *kw)
HTAB * PLy_spi_exceptions
static PyObject * PLy_fatal(PyObject *self, PyObject *args, PyObject *kw)
static const ExceptionMap exception_map[]
static void PLy_add_exceptions(PyObject *plpy)
void PLy_result_init_type(void)
PyObject * PLy_spi_prepare(PyObject *self, PyObject *args)
Definition plpy_spi.c:36
PyObject * PLy_commit(PyObject *self, PyObject *args)
Definition plpy_spi.c:446
PyObject * PLy_rollback(PyObject *self, PyObject *args)
Definition plpy_spi.c:493
PyObject * PLy_spi_execute(PyObject *self, PyObject *args)
Definition plpy_spi.c:150
PyObject * PLy_subtransaction_new(PyObject *self, PyObject *unused)
void PLy_subtransaction_init_type(void)
char * PLyUnicode_AsString(PyObject *unicode)
Definition plpy_util.c:81
PyObject * PLyUnicode_FromString(const char *s)
Definition plpy_util.c:116
#define PG_DIAG_SCHEMA_NAME
#define PG_DIAG_CONSTRAINT_NAME
#define PG_DIAG_DATATYPE_NAME
#define PG_DIAG_TABLE_NAME
#define PG_DIAG_COLUMN_NAME
static int fb(int x)
char * quote_literal_cstr(const char *rawstr)
Definition quote.c:101
const char * quote_identifier(const char *ident)
PyObject * exc
Definition plpy_spi.h:21
const char * name