DeleteOperation.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.operation;

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

  24. import java.util.BitSet;

  25. import org.dbunit.DatabaseUnitException;
  26. import org.dbunit.database.IDatabaseConnection;
  27. import org.dbunit.dataset.Column;
  28. import org.dbunit.dataset.DataSetException;
  29. import org.dbunit.dataset.IDataSet;
  30. import org.dbunit.dataset.ITableIterator;
  31. import org.dbunit.dataset.ITableMetaData;
  32. import org.dbunit.dataset.NoPrimaryKeyException;

  33. /**
  34.  * Deletes only the dataset contents from the database. This operation does not
  35.  * delete the entire table contents but only data that are present in the
  36.  * dataset.
  37.  *
  38.  * @author Manuel Laflamme
  39.  * @version $Revision$
  40.  * @since Feb 19, 2002
  41.  */
  42. public class DeleteOperation extends AbstractBatchOperation
  43. {

  44.     /**
  45.      * Logger for this class
  46.      */
  47.     private static final Logger logger = LoggerFactory.getLogger(DeleteOperation.class);

  48.     DeleteOperation()
  49.     {
  50.         _reverseRowOrder = true;
  51.     }

  52.     ////////////////////////////////////////////////////////////////////////////
  53.     // AbstractBatchOperation class

  54.     protected ITableIterator iterator(IDataSet dataSet) throws DatabaseUnitException
  55.     {
  56.         logger.debug("iterator(dataSet={}) - start", dataSet);
  57.         return dataSet.reverseIterator();
  58.     }

  59.     public OperationData getOperationData(ITableMetaData metaData, BitSet ignoreMapping, IDatabaseConnection connection) throws DataSetException
  60.     {
  61.         if (logger.isDebugEnabled())
  62.         {
  63.             logger.debug("getOperationData(metaData={}, ignoreMapping={}, connection={}) - start",
  64.                     metaData, ignoreMapping, connection);
  65.         }

  66.         // cannot construct where clause if no primary key
  67.         Column[] primaryKeys = metaData.getPrimaryKeys();
  68.         if (primaryKeys.length == 0)
  69.         {
  70.             throw new NoPrimaryKeyException(metaData.getTableName());
  71.         }

  72.         // delete from
  73.         final StringBuilder sqlBuffer = new StringBuilder(128);
  74.         sqlBuffer.append("delete from ");
  75.         sqlBuffer.append(getQualifiedName(connection.getSchema(),
  76.                 metaData.getTableName(), connection));

  77.         // where
  78.         sqlBuffer.append(" where ");
  79.         for (int i = 0; i < primaryKeys.length; i++)
  80.         {
  81.             // escape column name
  82.             String columnName = getQualifiedName(null,
  83.                     primaryKeys[i].getColumnName(), connection);
  84.             sqlBuffer.append(columnName);

  85.             sqlBuffer.append(" = ?");
  86.             if (i + 1 < primaryKeys.length)
  87.             {
  88.                 sqlBuffer.append(" and ");
  89.             }
  90.         }

  91.         return new OperationData(sqlBuffer.toString(), primaryKeys);
  92.     }

  93. }