JDBC Issue warning if could not find encoding - Mailing list pgsql-patches

From Fernando Nasser
Subject JDBC Issue warning if could not find encoding
Date
Msg-id 3DE2A417.7030108@redhat.com
Whole thread Raw
List pgsql-patches
We currently silent fall back to the default JVM encoding if we could not find
the requested on or the one used by the backend (if it is unknown we also fall
back).  At least we should log a warning.  This patch adds the appropriate warnings.

P.S.:   This affects up to 7.2 backends only. None of this makes sense on 7.3+
backends; we will just use UNICODE and warn that we ignored charSet.

--
Fernando Nasser
Red Hat - Toronto                       E-Mail:  fnasser@redhat.com
2323 Yonge Street, Suite #300
Toronto, Ontario   M4P 2C9

Index: src/interfaces/jdbc/org/postgresql/core/Encoding.java
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/core/Encoding.java,v
retrieving revision 1.8
diff -c -p -r1.8 Encoding.java
*** src/interfaces/jdbc/org/postgresql/core/Encoding.java    2002/11/14 05:35:45    1.8
--- src/interfaces/jdbc/org/postgresql/core/Encoding.java    2002/11/25 22:17:47
*************** public class Encoding
*** 72,82 ****
      }

      /*
!      * Get an Encoding for from the given database encoding and
       * the encoding passed in by the user.
       */
!     public static Encoding getEncoding(String databaseEncoding,
!                                        String passedEncoding)
      {
          if (passedEncoding != null)
          {
--- 72,82 ----
      }

      /*
!      * Get an Encoding from the given database encoding and
       * the encoding passed in by the user.
+      * Return null if cannot find a suitable one.
       */
!     public static Encoding getEncoding(String databaseEncoding, String passedEncoding)
      {
          if (passedEncoding != null)
          {
*************** public class Encoding
*** 86,121 ****
              }
              else
              {
!                 return defaultEncoding();
              }
          }
          else
          {
!             return encodingForDatabaseEncoding(databaseEncoding);
!         }
!     }
!
!     /*
!      * Get an Encoding matching the given database encoding.
!      */
!     private static Encoding encodingForDatabaseEncoding(String databaseEncoding)
!     {
!         // If the backend encoding is known and there is a suitable
!         // encoding in the JVM we use that. Otherwise we fall back
!         // to the default encoding of the JVM.
!
!         if (encodings.containsKey(databaseEncoding))
!         {
!             String[] candidates = (String[]) encodings.get(databaseEncoding);
!             for (int i = 0; i < candidates.length; i++)
              {
!                 if (isAvailable(candidates[i]))
                  {
!                     return new Encoding(candidates[i]);
                  }
              }
          }
-         return defaultEncoding();
      }

      /*
--- 86,116 ----
              }
              else
              {
!                 // Requested encoding not available in the JVM
!                 return null;
              }
          }
          else
          {
!             // Get an Encoding matching the given database encoding.
!             // If the backend encoding is known and there is a suitable
!             // encoding in the JVM we return that.
!
!             if (encodings.containsKey(databaseEncoding))
              {
!                 String[] candidates = (String[]) encodings.get(databaseEncoding);
!                 for (int i = 0; i < candidates.length; i++)
                  {
!                     if (isAvailable(candidates[i]))
!                     {
!                         return new Encoding(candidates[i]);
!                     }
                  }
              }
+
+             // Bakend encoding UNKNOWN or not available in the JVM
+             return null;
          }
      }

      /*
*************** public class Encoding
*** 144,150 ****
          }
          catch (UnsupportedEncodingException e)
          {
!             throw new PSQLException("postgresql.stream.encoding", e);
          }
      }

--- 139,146 ----
          }
          catch (UnsupportedEncodingException e)
          {
!             // This will never happen as we made sure that the encoding is valid when the connection was created
!             throw new PSQLException("postgresql.stream.encoding", PSQLException.data_exception, e);
          }
      }

*************** public class Encoding
*** 169,175 ****
          }
          catch (UnsupportedEncodingException e)
          {
!             throw new PSQLException("postgresql.stream.encoding", e);
          }
      }

--- 165,172 ----
          }
          catch (UnsupportedEncodingException e)
          {
!             // This will never happen as we made sure that the encoding is valid when the connection was created
!             throw new PSQLException("postgresql.stream.encoding", PSQLException.data_exception, e);
          }
      }

*************** public class Encoding
*** 199,205 ****
          }
          catch (UnsupportedEncodingException e)
          {
!             throw new PSQLException("postgresql.res.encoding", e);
          }
      }

--- 196,203 ----
          }
          catch (UnsupportedEncodingException e)
          {
!             // This will never happen as we made sure that the encoding is valid when the connection was created
!             throw new PSQLException("postgresql.res.encoding", PSQLException.data_exception, e);
          }
      }

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/25 22:26:34
*************** public abstract class AbstractJdbc1Conne
*** 355,367 ****

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

          String dbEncoding = resultSet.getString(2);
!         encoding = Encoding.getEncoding(dbEncoding, info.getProperty("charSet"));
          //In 7.3 we are forced to do a second roundtrip to handle the case
          //where a database may not be running in autocommit mode
          //jdbc by default assumes autocommit is on until setAutoCommit(false)
--- 359,372 ----

          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);

          String dbEncoding = resultSet.getString(2);
!
          //In 7.3 we are forced to do a second roundtrip to handle the case
          //where a database may not be running in autocommit mode
          //jdbc by default assumes autocommit is on until setAutoCommit(false)
*************** public abstract class AbstractJdbc1Conne
*** 377,386 ****

              //set encoding to be unicode
              encoding = Encoding.getEncoding("UNICODE", null);

              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
--- 382,396 ----

              //set encoding to be unicode
              encoding = Encoding.getEncoding("UNICODE", null);
+             if (info.getProperty("charSet") != null)
+             {
+                 addWarning("Obsolete charSet connection property ignored");
+             }

              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
*** 391,396 ****
--- 401,424 ----
                  ExecSQL("set autocommit = on; commit;");
              }
          }
+         else
+         {
+             // Try setting the encoding to the requested encoding or backend encoding
+             encoding = Encoding.getEncoding(dbEncoding, info.getProperty("charSet"));
+
+             if (encoding == null)
+             {
+                 if (info.getProperty("charSet") != null)
+                 {
+                     addWarning("Requested encoding not available in the JVM - using JVM default encoding");
+                 }
+                 else
+                 {
+                     addWarning("Backend encoding unknown or not available in the JVM - using JVM default encoding");
+                 }
+                 encoding = Encoding.defaultEncoding();
+             }
+         }

          // Initialise object handling
          initObjectTypes();

pgsql-patches by date:

Previous
From: Rod Taylor
Date:
Subject: Resultmap for FreeBSD 4.7
Next
From: Fernando Nasser
Date:
Subject: JDBC SQLStatus for Dynamic SQL, unsupported feature, invalid transaction state and others