DeleteAllOperation.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 org.dbunit.DatabaseUnitException;
  25. import org.dbunit.database.DatabaseConfig;
  26. import org.dbunit.database.IDatabaseConnection;
  27. import org.dbunit.database.statement.IBatchStatement;
  28. import org.dbunit.database.statement.IStatementFactory;
  29. import org.dbunit.dataset.IDataSet;
  30. import org.dbunit.dataset.ITableIterator;
  31. import org.dbunit.dataset.ITableMetaData;

  32. import java.sql.SQLException;
  33. import java.util.ArrayDeque;
  34. import java.util.Deque;
  35. import java.util.HashSet;
  36. import java.util.Set;

  37. /**
  38.  * Deletes all rows of tables present in the specified dataset. If the dataset
  39.  * does not contains a particular table, but that table exists in the database,
  40.  * the database table is not affected. Table are truncated in
  41.  * reverse sequence.
  42.  * <p/>
  43.  * This operation has the same effect of as {@link TruncateTableOperation}.
  44.  * TruncateTableOperation is faster, and it is non-logged, meaning it cannot be
  45.  * rollback. DeleteAllOperation is more portable because not all database vendor
  46.  * support TRUNCATE_TABLE TABLE statement.
  47.  *
  48.  * @author Manuel Laflamme
  49.  * @version $Revision$
  50.  * @see TruncateTableOperation
  51.  * @since Feb 18, 2002
  52.  */
  53. public class DeleteAllOperation extends AbstractOperation
  54. {

  55.     /**
  56.      * Logger for this class
  57.      */
  58.     private static final Logger logger = LoggerFactory.getLogger(DeleteAllOperation.class);

  59.     DeleteAllOperation()
  60.     {
  61.     }

  62.     protected String getDeleteAllCommand()
  63.     {
  64.         return "delete from ";
  65.     }

  66.     ////////////////////////////////////////////////////////////////////////////
  67.     // DatabaseOperation class

  68.     public void execute(IDatabaseConnection connection, IDataSet dataSet)
  69.             throws DatabaseUnitException, SQLException
  70.     {
  71.         logger.debug("execute(connection={}, dataSet={}) - start", connection, dataSet);

  72.         IDataSet databaseDataSet = connection.createDataSet();

  73.         DatabaseConfig databaseConfig = connection.getConfig();
  74.         IStatementFactory statementFactory = (IStatementFactory)databaseConfig.getProperty(DatabaseConfig.PROPERTY_STATEMENT_FACTORY);
  75.         IBatchStatement statement = statementFactory.createBatchStatement(connection);
  76.         try
  77.         {
  78.             int count = 0;
  79.            
  80.             final Deque<String> tableNames = new ArrayDeque<>();
  81.             final Set<String> tablesSeen = new HashSet<>();
  82.             ITableIterator iterator = dataSet.iterator();
  83.             while (iterator.next())
  84.             {
  85.                 String tableName = iterator.getTableMetaData().getTableName();
  86.                 if (!tablesSeen.contains(tableName))
  87.                 {
  88.                     tableNames.push(tableName);
  89.                     tablesSeen.add(tableName);
  90.                 }
  91.             }

  92.             // delete tables once each in reverse order of seeing them.
  93.             while (!tableNames.isEmpty())
  94.             {
  95.                 String tableName = (String)tableNames.pop();

  96.                 // Use database table name. Required to support case sensitive database.
  97.                 ITableMetaData databaseMetaData = databaseDataSet.getTableMetaData(tableName);
  98.                 tableName = databaseMetaData.getTableName();

  99.                 final StringBuilder sqlBuffer = new StringBuilder(128);
  100.                 sqlBuffer.append(getDeleteAllCommand());
  101.                 sqlBuffer.append(getQualifiedName(connection.getSchema(), tableName, connection));
  102.                 String sql = sqlBuffer.toString();
  103.                 statement.addBatch(sql);

  104.                 if(logger.isDebugEnabled())
  105.                     logger.debug("Added SQL: {}", sql);
  106.                
  107.                 count++;
  108.             }

  109.             if (count > 0)
  110.             {
  111.                 statement.executeBatch();
  112.                 statement.clearBatch();
  113.             }
  114.         }
  115.         finally
  116.         {
  117.             statement.close();
  118.         }
  119.     }
  120. }