Age Owner TLA Line data Source code
1 : #include "postgres.h"
2 :
3 : #include "fmgr.h"
4 : #include "ltree/ltree.h"
5 : #include "plpython.h"
6 :
2905 peter_e 7 CBC 1 : PG_MODULE_MAGIC;
8 :
9 : /* Linkage to functions in plpython module */
10 : typedef PyObject *(*PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size);
11 : static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p;
12 :
13 :
14 : /*
15 : * Module initialize function: fetch function pointers for cross-module calls.
2378 tgl 16 ECB : */
17 : void
2378 tgl 18 GIC 1 : _PG_init(void)
19 : {
2378 tgl 20 ECB : /* Asserts verify that typedefs above match original declarations */
21 : AssertVariableIsOfType(&PLyUnicode_FromStringAndSize, PLyUnicode_FromStringAndSize_t);
2378 tgl 22 GIC 1 : PLyUnicode_FromStringAndSize_p = (PLyUnicode_FromStringAndSize_t)
2378 tgl 23 CBC 1 : load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
24 : true, NULL);
2378 tgl 25 GIC 1 : }
26 :
27 :
28 : /* These defines must be after the module init function */
29 : #define PLyUnicode_FromStringAndSize PLyUnicode_FromStringAndSize_p
2378 tgl 30 ECB :
31 :
2905 peter_e 32 GIC 2 : PG_FUNCTION_INFO_V1(ltree_to_plpython);
2905 peter_e 33 ECB :
34 : Datum
2905 peter_e 35 CBC 2 : ltree_to_plpython(PG_FUNCTION_ARGS)
36 : {
2029 tgl 37 GIC 2 : ltree *in = PG_GETARG_LTREE_P(0);
38 : int i;
39 : PyObject *list;
2905 peter_e 40 ECB : ltree_level *curlevel;
41 :
2905 peter_e 42 GBC 2 : list = PyList_New(in->numlevel);
1986 peter_e 43 GIC 2 : if (!list)
1986 peter_e 44 UIC 0 : ereport(ERROR,
45 : (errcode(ERRCODE_OUT_OF_MEMORY),
1986 peter_e 46 ECB : errmsg("out of memory")));
2905 47 :
2905 peter_e 48 GIC 2 : curlevel = LTREE_FIRST(in);
2905 peter_e 49 CBC 8 : for (i = 0; i < in->numlevel; i++)
2905 peter_e 50 ECB : {
398 andres 51 GIC 6 : PyList_SetItem(list, i, PLyUnicode_FromStringAndSize(curlevel->name, curlevel->len));
2905 peter_e 52 6 : curlevel = LEVEL_NEXT(curlevel);
2905 peter_e 53 ECB : }
54 :
2905 peter_e 55 CBC 2 : PG_FREE_IF_COPY(in, 0);
56 :
2905 peter_e 57 GIC 2 : return PointerGetDatum(list);
58 : }
|