Browse Source

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
tags/jackcess-2.0.4
James Ahlborn 10 years ago
parent
commit
23b2a93764

+ 40
- 0
src/main/java/com/healthmarketscience/jackcess/CursorBuilder.java View File

@@ -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

+ 11
- 0
src/main/java/com/healthmarketscience/jackcess/IndexCursor.java View File

@@ -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

+ 9
- 0
src/main/java/com/healthmarketscience/jackcess/impl/IndexCursorImpl.java View File

@@ -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
{

+ 2
- 2
src/test/java/com/healthmarketscience/jackcess/IndexTest.java View File

@@ -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,

Loading…
Cancel
Save