001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2005, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021: package org.dbunit.database.search;
022:
023: import org.slf4j.Logger;
024: import org.slf4j.LoggerFactory;
025:
026: import java.sql.SQLException;
027: import java.util.HashMap;
028: import java.util.Map;
029: import java.util.Set;
030:
031: import org.dbunit.database.IDatabaseConnection;
032: import org.dbunit.dataset.FilteredDataSet;
033: import org.dbunit.dataset.IDataSet;
034: import org.dbunit.dataset.filter.ITableFilter;
035:
036: import org.dbunit.util.CollectionsHelper;
037: import org.dbunit.util.search.DepthFirstSearch;
038: import org.dbunit.util.search.SearchException;
039:
040: /**
041: * Helper for the graph-search based classes used to calculate dependency
042: * among tables.
043: *
044: * @author Felipe Leme <dbunit@felipeal.net>
045: * @version $Revision: 554 $
046: * @since Aug 26, 2005
047: */
048: public class TablesDependencyHelper {
049:
050: /**
051: * Logger for this class
052: */
053: private static final Logger logger = LoggerFactory
054: .getLogger(TablesDependencyHelper.class);
055:
056: // this is a "static" class
057: private TablesDependencyHelper() {
058: }
059:
060: /**
061: * Get the name of all tables that depend on a root table (i.e, all tables whose PK
062: * is a FK for the root table).
063: * @param connection database conncetion
064: * @param rootTable root table described above
065: * @return name of all tables that depend on the root table (including the root table),
066: * in the right order for insertions
067: * @throws SearchException if an exception occurred while calculating the order
068: */
069: public static String[] getDependentTables(
070: IDatabaseConnection connection, String rootTable)
071: throws SearchException {
072: logger.debug("getDependentTables(connection=" + connection
073: + ", rootTable=" + rootTable + ") - start");
074:
075: return getDependentTables(connection,
076: new String[] { rootTable });
077: }
078:
079: /**
080: * Get the name of all tables that depend on the root tables (i.e, all tables whose PK
081: * is a FK for one of root tables).
082: * @param connection database conncetion
083: * @param rootTables array of root tables described above
084: * @return name of all tables that depend on the root tables (including the root tables),
085: * in the right order for insertions
086: * @throws SearchException if an exception occurred while calculating the order
087: */
088: public static String[] getDependentTables(
089: IDatabaseConnection connection, String[] rootTables)
090: throws SearchException {
091: logger.debug("getDependentTables(connection=" + connection
092: + ", rootTables=" + rootTables + ") - start");
093:
094: ImportedKeysSearchCallback callback = new ImportedKeysSearchCallback(
095: connection);
096: DepthFirstSearch search = new DepthFirstSearch();
097: Set tables = search.search(rootTables, callback);
098: return (String[]) CollectionsHelper.setToStrings(tables);
099: }
100:
101: /**
102: * Get the name of all tables that depend on a root table ( i.e, all tables whose PK
103: * is a FK for the root table) and also the tables the root table depends on
104: * (i.e., all tables which have a FK for the root table's PK).
105: * @param connection database conncetion
106: * @param rootTable root table described above
107: * @return name of all tables that depend on the root table (including the root table),
108: * in the right order for insertions
109: * @throws SearchException if an exception occurred while calculating the order
110: */
111: public static String[] getAllDependentTables(
112: IDatabaseConnection connection, String rootTable)
113: throws SearchException {
114: logger.debug("getAllDependentTables(connection=" + connection
115: + ", rootTable=" + rootTable + ") - start");
116:
117: return getAllDependentTables(connection,
118: new String[] { rootTable });
119: }
120:
121: /**
122: * Get the name of all tables that depend on the root tables ( i.e, all tables whose PK
123: * is a FK for any of the root tables) and also the tables the root tables depends on
124: * (i.e., all tables which have a FK for any of the root table's PK).
125: * @param connection database conncetion
126: * @param rootTables root tables described above
127: * @return name of all tables that depend on the root tables (including the root tables),
128: * in the right order for insertions
129: * @throws SearchException if an exception occurred while calculating the order
130: */
131: public static String[] getAllDependentTables(
132: IDatabaseConnection connection, String[] rootTables)
133: throws SearchException {
134: logger.debug("getAllDependentTables(connection=" + connection
135: + ", rootTables=" + rootTables + ") - start");
136:
137: ImportedAndExportedKeysSearchCallback callback = new ImportedAndExportedKeysSearchCallback(
138: connection);
139: DepthFirstSearch search = new DepthFirstSearch();
140: Set tables = search.search(rootTables, callback);
141: return (String[]) CollectionsHelper.setToStrings(tables);
142: }
143:
144: // TODO: javadoc (and unit tests) from down here...
145:
146: public static IDataSet getDataset(IDatabaseConnection connection,
147: String rootTable, Set allowedIds) throws SearchException,
148: SQLException {
149: logger.debug("getDataset(connection=" + connection
150: + ", rootTable=" + rootTable + ", allowedIds="
151: + allowedIds + ") - start");
152:
153: HashMap map = new HashMap(1);
154: map.put(rootTable, allowedIds);
155: return getDataset(connection, map);
156: }
157:
158: public static IDataSet getDataset(IDatabaseConnection connection,
159: Map rootTables) throws SearchException, SQLException {
160: logger.debug("getDataset(connection=" + connection
161: + ", rootTables=" + rootTables + ") - start");
162:
163: ImportedKeysSearchCallbackFilteredByPKs callback = new ImportedKeysSearchCallbackFilteredByPKs(
164: connection, rootTables);
165: ITableFilter filter = callback.getFilter();
166: DepthFirstSearch search = new DepthFirstSearch();
167: String[] tableNames = CollectionsHelper.setToStrings(rootTables
168: .keySet());
169: Set tmpTables = search.search(tableNames, callback);
170: String[] dependentTables = CollectionsHelper
171: .setToStrings(tmpTables);
172: IDataSet tmpDataset = connection.createDataSet(dependentTables);
173: FilteredDataSet dataset = new FilteredDataSet(filter,
174: tmpDataset);
175: return dataset;
176: }
177:
178: public static IDataSet getAllDataset(
179: IDatabaseConnection connection, String rootTable,
180: Set allowedPKs) throws SearchException, SQLException {
181: logger.debug("getAllDataset(connection=" + connection
182: + ", rootTable=" + rootTable + ", allowedPKs="
183: + allowedPKs + ") - start");
184:
185: HashMap map = new HashMap(1);
186: map.put(rootTable, allowedPKs);
187: return getAllDataset(connection, map);
188: }
189:
190: public static IDataSet getAllDataset(
191: IDatabaseConnection connection, Map rootTables)
192: throws SearchException, SQLException {
193: logger.debug("getAllDataset(connection=" + connection
194: + ", rootTables=" + rootTables + ") - start");
195:
196: ImportedAndExportedKeysSearchCallbackFilteredByPKs callback = new ImportedAndExportedKeysSearchCallbackFilteredByPKs(
197: connection, rootTables);
198: ITableFilter filter = callback.getFilter();
199: DepthFirstSearch search = new DepthFirstSearch();
200: String[] tableNames = CollectionsHelper.setToStrings(rootTables
201: .keySet());
202: Set tmpTables = search.search(tableNames, callback);
203: String[] dependentTables = CollectionsHelper
204: .setToStrings(tmpTables);
205: IDataSet tmpDataset = connection.createDataSet(dependentTables);
206: FilteredDataSet dataset = new FilteredDataSet(filter,
207: tmpDataset);
208: return dataset;
209: }
210:
211: }
|