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