From: James Ahlborn Date: Sat, 14 Feb 2015 02:53:07 +0000 (+0000) Subject: add more flexible table iteration, feature #28 X-Git-Tag: jackcess-2.0.9~10 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=54713bcd6c5f1def3e926a93caf1afba5a91f1f9;p=jackcess.git add more flexible table iteration, feature #28 git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@916 f203690c-595d-4dc9-a70b-905162fa7fd2 --- diff --git a/src/main/java/com/healthmarketscience/jackcess/Database.java b/src/main/java/com/healthmarketscience/jackcess/Database.java index 808dac7..ddb7f38 100644 --- a/src/main/java/com/healthmarketscience/jackcess/Database.java +++ b/src/main/java/com/healthmarketscience/jackcess/Database.java @@ -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, Closeable, Flushable */ public Iterator
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 diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java index aa75896..969f93d 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java @@ -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 getTableNames() throws IOException { if(_tableNames == null) { - Set tableNames = - new TreeSet(String.CASE_INSENSITIVE_ORDER); - _tableFinder.getTableNames(tableNames, false); - _tableNames = tableNames; + _tableNames = getTableNames(true, false, true); } return _tableNames; } public Set getSystemTableNames() throws IOException { - Set sysTableNames = - new TreeSet(String.CASE_INSENSITIVE_ORDER); - _tableFinder.getTableNames(sysTableNames, true); - return sysTableNames; + return getTableNames(false, true, false); + } + + private Set getTableNames(boolean normalTables, boolean systemTables, + boolean linkedTables) + throws IOException + { + Set tableNames = new TreeSet(String.CASE_INSENSITIVE_ORDER); + _tableFinder.getTableNames(tableNames, normalTables, systemTables, + linkedTables); + return tableNames; } public Iterator
iterator() { - return new TableIterator(); + try { + return new TableIterator(getTableNames()); + } catch(IOException e) { + throw new RuntimeIOException(e); + } } + public Iterator
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 _tableNameIter; - private TableIterator() { - try { - _tableNameIter = getTableNames().iterator(); - } catch(IOException e) { - throw new RuntimeIOException(e); - } + private TableIterator(Set tableNames) { + _tableNameIter = tableNames.iterator(); } public boolean hasNext() { @@ -1895,7 +1913,9 @@ public class DatabaseImpl implements Database } public void getTableNames(Set 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 index 0000000..0390ed0 --- /dev/null +++ b/src/main/java/com/healthmarketscience/jackcess/util/TableIterableBuilder.java @@ -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
+{ + 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
iterator() { + return ((DatabaseImpl)_db).iterator(this); + } +}