LCOV - code coverage report
Current view: top level - src/pl/plperl - Util.xs (source / functions) Coverage Total Hit
Test: Code coverage Lines: 0.0 % 7 0
Test Date: 2026-01-26 10:56:24 Functions: 0.0 % 1 0
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /**********************************************************************
       2              :  * PostgreSQL::InServer::Util
       3              :  *
       4              :  * src/pl/plperl/Util.xs
       5              :  *
       6              :  * Defines plperl interfaces for general-purpose utilities.
       7              :  * This module is bootstrapped as soon as an interpreter is initialized.
       8              :  * Currently doesn't define a PACKAGE= so all subs are in main:: to avoid
       9              :  * the need for explicit importing.
      10              :  *
      11              :  **********************************************************************/
      12              : 
      13              : /* this must be first: */
      14              : #include "postgres.h"
      15              : 
      16              : #include "fmgr.h"
      17              : #include "utils/builtins.h"
      18              : #include "utils/bytea.h"       /* for byteain & byteaout */
      19              : #include "varatt.h"
      20              : 
      21              : /* perl stuff */
      22              : #define PG_NEED_PERL_XSUB_H
      23              : #include "plperl.h"
      24              : 
      25              : 
      26              : static text *
      27            0 : sv2text(SV *sv)
      28              : {
      29            0 :         char       *str = sv2cstr(sv);
      30            0 :         text       *text;
      31              : 
      32            0 :         text = cstring_to_text(str);
      33            0 :         pfree(str);
      34            0 :         return text;
      35            0 : }
      36              : 
      37              : MODULE = PostgreSQL::InServer::Util PREFIX = util_
      38              : 
      39              : PROTOTYPES: ENABLE
      40              : VERSIONCHECK: DISABLE
      41              : 
      42              : int
      43              : _aliased_constants()
      44              :     PROTOTYPE:
      45              :     ALIAS:
      46              :         DEBUG   = DEBUG2
      47              :         LOG     = LOG
      48              :         INFO    = INFO
      49              :         NOTICE  = NOTICE
      50              :         WARNING = WARNING
      51              :         ERROR   = ERROR
      52              :     CODE:
      53              :     /* uses the ALIAS value as the return value */
      54              :     RETVAL = ix;
      55              :     OUTPUT:
      56              :     RETVAL
      57              : 
      58              : 
      59              : void
      60              : util_elog(level, msg)
      61              :     int level
      62              :     SV *msg
      63              :     CODE:
      64              :         if (level > ERROR)      /* no PANIC allowed thanks */
      65              :             level = ERROR;
      66              :         if (level < DEBUG5)
      67              :             level = DEBUG5;
      68              :         plperl_util_elog(level, msg);
      69              : 
      70              : SV *
      71              : util_quote_literal(sv)
      72              :     SV *sv
      73              :     CODE:
      74              :     if (!sv || !SvOK(sv)) {
      75              :         RETVAL = &PL_sv_undef;
      76              :     }
      77              :     else {
      78              :         text *arg = sv2text(sv);
      79              :                 text *quoted = DatumGetTextPP(DirectFunctionCall1(quote_literal, PointerGetDatum(arg)));
      80              :                 char *str;
      81              : 
      82              :                 pfree(arg);
      83              :                 str = text_to_cstring(quoted);
      84              :                 RETVAL = cstr2sv(str);
      85              :                 pfree(str);
      86              :     }
      87              :     OUTPUT:
      88              :     RETVAL
      89              : 
      90              : SV *
      91              : util_quote_nullable(sv)
      92              :     SV *sv
      93              :     CODE:
      94              :     if (!sv || !SvOK(sv))
      95              :         {
      96              :         RETVAL = cstr2sv("NULL");
      97              :     }
      98              :     else
      99              :         {
     100              :         text *arg = sv2text(sv);
     101              :                 text *quoted = DatumGetTextPP(DirectFunctionCall1(quote_nullable, PointerGetDatum(arg)));
     102              :                 char *str;
     103              : 
     104              :                 pfree(arg);
     105              :                 str = text_to_cstring(quoted);
     106              :                 RETVAL = cstr2sv(str);
     107              :                 pfree(str);
     108              :     }
     109              :     OUTPUT:
     110              :     RETVAL
     111              : 
     112              : SV *
     113              : util_quote_ident(sv)
     114              :     SV *sv
     115              :     PREINIT:
     116              :         text *arg;
     117              :                 text *quoted;
     118              :                 char *str;
     119              :     CODE:
     120              :         arg = sv2text(sv);
     121              :                 quoted = DatumGetTextPP(DirectFunctionCall1(quote_ident, PointerGetDatum(arg)));
     122              : 
     123              :                 pfree(arg);
     124              :                 str = text_to_cstring(quoted);
     125              :                 RETVAL = cstr2sv(str);
     126              :                 pfree(str);
     127              :     OUTPUT:
     128              :     RETVAL
     129              : 
     130              : SV *
     131              : util_decode_bytea(sv)
     132              :     SV *sv
     133              :     PREINIT:
     134              :         char *arg;
     135              :         text *ret;
     136              :     CODE:
     137              :         arg = SvPVbyte_nolen(sv);
     138              :         ret = DatumGetTextPP(DirectFunctionCall1(byteain, PointerGetDatum(arg)));
     139              :         /* not cstr2sv because this is raw bytes not utf8'able */
     140              :         RETVAL = newSVpvn(VARDATA_ANY(ret), VARSIZE_ANY_EXHDR(ret));
     141              :     OUTPUT:
     142              :     RETVAL
     143              : 
     144              : SV *
     145              : util_encode_bytea(sv)
     146              :     SV *sv
     147              :     PREINIT:
     148              :         text *arg;
     149              :         char *ret;
     150              :                 STRLEN len;
     151              :     CODE:
     152              :         /* not sv2text because this is raw bytes not utf8'able */
     153              :         ret = SvPVbyte(sv, len);
     154              :                 arg = cstring_to_text_with_len(ret, len);
     155              :         ret = DatumGetCString(DirectFunctionCall1(byteaout, PointerGetDatum(arg)));
     156              :         RETVAL = cstr2sv(ret);
     157              :     OUTPUT:
     158              :     RETVAL
     159              : 
     160              : SV *
     161              : looks_like_number(sv)
     162              :     SV *sv
     163              :     CODE:
     164              :     if (!SvOK(sv))
     165              :         RETVAL = &PL_sv_undef;
     166              :     else if ( looks_like_number(sv) )
     167              :         RETVAL = &PL_sv_yes;
     168              :     else
     169              :         RETVAL = &PL_sv_no;
     170              :     OUTPUT:
     171              :     RETVAL
     172              : 
     173              : SV *
     174              : encode_typed_literal(sv, typname)
     175              :         SV         *sv
     176              :         char   *typname;
     177              :         PREINIT:
     178              :                 char    *outstr;
     179              :         CODE:
     180              :                 outstr = plperl_sv_to_literal(sv, typname);
     181              :                 if (outstr == NULL)
     182              :                         RETVAL = &PL_sv_undef;
     183              :                 else
     184              :                         RETVAL = cstr2sv(outstr);
     185              :         OUTPUT:
     186              :         RETVAL
     187              : 
     188              : BOOT:
     189              :     items = 0;  /* avoid 'unused variable' warning */
        

Generated by: LCOV version 2.3.2-1