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  
22  package org.dbunit.operation;
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import java.util.BitSet;
28  
29  import org.dbunit.DatabaseUnitException;
30  import org.dbunit.database.IDatabaseConnection;
31  import org.dbunit.dataset.Column;
32  import org.dbunit.dataset.DataSetException;
33  import org.dbunit.dataset.IDataSet;
34  import org.dbunit.dataset.ITableIterator;
35  import org.dbunit.dataset.ITableMetaData;
36  import org.dbunit.dataset.NoPrimaryKeyException;
37  
38  /**
39   * Deletes only the dataset contents from the database. This operation does not
40   * delete the entire table contents but only data that are present in the
41   * dataset.
42   *
43   * @author Manuel Laflamme
44   * @version $Revision$
45   * @since Feb 19, 2002
46   */
47  public class DeleteOperation extends AbstractBatchOperation
48  {
49  
50      /**
51       * Logger for this class
52       */
53      private static final Logger logger = LoggerFactory.getLogger(DeleteOperation.class);
54  
55      DeleteOperation()
56      {
57          _reverseRowOrder = true;
58      }
59  
60      ////////////////////////////////////////////////////////////////////////////
61      // AbstractBatchOperation class
62  
63      protected ITableIterator iterator(IDataSet dataSet) throws DatabaseUnitException
64      {
65          logger.debug("iterator(dataSet={}) - start", dataSet);
66          return dataSet.reverseIterator();
67      }
68  
69      public OperationData getOperationData(ITableMetaData metaData, BitSet ignoreMapping, IDatabaseConnection connection) throws DataSetException
70      {
71      	if (logger.isDebugEnabled())
72      	{
73      		logger.debug("getOperationData(metaData={}, ignoreMapping={}, connection={}) - start",
74      				new Object[]{ metaData, ignoreMapping, connection });
75      	}
76  
77          // cannot construct where clause if no primary key
78          Column[] primaryKeys = metaData.getPrimaryKeys();
79          if (primaryKeys.length == 0)
80          {
81              throw new NoPrimaryKeyException(metaData.getTableName());
82          }
83  
84          // delete from
85          StringBuffer sqlBuffer = new StringBuffer(128);
86          sqlBuffer.append("delete from ");
87          sqlBuffer.append(getQualifiedName(connection.getSchema(),
88                  metaData.getTableName(), connection));
89  
90          // where
91          sqlBuffer.append(" where ");
92          for (int i = 0; i < primaryKeys.length; i++)
93          {
94              // escape column name
95              String columnName = getQualifiedName(null,
96                      primaryKeys[i].getColumnName(), connection);
97              sqlBuffer.append(columnName);
98  
99              sqlBuffer.append(" = ?");
100             if (i + 1 < primaryKeys.length)
101             {
102                 sqlBuffer.append(" and ");
103             }
104         }
105 
106         return new OperationData(sqlBuffer.toString(), primaryKeys);
107     }
108 
109 }