in ext/pg_query/pg_query_deparse.c [4328:4389]
static void deparseCreateFunctionStmt(StringInfo str, CreateFunctionStmt *create_function_stmt)
{
ListCell *lc;
bool tableFunc = false;
appendStringInfoString(str, "CREATE ");
if (create_function_stmt->replace)
appendStringInfoString(str, "OR REPLACE ");
if (create_function_stmt->is_procedure)
appendStringInfoString(str, "PROCEDURE ");
else
appendStringInfoString(str, "FUNCTION ");
deparseFuncName(str, create_function_stmt->funcname);
appendStringInfoChar(str, '(');
foreach(lc, create_function_stmt->parameters)
{
FunctionParameter *function_parameter = castNode(FunctionParameter, lfirst(lc));
if (function_parameter->mode != FUNC_PARAM_TABLE)
{
deparseFunctionParameter(str, function_parameter);
if (lnext(create_function_stmt->parameters, lc) && castNode(FunctionParameter, lfirst(lnext(create_function_stmt->parameters, lc)))->mode != FUNC_PARAM_TABLE)
appendStringInfoString(str, ", ");
}
else
{
tableFunc = true;
}
}
appendStringInfoString(str, ") ");
if (tableFunc)
{
appendStringInfoString(str, "RETURNS TABLE (");
foreach(lc, create_function_stmt->parameters)
{
FunctionParameter *function_parameter = castNode(FunctionParameter, lfirst(lc));
if (function_parameter->mode == FUNC_PARAM_TABLE)
{
deparseFunctionParameter(str, function_parameter);
if (lnext(create_function_stmt->parameters, lc))
appendStringInfoString(str, ", ");
}
}
appendStringInfoString(str, ") ");
}
else if (create_function_stmt->returnType != NULL)
{
appendStringInfoString(str, "RETURNS ");
deparseTypeName(str, create_function_stmt->returnType);
appendStringInfoChar(str, ' ');
}
foreach(lc, create_function_stmt->options)
{
deparseCreateFuncOptItem(str, castNode(DefElem, lfirst(lc)));
appendStringInfoChar(str, ' ');
}
removeTrailingSpace(str);
}