001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021:
022: package org.dbunit.operation;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import java.util.BitSet;
028:
029: import org.dbunit.DatabaseUnitException;
030: import org.dbunit.database.IDatabaseConnection;
031: import org.dbunit.dataset.Column;
032: import org.dbunit.dataset.DataSetException;
033: import org.dbunit.dataset.IDataSet;
034: import org.dbunit.dataset.ITableIterator;
035: import org.dbunit.dataset.ITableMetaData;
036: import org.dbunit.dataset.NoPrimaryKeyException;
037:
038: /**
039: * Deletes only the dataset contents from the database. This operation does not
040: * delete the entire table contents but only data that are present in the
041: * dataset.
042: *
043: * @author Manuel Laflamme
044: * @version $Revision: 554 $
045: * @since Feb 19, 2002
046: */
047: public class DeleteOperation extends AbstractBatchOperation {
048:
049: /**
050: * Logger for this class
051: */
052: private static final Logger logger = LoggerFactory
053: .getLogger(DeleteOperation.class);
054:
055: DeleteOperation() {
056: _reverseRowOrder = true;
057: }
058:
059: ////////////////////////////////////////////////////////////////////////////
060: // AbstractBatchOperation class
061:
062: protected ITableIterator iterator(IDataSet dataSet)
063: throws DatabaseUnitException {
064: logger.debug("iterator(dataSet=" + dataSet + ") - start");
065:
066: return dataSet.reverseIterator();
067: }
068:
069: public OperationData getOperationData(ITableMetaData metaData,
070: BitSet ignoreMapping, IDatabaseConnection connection)
071: throws DataSetException {
072: logger.debug("getOperationData(metaData=" + metaData
073: + ", ignoreMapping=" + ignoreMapping + ", connection="
074: + connection + ") - start");
075:
076: // cannot construct where clause if no primary key
077: Column[] primaryKeys = metaData.getPrimaryKeys();
078: if (primaryKeys.length == 0) {
079: throw new NoPrimaryKeyException(metaData.getTableName());
080: }
081:
082: // delete from
083: StringBuffer sqlBuffer = new StringBuffer(128);
084: sqlBuffer.append("delete from ");
085: sqlBuffer.append(getQualifiedName(connection.getSchema(),
086: metaData.getTableName(), connection));
087:
088: // where
089: sqlBuffer.append(" where ");
090: for (int i = 0; i < primaryKeys.length; i++) {
091: // escape column name
092: String columnName = getQualifiedName(null, primaryKeys[i]
093: .getColumnName(), connection);
094: sqlBuffer.append(columnName);
095:
096: sqlBuffer.append(" = ?");
097: if (i + 1 < primaryKeys.length) {
098: sqlBuffer.append(" and ");
099: }
100: }
101:
102: return new OperationData(sqlBuffer.toString(), primaryKeys);
103: }
104:
105: }
|