LCOV - code coverage report
Current view: top level - contrib/hstore_plperl - hstore_plperl.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 58 58 100.0 %
Date: 2024-04-24 23:13:15 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #include "postgres.h"
       2             : 
       3             : #include "fmgr.h"
       4             : #include "hstore/hstore.h"
       5             : #include "plperl.h"
       6             : 
       7           6 : PG_MODULE_MAGIC;
       8             : 
       9             : /* Linkage to functions in hstore module */
      10             : typedef HStore *(*hstoreUpgrade_t) (Datum orig);
      11             : static hstoreUpgrade_t hstoreUpgrade_p;
      12             : typedef int (*hstoreUniquePairs_t) (Pairs *a, int32 l, int32 *buflen);
      13             : static hstoreUniquePairs_t hstoreUniquePairs_p;
      14             : typedef HStore *(*hstorePairs_t) (Pairs *pairs, int32 pcount, int32 buflen);
      15             : static hstorePairs_t hstorePairs_p;
      16             : typedef size_t (*hstoreCheckKeyLen_t) (size_t len);
      17             : static hstoreCheckKeyLen_t hstoreCheckKeyLen_p;
      18             : typedef size_t (*hstoreCheckValLen_t) (size_t len);
      19             : static hstoreCheckValLen_t hstoreCheckValLen_p;
      20             : 
      21             : 
      22             : /*
      23             :  * Module initialize function: fetch function pointers for cross-module calls.
      24             :  */
      25             : void
      26           6 : _PG_init(void)
      27             : {
      28             :     /* Asserts verify that typedefs above match original declarations */
      29             :     AssertVariableIsOfType(&hstoreUpgrade, hstoreUpgrade_t);
      30           6 :     hstoreUpgrade_p = (hstoreUpgrade_t)
      31           6 :         load_external_function("$libdir/hstore", "hstoreUpgrade",
      32             :                                true, NULL);
      33             :     AssertVariableIsOfType(&hstoreUniquePairs, hstoreUniquePairs_t);
      34           6 :     hstoreUniquePairs_p = (hstoreUniquePairs_t)
      35           6 :         load_external_function("$libdir/hstore", "hstoreUniquePairs",
      36             :                                true, NULL);
      37             :     AssertVariableIsOfType(&hstorePairs, hstorePairs_t);
      38           6 :     hstorePairs_p = (hstorePairs_t)
      39           6 :         load_external_function("$libdir/hstore", "hstorePairs",
      40             :                                true, NULL);
      41             :     AssertVariableIsOfType(&hstoreCheckKeyLen, hstoreCheckKeyLen_t);
      42           6 :     hstoreCheckKeyLen_p = (hstoreCheckKeyLen_t)
      43           6 :         load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
      44             :                                true, NULL);
      45             :     AssertVariableIsOfType(&hstoreCheckValLen, hstoreCheckValLen_t);
      46           6 :     hstoreCheckValLen_p = (hstoreCheckValLen_t)
      47           6 :         load_external_function("$libdir/hstore", "hstoreCheckValLen",
      48             :                                true, NULL);
      49           6 : }
      50             : 
      51             : 
      52             : /* These defines must be after the module init function */
      53             : #define hstoreUpgrade hstoreUpgrade_p
      54             : #define hstoreUniquePairs hstoreUniquePairs_p
      55             : #define hstorePairs hstorePairs_p
      56             : #define hstoreCheckKeyLen hstoreCheckKeyLen_p
      57             : #define hstoreCheckValLen hstoreCheckValLen_p
      58             : 
      59             : 
      60          10 : PG_FUNCTION_INFO_V1(hstore_to_plperl);
      61             : 
      62             : Datum
      63          14 : hstore_to_plperl(PG_FUNCTION_ARGS)
      64             : {
      65          14 :     dTHX;
      66          14 :     HStore     *in = PG_GETARG_HSTORE_P(0);
      67             :     int         i;
      68          14 :     int         count = HS_COUNT(in);
      69          14 :     char       *base = STRPTR(in);
      70          14 :     HEntry     *entries = ARRPTR(in);
      71             :     HV         *hv;
      72             : 
      73          14 :     hv = newHV();
      74             : 
      75          40 :     for (i = 0; i < count; i++)
      76             :     {
      77             :         const char *key;
      78             :         SV         *value;
      79             : 
      80          26 :         key = pnstrdup(HSTORE_KEY(entries, base, i),
      81          26 :                        HSTORE_KEYLEN(entries, i));
      82          26 :         value = HSTORE_VALISNULL(entries, i) ? newSV(0) :
      83          14 :             cstr2sv(pnstrdup(HSTORE_VAL(entries, base, i),
      84          14 :                              HSTORE_VALLEN(entries, i)));
      85             : 
      86          26 :         (void) hv_store(hv, key, strlen(key), value, 0);
      87             :     }
      88             : 
      89          14 :     return PointerGetDatum(newRV((SV *) hv));
      90             : }
      91             : 
      92             : 
      93          12 : PG_FUNCTION_INFO_V1(plperl_to_hstore);
      94             : 
      95             : Datum
      96          14 : plperl_to_hstore(PG_FUNCTION_ARGS)
      97             : {
      98          14 :     dTHX;
      99          14 :     SV         *in = (SV *) PG_GETARG_POINTER(0);
     100             :     HV         *hv;
     101             :     HE         *he;
     102             :     int32       buflen;
     103             :     int32       i;
     104             :     int32       pcount;
     105             :     HStore     *out;
     106             :     Pairs      *pairs;
     107             : 
     108             :     /* Dereference references recursively. */
     109          26 :     while (SvROK(in))
     110          12 :         in = SvRV(in);
     111             : 
     112             :     /* Now we must have a hash. */
     113          14 :     if (SvTYPE(in) != SVt_PVHV)
     114           4 :         ereport(ERROR,
     115             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     116             :                  errmsg("cannot transform non-hash Perl value to hstore")));
     117          10 :     hv = (HV *) in;
     118             : 
     119          10 :     pcount = hv_iterinit(hv);
     120             : 
     121          10 :     pairs = palloc(pcount * sizeof(Pairs));
     122             : 
     123          10 :     i = 0;
     124          36 :     while ((he = hv_iternext(hv)))
     125             :     {
     126          26 :         char       *key = sv2cstr(HeSVKEY_force(he));
     127          26 :         SV         *value = HeVAL(he);
     128             : 
     129          26 :         pairs[i].key = pstrdup(key);
     130          26 :         pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
     131          26 :         pairs[i].needfree = true;
     132             : 
     133          26 :         if (!SvOK(value))
     134             :         {
     135           8 :             pairs[i].val = NULL;
     136           8 :             pairs[i].vallen = 0;
     137           8 :             pairs[i].isnull = true;
     138             :         }
     139             :         else
     140             :         {
     141          18 :             pairs[i].val = pstrdup(sv2cstr(value));
     142          18 :             pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
     143          18 :             pairs[i].isnull = false;
     144             :         }
     145             : 
     146          26 :         i++;
     147             :     }
     148             : 
     149          10 :     pcount = hstoreUniquePairs(pairs, pcount, &buflen);
     150          10 :     out = hstorePairs(pairs, pcount, buflen);
     151          10 :     PG_RETURN_POINTER(out);
     152             : }

Generated by: LCOV version 1.14