KickJava   Java API By Example, From Geeks To Geeks.

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


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.dbunit.DatabaseUnitException;
25 import org.dbunit.database.IDatabaseConnection;
26 import org.dbunit.dataset.*;
27
28 import java.math.BigInteger JavaDoc;
29 import java.util.BitSet JavaDoc;
30
31 /**
32  * Deletes only the dataset contents from the database. This operation does not
33  * delete the entire table contents but only data that are present in the
34  * dataset.
35  *
36  * @author Manuel Laflamme
37  * @version $Revision: 1.18 $
38  * @since Feb 19, 2002
39  */

40 public class DeleteOperation extends AbstractBatchOperation
41 {
42     DeleteOperation()
43     {
44         _reverseRowOrder = true;
45     }
46
47     ////////////////////////////////////////////////////////////////////////////
48
// AbstractBatchOperation class
49

50     protected ITableIterator iterator(IDataSet dataSet) throws DatabaseUnitException
51     {
52         return dataSet.reverseIterator();
53     }
54
55     public OperationData getOperationData(ITableMetaData metaData, BitSet JavaDoc ignoreMapping, IDatabaseConnection connection) throws DataSetException
56     {
57         // cannot construct where clause if no primary key
58
Column[] primaryKeys = metaData.getPrimaryKeys();
59         if (primaryKeys.length == 0)
60         {
61             throw new NoPrimaryKeyException(metaData.getTableName());
62         }
63
64         // delete from
65
StringBuffer JavaDoc sqlBuffer = new StringBuffer JavaDoc(128);
66         sqlBuffer.append("delete from ");
67         sqlBuffer.append(getQualifiedName(connection.getSchema(),
68                 metaData.getTableName(), connection));
69
70         // where
71
sqlBuffer.append(" where ");
72         for (int i = 0; i < primaryKeys.length; i++)
73         {
74             // escape column name
75
String JavaDoc columnName = getQualifiedName(null,
76                     primaryKeys[i].getColumnName(), connection);
77             sqlBuffer.append(columnName);
78
79             sqlBuffer.append(" = ?");
80             if (i + 1 < primaryKeys.length)
81             {
82                 sqlBuffer.append(" and ");
83             }
84         }
85
86         return new OperationData(sqlBuffer.toString(), primaryKeys);
87     }
88
89 }
90
91
92
93
94
95
96
97
98
Popular Tags