Commit 020140d8 authored by Peter Eisentraut's avatar Peter Eisentraut

PL/Python: Rename new keyword arguments of plpy.error() etc.

Rename schema -> schema_name etc. to remain consistent with C API and
PL/pgSQL.
parent 4bc0f165
...@@ -1374,9 +1374,9 @@ $$ LANGUAGE plpythonu; ...@@ -1374,9 +1374,9 @@ $$ LANGUAGE plpythonu;
The following keyword-only arguments are accepted: The following keyword-only arguments are accepted:
<literal> <literal>
<replaceable>detail</replaceable>, <replaceable>hint</replaceable>, <replaceable>detail</replaceable>, <replaceable>hint</replaceable>,
<replaceable>sqlstate</replaceable>, <replaceable>schema</replaceable>, <replaceable>sqlstate</replaceable>, <replaceable>schema_name</replaceable>,
<replaceable>table</replaceable>, <replaceable>column</replaceable>, <replaceable>table_name</replaceable>, <replaceable>column_name</replaceable>,
<replaceable>datatype</replaceable> , <replaceable>constraint</replaceable> <replaceable>datatype_name</replaceable> , <replaceable>constraint_name</replaceable>
</literal>. </literal>.
The string representation of the objects passed as keyword-only arguments The string representation of the objects passed as keyword-only arguments
is used to enrich the messages reported to the client. For example: is used to enrich the messages reported to the client. For example:
......
...@@ -9,11 +9,11 @@ plpy.info('This is message text.', ...@@ -9,11 +9,11 @@ plpy.info('This is message text.',
detail = 'This is detail text', detail = 'This is detail text',
hint = 'This is hint text.', hint = 'This is hint text.',
sqlstate = 'XX000', sqlstate = 'XX000',
schema = 'any info about schema', schema_name = 'any info about schema',
table = 'any info about table', table_name = 'any info about table',
column = 'any info about column', column_name = 'any info about column',
datatype = 'any info about datatype', datatype_name = 'any info about datatype',
constraint = 'any info about constraint') constraint_name = 'any info about constraint')
plpy.notice('notice', detail = 'some detail') plpy.notice('notice', detail = 'some detail')
plpy.warning('warning', detail = 'some detail') plpy.warning('warning', detail = 'some detail')
plpy.error('stop on error', detail = 'some detail', hint = 'some hint') plpy.error('stop on error', detail = 'some detail', hint = 'some hint')
...@@ -70,12 +70,12 @@ CONTEXT: PL/Python anonymous code block ...@@ -70,12 +70,12 @@ CONTEXT: PL/Python anonymous code block
-- raise exception in python, handle exception in plgsql -- raise exception in python, handle exception in plgsql
CREATE OR REPLACE FUNCTION raise_exception(_message text, _detail text DEFAULT NULL, _hint text DEFAULT NULL, CREATE OR REPLACE FUNCTION raise_exception(_message text, _detail text DEFAULT NULL, _hint text DEFAULT NULL,
_sqlstate text DEFAULT NULL, _sqlstate text DEFAULT NULL,
_schema text DEFAULT NULL, _table text DEFAULT NULL, _column text DEFAULT NULL, _schema_name text DEFAULT NULL, _table_name text DEFAULT NULL, _column_name text DEFAULT NULL,
_datatype text DEFAULT NULL, _constraint text DEFAULT NULL) _datatype_name text DEFAULT NULL, _constraint_name text DEFAULT NULL)
RETURNS void AS $$ RETURNS void AS $$
kwargs = { "message":_message, "detail":_detail, "hint":_hint, kwargs = { "message":_message, "detail":_detail, "hint":_hint,
"sqlstate":_sqlstate, "schema":_schema, "table":_table, "sqlstate":_sqlstate, "schema_name":_schema_name, "table_name":_table_name,
"column":_column, "datatype":_datatype, "constraint":_constraint } "column_name":_column_name, "datatype_name":_datatype_name, "constraint_name":_constraint_name }
# ignore None values - should work on Python2.3 # ignore None values - should work on Python2.3
dict = {} dict = {}
for k in kwargs: for k in kwargs:
...@@ -101,11 +101,11 @@ SELECT raise_exception(_message => 'message text', ...@@ -101,11 +101,11 @@ SELECT raise_exception(_message => 'message text',
_detail => 'detail text', _detail => 'detail text',
_hint => 'hint text', _hint => 'hint text',
_sqlstate => 'XX555', _sqlstate => 'XX555',
_schema => 'schema text', _schema_name => 'schema text',
_table => 'table text', _table_name => 'table text',
_column => 'column text', _column_name => 'column text',
_datatype => 'datatype text', _datatype_name => 'datatype text',
_constraint => 'constraint text'); _constraint_name => 'constraint text');
ERROR: plpy.Error: message text ERROR: plpy.Error: message text
DETAIL: detail text DETAIL: detail text
HINT: hint text HINT: hint text
...@@ -115,9 +115,9 @@ CONTEXT: Traceback (most recent call last): ...@@ -115,9 +115,9 @@ CONTEXT: Traceback (most recent call last):
PL/Python function "raise_exception" PL/Python function "raise_exception"
SELECT raise_exception(_message => 'message text', SELECT raise_exception(_message => 'message text',
_hint => 'hint text', _hint => 'hint text',
_schema => 'schema text', _schema_name => 'schema text',
_column => 'column text', _column_name => 'column text',
_constraint => 'constraint text'); _constraint_name => 'constraint text');
ERROR: plpy.Error: message text ERROR: plpy.Error: message text
HINT: hint text HINT: hint text
CONTEXT: Traceback (most recent call last): CONTEXT: Traceback (most recent call last):
...@@ -133,19 +133,19 @@ DECLARE ...@@ -133,19 +133,19 @@ DECLARE
__schema_name text; __schema_name text;
__table_name text; __table_name text;
__column_name text; __column_name text;
__datatype text; __datatype_name text;
__constraint text; __constraint_name text;
BEGIN BEGIN
BEGIN BEGIN
PERFORM raise_exception(_message => 'message text', PERFORM raise_exception(_message => 'message text',
_detail => 'detail text', _detail => 'detail text',
_hint => 'hint text', _hint => 'hint text',
_sqlstate => 'XX555', _sqlstate => 'XX555',
_schema => 'schema text', _schema_name => 'schema text',
_table => 'table text', _table_name => 'table text',
_column => 'column text', _column_name => 'column text',
_datatype => 'datatype text', _datatype_name => 'datatype text',
_constraint => 'constraint text'); _constraint_name => 'constraint text');
EXCEPTION WHEN SQLSTATE 'XX555' THEN EXCEPTION WHEN SQLSTATE 'XX555' THEN
GET STACKED DIAGNOSTICS __message = MESSAGE_TEXT, GET STACKED DIAGNOSTICS __message = MESSAGE_TEXT,
__detail = PG_EXCEPTION_DETAIL, __detail = PG_EXCEPTION_DETAIL,
...@@ -154,24 +154,24 @@ BEGIN ...@@ -154,24 +154,24 @@ BEGIN
__schema_name = SCHEMA_NAME, __schema_name = SCHEMA_NAME,
__table_name = TABLE_NAME, __table_name = TABLE_NAME,
__column_name = COLUMN_NAME, __column_name = COLUMN_NAME,
__datatype = PG_DATATYPE_NAME, __datatype_name = PG_DATATYPE_NAME,
__constraint = CONSTRAINT_NAME; __constraint_name = CONSTRAINT_NAME;
RAISE NOTICE 'handled exception' RAISE NOTICE 'handled exception'
USING DETAIL = format('message:(%s), detail:(%s), hint: (%s), sqlstate: (%s), ' USING DETAIL = format('message:(%s), detail:(%s), hint: (%s), sqlstate: (%s), '
'schema:(%s), table:(%s), column:(%s), datatype:(%s), constraint:(%s)', 'schema_name:(%s), table_name:(%s), column_name:(%s), datatype_name:(%s), constraint_name:(%s)',
__message, __detail, __hint, __sqlstate, __schema_name, __message, __detail, __hint, __sqlstate, __schema_name,
__table_name, __column_name, __datatype, __constraint); __table_name, __column_name, __datatype_name, __constraint_name);
END; END;
END; END;
$$; $$;
NOTICE: handled exception NOTICE: handled exception
DETAIL: message:(plpy.Error: message text), detail:(detail text), hint: (hint text), sqlstate: (XX555), schema:(schema text), table:(table text), column:(column text), datatype:(datatype text), constraint:(constraint text) DETAIL: message:(plpy.Error: message text), detail:(detail text), hint: (hint text), sqlstate: (XX555), schema_name:(schema text), table_name:(table text), column_name:(column text), datatype_name:(datatype text), constraint_name:(constraint text)
-- the displayed context is different between Python2 and Python3, -- the displayed context is different between Python2 and Python3,
-- but that's not important for this test -- but that's not important for this test
\set SHOW_CONTEXT never \set SHOW_CONTEXT never
do $$ do $$
try: try:
plpy.execute("select raise_exception(_message => 'my message', _sqlstate => 'XX987', _hint => 'some hint', _table=> 'users_tab', _datatype => 'user_type')") plpy.execute("select raise_exception(_message => 'my message', _sqlstate => 'XX987', _hint => 'some hint', _table_name => 'users_tab', _datatype_name => 'user_type')")
except Exception, e: except Exception, e:
plpy.info(e.spidata) plpy.info(e.spidata)
raise e raise e
...@@ -181,11 +181,11 @@ ERROR: plpy.SPIError: plpy.Error: my message ...@@ -181,11 +181,11 @@ ERROR: plpy.SPIError: plpy.Error: my message
HINT: some hint HINT: some hint
do $$ do $$
try: try:
plpy.error(message = 'my message', sqlstate = 'XX987', hint = 'some hint', table = 'users_tab', datatype = 'user_type') plpy.error(message = 'my message', sqlstate = 'XX987', hint = 'some hint', table_name = 'users_tab', datatype_name = 'user_type')
except Exception, e: except Exception, e:
plpy.info('sqlstate: %s, hint: %s, tablename: %s, datatype: %s' % (e.sqlstate, e.hint, e.table_name, e.datatype_name)) plpy.info('sqlstate: %s, hint: %s, table_name: %s, datatype_name: %s' % (e.sqlstate, e.hint, e.table_name, e.datatype_name))
raise e raise e
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
INFO: sqlstate: XX987, hint: some hint, tablename: users_tab, datatype: user_type INFO: sqlstate: XX987, hint: some hint, table_name: users_tab, datatype_name: user_type
ERROR: plpy.Error: my message ERROR: plpy.Error: my message
HINT: some hint HINT: some hint
...@@ -399,11 +399,11 @@ PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw) ...@@ -399,11 +399,11 @@ PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw)
char *volatile message = NULL; char *volatile message = NULL;
char *volatile detail = NULL; char *volatile detail = NULL;
char *volatile hint = NULL; char *volatile hint = NULL;
char *volatile column = NULL; char *volatile column_name = NULL;
char *volatile constraint = NULL; char *volatile constraint_name = NULL;
char *volatile datatype = NULL; char *volatile datatype_name = NULL;
char *volatile table = NULL; char *volatile table_name = NULL;
char *volatile schema = NULL; char *volatile schema_name = NULL;
volatile MemoryContext oldcontext; volatile MemoryContext oldcontext;
PyObject *key, PyObject *key,
*value; *value;
...@@ -456,16 +456,16 @@ PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw) ...@@ -456,16 +456,16 @@ PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw)
hint = object_to_string(value); hint = object_to_string(value);
else if (strcmp(keyword, "sqlstate") == 0) else if (strcmp(keyword, "sqlstate") == 0)
sqlstatestr = object_to_string(value); sqlstatestr = object_to_string(value);
else if (strcmp(keyword, "schema") == 0) else if (strcmp(keyword, "schema_name") == 0)
schema = object_to_string(value); schema_name = object_to_string(value);
else if (strcmp(keyword, "table") == 0) else if (strcmp(keyword, "table_name") == 0)
table = object_to_string(value); table_name = object_to_string(value);
else if (strcmp(keyword, "column") == 0) else if (strcmp(keyword, "column_name") == 0)
column = object_to_string(value); column_name = object_to_string(value);
else if (strcmp(keyword, "datatype") == 0) else if (strcmp(keyword, "datatype_name") == 0)
datatype = object_to_string(value); datatype_name = object_to_string(value);
else if (strcmp(keyword, "constraint") == 0) else if (strcmp(keyword, "constraint_name") == 0)
constraint = object_to_string(value); constraint_name = object_to_string(value);
else else
PLy_elog(ERROR, "'%s' is an invalid keyword argument for this function", PLy_elog(ERROR, "'%s' is an invalid keyword argument for this function",
keyword); keyword);
...@@ -496,32 +496,32 @@ PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw) ...@@ -496,32 +496,32 @@ PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw)
pg_verifymbstr(detail, strlen(detail), false); pg_verifymbstr(detail, strlen(detail), false);
if (hint != NULL) if (hint != NULL)
pg_verifymbstr(hint, strlen(hint), false); pg_verifymbstr(hint, strlen(hint), false);
if (schema != NULL) if (schema_name != NULL)
pg_verifymbstr(schema, strlen(schema), false); pg_verifymbstr(schema_name, strlen(schema_name), false);
if (table != NULL) if (table_name != NULL)
pg_verifymbstr(table, strlen(table), false); pg_verifymbstr(table_name, strlen(table_name), false);
if (column != NULL) if (column_name != NULL)
pg_verifymbstr(column, strlen(column), false); pg_verifymbstr(column_name, strlen(column_name), false);
if (datatype != NULL) if (datatype_name != NULL)
pg_verifymbstr(datatype, strlen(datatype), false); pg_verifymbstr(datatype_name, strlen(datatype_name), false);
if (constraint != NULL) if (constraint_name != NULL)
pg_verifymbstr(constraint, strlen(constraint), false); pg_verifymbstr(constraint_name, strlen(constraint_name), false);
ereport(level, ereport(level,
((sqlstate != 0) ? errcode(sqlstate) : 0, ((sqlstate != 0) ? errcode(sqlstate) : 0,
(message != NULL) ? errmsg_internal("%s", message) : 0, (message != NULL) ? errmsg_internal("%s", message) : 0,
(detail != NULL) ? errdetail_internal("%s", detail) : 0, (detail != NULL) ? errdetail_internal("%s", detail) : 0,
(hint != NULL) ? errhint("%s", hint) : 0, (hint != NULL) ? errhint("%s", hint) : 0,
(column != NULL) ? (column_name != NULL) ?
err_generic_string(PG_DIAG_COLUMN_NAME, column) : 0, err_generic_string(PG_DIAG_COLUMN_NAME, column_name) : 0,
(constraint != NULL) ? (constraint_name != NULL) ?
err_generic_string(PG_DIAG_CONSTRAINT_NAME, constraint) : 0, err_generic_string(PG_DIAG_CONSTRAINT_NAME, constraint_name) : 0,
(datatype != NULL) ? (datatype_name != NULL) ?
err_generic_string(PG_DIAG_DATATYPE_NAME, datatype) : 0, err_generic_string(PG_DIAG_DATATYPE_NAME, datatype_name) : 0,
(table != NULL) ? (table_name != NULL) ?
err_generic_string(PG_DIAG_TABLE_NAME, table) : 0, err_generic_string(PG_DIAG_TABLE_NAME, table_name) : 0,
(schema != NULL) ? (schema_name != NULL) ?
err_generic_string(PG_DIAG_SCHEMA_NAME, schema) : 0)); err_generic_string(PG_DIAG_SCHEMA_NAME, schema_name) : 0));
} }
PG_CATCH(); PG_CATCH();
{ {
......
...@@ -9,11 +9,11 @@ plpy.info('This is message text.', ...@@ -9,11 +9,11 @@ plpy.info('This is message text.',
detail = 'This is detail text', detail = 'This is detail text',
hint = 'This is hint text.', hint = 'This is hint text.',
sqlstate = 'XX000', sqlstate = 'XX000',
schema = 'any info about schema', schema_name = 'any info about schema',
table = 'any info about table', table_name = 'any info about table',
column = 'any info about column', column_name = 'any info about column',
datatype = 'any info about datatype', datatype_name = 'any info about datatype',
constraint = 'any info about constraint') constraint_name = 'any info about constraint')
plpy.notice('notice', detail = 'some detail') plpy.notice('notice', detail = 'some detail')
plpy.warning('warning', detail = 'some detail') plpy.warning('warning', detail = 'some detail')
plpy.error('stop on error', detail = 'some detail', hint = 'some hint') plpy.error('stop on error', detail = 'some detail', hint = 'some hint')
...@@ -43,12 +43,12 @@ do $$ plpy.info('first message', 'second message', message='third message') $$ L ...@@ -43,12 +43,12 @@ do $$ plpy.info('first message', 'second message', message='third message') $$ L
-- raise exception in python, handle exception in plgsql -- raise exception in python, handle exception in plgsql
CREATE OR REPLACE FUNCTION raise_exception(_message text, _detail text DEFAULT NULL, _hint text DEFAULT NULL, CREATE OR REPLACE FUNCTION raise_exception(_message text, _detail text DEFAULT NULL, _hint text DEFAULT NULL,
_sqlstate text DEFAULT NULL, _sqlstate text DEFAULT NULL,
_schema text DEFAULT NULL, _table text DEFAULT NULL, _column text DEFAULT NULL, _schema_name text DEFAULT NULL, _table_name text DEFAULT NULL, _column_name text DEFAULT NULL,
_datatype text DEFAULT NULL, _constraint text DEFAULT NULL) _datatype_name text DEFAULT NULL, _constraint_name text DEFAULT NULL)
RETURNS void AS $$ RETURNS void AS $$
kwargs = { "message":_message, "detail":_detail, "hint":_hint, kwargs = { "message":_message, "detail":_detail, "hint":_hint,
"sqlstate":_sqlstate, "schema":_schema, "table":_table, "sqlstate":_sqlstate, "schema_name":_schema_name, "table_name":_table_name,
"column":_column, "datatype":_datatype, "constraint":_constraint } "column_name":_column_name, "datatype_name":_datatype_name, "constraint_name":_constraint_name }
# ignore None values - should work on Python2.3 # ignore None values - should work on Python2.3
dict = {} dict = {}
for k in kwargs: for k in kwargs:
...@@ -63,17 +63,17 @@ SELECT raise_exception(_message => 'message text', ...@@ -63,17 +63,17 @@ SELECT raise_exception(_message => 'message text',
_detail => 'detail text', _detail => 'detail text',
_hint => 'hint text', _hint => 'hint text',
_sqlstate => 'XX555', _sqlstate => 'XX555',
_schema => 'schema text', _schema_name => 'schema text',
_table => 'table text', _table_name => 'table text',
_column => 'column text', _column_name => 'column text',
_datatype => 'datatype text', _datatype_name => 'datatype text',
_constraint => 'constraint text'); _constraint_name => 'constraint text');
SELECT raise_exception(_message => 'message text', SELECT raise_exception(_message => 'message text',
_hint => 'hint text', _hint => 'hint text',
_schema => 'schema text', _schema_name => 'schema text',
_column => 'column text', _column_name => 'column text',
_constraint => 'constraint text'); _constraint_name => 'constraint text');
DO $$ DO $$
DECLARE DECLARE
...@@ -84,19 +84,19 @@ DECLARE ...@@ -84,19 +84,19 @@ DECLARE
__schema_name text; __schema_name text;
__table_name text; __table_name text;
__column_name text; __column_name text;
__datatype text; __datatype_name text;
__constraint text; __constraint_name text;
BEGIN BEGIN
BEGIN BEGIN
PERFORM raise_exception(_message => 'message text', PERFORM raise_exception(_message => 'message text',
_detail => 'detail text', _detail => 'detail text',
_hint => 'hint text', _hint => 'hint text',
_sqlstate => 'XX555', _sqlstate => 'XX555',
_schema => 'schema text', _schema_name => 'schema text',
_table => 'table text', _table_name => 'table text',
_column => 'column text', _column_name => 'column text',
_datatype => 'datatype text', _datatype_name => 'datatype text',
_constraint => 'constraint text'); _constraint_name => 'constraint text');
EXCEPTION WHEN SQLSTATE 'XX555' THEN EXCEPTION WHEN SQLSTATE 'XX555' THEN
GET STACKED DIAGNOSTICS __message = MESSAGE_TEXT, GET STACKED DIAGNOSTICS __message = MESSAGE_TEXT,
__detail = PG_EXCEPTION_DETAIL, __detail = PG_EXCEPTION_DETAIL,
...@@ -105,13 +105,13 @@ BEGIN ...@@ -105,13 +105,13 @@ BEGIN
__schema_name = SCHEMA_NAME, __schema_name = SCHEMA_NAME,
__table_name = TABLE_NAME, __table_name = TABLE_NAME,
__column_name = COLUMN_NAME, __column_name = COLUMN_NAME,
__datatype = PG_DATATYPE_NAME, __datatype_name = PG_DATATYPE_NAME,
__constraint = CONSTRAINT_NAME; __constraint_name = CONSTRAINT_NAME;
RAISE NOTICE 'handled exception' RAISE NOTICE 'handled exception'
USING DETAIL = format('message:(%s), detail:(%s), hint: (%s), sqlstate: (%s), ' USING DETAIL = format('message:(%s), detail:(%s), hint: (%s), sqlstate: (%s), '
'schema:(%s), table:(%s), column:(%s), datatype:(%s), constraint:(%s)', 'schema_name:(%s), table_name:(%s), column_name:(%s), datatype_name:(%s), constraint_name:(%s)',
__message, __detail, __hint, __sqlstate, __schema_name, __message, __detail, __hint, __sqlstate, __schema_name,
__table_name, __column_name, __datatype, __constraint); __table_name, __column_name, __datatype_name, __constraint_name);
END; END;
END; END;
$$; $$;
...@@ -122,7 +122,7 @@ $$; ...@@ -122,7 +122,7 @@ $$;
do $$ do $$
try: try:
plpy.execute("select raise_exception(_message => 'my message', _sqlstate => 'XX987', _hint => 'some hint', _table=> 'users_tab', _datatype => 'user_type')") plpy.execute("select raise_exception(_message => 'my message', _sqlstate => 'XX987', _hint => 'some hint', _table_name => 'users_tab', _datatype_name => 'user_type')")
except Exception, e: except Exception, e:
plpy.info(e.spidata) plpy.info(e.spidata)
raise e raise e
...@@ -130,8 +130,8 @@ $$ LANGUAGE plpythonu; ...@@ -130,8 +130,8 @@ $$ LANGUAGE plpythonu;
do $$ do $$
try: try:
plpy.error(message = 'my message', sqlstate = 'XX987', hint = 'some hint', table = 'users_tab', datatype = 'user_type') plpy.error(message = 'my message', sqlstate = 'XX987', hint = 'some hint', table_name = 'users_tab', datatype_name = 'user_type')
except Exception, e: except Exception, e:
plpy.info('sqlstate: %s, hint: %s, tablename: %s, datatype: %s' % (e.sqlstate, e.hint, e.table_name, e.datatype_name)) plpy.info('sqlstate: %s, hint: %s, table_name: %s, datatype_name: %s' % (e.sqlstate, e.hint, e.table_name, e.datatype_name))
raise e raise e
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment