This set of changes applies a patch from KHO at redhat to add some SQLState
authorBarry Lind <barry@xythos.com>
Mon, 8 Sep 2003 17:30:22 +0000 (17:30 +0000)
committerBarry Lind <barry@xythos.com>
Mon, 8 Sep 2003 17:30:22 +0000 (17:30 +0000)
support to the jdbc driver.
That patch needed some work: it assumed the sqlcode in a server message was
fixed in its position, the patch lost the ability to pass exceptions, and the
patch missed a couple of places where server errors where being received.
In addition to fixing the above, I also added full support for the V3 protocol
error message syntax, I reversed the order of arguments in the PSQLException
constructor to more closely follow the constructors for SQLException, I changed
the new constructors that take PSQLState to take Object for additional
parameters as the old ones did.
Still todo are to add SQLState values to all existing exceptions thrown in the
driver and add support for parsing the V3 protocol format for notices.

 Modified Files:
  jdbc/build.xml jdbc/org/postgresql/Driver.java.in
  jdbc/org/postgresql/errors.properties
  jdbc/org/postgresql/core/Encoding.java
  jdbc/org/postgresql/core/PGStream.java
  jdbc/org/postgresql/core/QueryExecutor.java
  jdbc/org/postgresql/fastpath/Fastpath.java
  jdbc/org/postgresql/jdbc1/AbstractJdbc1Connection.java
  jdbc/org/postgresql/jdbc1/AbstractJdbc1ResultSet.java
  jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java
  jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java
  jdbc/org/postgresql/jdbc2/AbstractJdbc2Statement.java
  jdbc/org/postgresql/util/MessageTranslator.java
  jdbc/org/postgresql/util/PSQLException.java

src/interfaces/jdbc/org/postgresql/Driver.java.in
src/interfaces/jdbc/org/postgresql/errors.properties
src/interfaces/jdbc/org/postgresql/fastpath/Fastpath.java
src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Connection.java
src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1ResultSet.java
src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java
src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java
src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2Statement.java
src/interfaces/jdbc/org/postgresql/util/MessageTranslator.java
src/interfaces/jdbc/org/postgresql/util/PSQLException.java

index 4be5444b40a0e686f0c4213f8ceb5e550cc5095e..6571380f527116f4fdab7919000e4b63e03a4d7a 100644 (file)
@@ -17,6 +17,7 @@ import java.sql.*;
 import java.util.*;
 
 import org.postgresql.util.PSQLException;
+import org.postgresql.util.PSQLState;
 
 /*
  * The Java SQL framework allows for multiple database drivers.  Each
@@ -419,7 +420,7 @@ public class Driver implements java.sql.Driver
         */
        public static SQLException notImplemented()
        {
-               return new PSQLException("postgresql.unimplemented");
+               return new PSQLException("postgresql.unimplemented", PSQLState.NOT_IMPLEMENTED);
        }
 
        /**
index 6f5f2550ab3219940690d2c6fa6f7b0a7277abfd..9b8edc3a5f4049a8029abf22feabbe11ee25bc9f 100644 (file)
@@ -9,6 +9,8 @@ postgresql.con.invalidchar:Invalid character data was found.  This is most likel
 postgresql.con.closed:Connection is closed.  Operation is not permitted.
 postgresql.con.creobj:Failed to create object for {0} {1}
 postgresql.con.failed:The connection attempt failed because {0}
+postgresql.con.failed.bad.encoding:The connection attempt failed trying to get the server encoding
+postgresql.con.failed.bad.autocommit:The connection attempt failed trying to get the autocommit status
 postgresql.con.fathom:Unable to fathom update count {0}
 postgresql.con.garbled:Garbled data received.
 postgresql.con.ioerror:An IO erro occured while sending to the backend - {0}
@@ -29,6 +31,11 @@ postgresql.con.isolevel:Transaction isolation level {0} is not supported.
 postgresql.con.tuple:Tuple received before MetaData.
 postgresql.con.type:Unknown Response Type {0}
 postgresql.con.user:The user property is missing. It is mandatory.
+postgresql.error.detail:Detail: {0}
+postgresql.error.hint:Hint: {0}
+postgresql.error.position:Position: {0}
+postgresql.error.where:Where: {0}
+postgresql.error.location:Location: {0}
 postgresql.fp.error:FastPath call returned {0}
 postgresql.fp.expint:Fastpath call {0} - No result was returned and we expected an integer.
 postgresql.fp.protocol:FastPath protocol error: {0}
index 0d0c16052f122c8003751fe5963033725d502d3e..b12f777a0377dbfa15f3ae4967bc0dcf34fb993d 100644 (file)
@@ -20,6 +20,7 @@ import org.postgresql.Driver;
 import org.postgresql.core.BaseConnection;
 import org.postgresql.core.PGStream;
 import org.postgresql.util.PSQLException;
+import org.postgresql.util.PSQLState;
 
 /*
  * This class implements the Fastpath api.
@@ -100,14 +101,14 @@ public class Fastpath
                        }
                        catch (IOException ioe)
                        {
-                               throw new PSQLException("postgresql.fp.send", new Integer(fnid), ioe);
+                               throw new PSQLException("postgresql.fp.send", PSQLState.COMMUNICATION_ERROR, new Integer(fnid), ioe);
                        }
 
                        // Now handle the result
 
                        // Now loop, reading the results
                        Object result = null; // our result
-                       StringBuffer errorMessage = null;
+                       PSQLException error = null;
                        int c;
                        boolean l_endQuery = false;
                        while (!l_endQuery)
@@ -124,11 +125,16 @@ public class Fastpath
                                                //------------------------------
                                                // Error message returned
                                        case 'E':
-                                               if ( errorMessage == null )
-                                                       errorMessage = new StringBuffer();
-
                                                int l_elen = stream.ReceiveIntegerR(4);
-                                               errorMessage.append(conn.getEncoding().decode(stream.Receive(l_elen-4)));
+                                               String totalMessage = conn.getEncoding().decode(stream.Receive(l_elen-4));
+                                               PSQLException l_error = PSQLException.parseServerError(totalMessage);
+
+                                               if (error != null) {
+                                                       error.setNextException(l_error);
+                                               } else {
+                                                       error = l_error;
+                                               }
+
                                                break;
                                                //------------------------------
                                                // Notice from backend
@@ -165,7 +171,7 @@ public class Fastpath
 
                                        case 'Z':
                                                //TODO: use size better
-                                               if (stream.ReceiveIntegerR(4) != 5) throw new PSQLException("postgresql.con.setup"); 
+                                               if (stream.ReceiveIntegerR(4) != 5) throw new PSQLException("postgresql.con.setup", PSQLState.CONNECTION_UNABLE_TO_CONNECT); 
                                                //TODO: handle transaction status
                                                char l_tStatus = (char)stream.ReceiveChar();
                                                l_endQuery = true;
@@ -176,8 +182,8 @@ public class Fastpath
                                }
                        }
 
-                       if ( errorMessage != null )
-                               throw new PSQLException("postgresql.fp.error", errorMessage.toString());
+                       if ( error != null )
+                               throw error;
 
                        return result;
                }
@@ -208,7 +214,8 @@ public class Fastpath
                        }
                        catch (IOException ioe)
                        {
-                               throw new PSQLException("postgresql.fp.send", new Integer(fnid), ioe);
+                               //Should be sending exception as second arg.
+                               throw new PSQLException("postgresql.fp.send", PSQLState.COMMUNICATION_ERROR, new Integer(fnid), ioe);
                        }
 
                        // Now handle the result
index b91cf2f59f27f9d0c5a75ae0f91eb1a2dbb72ed3..76da3f147c49984c4ae80d33e1953edbe3cae4df 100644 (file)
@@ -34,6 +34,7 @@ import org.postgresql.largeobject.LargeObjectManager;
 import org.postgresql.util.MD5Digest;
 import org.postgresql.util.PGobject;
 import org.postgresql.util.PSQLException;
+import org.postgresql.util.PSQLState;
 import org.postgresql.util.UnixCrypt;
 
 public abstract class AbstractJdbc1Connection implements BaseConnection
@@ -126,7 +127,7 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                // This occasionally occurs when the client uses the properties version
                // of getConnection(), and is a common question on the email lists
                if (info.getProperty("user") == null)
-                       throw new PSQLException("postgresql.con.user");
+                       throw new PSQLException("postgresql.con.user", PSQLState.CONNECTION_REJECTED);
 
                this_driver = (Driver)d;
                this_url = url;
@@ -200,11 +201,11 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                        // Added by Peter Mount <peter@retep.org.uk>
                        // ConnectException is thrown when the connection cannot be made.
                        // we trap this an return a more meaningful message for the end user
-                       throw new PSQLException ("postgresql.con.refused");
+                       throw new PSQLException ("postgresql.con.refused", PSQLState.CONNECTION_REJECTED);
                }
                catch (IOException e)
                {
-                       throw new PSQLException ("postgresql.con.failed", e);
+                       throw new PSQLException ("postgresql.con.failed", PSQLState.CONNECTION_UNABLE_TO_CONNECT, e);
                }
 
            //Now do the protocol work
@@ -247,7 +248,7 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                                                // The most common one to be thrown here is:
                                                // "User authentication failed"
                                                //
-                                               throw new PSQLException("postgresql.con.misc", pgStream.ReceiveString(encoding));
+                                               throw new PSQLException("postgresql.con.misc", PSQLState.CONNECTION_REJECTED, pgStream.ReceiveString(encoding));
                                                
                                        case 'N':
                                                // Server does not support ssl
@@ -267,7 +268,7 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                }
                catch (IOException e)
                {
-                       throw new PSQLException("postgresql.con.failed", e);
+                       throw new PSQLException("postgresql.con.failed", PSQLState.CONNECTION_UNABLE_TO_CONNECT, e);
                }
 
 
@@ -313,16 +314,16 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                                                                // Added by Peter Mount <peter@retep.org.uk>
                                                                // ConnectException is thrown when the connection cannot be made.
                                                                // we trap this an return a more meaningful message for the end user
-                                                               throw new PSQLException ("postgresql.con.refused");
+                                                               throw new PSQLException ("postgresql.con.refused", PSQLState.CONNECTION_REJECTED);
                                                        }
                                                        catch (IOException e)
                                                        {
-                                                               throw new PSQLException ("postgresql.con.failed", e);
+                                                               throw new PSQLException ("postgresql.con.failed", PSQLState.CONNECTION_UNABLE_TO_CONNECT, e);
                                                        }
                                                        openConnectionV2(p_host, p_port, p_info, p_database, p_url, p_d, p_password);
                                                        return;
                                                }
-                                               throw new PSQLException("postgresql.con.misc",encoding.decode(pgStream.Receive(l_elen-4)));
+                                               throw new PSQLException("postgresql.con.misc", PSQLState.CONNECTION_REJECTED, PSQLException.parseServerError(encoding.decode(pgStream.Receive(l_elen-4))));
 
                                        case 'R':
                                                // Get the message length
@@ -362,12 +363,12 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                                                        case AUTH_REQ_KRB4:
                                                                if (Driver.logDebug)
                                                                        Driver.debug("postgresql: KRB4");
-                                                               throw new PSQLException("postgresql.con.kerb4");
+                                                               throw new PSQLException("postgresql.con.kerb4", PSQLState.CONNECTION_REJECTED);
 
                                                        case AUTH_REQ_KRB5:
                                                                if (Driver.logDebug)
                                                                        Driver.debug("postgresql: KRB5");
-                                                               throw new PSQLException("postgresql.con.kerb5");
+                                                               throw new PSQLException("postgresql.con.kerb5", PSQLState.CONNECTION_REJECTED);
 
                                                        case AUTH_REQ_SCM:
                                                                if (Driver.logDebug)
@@ -408,12 +409,12 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                                                                break;
 
                                                        default:
-                                                               throw new PSQLException("postgresql.con.auth", new Integer(areq));
+                                                               throw new PSQLException("postgresql.con.auth", PSQLState.CONNECTION_REJECTED, new Integer(areq));
                                                }
                                                break;
 
                                        default:
-                                               throw new PSQLException("postgresql.con.authfail");
+                                               throw new PSQLException("postgresql.con.authfail", PSQLState.CONNECTION_REJECTED);
                                }
                        }
                        while (areq != AUTH_REQ_OK);
@@ -421,7 +422,7 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                }
                catch (IOException e)
                {
-                       throw new PSQLException("postgresql.con.failed", e);
+                       throw new PSQLException("postgresql.con.failed", PSQLState.CONNECTION_UNABLE_TO_CONNECT, e);
                }
 
                int beresp;
@@ -435,13 +436,13 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                                        break;
                                case 'K':
                                        int l_msgLen = pgStream.ReceiveIntegerR(4);
-                                       if (l_msgLen != 12) throw new PSQLException("postgresql.con.setup");
+                                       if (l_msgLen != 12) throw new PSQLException("postgresql.con.setup", PSQLState.CONNECTION_UNABLE_TO_CONNECT);
                                        pid = pgStream.ReceiveIntegerR(4);
                                        ckey = pgStream.ReceiveIntegerR(4);
                                        break;
                                case 'E':
                                        int l_elen = pgStream.ReceiveIntegerR(4);
-                                       throw new PSQLException("postgresql.con.backend",encoding.decode(pgStream.Receive(l_elen-4)));
+                                       throw new PSQLException("postgresql.con.backend", PSQLState.CONNECTION_UNABLE_TO_CONNECT, PSQLException.parseServerError(encoding.decode(pgStream.Receive(l_elen-4))));
                                case 'N':
                                        int l_nlen = pgStream.ReceiveIntegerR(4);
                                        addWarning(encoding.decode(pgStream.Receive(l_nlen-4)));
@@ -456,12 +457,12 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                                default:
                                        if (Driver.logDebug)
                                                Driver.debug("invalid state="+ (char)beresp);
-                                       throw new PSQLException("postgresql.con.setup");
+                                       throw new PSQLException("postgresql.con.setup", PSQLState.CONNECTION_UNABLE_TO_CONNECT);
                        }
                }
                while (beresp != 'Z');
                // read ReadyForQuery
-               if (pgStream.ReceiveIntegerR(4) != 5) throw new PSQLException("postgresql.con.setup"); 
+               if (pgStream.ReceiveIntegerR(4) != 5) throw new PSQLException("postgresql.con.setup", PSQLState.CONNECTION_UNABLE_TO_CONNECT); 
                //TODO: handle transaction status
                char l_tStatus = (char)pgStream.ReceiveChar();
 
@@ -490,7 +491,7 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                
                if (! resultSet.next())
                {
-                       throw new PSQLException("postgresql.con.failed", "failed getting backend encoding");
+                       throw new PSQLException("postgresql.con.failed.bad.encoding", PSQLState.CONNECTION_UNABLE_TO_CONNECT);
                }
                String version = resultSet.getString(1);
                dbVersionNumber = extractVersionNumber(version);
@@ -554,7 +555,7 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                                                // The most common one to be thrown here is:
                                                // "User authentication failed"
                                                //
-                                               throw new PSQLException("postgresql.con.misc", pgStream.ReceiveString(encoding));
+                                               throw new PSQLException("postgresql.con.misc", PSQLState.CONNECTION_REJECTED, pgStream.ReceiveString(encoding));
                                                
                                        case 'N':
                                                // Server does not support ssl
@@ -574,7 +575,7 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                }
                catch (IOException e)
                {
-                       throw new PSQLException("postgresql.con.failed", e);
+                       throw new PSQLException("postgresql.con.failed", PSQLState.CONNECTION_UNABLE_TO_CONNECT, e);
                }
 
 
@@ -606,7 +607,7 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                                                // The most common one to be thrown here is:
                                                // "User authentication failed"
                                                //
-                                               throw new PSQLException("postgresql.con.misc", pgStream.ReceiveString(encoding));
+                                               throw new PSQLException("postgresql.con.misc", PSQLState.CONNECTION_REJECTED, pgStream.ReceiveString(encoding));
 
                                        case 'R':
                                                // Get the type of request
@@ -644,12 +645,12 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                                                        case AUTH_REQ_KRB4:
                                                                if (Driver.logDebug)
                                                                        Driver.debug("postgresql: KRB4");
-                                                               throw new PSQLException("postgresql.con.kerb4");
+                                                               throw new PSQLException("postgresql.con.kerb4", PSQLState.CONNECTION_REJECTED);
 
                                                        case AUTH_REQ_KRB5:
                                                                if (Driver.logDebug)
                                                                        Driver.debug("postgresql: KRB5");
-                                                               throw new PSQLException("postgresql.con.kerb5");
+                                                               throw new PSQLException("postgresql.con.kerb5", PSQLState.CONNECTION_REJECTED);
 
                                                        case AUTH_REQ_PASSWORD:
                                                                if (Driver.logDebug)
@@ -681,12 +682,12 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                                                                break;
 
                                                        default:
-                                                               throw new PSQLException("postgresql.con.auth", new Integer(areq));
+                                                               throw new PSQLException("postgresql.con.auth", PSQLState.CONNECTION_REJECTED, new Integer(areq));
                                                }
                                                break;
 
                                        default:
-                                               throw new PSQLException("postgresql.con.authfail");
+                                               throw new PSQLException("postgresql.con.authfail", PSQLState.CONNECTION_REJECTED);
                                }
                        }
                        while (areq != AUTH_REQ_OK);
@@ -694,7 +695,8 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                }
                catch (IOException e)
                {
-                       throw new PSQLException("postgresql.con.failed", e);
+                       //Should be passing exception as arg.
+                       throw new PSQLException("postgresql.con.failed", PSQLState.CONNECTION_UNABLE_TO_CONNECT, e);
                }
 
 
@@ -710,12 +712,12 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                                        ckey = pgStream.ReceiveIntegerR(4);
                                        break;
                                case 'E':
-                                       throw new PSQLException("postgresql.con.backend", pgStream.ReceiveString(encoding));
+                                       throw new PSQLException("postgresql.con.backend", PSQLState.CONNECTION_UNABLE_TO_CONNECT, pgStream.ReceiveString(encoding));
                                case 'N':
                                        addWarning(pgStream.ReceiveString(encoding));
                                        break;
                                default:
-                                       throw new PSQLException("postgresql.con.setup");
+                                       throw new PSQLException("postgresql.con.setup", PSQLState.CONNECTION_UNABLE_TO_CONNECT);
                        }
                }
                while (beresp == 'N');
@@ -732,9 +734,9 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                                        addWarning(pgStream.ReceiveString(encoding));
                                        break;
                                case 'E':
-                                       throw new PSQLException("postgresql.con.backend", pgStream.ReceiveString(encoding));
+                                       throw new PSQLException("postgresql.con.backend", PSQLState.CONNECTION_UNABLE_TO_CONNECT, pgStream.ReceiveString(encoding));
                                default:
-                                       throw new PSQLException("postgresql.con.setup");
+                                       throw new PSQLException("postgresql.con.setup", PSQLState.CONNECTION_UNABLE_TO_CONNECT);
                        }
                }
                while (beresp == 'N');
@@ -763,7 +765,7 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                
                if (! resultSet.next())
                {
-                       throw new PSQLException("postgresql.con.failed", "failed getting backend encoding");
+                       throw new PSQLException("postgresql.con.failed.bad.encoding", PSQLState.CONNECTION_UNABLE_TO_CONNECT);
                }
                String version = resultSet.getString(1);
                dbVersionNumber = extractVersionNumber(version);
@@ -799,7 +801,7 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
 
                        if (!acRset.next())
                        {
-                               throw new PSQLException("postgresql.con.failed", "failed getting autocommit status");
+                               throw new PSQLException("postgresql.con.failed.bad.autocommit", PSQLState.CONNECTION_UNABLE_TO_CONNECT);
                        }
                        //if autocommit is currently off we need to turn it on
                        //note that we will be in a transaction because the select above
@@ -1036,7 +1038,7 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                }
                catch (Exception ex)
                {
-                       throw new PSQLException("postgresql.con.creobj", type, ex);
+                       throw new PSQLException("postgresql.con.creobj", PSQLState.CONNECTION_FAILURE, type, ex);
                }
 
                // should never be reached
@@ -1409,7 +1411,7 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                                        isolationLevelSQL += "SERIALIZABLE";
                                        break;
                                default:
-                                       throw new PSQLException("postgresql.con.isolevel",
+                                       throw new PSQLException("postgresql.con.isolevel", PSQLState.TRANSACTION_STATE_INVALID,
                                                                                        new Integer(isolationLevel));
                        }
                }
@@ -1446,7 +1448,7 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                                break;
 
                        default:
-                               throw new PSQLException("postgresql.con.isolevel", new Integer(isolationLevel));
+                               throw new PSQLException("postgresql.con.isolevel", PSQLState.TRANSACTION_STATE_INVALID, new Integer(isolationLevel));
                }
                return sb.toString();
        }
@@ -1785,11 +1787,11 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                        // Added by Peter Mount <peter@retep.org.uk>
                        // ConnectException is thrown when the connection cannot be made.
                        // we trap this an return a more meaningful message for the end user
-                       throw new PSQLException ("postgresql.con.refused");
+                       throw new PSQLException ("postgresql.con.refused", PSQLState.CONNECTION_REJECTED);
                }
                catch (IOException e)
                {
-                       throw new PSQLException ("postgresql.con.failed", e);
+                       throw new PSQLException ("postgresql.con.failed", PSQLState.CONNECTION_UNABLE_TO_CONNECT, e);
                }
 
                // Now we need to construct and send a cancel packet
@@ -1803,7 +1805,7 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                }
                catch (IOException e)
                {
-                       throw new PSQLException("postgresql.con.failed", e);
+                       throw new PSQLException("postgresql.con.failed", PSQLState.CONNECTION_UNABLE_TO_CONNECT, e);
                }
                finally
                {
index 6122e1689088b7e3a91da45a62c29ed6955adfe8..7a587379a3411643a2a44411d435bbef81701a88 100644 (file)
@@ -32,6 +32,7 @@ import org.postgresql.largeobject.*;
 import org.postgresql.util.PGbytea;
 import org.postgresql.util.PGtokenizer;
 import org.postgresql.util.PSQLException;
+import org.postgresql.util.PSQLState;
 
 public abstract class AbstractJdbc1ResultSet implements BaseResultSet
 {
@@ -115,7 +116,7 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet
        public boolean next() throws SQLException
        {
                if (rows == null)
-                       throw new PSQLException("postgresql.con.closed");
+                       throw new PSQLException("postgresql.con.closed", PSQLState.CONNECTION_DOES_NOT_EXIST);
 
                if (++current_row >= rows.size())
                {
@@ -1139,7 +1140,7 @@ public abstract class AbstractJdbc1ResultSet implements BaseResultSet
                        }
                        catch (ParseException e)
                        {
-                               throw new PSQLException("postgresql.res.badtimestamp", new Integer(e.getErrorOffset()), s);
+                               throw new PSQLException("postgresql.res.badtimestamp", PSQLState.UNKNOWN_STATE, new Integer(e.getErrorOffset()), s);
                        }
                }
        }
index adfb400b93c2209381ca5534191df610f785f7be..561d523ec8ff812d1cf2cf841c35f3fa3a0d8e33 100644 (file)
@@ -10,6 +10,7 @@ import org.postgresql.largeobject.LargeObjectManager;
 import org.postgresql.util.PGbytea;
 import org.postgresql.util.PGobject;
 import org.postgresql.util.PSQLException;
+import org.postgresql.util.PSQLState;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -676,7 +677,7 @@ public abstract class AbstractJdbc1Statement implements BaseStatement
         */
        public void cancel() throws SQLException
        {
-               throw new PSQLException("postgresql.unimplemented");
+               throw new PSQLException("postgresql.unimplemented", PSQLState.NOT_IMPLEMENTED);
        }
 
        /*
@@ -1558,7 +1559,7 @@ public abstract class AbstractJdbc1Statement implements BaseStatement
                                }
                                else
                                {
-                                       throw new PSQLException("postgresql.prep.type");
+                                       throw new PSQLException("postgresql.prep.type", PSQLState.INVALID_PARAMETER_TYPE);
                                }
                                break;
                        case Types.BINARY:
@@ -1570,7 +1571,7 @@ public abstract class AbstractJdbc1Statement implements BaseStatement
                                setString(parameterIndex, ((PGobject)x).getValue(), PG_TEXT);
                                break;
                        default:
-                               throw new PSQLException("postgresql.prep.type");
+                               throw new PSQLException("postgresql.prep.type", PSQLState.INVALID_PARAMETER_TYPE);
                }
        }
 
@@ -1954,7 +1955,7 @@ public abstract class AbstractJdbc1Statement implements BaseStatement
        private void bind(int paramIndex, Object s, String type) throws SQLException
        {
                if (paramIndex < 1 || paramIndex > m_binds.length)
-                       throw new PSQLException("postgresql.prep.range");
+                       throw new PSQLException("postgresql.prep.range", PSQLState.PARAMETER_ERROR);
                if (paramIndex == 1 && isFunction) // need to registerOut instead
                        throw new PSQLException ("postgresql.call.funcover");
                m_binds[paramIndex - 1] = s;
@@ -2096,7 +2097,7 @@ public abstract class AbstractJdbc1Statement implements BaseStatement
                }
                catch (Exception e)
                {
-                       throw new PSQLException("postgresql.format.baddate",s , "yyyy-MM-dd[-tz]");
+                       throw new PSQLException("postgresql.format.baddate", PSQLState.UNKNOWN_STATE, s , "yyyy-MM-dd[-tz]");
                }
                timezone = 0;
                if (timezoneLocation>7 && timezoneLocation+3 == s.length())
@@ -2127,7 +2128,7 @@ public abstract class AbstractJdbc1Statement implements BaseStatement
                }
                catch (Exception e)
                {
-                       throw new PSQLException("postgresql.format.badtime",s, "HH:mm:ss[-tz]");
+                       throw new PSQLException("postgresql.format.badtime", PSQLState.UNKNOWN_STATE, s, "HH:mm:ss[-tz]");
                }
                timezone = 0;
                if (timezoneLocation != -1 && timezoneLocation+3 == s.length())
@@ -2166,7 +2167,7 @@ public abstract class AbstractJdbc1Statement implements BaseStatement
                }
                catch (Exception e)
                {
-                       throw new PSQLException("postgresql.format.badtimestamp", s, "yyyy-MM-dd HH:mm:ss[.xxxxxx][-tz]");
+                       throw new PSQLException("postgresql.format.badtimestamp", PSQLState.UNKNOWN_STATE, s, "yyyy-MM-dd HH:mm:ss[.xxxxxx][-tz]");
                }
                timezone = 0;
                if (nanospos != -1)
index 5692ba0c0ce601b8418dcbd0fb9f8e8e2442b099..2f77b0a1bcb51d8e86db70988958f0ffbb37c281 100644 (file)
@@ -30,6 +30,7 @@ import org.postgresql.core.BaseStatement;
 import org.postgresql.core.Field;
 import org.postgresql.core.Encoding;
 import org.postgresql.util.PSQLException;
+import org.postgresql.util.PSQLState;
 
 
 public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.AbstractJdbc1ResultSet
@@ -421,7 +422,7 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
        public Ref getRef(int i) throws SQLException
        {
                //The backend doesn't yet have SQL3 REF types
-               throw new PSQLException("postgresql.psqlnotimp");
+               throw new PSQLException("postgresql.psqlnotimp", PSQLState.NOT_IMPLEMENTED);
        }
 
 
@@ -513,7 +514,7 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
 
        public void setFetchDirection(int direction) throws SQLException
        {
-               throw new PSQLException("postgresql.psqlnotimp");
+               throw new PSQLException("postgresql.psqlnotimp", PSQLState.NOT_IMPLEMENTED);
        }
 
 
index ef07f0f5fd1482d73d362dc12365d2839315d4be..03724367f3a000cf43b40168f330792f81d906d0 100644 (file)
@@ -8,6 +8,7 @@ import java.util.Vector;
 import org.postgresql.Driver;
 import org.postgresql.largeobject.*;
 import org.postgresql.util.PSQLException;
+import org.postgresql.util.PSQLState;
 
 /* $Header$
  * This class defines methods of the jdbc2 specification.  This class extends
@@ -129,7 +130,7 @@ public abstract class AbstractJdbc2Statement extends org.postgresql.jdbc1.Abstra
 
        public int getFetchDirection() throws SQLException
        {
-               throw new PSQLException("postgresql.psqlnotimp");
+               throw new PSQLException("postgresql.psqlnotimp", PSQLState.NOT_IMPLEMENTED);
        }
 
        public int getResultSetConcurrency() throws SQLException
index 6dd877d0b0379205ba15c12d9650085b1e53ae22..bb166d3ee8d344a50b049ea6c865b0f2e9049ba6 100644 (file)
@@ -56,6 +56,14 @@ public class MessageTranslator
                return translator._translate(id, args);
        }
 
+       public final static String translate(String id, Object arg)
+       {
+               MessageTranslator translator = MessageTranslator.getInstance();
+               Object[] args = new Object[1];
+               args[0] = arg;
+               return translator._translate(id, args);
+       }
+
        private final String _translate(String id, Object[] args)
        {
                String message;
index 73c5f75210c357b19f8000a0ff1c0e17468f5138..22394ed2de4023e043fa2dac37801a99ba540f40 100644 (file)
@@ -16,100 +16,194 @@ package org.postgresql.util;
 import java.io.ByteArrayOutputStream;
 import java.io.PrintWriter;
 import java.sql.SQLException;
+import java.util.Hashtable;
 import org.postgresql.Driver;
 
 public class PSQLException extends SQLException
 {
        private String message;
+       private PSQLState state;
 
-       /*
-        * This provides the same functionality to SQLException
-        * @param error Error string
-        */
-       public PSQLException(String error)
+       //-------start new constructors-------
+       
+       public PSQLException(String msg, PSQLState state)
        {
-               super();
-               translate(error, null);
+               this.state = state;
+               translate(msg, null);
                if (Driver.logDebug)
-                       Driver.debug("Exception: " + this);
+                       Driver.debug("Exception: " + this);             
        }
-
-       /*
-        * A more generic entry point.
-        * @param error Error string or standard message id
-        * @param args Array of arguments
-        */
-       public PSQLException(String error, Object[] args)
+       
+       public PSQLException(String msg, PSQLState state, Object[] argv)
        {
-               super();
-               translate(error, args);
+               this.state = state;
+               translate(msg, argv);
                if (Driver.logDebug)
                        Driver.debug("Exception: " + this);
        }
 
-       /*
-        * Helper version for 1 arg
-        */
-       public PSQLException(String error, Object arg)
+       //Helper version for one arg
+       public PSQLException(String msg, PSQLState state, Object arg1)
        {
-               super();
+               this.state = state;
                Object[] argv = new Object[1];
-               argv[0] = arg;
-               translate(error, argv);
+               argv[0] = arg1;
+               translate(msg, argv);
                if (Driver.logDebug)
                        Driver.debug("Exception: " + this);
        }
+       
+       //Helper version for two args
+       public PSQLException(String msg, PSQLState state, Object arg1, Object arg2)
+       {
+               this.state = state;
+               Object[] argv = new Object[2];
+               argv[0] = arg1;
+               argv[1] = arg2;
+               translate(msg, argv);
+               if (Driver.logDebug)
+                       Driver.debug("Exception: " + this);
+       }
+       
+       //-------end new constructors-------
 
-       /*
-        * Helper version for 1 arg. This is used for debug purposes only with
-        * some unusual Exception's. It allows the originiating Exceptions stack
-        * trace to be returned.
-        */
-       public PSQLException(String error, Exception ex)
+       public static PSQLException parseServerError(String p_serverError) 
        {
-               super();
+               if (Driver.logDebug)
+                       Driver.debug("Constructing exception from server message: " + p_serverError);
+               char[] l_chars = p_serverError.toCharArray();
+               int l_pos = 0;
+               int l_length = l_chars.length;
+               Hashtable l_mesgParts = new Hashtable();
+               while (l_pos < l_length) {
+                       char l_mesgType = l_chars[l_pos];
+                       if (l_mesgType != '\0') {
+                               l_pos++;
+                               int l_startString = l_pos;
+                               while (l_chars[l_pos] != '\0' && l_pos < l_length) {
+                                       l_pos++;
+                               }
+                               String l_mesgPart = new String(l_chars, l_startString, l_pos - l_startString);
+                               l_mesgParts.put(new Character(l_mesgType),l_mesgPart);
+                       }
+                       l_pos++;
+               }
 
-               Object[] argv = new Object[1];
+        //Now construct the message from what the server sent
+               //The general format is:
+               //SEVERITY: Message \n
+               //  Detail: \n
+               //  Hint: \n
+               //  Position: \n
+               //  Where: \n
+               //  Location: File:Line:Routine \n
+               //  SQLState: \n
+               //
+               //Normally only the message and detail is included.
+               //If INFO level logging is enabled then detail, hint, position and where are
+               //included.  If DEBUG level logging is enabled then all information 
+               //is included.
 
-               try
-               {
-                       ByteArrayOutputStream baos = new ByteArrayOutputStream();
-                       PrintWriter pw = new PrintWriter(baos);
-                       pw.println("Exception: " + ex.toString() + "\nStack Trace:\n");
-                       ex.printStackTrace(pw);
-                       pw.println("End of Stack Trace");
-                       pw.flush();
-                       argv[0] = baos.toString();
-                       pw.close();
-                       baos.close();
-               }
-               catch (Exception ioe)
-               {
-                       argv[0] = ex.toString() + "\nIO Error on stack trace generation! " + ioe.toString();
+               StringBuffer l_totalMessage = new StringBuffer();
+               String l_message = (String)l_mesgParts.get(MESSAGE_TYPE_S);
+               if (l_message != null) 
+                       l_totalMessage.append(l_message).append(": ");
+               l_message = (String)l_mesgParts.get(MESSAGE_TYPE_M);
+               if (l_message != null)
+                       l_totalMessage.append(l_message).append('\n');
+               if (Driver.logInfo) {
+                       l_message = (String)l_mesgParts.get(MESSAGE_TYPE_D);
+                       if (l_message != null)
+                               l_totalMessage.append("  ").append(MessageTranslator.translate("postgresql.error.detail", l_message)).append('\n');
+                       l_message = (String)l_mesgParts.get(MESSAGE_TYPE_H);
+                       if (l_message != null)
+                               l_totalMessage.append("  ").append(MessageTranslator.translate("postgresql.error.hint", l_message)).append('\n');
+                       l_message = (String)l_mesgParts.get(MESSAGE_TYPE_P);
+                       if (l_message != null)
+                               l_totalMessage.append("  ").append(MessageTranslator.translate("postgresql.error.position", l_message)).append('\n');
+                       l_message = (String)l_mesgParts.get(MESSAGE_TYPE_W);
+                       if (l_message != null)
+                               l_totalMessage.append("  ").append(MessageTranslator.translate("postgresql.error.where", l_message)).append('\n');
+           }
+               if (Driver.logDebug) {
+                       String l_file = (String)l_mesgParts.get(MESSAGE_TYPE_F);
+                       String l_line = (String)l_mesgParts.get(MESSAGE_TYPE_L);
+                       String l_routine = (String)l_mesgParts.get(MESSAGE_TYPE_R);
+                       if (l_file != null || l_line != null || l_routine != null)
+                               l_totalMessage.append("  ").append(MessageTranslator.translate("postgresql.error.location", l_file+":"+l_line+":"+l_routine)).append('\n');
+                       l_message = (String)l_mesgParts.get(MESSAGE_TYPE_C);
+                       if (l_message != null)
+                               l_totalMessage.append("  ").append("ServerSQLState: " + l_message).append('\n');
                }
 
-               translate(error, argv);
+               PSQLException l_return = new PSQLException(l_totalMessage.toString(), PSQLState.UNKNOWN_STATE);
+               l_return.state = new PSQLState((String)l_mesgParts.get(MESSAGE_TYPE_C));
+               return l_return;
+       }
+       
+       private static final Character MESSAGE_TYPE_S = new Character('S');
+       private static final Character MESSAGE_TYPE_M = new Character('M');
+       private static final Character MESSAGE_TYPE_D = new Character('D');
+       private static final Character MESSAGE_TYPE_H = new Character('H');
+       private static final Character MESSAGE_TYPE_P = new Character('P');
+       private static final Character MESSAGE_TYPE_W = new Character('W');
+       private static final Character MESSAGE_TYPE_F = new Character('F');
+       private static final Character MESSAGE_TYPE_L = new Character('L');
+       private static final Character MESSAGE_TYPE_R = new Character('R');
+       private static final Character MESSAGE_TYPE_C = new Character('C');
+
+       /*
+        * This provides the same functionality to SQLException
+        * @param error Error string
+        */
+       public PSQLException(String error)
+       {
+               translate(error, null);
                if (Driver.logDebug)
                        Driver.debug("Exception: " + this);
        }
 
        /*
-        * Helper version for 2 args
+        * Helper version for 1 arg
         */
-       public PSQLException(String error, Object arg1, Object arg2)
+       public PSQLException(String error, Object arg)
        {
-               super();
-               Object[] argv = new Object[2];
-               argv[0] = arg1;
-               argv[1] = arg2;
+               Object[] argv = new Object[1];
+               argv[0] = arg;
                translate(error, argv);
                if (Driver.logDebug)
                        Driver.debug("Exception: " + this);
        }
 
-       private void translate(String error, Object[] args)
-       {
+
+    private void translate(String error, Object[] args) {
+       //We convert exception objects to Strings that 
+               //contain the full stack trace 
+               if (args != null) {
+                       for (int i = 0; i < args.length; i++) {
+                               if (args[i] instanceof Exception && !(args[i] instanceof PSQLException)) {
+                                       Exception ex = (Exception) args[i];
+                                       try {
+                                               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                                               PrintWriter pw = new PrintWriter(baos);
+                                               pw.println("Exception: " + ex.toString() + "\nStack Trace:\n");
+                                               ex.printStackTrace(pw);
+                                               pw.println("End of Stack Trace");
+                                               pw.flush();
+                                               args[i] = baos.toString();
+                                               pw.close();
+                                               baos.close();
+                                       }
+                                       catch (Exception ioe)
+                                       {
+                                               args[i] = ex.toString() + "\nIO Error on stack trace generation! " + ioe.toString();
+                                       }
+                               }
+                       }
+               }
+
                message = MessageTranslator.translate(error, args);
+
        }
 
        /*
@@ -127,4 +221,9 @@ public class PSQLException extends SQLException
        {
                return message;
        }
+       
+       public String getSQLState()
+       {
+               return state.getState();
+       }
 }