From 82caf7c825cb6eb6aa97131534e2214cad12c8ba Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Fri, 17 Jun 2011 03:06:16 +0000 Subject: [PATCH] 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 --- .../healthmarketscience/jackcess/Joiner.java | 27 +++++++++++++- .../healthmarketscience/jackcess/Table.java | 36 ++++++++++++++++++- .../jackcess/IndexTest.java | 6 ++++ .../jackcess/JoinerTest.java | 5 ++- 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 @@ -46,10 +46,25 @@ public class Joiner _toCursor = toCursor; } + /** + * 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 foundPKs = new HashMap(); + Index pkIndex = null; for(Index index : table.getIndexes()) { foundPKs.put(index.getColumns().iterator().next().getName(), index.isPrimaryKey()); + if(index.isPrimaryKey()) { + pkIndex= index; + + } } Map expectedPKs = new HashMap(); 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 colNames = new HashSet( Arrays.asList("id", "data")); - + + Joiner revJoin = join.createReverse(); for(Map 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)); } -- 2.39.5