Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PL/pgSQL: Setup namespace items for input parameters #123

Merged
merged 1 commit into from
Oct 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/extract_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def write_out
runner.mock('format_type_be', 'char * format_type_be(Oid type_oid) { return pstrdup("-"); }')
runner.mock('build_row_from_class', 'static PLpgSQL_row *build_row_from_class(Oid classOid) { return NULL; }')
runner.mock('plpgsql_build_datatype', 'PLpgSQL_type * plpgsql_build_datatype(Oid typeOid, int32 typmod, Oid collation, TypeName *origtypname) { PLpgSQL_type *typ; typ = (PLpgSQL_type *) palloc0(sizeof(PLpgSQL_type)); typ->typname = pstrdup("UNKNOWN"); typ->ttype = PLPGSQL_TTYPE_SCALAR; return typ; }')
runner.mock('parse_datatype', 'static PLpgSQL_type * parse_datatype(const char *string, int location) { PLpgSQL_type *typ; typ = (PLpgSQL_type *) palloc0(sizeof(PLpgSQL_type)); typ->typname = pstrdup(string); typ->ttype = PLPGSQL_TTYPE_SCALAR; return typ; }')
runner.mock('parse_datatype', 'static PLpgSQL_type * parse_datatype(const char *string, int location) { PLpgSQL_type *typ; typ = (PLpgSQL_type *) palloc0(sizeof(PLpgSQL_type)); typ->typname = pstrdup(string); typ->ttype = strcmp(string, "RECORD") == 0 ? PLPGSQL_TTYPE_REC : PLPGSQL_TTYPE_SCALAR; return typ; }')
runner.mock('get_collation_oid', 'Oid get_collation_oid(List *name, bool missing_ok) { return -1; }')
runner.mock('plpgsql_parse_wordtype', 'PLpgSQL_type * plpgsql_parse_wordtype(char *ident) { return NULL; }')
runner.mock('plpgsql_parse_wordrowtype', 'PLpgSQL_type * plpgsql_parse_wordrowtype(char *ident) { return NULL; }')
Expand Down
22 changes: 21 additions & 1 deletion src/pg_query_parse_plpgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ static PLpgSQL_function *compile_create_function_stmt(CreateFunctionStmt* stmt)
}
}

assert(proc_source);
assert(proc_source != NULL);

if (stmt->returnType != NULL) {
foreach(lc3, stmt->returnType->names)
Expand Down Expand Up @@ -179,6 +179,26 @@ static PLpgSQL_function *compile_create_function_stmt(CreateFunctionStmt* stmt)
plpgsql_DumpExecTree = false;
plpgsql_start_datums();

/* Setup parameter names */
foreach(lc, stmt->parameters)
{
FunctionParameter *param = lfirst_node(FunctionParameter, lc);
if (param->name != NULL)
{
char buf[32];
PLpgSQL_type *argdtype;
PLpgSQL_variable *argvariable;
PLpgSQL_nsitem_type argitemtype;
snprintf(buf, sizeof(buf), "$%d", foreach_current_index(lc) + 1);
argdtype = plpgsql_build_datatype(UNKNOWNOID, -1, InvalidOid, NULL);
argvariable = plpgsql_build_variable(param->name ? param->name : buf, 0, argdtype, false);
argitemtype = argvariable->dtype == PLPGSQL_DTYPE_VAR ? PLPGSQL_NSTYPE_VAR : PLPGSQL_NSTYPE_REC;
plpgsql_ns_additem(argitemtype, argvariable->dno, buf);
if (param->name != NULL)
plpgsql_ns_additem(argitemtype, argvariable->dno, param->name);
}
}

/* Set up as though in a function returning VOID */
function->fn_rettype = VOIDOID;
function->fn_retset = is_setof;
Expand Down
2 changes: 1 addition & 1 deletion src/postgres/src_pl_plpgsql_src_pl_gram.c
Original file line number Diff line number Diff line change
Expand Up @@ -6147,7 +6147,7 @@ plpgsql_sql_error_callback(void *arg)
* This is handled the same as in check_sql_expr(), and we likewise
* expect that the given string is a copy from the source text.
*/
static PLpgSQL_type * parse_datatype(const char *string, int location) { PLpgSQL_type *typ; typ = (PLpgSQL_type *) palloc0(sizeof(PLpgSQL_type)); typ->typname = pstrdup(string); typ->ttype = PLPGSQL_TTYPE_SCALAR; return typ; }
static PLpgSQL_type * parse_datatype(const char *string, int location) { PLpgSQL_type *typ; typ = (PLpgSQL_type *) palloc0(sizeof(PLpgSQL_type)); typ->typname = pstrdup(string); typ->ttype = strcmp(string, "RECORD") == 0 ? PLPGSQL_TTYPE_REC : PLPGSQL_TTYPE_SCALAR; return typ; }


/*
Expand Down
Loading