diff options
4 files changed, 62 insertions, 2 deletions
diff --git a/src/main/java/com/healthmarketscience/jackcess/CursorBuilder.java b/src/main/java/com/healthmarketscience/jackcess/CursorBuilder.java index 1c2b312..f9d5c71 100644 --- a/src/main/java/com/healthmarketscience/jackcess/CursorBuilder.java +++ b/src/main/java/com/healthmarketscience/jackcess/CursorBuilder.java @@ -346,6 +346,16 @@ public class CursorBuilder { { return index.getTable().newCursor().setIndex(index).toIndexCursor(); } + + /** + * Creates an indexed cursor for the primary key cursor of the given table. + * @param table the table over which this cursor will traverse + */ + public static IndexCursor createPrimaryKeyCursor(Table table) + throws IOException + { + return createCursor(table.getPrimaryKeyIndex()); + } /** * Creates an indexed cursor for the given table, narrowed to the given @@ -427,6 +437,36 @@ public class CursorBuilder { } /** + * Convenience method for finding a specific row (as defined by the cursor) + * where the index entries match the given values. See {@link + * IndexCursor#findRowByEntry(Object...)} for details on the entryValues. + * + * @param index the index to search + * @param entryValues the column values for the index's columns. + * @return the matching row or {@code null} if a match could not be found. + */ + public static Row findRowByEntry(Index index, Object... entryValues) + throws IOException + { + return createCursor(index).findRowByEntry(entryValues); + } + + /** + * Convenience method for finding a specific row by the primary key of the + * table. See {@link IndexCursor#findRowByEntry(Object...)} for details on + * the entryValues. + * + * @param table the table to search + * @param entryValues the column values for the table's primary key columns. + * @return the matching row or {@code null} if a match could not be found. + */ + public static Row findRowByPrimaryKey(Table table, Object... entryValues) + throws IOException + { + return findRowByEntry(table.getPrimaryKeyIndex(), entryValues); + } + + /** * Convenience method for finding a specific row in a table which matches a * given row "pattern". See {@link Cursor#findFirstRow(Column,Object)} for * details on the pattern. diff --git a/src/main/java/com/healthmarketscience/jackcess/IndexCursor.java b/src/main/java/com/healthmarketscience/jackcess/IndexCursor.java index 108ecdf..5fdcab1 100644 --- a/src/main/java/com/healthmarketscience/jackcess/IndexCursor.java +++ b/src/main/java/com/healthmarketscience/jackcess/IndexCursor.java @@ -37,6 +37,17 @@ public interface IndexCursor extends Cursor public Index getIndex(); /** + * Finds the first row (as defined by the cursor) where the index entries + * match the given values. If a match is not found (or an exception is + * thrown), the cursor is restored to its previous state. + * + * @param entryValues the column values for the index's columns. + * @return the matching row or {@code null} if a match could not be found. + */ + public Row findRowByEntry(Object... entryValues) + throws IOException; + + /** * Moves to the first row (as defined by the cursor) where the index entries * match the given values. If a match is not found (or an exception is * thrown), the cursor is restored to its previous state. diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/IndexCursorImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/IndexCursorImpl.java index 365cd2e..c2f0280 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/IndexCursorImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/IndexCursorImpl.java @@ -125,6 +125,15 @@ public class IndexCursorImpl extends CursorImpl implements IndexCursor return _index; } + public Row findRowByEntry(Object... entryValues) + throws IOException + { + if(findFirstRowByEntry(entryValues)) { + return getCurrentRow(); + } + return null; + } + public boolean findFirstRowByEntry(Object... entryValues) throws IOException { diff --git a/src/test/java/com/healthmarketscience/jackcess/IndexTest.java b/src/test/java/com/healthmarketscience/jackcess/IndexTest.java index 2b325a6..aad0376 100644 --- a/src/test/java/com/healthmarketscience/jackcess/IndexTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/IndexTest.java @@ -538,7 +538,7 @@ public class IndexTest extends TestCase { assertTable(expectedRows, t); - IndexCursor pkCursor = CursorBuilder.createCursor(t.getPrimaryKeyIndex()); + IndexCursor pkCursor = CursorBuilder.createPrimaryKeyCursor(t); assertCursor(expectedRows, pkCursor); assertCursor(expectedRows, @@ -633,7 +633,7 @@ public class IndexTest extends TestCase { assertTable(expectedRows, t); - IndexCursor pkCursor = CursorBuilder.createCursor(t.getPrimaryKeyIndex()); + IndexCursor pkCursor = CursorBuilder.createPrimaryKeyCursor(t); assertCursor(expectedRows, pkCursor); assertCursor(expectedRows, |