mirror of
https://github.com/postgres/postgres.git
synced 2025-07-02 09:02:37 +03:00
Add transforms feature
This provides a mechanism for specifying conversions between SQL data types and procedural languages. As examples, there are transforms for hstore and ltree for PL/Perl and PL/Python. reviews by Pavel Stěhule and Andres Freund
This commit is contained in:
@ -306,6 +306,7 @@ static text *pg_get_expr_worker(text *expr, Oid relid, const char *relname,
|
||||
static int print_function_arguments(StringInfo buf, HeapTuple proctup,
|
||||
bool print_table_args, bool print_defaults);
|
||||
static void print_function_rettype(StringInfo buf, HeapTuple proctup);
|
||||
static void print_function_trftypes(StringInfo buf, HeapTuple proctup);
|
||||
static void set_rtable_names(deparse_namespace *dpns, List *parent_namespaces,
|
||||
Bitmapset *rels_used);
|
||||
static bool refname_is_unique(char *refname, deparse_namespace *dpns,
|
||||
@ -1912,9 +1913,7 @@ pg_get_functiondef(PG_FUNCTION_ARGS)
|
||||
StringInfoData buf;
|
||||
StringInfoData dq;
|
||||
HeapTuple proctup;
|
||||
HeapTuple langtup;
|
||||
Form_pg_proc proc;
|
||||
Form_pg_language lang;
|
||||
Datum tmp;
|
||||
bool isnull;
|
||||
const char *prosrc;
|
||||
@ -1937,12 +1936,6 @@ pg_get_functiondef(PG_FUNCTION_ARGS)
|
||||
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
||||
errmsg("\"%s\" is an aggregate function", name)));
|
||||
|
||||
/* Need its pg_language tuple for the language name */
|
||||
langtup = SearchSysCache1(LANGOID, ObjectIdGetDatum(proc->prolang));
|
||||
if (!HeapTupleIsValid(langtup))
|
||||
elog(ERROR, "cache lookup failed for language %u", proc->prolang);
|
||||
lang = (Form_pg_language) GETSTRUCT(langtup);
|
||||
|
||||
/*
|
||||
* We always qualify the function name, to ensure the right function gets
|
||||
* replaced.
|
||||
@ -1953,8 +1946,11 @@ pg_get_functiondef(PG_FUNCTION_ARGS)
|
||||
(void) print_function_arguments(&buf, proctup, false, true);
|
||||
appendStringInfoString(&buf, ")\n RETURNS ");
|
||||
print_function_rettype(&buf, proctup);
|
||||
|
||||
print_function_trftypes(&buf, proctup);
|
||||
|
||||
appendStringInfo(&buf, "\n LANGUAGE %s\n",
|
||||
quote_identifier(NameStr(lang->lanname)));
|
||||
quote_identifier(get_language_name(proc->prolang, false)));
|
||||
|
||||
/* Emit some miscellaneous options on one line */
|
||||
oldlen = buf.len;
|
||||
@ -2074,7 +2070,6 @@ pg_get_functiondef(PG_FUNCTION_ARGS)
|
||||
|
||||
appendStringInfoChar(&buf, '\n');
|
||||
|
||||
ReleaseSysCache(langtup);
|
||||
ReleaseSysCache(proctup);
|
||||
|
||||
PG_RETURN_TEXT_P(string_to_text(buf.data));
|
||||
@ -2350,6 +2345,30 @@ is_input_argument(int nth, const char *argmodes)
|
||||
|| argmodes[nth] == PROARGMODE_VARIADIC);
|
||||
}
|
||||
|
||||
/*
|
||||
* Append used transformated types to specified buffer
|
||||
*/
|
||||
static void
|
||||
print_function_trftypes(StringInfo buf, HeapTuple proctup)
|
||||
{
|
||||
Oid *trftypes;
|
||||
int ntypes;
|
||||
|
||||
ntypes = get_func_trftypes(proctup, &trftypes);
|
||||
if (ntypes > 0)
|
||||
{
|
||||
int i;
|
||||
|
||||
appendStringInfoString(buf, "\n TRANSFORM ");
|
||||
for (i = 0; i < ntypes; i++)
|
||||
{
|
||||
if (i != 0)
|
||||
appendStringInfoString(buf, ", ");
|
||||
appendStringInfo(buf, "FOR TYPE %s", format_type_be(trftypes[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Get textual representation of a function argument's default value. The
|
||||
* second argument of this function is the argument number among all arguments
|
||||
|
Reference in New Issue
Block a user