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;
}
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);
}
{
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() {
}
public void getTableNames(Set<String> tableNames,
- boolean systemTables)
+ boolean normalTables,
+ boolean systemTables,
+ boolean linkedTables)
throws IOException
{
for(Row row : getTableNamesCursor().newIterable().setColumnNames(
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);
}
}
--- /dev/null
+/*
+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);
+ }
+}