aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/java/com/healthmarketscience/jackcess/Joiner.java27
-rw-r--r--src/java/com/healthmarketscience/jackcess/Table.java36
-rw-r--r--test/src/java/com/healthmarketscience/jackcess/IndexTest.java6
-rw-r--r--test/src/java/com/healthmarketscience/jackcess/JoinerTest.java5
4 files changed, 71 insertions, 3 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)
*/
diff --git a/test/src/java/com/healthmarketscience/jackcess/IndexTest.java b/test/src/java/com/healthmarketscience/jackcess/IndexTest.java
index 21e4fd9..8f078f6 100644
--- a/test/src/java/com/healthmarketscience/jackcess/IndexTest.java
+++ b/test/src/java/com/healthmarketscience/jackcess/IndexTest.java
@@ -88,14 +88,20 @@ public class IndexTest extends TestCase {
for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) {
Table table = open(testDB).getTable("Table1");
Map<String, Boolean> foundPKs = new HashMap<String, Boolean>();
+ Index pkIndex = null;
for(Index index : table.getIndexes()) {
foundPKs.put(index.getColumns().iterator().next().getName(),
index.isPrimaryKey());
+ if(index.isPrimaryKey()) {
+ pkIndex= index;
+
+ }
}
Map<String, Boolean> expectedPKs = new HashMap<String, Boolean>();
expectedPKs.put("A", Boolean.TRUE);
expectedPKs.put("B", Boolean.FALSE);
assertEquals(expectedPKs, foundPKs);
+ assertSame(pkIndex, table.getPrimaryKeyIndex());
}
}
diff --git a/test/src/java/com/healthmarketscience/jackcess/JoinerTest.java b/test/src/java/com/healthmarketscience/jackcess/JoinerTest.java
index 1f2961c..87f70fd 100644
--- a/test/src/java/com/healthmarketscience/jackcess/JoinerTest.java
+++ b/test/src/java/com/healthmarketscience/jackcess/JoinerTest.java
@@ -83,7 +83,8 @@ public class JoinerTest extends TestCase {
{
final Set<String> colNames = new HashSet<String>(
Arrays.asList("id", "data"));
-
+
+ Joiner revJoin = join.createReverse();
for(Map<String,Object> row : join.getFromTable()) {
Integer id = (Integer)row.get("id");
@@ -98,6 +99,8 @@ public class JoinerTest extends TestCase {
if(!expectedRows.isEmpty()) {
assertEquals(expectedRows.get(0), join.findFirstRow(row));
+
+ assertEquals(row, revJoin.findFirstRow(expectedRows.get(0)));
} else {
assertNull(join.findFirstRow(row));
}