diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2011-06-17 03:06:16 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2011-06-17 03:06:16 +0000 |
commit | 82caf7c825cb6eb6aa97131534e2214cad12c8ba (patch) | |
tree | 725f9b59c6f7bde52214cce0b968508b360256a2 /src/java/com | |
parent | 69f4ea2454423c631c69444018d850428cfc09d0 (diff) | |
download | jackcess-82caf7c825cb6eb6aa97131534e2214cad12c8ba.tar.gz jackcess-82caf7c825cb6eb6aa97131534e2214cad12c8ba.zip |
add a few util methods related to indexes and joins
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@566 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'src/java/com')
-rw-r--r-- | src/java/com/healthmarketscience/jackcess/Joiner.java | 27 | ||||
-rw-r--r-- | src/java/com/healthmarketscience/jackcess/Table.java | 36 |
2 files changed, 61 insertions, 2 deletions
diff --git a/src/java/com/healthmarketscience/jackcess/Joiner.java b/src/java/com/healthmarketscience/jackcess/Joiner.java index afe445d..89da959 100644 --- a/src/java/com/healthmarketscience/jackcess/Joiner.java +++ b/src/java/com/healthmarketscience/jackcess/Joiner.java @@ -47,9 +47,24 @@ public class Joiner } /** + * Creates a new Joiner based on the foreign-key relationship between the + * given "from"" table and the given "to"" table. + * + * @param fromTable the "from" side of the relationship + * @param toTable the "to" side of the relationship + * @throws IllegalArgumentException if there is no relationship between the + * given tables + */ + public static Joiner create(Table fromTable, Table toTable) + throws IOException + { + return create(fromTable.getForeignKeyIndex(toTable)); + } + + /** * Creates a new Joiner based on the given index which backs a foreign-key * relationship. The table of the given index will be the "from" table and - * the table on the other end of the relationsip is the "to" table. + * the table on the other end of the relationship will be the "to" table. * * @param fromIndex the index backing one side of a foreign-key relationship */ @@ -63,6 +78,16 @@ public class Joiner toCursor.setColumnMatcher(CaseInsensitiveColumnMatcher.INSTANCE); return new Joiner(fromIndex, toCursor); } + + /** + * Creates a new Joiner that is the reverse of this Joiner (the "from" and + * "to" tables are swapped). + */ + public Joiner createReverse() + throws IOException + { + return create(getToTable(), getFromTable()); + } public Table getFromTable() { diff --git a/src/java/com/healthmarketscience/jackcess/Table.java b/src/java/com/healthmarketscience/jackcess/Table.java index 1d1c7a3..34b3c27 100644 --- a/src/java/com/healthmarketscience/jackcess/Table.java +++ b/src/java/com/healthmarketscience/jackcess/Table.java @@ -367,6 +367,7 @@ public class Table /** * @return the index with the given name + * @throws IllegalArgumentException if there is no index with the given name */ public Index getIndex(String name) { for(Index index : _indexes) { @@ -377,7 +378,40 @@ public class Table throw new IllegalArgumentException("Index with name " + name + " does not exist on this table"); } - + + /** + * @return the primary key index for this table + * @throws IllegalArgumentException if there is no primary key index on this + * table + */ + public Index getPrimaryKeyIndex() { + for(Index index : _indexes) { + if(index.isPrimaryKey()) { + return index; + } + } + throw new IllegalArgumentException("Table " + getName() + + " does not have a primary key index"); + } + + /** + * @return the foreign key index joining this table to the given other table + * @throws IllegalArgumentException if there is no relationship between this + * table and the given table + */ + public Index getForeignKeyIndex(Table otherTable) { + for(Index index : _indexes) { + if(index.isForeignKey() && (index.getReference() != null) && + (index.getReference().getOtherTablePageNumber() == + otherTable.getTableDefPageNumber())) { + return index; + } + } + throw new IllegalArgumentException( + "Table " + getName() + " does not have a foreign key reference to " + + otherTable.getName()); + } + /** * @return All of the IndexData on this table (unmodifiable List) */ |