-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplxslt.c
256 lines (199 loc) · 6.03 KB
/
plxslt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* PL/XSLT language handler
*
* Copyright © 2007-2013 by Peter Eisentraut
* See the COPYING file for details.
*
*/
#include <postgres.h>
#include <fmgr.h>
#include <funcapi.h>
#include <miscadmin.h>
#include <access/heapam.h>
#if defined(PG_VERSION_NUM) && PG_VERSION_NUM >= 90300
#include <access/htup_details.h>
#endif
#include <catalog/pg_proc.h>
#include <catalog/pg_type.h>
#include <commands/trigger.h>
#include <utils/syscache.h>
#include <utils/builtins.h>
#include <utils/rel.h>
#include <utils/xml.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxslt/transform.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/xsltutils.h>
PG_MODULE_MAGIC;
#define _textout(x) (DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(&x))))
/*
* Convert the C string "input" to a Datum of type "typeoid".
*/
static Datum
cstring_to_type(char * input, Oid typeoid)
{
HeapTuple typetuple;
Form_pg_type pg_type_entry;
Datum ret;
typetuple = SearchSysCache(TYPEOID, ObjectIdGetDatum(typeoid), 0, 0, 0);
if (!HeapTupleIsValid(typetuple))
elog(ERROR, "cache lookup failed for type %u", typeoid);
pg_type_entry = (Form_pg_type) GETSTRUCT(typetuple);
ret = OidFunctionCall3(pg_type_entry->typinput,
CStringGetDatum(input),
0, -1);
ReleaseSysCache(typetuple);
PG_RETURN_DATUM(ret);
}
/*
* Convert the Datum "input" that is of type "typeoid" to a C string.
*/
static char *
type_to_cstring(Datum input, Oid typeoid)
{
HeapTuple typetuple;
Form_pg_type pg_type_entry;
Datum ret;
typetuple = SearchSysCache(TYPEOID, ObjectIdGetDatum(typeoid), 0, 0, 0);
if (!HeapTupleIsValid(typetuple))
elog(ERROR, "cache lookup failed for type %u", typeoid);
pg_type_entry = (Form_pg_type) GETSTRUCT(typetuple);
ret = OidFunctionCall3(pg_type_entry->typoutput,
input,
0, -1);
ReleaseSysCache(typetuple);
return DatumGetCString(ret);
}
/*
* Internal handler function
*/
static Datum
handler_internal(Oid function_oid, FunctionCallInfo fcinfo, bool execute)
{
HeapTuple proctuple;
Form_pg_proc pg_proc_entry;
char *sourcecode;
Datum prosrcdatum;
bool isnull;
const char **xslt_params;
int i;
Oid *argtypes;
char **argnames;
char *argmodes;
int numargs;
xmlDocPtr ssdoc;
xmlDocPtr xmldoc;
xmlDocPtr resdoc;
xsltStylesheetPtr stylesheet;
int reslen;
xmlChar *resstr;
Datum resdatum;
if (CALLED_AS_TRIGGER(fcinfo))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("trigger functions not supported")));
proctuple = SearchSysCache(PROCOID, ObjectIdGetDatum(function_oid), 0, 0, 0);
if (!HeapTupleIsValid(proctuple))
elog(ERROR, "cache lookup failed for function %u", function_oid);
prosrcdatum = SysCacheGetAttr(PROCOID, proctuple, Anum_pg_proc_prosrc, &isnull);
if (isnull)
elog(ERROR, "null prosrc");
sourcecode = pstrdup(DatumGetCString(DirectFunctionCall1(textout,
prosrcdatum)));
/* allow one blank line at the start */
if (sourcecode[0] == '\n')
sourcecode++;
numargs = get_func_arg_info(proctuple,
&argtypes, &argnames, &argmodes);
if (numargs < 1)
ereport(ERROR,
(errmsg("XSLT function must have at least one argument")));
if (argtypes[0] != XMLOID)
ereport(ERROR,
(errmsg("first argument of XSLT function must have type XML")));
#if 0
xsltSetGenericErrorFunc(NULL, xmlGenericError);
#endif
ssdoc = xmlParseDoc((xmlChar *) sourcecode); /* XXX use backend's xml_parse here() */
stylesheet = xsltParseStylesheetDoc(ssdoc); /* XXX check error handling */
if (!stylesheet)
ereport(ERROR,
(errmsg("could not parse stylesheet")));
pg_proc_entry = (Form_pg_proc) GETSTRUCT(proctuple);
{
char *method;
method = (char *) stylesheet->method;
/*
* TODO: This is strictly speaking not correct because the
* default output method may be "html", but that can only
* detected at run time, so punt for now.
*/
if (!method)
method = "xml";
if (strcmp(method, "xml") == 0 && pg_proc_entry->prorettype != XMLOID)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("XSLT stylesheet has output method \"xml\" but return type of function is not xml")));
else if ((strcmp(method, "html") == 0 || strcmp(method, "text") == 0)
&& pg_proc_entry->prorettype != TEXTOID && pg_proc_entry->prorettype != VARCHAROID)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("XSLT stylesheet has output method \"%s\" but return type of function is not text or varchar", method)));
}
/* validation stops here */
if (!execute)
{
ReleaseSysCache(proctuple);
PG_RETURN_VOID();
}
/* execution begins here */
xslt_params = palloc(((numargs - 1) * 2 + 1) * sizeof(*xslt_params));
for (i = 0; i < numargs-1; i++)
{
xslt_params[i*2] = argnames[i+1];
xslt_params[i*2+1] = type_to_cstring(PG_GETARG_DATUM(i+1),
argtypes[i+1]);
}
xslt_params[i*2] = NULL;
{
xmltype *arg0 = PG_GETARG_XML_P(0);
// XXX this ought to use xml_parse()
xmldoc = xmlParseMemory((char *) VARDATA(arg0), VARSIZE(arg0) - VARHDRSZ);
}
resdoc = xsltApplyStylesheet(stylesheet, xmldoc, xslt_params);
if (!resdoc)
elog(ERROR, "xsltApplyStylesheet() failed");
xmlFreeDoc(xmldoc);
if (xsltSaveResultToString(&resstr, &reslen, resdoc, stylesheet) != 0)
elog(ERROR, "result serialization failed");
xsltFreeStylesheet(stylesheet);
xmlFreeDoc(resdoc);
xsltCleanupGlobals();
xmlCleanupParser();
resdatum = cstring_to_type(resstr ? (char *) resstr : "", pg_proc_entry->prorettype);
ReleaseSysCache(proctuple);
PG_RETURN_DATUM(resdatum);
}
/*
* The PL handler
*/
PG_FUNCTION_INFO_V1(plxslt_handler);
Datum plxslt_handler(PG_FUNCTION_ARGS);
Datum
plxslt_handler(PG_FUNCTION_ARGS)
{
return handler_internal(fcinfo->flinfo->fn_oid, fcinfo, true);
}
/*
* Validator function
*/
PG_FUNCTION_INFO_V1(plxslt_validator);
Datum plxslt_validator(PG_FUNCTION_ARGS);
Datum
plxslt_validator(PG_FUNCTION_ARGS)
{
if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, PG_GETARG_OID(0)))
PG_RETURN_VOID();
return handler_internal(PG_GETARG_OID(0), fcinfo, false);
}