001: /*
002: * The DbUnit Database Testing Framework
003: * Copyright (C)2002-2004, DbUnit.org
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: */
020:
021: package org.dbunit.operation;
022:
023: import org.slf4j.Logger;
024: import org.slf4j.LoggerFactory;
025:
026: import org.dbunit.DatabaseUnitException;
027: import org.dbunit.database.DatabaseConfig;
028: import org.dbunit.database.IDatabaseConnection;
029: import org.dbunit.database.statement.IBatchStatement;
030: import org.dbunit.database.statement.IStatementFactory;
031: import org.dbunit.dataset.IDataSet;
032: import org.dbunit.dataset.ITableIterator;
033: import org.dbunit.dataset.ITableMetaData;
034:
035: import java.sql.SQLException;
036: import java.util.HashSet;
037: import java.util.Set;
038: import java.util.Stack;
039:
040: /**
041: * Deletes all rows of tables present in the specified dataset. If the dataset
042: * does not contains a particular table, but that table exists in the database,
043: * the database table is not affected. Table are truncated in
044: * reverse sequence.
045: * <p/>
046: * This operation has the same effect of as {@link TruncateTableOperation}.
047: * TruncateTableOperation is faster, and it is non-logged, meaning it cannot be
048: * rollback. DeleteAllOperation is more portable because not all database vendor
049: * support TRUNCATE_TABLE TABLE statement.
050: *
051: * @author Manuel Laflamme
052: * @version $Revision: 554 $
053: * @see TruncateTableOperation
054: * @since Feb 18, 2002
055: */
056: public class DeleteAllOperation extends AbstractOperation {
057:
058: /**
059: * Logger for this class
060: */
061: private static final Logger logger = LoggerFactory
062: .getLogger(DeleteAllOperation.class);
063:
064: DeleteAllOperation() {
065: }
066:
067: protected String getDeleteAllCommand() {
068: logger.debug("getDeleteAllCommand() - start");
069:
070: return "delete from ";
071: }
072:
073: ////////////////////////////////////////////////////////////////////////////
074: // DatabaseOperation class
075:
076: public void execute(IDatabaseConnection connection, IDataSet dataSet)
077: throws DatabaseUnitException, SQLException {
078: logger.debug("execute(connection=" + connection + ", dataSet="
079: + dataSet + ") - start");
080:
081: IDataSet databaseDataSet = connection.createDataSet();
082:
083: DatabaseConfig databaseConfig = connection.getConfig();
084: IStatementFactory statementFactory = (IStatementFactory) databaseConfig
085: .getProperty(DatabaseConfig.PROPERTY_STATEMENT_FACTORY);
086: IBatchStatement statement = statementFactory
087: .createBatchStatement(connection);
088: try {
089: int count = 0;
090:
091: Stack tableNames = new Stack();
092: Set tablesSeen = new HashSet();
093: ITableIterator iterator = dataSet.iterator();
094: while (iterator.next()) {
095: String tableName = iterator.getTableMetaData()
096: .getTableName();
097: if (!tablesSeen.contains(tableName)) {
098: tableNames.push(tableName);
099: tablesSeen.add(tableName);
100: }
101: }
102:
103: // delete tables once each in reverse order of seeing them.
104: while (!tableNames.isEmpty()) {
105: String tableName = (String) tableNames.pop();
106:
107: // Use database table name. Required to support case sensitive database.
108: ITableMetaData databaseMetaData = databaseDataSet
109: .getTableMetaData(tableName);
110: tableName = databaseMetaData.getTableName();
111:
112: StringBuffer sqlBuffer = new StringBuffer(128);
113: sqlBuffer.append(getDeleteAllCommand());
114: sqlBuffer.append(getQualifiedName(connection
115: .getSchema(), tableName, connection));
116: statement.addBatch(sqlBuffer.toString());
117:
118: count++;
119: }
120:
121: if (count > 0) {
122: statement.executeBatch();
123: statement.clearBatch();
124: }
125: } finally {
126: statement.close();
127: }
128: }
129: }
|