mirror of
https://git.postgresql.org/git/postgresql.git
synced 2025-01-12 18:34:36 +08:00
PL/Python: Rename new keyword arguments of plpy.error() etc.
Rename schema -> schema_name etc. to remain consistent with C API and PL/pgSQL.
This commit is contained in:
parent
4bc0f165cb
commit
020140d84d
@ -1374,9 +1374,9 @@ $$ LANGUAGE plpythonu;
|
||||
The following keyword-only arguments are accepted:
|
||||
<literal>
|
||||
<replaceable>detail</replaceable>, <replaceable>hint</replaceable>,
|
||||
<replaceable>sqlstate</replaceable>, <replaceable>schema</replaceable>,
|
||||
<replaceable>table</replaceable>, <replaceable>column</replaceable>,
|
||||
<replaceable>datatype</replaceable> , <replaceable>constraint</replaceable>
|
||||
<replaceable>sqlstate</replaceable>, <replaceable>schema_name</replaceable>,
|
||||
<replaceable>table_name</replaceable>, <replaceable>column_name</replaceable>,
|
||||
<replaceable>datatype_name</replaceable> , <replaceable>constraint_name</replaceable>
|
||||
</literal>.
|
||||
The string representation of the objects passed as keyword-only arguments
|
||||
is used to enrich the messages reported to the client. For example:
|
||||
|
@ -9,11 +9,11 @@ plpy.info('This is message text.',
|
||||
detail = 'This is detail text',
|
||||
hint = 'This is hint text.',
|
||||
sqlstate = 'XX000',
|
||||
schema = 'any info about schema',
|
||||
table = 'any info about table',
|
||||
column = 'any info about column',
|
||||
datatype = 'any info about datatype',
|
||||
constraint = 'any info about constraint')
|
||||
schema_name = 'any info about schema',
|
||||
table_name = 'any info about table',
|
||||
column_name = 'any info about column',
|
||||
datatype_name = 'any info about datatype',
|
||||
constraint_name = 'any info about constraint')
|
||||
plpy.notice('notice', detail = 'some detail')
|
||||
plpy.warning('warning', detail = 'some detail')
|
||||
plpy.error('stop on error', detail = 'some detail', hint = 'some hint')
|
||||
@ -70,12 +70,12 @@ CONTEXT: PL/Python anonymous code block
|
||||
-- raise exception in python, handle exception in plgsql
|
||||
CREATE OR REPLACE FUNCTION raise_exception(_message text, _detail text DEFAULT NULL, _hint text DEFAULT NULL,
|
||||
_sqlstate text DEFAULT NULL,
|
||||
_schema text DEFAULT NULL, _table text DEFAULT NULL, _column text DEFAULT NULL,
|
||||
_datatype text DEFAULT NULL, _constraint text DEFAULT NULL)
|
||||
_schema_name text DEFAULT NULL, _table_name text DEFAULT NULL, _column_name text DEFAULT NULL,
|
||||
_datatype_name text DEFAULT NULL, _constraint_name text DEFAULT NULL)
|
||||
RETURNS void AS $$
|
||||
kwargs = { "message":_message, "detail":_detail, "hint":_hint,
|
||||
"sqlstate":_sqlstate, "schema":_schema, "table":_table,
|
||||
"column":_column, "datatype":_datatype, "constraint":_constraint }
|
||||
"sqlstate":_sqlstate, "schema_name":_schema_name, "table_name":_table_name,
|
||||
"column_name":_column_name, "datatype_name":_datatype_name, "constraint_name":_constraint_name }
|
||||
# ignore None values - should work on Python2.3
|
||||
dict = {}
|
||||
for k in kwargs:
|
||||
@ -101,11 +101,11 @@ SELECT raise_exception(_message => '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');
|
||||
_schema_name => 'schema text',
|
||||
_table_name => 'table text',
|
||||
_column_name => 'column text',
|
||||
_datatype_name => 'datatype text',
|
||||
_constraint_name => 'constraint text');
|
||||
ERROR: plpy.Error: message text
|
||||
DETAIL: detail text
|
||||
HINT: hint text
|
||||
@ -115,9 +115,9 @@ CONTEXT: Traceback (most recent call last):
|
||||
PL/Python function "raise_exception"
|
||||
SELECT raise_exception(_message => 'message text',
|
||||
_hint => 'hint text',
|
||||
_schema => 'schema text',
|
||||
_column => 'column text',
|
||||
_constraint => 'constraint text');
|
||||
_schema_name => 'schema text',
|
||||
_column_name => 'column text',
|
||||
_constraint_name => 'constraint text');
|
||||
ERROR: plpy.Error: message text
|
||||
HINT: hint text
|
||||
CONTEXT: Traceback (most recent call last):
|
||||
@ -133,19 +133,19 @@ DECLARE
|
||||
__schema_name text;
|
||||
__table_name text;
|
||||
__column_name text;
|
||||
__datatype text;
|
||||
__constraint text;
|
||||
__datatype_name text;
|
||||
__constraint_name text;
|
||||
BEGIN
|
||||
BEGIN
|
||||
PERFORM raise_exception(_message => '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');
|
||||
_schema_name => 'schema text',
|
||||
_table_name => 'table text',
|
||||
_column_name => 'column text',
|
||||
_datatype_name => 'datatype text',
|
||||
_constraint_name => 'constraint text');
|
||||
EXCEPTION WHEN SQLSTATE 'XX555' THEN
|
||||
GET STACKED DIAGNOSTICS __message = MESSAGE_TEXT,
|
||||
__detail = PG_EXCEPTION_DETAIL,
|
||||
@ -154,24 +154,24 @@ BEGIN
|
||||
__schema_name = SCHEMA_NAME,
|
||||
__table_name = TABLE_NAME,
|
||||
__column_name = COLUMN_NAME,
|
||||
__datatype = PG_DATATYPE_NAME,
|
||||
__constraint = CONSTRAINT_NAME;
|
||||
__datatype_name = PG_DATATYPE_NAME,
|
||||
__constraint_name = CONSTRAINT_NAME;
|
||||
RAISE NOTICE 'handled exception'
|
||||
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,
|
||||
__table_name, __column_name, __datatype, __constraint);
|
||||
__table_name, __column_name, __datatype_name, __constraint_name);
|
||||
END;
|
||||
END;
|
||||
$$;
|
||||
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,
|
||||
-- but that's not important for this test
|
||||
\set SHOW_CONTEXT never
|
||||
do $$
|
||||
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:
|
||||
plpy.info(e.spidata)
|
||||
raise e
|
||||
@ -181,11 +181,11 @@ ERROR: plpy.SPIError: plpy.Error: my message
|
||||
HINT: some hint
|
||||
do $$
|
||||
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:
|
||||
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
|
||||
$$ 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
|
||||
HINT: some hint
|
||||
|
@ -399,11 +399,11 @@ PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw)
|
||||
char *volatile message = NULL;
|
||||
char *volatile detail = NULL;
|
||||
char *volatile hint = NULL;
|
||||
char *volatile column = NULL;
|
||||
char *volatile constraint = NULL;
|
||||
char *volatile datatype = NULL;
|
||||
char *volatile table = NULL;
|
||||
char *volatile schema = NULL;
|
||||
char *volatile column_name = NULL;
|
||||
char *volatile constraint_name = NULL;
|
||||
char *volatile datatype_name = NULL;
|
||||
char *volatile table_name = NULL;
|
||||
char *volatile schema_name = NULL;
|
||||
volatile MemoryContext oldcontext;
|
||||
PyObject *key,
|
||||
*value;
|
||||
@ -456,16 +456,16 @@ PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw)
|
||||
hint = object_to_string(value);
|
||||
else if (strcmp(keyword, "sqlstate") == 0)
|
||||
sqlstatestr = object_to_string(value);
|
||||
else if (strcmp(keyword, "schema") == 0)
|
||||
schema = object_to_string(value);
|
||||
else if (strcmp(keyword, "table") == 0)
|
||||
table = object_to_string(value);
|
||||
else if (strcmp(keyword, "column") == 0)
|
||||
column = object_to_string(value);
|
||||
else if (strcmp(keyword, "datatype") == 0)
|
||||
datatype = object_to_string(value);
|
||||
else if (strcmp(keyword, "constraint") == 0)
|
||||
constraint = object_to_string(value);
|
||||
else if (strcmp(keyword, "schema_name") == 0)
|
||||
schema_name = object_to_string(value);
|
||||
else if (strcmp(keyword, "table_name") == 0)
|
||||
table_name = object_to_string(value);
|
||||
else if (strcmp(keyword, "column_name") == 0)
|
||||
column_name = object_to_string(value);
|
||||
else if (strcmp(keyword, "datatype_name") == 0)
|
||||
datatype_name = object_to_string(value);
|
||||
else if (strcmp(keyword, "constraint_name") == 0)
|
||||
constraint_name = object_to_string(value);
|
||||
else
|
||||
PLy_elog(ERROR, "'%s' is an invalid keyword argument for this function",
|
||||
keyword);
|
||||
@ -496,32 +496,32 @@ PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw)
|
||||
pg_verifymbstr(detail, strlen(detail), false);
|
||||
if (hint != NULL)
|
||||
pg_verifymbstr(hint, strlen(hint), false);
|
||||
if (schema != NULL)
|
||||
pg_verifymbstr(schema, strlen(schema), false);
|
||||
if (table != NULL)
|
||||
pg_verifymbstr(table, strlen(table), false);
|
||||
if (column != NULL)
|
||||
pg_verifymbstr(column, strlen(column), false);
|
||||
if (datatype != NULL)
|
||||
pg_verifymbstr(datatype, strlen(datatype), false);
|
||||
if (constraint != NULL)
|
||||
pg_verifymbstr(constraint, strlen(constraint), false);
|
||||
if (schema_name != NULL)
|
||||
pg_verifymbstr(schema_name, strlen(schema_name), false);
|
||||
if (table_name != NULL)
|
||||
pg_verifymbstr(table_name, strlen(table_name), false);
|
||||
if (column_name != NULL)
|
||||
pg_verifymbstr(column_name, strlen(column_name), false);
|
||||
if (datatype_name != NULL)
|
||||
pg_verifymbstr(datatype_name, strlen(datatype_name), false);
|
||||
if (constraint_name != NULL)
|
||||
pg_verifymbstr(constraint_name, strlen(constraint_name), false);
|
||||
|
||||
ereport(level,
|
||||
((sqlstate != 0) ? errcode(sqlstate) : 0,
|
||||
(message != NULL) ? errmsg_internal("%s", message) : 0,
|
||||
(detail != NULL) ? errdetail_internal("%s", detail) : 0,
|
||||
(hint != NULL) ? errhint("%s", hint) : 0,
|
||||
(column != NULL) ?
|
||||
err_generic_string(PG_DIAG_COLUMN_NAME, column) : 0,
|
||||
(constraint != NULL) ?
|
||||
err_generic_string(PG_DIAG_CONSTRAINT_NAME, constraint) : 0,
|
||||
(datatype != NULL) ?
|
||||
err_generic_string(PG_DIAG_DATATYPE_NAME, datatype) : 0,
|
||||
(table != NULL) ?
|
||||
err_generic_string(PG_DIAG_TABLE_NAME, table) : 0,
|
||||
(schema != NULL) ?
|
||||
err_generic_string(PG_DIAG_SCHEMA_NAME, schema) : 0));
|
||||
(column_name != NULL) ?
|
||||
err_generic_string(PG_DIAG_COLUMN_NAME, column_name) : 0,
|
||||
(constraint_name != NULL) ?
|
||||
err_generic_string(PG_DIAG_CONSTRAINT_NAME, constraint_name) : 0,
|
||||
(datatype_name != NULL) ?
|
||||
err_generic_string(PG_DIAG_DATATYPE_NAME, datatype_name) : 0,
|
||||
(table_name != NULL) ?
|
||||
err_generic_string(PG_DIAG_TABLE_NAME, table_name) : 0,
|
||||
(schema_name != NULL) ?
|
||||
err_generic_string(PG_DIAG_SCHEMA_NAME, schema_name) : 0));
|
||||
}
|
||||
PG_CATCH();
|
||||
{
|
||||
|
@ -9,11 +9,11 @@ plpy.info('This is message text.',
|
||||
detail = 'This is detail text',
|
||||
hint = 'This is hint text.',
|
||||
sqlstate = 'XX000',
|
||||
schema = 'any info about schema',
|
||||
table = 'any info about table',
|
||||
column = 'any info about column',
|
||||
datatype = 'any info about datatype',
|
||||
constraint = 'any info about constraint')
|
||||
schema_name = 'any info about schema',
|
||||
table_name = 'any info about table',
|
||||
column_name = 'any info about column',
|
||||
datatype_name = 'any info about datatype',
|
||||
constraint_name = 'any info about constraint')
|
||||
plpy.notice('notice', detail = 'some detail')
|
||||
plpy.warning('warning', detail = 'some detail')
|
||||
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
|
||||
-- raise exception in python, handle exception in plgsql
|
||||
CREATE OR REPLACE FUNCTION raise_exception(_message text, _detail text DEFAULT NULL, _hint text DEFAULT NULL,
|
||||
_sqlstate text DEFAULT NULL,
|
||||
_schema text DEFAULT NULL, _table text DEFAULT NULL, _column text DEFAULT NULL,
|
||||
_datatype text DEFAULT NULL, _constraint text DEFAULT NULL)
|
||||
_schema_name text DEFAULT NULL, _table_name text DEFAULT NULL, _column_name text DEFAULT NULL,
|
||||
_datatype_name text DEFAULT NULL, _constraint_name text DEFAULT NULL)
|
||||
RETURNS void AS $$
|
||||
kwargs = { "message":_message, "detail":_detail, "hint":_hint,
|
||||
"sqlstate":_sqlstate, "schema":_schema, "table":_table,
|
||||
"column":_column, "datatype":_datatype, "constraint":_constraint }
|
||||
"sqlstate":_sqlstate, "schema_name":_schema_name, "table_name":_table_name,
|
||||
"column_name":_column_name, "datatype_name":_datatype_name, "constraint_name":_constraint_name }
|
||||
# ignore None values - should work on Python2.3
|
||||
dict = {}
|
||||
for k in kwargs:
|
||||
@ -63,17 +63,17 @@ SELECT raise_exception(_message => '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');
|
||||
_schema_name => 'schema text',
|
||||
_table_name => 'table text',
|
||||
_column_name => 'column text',
|
||||
_datatype_name => 'datatype text',
|
||||
_constraint_name => 'constraint text');
|
||||
|
||||
SELECT raise_exception(_message => 'message text',
|
||||
_hint => 'hint text',
|
||||
_schema => 'schema text',
|
||||
_column => 'column text',
|
||||
_constraint => 'constraint text');
|
||||
_schema_name => 'schema text',
|
||||
_column_name => 'column text',
|
||||
_constraint_name => 'constraint text');
|
||||
|
||||
DO $$
|
||||
DECLARE
|
||||
@ -84,19 +84,19 @@ DECLARE
|
||||
__schema_name text;
|
||||
__table_name text;
|
||||
__column_name text;
|
||||
__datatype text;
|
||||
__constraint text;
|
||||
__datatype_name text;
|
||||
__constraint_name text;
|
||||
BEGIN
|
||||
BEGIN
|
||||
PERFORM raise_exception(_message => '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');
|
||||
_schema_name => 'schema text',
|
||||
_table_name => 'table text',
|
||||
_column_name => 'column text',
|
||||
_datatype_name => 'datatype text',
|
||||
_constraint_name => 'constraint text');
|
||||
EXCEPTION WHEN SQLSTATE 'XX555' THEN
|
||||
GET STACKED DIAGNOSTICS __message = MESSAGE_TEXT,
|
||||
__detail = PG_EXCEPTION_DETAIL,
|
||||
@ -105,13 +105,13 @@ BEGIN
|
||||
__schema_name = SCHEMA_NAME,
|
||||
__table_name = TABLE_NAME,
|
||||
__column_name = COLUMN_NAME,
|
||||
__datatype = PG_DATATYPE_NAME,
|
||||
__constraint = CONSTRAINT_NAME;
|
||||
__datatype_name = PG_DATATYPE_NAME,
|
||||
__constraint_name = CONSTRAINT_NAME;
|
||||
RAISE NOTICE 'handled exception'
|
||||
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,
|
||||
__table_name, __column_name, __datatype, __constraint);
|
||||
__table_name, __column_name, __datatype_name, __constraint_name);
|
||||
END;
|
||||
END;
|
||||
$$;
|
||||
@ -122,7 +122,7 @@ $$;
|
||||
|
||||
do $$
|
||||
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:
|
||||
plpy.info(e.spidata)
|
||||
raise e
|
||||
@ -130,8 +130,8 @@ $$ LANGUAGE plpythonu;
|
||||
|
||||
do $$
|
||||
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:
|
||||
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
|
||||
$$ LANGUAGE plpythonu;
|
||||
|
Loading…
Reference in New Issue
Block a user