JDBC More SQLStatus for connections - Mailing list pgsql-patches

From Fernando Nasser
Subject JDBC More SQLStatus for connections
Date
Msg-id 3DDE84A4.3000308@redhat.com
Whole thread Raw
List pgsql-patches
This patch includes the generation of SQLStatus for other connection
problems that may arise.  All the codes are standard this time. I used
the best of my judgment to attribute the standard code to each situation.

The patch includes the previous one as I could not calculate the cvs
diff, but I will duplicate my source tree and generate an incremental
diff if necessary.

Regards,
Fernando


--
Fernando Nasser
Red Hat Canada Ltd.                     E-Mail:  fnasser@redhat.com
2323 Yonge Street, Suite #300
Toronto, Ontario   M4P 2C9
Index: src/interfaces/jdbc/org/postgresql/PG_Stream.java
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/PG_Stream.java,v
retrieving revision 1.17
diff -c -p -r1.17 PG_Stream.java
*** src/interfaces/jdbc/org/postgresql/PG_Stream.java    2002/08/20 04:26:02    1.17
--- src/interfaces/jdbc/org/postgresql/PG_Stream.java    2002/11/22 19:11:02
*************** public class PG_Stream
*** 137,147 ****
          {
              c = pg_input.read();
              if (c < 0)
!                 throw new PSQLException("postgresql.stream.eof");
          }
          catch (IOException e)
          {
!             throw new PSQLException("postgresql.stream.ioerror", e);
          }
          return c;
      }
--- 137,147 ----
          {
              c = pg_input.read();
              if (c < 0)
!                 throw new PSQLException("postgresql.stream.eof", PSQLException.comm_link_error);
          }
          catch (IOException e)
          {
!             throw new PSQLException("postgresql.stream.ioerror", PSQLException.comm_link_error, e);
          }
          return c;
      }
*************** public class PG_Stream
*** 164,176 ****
                  int b = pg_input.read();

                  if (b < 0)
!                     throw new PSQLException("postgresql.stream.eof");
                  n = n | (b << (8 * i)) ;
              }
          }
          catch (IOException e)
          {
!             throw new PSQLException("postgresql.stream.ioerror", e);
          }
          return n;
      }
--- 164,176 ----
                  int b = pg_input.read();

                  if (b < 0)
!                     throw new PSQLException("postgresql.stream.eof", PSQLException.comm_link_error);
                  n = n | (b << (8 * i)) ;
              }
          }
          catch (IOException e)
          {
!             throw new PSQLException("postgresql.stream.ioerror", PSQLException.comm_link_error, e);
          }
          return n;
      }
*************** public class PG_Stream
*** 193,205 ****
                  int b = pg_input.read();

                  if (b < 0)
!                     throw new PSQLException("postgresql.stream.eof");
                  n = b | (n << 8);
              }
          }
          catch (IOException e)
          {
!             throw new PSQLException("postgresql.stream.ioerror", e);
          }
          return n;
      }
--- 193,205 ----
                  int b = pg_input.read();

                  if (b < 0)
!                     throw new PSQLException("postgresql.stream.eof", PSQLException.comm_link_error);
                  n = b | (n << 8);
              }
          }
          catch (IOException e)
          {
!             throw new PSQLException("postgresql.stream.ioerror", PSQLException.comm_link_error, e);
          }
          return n;
      }
*************** public class PG_Stream
*** 227,233 ****
                  {
                      int c = pg_input.read();
                      if (c < 0)
!                         throw new PSQLException("postgresql.stream.eof");
                      else if (c == 0)
                      {
                          rst[s] = 0;
--- 227,233 ----
                  {
                      int c = pg_input.read();
                      if (c < 0)
!                         throw new PSQLException("postgresql.stream.eof", PSQLException.comm_link_error);
                      else if (c == 0)
                      {
                          rst[s] = 0;
*************** public class PG_Stream
*** 250,256 ****
          }
          catch (IOException e)
          {
!             throw new PSQLException("postgresql.stream.ioerror", e);
          }
          return encoding.decode(rst, 0, s);
      }
--- 250,256 ----
          }
          catch (IOException e)
          {
!             throw new PSQLException("postgresql.stream.ioerror", PSQLException.comm_link_error, e);
          }
          return encoding.decode(rst, 0, s);
      }
*************** public class PG_Stream
*** 330,342 ****
              {
                  int w = pg_input.read(b, off + s, siz - s);
                  if (w < 0)
!                     throw new PSQLException("postgresql.stream.eof");
                  s += w;
              }
          }
          catch (IOException e)
          {
!             throw new PSQLException("postgresql.stream.ioerror", e);
          }
      }

--- 330,342 ----
              {
                  int w = pg_input.read(b, off + s, siz - s);
                  if (w < 0)
!                     throw new PSQLException("postgresql.stream.eof", PSQLException.comm_link_error);
                  s += w;
              }
          }
          catch (IOException e)
          {
!             throw new PSQLException("postgresql.stream.ioerror", PSQLException.comm_link_error, e);
          }
      }

*************** public class PG_Stream
*** 353,359 ****
          }
          catch (IOException e)
          {
!             throw new PSQLException("postgresql.stream.flush", e);
          }
      }

--- 353,359 ----
          }
          catch (IOException e)
          {
!             throw new PSQLException("postgresql.stream.flush", PSQLException.comm_link_error, e);
          }
      }

Index: src/interfaces/jdbc/org/postgresql/core/QueryExecutor.java
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/core/QueryExecutor.java,v
retrieving revision 1.17
diff -c -p -r1.17 QueryExecutor.java
*** src/interfaces/jdbc/org/postgresql/core/QueryExecutor.java    2002/11/14 05:35:45    1.17
--- src/interfaces/jdbc/org/postgresql/core/QueryExecutor.java    2002/11/22 19:11:02
*************** public class QueryExecutor
*** 61,67 ****

          if (pg_stream == null)
          {
!             throw new PSQLException("postgresql.con.closed");
          }

          synchronized (pg_stream)
--- 61,67 ----

          if (pg_stream == null)
          {
!             throw new PSQLException("postgresql.con.closed", PSQLException.inexistent_connection);
          }

          synchronized (pg_stream)
*************** public class QueryExecutor
*** 120,126 ****
                          l_endQuery = true;
                          break;
                      default:
!                         throw new PSQLException("postgresql.con.type",
                                                  new Character((char) c));
                  }

--- 120,126 ----
                          l_endQuery = true;
                          break;
                      default:
!                         throw new PSQLException("postgresql.con.type", PSQLException.connection_failure,
                                                  new Character((char) c));
                  }

*************** public class QueryExecutor
*** 156,162 ****
          }
          catch (IOException e)
          {
!             throw new PSQLException("postgresql.con.ioerror", e);
          }
      }

--- 156,162 ----
          }
          catch (IOException e)
          {
!             throw new PSQLException("postgresql.con.ioerror", PSQLException.comm_link_error, e);
          }
      }

*************** public class QueryExecutor
*** 168,174 ****
      private void receiveTuple(boolean isBinary) throws SQLException
      {
          if (fields == null)
!             throw new PSQLException("postgresql.con.tuple");
          Object tuple = pg_stream.ReceiveTuple(fields.length, isBinary);
          if (isBinary)
              binaryCursor = true;
--- 168,174 ----
      private void receiveTuple(boolean isBinary) throws SQLException
      {
          if (fields == null)
!             throw new PSQLException("postgresql.con.tuple", PSQLException.connection_failure);
          Object tuple = pg_stream.ReceiveTuple(fields.length, isBinary);
          if (isBinary)
              binaryCursor = true;
*************** public class QueryExecutor
*** 199,205 ****
          }
          catch (NumberFormatException nfe)
          {
!             throw new PSQLException("postgresql.con.fathom", status);
          }
      }

--- 199,205 ----
          }
          catch (NumberFormatException nfe)
          {
!             throw new PSQLException("postgresql.con.fathom", PSQLException.connection_failure, status);
          }
      }

*************** public class QueryExecutor
*** 209,215 ****
      private void receiveFields() throws SQLException
      {
          if (fields != null)
!             throw new PSQLException("postgresql.con.multres");

          int size = pg_stream.ReceiveIntegerR(2);
          fields = new Field[size];
--- 209,215 ----
      private void receiveFields() throws SQLException
      {
          if (fields != null)
!             throw new PSQLException("postgresql.con.multres", PSQLException.connection_failure);

          int size = pg_stream.ReceiveIntegerR(2);
          fields = new Field[size];
Index: src/interfaces/jdbc/org/postgresql/fastpath/Fastpath.java
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/fastpath/Fastpath.java,v
retrieving revision 1.11
diff -c -p -r1.11 Fastpath.java
*** src/interfaces/jdbc/org/postgresql/fastpath/Fastpath.java    2002/09/06 21:23:05    1.11
--- src/interfaces/jdbc/org/postgresql/fastpath/Fastpath.java    2002/11/22 19:11:03
*************** public class Fastpath
*** 81,87 ****
              }
              catch (IOException ioe)
              {
!                 throw new PSQLException("postgresql.fp.send", new Integer(fnid), ioe);
              }

              // Now handle the result
--- 81,89 ----
              }
              catch (IOException ioe)
              {
!                 throw new PSQLException("postgresql.fp.send",
!                                         PSQLException.comm_link_error,
!                                         new Integer(fnid), ioe);
              }

              // Now handle the result
Index: src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Connection.java
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Connection.java,v
retrieving revision 1.13
diff -c -p -r1.13 AbstractJdbc1Connection.java
*** src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Connection.java    2002/11/14 05:35:45    1.13
--- src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Connection.java    2002/11/22 19:11:03
*************** public abstract class AbstractJdbc1Conne
*** 104,110 ****
          // 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");

          this_driver = (org.postgresql.Driver)d;
          this_url = url;
--- 104,110 ----
          // 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", PSQLException.connection_rejected);

          this_driver = (org.postgresql.Driver)d;
          this_url = url;
*************** public abstract class AbstractJdbc1Conne
*** 164,174 ****
              // 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");
          }
          catch (IOException e)
          {
!             throw new PSQLException ("postgresql.con.failed", e);
          }

          // Now we need to construct and send a startup packet
--- 164,174 ----
              // 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", PSQLException.connection_rejected);
          }
          catch (IOException e)
          {
!             throw new PSQLException ("postgresql.con.failed", PSQLException.unable_to_connect, e);
          }

          // Now we need to construct and send a startup packet
*************** public abstract class AbstractJdbc1Conne
*** 199,205 ****
                          // The most common one to be thrown here is:
                          // "User authentication failed"
                          //
!                         throw new PSQLException("postgresql.con.misc", pg_stream.ReceiveString(encoding));

                      case 'R':
                          // Get the type of request
--- 199,206 ----
                          // The most common one to be thrown here is:
                          // "User authentication failed"
                          //
!                         throw new PSQLException("postgresql.con.misc", PSQLException.connection_rejected,
!                                                 pg_stream.ReceiveString(encoding));

                      case 'R':
                          // Get the type of request
*************** public abstract class AbstractJdbc1Conne
*** 237,248 ****
                              case AUTH_REQ_KRB4:
                                  if (org.postgresql.Driver.logDebug)
                                      org.postgresql.Driver.debug("postgresql: KRB4");
!                                 throw new PSQLException("postgresql.con.kerb4");

                              case AUTH_REQ_KRB5:
                                  if (org.postgresql.Driver.logDebug)
                                      org.postgresql.Driver.debug("postgresql: KRB5");
!                                 throw new PSQLException("postgresql.con.kerb5");

                              case AUTH_REQ_PASSWORD:
                                  if (org.postgresql.Driver.logDebug)
--- 238,249 ----
                              case AUTH_REQ_KRB4:
                                  if (org.postgresql.Driver.logDebug)
                                      org.postgresql.Driver.debug("postgresql: KRB4");
!                                 throw new PSQLException("postgresql.con.kerb4", PSQLException.connection_rejected);

                              case AUTH_REQ_KRB5:
                                  if (org.postgresql.Driver.logDebug)
                                      org.postgresql.Driver.debug("postgresql: KRB5");
!                                 throw new PSQLException("postgresql.con.kerb5", PSQLException.connection_rejected);

                              case AUTH_REQ_PASSWORD:
                                  if (org.postgresql.Driver.logDebug)
*************** public abstract class AbstractJdbc1Conne
*** 274,285 ****
                                  break;

                              default:
!                                 throw new PSQLException("postgresql.con.auth", new Integer(areq));
                          }
                          break;

                      default:
!                         throw new PSQLException("postgresql.con.authfail");
                  }
              }
              while (areq != AUTH_REQ_OK);
--- 275,287 ----
                                  break;

                              default:
!                                 throw new PSQLException("postgresql.con.auth", PSQLException.connection_rejected,
!                                                         new Integer(areq));
                          }
                          break;

                      default:
!                         throw new PSQLException("postgresql.con.authfail", PSQLException.connection_rejected);
                  }
              }
              while (areq != AUTH_REQ_OK);
*************** public abstract class AbstractJdbc1Conne
*** 287,293 ****
          }
          catch (IOException e)
          {
!             throw new PSQLException("postgresql.con.failed", e);
          }


--- 289,295 ----
          }
          catch (IOException e)
          {
!             throw new PSQLException("postgresql.con.failed", PSQLException.unable_to_connect, e);
          }


*************** public abstract class AbstractJdbc1Conne
*** 303,314 ****
                      ckey = pg_stream.ReceiveIntegerR(4);
                      break;
                  case 'E':
!                     throw new PSQLException("postgresql.con.backend", pg_stream.ReceiveString(encoding));
                  case 'N':
                      addWarning(pg_stream.ReceiveString(encoding));
                      break;
                  default:
!                     throw new PSQLException("postgresql.con.setup");
              }
          }
          while (beresp == 'N');
--- 305,317 ----
                      ckey = pg_stream.ReceiveIntegerR(4);
                      break;
                  case 'E':
!                     throw new PSQLException("postgresql.con.backend", PSQLException.unable_to_connect,
!                                             pg_stream.ReceiveString(encoding));
                  case 'N':
                      addWarning(pg_stream.ReceiveString(encoding));
                      break;
                  default:
!                     throw new PSQLException("postgresql.con.setup", PSQLException.unable_to_connect);
              }
          }
          while (beresp == 'N');
*************** public abstract class AbstractJdbc1Conne
*** 325,333 ****
                      addWarning(pg_stream.ReceiveString(encoding));
                      break;
                  case 'E':
!                     throw new PSQLException("postgresql.con.backend", pg_stream.ReceiveString(encoding));
                  default:
!                     throw new PSQLException("postgresql.con.setup");
              }
          }
          while (beresp == 'N');
--- 328,337 ----
                      addWarning(pg_stream.ReceiveString(encoding));
                      break;
                  case 'E':
!                     throw new PSQLException("postgresql.con.backend", PSQLException.unable_to_connect,
!                                             pg_stream.ReceiveString(encoding));
                  default:
!                     throw new PSQLException("postgresql.con.setup", PSQLException.unable_to_connect);
              }
          }
          while (beresp == 'N');
*************** public abstract class AbstractJdbc1Conne
*** 355,361 ****

          if (! resultSet.next())
          {
!             throw new PSQLException("postgresql.con.failed", "failed getting backend encoding");
          }
          String version = resultSet.getString(1);
          dbVersionNumber = extractVersionNumber(version);
--- 359,366 ----

          if (! resultSet.next())
          {
!             throw new PSQLException("postgresql.con.failed", PSQLException.unable_to_connect,
!                                     "failed getting backend encoding");
          }
          String version = resultSet.getString(1);
          dbVersionNumber = extractVersionNumber(version);
*************** public abstract class AbstractJdbc1Conne
*** 380,386 ****

              if (!acRset.next())
              {
!                 throw new PSQLException("postgresql.con.failed", "failed getting autocommit status");
              }
              //if autocommit is currently off we need to turn it on
              //note that we will be in a transaction because the select above
--- 385,392 ----

              if (!acRset.next())
              {
!                 throw new PSQLException("postgresql.con.failed", PSQLException.unable_to_connect,
!                                         "failed getting autocommit status");
              }
              //if autocommit is currently off we need to turn it on
              //note that we will be in a transaction because the select above
*************** public abstract class AbstractJdbc1Conne
*** 477,483 ****
      {
          if (isClosed())
          {
!             throw new PSQLException("postgresql.con.closed");
          }
          return new QueryExecutor(new String[] {sql}, EMPTY_OBJECT_ARRAY, stat, pg_stream,
(java.sql.Connection)this).execute();
      }
--- 483,489 ----
      {
          if (isClosed())
          {
!             throw new PSQLException("postgresql.con.closed", PSQLException.inexistent_connection);
          }
          return new QueryExecutor(new String[] {sql}, EMPTY_OBJECT_ARRAY, stat, pg_stream,
(java.sql.Connection)this).execute();
      }
*************** public abstract class AbstractJdbc1Conne
*** 500,506 ****
      {
          if (isClosed())
          {
!             throw new PSQLException("postgresql.con.closed");
          }
          return new QueryExecutor(p_sqlFragments, p_binds, stat, pg_stream, (java.sql.Connection)this).execute();
      }
--- 506,512 ----
      {
          if (isClosed())
          {
!             throw new PSQLException("postgresql.con.closed", PSQLException.inexistent_connection);
          }
          return new QueryExecutor(p_sqlFragments, p_binds, stat, pg_stream, (java.sql.Connection)this).execute();
      }
*************** public abstract class AbstractJdbc1Conne
*** 695,701 ****
          }
          catch (Exception ex)
          {
!             throw new PSQLException("postgresql.con.creobj", type, ex);
          }

          // should never be reached
--- 701,707 ----
          }
          catch (Exception ex)
          {
!             throw new PSQLException("postgresql.con.creobj", PSQLException.connection_failure, type, ex);
          }

          // should never be reached
*************** public abstract class AbstractJdbc1Conne
*** 744,750 ****
                  return ((Serialize)x).storeObject(o);

              // Thow an exception because the type is unknown
!             throw new PSQLException("postgresql.con.strobj");

          }
          catch (SQLException sx)
--- 750,756 ----
                  return ((Serialize)x).storeObject(o);

              // Thow an exception because the type is unknown
!             throw new PSQLException("postgresql.con.strobj", PSQLException.data_exception);

          }
          catch (SQLException sx)
*************** public abstract class AbstractJdbc1Conne
*** 1117,1123 ****
                      isolationLevelSQL += "SERIALIZABLE";
                      break;
                  default:
!                     throw new PSQLException("postgresql.con.isolevel",
                                              new Integer(isolationLevel));
              }
          }
--- 1123,1129 ----
                      isolationLevelSQL += "SERIALIZABLE";
                      break;
                  default:
!                     throw new PSQLException("postgresql.con.isolevel", PSQLException.inv_transaction_state,
                                              new Integer(isolationLevel));
              }
          }
*************** public abstract class AbstractJdbc1Conne
*** 1154,1160 ****
                  break;

              default:
!                 throw new PSQLException("postgresql.con.isolevel", new Integer(isolationLevel));
          }
          return sb.toString();
      }
--- 1160,1167 ----
                  break;

              default:
!                 throw new PSQLException("postgresql.con.isolevel", PSQLException.inv_transaction_state,
!                                         new Integer(isolationLevel));
          }
          return sb.toString();
      }
Index: src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1ResultSet.java
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1ResultSet.java,v
retrieving revision 1.7
diff -c -p -r1.7 AbstractJdbc1ResultSet.java
*** src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1ResultSet.java    2002/10/19 22:10:36    1.7
--- src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1ResultSet.java    2002/11/22 19:11:03
*************** public abstract class AbstractJdbc1Resul
*** 60,66 ****
      public boolean next() throws SQLException
      {
          if (rows == null)
!             throw new PSQLException("postgresql.con.closed");

          if (++current_row >= rows.size())
              return false;
--- 60,66 ----
      public boolean next() throws SQLException
      {
          if (rows == null)
!             throw new PSQLException("postgresql.con.closed", PSQLException.inexistent_connection);

          if (++current_row >= rows.size())
              return false;
Index: src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2Connection.java
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2Connection.java,v
retrieving revision 1.2
diff -c -p -r1.2 AbstractJdbc2Connection.java
*** src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2Connection.java    2002/09/06 21:23:06    1.2
--- src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2Connection.java    2002/11/22 19:11:03
*************** public abstract class AbstractJdbc2Conne
*** 64,74 ****
              // 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");
          }
          catch (IOException e)
          {
!             throw new PSQLException ("postgresql.con.failed", e);
          }

          // Now we need to construct and send a cancel packet
--- 64,74 ----
              // 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", PSQLException.connection_rejected);
          }
          catch (IOException e)
          {
!             throw new PSQLException ("postgresql.con.failed", PSQLException.unable_to_connect, e);
          }

          // Now we need to construct and send a cancel packet
*************** public abstract class AbstractJdbc2Conne
*** 82,88 ****
          }
          catch (IOException e)
          {
!             throw new PSQLException("postgresql.con.failed", e);
          }
          finally
          {
--- 82,88 ----
          }
          catch (IOException e)
          {
!             throw new PSQLException("postgresql.con.failed", PSQLException.comm_link_error, e);
          }
          finally
          {
Index: src/interfaces/jdbc/org/postgresql/jdbc3/AbstractJdbc3DatabaseMetaData.java
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/jdbc3/AbstractJdbc3DatabaseMetaData.java,v
retrieving revision 1.3
diff -c -p -r1.3 AbstractJdbc3DatabaseMetaData.java
*** src/interfaces/jdbc/org/postgresql/jdbc3/AbstractJdbc3DatabaseMetaData.java    2002/11/20 20:37:53    1.3
--- src/interfaces/jdbc/org/postgresql/jdbc3/AbstractJdbc3DatabaseMetaData.java    2002/11/22 19:11:03
*************** public abstract class AbstractJdbc3Datab
*** 323,329 ****
       */
      public int getSQLStateType() throws SQLException
      {
!         throw org.postgresql.Driver.notImplemented();
      }

      /**
--- 323,329 ----
       */
      public int getSQLStateType() throws SQLException
      {
!         return DatabaseMetaData.sqlStateXOpen;
      }

      /**
Index: src/interfaces/jdbc/org/postgresql/util/PSQLException.java
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/util/PSQLException.java,v
retrieving revision 1.7
diff -c -p -r1.7 PSQLException.java
*** src/interfaces/jdbc/org/postgresql/util/PSQLException.java    2001/11/19 22:33:39    1.7
--- src/interfaces/jdbc/org/postgresql/util/PSQLException.java    2002/11/22 19:11:03
*************** public class PSQLException extends SQLEx
*** 11,16 ****
--- 11,30 ----
      private String message;

      /*
+      * PSQLState constants
+      *
+      * Using the constants make the code more legible.
+      */
+
+     public static final PSQLState unable_to_connect = new PSQLState("08001");
+     public static final PSQLState inexistent_connection = new PSQLState("08003");
+     public static final PSQLState connection_rejected = new PSQLState("08004");
+     public static final PSQLState connection_failure = new PSQLState("08006");
+     public static final PSQLState comm_link_error = new PSQLState("08S01");
+     public static final PSQLState data_exception = new PSQLState("22000");
+     public static final PSQLState inv_transaction_state = new PSQLState("25000");
+
+     /*
       * This provides the same functionality to SQLException
       * @param error Error string
       */
*************** public class PSQLException extends SQLEx
*** 21,26 ****
--- 35,51 ----
      }

      /*
+      * Like the above, but sets SQLState
+      * @param error Error string
+      * @param sqlstate PSQLState constant
+      */
+     public PSQLException(String error, PSQLState sqlstate)
+     {
+         super("", sqlstate.toString());
+         translate(error, null);
+     }
+
+     /*
       * A more generic entry point.
       * @param error Error string or standard message id
       * @param args Array of arguments
*************** public class PSQLException extends SQLEx
*** 74,84 ****
--- 99,152 ----
      }

      /*
+      * Like the above, but sets the SQLState
+      */
+     public PSQLException(String error, PSQLState sqlstate, Exception ex)
+     {
+         super("", sqlstate.toString());
+
+         Object[] argv = new Object[1];
+
+         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();
+         }
+
+         translate(error, argv);
+     }
+
+     /*
       * Helper version for 2 args
       */
      public PSQLException(String error, Object arg1, Object arg2)
      {
          super();
+         Object[] argv = new Object[2];
+         argv[0] = arg1;
+         argv[1] = arg2;
+         translate(error, argv);
+     }
+
+     /*
+      * Like the above, but sets SQLState
+      * @param error Error string
+      * @param sqlstate PSQLState constant
+      */
+     public PSQLException(String error, PSQLState sqlstate, Object arg1, Object arg2)
+     {
+         super("", sqlstate.toString());
          Object[] argv = new Object[2];
          argv[0] = arg1;
          argv[1] = arg2;


New file: src/interfaces/jdbc/org/postgresql/util/PSQLState.java
===================================================================
package org.postgresql.util;

/*
 * This class encapsulates a String, so that SQLState arguments have their own type
 * (for method overload resolution)
 */
public class PSQLState extends Object
{
    private String sqlstate;

    /*
     * We only instantiate this with the X/Open strintgs,
     * so we only provide this constructor.
     * @param sqlstate PSQLState constant
     */
    public PSQLState(String sqlstate)
    {
        this.sqlstate = sqlstate;
    }

    public String toString()
    {
        return sqlstate;
    }

    public boolean equals(String other)
    {
        return sqlstate.equals(other);
    }
}

pgsql-patches by date:

Previous
From: Fernando Nasser
Date:
Subject: JDBC Emit SQLStatus for communication link errors
Next
From: Neil Conway
Date:
Subject: release note fixes (7.3 & HEAD)