OracleXMLTypeDataType.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.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.sql.SQLXML;
  26. import java.sql.Types;

  27. import org.dbunit.dataset.datatype.BlobDataType;
  28. import org.dbunit.dataset.datatype.TypeCastException;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;

  31. import oracle.jdbc.OraclePreparedStatement;
  32. import oracle.jdbc.OracleResultSet;

  33. /**
  34.  *
  35.  * TODO UnitTests are completely missing
  36.  *
  37.  * @author Phil Barr
  38.  * @author Last changed by: $Author$
  39.  * @version $Revision$ $Date$
  40.  * @since 2.4.0
  41.  */
  42. public class OracleXMLTypeDataType extends BlobDataType
  43. {
  44.     private final Logger log = LoggerFactory.getLogger(getClass());

  45.     OracleXMLTypeDataType()
  46.     {
  47.         super("SQLXML", Types.SQLXML);
  48.     }

  49.     @Override
  50.     public Object getSqlValue(final int column, final ResultSet resultSet)
  51.             throws SQLException, TypeCastException
  52.     {
  53.         byte[] data = null;
  54.         final OracleResultSet oracleResultSet =
  55.                 resultSet.unwrap(OracleResultSet.class);
  56.         final SQLXML sqlXml = oracleResultSet.getSQLXML(column);
  57.         if (sqlXml != null)
  58.         {
  59.             data = sqlXml.getString().getBytes();
  60.         }

  61.         // return the byte data (using typeCast to cast it to Base64 notation)
  62.         final Object typeCast = typeCast(data);
  63.         final String string =
  64.                 typeCast == null ? null : new String((byte[]) typeCast);
  65.         log.trace("getSqlValue: column={}, data={}, typeCast={}, string={}",
  66.                 column, data, typeCast, string);

  67.         return typeCast;
  68.     }

  69.     @Override
  70.     public void setSqlValue(final Object value, final int column,
  71.             final PreparedStatement statement)
  72.             throws SQLException, TypeCastException
  73.     {
  74.         final OraclePreparedStatement oraclePreparedStatement =
  75.                 statement.unwrap(OraclePreparedStatement.class);
  76.         final SQLXML sqlXmlValue =
  77.                 oraclePreparedStatement.getConnection().createSQLXML();

  78.         // XML document in the parameter is Base64 encoded (it is entered in XML
  79.         // parameter)
  80.         final byte[] typeCast = (byte[]) typeCast(value);
  81.         final String string = new String(typeCast);

  82.         log.trace("setSqlValue: column={}, value={}, typeCast={}, string={}",
  83.                 column, value, typeCast, string);

  84.         sqlXmlValue.setString(string);
  85.         oraclePreparedStatement.setSQLXML(column, sqlXmlValue);
  86.     }

  87.     @Override
  88.     public String getSqlTypeName()
  89.     {
  90.         return "SYS.XMLTYPE";
  91.     }
  92. }