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 "ltree/ltree.h"
5 #include "plpython.h"
6 
8 
9 extern void _PG_init(void);
10 
11 /* Linkage to functions in plpython module */
12 typedef PyObject *(*PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size);
14 
15 
16 /*
17  * Module initialize function: fetch function pointers for cross-module calls.
18  */
19 void
20 _PG_init(void)
21 {
22  /* Asserts verify that typedefs above match original declarations */
25  load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
26  true, NULL);
27 }
28 
29 
30 /* These defines must be after the module init function */
31 #define PLyUnicode_FromStringAndSize PLyUnicode_FromStringAndSize_p
32 
33 
35 
36 Datum
38 {
39  ltree *in = PG_GETARG_LTREE_P(0);
40  int i;
41  PyObject *list;
42  ltree_level *curlevel;
43 
44  list = PyList_New(in->numlevel);
45  if (!list)
46  ereport(ERROR,
47  (errcode(ERRCODE_OUT_OF_MEMORY),
48  errmsg("out of memory")));
49 
50  curlevel = LTREE_FIRST(in);
51  for (i = 0; i < in->numlevel; i++)
52  {
53  PyList_SetItem(list, i, PLyUnicode_FromStringAndSize(curlevel->name, curlevel->len));
54  curlevel = LEVEL_NEXT(curlevel);
55  }
56 
57  PG_FREE_IF_COPY(in, 0);
58 
59  return PointerGetDatum(list);
60 }
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:963
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:105
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
int i
Definition: isn.c:73
#define LTREE_FIRST(x)
Definition: ltree.h:51
#define LEVEL_NEXT(x)
Definition: ltree.h:40
#define PG_GETARG_LTREE_P(n)
Definition: ltree.h:217
#define PLyUnicode_FromStringAndSize
void _PG_init(void)
PyObject *(* PLyUnicode_FromStringAndSize_t)(const char *s, Py_ssize_t size)
PG_MODULE_MAGIC
Definition: ltree_plpython.c:7
static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p
Datum ltree_to_plpython(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(ltree_to_plpython)
uintptr_t Datum
Definition: postgres.h:411
#define PointerGetDatum(X)
Definition: postgres.h:600
char name[FLEXIBLE_ARRAY_MEMBER]
Definition: ltree.h:36
uint16 len
Definition: ltree.h:35
Definition: ltree.h:43
uint16 numlevel
Definition: ltree.h:45