View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  package org.dbunit.ext.oracle;
22  
23  import oracle.sql.BLOB;
24  
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import org.dbunit.dataset.datatype.BlobDataType;
29  import org.dbunit.dataset.datatype.TypeCastException;
30  
31  import java.io.IOException;
32  import java.io.OutputStream;
33  import java.sql.Connection;
34  import java.sql.PreparedStatement;
35  import java.sql.ResultSet;
36  import java.sql.SQLException;
37  
38  /**
39   * @author Manuel Laflamme
40   * @author Last changed by: $Author$
41   * @version $Revision$ $Date$
42   * @since Feb 2, 2004
43   */
44  public class OracleBlobDataType extends BlobDataType
45  {
46  
47      /**
48       * Logger for this class
49       */
50      private static final Logger logger = LoggerFactory.getLogger(OracleBlobDataType.class);
51  
52      public Object getSqlValue(int column, ResultSet resultSet) throws SQLException, TypeCastException
53      {
54      	if(logger.isDebugEnabled())
55      		logger.debug("getSqlValue(column={}, resultSet={}) - start", new Integer(column), resultSet);
56  
57          return typeCast(resultSet.getBlob(column));
58      }
59  
60      public void setSqlValue(Object value, int column, PreparedStatement statement)
61              throws SQLException, TypeCastException
62      {
63      	if(logger.isDebugEnabled())
64      		logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
65      				new Object[]{value, new Integer(column), statement} );
66  
67          statement.setObject(column, getBlob(value, statement.getConnection()));
68      }
69  
70      private Object getBlob(Object value, Connection connection) throws TypeCastException
71      {
72          logger.debug("getBlob(value={}, connection={}) - start", value, connection);
73  
74          oracle.sql.BLOB tempBlob = null;
75          try
76          {
77              tempBlob = oracle.sql.BLOB.createTemporary(connection, true, oracle.sql.BLOB.DURATION_SESSION);
78              tempBlob.open(oracle.sql.BLOB.MODE_READWRITE);
79              OutputStream tempBlobOutputStream = tempBlob.getBinaryOutputStream();
80  
81              // Write the data into the temporary BLOB
82              tempBlobOutputStream.write((byte[])typeCast(value));
83  
84              // Flush and close the stream
85              tempBlobOutputStream.flush();
86              tempBlobOutputStream.close();
87  
88              // Close the temporary Blob
89              tempBlob.close();
90          }
91          catch (SQLException e)
92          {
93              // JH_TODO: shouldn't freeTemporary be called in finally {} ?
94              // It wasn't done like that in the original reflection-styled DbUnit code.
95              freeTemporaryBlob(tempBlob);
96              throw new TypeCastException(value, this, e);
97          }
98          catch (IOException e)
99          {
100             freeTemporaryBlob(tempBlob);
101             throw new TypeCastException(value, this, e);
102         }
103 
104         return tempBlob;
105     }
106 
107 
108     private void freeTemporaryBlob(oracle.sql.BLOB tempBlob) throws TypeCastException
109     {
110         logger.debug("freeTemporaryBlob(tempBlob={}) - start", tempBlob);
111 
112         if (tempBlob == null)
113         {
114             return;
115         }
116 
117         try
118         {
119             tempBlob.freeTemporary();
120         }
121         catch (SQLException e)
122         {
123             throw new TypeCastException("Error freeing Oracle BLOB", e);
124         }
125     }
126 
127 }