1 21 package org.dbunit.database; 22 23 import org.dbunit.dataset.DataSetException; 24 import org.dbunit.dataset.filter.SequenceTableFilter; 25 26 import java.sql.DatabaseMetaData ; 27 import java.sql.ResultSet ; 28 import java.sql.SQLException ; 29 import java.util.Arrays ; 30 import java.util.HashMap ; 31 import java.util.HashSet ; 32 import java.util.Iterator ; 33 import java.util.LinkedList ; 34 import java.util.List ; 35 import java.util.Map ; 36 import java.util.Set ; 37 38 47 public class DatabaseSequenceFilter extends SequenceTableFilter 48 { 49 50 51 private static Map _dependentMap; 52 53 54 57 public DatabaseSequenceFilter(IDatabaseConnection connection, 58 String [] tableNames) throws DataSetException, SQLException 59 { 60 super(sortTableNames(connection, tableNames)); 61 } 62 63 66 public DatabaseSequenceFilter(IDatabaseConnection connection) 67 throws DataSetException, SQLException 68 { 69 this(connection, connection.createDataSet().getTableNames()); 70 } 71 72 81 static String [] sortTableNames( 82 IDatabaseConnection connection, 83 String [] tableNames) 84 throws DataSetException, SQLException 85 { 87 boolean reprocess = true; 88 List tmpTableNames = Arrays.asList(tableNames); 89 List sortedTableNames = null; 90 DatabaseSequenceFilter._dependentMap = new HashMap (); 91 92 while (reprocess) { 93 sortedTableNames = new LinkedList (); 94 95 for (Iterator i = tmpTableNames.iterator(); i.hasNext();) 97 { 98 boolean foundDependentInSortedTableNames = false; 99 String tmpTable = (String )i.next(); 100 Set tmpTableDependents = getDependentTableNames(connection, tmpTable); 101 102 103 int sortedTableIndex = -1; 104 for (Iterator k = sortedTableNames.iterator(); k.hasNext();) 105 { 106 String sortedTable = (String )k.next(); 107 if (tmpTableDependents.contains(sortedTable)) 108 { 109 sortedTableIndex = sortedTableNames.indexOf(sortedTable); 110 foundDependentInSortedTableNames = true; 111 break; } 113 } 114 115 116 if (foundDependentInSortedTableNames) { 120 if (sortedTableIndex < 0) { 121 throw new IllegalStateException ( 122 "sortedTableIndex should be 0 or greater, but is " 123 + sortedTableIndex); 124 } 125 sortedTableNames.add(sortedTableIndex, tmpTable); 126 } 127 else 128 { 129 sortedTableNames.add(tmpTable); 130 } 131 } 132 133 134 135 if (tmpTableNames.equals(sortedTableNames)) 137 { 138 reprocess = false; 139 } 140 else 141 { 142 143 tmpTableNames = null; 144 tmpTableNames = (List )((LinkedList )sortedTableNames).clone(); 145 } 146 } 148 return (String [])sortedTableNames.toArray(new String [0]); 149 } 150 151 152 163 private static Set getDependentTableNames( 164 IDatabaseConnection connection, 165 String tableName) 166 throws SQLException 167 { 168 if (_dependentMap.containsKey(tableName)) 169 { 170 return (Set )_dependentMap.get(tableName); 171 } 172 173 DatabaseMetaData metaData = connection.getConnection().getMetaData(); 174 String schema = connection.getSchema(); 175 176 ResultSet resultSet = metaData.getExportedKeys(null, schema, tableName); 177 try 178 { 179 Set foreignTableSet = new HashSet (); 180 181 while (resultSet.next()) 182 { 183 String foreignTableName = resultSet.getString(7); 186 187 foreignTableSet.add(foreignTableName); 188 } 189 190 _dependentMap.put(tableName, foreignTableSet); 191 return foreignTableSet; 192 } 193 finally 194 { 195 resultSet.close(); 196 } 197 } 198 199 } 200 | Popular Tags |