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      protected String getDeleteAllCommandSuffix(IDatabaseConnection connection) throws SQLException
75      {
76          return "";
77      }
78  
79      ////////////////////////////////////////////////////////////////////////////
80      // DatabaseOperation class
81  
82      public void execute(IDatabaseConnection connection, IDataSet dataSet)
83              throws DatabaseUnitException, SQLException
84      {
85          logger.debug("execute(connection={}, dataSet={}) - start", connection, dataSet);
86  
87          IDataSet databaseDataSet = connection.createDataSet();
88  
89          DatabaseConfig databaseConfig = connection.getConfig();
90          IStatementFactory statementFactory = (IStatementFactory)databaseConfig.getProperty(DatabaseConfig.PROPERTY_STATEMENT_FACTORY);
91          IBatchStatement statement = statementFactory.createBatchStatement(connection);
92          try
93          {
94              int count = 0;
95              
96              final Deque<String> tableNames = new ArrayDeque<>();
97              final Set<String> tablesSeen = new HashSet<>();
98              ITableIterator iterator = dataSet.iterator();
99              while (iterator.next())
100             {
101                 String tableName = iterator.getTableMetaData().getTableName();
102                 if (!tablesSeen.contains(tableName))
103                 {
104                     tableNames.push(tableName);
105                     tablesSeen.add(tableName);
106                 }
107             }
108 
109             // delete tables once each in reverse order of seeing them.
110             while (!tableNames.isEmpty())
111             {
112                 String tableName = (String)tableNames.pop();
113 
114                 // Use database table name. Required to support case sensitive database.
115                 ITableMetaData databaseMetaData = databaseDataSet.getTableMetaData(tableName);
116                 tableName = databaseMetaData.getTableName();
117 
118                 final StringBuilder sqlBuffer = new StringBuilder(128);
119                 sqlBuffer.append(getDeleteAllCommand());
120                 sqlBuffer.append(getQualifiedName(connection.getSchema(), tableName, connection));
121                 sqlBuffer.append(getDeleteAllCommandSuffix(connection));
122                 String sql = sqlBuffer.toString();
123                 statement.addBatch(sql);
124 
125                 if(logger.isDebugEnabled())
126                     logger.debug("Added SQL: {}", sql);
127                 
128                 count++;
129             }
130 
131             if (count > 0)
132             {
133                 statement.executeBatch();
134                 statement.clearBatch();
135             }
136         }
137         finally
138         {
139             statement.close();
140         }
141     }
142 }