PostgreSQL Source Code: contrib/ltree_plpython/ltree_plpython.c Source File

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 "plpy_util.h"
6
7 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
17/*
18 * Module initialize function: fetch function pointers for cross-module calls.
19 */
20void
21 _PG_init(void)
22{
23 /* Asserts verify that typedefs above match original declarations */
24 AssertVariableIsOfType(&PLyUnicode_FromStringAndSize, PLyUnicode_FromStringAndSize_t);
25 PLyUnicode_FromStringAndSize_p = (PLyUnicode_FromStringAndSize_t)
26 load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
27 true, NULL);
28}
29
30
31/* These defines must be after the module init function */
32 #define PLyUnicode_FromStringAndSize PLyUnicode_FromStringAndSize_p
33
34
35 PG_FUNCTION_INFO_V1(ltree_to_plpython);
36
37Datum
38 ltree_to_plpython(PG_FUNCTION_ARGS)
39{
40 ltree *in = PG_GETARG_LTREE_P(0);
41 int i;
42 PyObject *list;
43 ltree_level *curlevel;
44
45 list = PyList_New(in->numlevel);
46 if (!list)
47 ereport(ERROR,
48 (errcode(ERRCODE_OUT_OF_MEMORY),
49 errmsg("out of memory")));
50
51 curlevel = LTREE_FIRST(in);
52 for (i = 0; i < in->numlevel; i++)
53 {
54 PyList_SetItem(list, i, PLyUnicode_FromStringAndSize(curlevel->name, curlevel->len));
55 curlevel = LEVEL_NEXT(curlevel);
56 }
57
58 PG_FREE_IF_COPY(in, 0);
59
60 return PointerGetDatum(list);
61}
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:980
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:95
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
i
int i
Definition: isn.c:77
#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:218
#define PLyUnicode_FromStringAndSize
Definition: ltree_plpython.c:32
void _PG_init(void)
Definition: ltree_plpython.c:21
PyObject *(* PLyUnicode_FromStringAndSize_t)(const char *s, Py_ssize_t size)
Definition: ltree_plpython.c:13
PG_MODULE_MAGIC_EXT(.name="ltree_plpython",.version=PG_VERSION)
static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p
Definition: ltree_plpython.c:14
Datum ltree_to_plpython(PG_FUNCTION_ARGS)
Definition: ltree_plpython.c:38
PG_FUNCTION_INFO_V1(ltree_to_plpython)
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332
uint64_t Datum
Definition: postgres.h:70
Definition: ltree.h:34
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
const char * name

AltStyle によって変換されたページ (->オリジナル) /