summaryrefslogtreecommitdiff
path: root/src/pl/plpython/sql/plpython_error.sql
diff options
context:
space:
mode:
authorPeter Eisentraut <peter_e@gmx.net>2011-02-28 18:41:10 +0200
committerPeter Eisentraut <peter_e@gmx.net>2011-02-28 18:41:10 +0200
commit474a42473adf9b18417242f1fc0691a857ec578b (patch)
tree427ce236377cc8c596f258924f6374d990be77e3 /src/pl/plpython/sql/plpython_error.sql
parent0ef0b302040284a087c1bc90df5b115f0dea7764 (diff)
downloadpostgresql-474a42473adf9b18417242f1fc0691a857ec578b.tar.gz
PL/Python custom SPI exceptions
This provides a separate exception class for each error code that the backend defines, as well as the ability to get the SQLSTATE from the exception object. Jan UrbaƄski, reviewed by Steve Singer
Diffstat (limited to 'src/pl/plpython/sql/plpython_error.sql')
-rw-r--r--src/pl/plpython/sql/plpython_error.sql21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/pl/plpython/sql/plpython_error.sql b/src/pl/plpython/sql/plpython_error.sql
index 7861cd61a2..0f456f4bc3 100644
--- a/src/pl/plpython/sql/plpython_error.sql
+++ b/src/pl/plpython/sql/plpython_error.sql
@@ -131,6 +131,27 @@ return None
SELECT valid_type('rick');
+/* check catching specific types of exceptions
+ */
+CREATE TABLE specific (
+ i integer PRIMARY KEY
+);
+
+CREATE FUNCTION specific_exception(i integer) RETURNS void AS
+$$
+from plpy import spiexceptions
+try:
+ plpy.execute("insert into specific values (%s)" % (i or "NULL"));
+except spiexceptions.NotNullViolation, e:
+ plpy.notice("Violated the NOT NULL constraint, sqlstate %s" % e.sqlstate)
+except spiexceptions.UniqueViolation, e:
+ plpy.notice("Violated the UNIQUE constraint, sqlstate %s" % e.sqlstate)
+$$ LANGUAGE plpythonu;
+
+SELECT specific_exception(2);
+SELECT specific_exception(NULL);
+SELECT specific_exception(2);
+
/* manually starting subtransactions - a bad idea
*/
CREATE FUNCTION manual_subxact() RETURNS void AS $$