PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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(0);
44  int i;
45  PyObject *list;
46  ltree_level *curlevel;
47 
48  list = PyList_New(in->numlevel);
49 
50  curlevel = LTREE_FIRST(in);
51  for (i = 0; i < in->numlevel; i++)
52  {
53  PyList_SetItem(list, i, PyString_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 }
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
#define PLyUnicode_FromStringAndSize
int Py_ssize_t
Definition: plpython.h:64
#define PG_GETARG_LTREE(x)
Definition: ltree.h:168
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
Definition: ltree.h:19
uint16 numlevel
Definition: ltree.h:22
uintptr_t Datum
Definition: postgres.h:372
#define NULL
Definition: c.h:229
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
PG_FUNCTION_INFO_V1(ltree_to_plpython)
tuple list
Definition: sort-test.py:11
#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:783