Database implements Serializable, Cloneable { public static List<Table> sortByForeignKeys(List<Table> tables, Map<String, Table> allTables, Map<Integer, Set<Table>> dependencyMap, Map<Table, Set<String>> missingDependencyMap) { if (allTables == null) { allTables = new HashMap<String, Table>(); for (Table t : tables) { allTables.put(t.getName(), t); } } if (dependencyMap == null) { dependencyMap = new HashMap<Integer, Set<Table>>(); } if (missingDependencyMap == null) { missingDependencyMap = new HashMap<Table, Set<String>>(); } Set<Table> resolved = new HashSet<Table>(); Set<Table> temporary = new HashSet<Table>(); List<Table> finalList = new ArrayList<Table>(); MutableInt depth = new MutableInt(1); MutableInt position = new MutableInt(1); MutableInt parentPosition = new MutableInt(-1); Map<Table, Integer> resolvedPosition = new HashMap<Table, Integer>(); for(Table t : tables) { if (t != null) { depth.setValue(1); parentPosition.setValue(-1); resolveForeignKeyOrder(t, allTables, resolved, temporary, finalList, null, missingDependencyMap, dependencyMap, depth, position, resolvedPosition, parentPosition); } } Collections.reverse(finalList); return finalList; }  static List<Table> sortByForeignKeys(List<Table> tables, Map<String, Table> allTables,
            Map<Integer, Set<Table>> dependencyMap, Map<Table, Set<String>> missingDependencyMap); static void logMissingDependentTableNames(List<Table> tables); static Map<String, List<String>> findMissingDependentTableNames(List<Table> tables); static void resolveForeignKeyOrder(Table t, Map<String, Table> allTables, Set<Table> resolved, Set<Table> temporary, 
            List<Table> finalList, Table parentTable, Map<Table, Set<String>> missingDependencyMap,
            Map<Integer, Set<Table>> dependencyMap, MutableInt depth, MutableInt position, 
            Map<Table, Integer> resolvedPosition, MutableInt parentPosition); static String printTables(List<Table> tables); static Table[] sortByForeignKeys(Table... tables); static List<Table> sortByForeignKeys(List<Table> tables); void mergeWith(Database otherDb); String getName(); void setName(String name); String getCatalog(); void setCatalog(String catalog); String getSchema(); void setSchema(String schema); String getVersion(); void setVersion(String version); String getIdMethod(); void setIdMethod(String idMethod); int getTableCount(); Table[] getTables(); Table getTable(int idx); void addTable(Table table); void addTable(int idx, Table table); void addTables(Collection<Table> tables); void addTables(Table[] tables); void removeTable(Table table); void removeTable(int idx); void initialize(); Table findTable(String name); Table findTable(String name, boolean caseSensitive); Table findTable(String catalogName, String schemaName, String tableName,
            boolean caseSensitive); Table findTable(String catalogName, String schemaName, String tableName); void resetTableIndexCache(); void removeAllTablesExcept(String... tableNames); Database copy(); Object clone(); boolean equals(Object obj); int hashCode(); String toString(); String toVerboseString();  }
@Test public void testDependentMapParentChild() throws Exception { List<Table> list = new ArrayList<Table>(4); Table t1 = new Table("1"); Table t2 = new Table("2"); Table t3 = new Table("3"); t1.addForeignKey(new ForeignKey("2","2")); list.add(t1); list.add(t2); list.add(t3); Map<Integer, Set<Table>> dependencyMap = new HashMap<Integer, Set<Table>>(); list = Database.sortByForeignKeys(list, null, dependencyMap, null); assertTrue(dependencyMap.get(1).contains(t1)); assertTrue(dependencyMap.get(1).contains(t2)); assertTrue(dependencyMap.get(2).contains(t3)); }
@Test public void testDependentMapParentChildReverseOrder() throws Exception { List<Table> list = new ArrayList<Table>(4); Table t1 = new Table("1"); Table t2 = new Table("2"); Table t3 = new Table("3"); t1.addForeignKey(new ForeignKey("2","2")); list.add(t2); list.add(t1); list.add(t3); Map<Integer, Set<Table>> dependencyMap = new HashMap<Integer, Set<Table>>(); list = Database.sortByForeignKeys(list, null, dependencyMap, null); assertTrue(dependencyMap.get(1).contains(t1)); assertTrue(dependencyMap.get(1).contains(t2)); assertTrue(dependencyMap.get(2).contains(t3)); }
@Test public void testDependentMapTwoGroups() throws Exception { List<Table> list = new ArrayList<Table>(4); Table t1 = new Table("1"); Table t2 = new Table("2"); Table t3 = new Table("3"); Table t4 = new Table("4"); Table t5 = new Table("5"); Table t6 = new Table("6"); t2.addForeignKey(new ForeignKey("1","1")); t3.addForeignKey(new ForeignKey("1","1")); t5.addForeignKey(new ForeignKey("4","4")); t6.addForeignKey(new ForeignKey("4","4")); list.add(t1); list.add(t2); list.add(t3); list.add(t4); list.add(t5); list.add(t6); Map<Integer, Set<Table>> dependencyMap = new HashMap<Integer, Set<Table>>(); list = Database.sortByForeignKeys(list, null, dependencyMap, null); assertTrue(dependencyMap.get(1).contains(t1)); assertTrue(dependencyMap.get(1).contains(t2)); assertTrue(dependencyMap.get(1).contains(t3)); assertTrue(dependencyMap.get(2).contains(t4)); assertTrue(dependencyMap.get(2).contains(t5)); assertTrue(dependencyMap.get(2).contains(t6)); }
@Test public void testDependentMapCircular() throws Exception { List<Table> list = new ArrayList<Table>(4); Table t1 = new Table("1"); Table t2 = new Table("2"); Table t3 = new Table("3"); Table t4 = new Table("4"); Table t5 = new Table("5"); t4.addForeignKey(new ForeignKey("3","3")); t3.addForeignKey(new ForeignKey("2","2")); t4.addForeignKey(new ForeignKey("2","2")); list.add(t1); list.add(t2); list.add(t3); list.add(t4); list.add(t5); Map<Integer, Set<Table>> dependencyMap = new HashMap<Integer, Set<Table>>(); list = Database.sortByForeignKeys(list, null, dependencyMap, null); assertTrue(dependencyMap.get(1).contains(t1)); assertTrue(dependencyMap.get(2).contains(t2)); assertTrue(dependencyMap.get(2).contains(t3)); assertTrue(dependencyMap.get(2).contains(t4)); assertTrue(dependencyMap.get(3).contains(t5)); }
@Test public void testDependentMapMultipleParents() throws Exception { List<Table> list = new ArrayList<Table>(4); Table t1 = new Table("1"); Table t2 = new Table("2"); Table t3 = new Table("3"); Table t4 = new Table("4"); Table t5 = new Table("5"); t2.addForeignKey(new ForeignKey("1","1")); t3.addForeignKey(new ForeignKey("1","1")); t3.addForeignKey(new ForeignKey("4","4")); t5.addForeignKey(new ForeignKey("4","4")); list.add(t1); list.add(t2); list.add(t3); list.add(t4); list.add(t5); Map<Integer, Set<Table>> dependencyMap = new HashMap<Integer, Set<Table>>(); list = Database.sortByForeignKeys(list, null, dependencyMap, null); assertTrue(dependencyMap.get(1).contains(t1)); assertTrue(dependencyMap.get(1).contains(t2)); assertTrue(dependencyMap.get(1).contains(t3)); assertTrue(dependencyMap.get(1).contains(t4)); assertTrue(dependencyMap.get(1).contains(t5)); }
@Test public void testDependentMapMergeGroups() throws Exception { List<Table> list = new ArrayList<Table>(4); Table t1 = new Table("1"); Table t2 = new Table("2"); Table t3 = new Table("3"); Table t4 = new Table("4"); Table t5 = new Table("5"); t2.addForeignKey(new ForeignKey("1","1")); t3.addForeignKey(new ForeignKey("1","1")); t3.addForeignKey(new ForeignKey("4","4")); t3.addForeignKey(new ForeignKey("5","5")); list.add(t1); list.add(t2); list.add(t4); list.add(t5); list.add(t3); Map<Integer, Set<Table>> dependencyMap = new HashMap<Integer, Set<Table>>(); list = Database.sortByForeignKeys(list, null, dependencyMap, null); assertTrue(dependencyMap.get(1).contains(t1)); assertTrue(dependencyMap.get(1).contains(t2)); assertTrue(dependencyMap.get(1).contains(t3)); assertTrue(dependencyMap.get(1).contains(t4)); assertTrue(dependencyMap.get(1).contains(t5)); }
@Test public void testDependentMapOutOfOrder() throws Exception { List<Table> list = new ArrayList<Table>(4); Table t1 = new Table("1"); Table t2 = new Table("2"); Table t3 = new Table("3"); Table t4 = new Table("4"); Table t5 = new Table("5"); t5.addForeignKey(new ForeignKey("1","1")); t4.addForeignKey(new ForeignKey("1","1")); list.add(t1); list.add(t3); list.add(t4); list.add(t2); list.add(t5); Map<Integer, Set<Table>> dependencyMap = new HashMap<Integer, Set<Table>>(); list = Database.sortByForeignKeys(list, null, dependencyMap, null); assertTrue(dependencyMap.get(1).contains(t1)); assertTrue(dependencyMap.get(1).contains(t4)); assertTrue(dependencyMap.get(1).contains(t5)); assertTrue(dependencyMap.get(2).contains(t3)); assertTrue(dependencyMap.get(3).contains(t2)); }
@Test public void testCyclicalReferences() { List<Table> list = new ArrayList<Table>(4); Table t1 = new Table("1"); Table t2 = new Table("2"); Table t3 = new Table("3"); t1.addForeignKey(new ForeignKey("2","2")); t2.addForeignKey(new ForeignKey("3","3")); t3.addForeignKey(new ForeignKey("1","1")); list.add(t3); list.add(t2); list.add(t1); list = Database.sortByForeignKeys(list, null, null, null); }
@Test public void testDependentMapIndependent() throws Exception { List<Table> list = new ArrayList<Table>(4); Table t1 = new Table("1"); Table t2 = new Table("2"); Table t3 = new Table("3"); list.add(t1); list.add(t2); list.add(t3); Map<Integer, Set<Table>> dependencyMap = new HashMap<Integer, Set<Table>>(); list = Database.sortByForeignKeys(list, null, dependencyMap, null); assertTrue(dependencyMap.size() == 3); }