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