You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by ha...@apache.org on 2003/10/06 18:03:10 UTC

cvs commit: cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/components/language/markup/xsp EsqlHelper.java

haul        2003/10/06 09:03:10

  Modified:    src/blocks/databases/java/org/apache/cocoon/components/language/markup/xsp
                        EsqlHelper.java
  Log:
  fixes Bug 23542 [PATCH/BUG] Better handling of CLOB in esql (get-xml) and handling of Oracle 'temporary lobs' Reporter: tomasz.bech@bull.com.pl (Tomasz Bech)
  
  Revision  Changes    Path
  1.2       +205 -89   cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/components/language/markup/xsp/EsqlHelper.java
  
  Index: EsqlHelper.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/components/language/markup/xsp/EsqlHelper.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- EsqlHelper.java	9 Mar 2003 00:03:06 -0000	1.1
  +++ EsqlHelper.java	6 Oct 2003 16:03:10 -0000	1.2
  @@ -73,14 +73,19 @@
   
   public class EsqlHelper {
   
  +    private static Boolean TRUE;
  +
  +    static {
  +        EsqlHelper.TRUE = new Boolean(true);
  +    }
   
       /** returns byte array from BLOB
        */
       public final static byte[] getBlob(ResultSet set, String column) throws RuntimeException {
  -        
  +
           byte[] result = null;
           try {
  -            result = EsqlHelper.getBlob(set,set.findColumn(column));
  +            result = EsqlHelper.getBlob(set, set.findColumn(column));
           } catch (Exception e) {
               throw new CascadingRuntimeException("Error getting blob data for column " + column, e);
           }
  @@ -90,13 +95,14 @@
       /** returns byte array from BLOB
        */
       public final static byte[] getBlob(ResultSet set, int column) throws java.lang.Exception {
  -        
  +
           InputStream reader = null;
           byte[] buffer = null;
  -    
  +        Blob dbBlob = null;
  +
           try {
  -            if (set.getMetaData().getColumnType(column)==java.sql.Types.BLOB) {
  -                Blob dbBlob = set.getBlob(column);
  +            if (set.getMetaData().getColumnType(column) == java.sql.Types.BLOB) {
  +                dbBlob = set.getBlob(column);
                   int length = (int) dbBlob.length();
                   reader = dbBlob.getBinaryStream();
                   buffer = new byte[length];
  @@ -107,24 +113,38 @@
                   if (buffer == null)
                       return null;
                   return buffer;
  -            } else {           
  +            } else {
                   return set.getString(column).getBytes();
               }
  -        } catch ( Exception e) {
  +        } catch (Exception e) {
               throw new CascadingRuntimeException("Error getting blob data for column " + column, e);
  +        } finally {
  +            // ORACLE 'temporary lob' problem patch start
  +            if (dbBlob != null && dbBlob.getClass().getName().equals("oracle.sql.BLOB")) {
  +                if (dbBlob
  +                    .getClass()
  +                    .getMethod("isTemporary", new Class[0])
  +                    .invoke(dbBlob, new Object[0])
  +                    .equals(TRUE))
  +                    dbBlob.getClass().getMethod("freeTemporary", new Class[0]).invoke(
  +                        dbBlob,
  +                        new Object[0]);
  +            }
           }
       }
   
       /** returns byte array from BLOB
        */
  -    public final static byte[] getBlob(CallableStatement cs, int column, String defaultString) throws java.lang.Exception {
  -        
  +    public final static byte[] getBlob(CallableStatement cs, int column, String defaultString)
  +        throws java.lang.Exception {
  +
           InputStream reader = null;
           byte[] buffer = null;
  -	byte[] result = null;
  -    
  +        byte[] result = null;
  +        Blob dbBlob = null;
  +
           try {
  -            Blob dbBlob = cs.getBlob(column);
  +            dbBlob = cs.getBlob(column);
               int length = (int) dbBlob.length();
               reader = dbBlob.getBinaryStream();
               buffer = new byte[length];
  @@ -132,41 +152,57 @@
               reader.close();
               if (reader != null)
                   reader.close();
  -	    if(buffer != null) result = buffer;
  -	    else if(defaultString != null && !defaultString.equals("_null_"))
  -		result = defaultString.getBytes();
  -	    else result = null;
  -        } catch ( Exception e) {
  +            if (buffer != null)
  +                result = buffer;
  +            else if (defaultString != null && !defaultString.equals("_null_"))
  +                result = defaultString.getBytes();
  +            else
  +                result = null;
  +        } catch (Exception e) {
               throw new CascadingRuntimeException("Error getting blob data for column " + column, e);
  +        } finally {
  +            // ORACLE 'temporary lob' problem patch start
  +            if (dbBlob != null && dbBlob.getClass().getName().equals("oracle.sql.BLOB")) {
  +                if (dbBlob
  +                    .getClass()
  +                    .getMethod("isTemporary", new Class[0])
  +                    .invoke(dbBlob, new Object[0])
  +                    .equals(TRUE))
  +                    dbBlob.getClass().getMethod("freeTemporary", new Class[0]).invoke(
  +                        dbBlob,
  +                        new Object[0]);
  +            }
           }
  -	return result;
  +        return result;
       }
   
       /** returns Unicode encoded string from CLOB or String column 
        */
  -    public final static String getStringOrClob(ResultSet set, String column, String defaultString) throws RuntimeException {
  -        
  +    public final static String getStringOrClob(ResultSet set, String column, String defaultString)
  +        throws RuntimeException {
  +
           String result = null;
           try {
  -            result = EsqlHelper.getStringOrClob(set,set.findColumn(column), defaultString);
  +            result = EsqlHelper.getStringOrClob(set, set.findColumn(column), defaultString);
           } catch (Exception e) {
               throw new CascadingRuntimeException("Error getting text from column " + column, e);
           }
           return result;
       }
   
  -
       /** returns Unicode encoded string from CLOB or String column 
        */
  -    public final static String getStringOrClob(ResultSet set, int column, String defaultString) throws java.lang.Exception {
  -        
  +    public final static String getStringOrClob(ResultSet set, int column, String defaultString)
  +        throws java.lang.Exception {
  +
           Reader reader = null;
           char[] buffer = null;
  -	String result = null;
  -    
  +        String result = null;
  +        Clob dbClob = null;
  +
           try {
  -            if (set.getMetaData().getColumnType(column)==java.sql.Types.CLOB) {
  -                Clob dbClob = set.getClob(column);
  +            if (set.getMetaData().getColumnType(column) == java.sql.Types.CLOB) {
  +                dbClob = set.getClob(column);
                   int length = (int) dbClob.length();
                   reader = new BufferedReader(dbClob.getCharacterStream());
                   buffer = new char[length];
  @@ -174,32 +210,54 @@
                   reader.close();
                   if (reader != null)
                       reader.close();
  -		if(buffer != null) result = new String(buffer);
  -		else if(defaultString != null && !defaultString.equals("_null_"))
  -		    result = defaultString;
  -		else result = null;
  -            } else {           
  +                if (buffer != null)
  +                    result = new String(buffer);
  +                else if (defaultString != null && !defaultString.equals("_null_"))
  +                    result = defaultString;
  +                else
  +                    result = null;
  +            } else {
                   result = set.getString(column);
  -		if(result == null &&
  -		   defaultString != null && !defaultString.equals("_null_"))
  -		    result = defaultString;
  +                if (result == null && defaultString != null && !defaultString.equals("_null_"))
  +                    result = defaultString;
               }
  -        } catch ( Exception e) {
  +        } catch (Exception e) {
               throw new CascadingRuntimeException("Error getting text from column " + column, e);
  +        } finally {
  +            // ORACLE 'temporary lob' problem patch start
  +            if (dbClob != null && dbClob.getClass().getName().equals("oracle.sql.CLOB")) {
  +                try {
  +                    if (dbClob
  +                        .getClass()
  +                        .getMethod("isTemporary", new Class[0])
  +                        .invoke(dbClob, new Object[0])
  +                        .equals(TRUE))
  +                        dbClob.getClass().getMethod("freeTemporary", new Class[0]).invoke(
  +                            dbClob,
  +                            new Object[0]);
  +                } catch (Exception e1) {
  +                    // swallow
  +                }
  +            }
           }
  -	return result;
  +        return result;
       }
   
       /** returns Unicode encoded string from CLOB or String column 
        */
  -    public final static String getStringOrClob(CallableStatement cs, int column, String defaultString) throws java.lang.Exception {
  -        
  +    public final static String getStringOrClob(
  +        CallableStatement cs,
  +        int column,
  +        String defaultString)
  +        throws java.lang.Exception {
  +
           Reader reader = null;
           char[] buffer = null;
  -	String result = null;
  -    
  +        String result = null;
  +        Clob dbClob = null;
  +
           try {
  -            Clob dbClob = cs.getClob(column);
  +            dbClob = cs.getClob(column);
               int length = (int) dbClob.length();
               reader = new BufferedReader(dbClob.getCharacterStream());
               buffer = new char[length];
  @@ -207,58 +265,79 @@
               reader.close();
               if (reader != null)
                   reader.close();
  -	    if(buffer != null) result = new String(buffer);
  -	    else if(defaultString != null && !defaultString.equals("_null_"))
  -		result = defaultString;
  -	    else result = null;
  -        } catch ( Exception e) {
  +            if (buffer != null)
  +                result = new String(buffer);
  +            else if (defaultString != null && !defaultString.equals("_null_"))
  +                result = defaultString;
  +            else
  +                result = null;
  +        } catch (Exception e) {
               throw new CascadingRuntimeException("Error getting text from column " + column, e);
  +        } finally {
  +            // ORACLE 'temporary lob' problem patch start
  +            if (dbClob != null && dbClob.getClass().getName().equals("oracle.sql.CLOB")) {
  +                try {
  +                    if (dbClob
  +                        .getClass()
  +                        .getMethod("isTemporary", new Class[0])
  +                        .invoke(dbClob, new Object[0])
  +                        .equals(TRUE))
  +                        dbClob.getClass().getMethod("freeTemporary", new Class[0]).invoke(
  +                            dbClob,
  +                            new Object[0]);
  +                } catch (Exception e1) {
  +                    // swallow
  +                }
  +            }
           }
  -	return result;
  +        return result;
       }
   
  -
       /** returns ascii string from CLOB or String column 
        */
  -      public final static String getAscii(ResultSet set, String column, String defaultString) throws RuntimeException {
  -
  -          String result = null;
  -          try {
  -              result = EsqlHelper.getAscii(set,set.findColumn(column),defaultString);
  -          } catch (Exception e) {
  -              throw new CascadingRuntimeException("Error getting ascii data for column " + column, e);
  -          }
  -          return result;
  -      }
  +    public final static String getAscii(ResultSet set, String column, String defaultString)
  +        throws RuntimeException {
   
  +        String result = null;
  +        try {
  +            result = EsqlHelper.getAscii(set, set.findColumn(column), defaultString);
  +        } catch (Exception e) {
  +            throw new CascadingRuntimeException("Error getting ascii data for column " + column, e);
  +        }
  +        return result;
  +    }
   
       /** returns ascii string from CLOB or String column 
        */
  -      public final static String getAscii(ResultSet set, int column, String defaultString) {
  +    public final static String getAscii(ResultSet set, int column, String defaultString) {
           InputStream asciiStream = null;
           String result = null;
  +        Clob dbClob = null;
   
           try {
               if (set.getMetaData().getColumnType(column) == Types.CLOB) {
                   byte[] buffer = null;
  -                Clob dbClob = set.getClob(column);
  +                dbClob = set.getClob(column);
                   int length = (int) dbClob.length();
                   asciiStream = new BufferedInputStream(dbClob.getAsciiStream());
                   buffer = new byte[length];
                   asciiStream.read(buffer);
                   asciiStream.close();
  -		if(buffer != null) result = new String(buffer);
  -		else if(defaultString != null && !defaultString.equals("_null_"))
  -		    result = defaultString;
  -		else result = null;
  +                if (buffer != null)
  +                    result = new String(buffer);
  +                else if (defaultString != null && !defaultString.equals("_null_"))
  +                    result = defaultString;
  +                else
  +                    result = null;
               } else {
                   result = set.getString(column);
  -		if(result == null &&
  -                   defaultString != null && !defaultString.equals("_null_"))
  -		    result = defaultString;
  +                if (result == null && defaultString != null && !defaultString.equals("_null_"))
  +                    result = defaultString;
               }
           } catch (Exception e) {
  -            throw new CascadingRuntimeException("Error getting ascii data from column " + column, e);
  +            throw new CascadingRuntimeException(
  +                "Error getting ascii data from column " + column,
  +                e);
           } finally {
               if (asciiStream != null) {
                   try {
  @@ -267,29 +346,48 @@
                       throw new CascadingRuntimeException("Error closing clob stream", ase);
                   }
               }
  +            // ORACLE 'temporary lob' problem patch start
  +            if (dbClob != null && dbClob.getClass().getName().equals("oracle.sql.CLOB")) {
  +                try {
  +                    if (dbClob
  +                        .getClass()
  +                        .getMethod("isTemporary", new Class[0])
  +                        .invoke(dbClob, new Object[0])
  +                        .equals(TRUE))
  +                        dbClob.getClass().getMethod("freeTemporary", new Class[0]).invoke(
  +                            dbClob,
  +                            new Object[0]);
  +                } catch (Exception e1) {
  +                    // swallow
  +                }
  +            }
  +
           }
  -        
  +
           return result;
  -      }
  +    }
   
       /** returns ascii string from CLOB or String column 
        */
       public final static String getAscii(CallableStatement cs, int column, String defaultString) {
           InputStream asciiStream = null;
           String result = null;
  +        Clob dbClob = null;
   
           try {
               byte[] buffer = null;
  -            Clob dbClob = cs.getClob(column);
  +            dbClob = cs.getClob(column);
               int length = (int) dbClob.length();
               asciiStream = new BufferedInputStream(dbClob.getAsciiStream());
               buffer = new byte[length];
               asciiStream.read(buffer);
               asciiStream.close();
  -	    if(buffer != null) result = new String(buffer);
  -	    else if(defaultString != null && !defaultString.equals("_null_"))
  -		result = defaultString;
  -	    else result = null;
  +            if (buffer != null)
  +                result = new String(buffer);
  +            else if (defaultString != null && !defaultString.equals("_null_"))
  +                result = defaultString;
  +            else
  +                result = null;
           } catch (Exception e) {
               throw new CascadingRuntimeException("Error getting ascii data for column " + column, e);
           } finally {
  @@ -300,24 +398,42 @@
                       throw new CascadingRuntimeException("Error closing clob stream", ase);
                   }
               }
  +            // ORACLE 'temporary lob' problem patch start
  +            if (dbClob != null && dbClob.getClass().getName().equals("oracle.sql.CLOB")) {
  +                try {
  +                    if (dbClob
  +                        .getClass()
  +                        .getMethod("isTemporary", new Class[0])
  +                        .invoke(dbClob, new Object[0])
  +                        .equals(TRUE))
  +                        dbClob.getClass().getMethod("freeTemporary", new Class[0]).invoke(
  +                            dbClob,
  +                            new Object[0]);
  +                } catch (Exception e1) {
  +                    // swallow
  +                }
  +            }
           }
  -        
  +
           return result;
  -      }
  +    }
   
  -      public final static String getStringFromByteArray(byte[] bytes, String encoding, String defaultString) {
  +    public final static String getStringFromByteArray(
  +        byte[] bytes,
  +        String encoding,
  +        String defaultString) {
           if (bytes != null) {
               try {
  -                return new String(bytes,encoding);
  +                return new String(bytes, encoding);
               } catch (java.io.UnsupportedEncodingException uee) {
                   throw new CascadingRuntimeException("Unsupported Encoding Exception", uee);
               }
  +        } else {
  +            if (defaultString != null && !defaultString.equals("_null_"))
  +                return defaultString;
  +            else
  +                return null; /* before was "" but null is more consequent */
           }
  -        else {
  -	    if(defaultString != null && !defaultString.equals("_null_"))
  -		  return defaultString;
  -            else return null; /* before was "" but null is more consequent */
  -        }
  -      }
  +    }
   
   }