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
* Copyright (c) 2003, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/Attic/Driver.java.in,v 1.34 2003/07/24 00:30:38 barry Exp $
+ * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/Attic/Driver.java.in,v 1.35 2003/09/08 17:30:22 barry Exp $
*
*-------------------------------------------------------------------------
*/
import java.util.*;
import org.postgresql.util.PSQLException;
+import org.postgresql.util.PSQLState;
/*
* The Java SQL framework allows for multiple database drivers. Each
*/
public static SQLException notImplemented()
{
- return new PSQLException("postgresql.unimplemented");
+ return new PSQLException("postgresql.unimplemented", PSQLState.NOT_IMPLEMENTED);
}
/**
* Copyright (c) 2003, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/core/Attic/Encoding.java,v 1.11 2003/05/29 03:21:32 barry Exp $
+ * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/core/Attic/Encoding.java,v 1.12 2003/09/08 17:30:22 barry Exp $
*
*-------------------------------------------------------------------------
*/
import java.sql.SQLException;
import java.util.Hashtable;
import org.postgresql.util.PSQLException;
+import org.postgresql.util.PSQLState;
public class Encoding
{
}
catch (UnsupportedEncodingException e)
{
- throw new PSQLException("postgresql.stream.encoding", e);
+ throw new PSQLException("postgresql.stream.encoding", PSQLState.DATA_ERROR, e);
}
}
}
catch (UnsupportedEncodingException e)
{
- throw new PSQLException("postgresql.stream.encoding", e);
+ throw new PSQLException("postgresql.stream.encoding", PSQLState.DATA_ERROR, e);
}
}
}
catch (UnsupportedEncodingException e)
{
- throw new PSQLException("postgresql.res.encoding", e);
+ throw new PSQLException("postgresql.res.encoding", PSQLState.DATA_ERROR, e);
}
}
* Copyright (c) 2003, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/core/Attic/PGStream.java,v 1.2 2003/05/29 03:21:32 barry Exp $
+ * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/core/Attic/PGStream.java,v 1.3 2003/09/08 17:30:22 barry Exp $
*
*-------------------------------------------------------------------------
*/
import java.net.Socket;
import java.sql.*;
import org.postgresql.util.PSQLException;
+import org.postgresql.util.PSQLState;
public class PGStream
{
c = pg_input.read();
if (c < 0)
- throw new PSQLException("postgresql.stream.eof");
+ throw new PSQLException("postgresql.stream.eof", PSQLState.COMMUNICATION_ERROR);
}
catch (IOException e)
{
- throw new PSQLException("postgresql.stream.ioerror", e);
+ throw new PSQLException("postgresql.stream.ioerror", PSQLState.COMMUNICATION_ERROR, e);
}
return c;
}
int b = pg_input.read();
if (b < 0)
- throw new PSQLException("postgresql.stream.eof");
+ throw new PSQLException("postgresql.stream.eof", PSQLState.COMMUNICATION_ERROR);
n = n | (b << (8 * i)) ;
}
}
catch (IOException e)
{
- throw new PSQLException("postgresql.stream.ioerror", e);
+ throw new PSQLException("postgresql.stream.ioerror", PSQLState.COMMUNICATION_ERROR, e);
}
return n;
}
int b = pg_input.read();
if (b < 0)
- throw new PSQLException("postgresql.stream.eof");
+ throw new PSQLException("postgresql.stream.eof", PSQLState.COMMUNICATION_ERROR);
n = b | (n << 8);
}
}
catch (IOException e)
{
- throw new PSQLException("postgresql.stream.ioerror", e);
+ throw new PSQLException("postgresql.stream.ioerror", PSQLState.COMMUNICATION_ERROR, e);
}
return n;
}
{
int c = pg_input.read();
if (c < 0)
- throw new PSQLException("postgresql.stream.eof");
+ throw new PSQLException("postgresql.stream.eof", PSQLState.COMMUNICATION_ERROR);
else if (c == 0)
{
rst[s] = 0;
}
catch (IOException e)
{
- throw new PSQLException("postgresql.stream.ioerror", e);
+ throw new PSQLException("postgresql.stream.ioerror", PSQLState.COMMUNICATION_ERROR, e);
}
return encoding.decode(rst, 0, s);
}
{
int w = pg_input.read(b, off + s, siz - s);
if (w < 0)
- throw new PSQLException("postgresql.stream.eof");
+ throw new PSQLException("postgresql.stream.eof", PSQLState.COMMUNICATION_ERROR);
s += w;
}
}
catch (IOException e)
{
- throw new PSQLException("postgresql.stream.ioerror", e);
+ throw new PSQLException("postgresql.stream.ioerror", PSQLState.COMMUNICATION_ERROR, e);
}
}
}
catch (IOException e)
{
- throw new PSQLException("postgresql.stream.flush", e);
+ throw new PSQLException("postgresql.stream.flush", PSQLState.COMMUNICATION_ERROR, e);
}
}
* Copyright (c) 2003, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/core/Attic/QueryExecutor.java,v 1.23 2003/08/11 21:18:47 barry Exp $
+ * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/core/Attic/QueryExecutor.java,v 1.24 2003/09/08 17:30:22 barry Exp $
*
*-------------------------------------------------------------------------
*/
import java.sql.*;
import org.postgresql.Driver;
import org.postgresql.util.PSQLException;
+import org.postgresql.util.PSQLState;
public class QueryExecutor
{
private BaseResultSet executeV3() throws SQLException
{
- StringBuffer errorMessage = null;
+ PSQLException error = null;
if (pgStream == null)
{
- throw new PSQLException("postgresql.con.closed");
+ throw new PSQLException("postgresql.con.closed", PSQLState.CONNECTION_DOES_NOT_EXIST);
}
synchronized (pgStream)
// so, append messages to a string buffer and keep processing
// check at the bottom to see if we need to throw an exception
- if ( errorMessage == null )
- errorMessage = new StringBuffer();
+ int l_elen = pgStream.ReceiveIntegerR(4);
+ String totalMessage = connection.getEncoding().decode(pgStream.Receive(l_elen-4));
+ PSQLException l_error = PSQLException.parseServerError(totalMessage);
+
+ if (error != null) {
+ error.setNextException(l_error);
+ } else {
+ error = l_error;
+ }
- int l_elen = pgStream.ReceiveIntegerR(4);
- errorMessage.append(connection.getEncoding().decode(pgStream.Receive(l_elen-4)));
// keep processing
break;
case 'I': // Empty Query
case 'Z':
// read ReadyForQuery
//TODO: use size better
- 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();
l_endQuery = true;
break;
default:
- throw new PSQLException("postgresql.con.type",
- new Character((char) c));
+ throw new PSQLException("postgresql.con.type", PSQLState.CONNECTION_FAILURE, new Character((char) c));
}
}
// did we get an error during this query?
- if ( errorMessage != null )
- throw new SQLException( errorMessage.toString().trim() );
-
+ if ( error != null )
+ throw error;
//if an existing result set was passed in reuse it, else
//create a new one
if (pgStream == null)
{
- throw new PSQLException("postgresql.con.closed");
+ throw new PSQLException("postgresql.con.closed", PSQLState.CONNECTION_DOES_NOT_EXIST);
}
synchronized (pgStream)
l_endQuery = true;
break;
default:
- throw new PSQLException("postgresql.con.type",
- new Character((char) c));
+ throw new PSQLException("postgresql.con.type", PSQLState.CONNECTION_FAILURE, new Character((char) c));
}
}
for ( int i = 0; i < m_binds.length ; i++ )
{
if ( m_binds[i] == null )
- throw new PSQLException("postgresql.prep.param", new Integer(i + 1));
+ throw new PSQLException("postgresql.prep.param", PSQLState.PARAMETER_ERROR, new Integer(i + 1));
}
try
{
for ( int i = 0; i < m_binds.length ; i++ )
{
if ( m_binds[i] == null )
- throw new PSQLException("postgresql.prep.param", new Integer(i + 1));
+ throw new PSQLException("postgresql.prep.param", PSQLState.PARAMETER_ERROR, new Integer(i + 1));
}
try
{
private void receiveTupleV3(boolean isBinary) throws SQLException
{
if (fields == null)
- throw new PSQLException("postgresql.con.tuple");
+ throw new PSQLException("postgresql.con.tuple", PSQLState.CONNECTION_FAILURE);
Object tuple = pgStream.ReceiveTupleV3(fields.length, isBinary);
if (isBinary)
binaryCursor = true;
private void receiveTupleV2(boolean isBinary) throws SQLException
{
if (fields == null)
- throw new PSQLException("postgresql.con.tuple");
+ throw new PSQLException("postgresql.con.tuple", PSQLState.CONNECTION_FAILURE);
Object tuple = pgStream.ReceiveTupleV2(fields.length, isBinary);
if (isBinary)
binaryCursor = true;
}
catch (NumberFormatException nfe)
{
- throw new PSQLException("postgresql.con.fathom", status);
+ throw new PSQLException("postgresql.con.fathom", PSQLState.CONNECTION_FAILURE, status);
}
}
/*
}
catch (NumberFormatException nfe)
{
- throw new PSQLException("postgresql.con.fathom", status);
+ throw new PSQLException("postgresql.con.fathom", PSQLState.CONNECTION_FAILURE, status);
}
}
//TODO: use the msgSize
//TODO: use the tableOid, and tablePosition
if (fields != null)
- throw new PSQLException("postgresql.con.multres");
+ throw new PSQLException("postgresql.con.multres", PSQLState.CONNECTION_FAILURE);
int l_msgSize = pgStream.ReceiveIntegerR(4);
int size = pgStream.ReceiveIntegerR(2);
fields = new Field[size];
private void receiveFieldsV2() throws SQLException
{
if (fields != null)
- throw new PSQLException("postgresql.con.multres");
+ throw new PSQLException("postgresql.con.multres", PSQLState.CONNECTION_FAILURE);
int size = pgStream.ReceiveIntegerR(2);
fields = new Field[size];
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}
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}
* Copyright (c) 2003, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/fastpath/Attic/Fastpath.java,v 1.14 2003/05/29 04:39:51 barry Exp $
+ * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/fastpath/Attic/Fastpath.java,v 1.15 2003/09/08 17:30:22 barry Exp $
*
*-------------------------------------------------------------------------
*/
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.
}
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)
//------------------------------
// 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
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;
}
}
- if ( errorMessage != null )
- throw new PSQLException("postgresql.fp.error", errorMessage.toString());
+ if ( error != null )
+ throw error;
return result;
}
}
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
* Copyright (c) 2003, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Connection.java,v 1.23 2003/08/15 18:36:58 barry Exp $
+ * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Connection.java,v 1.24 2003/09/08 17:30:22 barry Exp $
*
*-------------------------------------------------------------------------
*/
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
// 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;
// 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
// 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
}
catch (IOException e)
{
- throw new PSQLException("postgresql.con.failed", e);
+ throw new PSQLException("postgresql.con.failed", PSQLState.CONNECTION_UNABLE_TO_CONNECT, e);
}
// 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
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)
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);
}
catch (IOException e)
{
- throw new PSQLException("postgresql.con.failed", e);
+ throw new PSQLException("postgresql.con.failed", PSQLState.CONNECTION_UNABLE_TO_CONNECT, e);
}
int beresp;
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)));
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();
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);
// 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
}
catch (IOException e)
{
- throw new PSQLException("postgresql.con.failed", e);
+ throw new PSQLException("postgresql.con.failed", PSQLState.CONNECTION_UNABLE_TO_CONNECT, e);
}
// 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
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)
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);
}
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);
}
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');
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');
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);
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
}
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
isolationLevelSQL += "SERIALIZABLE";
break;
default:
- throw new PSQLException("postgresql.con.isolevel",
+ throw new PSQLException("postgresql.con.isolevel", PSQLState.TRANSACTION_STATE_INVALID,
new Integer(isolationLevel));
}
}
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();
}
// 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
}
catch (IOException e)
{
- throw new PSQLException("postgresql.con.failed", e);
+ throw new PSQLException("postgresql.con.failed", PSQLState.CONNECTION_UNABLE_TO_CONNECT, e);
}
finally
{
* Copyright (c) 2003, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1ResultSet.java,v 1.15 2003/08/24 22:10:09 barry Exp $
+ * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1ResultSet.java,v 1.16 2003/09/08 17:30:22 barry Exp $
*
*-------------------------------------------------------------------------
*/
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
{
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())
{
}
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);
}
}
}
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;
import java.sql.Types;
import java.util.Vector;
-/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.33 2003/08/26 06:50:39 barry Exp $
+/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.34 2003/09/08 17:30:22 barry Exp $
* This class defines methods of the jdbc1 specification. This class is
* extended by org.postgresql.jdbc2.AbstractJdbc2Statement which adds the jdbc2
* methods. The real Statement class (for jdbc1) is org.postgresql.jdbc1.Jdbc1Statement
*/
public void cancel() throws SQLException
{
- throw new PSQLException("postgresql.unimplemented");
+ throw new PSQLException("postgresql.unimplemented", PSQLState.NOT_IMPLEMENTED);
}
/*
}
else
{
- throw new PSQLException("postgresql.prep.type");
+ throw new PSQLException("postgresql.prep.type", PSQLState.INVALID_PARAMETER_TYPE);
}
break;
case Types.BINARY:
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);
}
}
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;
}
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())
}
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())
}
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)
* Copyright (c) 2003, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/AbstractJdbc2ResultSet.java,v 1.21 2003/08/11 21:33:50 barry Exp $
+ * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/AbstractJdbc2ResultSet.java,v 1.22 2003/09/08 17:30:22 barry Exp $
*
*-------------------------------------------------------------------------
*/
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
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);
}
public void setFetchDirection(int direction) throws SQLException
{
- throw new PSQLException("postgresql.psqlnotimp");
+ throw new PSQLException("postgresql.psqlnotimp", PSQLState.NOT_IMPLEMENTED);
}
import org.postgresql.Driver;
import org.postgresql.largeobject.*;
import org.postgresql.util.PSQLException;
+import org.postgresql.util.PSQLState;
-/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/AbstractJdbc2Statement.java,v 1.15 2003/06/30 16:38:30 barry Exp $
+/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/AbstractJdbc2Statement.java,v 1.16 2003/09/08 17:30:22 barry Exp $
* This class defines methods of the jdbc2 specification. This class extends
* org.postgresql.jdbc1.AbstractJdbc1Statement which provides the jdbc1
* methods. The real Statement class (for jdbc2) is org.postgresql.jdbc2.Jdbc2Statement
public int getFetchDirection() throws SQLException
{
- throw new PSQLException("postgresql.psqlnotimp");
+ throw new PSQLException("postgresql.psqlnotimp", PSQLState.NOT_IMPLEMENTED);
}
public int getResultSetConcurrency() throws SQLException
* Copyright (c) 2003, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/util/Attic/MessageTranslator.java,v 1.4 2003/03/07 18:39:46 barry Exp $
+ * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/util/Attic/MessageTranslator.java,v 1.5 2003/09/08 17:30:22 barry Exp $
*
*-------------------------------------------------------------------------
*/
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;
* Copyright (c) 2003, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/util/Attic/PSQLException.java,v 1.12 2003/08/11 21:18:47 barry Exp $
+ * $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/util/Attic/PSQLException.java,v 1.13 2003/09/08 17:30:22 barry Exp $
*
*-------------------------------------------------------------------------
*/
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);
+
}
/*
{
return message;
}
+
+ public String getSQLState()
+ {
+ return state.getState();
+ }
}