Consistently use "superuser" instead of "super user"
[pgsql.git] / src / pl / plperl / Util.xs
blob47eba5941556000a0c1c50a6aec82133ccd4b24f
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  **********************************************************************/
13 /* this must be first: */
14 #include "postgres.h"
16 #include "fmgr.h"
17 #include "utils/builtins.h"
18 #include "utils/bytea.h"       /* for byteain & byteaout */
20 /* perl stuff */
21 #define PG_NEED_PERL_XSUB_H
22 #include "plperl.h"
23 #include "plperl_helpers.h"
26 static text *
27 sv2text(SV *sv)
29         char       *str = sv2cstr(sv);
30         text       *text;
32         text = cstring_to_text(str);
33         pfree(str);
34         return text;
37 MODULE = PostgreSQL::InServer::Util PREFIX = util_
39 PROTOTYPES: ENABLE
40 VERSIONCHECK: DISABLE
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
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);
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;
82                 pfree(arg);
83                 str = text_to_cstring(quoted);
84                 RETVAL = cstr2sv(str);
85                 pfree(str);
86     }
87     OUTPUT:
88     RETVAL
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;
104                 pfree(arg);
105                 str = text_to_cstring(quoted);
106                 RETVAL = cstr2sv(str);
107                 pfree(str);
108     }
109     OUTPUT:
110     RETVAL
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)));
123                 pfree(arg);
124                 str = text_to_cstring(quoted);
125                 RETVAL = cstr2sv(str);
126                 pfree(str);
127     OUTPUT:
128     RETVAL
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
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
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
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
188 BOOT:
189     items = 0;  /* avoid 'unused variable' warning */