KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > database > DatabaseSequenceFilter


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 package org.dbunit.database;
22
23 import org.dbunit.dataset.DataSetException;
24 import org.dbunit.dataset.filter.SequenceTableFilter;
25
26 import java.sql.DatabaseMetaData JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.LinkedList JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Set JavaDoc;
37
38 /**
39  * This filter orders tables using dependency information provided by
40  * {@link java.sql.DatabaseMetaData#getExportedKeys}.
41  *
42  * @author Manuel Laflamme
43  * @author Erik Price
44  * @since Mar 23, 2003
45  * @version $Revision: 1.9 $
46  */

47 public class DatabaseSequenceFilter extends SequenceTableFilter
48 {
49   
50     /** Cache for tablename/foreign key mappings. */
51     private static Map JavaDoc _dependentMap;
52
53
54     /**
55      * Create a DatabaseSequenceFilter that only exposes specified table names.
56      */

57     public DatabaseSequenceFilter(IDatabaseConnection connection,
58             String JavaDoc[] tableNames) throws DataSetException, SQLException JavaDoc
59     {
60         super(sortTableNames(connection, tableNames));
61     }
62
63     /**
64      * Create a DatabaseSequenceFilter that exposes all the database tables.
65      */

66     public DatabaseSequenceFilter(IDatabaseConnection connection)
67             throws DataSetException, SQLException JavaDoc
68     {
69         this(connection, connection.createDataSet().getTableNames());
70     }
71
72     /**
73      * Re-orders a string array of table names, placing dependent ("parent")
74      * tables after their dependencies ("children").
75      *
76      * @param tableNames A string array of table names to be ordered.
77      * @return The re-ordered array of table names.
78      * @throws DataSetException
79      * @throws SQLException If an exception is encountered in accessing the database.
80      */

81     static String JavaDoc[] sortTableNames(
82         IDatabaseConnection connection,
83         String JavaDoc[] tableNames)
84         throws DataSetException, SQLException JavaDoc
85             // not sure why this throws DataSetException ? - ENP
86
{
87         boolean reprocess = true;
88         List JavaDoc tmpTableNames = Arrays.asList(tableNames);
89         List JavaDoc sortedTableNames = null;
90         DatabaseSequenceFilter._dependentMap = new HashMap JavaDoc();
91         
92         while (reprocess) {
93             sortedTableNames = new LinkedList JavaDoc();
94             
95             // re-order 'tmpTableNames' into 'sortedTableNames'
96
for (Iterator JavaDoc i = tmpTableNames.iterator(); i.hasNext();)
97             {
98                 boolean foundDependentInSortedTableNames = false;
99                 String JavaDoc tmpTable = (String JavaDoc)i.next();
100                 Set JavaDoc tmpTableDependents = getDependentTableNames(connection, tmpTable);
101                 
102
103                 int sortedTableIndex = -1;
104                 for (Iterator JavaDoc k = sortedTableNames.iterator(); k.hasNext();)
105                 {
106                     String JavaDoc sortedTable = (String JavaDoc)k.next();
107                     if (tmpTableDependents.contains(sortedTable))
108                     {
109                         sortedTableIndex = sortedTableNames.indexOf(sortedTable);
110                         foundDependentInSortedTableNames = true;
111                         break; // end for loop; we know the index
112
}
113                 }
114
115                 
116                 // add 'tmpTable' to 'sortedTableNames'.
117
// Insert it before its first dependent if there are any,
118
// otherwise append it to the end of 'sortedTableNames'
119
if (foundDependentInSortedTableNames) {
120                     if (sortedTableIndex < 0) {
121                         throw new IllegalStateException JavaDoc(
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             // don't stop processing until we have a perfect run (no re-ordering)
136
if (tmpTableNames.equals(sortedTableNames))
137             {
138                 reprocess = false;
139             }
140             else
141             {
142
143                 tmpTableNames = null;
144                 tmpTableNames = (List JavaDoc)((LinkedList JavaDoc)sortedTableNames).clone();
145             }
146         }// end 'while (reprocess)'
147

148         return (String JavaDoc[])sortedTableNames.toArray(new String JavaDoc[0]);
149     }
150     
151
152     /**
153      * Returns a Set containing the names of all tables which are dependent upon
154      * <code>tableName</code>'s primary key as foreign keys.
155      *
156      * @param connection An IDatabaseConnection to a database that supports
157      * referential integrity.
158      * @param tableName The table whose primary key is to be used in determining
159      * dependent foreign key tables.
160      * @return The Set of dependent foreign key table names.
161      * @throws SQLException If an exception is encountered in accessing the database.
162      */

163     private static Set JavaDoc getDependentTableNames(
164         IDatabaseConnection connection,
165         String JavaDoc tableName)
166         throws SQLException JavaDoc
167     {
168         if (_dependentMap.containsKey(tableName))
169         {
170             return (Set JavaDoc)_dependentMap.get(tableName);
171         }
172
173         DatabaseMetaData JavaDoc metaData = connection.getConnection().getMetaData();
174         String JavaDoc schema = connection.getSchema();
175
176         ResultSet JavaDoc resultSet = metaData.getExportedKeys(null, schema, tableName);
177         try
178         {
179             Set JavaDoc foreignTableSet = new HashSet JavaDoc();
180
181             while (resultSet.next())
182             {
183                 // TODO : add support for qualified table names
184
// String foreignSchemaName = resultSet.getString(6);
185
String JavaDoc 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