OracleClobDataType.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 java.io.Closeable;
  23. import java.io.IOException;
  24. import java.io.Writer;
  25. import java.sql.Connection;
  26. import java.sql.PreparedStatement;
  27. import java.sql.ResultSet;
  28. import java.sql.SQLException;

  29. import org.dbunit.dataset.datatype.ClobDataType;
  30. import org.dbunit.dataset.datatype.TypeCastException;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;

  33. /**
  34.  * @author Manuel Laflamme
  35.  * @author Last changed by: $Author$
  36.  * @version $Revision$ $Date$
  37.  * @since Jan 12, 2004
  38.  */
  39. public class OracleClobDataType extends ClobDataType
  40. {

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

  46.     @Override
  47.     public Object getSqlValue(final int column, final ResultSet resultSet)
  48.             throws SQLException, TypeCastException
  49.     {
  50.         logger.debug("getSqlValue(column={}, resultSet={}) - start", column, resultSet);

  51.         return typeCast(resultSet.getClob(column));
  52.     }

  53.     @Override
  54.     public void setSqlValue(final Object value, final int column,
  55.             final PreparedStatement statement)
  56.             throws SQLException, TypeCastException
  57.     {
  58.         logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
  59.                 value, column, statement);

  60.         statement.setObject(column, getClob(value, statement.getConnection()));
  61.     }

  62.     protected Object getClob(final Object value, final Connection connection)
  63.             throws TypeCastException
  64.     {
  65.         logger.debug("getClob(value={}, connection={}) - start", value,
  66.                 connection);

  67.         Writer tempClobWriter = null;
  68.         try
  69.         {
  70.             final java.sql.Clob tempClob = connection.createClob();
  71.             tempClobWriter = tempClob.setCharacterStream(1);

  72.             // Write the data into the temporary CLOB
  73.             tempClobWriter.write((String) typeCast(value));

  74.             // Flush and close the stream
  75.             tempClobWriter.flush();
  76.             return tempClob;
  77.         } catch (IOException | SQLException e)
  78.         {
  79.             throw new TypeCastException(value, this, e);
  80.         } finally
  81.         {
  82.             closeQuietly(tempClobWriter);
  83.         }
  84.     }

  85.     private void closeQuietly(final Closeable c)
  86.     {
  87.         if (c != null)
  88.         {
  89.             try
  90.             {
  91.                 c.close();
  92.             } catch (final IOException ignored)
  93.             {
  94.                 // ignore
  95.             }
  96.         }
  97.     }
  98. }