]> source.dussan.org Git - jackcess.git/commitdiff
fix linked table test; add unit tests for new table iteration methods
authorJames Ahlborn <jtahlborn@yahoo.com>
Mon, 23 Feb 2015 04:08:54 +0000 (04:08 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Mon, 23 Feb 2015 04:08:54 +0000 (04:08 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@917 f203690c-595d-4dc9-a70b-905162fa7fd2

src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java
src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java

index 969f93d91b80706f4db425a04082204a90140b4b..414b43bb04a37e5ebc15dfcb475dd2963f1895c0 100644 (file)
@@ -601,20 +601,31 @@ public class DatabaseImpl implements Database
   }
 
   public boolean isLinkedTable(Table table) throws IOException {
-    
+
     if((table == null) || (this == table.getDatabase())) {
       // if the table is null or this db owns the table, not linked
       return false;
     }
 
+    // common case, local table name == remote table name
     TableInfo tableInfo = lookupTable(table.getName());
-    
-    return((tableInfo != null) &&
-           tableInfo.isLinked() &&
-           (_linkedDbs != null) &&
-           (_linkedDbs.get(((LinkedTableInfo)tableInfo).linkedDbName) ==
-            table.getDatabase()));
+    if((tableInfo != null) && tableInfo.isLinked() &&
+       matchesLinkedTable(table, ((LinkedTableInfo)tableInfo).linkedTableName,
+                          ((LinkedTableInfo)tableInfo).linkedDbName)) {
+      return true;
+    }
+
+    // but, the local table name may not match the remote table name, so we
+    // need to do a search if the common case fails
+    return _tableFinder.isLinkedTable(table);
   }  
+
+  boolean matchesLinkedTable(Table table, String linkedTableName,
+                             String linkedDbName) {
+    return (table.getName().equalsIgnoreCase(linkedTableName) &&
+            (_linkedDbs != null) &&
+            (_linkedDbs.get(linkedDbName) == table.getDatabase()));
+  }
   
   public TimeZone getTimeZone() {
     return _timeZone;
@@ -1873,7 +1884,7 @@ public class DatabaseImpl implements Database
         throw new NoSuchElementException();
       }
       try {
-        return getTable(_tableNameIter.next());
+        return getTable(_tableNameIter.next(), true);
       } catch(IOException e) {
         throw new RuntimeIOException(e);
       }
@@ -1944,6 +1955,22 @@ public class DatabaseImpl implements Database
       }
     }
 
+    public boolean isLinkedTable(Table table) throws IOException
+    {
+      for(Row row : getTableNamesCursor().newIterable().setColumnNames(
+              SYSTEM_CATALOG_COLUMNS)) {
+        Short type = row.getShort(CAT_COL_TYPE);
+        String linkedDbName = row.getString(CAT_COL_DATABASE);
+        String linkedTableName = row.getString(CAT_COL_FOREIGN_NAME);
+
+        if(TYPE_LINKED_TABLE.equals(type) &&
+           matchesLinkedTable(table, linkedTableName, linkedDbName)) {
+          return true;
+        } 
+      }
+      return false;
+    }
+
     protected abstract Cursor findRow(Integer parentId, String name)
       throws IOException;
 
index 254901aafa7e2a35fc09c7a3b778157e61e0d7b2..e2751cbb98e67611121d47269895eba3d2b76c02 100644 (file)
@@ -1442,14 +1442,56 @@ public class DatabaseTest extends TestCase
 
       assertTable(expectedRows, t3);
 
+      Table t1 = db.getTable("Table1");
+      
       assertFalse(db.isLinkedTable(null));
-      assertFalse(db.isLinkedTable(t2));
+      assertTrue(db.isLinkedTable(t2));
       assertTrue(db.isLinkedTable(t3));
+      assertFalse(db.isLinkedTable(t1));
+
+      List<Table> tables = getTables(db.newIterable());
+      assertEquals(3, tables.size());
+      assertTrue(tables.contains(t1));
+      assertTrue(tables.contains(t2));
+      assertTrue(tables.contains(t3));
+      assertFalse(tables.contains(((DatabaseImpl)db).getSystemCatalog()));
+      
+      tables = getTables(db.newIterable().setIncludeNormalTables(false));
+      assertEquals(2, tables.size());
+      assertFalse(tables.contains(t1));
+      assertTrue(tables.contains(t2));
+      assertTrue(tables.contains(t3));
+      assertFalse(tables.contains(((DatabaseImpl)db).getSystemCatalog()));
+      
+      tables = getTables(db.newIterable().setIncludeLinkedTables(false));
+      assertEquals(1, tables.size());
+      assertTrue(tables.contains(t1));
+      assertFalse(tables.contains(t2));
+      assertFalse(tables.contains(t3));
+      assertFalse(tables.contains(((DatabaseImpl)db).getSystemCatalog()));
+      
+      tables = getTables(db.newIterable().setIncludeLinkedTables(false)
+                         .setIncludeNormalTables(false)
+                         .setIncludeSystemTables(true));
+      assertTrue(tables.size() > 5);
+      assertFalse(tables.contains(t1));
+      assertFalse(tables.contains(t2));
+      assertFalse(tables.contains(t3));
+      assertTrue(tables.contains(((DatabaseImpl)db).getSystemCatalog()));
       
       db.close();
     }
   }
 
+  private static List<Table> getTables(Iterable<Table> tableIter)
+  {
+    List<Table> tableList = new ArrayList<Table>();
+    for(Table t : tableIter) {
+      tableList.add(t);
+    }
+    return tableList;
+  }
+  
   public void testTimeZone() throws Exception
   {
     TimeZone tz = TimeZone.getTimeZone("America/New_York");