11CREATE FUNCTION elog_test() RETURNS void
22AS $$
3- plpy.debug('debug', detail = 'some detail')
4- plpy.log('log', detail = 'some detail')
5- plpy.info('info', detail = 'some detail')
3+ plpy.debug('debug', detail= 'some detail')
4+ plpy.log('log', detail= 'some detail')
5+ plpy.info('info', detail= 'some detail')
66plpy.info()
7- plpy.info('the question', detail = 42);
7+ plpy.info('the question', detail= 42);
88plpy.info('This is message text.',
9- detail = 'This is detail text',
10- hint = 'This is hint text.',
11- sqlstate = 'XX000',
12- schema_name = 'any info about schema',
13- table_name = 'any info about table',
14- column_name = 'any info about column',
15- datatype_name = 'any info about datatype',
16- constraint_name = 'any info about constraint')
17- plpy.notice('notice', detail = 'some detail')
18- plpy.warning('warning', detail = 'some detail')
19- plpy.error('stop on error', detail = 'some detail', hint = 'some hint')
9+ detail= 'This is detail text',
10+ hint= 'This is hint text.',
11+ sqlstate= 'XX000',
12+ schema_name= 'any info about schema',
13+ table_name= 'any info about table',
14+ column_name= 'any info about column',
15+ datatype_name= 'any info about datatype',
16+ constraint_name= 'any info about constraint')
17+ plpy.notice('notice', detail= 'some detail')
18+ plpy.warning('warning', detail= 'some detail')
19+ plpy.error('stop on error', detail= 'some detail', hint= 'some hint')
2020$$ LANGUAGE plpythonu;
2121SELECT elog_test();
2222INFO: info
@@ -36,92 +36,98 @@ DETAIL: some detail
3636HINT: some hint
3737CONTEXT: Traceback (most recent call last):
3838 PL/Python function "elog_test", line 18, in <module>
39- plpy.error('stop on error', detail = 'some detail', hint = 'some hint')
39+ plpy.error('stop on error', detail= 'some detail', hint= 'some hint')
4040PL/Python function "elog_test"
41- do $$ plpy.info('other types', detail = (10,20)) $$ LANGUAGE plpythonu;
41+ DO $$ plpy.info('other types', detail= (10, 20)) $$ LANGUAGE plpythonu;
4242INFO: other types
4343DETAIL: (10, 20)
44- do $$
44+ DO $$
4545import time;
4646from datetime import date
47- plpy.info('other types', detail = date(2016,2, 26))
47+ plpy.info('other types', detail= date(2016, 2, 26))
4848$$ LANGUAGE plpythonu;
4949INFO: other types
5050DETAIL: 2016-02-26
51- do $$
51+ DO $$
5252basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
53- plpy.info('other types', detail = basket)
53+ plpy.info('other types', detail= basket)
5454$$ LANGUAGE plpythonu;
5555INFO: other types
5656DETAIL: ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
5757-- should fail
58- do $$ plpy.info('wrong sqlstate', sqlstate='54444A') $$ LANGUAGE plpythonu;
58+ DO $$ plpy.info('wrong sqlstate', sqlstate='54444A') $$ LANGUAGE plpythonu;
5959ERROR: invalid SQLSTATE code
6060CONTEXT: PL/Python anonymous code block
61- do $$ plpy.info('unsupported argument', blabla='fooboo') $$ LANGUAGE plpythonu;
61+ DO $$ plpy.info('unsupported argument', blabla='fooboo') $$ LANGUAGE plpythonu;
6262ERROR: 'blabla' is an invalid keyword argument for this function
6363CONTEXT: PL/Python anonymous code block
64- do $$ plpy.info('first message', message='second message') $$ LANGUAGE plpythonu;
64+ DO $$ plpy.info('first message', message='second message') $$ LANGUAGE plpythonu;
6565ERROR: the message is already specified
6666CONTEXT: PL/Python anonymous code block
67- do $$ plpy.info('first message', 'second message', message='third message') $$ LANGUAGE plpythonu;
67+ DO $$ plpy.info('first message', 'second message', message='third message') $$ LANGUAGE plpythonu;
6868ERROR: the message is already specified
6969CONTEXT: PL/Python anonymous code block
7070-- raise exception in python, handle exception in plgsql
7171CREATE OR REPLACE FUNCTION raise_exception(_message text, _detail text DEFAULT NULL, _hint text DEFAULT NULL,
72- _sqlstate text DEFAULT NULL,
73- _schema_name text DEFAULT NULL, _table_name text DEFAULT NULL, _column_name text DEFAULT NULL,
74- _datatype_name text DEFAULT NULL, _constraint_name text DEFAULT NULL)
72+ _sqlstate text DEFAULT NULL,
73+ _schema_name text DEFAULT NULL,
74+ _table_name text DEFAULT NULL,
75+ _column_name text DEFAULT NULL,
76+ _datatype_name text DEFAULT NULL,
77+ _constraint_name text DEFAULT NULL)
7578RETURNS void AS $$
76- kwargs = { "message":_message, "detail":_detail, "hint":_hint,
77- "sqlstate":_sqlstate, "schema_name":_schema_name, "table_name":_table_name,
78- "column_name":_column_name, "datatype_name":_datatype_name, "constraint_name":_constraint_name }
79+ kwargs = {
80+ "message": _message, "detail": _detail, "hint": _hint,
81+ "sqlstate": _sqlstate, "schema_name": _schema_name, "table_name": _table_name,
82+ "column_name": _column_name, "datatype_name": _datatype_name,
83+ "constraint_name": _constraint_name
84+ }
7985# ignore None values - should work on Python2.3
8086dict = {}
8187for k in kwargs:
82- if kwargs[k] is not None:
83- dict[k] = kwargs[k]
88+ if kwargs[k] is not None:
89+ dict[k] = kwargs[k]
8490plpy.error(**dict)
8591$$ LANGUAGE plpythonu;
8692SELECT raise_exception('hello', 'world');
8793ERROR: plpy.Error: hello
8894DETAIL: world
8995CONTEXT: Traceback (most recent call last):
90- PL/Python function "raise_exception", line 10 , in <module>
96+ PL/Python function "raise_exception", line 13 , in <module>
9197 plpy.error(**dict)
9298PL/Python function "raise_exception"
9399SELECT raise_exception('message text', 'detail text', _sqlstate => 'YY333');
94100ERROR: plpy.Error: message text
95101DETAIL: detail text
96102CONTEXT: Traceback (most recent call last):
97- PL/Python function "raise_exception", line 10 , in <module>
103+ PL/Python function "raise_exception", line 13 , in <module>
98104 plpy.error(**dict)
99105PL/Python function "raise_exception"
100106SELECT raise_exception(_message => 'message text',
101- _detail => 'detail text',
102- _hint => 'hint text',
103- _sqlstate => 'XX555',
104- _schema_name => 'schema text',
105- _table_name => 'table text',
106- _column_name => 'column text',
107- _datatype_name => 'datatype text',
108- _constraint_name => 'constraint text');
107+ _detail => 'detail text',
108+ _hint => 'hint text',
109+ _sqlstate => 'XX555',
110+ _schema_name => 'schema text',
111+ _table_name => 'table text',
112+ _column_name => 'column text',
113+ _datatype_name => 'datatype text',
114+ _constraint_name => 'constraint text');
109115ERROR: plpy.Error: message text
110116DETAIL: detail text
111117HINT: hint text
112118CONTEXT: Traceback (most recent call last):
113- PL/Python function "raise_exception", line 10 , in <module>
119+ PL/Python function "raise_exception", line 13 , in <module>
114120 plpy.error(**dict)
115121PL/Python function "raise_exception"
116122SELECT raise_exception(_message => 'message text',
117- _hint => 'hint text',
118- _schema_name => 'schema text',
119- _column_name => 'column text',
120- _constraint_name => 'constraint text');
123+ _hint => 'hint text',
124+ _schema_name => 'schema text',
125+ _column_name => 'column text',
126+ _constraint_name => 'constraint text');
121127ERROR: plpy.Error: message text
122128HINT: hint text
123129CONTEXT: Traceback (most recent call last):
124- PL/Python function "raise_exception", line 10 , in <module>
130+ PL/Python function "raise_exception", line 13 , in <module>
125131 plpy.error(**dict)
126132PL/Python function "raise_exception"
127133DO $$
@@ -157,34 +163,34 @@ BEGIN
157163 __datatype_name = PG_DATATYPE_NAME,
158164 __constraint_name = CONSTRAINT_NAME;
159165 RAISE NOTICE 'handled exception'
160- USING DETAIL = format('message:(%s), detail:(%s), hint: (%s), sqlstate: (%s), '
161- 'schema_name:(%s), table_name:(%s), column_name:(%s), datatype_name:(%s), constraint_name:(%s)',
162- __message, __detail, __hint, __sqlstate, __schema_name,
163- __table_name, __column_name, __datatype_name, __constraint_name);
166+ USING DETAIL = format('message:(%s), detail:(%s), hint: (%s), sqlstate: (%s), '
167+ 'schema_name:(%s), table_name:(%s), column_name:(%s), datatype_name:(%s), constraint_name:(%s)',
168+ __message, __detail, __hint, __sqlstate, __schema_name,
169+ __table_name, __column_name, __datatype_name, __constraint_name);
164170 END;
165171END;
166172$$;
167173NOTICE: handled exception
168174DETAIL: 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)
169- -- the displayed context is different between Python2 and Python3,
170- -- but that's not important for this test
175+ -- The displayed context is different between Python2 and Python3,
176+ -- but that's not important for this test.
171177\set SHOW_CONTEXT never
172- do $$
178+ DO $$
173179try:
174- plpy.execute("select raise_exception(_message => 'my message', _sqlstate => 'XX987', _hint => 'some hint', _table_name => 'users_tab', _datatype_name => 'user_type')")
180+ plpy.execute("select raise_exception(_message => 'my message', _sqlstate => 'XX987', _hint => 'some hint', _table_name => 'users_tab', _datatype_name => 'user_type')")
175181except Exception, e:
176- plpy.info(e.spidata)
177- raise e
182+ plpy.info(e.spidata)
183+ raise e
178184$$ LANGUAGE plpythonu;
179185INFO: (119577128, None, 'some hint', None, 0, None, 'users_tab', None, 'user_type', None)
180186ERROR: plpy.SPIError: plpy.Error: my message
181187HINT: some hint
182- do $$
188+ DO $$
183189try:
184- plpy.error(message = 'my message', sqlstate = 'XX987', hint = 'some hint', table_name = 'users_tab', datatype_name = 'user_type')
190+ plpy.error(message = 'my message', sqlstate = 'XX987', hint = 'some hint', table_name = 'users_tab', datatype_name = 'user_type')
185191except Exception, e:
186- plpy.info('sqlstate: %s, hint: %s, table_name: %s, datatype_name: %s' % (e.sqlstate, e.hint, e.table_name, e.datatype_name))
187- raise e
192+ plpy.info('sqlstate: %s, hint: %s, table_name: %s, datatype_name: %s' % (e.sqlstate, e.hint, e.table_name, e.datatype_name))
193+ raise e
188194$$ LANGUAGE plpythonu;
189195INFO: sqlstate: XX987, hint: some hint, table_name: users_tab, datatype_name: user_type
190196ERROR: plpy.Error: my message
0 commit comments