View Javadoc
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  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import org.dbunit.DatabaseUnitException;
27  import org.dbunit.database.DatabaseConfig;
28  import org.dbunit.database.IDatabaseConnection;
29  import org.dbunit.database.statement.IBatchStatement;
30  import org.dbunit.database.statement.IStatementFactory;
31  import org.dbunit.dataset.IDataSet;
32  import org.dbunit.dataset.ITableIterator;
33  import org.dbunit.dataset.ITableMetaData;
34  
35  import java.sql.SQLException;
36  import java.util.HashSet;
37  import java.util.Set;
38  import java.util.Stack;
39  
40  /**
41   * Deletes all rows of tables present in the specified dataset. If the dataset
42   * does not contains a particular table, but that table exists in the database,
43   * the database table is not affected. Table are truncated in
44   * reverse sequence.
45   * <p/>
46   * This operation has the same effect of as {@link TruncateTableOperation}.
47   * TruncateTableOperation is faster, and it is non-logged, meaning it cannot be
48   * rollback. DeleteAllOperation is more portable because not all database vendor
49   * support TRUNCATE_TABLE TABLE statement.
50   *
51   * @author Manuel Laflamme
52   * @version $Revision$
53   * @see TruncateTableOperation
54   * @since Feb 18, 2002
55   */
56  public class DeleteAllOperation extends AbstractOperation
57  {
58  
59      /**
60       * Logger for this class
61       */
62      private static final Logger logger = LoggerFactory.getLogger(DeleteAllOperation.class);
63  
64      DeleteAllOperation()
65      {
66      }
67  
68      protected String getDeleteAllCommand()
69      {
70          return "delete from ";
71      }
72  
73      ////////////////////////////////////////////////////////////////////////////
74      // DatabaseOperation class
75  
76      public void execute(IDatabaseConnection connection, IDataSet dataSet)
77              throws DatabaseUnitException, SQLException
78      {
79          logger.debug("execute(connection={}, dataSet={}) - start", connection, dataSet);
80  
81          IDataSet databaseDataSet = connection.createDataSet();
82  
83          DatabaseConfig databaseConfig = connection.getConfig();
84          IStatementFactory statementFactory = (IStatementFactory)databaseConfig.getProperty(DatabaseConfig.PROPERTY_STATEMENT_FACTORY);
85          IBatchStatement statement = statementFactory.createBatchStatement(connection);
86          try
87          {
88              int count = 0;
89              
90              Stack tableNames = new Stack();
91              Set tablesSeen = new HashSet();
92              ITableIterator iterator = dataSet.iterator();
93              while (iterator.next())
94              {
95                  String tableName = iterator.getTableMetaData().getTableName();
96                  if (!tablesSeen.contains(tableName))
97                  {
98                      tableNames.push(tableName);
99                      tablesSeen.add(tableName);
100                 }
101             }
102 
103             // delete tables once each in reverse order of seeing them.
104             while (!tableNames.isEmpty())
105             {
106                 String tableName = (String)tableNames.pop();
107 
108                 // Use database table name. Required to support case sensitive database.
109                 ITableMetaData databaseMetaData = databaseDataSet.getTableMetaData(tableName);
110                 tableName = databaseMetaData.getTableName();
111 
112                 StringBuffer sqlBuffer = new StringBuffer(128);
113                 sqlBuffer.append(getDeleteAllCommand());
114                 sqlBuffer.append(getQualifiedName(connection.getSchema(), tableName, connection));
115                 String sql = sqlBuffer.toString();
116                 statement.addBatch(sql);
117 
118                 if(logger.isDebugEnabled())
119                     logger.debug("Added SQL: {}", sql);
120                 
121                 count++;
122             }
123 
124             if (count > 0)
125             {
126                 statement.executeBatch();
127                 statement.clearBatch();
128             }
129         }
130         finally
131         {
132             statement.close();
133         }
134     }
135 }