diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2014-03-24 02:13:09 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2014-03-24 02:13:09 +0000 |
commit | 23b2a93764a458bc86ca6121b2b4e81b2f283e32 (patch) | |
tree | 52d2644869b60b233bf62278fe207f24d5398967 /src/main/java | |
parent | f73e6c33c75c8a0ba444ff200fdc234b59e59027 (diff) | |
download | jackcess-23b2a93764a458bc86ca6121b2b4e81b2f283e32.tar.gz jackcess-23b2a93764a458bc86ca6121b2b4e81b2f283e32.zip |
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
Diffstat (limited to 'src/main/java')
3 files changed, 60 insertions, 0 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 { |