KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > operation > DeleteAllOperation


1 /*
2  * The DbUnit Database Testing Framework
3  * Copyright (C)2002-2004, DbUnit.org
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */

20
21 package org.dbunit.operation;
22
23 import org.dbunit.DatabaseUnitException;
24 import org.dbunit.database.DatabaseConfig;
25 import org.dbunit.database.IDatabaseConnection;
26 import org.dbunit.database.statement.IBatchStatement;
27 import org.dbunit.database.statement.IStatementFactory;
28 import org.dbunit.dataset.IDataSet;
29 import org.dbunit.dataset.ITableIterator;
30 import org.dbunit.dataset.ITableMetaData;
31
32 import java.sql.SQLException JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.Stack JavaDoc;
36
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: 1.19 $
50  * @see TruncateTableOperation
51  * @since Feb 18, 2002
52  */

53 public class DeleteAllOperation extends AbstractOperation
54 {
55     DeleteAllOperation()
56     {
57     }
58
59     protected String JavaDoc getDeleteAllCommand()
60     {
61         return "delete from ";
62     }
63
64     ////////////////////////////////////////////////////////////////////////////
65
// DatabaseOperation class
66

67     public void execute(IDatabaseConnection connection, IDataSet dataSet)
68             throws DatabaseUnitException, SQLException JavaDoc
69     {
70         IDataSet databaseDataSet = connection.createDataSet();
71
72         DatabaseConfig databaseConfig = connection.getConfig();
73         IStatementFactory statementFactory = (IStatementFactory)databaseConfig.getProperty(DatabaseConfig.PROPERTY_STATEMENT_FACTORY);
74         IBatchStatement statement = statementFactory.createBatchStatement(connection);
75         try
76         {
77             int count = 0;
78             
79             Stack JavaDoc tableNames = new Stack JavaDoc();
80             Set tablesSeen = new HashSet JavaDoc();
81             ITableIterator iterator = dataSet.iterator();
82             while (iterator.next())
83             {
84                 String JavaDoc tableName = iterator.getTableMetaData().getTableName();
85                 if (!tablesSeen.contains(tableName))
86                 {
87                     tableNames.push(tableName);
88                     tablesSeen.add(tableName);
89                 }
90             }
91
92             // delete tables once each in reverse order of seeing them.
93
while (!tableNames.isEmpty())
94             {
95                 String JavaDoc tableName = (String JavaDoc)tableNames.pop();
96
97                 // Use database table name. Required to support case sensitive database.
98
ITableMetaData databaseMetaData =
99                         databaseDataSet.getTableMetaData(tableName);
100                 tableName = databaseMetaData.getTableName();
101
102                 StringBuffer JavaDoc sqlBuffer = new StringBuffer JavaDoc(128);
103                 sqlBuffer.append(getDeleteAllCommand());
104                 sqlBuffer.append(getQualifiedName(connection.getSchema(), tableName, connection));
105                 statement.addBatch(sqlBuffer.toString());
106
107                 count++;
108             }
109
110             if (count > 0)
111             {
112                 statement.executeBatch();
113                 statement.clearBatch();
114             }
115         }
116         finally
117         {
118             statement.close();
119         }
120     }
121 }
122
123
124
125
126
127
128
129
Popular Tags