mirror of
https://github.com/postgres/postgres.git
synced 2025-07-02 09:02:37 +03:00
Add procedure support to pg_get_functiondef
This also makes procedures work in psql's \ef and \sf commands. Reported-by: Pavel Stehule <pavel.stehule@gmail.com>
This commit is contained in:
@ -2449,6 +2449,7 @@ pg_get_functiondef(PG_FUNCTION_ARGS)
|
||||
StringInfoData dq;
|
||||
HeapTuple proctup;
|
||||
Form_pg_proc proc;
|
||||
bool isfunction;
|
||||
Datum tmp;
|
||||
bool isnull;
|
||||
const char *prosrc;
|
||||
@ -2472,20 +2473,28 @@ pg_get_functiondef(PG_FUNCTION_ARGS)
|
||||
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
||||
errmsg("\"%s\" is an aggregate function", name)));
|
||||
|
||||
isfunction = (proc->prorettype != InvalidOid);
|
||||
|
||||
/*
|
||||
* We always qualify the function name, to ensure the right function gets
|
||||
* replaced.
|
||||
*/
|
||||
nsp = get_namespace_name(proc->pronamespace);
|
||||
appendStringInfo(&buf, "CREATE OR REPLACE FUNCTION %s(",
|
||||
appendStringInfo(&buf, "CREATE OR REPLACE %s %s(",
|
||||
isfunction ? "FUNCTION" : "PROCEDURE",
|
||||
quote_qualified_identifier(nsp, name));
|
||||
(void) print_function_arguments(&buf, proctup, false, true);
|
||||
appendStringInfoString(&buf, ")\n RETURNS ");
|
||||
print_function_rettype(&buf, proctup);
|
||||
appendStringInfoString(&buf, ")\n");
|
||||
if (isfunction)
|
||||
{
|
||||
appendStringInfoString(&buf, " RETURNS ");
|
||||
print_function_rettype(&buf, proctup);
|
||||
appendStringInfoChar(&buf, '\n');
|
||||
}
|
||||
|
||||
print_function_trftypes(&buf, proctup);
|
||||
|
||||
appendStringInfo(&buf, "\n LANGUAGE %s\n",
|
||||
appendStringInfo(&buf, " LANGUAGE %s\n",
|
||||
quote_identifier(get_language_name(proc->prolang, false)));
|
||||
|
||||
/* Emit some miscellaneous options on one line */
|
||||
@ -2607,10 +2616,11 @@ pg_get_functiondef(PG_FUNCTION_ARGS)
|
||||
*
|
||||
* Since the user is likely to be editing the function body string, we
|
||||
* shouldn't use a short delimiter that he might easily create a conflict
|
||||
* with. Hence prefer "$function$", but extend if needed.
|
||||
* with. Hence prefer "$function$"/"$procedure$", but extend if needed.
|
||||
*/
|
||||
initStringInfo(&dq);
|
||||
appendStringInfoString(&dq, "$function");
|
||||
appendStringInfoChar(&dq, '$');
|
||||
appendStringInfoString(&dq, (isfunction ? "function" : "procedure"));
|
||||
while (strstr(prosrc, dq.data) != NULL)
|
||||
appendStringInfoChar(&dq, 'x');
|
||||
appendStringInfoChar(&dq, '$');
|
||||
|
Reference in New Issue
Block a user