haul        2002/07/05 07:43:14

  Modified:    src/java/org/apache/cocoon/components/language/markup/xsp
                        Tag: cocoon_2_0_3_branch EsqlHelper.java
  Log:
  Support xLOBs with CallableStatements
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.7.2.2   +117 -1    
xml-cocoon2/src/java/org/apache/cocoon/components/language/markup/xsp/EsqlHelper.java
  
  Index: EsqlHelper.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/language/markup/xsp/EsqlHelper.java,v
  retrieving revision 1.7.2.1
  retrieving revision 1.7.2.2
  diff -u -r1.7.2.1 -r1.7.2.2
  --- EsqlHelper.java   4 Jun 2002 09:37:17 -0000       1.7.2.1
  +++ EsqlHelper.java   5 Jul 2002 14:43:14 -0000       1.7.2.2
  @@ -53,8 +53,10 @@
   import java.io.BufferedInputStream;
   import java.io.BufferedReader;
   import java.io.Reader;
  +import java.sql.CallableStatement;
   import java.sql.ResultSet;
   import java.io.InputStream;
  +import java.sql.Blob;
   import java.sql.Clob;
   import java.sql.Types;
   
  @@ -70,6 +72,71 @@
   public class EsqlHelper {
   
   
  +    /** 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));
  +        } catch (Exception e) {
  +            throw new RuntimeException("Error getting blob data: " + 
e.getMessage());
  +        }
  +        return result;
  +    }
  +
  +    /** returns byte array from BLOB
  +     */
  +    public final static byte[] getBlob(ResultSet set, int column) throws 
java.lang.Exception {
  +        
  +        InputStream reader = null;
  +        byte[] buffer = null;
  +    
  +        try {
  +            if (set.getMetaData().getColumnType(column)==java.sql.Types.BLOB) {
  +                Blob dbBlob = set.getBlob(column);
  +                int length = (int) dbBlob.length();
  +                reader = dbBlob.getBinaryStream();
  +                buffer = new byte[length];
  +                reader.read(buffer);
  +                reader.close();
  +                if (reader != null)
  +                    reader.close();
  +                if (buffer == null)
  +                    return null;
  +                return buffer;
  +            } else {           
  +                return set.getString(column).getBytes();
  +            }
  +        } catch ( Exception e) {
  +            throw new RuntimeException("Error getting blob data: " + 
e.getMessage());
  +        }
  +    }
  +
  +    /** returns byte array from BLOB
  +     */
  +    public final static byte[] getBlob(CallableStatement cs, int column) throws 
java.lang.Exception {
  +        
  +        InputStream reader = null;
  +        byte[] buffer = null;
  +    
  +        try {
  +            Blob dbBlob = cs.getBlob(column);
  +            int length = (int) dbBlob.length();
  +            reader = dbBlob.getBinaryStream();
  +            buffer = new byte[length];
  +            reader.read(buffer);
  +            reader.close();
  +            if (reader != null)
  +                reader.close();
  +            if (buffer == null)
  +                return null;
  +            return buffer;
  +        } catch ( Exception e) {
  +            throw new RuntimeException("Error getting blob data: " + 
e.getMessage());
  +        }
  +    }
  +
       /** returns Unicode encoded string from CLOB or String column 
        */
       public final static String getStringOrClob(ResultSet set, String column) throws 
RuntimeException {
  @@ -112,6 +179,30 @@
           }
       }
   
  +    /** returns Unicode encoded string from CLOB or String column 
  +     */
  +    public final static String getStringOrClob(CallableStatement cs, int column) 
throws java.lang.Exception {
  +        
  +        Reader reader = null;
  +        char[] buffer = null;
  +    
  +        try {
  +            Clob dbClob = cs.getClob(column);
  +            int length = (int) dbClob.length();
  +            reader = new BufferedReader(dbClob.getCharacterStream());
  +            buffer = new char[length];
  +            reader.read(buffer);
  +            reader.close();
  +            if (reader != null)
  +                reader.close();
  +            if (buffer == null)
  +                return "";
  +            return new String(buffer);
  +        } catch ( Exception e) {
  +            throw new RuntimeException("Error getting clob data: " + 
e.getMessage());
  +        }
  +    }
  +
   
       /** returns ascii string from CLOB or String column 
        */
  @@ -154,6 +245,31 @@
               }
           }
           
  +        return result;
  +      }
  +
  +    /** returns ascii string from CLOB or String column 
  +     */
  +    public final static String getAscii(CallableStatement cs, int column) {
  +        InputStream asciiStream = null;
  +        String result = null;
  +
  +        try {
  +            byte[] buffer = null;
  +            Clob dbClob = cs.getClob(column);
  +            int length = (int) dbClob.length();
  +            asciiStream = new BufferedInputStream(dbClob.getAsciiStream());
  +            buffer = new byte[length];
  +            asciiStream.read(buffer);
  +            asciiStream.close();
  +            result = (buffer!=null? new String(buffer) : null);
  +        } catch (Exception e) {
  +            throw new RuntimeException("Error getting clob data: " + 
e.getMessage());
  +        } finally {
  +            if (asciiStream != null) try {asciiStream.close();} catch (Exception 
ase) {
  +                throw new RuntimeException("Error closing clob stream: " + 
ase.getMessage());
  +            }
  +        }
           
           return result;
         }
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to