KickJava   Java API By Example, From Geeks To Geeks.

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


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.DatabaseConfig;
26 import org.dbunit.database.IDatabaseConnection;
27 import org.dbunit.database.statement.IPreparedBatchStatement;
28 import org.dbunit.database.statement.IStatementFactory;
29 import org.dbunit.dataset.Column;
30 import org.dbunit.dataset.DataSetException;
31 import org.dbunit.dataset.IDataSet;
32 import org.dbunit.dataset.ITable;
33 import org.dbunit.dataset.ITableIterator;
34 import org.dbunit.dataset.ITableMetaData;
35 import org.dbunit.dataset.RowOutOfBoundsException;
36
37 import java.sql.SQLException JavaDoc;
38 import java.util.BitSet JavaDoc;
39
40 /**
41  * Base implementation for database operation that are executed in batch.
42  *
43  * @author Manuel Laflamme
44  * @version $Revision: 1.28 $
45  * @since Feb 19, 2002
46  */

47 public abstract class AbstractBatchOperation extends AbstractOperation
48 {
49     private static final BitSet JavaDoc EMPTY_BITSET = new BitSet JavaDoc();
50     protected boolean _reverseRowOrder = false;
51
52     static boolean isEmpty(ITable table) throws DataSetException
53     {
54         Column[] columns = table.getTableMetaData().getColumns();
55
56         // No columns = empty
57
if (columns.length == 0)
58         {
59             return true;
60         }
61
62         // Try to fetch first table value
63
try
64         {
65             table.getValue(0, columns[0].getColumnName());
66             return false;
67         }
68         catch (RowOutOfBoundsException e)
69         {
70             // Not able to access first row thus empty
71
return true;
72         }
73     }
74
75     /**
76      * Returns list of tables this operation is applied to. This method
77      * allow subclass to do filtering.
78      */

79     protected ITableIterator iterator(IDataSet dataSet) throws DatabaseUnitException
80     {
81         return dataSet.iterator();
82     }
83
84     /**
85      * Returns mapping of columns to ignore by this operation. Each bit set represent
86      * a column to ignore.
87      */

88     BitSet JavaDoc getIgnoreMapping(ITable table, int row)
89             throws DataSetException
90     {
91         return EMPTY_BITSET;
92     }
93
94     /**
95      * Returns false if the specified table row have a different ignore mapping
96      * than the specified mapping.
97      */

98     boolean equalsIgnoreMapping(BitSet JavaDoc ignoreMapping, ITable table,
99             int row) throws DataSetException
100     {
101         return true;
102     }
103
104     abstract OperationData getOperationData(ITableMetaData metaData,
105             BitSet JavaDoc ignoreMapping, IDatabaseConnection connection) throws DataSetException;
106
107     ////////////////////////////////////////////////////////////////////////////
108
// DatabaseOperation class
109

110     public void execute(IDatabaseConnection connection, IDataSet dataSet)
111             throws DatabaseUnitException, SQLException JavaDoc
112     {
113         DatabaseConfig databaseConfig = connection.getConfig();
114         IStatementFactory factory = (IStatementFactory)databaseConfig.getProperty(
115                 DatabaseConfig.PROPERTY_STATEMENT_FACTORY);
116
117         // for each table
118
ITableIterator iterator = iterator(dataSet);
119         while (iterator.next())
120         {
121             ITable table = iterator.getTable();
122
123             // Do not process empty table
124
if (isEmpty(table))
125             {
126                 continue;
127             }
128
129             ITableMetaData metaData = getOperationMetaData(connection,
130                     table.getTableMetaData());
131             BitSet JavaDoc ignoreMapping = null;
132             OperationData operationData = null;
133             IPreparedBatchStatement statement = null;
134
135             try
136             {
137                 // For each row
138
int start = _reverseRowOrder ? table.getRowCount() - 1 : 0;
139                 int increment = _reverseRowOrder ? -1 : 1;
140
141                 try
142                 {
143                     for (int i = start; ; i = i + increment)
144                     {
145                         int row = i;
146
147                         // If current row have a diffrent ignore value mapping than
148
// previous one, we generate a new statement
149
if (ignoreMapping == null || !equalsIgnoreMapping(ignoreMapping, table, row))
150                         {
151                             // Execute and close previous statement
152
if (statement != null)
153                             {
154                                 statement.executeBatch();
155                                 statement.clearBatch();
156                                 statement.close();
157                             }
158
159                             ignoreMapping = getIgnoreMapping(table, row);
160                             operationData = getOperationData(metaData, ignoreMapping, connection);
161                             statement = factory.createPreparedBatchStatement(
162                                     operationData.getSql(), connection);
163                         }
164
165
166                         // for each column
167
Column[] columns = operationData.getColumns();
168                         for (int j = 0; j < columns.length; j++)
169                         {
170                             // Bind value only if not in ignore mapping
171
if (!ignoreMapping.get(j))
172                             {
173                                 Column column = columns[j];
174                                 statement.addValue(table.getValue(row,
175                                         column.getColumnName()), column.getDataType());
176                             }
177                         }
178                         statement.addBatch();
179                     }
180                 }
181                 catch (RowOutOfBoundsException e)
182                 {
183                     // end of table
184
}
185
186                 statement.executeBatch();
187                 statement.clearBatch();
188             }
189             finally
190             {
191                 if (statement != null)
192                 {
193                     statement.close();
194                 }
195             }
196         }
197     }
198 }
199
200
201
202
203
204
205
206
207
208
209
210
211
Popular Tags