OracleBlobDataType.java

  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. import oracle.sql.BLOB;

  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;

  25. import org.dbunit.dataset.datatype.BlobDataType;
  26. import org.dbunit.dataset.datatype.TypeCastException;

  27. import java.io.IOException;
  28. import java.io.OutputStream;
  29. import java.sql.Connection;
  30. import java.sql.PreparedStatement;
  31. import java.sql.ResultSet;
  32. import java.sql.SQLException;

  33. /**
  34.  * @author Manuel Laflamme
  35.  * @author Last changed by: $Author$
  36.  * @version $Revision$ $Date$
  37.  * @since Feb 2, 2004
  38.  */
  39. public class OracleBlobDataType extends BlobDataType
  40. {

  41.     /**
  42.      * Logger for this class
  43.      */
  44.     private static final Logger logger = LoggerFactory.getLogger(OracleBlobDataType.class);

  45.     public Object getSqlValue(int column, ResultSet resultSet) throws SQLException, TypeCastException
  46.     {
  47.         logger.debug("getSqlValue(column={}, resultSet={}) - start", column, resultSet);

  48.         return typeCast(resultSet.getBlob(column));
  49.     }

  50.     public void setSqlValue(Object value, int column, PreparedStatement statement)
  51.             throws SQLException, TypeCastException
  52.     {
  53.         logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
  54.             value, column, statement);

  55.         statement.setObject(column, getBlob(value, statement.getConnection()));
  56.     }

  57.     private Object getBlob(Object value, Connection connection) throws TypeCastException
  58.     {
  59.         logger.debug("getBlob(value={}, connection={}) - start", value, connection);

  60.         oracle.sql.BLOB tempBlob = null;
  61.         try
  62.         {
  63.             tempBlob = oracle.sql.BLOB.createTemporary(connection, true, oracle.sql.BLOB.DURATION_SESSION);
  64.             tempBlob.open(oracle.sql.BLOB.MODE_READWRITE);
  65.             OutputStream tempBlobOutputStream = tempBlob.getBinaryOutputStream();

  66.             // Write the data into the temporary BLOB
  67.             tempBlobOutputStream.write((byte[])typeCast(value));

  68.             // Flush and close the stream
  69.             tempBlobOutputStream.flush();
  70.             tempBlobOutputStream.close();

  71.             // Close the temporary Blob
  72.             tempBlob.close();
  73.         }
  74.         catch (SQLException e)
  75.         {
  76.             // JH_TODO: shouldn't freeTemporary be called in finally {} ?
  77.             // It wasn't done like that in the original reflection-styled DbUnit code.
  78.             freeTemporaryBlob(tempBlob);
  79.             throw new TypeCastException(value, this, e);
  80.         }
  81.         catch (IOException e)
  82.         {
  83.             freeTemporaryBlob(tempBlob);
  84.             throw new TypeCastException(value, this, e);
  85.         }

  86.         return tempBlob;
  87.     }


  88.     private void freeTemporaryBlob(oracle.sql.BLOB tempBlob) throws TypeCastException
  89.     {
  90.         logger.debug("freeTemporaryBlob(tempBlob={}) - start", tempBlob);

  91.         if (tempBlob == null)
  92.         {
  93.             return;
  94.         }

  95.         try
  96.         {
  97.             tempBlob.freeTemporary();
  98.         }
  99.         catch (SQLException e)
  100.         {
  101.             throw new TypeCastException("Error freeing Oracle BLOB", e);
  102.         }
  103.     }

  104. }