/* * Internal handler function */ Datum handler_internal(Oid function_oid, FunctionCallInfo fcinfo, bool execute) { HeapTuple proctuple; Form_pg_proc pg_proc_entry; const char * sourcecode; const char * rest; char *tempfile; int i; int argc; char * arguments[FUNC_MAX_ARGS + 2]; char * ret; HeapTuple returntuple = NULL; Datum prosrcdatum; bool isnull; 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 = DatumGetCString(DirectFunctionCall1(textout, prosrcdatum)); parse_shell_and_arguments(sourcecode, &argc, arguments, &rest); /* validation stops here */ if (!execute) { ReleaseSysCache(proctuple); PG_RETURN_VOID(); } tempfile = write_to_tempfile(rest); arguments[argc++] = tempfile; /* evaluate arguments */ pg_proc_entry = (Form_pg_proc) GETSTRUCT(proctuple); if (CALLED_AS_TRIGGER(fcinfo)) { TriggerData *trigdata = (TriggerData *) fcinfo->context; Trigger *trigger = trigdata->tg_trigger; TupleDesc tupdesc = trigdata->tg_relation->rd_att; HeapTuple oldtuple = trigdata->tg_trigtuple; /* first the CREATE TRIGGER fixed arguments */ for (i = 0; i < trigger->tgnargs; i++) { arguments[argc++] = trigger->tgargs[i]; } if (TRIGGER_FIRED_FOR_ROW(trigdata->tg_event)) for (i = 0; i < tupdesc->natts; i++) { char * s; bool isnull; Datum attr; attr = heap_getattr(oldtuple, i + 1, tupdesc, &isnull); if (isnull) s = ""; else s = type_to_cstring(attr, tupdesc->attrs[i]->atttypid); elog(DEBUG2, "arg %d is \"%s\" (type %u)", i, s, tupdesc->attrs[i]->atttypid); arguments[argc++] = s; } /* since we can't alter the tuple anyway, set up a return tuple right now */ if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event)) returntuple = trigdata->tg_trigtuple; else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event)) returntuple = trigdata->tg_trigtuple; else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event)) returntuple = trigdata->tg_newtuple; #ifdef TRIGGER_FIRED_BY_TRUNCATE else if (TRIGGER_FIRED_BY_TRUNCATE(trigdata->tg_event)) returntuple = trigdata->tg_trigtuple; #endif else elog(ERROR, "unrecognized trigger action: not INSERT, DELETE, UPDATE, or TRUNCATE"); } else if (CALLED_AS_EVENT_TRIGGER(fcinfo)) { /* nothing */ } else /* not trigger */ { for (i = 0; i < pg_proc_entry->pronargs; i++) { char * s; if (PG_ARGISNULL(i)) s = ""; else s = type_to_cstring(PG_GETARG_DATUM(i), pg_proc_entry->proargtypes.values[i]); elog(DEBUG2, "arg %d is \"%s\"", i, s); arguments[argc++] = s; } } /* terminate list */ arguments[argc] = NULL; ret = handler_internal2(tempfile, arguments, NameStr(pg_proc_entry->proname), CALLED_AS_TRIGGER(fcinfo) ? (TriggerData *) fcinfo->context : NULL, CALLED_AS_EVENT_TRIGGER(fcinfo) ? (EventTriggerData *) fcinfo->context : NULL); ReleaseSysCache(proctuple); if (CALLED_AS_TRIGGER(fcinfo)) { PG_RETURN_DATUM(PointerGetDatum(returntuple)); } else if (CALLED_AS_EVENT_TRIGGER(fcinfo)) { PG_RETURN_NULL(); } else { if (ret) PG_RETURN_DATUM(cstring_to_type(ret, pg_proc_entry->prorettype)); else PG_RETURN_NULL(); } }
/* * 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); }