From 23b2a93764a458bc86ca6121b2b4e81b2f283e32 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Mon, 24 Mar 2014 02:13:09 +0000 Subject: add some convenience methods for working with primary keys git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@850 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../jackcess/CursorBuilder.java | 40 ++++++++++++++++++++++ .../healthmarketscience/jackcess/IndexCursor.java | 11 ++++++ .../jackcess/impl/IndexCursorImpl.java | 9 +++++ 3 files changed, 60 insertions(+) (limited to 'src/main/java') 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 @@ -426,6 +436,36 @@ public class CursorBuilder { return null; } + /** + * 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 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 @@ -36,6 +36,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 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 { -- cgit v1.2.3