mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-09-30 14:01:27 +02:00
Arrange to emit a CONTEXT: SQL function "foo" entry in an error
message that is reporting a prechecking error in a SQL function. This is to cue client-side code that the syntax error position, if any, is with respect to the function body and not the outer command.
This commit is contained in:
parent
afaf252cee
commit
a8a3b54724
@ -8,7 +8,7 @@
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $PostgreSQL: pgsql/src/backend/catalog/pg_proc.c,v 1.111 2004/01/06 23:55:18 tgl Exp $
|
* $PostgreSQL: pgsql/src/backend/catalog/pg_proc.c,v 1.112 2004/03/14 01:58:41 tgl Exp $
|
||||||
*
|
*
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
@ -44,6 +44,7 @@ Datum fmgr_sql_validator(PG_FUNCTION_ARGS);
|
|||||||
|
|
||||||
static Datum create_parameternames_array(int parameterCount,
|
static Datum create_parameternames_array(int parameterCount,
|
||||||
const char *parameterNames[]);
|
const char *parameterNames[]);
|
||||||
|
static void sql_function_parse_error_callback(void *arg);
|
||||||
|
|
||||||
|
|
||||||
/* ----------------------------------------------------------------
|
/* ----------------------------------------------------------------
|
||||||
@ -708,6 +709,7 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
|
|||||||
bool isnull;
|
bool isnull;
|
||||||
Datum tmp;
|
Datum tmp;
|
||||||
char *prosrc;
|
char *prosrc;
|
||||||
|
ErrorContextCallback sqlerrcontext;
|
||||||
char functyptype;
|
char functyptype;
|
||||||
bool haspolyarg;
|
bool haspolyarg;
|
||||||
int i;
|
int i;
|
||||||
@ -760,6 +762,16 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
|
|||||||
|
|
||||||
prosrc = DatumGetCString(DirectFunctionCall1(textout, tmp));
|
prosrc = DatumGetCString(DirectFunctionCall1(textout, tmp));
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Setup error traceback support for ereport(). This is mostly
|
||||||
|
* so we can add context info that shows that a syntax-error
|
||||||
|
* location is inside the function body, not out in CREATE FUNCTION.
|
||||||
|
*/
|
||||||
|
sqlerrcontext.callback = sql_function_parse_error_callback;
|
||||||
|
sqlerrcontext.arg = proc;
|
||||||
|
sqlerrcontext.previous = error_context_stack;
|
||||||
|
error_context_stack = &sqlerrcontext;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We can't do full prechecking of the function definition if there
|
* We can't do full prechecking of the function definition if there
|
||||||
* are any polymorphic input types, because actual datatypes of
|
* are any polymorphic input types, because actual datatypes of
|
||||||
@ -778,9 +790,32 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
querytree_list = pg_parse_query(prosrc);
|
querytree_list = pg_parse_query(prosrc);
|
||||||
|
|
||||||
|
error_context_stack = sqlerrcontext.previous;
|
||||||
}
|
}
|
||||||
|
|
||||||
ReleaseSysCache(tuple);
|
ReleaseSysCache(tuple);
|
||||||
|
|
||||||
PG_RETURN_VOID();
|
PG_RETURN_VOID();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* error context callback to let us supply a context marker
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
sql_function_parse_error_callback(void *arg)
|
||||||
|
{
|
||||||
|
Form_pg_proc proc = (Form_pg_proc) arg;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* XXX it'd be really nice to adjust the syntax error position to
|
||||||
|
* account for the offset from the start of the statement to the
|
||||||
|
* function body string, not to mention any quoting characters in
|
||||||
|
* the string, but I can't see any decent way to do that...
|
||||||
|
*
|
||||||
|
* In the meantime, put in a CONTEXT entry that can cue clients
|
||||||
|
* not to trust the syntax error position completely.
|
||||||
|
*/
|
||||||
|
errcontext("SQL function \"%s\"",
|
||||||
|
NameStr(proc->proname));
|
||||||
|
}
|
||||||
|
@ -52,16 +52,20 @@ CREATE FUNCTION test1 (int) RETURNS int LANGUAGE sql
|
|||||||
AS 'SELECT ''not an integer'';';
|
AS 'SELECT ''not an integer'';';
|
||||||
ERROR: return type mismatch in function declared to return integer
|
ERROR: return type mismatch in function declared to return integer
|
||||||
DETAIL: Actual return type is "unknown".
|
DETAIL: Actual return type is "unknown".
|
||||||
|
CONTEXT: SQL function "test1"
|
||||||
CREATE FUNCTION test1 (int) RETURNS int LANGUAGE sql
|
CREATE FUNCTION test1 (int) RETURNS int LANGUAGE sql
|
||||||
AS 'not even SQL';
|
AS 'not even SQL';
|
||||||
ERROR: syntax error at or near "not" at character 1
|
ERROR: syntax error at or near "not" at character 1
|
||||||
|
CONTEXT: SQL function "test1"
|
||||||
CREATE FUNCTION test1 (int) RETURNS int LANGUAGE sql
|
CREATE FUNCTION test1 (int) RETURNS int LANGUAGE sql
|
||||||
AS 'SELECT 1, 2, 3;';
|
AS 'SELECT 1, 2, 3;';
|
||||||
ERROR: return type mismatch in function declared to return integer
|
ERROR: return type mismatch in function declared to return integer
|
||||||
DETAIL: Final SELECT must return exactly one column.
|
DETAIL: Final SELECT must return exactly one column.
|
||||||
|
CONTEXT: SQL function "test1"
|
||||||
CREATE FUNCTION test1 (int) RETURNS int LANGUAGE sql
|
CREATE FUNCTION test1 (int) RETURNS int LANGUAGE sql
|
||||||
AS 'SELECT $2;';
|
AS 'SELECT $2;';
|
||||||
ERROR: there is no parameter $2
|
ERROR: there is no parameter $2
|
||||||
|
CONTEXT: SQL function "test1"
|
||||||
CREATE FUNCTION test1 (int) RETURNS int LANGUAGE sql
|
CREATE FUNCTION test1 (int) RETURNS int LANGUAGE sql
|
||||||
AS 'a', 'b';
|
AS 'a', 'b';
|
||||||
ERROR: only one AS item needed for language "sql"
|
ERROR: only one AS item needed for language "sql"
|
||||||
|
Loading…
Reference in New Issue
Block a user