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 17:13:01 Functions: 100.0 % 6 6 5 1 5
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (240..) days: 95.5 % 66 63 1 2 3 39 21 41
Legend: Lines: hit not hit Function coverage date bins:
(240..) days: 54.5 % 11 6 5 1 5

 Age         Owner                  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                 : 
 2905 peter_e                     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.
 2379 tgl                        31 ECB             :  */
                                 32                 : void
 2379 tgl                        33 GIC           1 : _PG_init(void)
                                 34                 : {
 2378 tgl                        35 ECB             :     /* Asserts verify that typedefs above match original declarations */
 2379                            36                 :     AssertVariableIsOfType(&PLyObject_AsString, PLyObject_AsString_t);
 2379 tgl                        37 GIC           1 :     PLyObject_AsString_p = (PLyObject_AsString_t)
                                 38               1 :         load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyObject_AsString",
 2379 tgl                        39 ECB             :                                true, NULL);
 2378                            40                 :     AssertVariableIsOfType(&PLyUnicode_FromStringAndSize, PLyUnicode_FromStringAndSize_t);
 2378 tgl                        41 GIC           1 :     PLyUnicode_FromStringAndSize_p = (PLyUnicode_FromStringAndSize_t)
                                 42               1 :         load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
 2378 tgl                        43 ECB             :                                true, NULL);
                                 44                 :     AssertVariableIsOfType(&hstoreUpgrade, hstoreUpgrade_t);
 2379 tgl                        45 GIC           1 :     hstoreUpgrade_p = (hstoreUpgrade_t)
                                 46               1 :         load_external_function("$libdir/hstore", "hstoreUpgrade",
 2379 tgl                        47 ECB             :                                true, NULL);
 2378                            48                 :     AssertVariableIsOfType(&hstoreUniquePairs, hstoreUniquePairs_t);
 2379 tgl                        49 GIC           1 :     hstoreUniquePairs_p = (hstoreUniquePairs_t)
                                 50               1 :         load_external_function("$libdir/hstore", "hstoreUniquePairs",
 2379 tgl                        51 ECB             :                                true, NULL);
 2378                            52                 :     AssertVariableIsOfType(&hstorePairs, hstorePairs_t);
 2379 tgl                        53 GIC           1 :     hstorePairs_p = (hstorePairs_t)
                                 54               1 :         load_external_function("$libdir/hstore", "hstorePairs",
 2379 tgl                        55 ECB             :                                true, NULL);
 2378                            56                 :     AssertVariableIsOfType(&hstoreCheckKeyLen, hstoreCheckKeyLen_t);
 2379 tgl                        57 GIC           1 :     hstoreCheckKeyLen_p = (hstoreCheckKeyLen_t)
                                 58               1 :         load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
 2379 tgl                        59 ECB             :                                true, NULL);
 2378                            60                 :     AssertVariableIsOfType(&hstoreCheckValLen, hstoreCheckValLen_t);
 2379 tgl                        61 GIC           1 :     hstoreCheckValLen_p = (hstoreCheckValLen_t)
 2379 tgl                        62 CBC           1 :         load_external_function("$libdir/hstore", "hstoreCheckValLen",
                                 63                 :                                true, NULL);
 2379 tgl                        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
 2379 tgl                        75 ECB             : 
                                 76                 : 
 2905 peter_e                    77 GIC           2 : PG_FUNCTION_INFO_V1(hstore_to_plpython);
 2905 peter_e                    78 ECB             : 
                                 79                 : Datum
 2905 peter_e                    80 CBC           7 : hstore_to_plpython(PG_FUNCTION_ARGS)
                                 81                 : {
 2029 tgl                        82               7 :     HStore     *in = PG_GETARG_HSTORE_P(0);
 2905 peter_e                    83 ECB             :     int         i;
 2905 peter_e                    84 CBC           7 :     int         count = HS_COUNT(in);
 2905 peter_e                    85 GIC           7 :     char       *base = STRPTR(in);
                                 86               7 :     HEntry     *entries = ARRPTR(in);
 2905 peter_e                    87 ECB             :     PyObject   *dict;
                                 88                 : 
 2905 peter_e                    89 GBC           7 :     dict = PyDict_New();
 1986 peter_e                    90 GIC           7 :     if (!dict)
 1986 peter_e                    91 UIC           0 :         ereport(ERROR,
                                 92                 :                 (errcode(ERRCODE_OUT_OF_MEMORY),
 1986 peter_e                    93 ECB             :                  errmsg("out of memory")));
                                 94                 : 
 2905 peter_e                    95 GIC          20 :     for (i = 0; i < count; i++)
                                 96                 :     {
 2905 peter_e                    97 ECB             :         PyObject   *key;
                                 98                 : 
  398 andres                     99 CBC          13 :         key = PLyUnicode_FromStringAndSize(HSTORE_KEY(entries, base, i),
  332 tgl                       100              13 :                                            HSTORE_KEYLEN(entries, i));
 2698 tgl                       101 GIC          13 :         if (HSTORE_VALISNULL(entries, i))
 2905 peter_e                   102               6 :             PyDict_SetItem(dict, key, Py_None);
                                103                 :         else
                                104                 :         {
 2878 bruce                     105 ECB             :             PyObject   *value;
 2905 peter_e                   106                 : 
  398 andres                    107 CBC           7 :             value = PLyUnicode_FromStringAndSize(HSTORE_VAL(entries, base, i),
  332 tgl                       108               7 :                                                  HSTORE_VALLEN(entries, i));
 2905 peter_e                   109 GIC           7 :             PyDict_SetItem(dict, key, value);
 2905 peter_e                   110 CBC           7 :             Py_XDECREF(value);
                                111                 :         }
 2905 peter_e                   112 GIC          13 :         Py_XDECREF(key);
 2905 peter_e                   113 ECB             :     }
                                114                 : 
 2905 peter_e                   115 GIC           7 :     return PointerGetDatum(dict);
                                116                 : }
 2905 peter_e                   117 ECB             : 
                                118                 : 
 2905 peter_e                   119 GIC           2 : PG_FUNCTION_INFO_V1(plpython_to_hstore);
 2905 peter_e                   120 ECB             : 
                                121                 : Datum
 2905 peter_e                   122 GIC           7 : plpython_to_hstore(PG_FUNCTION_ARGS)
                                123                 : {
                                124                 :     PyObject   *dict;
                                125                 :     PyObject   *volatile items;
                                126                 :     Py_ssize_t  pcount;
 1464 tgl                       127 ECB             :     HStore     *volatile out;
 2905 peter_e                   128                 : 
 2905 peter_e                   129 GBC           7 :     dict = (PyObject *) PG_GETARG_POINTER(0);
 2905 peter_e                   130 GIC           7 :     if (!PyMapping_Check(dict))
 2905 peter_e                   131 UIC           0 :         ereport(ERROR,
                                132                 :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
 2905 peter_e                   133 ECB             :                  errmsg("not a Python mapping")));
                                134                 : 
 2905 peter_e                   135 GIC           7 :     pcount = PyMapping_Size(dict);
 1487 peter                     136 CBC           7 :     items = PyMapping_Items(dict);
                                137                 : 
 2905 peter_e                   138 GIC           7 :     PG_TRY();
                                139                 :     {
                                140                 :         int32       buflen;
                                141                 :         Py_ssize_t  i;
 2905 peter_e                   142 ECB             :         Pairs      *pairs;
                                143                 : 
 2905 peter_e                   144 CBC           7 :         pairs = palloc(pcount * sizeof(*pairs));
                                145                 : 
 2905 peter_e                   146 GIC          26 :         for (i = 0; i < pcount; i++)
                                147                 :         {
                                148                 :             PyObject   *tuple;
                                149                 :             PyObject   *key;
 2878 bruce                     150 ECB             :             PyObject   *value;
 2905 peter_e                   151                 : 
 2905 peter_e                   152 CBC          19 :             tuple = PyList_GetItem(items, i);
 2905 peter_e                   153 GIC          19 :             key = PyTuple_GetItem(tuple, 0);
 2905 peter_e                   154 CBC          19 :             value = PyTuple_GetItem(tuple, 1);
 2905 peter_e                   155 ECB             : 
 2905 peter_e                   156 CBC          19 :             pairs[i].key = PLyObject_AsString(key);
 2905 peter_e                   157 GIC          19 :             pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
 2905 peter_e                   158 CBC          19 :             pairs[i].needfree = true;
                                159                 : 
                                160              19 :             if (value == Py_None)
 2905 peter_e                   161 ECB             :             {
 2905 peter_e                   162 CBC           6 :                 pairs[i].val = NULL;
 2905 peter_e                   163 GIC           6 :                 pairs[i].vallen = 0;
                                164               6 :                 pairs[i].isnull = true;
                                165                 :             }
 2905 peter_e                   166 ECB             :             else
                                167                 :             {
 2905 peter_e                   168 CBC          13 :                 pairs[i].val = PLyObject_AsString(value);
 2905 peter_e                   169 GIC          13 :                 pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
                                170              13 :                 pairs[i].isnull = false;
                                171                 :             }
 2905 peter_e                   172 ECB             :         }
                                173                 : 
 2905 peter_e                   174 GIC           7 :         pcount = hstoreUniquePairs(pairs, pcount, &buflen);
 2905 peter_e                   175 GBC           7 :         out = hstorePairs(pairs, pcount, buflen);
                                176                 :     }
 1255 peter                     177 LBC           0 :     PG_FINALLY();
                                178                 :     {
 1487 peter                     179 CBC           7 :         Py_DECREF(items);
                                180                 :     }
 2905 peter_e                   181               7 :     PG_END_TRY();
                                182                 : 
 2905 peter_e                   183 GIC           7 :     PG_RETURN_POINTER(out);
                                184                 : }
        

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