1 /*-------------------------------------------------------------------------
4 * Common include file for PL/Perl files
6 * This should be included _AFTER_ postgres.h and system include files, as
7 * well as headers that could in turn include system headers.
9 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1995, Regents of the University of California
12 * src/pl/plperl/plperl.h
18 /* defines free() by way of system headers, so must be included before perl.h */
19 #include "mb/pg_wchar.h"
22 * Pull in Perl headers via a wrapper header, to control the scope of
23 * the system_header pragma therein.
25 #include "plperl_system.h"
27 /* declare routines from plperl.c for access by .xs files */
28 HV
*plperl_spi_exec(char *, int);
29 void plperl_return_next(SV
*);
30 SV
*plperl_spi_query(char *);
31 SV
*plperl_spi_fetchrow(char *);
32 SV
*plperl_spi_prepare(char *, int, SV
**);
33 HV
*plperl_spi_exec_prepared(char *, HV
*, int, SV
**);
34 SV
*plperl_spi_query_prepared(char *, int, SV
**);
35 void plperl_spi_freeplan(char *);
36 void plperl_spi_cursor_close(char *);
37 void plperl_spi_commit(void);
38 void plperl_spi_rollback(void);
39 char *plperl_sv_to_literal(SV
*, char *);
40 void plperl_util_elog(int level
, SV
*msg
);
43 /* helper functions */
46 * convert from utf8 to database encoding
48 * Returns a palloc'ed copy of the original string
51 utf_u2e(char *utf8_str
, size_t len
)
55 ret
= pg_any_to_server(utf8_str
, len
, PG_UTF8
);
57 /* ensure we have a copy even if no conversion happened */
65 * convert from database encoding to utf8
67 * Returns a palloc'ed copy of the original string
70 utf_e2u(const char *str
)
74 ret
= pg_server_to_any(str
, strlen(str
), PG_UTF8
);
76 /* ensure we have a copy even if no conversion happened */
84 * Convert an SV to a char * in the current database encoding
86 * Returns a palloc'ed copy of the original string
97 * get a utf8 encoded char * out of perl. *note* it may not be valid utf8!
101 * SvPVutf8() croaks nastily on certain things, like typeglobs and
102 * readonly objects such as $^V. That's a perl bug - it's not supposed to
103 * happen. To avoid crashing the backend, we make a copy of the sv before
104 * passing it to SvPVutf8(). The copy is garbage collected when we're done
107 if (SvREADONLY(sv
) ||
109 (SvTYPE(sv
) > SVt_PVLV
&& SvTYPE(sv
) != SVt_PVFM
))
114 * increase the reference count so we can just SvREFCNT_dec() it when
117 SvREFCNT_inc_simple_void(sv
);
121 * Request the string from Perl, in UTF-8 encoding; but if we're in a
122 * SQL_ASCII database, just request the byte soup without trying to make
123 * it UTF8, because that might fail.
125 if (GetDatabaseEncoding() == PG_SQL_ASCII
)
128 val
= SvPVutf8(sv
, len
);
131 * Now convert to database encoding. We use perl's length in the event we
132 * had an embedded null byte to ensure we error out properly.
134 res
= utf_u2e(val
, len
);
136 /* safe now to garbage collect the new SV */
143 * Create a new SV from a string assumed to be in the current database's
147 cstr2sv(const char *str
)
153 /* no conversion when SQL_ASCII */
154 if (GetDatabaseEncoding() == PG_SQL_ASCII
)
155 return newSVpv(str
, 0);
157 utf8_str
= utf_e2u(str
);
159 sv
= newSVpv(utf8_str
, 0);
167 * croak() with specified message, which is given in the database encoding.
169 * Ideally we'd just write croak("%s", str), but plain croak() does not play
170 * nice with non-ASCII data. In modern Perl versions we can call cstr2sv()
171 * and pass the result to croak_sv(); in versions that don't have croak_sv(),
172 * we have to work harder.
175 croak_cstr(const char *str
)
180 /* Use sv_2mortal() to be sure the transient SV gets freed */
181 croak_sv(sv_2mortal(cstr2sv(str
)));
185 * The older way to do this is to assign a UTF8-marked value to ERRSV and
186 * then call croak(NULL). But if we leave it to croak() to append the
187 * error location, it does so too late (only after popping the stack) in
188 * some Perl versions. Hence, use mess() to create an SV with the error
189 * location info already appended.
191 SV
*errsv
= get_sv("@", GV_ADD
);
192 char *utf8_str
= utf_e2u(str
);
195 ssv
= mess("%s", utf8_str
);
200 sv_setsv(errsv
, ssv
);
203 #endif /* croak_sv */
206 #endif /* PL_PERL_H */