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

Generated by: LCOV version 1.16