LCOV - code coverage report
Current view: top level - contrib/hstore_plperl - hstore_plperl.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 100.0 % 58 58
Test Date: 2026-03-18 04:15:34 Functions: 100.0 % 6 6
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            3 : 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            3 : _PG_init(void)
      37              : {
      38            3 :     hstoreUpgrade_p = (hstoreUpgrade_t)
      39            3 :         load_external_function("$libdir/hstore", "hstoreUpgrade",
      40              :                                true, NULL);
      41            3 :     hstoreUniquePairs_p = (hstoreUniquePairs_t)
      42            3 :         load_external_function("$libdir/hstore", "hstoreUniquePairs",
      43              :                                true, NULL);
      44            3 :     hstorePairs_p = (hstorePairs_t)
      45            3 :         load_external_function("$libdir/hstore", "hstorePairs",
      46              :                                true, NULL);
      47            3 :     hstoreCheckKeyLen_p = (hstoreCheckKeyLen_t)
      48            3 :         load_external_function("$libdir/hstore", "hstoreCheckKeyLen",
      49              :                                true, NULL);
      50            3 :     hstoreCheckValLen_p = (hstoreCheckValLen_t)
      51            3 :         load_external_function("$libdir/hstore", "hstoreCheckValLen",
      52              :                                true, NULL);
      53            3 : }
      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            5 : PG_FUNCTION_INFO_V1(hstore_to_plperl);
      65              : 
      66              : Datum
      67            7 : hstore_to_plperl(PG_FUNCTION_ARGS)
      68              : {
      69            7 :     dTHX;
      70            7 :     HStore     *in = PG_GETARG_HSTORE_P(0);
      71              :     int         i;
      72            7 :     int         count = HS_COUNT(in);
      73            7 :     char       *base = STRPTR(in);
      74            7 :     HEntry     *entries = ARRPTR(in);
      75              :     HV         *hv;
      76              : 
      77            7 :     hv = newHV();
      78              : 
      79           20 :     for (i = 0; i < count; i++)
      80              :     {
      81              :         const char *key;
      82              :         SV         *value;
      83              : 
      84           13 :         key = pnstrdup(HSTORE_KEY(entries, base, i),
      85           13 :                        HSTORE_KEYLEN(entries, i));
      86           13 :         value = HSTORE_VALISNULL(entries, i) ? newSV(0) :
      87            7 :             cstr2sv(pnstrdup(HSTORE_VAL(entries, base, i),
      88            7 :                              HSTORE_VALLEN(entries, i)));
      89              : 
      90           13 :         (void) hv_store(hv, key, strlen(key), value, 0);
      91              :     }
      92              : 
      93            7 :     return PointerGetDatum(newRV((SV *) hv));
      94              : }
      95              : 
      96              : 
      97            6 : PG_FUNCTION_INFO_V1(plperl_to_hstore);
      98              : 
      99              : Datum
     100            7 : plperl_to_hstore(PG_FUNCTION_ARGS)
     101              : {
     102            7 :     dTHX;
     103            7 :     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           13 :     while (SvROK(in))
     114            6 :         in = SvRV(in);
     115              : 
     116              :     /* Now we must have a hash. */
     117            7 :     if (SvTYPE(in) != SVt_PVHV)
     118            2 :         ereport(ERROR,
     119              :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     120              :                  errmsg("cannot transform non-hash Perl value to hstore")));
     121            5 :     hv = (HV *) in;
     122              : 
     123            5 :     pcount = hv_iterinit(hv);
     124              : 
     125            5 :     pairs = palloc(pcount * sizeof(Pairs));
     126              : 
     127            5 :     i = 0;
     128           18 :     while ((he = hv_iternext(hv)))
     129              :     {
     130           13 :         char       *key = sv2cstr(HeSVKEY_force(he));
     131           13 :         SV         *value = HeVAL(he);
     132              : 
     133           13 :         pairs[i].key = pstrdup(key);
     134           13 :         pairs[i].keylen = hstoreCheckKeyLen(strlen(pairs[i].key));
     135           13 :         pairs[i].needfree = true;
     136              : 
     137           13 :         if (!SvOK(value))
     138              :         {
     139            4 :             pairs[i].val = NULL;
     140            4 :             pairs[i].vallen = 0;
     141            4 :             pairs[i].isnull = true;
     142              :         }
     143              :         else
     144              :         {
     145            9 :             pairs[i].val = pstrdup(sv2cstr(value));
     146            9 :             pairs[i].vallen = hstoreCheckValLen(strlen(pairs[i].val));
     147            9 :             pairs[i].isnull = false;
     148              :         }
     149              : 
     150           13 :         i++;
     151              :     }
     152              : 
     153            5 :     pcount = hstoreUniquePairs(pairs, pcount, &buflen);
     154            5 :     out = hstorePairs(pairs, pcount, buflen);
     155            5 :     PG_RETURN_POINTER(out);
     156              : }
        

Generated by: LCOV version 2.0-1