From 7d400b095a61cde03c7faba73e29acaab4a56f3e Mon Sep 17 00:00:00 2001 From: Miha Date: Thu, 2 Sep 2021 19:59:11 +0300 Subject: [PATCH] add where property to PGSQLExceptionInfo --- .../postgres/api/jdbc/PGSQLExceptionInfo.java | 7 +++-- .../impossibl/postgres/jdbc/ErrorUtils.java | 1 + ...IntegrityConstraintViolationException.java | 10 +++++++ .../postgres/jdbc/PGSQLSimpleException.java | 10 +++++++ .../postgres/jdbc/ServerErrorTest.java | 27 +++++++++++++++++++ 5 files changed, 53 insertions(+), 2 deletions(-) diff --git a/driver/src/main/java/com/impossibl/postgres/api/jdbc/PGSQLExceptionInfo.java b/driver/src/main/java/com/impossibl/postgres/api/jdbc/PGSQLExceptionInfo.java index 86ee44d66..dab16ec3d 100644 --- a/driver/src/main/java/com/impossibl/postgres/api/jdbc/PGSQLExceptionInfo.java +++ b/driver/src/main/java/com/impossibl/postgres/api/jdbc/PGSQLExceptionInfo.java @@ -31,9 +31,9 @@ /** * Driver specific interface for exceptions that carry extended error * information reported by the server. - * + * * @author kdubb - * + * */ public interface PGSQLExceptionInfo { @@ -61,4 +61,7 @@ public interface PGSQLExceptionInfo { void setDetail(String details); + String getWhere(); + + void setWhere(String where); } diff --git a/driver/src/main/java/com/impossibl/postgres/jdbc/ErrorUtils.java b/driver/src/main/java/com/impossibl/postgres/jdbc/ErrorUtils.java index be56d48bb..b50b2c175 100644 --- a/driver/src/main/java/com/impossibl/postgres/jdbc/ErrorUtils.java +++ b/driver/src/main/java/com/impossibl/postgres/jdbc/ErrorUtils.java @@ -205,6 +205,7 @@ public static SQLException makeSQLException(String messagePrefix, Notice notice) e.setDatatype(notice.getDatatype()); e.setConstraint(notice.getConstraint()); e.setDetail(notice.getDetail()); + e.setWhere(notice.getWhere()); return (SQLException) e; } diff --git a/driver/src/main/java/com/impossibl/postgres/jdbc/PGSQLIntegrityConstraintViolationException.java b/driver/src/main/java/com/impossibl/postgres/jdbc/PGSQLIntegrityConstraintViolationException.java index 7631c2f55..b4d797988 100644 --- a/driver/src/main/java/com/impossibl/postgres/jdbc/PGSQLIntegrityConstraintViolationException.java +++ b/driver/src/main/java/com/impossibl/postgres/jdbc/PGSQLIntegrityConstraintViolationException.java @@ -42,6 +42,7 @@ public class PGSQLIntegrityConstraintViolationException extends SQLIntegrityCons private String datatype; private String constraint; private String detail; + private String where; public PGSQLIntegrityConstraintViolationException() { super(); @@ -135,4 +136,13 @@ public void setDetail(String details) { this.detail = details; } + @Override + public String getWhere() { + return where; + } + + @Override + public void setWhere(String where) { + this.where = where; + } } diff --git a/driver/src/main/java/com/impossibl/postgres/jdbc/PGSQLSimpleException.java b/driver/src/main/java/com/impossibl/postgres/jdbc/PGSQLSimpleException.java index 187e171a5..07ff413ef 100644 --- a/driver/src/main/java/com/impossibl/postgres/jdbc/PGSQLSimpleException.java +++ b/driver/src/main/java/com/impossibl/postgres/jdbc/PGSQLSimpleException.java @@ -44,6 +44,7 @@ public class PGSQLSimpleException extends SQLException implements PGSQLException private String datatype; private String constraint; private String detail; + private String where; public PGSQLSimpleException() { super(); @@ -137,4 +138,13 @@ public void setDetail(String details) { this.detail = details; } + @Override + public String getWhere() { + return where; + } + + @Override + public void setWhere(String where) { + this.where = where; + } } diff --git a/driver/src/test/java/com/impossibl/postgres/jdbc/ServerErrorTest.java b/driver/src/test/java/com/impossibl/postgres/jdbc/ServerErrorTest.java index b02402dbb..4b5c0fc37 100644 --- a/driver/src/test/java/com/impossibl/postgres/jdbc/ServerErrorTest.java +++ b/driver/src/test/java/com/impossibl/postgres/jdbc/ServerErrorTest.java @@ -72,6 +72,14 @@ public void before() throws Exception { TestUtil.createTable(con, "testerr", "id int not null, val testdom not null"); stmt.execute("ALTER TABLE testerr ADD CONSTRAINT testerr_pk PRIMARY KEY (id)"); + + stmt.execute(""" + CREATE OR REPLACE FUNCTION testfun() RETURNS VOID AS $$ + BEGIN + raise exception 'test exception'; + END; + $$ LANGUAGE PLPGSQL; + """); stmt.close(); } @@ -82,6 +90,7 @@ public void after() throws Exception { Statement stmt = con.createStatement(); stmt.execute("DROP DOMAIN testdom"); + stmt.execute("DROP FUNCTION testfun()"); stmt.close(); TestUtil.closeDB(con); @@ -161,4 +170,22 @@ public void testDatatype() throws Exception { stmt.close(); } + @Test + public void testWhere() throws Exception { + if (!(con.unwrap(PGConnection.class)).isServerMinimumVersion(9, 3)) + return; + + Statement stmt = con.createStatement(); + + try { + stmt.executeUpdate("select testfun()"); + fail("Should have thrown a constraint violation."); + } + catch (SQLException e) { + PGSQLExceptionInfo sqle = (PGSQLExceptionInfo) e; + assertEquals("PL/pgSQL function testfun() line 3 at RAISE", sqle.getWhere()); + } + + stmt.close(); + } }