Re: SetMaxFieldSize

Поиск
Список
Период
Сортировка
От Barry Lind
Тема Re: SetMaxFieldSize
Дата
Msg-id 3F493871.70507@xythos.com
обсуждение исходный текст
Ответ на SetMaxFieldSize  (Kim Ho <kho@redhat.com>)
Список pgsql-jdbc
Patch applied.  I reworked it a lot to clean it up to avoid the
recursion problem you were having.

thanks,
--Barry


Kim Ho wrote:
> Implemented, but not very pretty.
>
> I'm wondering if someone can show me a better way of doing the
> getSQLType().
>
> Because the getSQLType() method eventually calls getString() again and
> then it throws it into infinite recursion, so I had to do it in a pretty
> messy way.
>
> If anyone can give me some pointers, much appreciated.
>
> Cheers,
>
> Kim
>
>
> ------------------------------------------------------------------------
>
> Index: org/postgresql/errors.properties
> ===================================================================
> RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/errors.properties,v
> retrieving revision 1.22
> diff -c -p -r1.22 errors.properties
> *** org/postgresql/errors.properties    9 Jul 2003 05:12:03 -0000    1.22
> --- org/postgresql/errors.properties    31 Jul 2003 19:37:03 -0000
> *************** postgresql.input.rows.gt0:Maximum number
> *** 103,105 ****
> --- 103,106 ----
>   postgresql.format.baddate:The date given: {0} does not match the format required: {1}.
>   postgresql.format.badtime:The time given: {0} does not match the format required: {1}.
>   postgresql.format.badtimestamp:The timestamp given {0} does not match the format required: {1}.
> + postgresql.input.field.gt0:The maximum field size must be a value greater than or equal to 0.
> Index: org/postgresql/core/BaseStatement.java
> ===================================================================
> RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/core/BaseStatement.java,v
> retrieving revision 1.3
> diff -c -p -r1.3 BaseStatement.java
> *** org/postgresql/core/BaseStatement.java    7 May 2003 03:03:30 -0000    1.3
> --- org/postgresql/core/BaseStatement.java    31 Jul 2003 19:37:04 -0000
> *************** public interface BaseStatement extends o
> *** 32,37 ****
>       public int getFetchSize() throws SQLException;
>       public int getMaxRows() throws SQLException;
>       public int getResultSetConcurrency() throws SQLException;
>       public String getStatementName();
> -
>   }
> --- 32,38 ----
>       public int getFetchSize() throws SQLException;
>       public int getMaxRows() throws SQLException;
>       public int getResultSetConcurrency() throws SQLException;
> +     public int getMaxFieldSize() throws SQLException;
> +     public void setMaxFieldSize(int max) throws SQLException;
>       public String getStatementName();
>   }
> Index: org/postgresql/jdbc1/AbstractJdbc1ResultSet.java
> ===================================================================
> RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1ResultSet.java,v
> retrieving revision 1.13
> diff -c -p -r1.13 AbstractJdbc1ResultSet.java
> *** org/postgresql/jdbc1/AbstractJdbc1ResultSet.java    30 Jun 2003 21:10:55 -0000    1.13
> --- org/postgresql/jdbc1/AbstractJdbc1ResultSet.java    31 Jul 2003 19:37:07 -0000
> *************** public abstract class AbstractJdbc1Resul
> *** 172,178 ****
>               return null;
>
>           Encoding encoding = connection.getEncoding();
> !         return encoding.decode(this_row[columnIndex - 1]);
>       }
>
>       public boolean getBoolean(int columnIndex) throws SQLException
> --- 172,183 ----
>               return null;
>
>           Encoding encoding = connection.getEncoding();
> !         int maxSize = statement.getMaxFieldSize();
> !         if (maxSize > 0 && checkTrimmableColumn(columnIndex))
> !         {
> !             return (encoding.decode(this_row[columnIndex-1], 0, maxSize));
> !         }
> !         return encoding.decode(this_row[columnIndex-1]);
>       }
>
>       public boolean getBoolean(int columnIndex) throws SQLException
> *************** public abstract class AbstractJdbc1Resul
> *** 299,309 ****
>                   //Version 7.2 supports the bytea datatype for byte arrays
>                   if (fields[columnIndex - 1].getPGType().equals("bytea"))
>                   {
> !                     return PGbytea.toBytes(this_row[columnIndex - 1]);
>                   }
>                   else
>                   {
> !                     return this_row[columnIndex - 1];
>                   }
>               }
>               else
> --- 304,314 ----
>                   //Version 7.2 supports the bytea datatype for byte arrays
>                   if (fields[columnIndex - 1].getPGType().equals("bytea"))
>                   {
> !                     return (checkTrimmableColumn(columnIndex) ? byteTrim(PGbytea.toBytes(this_row[columnIndex -
1])): PGbytea.toBytes(this_row[columnIndex - 1])); 
>                   }
>                   else
>                   {
> !                     return (checkTrimmableColumn(columnIndex) ? byteTrim(this_row[columnIndex - 1]) :
this_row[columnIndex- 1]); 
>                   }
>               }
>               else
> *************** public abstract class AbstractJdbc1Resul
> *** 316,326 ****
>                       LargeObject lob = lom.open(getInt(columnIndex));
>                       byte buf[] = lob.read(lob.size());
>                       lob.close();
> !                     return buf;
>                   }
>                   else
>                   {
> !                     return this_row[columnIndex - 1];
>                   }
>               }
>           }
> --- 321,331 ----
>                       LargeObject lob = lom.open(getInt(columnIndex));
>                       byte buf[] = lob.read(lob.size());
>                       lob.close();
> !                     return (checkTrimmableColumn(columnIndex) ? byteTrim(buf) : buf);
>                   }
>                   else
>                   {
> !                     return (checkTrimmableColumn(columnIndex) ? byteTrim(this_row[columnIndex - 1]) :
this_row[columnIndex- 1]); 
>                   }
>               }
>           }
> *************** public abstract class AbstractJdbc1Resul
> *** 1139,1145 ****
> --- 1144,1192 ----
>               }
>           }
>       }
> +
> +     private boolean checkTrimmableColumn(int columnIndex) throws SQLException
> +     {
> +         int tempSize = 0;
> +         try
> +         {
> +             //need to turn the trimming off when we go to get this kinda thing
> +             //or else it'll throw us into infinite loop
> +             tempSize = statement.getMaxFieldSize();
> +             statement.setMaxFieldSize(0);
> +             switch (fields[columnIndex-1].getSQLType())
> +             {
> +                 case Types.CHAR:
> +                 case Types.VARCHAR:
> +                 case Types.LONGVARCHAR:
> +                 case Types.BINARY:
> +                 case Types.VARBINARY:
> +                 case Types.LONGVARBINARY:
> +                     return true;
> +             }
> +             return false;
> +         }
> +         catch (SQLException e)
> +         {
> +             throw new PSQLException("postgresql.unexpected");
> +         }
> +         finally
> +         {
> +             statement.setMaxFieldSize(tempSize);
> +         }
> +     }
>
> +     private byte[] byteTrim(byte[] tmpBytes) throws SQLException
> +     {
> +         int tmpSize = statement.getMaxFieldSize();
> +         //in following cases, it is not necessary to make a copy.
> +         if (tmpSize == 0 || tmpSize > tmpBytes.length)
> +             return tmpBytes;
> +         byte[] bytes = new byte[tmpSize];
> +         System.arraycopy (tmpBytes, 0, bytes, 0, tmpSize);
> +         return bytes;
> +     }
> +
>       public SimpleDateFormat getTimestampTZFormat() {
>           if (m_tstzFormat == null) {
>               m_tstzFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
> Index: org/postgresql/jdbc1/AbstractJdbc1Statement.java
> ===================================================================
> RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java,v
> retrieving revision 1.29
> diff -c -p -r1.29 AbstractJdbc1Statement.java
> *** org/postgresql/jdbc1/AbstractJdbc1Statement.java    24 Jul 2003 00:30:39 -0000    1.29
> --- org/postgresql/jdbc1/AbstractJdbc1Statement.java    31 Jul 2003 19:37:10 -0000
> *************** public abstract class AbstractJdbc1State
> *** 87,93 ****
>       // returnTypeSet is true when a proper call to registerOutParameter has been made
>       private boolean returnTypeSet;
>       protected Object callResult;
> !
>
>       public abstract BaseResultSet createResultSet(Field[] fields, Vector tuples, String status, int updateCount,
longinsertOID, boolean binaryCursor) throws SQLException; 
>
> --- 87,93 ----
>       // returnTypeSet is true when a proper call to registerOutParameter has been made
>       private boolean returnTypeSet;
>       protected Object callResult;
> !     protected static int maxfieldSize = 0;
>
>       public abstract BaseResultSet createResultSet(Field[] fields, Vector tuples, String status, int updateCount,
longinsertOID, boolean binaryCursor) throws SQLException; 
>
> *************** public abstract class AbstractJdbc1State
> *** 640,646 ****
>        */
>       public int getMaxFieldSize() throws SQLException
>       {
> !         return 8192;        // We cannot change this
>       }
>
>       /*
> --- 640,646 ----
>        */
>       public int getMaxFieldSize() throws SQLException
>       {
> !         return maxfieldSize;
>       }
>
>       /*
> *************** public abstract class AbstractJdbc1State
> *** 652,658 ****
>        */
>       public void setMaxFieldSize(int max) throws SQLException
>       {
> !         throw new PSQLException("postgresql.stat.maxfieldsize");
>       }
>
>       /*
> --- 652,659 ----
>        */
>       public void setMaxFieldSize(int max) throws SQLException
>       {
> !         if (max < 0) throw new PSQLException("postgresql.input.field.gt0");
> !         maxfieldSize = max;
>       }
>
>       /*
>
>
> ------------------------------------------------------------------------
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster



В списке pgsql-jdbc по дате отправления:

Предыдущее
От: Barry Lind
Дата:
Сообщение: Re: patch: fix a couple of server-prepared-statement bugs
Следующее
От: Barry Lind
Дата:
Сообщение: Re: patch: add a finalizer to AbstractJdbc1Statement