1 /**********************************************************************
2 * PostgreSQL::InServer::Util
4 * src/pl/plperl/Util.xs
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.
11 **********************************************************************/
13 /* this must be first: */
17 #include "utils/builtins.h"
18 #include "utils/bytea.h" /* for byteain & byteaout */
21 #define PG_NEED_PERL_XSUB_H
23 #include "plperl_helpers.h"
29 char *str = sv2cstr(sv);
32 text = cstring_to_text(str);
37 MODULE = PostgreSQL::InServer::Util PREFIX = util_
53 /* uses the ALIAS value as the return value */
64 if (level > ERROR) /* no PANIC allowed thanks */
68 plperl_util_elog(level, msg);
71 util_quote_literal(sv)
74 if (!sv || !SvOK(sv)) {
75 RETVAL = &PL_sv_undef;
78 text *arg = sv2text(sv);
79 text *quoted = DatumGetTextPP(DirectFunctionCall1(quote_literal, PointerGetDatum(arg)));
83 str = text_to_cstring(quoted);
84 RETVAL = cstr2sv(str);
91 util_quote_nullable(sv)
96 RETVAL = cstr2sv("NULL");
100 text *arg = sv2text(sv);
101 text *quoted = DatumGetTextPP(DirectFunctionCall1(quote_nullable, PointerGetDatum(arg)));
105 str = text_to_cstring(quoted);
106 RETVAL = cstr2sv(str);
121 quoted = DatumGetTextPP(DirectFunctionCall1(quote_ident, PointerGetDatum(arg)));
124 str = text_to_cstring(quoted);
125 RETVAL = cstr2sv(str);
131 util_decode_bytea(sv)
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));
145 util_encode_bytea(sv)
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);
161 looks_like_number(sv)
165 RETVAL = &PL_sv_undef;
166 else if ( looks_like_number(sv) )
174 encode_typed_literal(sv, typname)
180 outstr = plperl_sv_to_literal(sv, typname);
182 RETVAL = &PL_sv_undef;
184 RETVAL = cstr2sv(outstr);
189 items = 0; /* avoid 'unused variable' warning */