diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2015-02-14 02:53:07 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2015-02-14 02:53:07 +0000 |
commit | 54713bcd6c5f1def3e926a93caf1afba5a91f1f9 (patch) | |
tree | b9a40cfce94b1a54a26598be7ea1881ce9864a38 /src | |
parent | 535c89ba66dc45f3b6cdf420d7f2ba30d6e01087 (diff) | |
download | jackcess-54713bcd6c5f1def3e926a93caf1afba5a91f1f9.tar.gz jackcess-54713bcd6c5f1def3e926a93caf1afba5a91f1f9.zip |
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
Diffstat (limited to 'src')
3 files changed, 136 insertions, 20 deletions
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 @@ -180,6 +181,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 * @usage _general_method_ 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<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 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<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); + } +} |