Header And Logo

PostgreSQL
| The world's most advanced open source database.

Functions

plperl_helpers.h File Reference

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

static char * utf_u2e (char *utf8_str, size_t len)
static char * utf_e2u (const char *str)
static char * sv2cstr (SV *sv)
static SV * cstr2sv (const char *str)

Function Documentation

static SV* cstr2sv ( const char *  str  )  [inline, static]

Definition at line 117 of file plperl_helpers.h.

References GetDatabaseEncoding(), pfree(), PG_SQL_ASCII, and utf_e2u().

Referenced by make_array_ref(), plperl_call_perl_func(), plperl_call_perl_trigger_func(), plperl_create_sub(), plperl_hash_from_tuple(), plperl_spi_execute_fetch_result(), plperl_spi_prepare(), plperl_spi_query(), plperl_spi_query_prepared(), and plperl_trigger_build_args().

{
    SV         *sv;
    char       *utf8_str;

    /* no conversion when SQL_ASCII */
    if (GetDatabaseEncoding() == PG_SQL_ASCII)
        return newSVpv(str, 0);

    utf8_str = utf_e2u(str);

    sv = newSVpv(utf8_str, 0);
    SvUTF8_on(sv);
    pfree(utf8_str);

    return sv;
}

static char* sv2cstr ( SV *  sv  )  [inline, static]

Definition at line 60 of file plperl_helpers.h.

References GetDatabaseEncoding(), isGV_with_GP, PG_SQL_ASCII, SvREFCNT_inc_simple_void, utf_u2e(), and val.

Referenced by hek2cstr(), plperl_call_perl_func(), plperl_call_perl_trigger_func(), plperl_create_sub(), plperl_init_interp(), plperl_spi_prepare(), plperl_sv_to_datum(), plperl_trigger_handler(), plperl_trusted_init(), plperl_untrusted_init(), and select_perl_context().

{
    char       *val,
               *res;
    STRLEN      len;

    /*
     * get a utf8 encoded char * out of perl. *note* it may not be valid utf8!
     */

    /*
     * SvPVutf8() croaks nastily on certain things, like typeglobs and
     * readonly objects such as $^V. That's a perl bug - it's not supposed to
     * happen. To avoid crashing the backend, we make a copy of the sv before
     * passing it to SvPVutf8(). The copy is garbage collected when we're done
     * with it.
     */
    if (SvREADONLY(sv) ||
        isGV_with_GP(sv) ||
        (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM))
        sv = newSVsv(sv);
    else
    {
        /*
         * increase the reference count so we can just SvREFCNT_dec() it when
         * we are done
         */
        SvREFCNT_inc_simple_void(sv);
    }

    /*
     * Request the string from Perl, in UTF-8 encoding; but if we're in a
     * SQL_ASCII database, just request the byte soup without trying to make it
     * UTF8, because that might fail.
     */
    if (GetDatabaseEncoding() == PG_SQL_ASCII)
        val = SvPV(sv, len);
    else
        val = SvPVutf8(sv, len);

    /*
     * Now convert to database encoding.  We use perl's length in the event we
     * had an embedded null byte to ensure we error out properly.
     */
    res = utf_u2e(val, len);

    /* safe now to garbage collect the new SV */
    SvREFCNT_dec(sv);

    return res;
}

static char* utf_e2u ( const char *  str  )  [inline, static]

Definition at line 42 of file plperl_helpers.h.

References GetDatabaseEncoding(), pg_do_encoding_conversion(), and pstrdup().

Referenced by cstr2sv().

{
    char       *ret =
        (char *) pg_do_encoding_conversion((unsigned char *) str, strlen(str),
                                           GetDatabaseEncoding(), PG_UTF8);

    if (ret == str)
        ret = pstrdup(ret);
    return ret;
}

static char* utf_u2e ( char *  utf8_str,
size_t  len 
) [inline, static]

Definition at line 10 of file plperl_helpers.h.

References enc, GetDatabaseEncoding(), pg_do_encoding_conversion(), PG_SQL_ASCII, PG_UTF8, pg_verify_mbstr_len(), and pstrdup().

Referenced by sv2cstr().

{
    int         enc = GetDatabaseEncoding();
    char       *ret;

    /*
     * When we are in a PG_UTF8 or SQL_ASCII database
     * pg_do_encoding_conversion() will not do any conversion (which is good)
     * or verification (not so much), so we need to run the verification step
     * separately.
     */
    if (enc == PG_UTF8 || enc == PG_SQL_ASCII)
    {
        pg_verify_mbstr_len(enc, utf8_str, len, false);
        ret = utf8_str;
    }
    else
        ret = (char *) pg_do_encoding_conversion((unsigned char *) utf8_str,
                                                 len, PG_UTF8, enc);

    if (ret == utf8_str)
        ret = pstrdup(ret);

    return ret;
}