]> source.dussan.org Git - jackcess.git/commitdiff
add more flexible table iteration, feature #28
authorJames Ahlborn <jtahlborn@yahoo.com>
Sat, 14 Feb 2015 02:53:07 +0000 (02:53 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Sat, 14 Feb 2015 02:53:07 +0000 (02:53 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@916 f203690c-595d-4dc9-a70b-905162fa7fd2

src/main/java/com/healthmarketscience/jackcess/Database.java
src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java
src/main/java/com/healthmarketscience/jackcess/util/TableIterableBuilder.java [new file with mode: 0644]

index 808dac76b4cddb160130f822d74204912427987d..ddb7f38916becbd2aaa85a59f77cd896cdad8a60 100644 (file)
@@ -36,6 +36,7 @@ import com.healthmarketscience.jackcess.impl.DatabaseImpl;
 import com.healthmarketscience.jackcess.util.ColumnValidatorFactory;
 import com.healthmarketscience.jackcess.util.ErrorHandler;
 import com.healthmarketscience.jackcess.util.LinkResolver;
+import com.healthmarketscience.jackcess.util.TableIterableBuilder;
 
 /**
  * An Access database instance.  A new instance can be instantiated by opening
@@ -179,6 +180,13 @@ public interface Database extends Iterable<Table>, Closeable, Flushable
    */
   public Iterator<Table> iterator();
 
+  /**
+   * Convenience method for constructing a new TableIterableBuilder for this
+   * cursor.  A TableIterableBuilder provides a variety of options for more
+   * flexible iteration of Tables.
+   */
+  public TableIterableBuilder newIterable();
+  
   /**
    * @param name Table name (case-insensitive)
    * @return The table, or null if it doesn't exist
index aa7589660e10a1d9aa689128078d5bd95a66a845..969f93d91b80706f4db425a04082204a90140b4b 100644 (file)
@@ -77,6 +77,7 @@ import com.healthmarketscience.jackcess.util.ColumnValidatorFactory;
 import com.healthmarketscience.jackcess.util.ErrorHandler;
 import com.healthmarketscience.jackcess.util.LinkResolver;
 import com.healthmarketscience.jackcess.util.SimpleColumnValidatorFactory;
+import com.healthmarketscience.jackcess.util.TableIterableBuilder;
 import org.apache.commons.lang.builder.ToStringBuilder;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -844,32 +845,53 @@ public class DatabaseImpl implements Database
     }
 
     if (LOG.isDebugEnabled()) {
-      LOG.debug("Finished reading system catalog.  Tables: " +
-                getTableNames());
+      LOG.debug("Finished reading system catalog.  Tables: " + getTableNames());
     }
   }
   
   public Set<String> getTableNames() throws IOException {
     if(_tableNames == null) {
-      Set<String> tableNames =
-        new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
-      _tableFinder.getTableNames(tableNames, false);
-      _tableNames = tableNames;
+      _tableNames = getTableNames(true, false, true);
     }
     return _tableNames;
   }
 
   public Set<String> getSystemTableNames() throws IOException {
-    Set<String> sysTableNames =
-      new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
-    _tableFinder.getTableNames(sysTableNames, true);
-    return sysTableNames;
+    return getTableNames(false, true, false);
+  }
+
+  private Set<String> getTableNames(boolean normalTables, boolean systemTables,
+                                    boolean linkedTables)
+    throws IOException
+  {
+    Set<String> tableNames = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
+    _tableFinder.getTableNames(tableNames, normalTables, systemTables,
+                               linkedTables);
+    return tableNames;
   }
 
   public Iterator<Table> iterator() {
-    return new TableIterator();
+    try {
+      return new TableIterator(getTableNames());
+    } catch(IOException e) {
+      throw new RuntimeIOException(e);
+    }
   }
 
+  public Iterator<Table> iterator(TableIterableBuilder builder) {
+    try {
+      return new TableIterator(getTableNames(builder.isIncludeNormalTables(),
+                                             builder.isIncludeSystemTables(),
+                                             builder.isIncludeLinkedTables()));
+    } catch(IOException e) {
+      throw new RuntimeIOException(e);
+    }
+  }
+
+  public TableIterableBuilder newIterable() {
+    return new TableIterableBuilder(this);
+  }
+  
   public TableImpl getTable(String name) throws IOException {
     return getTable(name, false);
   }
@@ -1834,12 +1856,8 @@ public class DatabaseImpl implements Database
   {
     private Iterator<String> _tableNameIter;
 
-    private TableIterator() {
-      try {
-        _tableNameIter = getTableNames().iterator();
-      } catch(IOException e) {
-        throw new RuntimeIOException(e);
-      }
+    private TableIterator(Set<String> tableNames) {
+      _tableNameIter = tableNames.iterator();
     }
 
     public boolean hasNext() {
@@ -1895,7 +1913,9 @@ public class DatabaseImpl implements Database
     }
 
     public void getTableNames(Set<String> tableNames,
-                              boolean systemTables)
+                              boolean normalTables,
+                              boolean systemTables,
+                              boolean linkedTables)
       throws IOException
     {
       for(Row row : getTableNamesCursor().newIterable().setColumnNames(
@@ -1906,8 +1926,19 @@ public class DatabaseImpl implements Database
         Short type = row.getShort(CAT_COL_TYPE);
         int parentId = row.getInt(CAT_COL_PARENT_ID);
 
-        if((parentId == _tableParentId) && isTableType(type) && 
-           (isSystemObject(flags) == systemTables)) {
+        if(parentId != _tableParentId) {
+          continue;
+        }
+
+        if(TYPE_TABLE.equals(type)) {
+          if(!isSystemObject(flags)) {
+            if(normalTables) {
+              tableNames.add(tableName);
+            }
+          } else if(systemTables) {
+            tableNames.add(tableName);
+          }
+        } else if(TYPE_LINKED_TABLE.equals(type) && linkedTables) {
           tableNames.add(tableName);
         }
       }
diff --git a/src/main/java/com/healthmarketscience/jackcess/util/TableIterableBuilder.java b/src/main/java/com/healthmarketscience/jackcess/util/TableIterableBuilder.java
new file mode 100644 (file)
index 0000000..0390ed0
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+Copyright (c) 2015 James Ahlborn
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
+USA
+*/
+
+package com.healthmarketscience.jackcess.util;
+
+import java.util.Iterator;
+
+import com.healthmarketscience.jackcess.Database;
+import com.healthmarketscience.jackcess.Table;
+import com.healthmarketscience.jackcess.impl.DatabaseImpl;
+
+/**
+ * Builder style class for constructing a {@link Database} Iterable/Iterator
+ * for {@link Table}s.  By default, normal (non-system, non-linked tables) and
+ * linked tables are included and system tables are not.
+ *
+ * @author James Ahlborn
+ * @usage _general_class_
+ */
+public class TableIterableBuilder implements Iterable<Table>
+{
+  private final Database _db;
+  private boolean _includeNormalTables = true;
+  private boolean _includeSystemTables;
+  private boolean _includeLinkedTables = true;
+  
+  public TableIterableBuilder(Database db) {
+    _db = db;
+  }
+
+  public boolean isIncludeNormalTables() {
+    return _includeNormalTables;
+  }
+  
+  public boolean isIncludeSystemTables() {
+    return _includeSystemTables;
+  }
+  
+  public boolean isIncludeLinkedTables() {
+    return _includeLinkedTables;
+  }
+  
+  public TableIterableBuilder setIncludeNormalTables(boolean includeNormalTables) {
+    _includeNormalTables = includeNormalTables;
+    return this;
+  }
+  
+  public TableIterableBuilder setIncludeSystemTables(boolean includeSystemTables) {
+    _includeSystemTables = includeSystemTables;
+    return this;
+  }
+
+  public TableIterableBuilder setIncludeLinkedTables(boolean includeLinkedTables) {
+    _includeLinkedTables = includeLinkedTables;
+    return this;
+  }
+  
+  public Iterator<Table> iterator() {
+    return ((DatabaseImpl)_db).iterator(this);
+  }
+}