1 #ifndef PL_PERL_HELPERS_H
2 #define PL_PERL_HELPERS_H
4 #include "mb/pg_wchar.h"
10 * convert from utf8 to database encoding
12 * Returns a palloc'ed copy of the original string
15 utf_u2e(char *utf8_str
, size_t len
)
19 ret
= pg_any_to_server(utf8_str
, len
, PG_UTF8
);
21 /* ensure we have a copy even if no conversion happened */
29 * convert from database encoding to utf8
31 * Returns a palloc'ed copy of the original string
34 utf_e2u(const char *str
)
38 ret
= pg_server_to_any(str
, strlen(str
), PG_UTF8
);
40 /* ensure we have a copy even if no conversion happened */
49 * Convert an SV to a char * in the current database encoding
51 * Returns a palloc'ed copy of the original string
62 * get a utf8 encoded char * out of perl. *note* it may not be valid utf8!
66 * SvPVutf8() croaks nastily on certain things, like typeglobs and
67 * readonly objects such as $^V. That's a perl bug - it's not supposed to
68 * happen. To avoid crashing the backend, we make a copy of the sv before
69 * passing it to SvPVutf8(). The copy is garbage collected when we're done
74 (SvTYPE(sv
) > SVt_PVLV
&& SvTYPE(sv
) != SVt_PVFM
))
79 * increase the reference count so we can just SvREFCNT_dec() it when
82 SvREFCNT_inc_simple_void(sv
);
86 * Request the string from Perl, in UTF-8 encoding; but if we're in a
87 * SQL_ASCII database, just request the byte soup without trying to make
88 * it UTF8, because that might fail.
90 if (GetDatabaseEncoding() == PG_SQL_ASCII
)
93 val
= SvPVutf8(sv
, len
);
96 * Now convert to database encoding. We use perl's length in the event we
97 * had an embedded null byte to ensure we error out properly.
99 res
= utf_u2e(val
, len
);
101 /* safe now to garbage collect the new SV */
108 * Create a new SV from a string assumed to be in the current database's
112 cstr2sv(const char *str
)
118 /* no conversion when SQL_ASCII */
119 if (GetDatabaseEncoding() == PG_SQL_ASCII
)
120 return newSVpv(str
, 0);
122 utf8_str
= utf_e2u(str
);
124 sv
= newSVpv(utf8_str
, 0);
132 * croak() with specified message, which is given in the database encoding.
134 * Ideally we'd just write croak("%s", str), but plain croak() does not play
135 * nice with non-ASCII data. In modern Perl versions we can call cstr2sv()
136 * and pass the result to croak_sv(); in versions that don't have croak_sv(),
137 * we have to work harder.
140 croak_cstr(const char *str
)
145 /* Use sv_2mortal() to be sure the transient SV gets freed */
146 croak_sv(sv_2mortal(cstr2sv(str
)));
150 * The older way to do this is to assign a UTF8-marked value to ERRSV and
151 * then call croak(NULL). But if we leave it to croak() to append the
152 * error location, it does so too late (only after popping the stack) in
153 * some Perl versions. Hence, use mess() to create an SV with the error
154 * location info already appended.
156 SV
*errsv
= get_sv("@", GV_ADD
);
157 char *utf8_str
= utf_e2u(str
);
160 ssv
= mess("%s", utf8_str
);
165 sv_setsv(errsv
, ssv
);
168 #endif /* croak_sv */
171 #endif /* PL_PERL_HELPERS_H */