LCOV - differential code coverage report
Current view: top level - contrib/hstore_plpython - hstore_plpython.c (source / functions) Coverage Total Hit LBC UIC GBC GIC CBC ECB
Current: Differential Code Coverage HEAD vs 15 Lines: 95.5 % 66 63 1 2 3 39 21 41
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 6 6 5 1 5
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

           TLA  Line data    Source code
       1                 : #include "postgres.h"
       2                 : 
       3                 : #include "fmgr.h"
       4                 : #include "hstore/hstore.h"
       5                 : #include "plpy_typeio.h"
       6                 : #include "plpython.h"
       7                 : 
       8 CBC           1 : PG_MODULE_MAGIC;
       9                 : 
      10                 : /* Linkage to functions in plpython module */
      11                 : typedef char *(*PLyObject_AsString_t) (PyObject *plrv);
      12                 : static PLyObject_AsString_t PLyObject_AsString_p;
      13                 : typedef PyObject *(*PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size);
      14                 : static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p;
      15                 : 
      16                 : /* Linkage to functions in hstore module */
      17                 : typedef HStore *(*hstoreUpgrade_t) (Datum orig);
      18                 : static hstoreUpgrade_t hstoreUpgrade_p;
      19                 : typedef int (*hstoreUniquePairs_t) (Pairs *a, int32 l, int32 *buflen);
      20                 : static hstoreUniquePairs_t hstoreUniquePairs_p;
      21                 : typedef HStore *(*hstorePairs_t) (Pairs *pairs, int32 pcount, int32 buflen);
      22                 : static hstorePairs_t hstorePairs_p;
      23                 : typedef size_t (*hstoreCheckKeyLen_t) (size_t len);
      24                 : static hstoreCheckKeyLen_t hstoreCheckKeyLen_p;
      25                 : typedef size_t (*hstoreCheckValLen_t) (size_t len);
      26                 : static hstoreCheckValLen_t hstoreCheckValLen_p;
      27                 : 
      28                 : 
      29                 : /*
      30                 :  * Module initialize function: fetch function pointers for cross-module calls.
      31 ECB             :  */
      32                 : void
      33 GIC           1 : _PG_init(void)
      34                 : {
      35 ECB             :     /* Asserts verify that typedefs above match original declarations */
      36                 :     AssertVariableIsOfType(&PLyObject_AsString, PLyObject_AsString_t);
      37 GIC           1 :     PLyObject_AsString_p = (PLyObject_AsString_t)
      38               1 :         load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyObject_AsString",
      39 ECB             :                                true, NULL);
      40                 :     AssertVariableIsOfType(&PLyUnicode_FromStringAndSize, PLyUnicode_FromStringAndSize_t);
      41 GIC           1 :     PLyUnicode_FromStringAndSize_p = (PLyUnicode_FromStringAndSize_t)
      42               1 :         load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
      43 ECB             :                                true, NULL);
      44                 :     AssertVariableIsOfType(&hstoreUpgrade, hstoreUpgrade_t);
      45 GIC           1 :     hstoreUpgrade_p = (hstoreUpgrade_t)
      46               1 :         load_external_function("$libdir/hstore", "hstoreUpgrade",
      47 ECB             :                                true, NULL);
      48                 :     AssertVariableIsOfType(&hstoreUniquePairs, hstoreUniquePairs_t);
      49 GIC           1 :     hstoreUniquePairs_p = (hstoreUniquePairs_t)
      50               1 :         load_external_function("$libdir/hstore", "hstoreUniquePairs",
      51 ECB             :                                true, NULL);
      52                 :     AssertVariableIsOfType(&hstorePairs, hstorePairs_t);
      53 GIC           1 :     hstorePairs_p = (hstorePairs_t)
      54               1 :         load_external_function("$libdir/hstore", "hstorePairs",
      55 ECB             :                                true, NULL);
      56                 :     AssertVariableIsOfType(&hstoreCheckKeyLen, hstoreCheckKeyLen_t);
      57 GIC           1 :     hstoreCheckKeyLen_p = (hstoreCheckKeyLen_t)
      58               1 :         load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
      59 ECB             :                                true, NULL);
      60                 :     AssertVariableIsOfType(&hstoreCheckValLen, hstoreCheckValLen_t);
      61 GIC           1 :     hstoreCheckValLen_p = (hstoreCheckValLen_t)
      62 CBC           1 :         load_external_function("$libdir/hstore", "hstoreCheckValLen",
      63                 :                                true, NULL);
      64 GIC           1 : }
      65                 : 
      66                 : 
      67                 : /* These defines must be after the module init function */
      68                 : #define PLyObject_AsString PLyObject_AsString_p
      69                 : #define PLyUnicode_FromStringAndSize PLyUnicode_FromStringAndSize_p
      70                 : #define hstoreUpgrade hstoreUpgrade_p
      71                 : #define hstoreUniquePairs hstoreUniquePairs_p
      72                 : #define hstorePairs hstorePairs_p
      73                 : #define hstoreCheckKeyLen hstoreCheckKeyLen_p
      74                 : #define hstoreCheckValLen hstoreCheckValLen_p
      75 ECB             : 
      76                 : 
      77 GIC           2 : PG_FUNCTION_INFO_V1(hstore_to_plpython);
      78 ECB             : 
      79                 : Datum
      80 CBC           7 : hstore_to_plpython(PG_FUNCTION_ARGS)
      81                 : {
      82               7 :     HStore     *in = PG_GETARG_HSTORE_P(0);
      83 ECB             :     int         i;
      84 CBC           7 :     int         count = HS_COUNT(in);
      85 GIC           7 :     char       *base = STRPTR(in);
      86               7 :     HEntry     *entries = ARRPTR(in);
      87 ECB             :     PyObject   *dict;
      88                 : 
      89 GBC           7 :     dict = PyDict_New();
      90 GIC           7 :     if (!dict)
      91 UIC           0 :         ereport(ERROR,
      92                 :                 (errcode(ERRCODE_OUT_OF_MEMORY),
      93 ECB             :                  errmsg("out of memory")));
      94                 : 
      95 GIC          20 :     for (i = 0; i < count; i++)
      96                 :     {
      97 ECB             :         PyObject   *key;
      98                 : 
      99 CBC          13 :         key = PLyUnicode_FromStringAndSize(HSTORE_KEY(entries, base, i),
     100              13 :                                            HSTORE_KEYLEN(entries, i));
     101 GIC          13 :         if (HSTORE_VALISNULL(entries, i))
     102               6 :             PyDict_SetItem(dict, key, Py_None);
     103                 :         else
     104                 :         {
     105 ECB             :             PyObject   *value;
     106                 : 
     107 CBC           7 :             value = PLyUnicode_FromStringAndSize(HSTORE_VAL(entries, base, i),
     108               7 :                                                  HSTORE_VALLEN(entries, i));
     109 GIC           7 :             PyDict_SetItem(dict, key, value);
     110 CBC           7 :             Py_XDECREF(value);
     111                 :         }
     112 GIC          13 :         Py_XDECREF(key);
     113 ECB             :     }
     114                 : 
     115 GIC           7 :     return PointerGetDatum(dict);
     116                 : }
     117 ECB             : 
     118                 : 
     119 GIC           2 : PG_FUNCTION_INFO_V1(plpython_to_hstore);
     120 ECB             : 
     121                 : Datum
     122 GIC           7 : plpython_to_hstore(PG_FUNCTION_ARGS)
     123                 : {
     124                 :     PyObject   *dict;
     125                 :     PyObject   *volatile items;
     126                 :     Py_ssize_t  pcount;
     127 ECB             :     HStore     *volatile out;
     128                 : 
     129 GBC           7 :     dict = (PyObject *) PG_GETARG_POINTER(0);
     130 GIC           7 :     if (!PyMapping_Check(dict))
     131 UIC           0 :         ereport(ERROR,
     132                 :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     133 ECB             :                  errmsg("not a Python mapping")));
     134                 : 
     135 GIC           7 :     pcount = PyMapping_Size(dict);
     136 CBC           7 :     items = PyMapping_Items(dict);
     137                 : 
     138 GIC           7 :     PG_TRY();
     139                 :     {
     140                 :         int32       buflen;
     141                 :         Py_ssize_t  i;
     142 ECB             :         Pairs      *pairs;
     143                 : 
     144 CBC           7 :         pairs = palloc(pcount * sizeof(*pairs));
     145                 : 
     146 GIC          26 :         for (i = 0; i < pcount; i++)
     147                 :         {
     148                 :             PyObject   *tuple;
     149                 :             PyObject   *key;
     150 ECB             :             PyObject   *value;
     151                 : 
     152 CBC          19 :             tuple = PyList_GetItem(items, i);
     153 GIC          19 :             key = PyTuple_GetItem(tuple, 0);
     154 CBC          19 :             value = PyTuple_GetItem(tuple, 1);
     155 ECB             : 
     156 CBC          19 :             pairs[i].key = PLyObject_AsString(key);
     157 GIC          19 :             pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
     158 CBC          19 :             pairs[i].needfree = true;
     159                 : 
     160              19 :             if (value == Py_None)
     161 ECB             :             {
     162 CBC           6 :                 pairs[i].val = NULL;
     163 GIC           6 :                 pairs[i].vallen = 0;
     164               6 :                 pairs[i].isnull = true;
     165                 :             }
     166 ECB             :             else
     167                 :             {
     168 CBC          13 :                 pairs[i].val = PLyObject_AsString(value);
     169 GIC          13 :                 pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
     170              13 :                 pairs[i].isnull = false;
     171                 :             }
     172 ECB             :         }
     173                 : 
     174 GIC           7 :         pcount = hstoreUniquePairs(pairs, pcount, &buflen);
     175 GBC           7 :         out = hstorePairs(pairs, pcount, buflen);
     176                 :     }
     177 LBC           0 :     PG_FINALLY();
     178                 :     {
     179 CBC           7 :         Py_DECREF(items);
     180                 :     }
     181               7 :     PG_END_TRY();
     182                 : 
     183 GIC           7 :     PG_RETURN_POINTER(out);
     184                 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a