TruncateTableOperation.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.dataset.IDataSet;

  28. import java.sql.SQLException;

  29. /**
  30.  * Truncate tables present in the specified dataset. If the dataset does not
  31.  * contains a particular table, but that table exists in the database,
  32.  * the database table is not affected. Table are truncated in
  33.  * reverse sequence.
  34.  * <p>
  35.  * This operation has the same effect of as {@link DeleteAllOperation}.
  36.  * TruncateTableOperation is faster, and it is non-logged, meaning it cannot be
  37.  * rollback. DeleteAllOperation is more portable because not all database vendor
  38.  * support TRUNCATE_TABLE TABLE statement.
  39.  *
  40.  * @author Manuel Laflamme
  41.  * @since Apr 10, 2003
  42.  * @version $Revision$
  43.  * @see DeleteAllOperation
  44.  */
  45. public class TruncateTableOperation extends DeleteAllOperation
  46. {

  47.     /**
  48.      * Logger for this class
  49.      */
  50.     private static final Logger logger = LoggerFactory.getLogger(TruncateTableOperation.class);

  51.     TruncateTableOperation()
  52.     {
  53.     }

  54.     ////////////////////////////////////////////////////////////////////////////
  55.     // DeleteAllOperation class

  56.     protected String getDeleteAllCommand()
  57.     {
  58.         return "truncate table ";
  59.     }

  60.     ////////////////////////////////////////////////////////////////////////////
  61.     // DatabaseOperation class

  62.     public void execute(IDatabaseConnection connection, IDataSet dataSet)
  63.             throws DatabaseUnitException, SQLException
  64.     {
  65.         logger.debug("execute(connection={}, dataSet={}) - start", connection, dataSet);

  66.         // Patch to make it work with MS SQL Server
  67.         DatabaseConfig config = connection.getConfig();
  68.         boolean oldValue = config.getFeature(DatabaseConfig.FEATURE_BATCHED_STATEMENTS);
  69.         try
  70.         {
  71.             config.setFeature(DatabaseConfig.FEATURE_BATCHED_STATEMENTS, false);
  72.             super.execute(connection, dataSet);
  73.         }
  74.         finally
  75.         {
  76.             config.setFeature(DatabaseConfig.FEATURE_BATCHED_STATEMENTS, oldValue);
  77.         }
  78.     }
  79. }