postgresql-jdbc.git / summary / log / commit / refs

commit    26a02ef2325af1bbfdbcb729b5b2dde4fb7959a2
Author:   Kris Jurka <books@ejurka.com>
Date:     Sun Apr 10 16:44:13 2005 +0000

    Make all the fields of a server error message available.
    
    As requested by NielsG.


org/postgresql/core/v3/ConnectionFactoryImpl.java | 6 +- org/postgresql/core/v3/QueryExecutorImpl.java | 4 +- org/postgresql/test/jdbc2/MiscTest.java | 24 +++++++- org/postgresql/util/PSQLException.java.in | 15 ++++- org/postgresql/util/PSQLWarning.java | 7 ++- org/postgresql/util/ServerErrorMessage.java | 74 ++++++++++++++++++++++- 6 files changed, 120 insertions(+), 10 deletions(-) diff --git a/org/postgresql/core/v3/ConnectionFactoryImpl.java b/org/postgresql/core/v3/ConnectionFactoryImpl.java index 7f8bacbd..913c8c7f 100644 --- a/org/postgresql/core/v3/ConnectionFactoryImpl.java +++ b/org/postgresql/core/v3/ConnectionFactoryImpl.java @@ -4,7 +4,7 @@ * Copyright (c) 2004, Open Cloud Limited. * * IDENTIFICATION -* $PostgreSQL: pgjdbc/org/postgresql/core/v3/ConnectionFactoryImpl.java,v 1.8 2004/12/11 03:31:52 jurka Exp $ +* $PostgreSQL: pgjdbc/org/postgresql/core/v3/ConnectionFactoryImpl.java,v 1.9 2005/01/11 08:25:44 jurka Exp $ * *------------------------------------------------------------------------- */ @@ -272,7 +272,7 @@ public class ConnectionFactoryImpl extends ConnectionFactory { ServerErrorMessage errorMsg = new ServerErrorMessage(pgStream.ReceiveString(l_elen - 4)); if (Driver.logDebug) Driver.debug(" <=BE ErrorMessage(" + errorMsg + ")"); - throw new PSQLException(GT.tr("Connection rejected: {0}.", errorMsg), PSQLState.CONNECTION_REJECTED); + throw new PSQLException(errorMsg); case 'R': // Authentication request. @@ -440,7 +440,7 @@ public class ConnectionFactoryImpl extends ConnectionFactory { if (Driver.logDebug) Driver.debug(" <=BE ErrorMessage(" + l_errorMsg + ")"); - throw new PSQLException(GT.tr("Backend start-up failed: {0}.", l_errorMsg), new PSQLState(l_errorMsg.getSQLState())); + throw new PSQLException(l_errorMsg); case 'N': // Warning diff --git a/org/postgresql/core/v3/QueryExecutorImpl.java b/org/postgresql/core/v3/QueryExecutorImpl.java index 945a9b80..dce00fe7 100644 --- a/org/postgresql/core/v3/QueryExecutorImpl.java +++ b/org/postgresql/core/v3/QueryExecutorImpl.java @@ -4,7 +4,7 @@ * Copyright (c) 2004, Open Cloud Limited. * * IDENTIFICATION -* $PostgreSQL: pgjdbc/org/postgresql/core/v3/QueryExecutorImpl.java,v 1.20 2005/01/27 22:50:13 oliver Exp $ +* $PostgreSQL: pgjdbc/org/postgresql/core/v3/QueryExecutorImpl.java,v 1.21 2005/02/01 07:27:54 jurka Exp $ * *------------------------------------------------------------------------- */ @@ -1468,7 +1468,7 @@ public class QueryExecutorImpl implements QueryExecutor { if (Driver.logDebug) Driver.debug(" <=BE ErrorMessage(" + errorMsg.toString() + ")"); - return new SQLException(errorMsg.toString(), errorMsg.getSQLState()); + return new PSQLException(errorMsg); } private SQLWarning receiveNoticeResponse() throws IOException { diff --git a/org/postgresql/test/jdbc2/MiscTest.java b/org/postgresql/test/jdbc2/MiscTest.java index 7fcd5b92..f4ee26b1 100644 --- a/org/postgresql/test/jdbc2/MiscTest.java +++ b/org/postgresql/test/jdbc2/MiscTest.java @@ -3,7 +3,7 @@ * Copyright (c) 2004-2005, PostgreSQL Global Development Group * * IDENTIFICATION -* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/MiscTest.java,v 1.16 2004/11/09 08:54:39 jurka Exp $ +* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/MiscTest.java,v 1.17 2005/01/11 08:25:48 jurka Exp $ * *------------------------------------------------------------------------- */ @@ -79,6 +79,28 @@ public class MiscTest extends TestCase con.close(); } + public void testWarning() throws Exception + { + Connection con = TestUtil.openDB(); + Statement stmt = con.createStatement(); + stmt.execute("CREATE TEMP TABLE t(a int primary key)"); + SQLWarning warning = stmt.getWarnings(); + // We should get a warning about primary key index creation + // it's possible we won't depending on the server's + // client_min_messages setting. + while (warning != null) { + // Verify that the SQLWarning is serializable. + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(warning); + oos.close(); + warning = warning.getNextWarning(); + } + + stmt.close(); + con.close(); + } + public void xtestLocking() throws SQLException { Connection con = TestUtil.openDB(); diff --git a/org/postgresql/util/PSQLException.java.in b/org/postgresql/util/PSQLException.java.in index 13272f9c..09d62aeb 100644 --- a/org/postgresql/util/PSQLException.java.in +++ b/org/postgresql/util/PSQLException.java.in @@ -3,7 +3,7 @@ * Copyright (c) 2003-2005, PostgreSQL Global Development Group * * IDENTIFICATION -* $PostgreSQL: pgjdbc/org/postgresql/util/PSQLException.java.in,v 1.9 2005/01/11 08:25:49 jurka Exp $ +* $PostgreSQL: pgjdbc/org/postgresql/util/PSQLException.java.in,v 1.10 2005/01/14 01:20:25 oliver Exp $ * *------------------------------------------------------------------------- */ @@ -18,6 +18,8 @@ import org.postgresql.Driver; public class PSQLException extends SQLException { + private ServerErrorMessage _serverError; + public PSQLException(String msg, PSQLState state, Throwable cause) { super(addCauseToMessage(msg, cause), state == null ? null : state.getState()); @@ -32,6 +34,17 @@ public class PSQLException extends SQLException this(msg, state, null); } + public PSQLException(ServerErrorMessage serverError) + { + this(serverError.toString(), new PSQLState(serverError.getSQLState())); + _serverError = serverError; + } + + public ServerErrorMessage getServerErrorMessage() + { + return _serverError; + } + private static String addCauseToMessage(String msg, Throwable cause) { boolean hasInitCause = true; @NOINITCAUSE@ hasInitCause = false; diff --git a/org/postgresql/util/PSQLWarning.java b/org/postgresql/util/PSQLWarning.java index a4087c38..022af95e 100644 --- a/org/postgresql/util/PSQLWarning.java +++ b/org/postgresql/util/PSQLWarning.java @@ -3,7 +3,7 @@ * Copyright (c) 2004-2005, PostgreSQL Global Development Group * * IDENTIFICATION -* $PostgreSQL: pgjdbc/org/postgresql/util/PSQLWarning.java,v 1.4 2005/01/11 08:25:49 jurka Exp $ +* $PostgreSQL: pgjdbc/org/postgresql/util/PSQLWarning.java,v 1.5 2005/02/10 19:53:17 jurka Exp $ * *------------------------------------------------------------------------- */ @@ -35,4 +35,9 @@ public class PSQLWarning extends SQLWarning { return serverError.getMessage(); } + + public ServerErrorMessage getServerErrorMessage() + { + return serverError; + } } diff --git a/org/postgresql/util/ServerErrorMessage.java b/org/postgresql/util/ServerErrorMessage.java index e6b8a642..f2bb8699 100644 --- a/org/postgresql/util/ServerErrorMessage.java +++ b/org/postgresql/util/ServerErrorMessage.java @@ -3,7 +3,7 @@ * Copyright (c) 2004-2005, PostgreSQL Global Development Group * * IDENTIFICATION -* $PostgreSQL: pgjdbc/org/postgresql/util/ServerErrorMessage.java,v 1.6 2005/01/11 08:25:49 jurka Exp $ +* $PostgreSQL: pgjdbc/org/postgresql/util/ServerErrorMessage.java,v 1.7 2005/02/10 19:53:17 jurka Exp $ * *------------------------------------------------------------------------- */ @@ -11,8 +11,9 @@ package org.postgresql.util; import org.postgresql.Driver; import java.util.Hashtable; +import java.io.Serializable; -public class ServerErrorMessage +public class ServerErrorMessage implements Serializable { private static final Character SEVERITY = new Character('S'); @@ -25,6 +26,8 @@ public class ServerErrorMessage private static final Character LINE = new Character('L'); private static final Character ROUTINE = new Character('R'); private static final Character SQLSTATE = new Character('C'); + private static final Character INTERNAL_POSITION = new Character('p'); + private static final Character INTERNAL_QUERY = new Character('q'); private Hashtable m_mesgParts; @@ -62,6 +65,64 @@ public class ServerErrorMessage return (String)m_mesgParts.get(MESSAGE); } + public String getSeverity() + { + return (String)m_mesgParts.get(SEVERITY); + } + + public String getDetail() + { + return (String)m_mesgParts.get(DETAIL); + } + + public String getHint() + { + return (String)m_mesgParts.get(HINT); + } + + public int getPosition() + { + return getIntegerPart(POSITION); + } + + public String getWhere() + { + return (String)m_mesgParts.get(WHERE); + } + + public String getFile() + { + return (String)m_mesgParts.get(FILE); + } + + public int getLine() + { + return getIntegerPart(LINE); + } + + public String getRoutine() + { + return (String)m_mesgParts.get(ROUTINE); + } + + public String getInternalQuery() + { + return (String)m_mesgParts.get(INTERNAL_QUERY); + } + + public int getInternalPosition() + { + return getIntegerPart(INTERNAL_POSITION); + } + + private int getIntegerPart(Character c) + { + String s = (String)m_mesgParts.get(c); + if (s == null) + return 0; + return Integer.parseInt(s); + } + public String toString() { //Now construct the message from what the server sent @@ -71,6 +132,8 @@ public class ServerErrorMessage // Hint: \n // Position: \n // Where: \n + // Internal Query: \n + // Internal Position: \n // Location: File:Line:Routine \n // SQLState: \n // @@ -103,6 +166,13 @@ public class ServerErrorMessage } if (Driver.logDebug) { + String l_internalQuery = (String)m_mesgParts.get(INTERNAL_QUERY); + if (l_internalQuery != null) + l_totalMessage.append("\n ").append(GT.tr("Internal Query: {0}", l_internalQuery)); + String l_internalPosition = (String)m_mesgParts.get(INTERNAL_POSITION); + if (l_internalPosition != null) + l_totalMessage.append("\n ").append(GT.tr("Internal Position: {0}", l_internalPosition)); + String l_file = (String)m_mesgParts.get(FILE); String l_line = (String)m_mesgParts.get(LINE); String l_routine = (String)m_mesgParts.get(ROUTINE); [parent: aeddf58f81eb]