1 20 21 package org.dbunit.operation; 22 23 import org.dbunit.DatabaseUnitException; 24 import org.dbunit.database.DatabaseConfig; 25 import org.dbunit.database.IDatabaseConnection; 26 import org.dbunit.database.statement.IBatchStatement; 27 import org.dbunit.database.statement.IStatementFactory; 28 import org.dbunit.dataset.IDataSet; 29 import org.dbunit.dataset.ITableIterator; 30 import org.dbunit.dataset.ITableMetaData; 31 32 import java.sql.SQLException ; 33 import java.util.HashSet ; 34 import java.util.Set ; 35 import java.util.Stack ; 36 37 53 public class DeleteAllOperation extends AbstractOperation 54 { 55 DeleteAllOperation() 56 { 57 } 58 59 protected String getDeleteAllCommand() 60 { 61 return "delete from "; 62 } 63 64 67 public void execute(IDatabaseConnection connection, IDataSet dataSet) 68 throws DatabaseUnitException, SQLException 69 { 70 IDataSet databaseDataSet = connection.createDataSet(); 71 72 DatabaseConfig databaseConfig = connection.getConfig(); 73 IStatementFactory statementFactory = (IStatementFactory)databaseConfig.getProperty(DatabaseConfig.PROPERTY_STATEMENT_FACTORY); 74 IBatchStatement statement = statementFactory.createBatchStatement(connection); 75 try 76 { 77 int count = 0; 78 79 Stack tableNames = new Stack (); 80 Set tablesSeen = new HashSet (); 81 ITableIterator iterator = dataSet.iterator(); 82 while (iterator.next()) 83 { 84 String tableName = iterator.getTableMetaData().getTableName(); 85 if (!tablesSeen.contains(tableName)) 86 { 87 tableNames.push(tableName); 88 tablesSeen.add(tableName); 89 } 90 } 91 92 while (!tableNames.isEmpty()) 94 { 95 String tableName = (String )tableNames.pop(); 96 97 ITableMetaData databaseMetaData = 99 databaseDataSet.getTableMetaData(tableName); 100 tableName = databaseMetaData.getTableName(); 101 102 StringBuffer sqlBuffer = new StringBuffer (128); 103 sqlBuffer.append(getDeleteAllCommand()); 104 sqlBuffer.append(getQualifiedName(connection.getSchema(), tableName, connection)); 105 statement.addBatch(sqlBuffer.toString()); 106 107 count++; 108 } 109 110 if (count > 0) 111 { 112 statement.executeBatch(); 113 statement.clearBatch(); 114 } 115 } 116 finally 117 { 118 statement.close(); 119 } 120 } 121 } 122 123 124 125 126 127 128 129 | Popular Tags |