PostgreSQL Source Code  git master
ltree_plpython.c
Go to the documentation of this file.
1 #include "postgres.h"
2 
3 #include "fmgr.h"
4 #include "plpython.h"
5 #include "ltree.h"
6 
8 
9 extern void _PG_init(void);
10 
11 /* Linkage to functions in plpython module */
12 #if PY_MAJOR_VERSION >= 3
13 typedef PyObject *(*PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size);
14 static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p;
15 #endif
16 
17 
18 /*
19  * Module initialize function: fetch function pointers for cross-module calls.
20  */
21 void
22 _PG_init(void)
23 {
24  /* Asserts verify that typedefs above match original declarations */
25 #if PY_MAJOR_VERSION >= 3
26  AssertVariableIsOfType(&PLyUnicode_FromStringAndSize, PLyUnicode_FromStringAndSize_t);
27  PLyUnicode_FromStringAndSize_p = (PLyUnicode_FromStringAndSize_t)
28  load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
29  true, NULL);
30 #endif
31 }
32 
33 
34 /* These defines must be after the module init function */
35 #define PLyUnicode_FromStringAndSize PLyUnicode_FromStringAndSize_p
36 
37 
39 
40 Datum
42 {
43  ltree *in = PG_GETARG_LTREE_P(0);
44  int i;
45  PyObject *list;
46  ltree_level *curlevel;
47 
48  list = PyList_New(in->numlevel);
49  if (!list)
50  ereport(ERROR,
51  (errcode(ERRCODE_OUT_OF_MEMORY),
52  errmsg("out of memory")));
53 
54  curlevel = LTREE_FIRST(in);
55  for (i = 0; i < in->numlevel; i++)
56  {
57  PyList_SetItem(list, i, PyString_FromStringAndSize(curlevel->name, curlevel->len));
58  curlevel = LEVEL_NEXT(curlevel);
59  }
60 
61  PG_FREE_IF_COPY(in, 0);
62 
63  return PointerGetDatum(list);
64 }
char name[FLEXIBLE_ARRAY_MEMBER]
Definition: ltree.h:13
void _PG_init(void)
uint16 len
Definition: ltree.h:12
#define PointerGetDatum(X)
Definition: postgres.h:562
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_LTREE_P(n)
Definition: ltree.h:171
#define PLyUnicode_FromStringAndSize
int Py_ssize_t
Definition: plpython.h:64
#define ERROR
Definition: elog.h:43
Datum ltree_to_plpython(PG_FUNCTION_ARGS)
#define LEVEL_NEXT(x)
Definition: ltree.h:17
PGFunction load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:94
#define ereport(elevel, rest)
Definition: elog.h:122
Definition: ltree.h:19
uint16 numlevel
Definition: ltree.h:22
uintptr_t Datum
Definition: postgres.h:372
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
PG_FUNCTION_INFO_V1(ltree_to_plpython)
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define LTREE_FIRST(x)
Definition: ltree.h:27
int i
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
PG_MODULE_MAGIC
Definition: ltree_plpython.c:7
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:779