aboutsummaryrefslogtreecommitdiffstats
path: root/src/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/java')
-rw-r--r--src/java/com/healthmarketscience/jackcess/Joiner.java27
-rw-r--r--src/java/com/healthmarketscience/jackcess/Table.java36
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)
*/