1 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 ; 38 import java.util.BitSet ; 39 40 47 public abstract class AbstractBatchOperation extends AbstractOperation 48 { 49 private static final BitSet EMPTY_BITSET = new BitSet (); 50 protected boolean _reverseRowOrder = false; 51 52 static boolean isEmpty(ITable table) throws DataSetException 53 { 54 Column[] columns = table.getTableMetaData().getColumns(); 55 56 if (columns.length == 0) 58 { 59 return true; 60 } 61 62 try 64 { 65 table.getValue(0, columns[0].getColumnName()); 66 return false; 67 } 68 catch (RowOutOfBoundsException e) 69 { 70 return true; 72 } 73 } 74 75 79 protected ITableIterator iterator(IDataSet dataSet) throws DatabaseUnitException 80 { 81 return dataSet.iterator(); 82 } 83 84 88 BitSet getIgnoreMapping(ITable table, int row) 89 throws DataSetException 90 { 91 return EMPTY_BITSET; 92 } 93 94 98 boolean equalsIgnoreMapping(BitSet ignoreMapping, ITable table, 99 int row) throws DataSetException 100 { 101 return true; 102 } 103 104 abstract OperationData getOperationData(ITableMetaData metaData, 105 BitSet ignoreMapping, IDatabaseConnection connection) throws DataSetException; 106 107 110 public void execute(IDatabaseConnection connection, IDataSet dataSet) 111 throws DatabaseUnitException, SQLException 112 { 113 DatabaseConfig databaseConfig = connection.getConfig(); 114 IStatementFactory factory = (IStatementFactory)databaseConfig.getProperty( 115 DatabaseConfig.PROPERTY_STATEMENT_FACTORY); 116 117 ITableIterator iterator = iterator(dataSet); 119 while (iterator.next()) 120 { 121 ITable table = iterator.getTable(); 122 123 if (isEmpty(table)) 125 { 126 continue; 127 } 128 129 ITableMetaData metaData = getOperationMetaData(connection, 130 table.getTableMetaData()); 131 BitSet ignoreMapping = null; 132 OperationData operationData = null; 133 IPreparedBatchStatement statement = null; 134 135 try 136 { 137 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 (ignoreMapping == null || !equalsIgnoreMapping(ignoreMapping, table, row)) 150 { 151 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 Column[] columns = operationData.getColumns(); 168 for (int j = 0; j < columns.length; j++) 169 { 170 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 } 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 |