diff options
Diffstat (limited to 'contrib/hstore_plpython/hstore_plpython.c')
-rw-r--r-- | contrib/hstore_plpython/hstore_plpython.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/contrib/hstore_plpython/hstore_plpython.c b/contrib/hstore_plpython/hstore_plpython.c index 6f2751a8df4..ce296ca486c 100644 --- a/contrib/hstore_plpython/hstore_plpython.c +++ b/contrib/hstore_plpython/hstore_plpython.c @@ -1,4 +1,5 @@ #include "postgres.h" + #include "fmgr.h" #include "plpython.h" #include "plpy_typeio.h" @@ -6,6 +7,70 @@ PG_MODULE_MAGIC; +extern void _PG_init(void); + +/* Linkage to functions in plpython module */ +typedef char *(*PLyObject_AsString_t) (PyObject *plrv); + +static PLyObject_AsString_t PLyObject_AsString_p; + +/* Linkage to functions in hstore module */ +typedef HStore *(*hstoreUpgrade_t) (Datum orig); +typedef int (*hstoreUniquePairs_t) (Pairs *a, int32 l, int32 *buflen); +typedef HStore *(*hstorePairs_t) (Pairs *pairs, int32 pcount, int32 buflen); +typedef size_t (*hstoreCheckKeyLen_t) (size_t len); +typedef size_t (*hstoreCheckValLen_t) (size_t len); + +static hstoreUpgrade_t hstoreUpgrade_p; +static hstoreUniquePairs_t hstoreUniquePairs_p; +static hstorePairs_t hstorePairs_p; +static hstoreCheckKeyLen_t hstoreCheckKeyLen_p; +static hstoreCheckValLen_t hstoreCheckValLen_p; + + +/* + * Module initialize function: fetch function pointers for cross-module calls. + */ +void +_PG_init(void) +{ + /* These asserts verify that typedefs above match original declarations */ + AssertVariableIsOfType(&PLyObject_AsString, PLyObject_AsString_t); + AssertVariableIsOfType(&hstoreUpgrade, hstoreUpgrade_t); + AssertVariableIsOfType(&hstoreUniquePairs, hstoreUniquePairs_t); + AssertVariableIsOfType(&hstorePairs, hstorePairs_t); + AssertVariableIsOfType(&hstoreCheckKeyLen, hstoreCheckKeyLen_t); + AssertVariableIsOfType(&hstoreCheckValLen, hstoreCheckValLen_t); + + PLyObject_AsString_p = (PLyObject_AsString_t) + load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyObject_AsString", + true, NULL); + hstoreUpgrade_p = (hstoreUpgrade_t) + load_external_function("$libdir/hstore", "hstoreUpgrade", + true, NULL); + hstoreUniquePairs_p = (hstoreUniquePairs_t) + load_external_function("$libdir/hstore", "hstoreUniquePairs", + true, NULL); + hstorePairs_p = (hstorePairs_t) + load_external_function("$libdir/hstore", "hstorePairs", + true, NULL); + hstoreCheckKeyLen_p = (hstoreCheckKeyLen_t) + load_external_function("$libdir/hstore", "hstoreCheckKeyLen", + true, NULL); + hstoreCheckValLen_p = (hstoreCheckValLen_t) + load_external_function("$libdir/hstore", "hstoreCheckValLen", + true, NULL); +} + + +/* These defines must be after the module init function */ +#define PLyObject_AsString PLyObject_AsString_p +#define hstoreUpgrade hstoreUpgrade_p +#define hstoreUniquePairs hstoreUniquePairs_p +#define hstorePairs hstorePairs_p +#define hstoreCheckKeyLen hstoreCheckKeyLen_p +#define hstoreCheckValLen hstoreCheckValLen_p + PG_FUNCTION_INFO_V1(hstore_to_plpython); |