From 274737e40f7fe274a2a2eacc51507bb31cd5b06f Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Fri, 15 Mar 2013 18:48:34 +0000 Subject: [PATCH] add addRow FromMap methods, add Row update/delete methods git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/jackcess-2@689 f203690c-595d-4dc9-a70b-905162fa7fd2 --- TODO.txt | 1 + .../healthmarketscience/jackcess/Cursor.java | 18 +++++-- .../healthmarketscience/jackcess/Table.java | 33 +++++++++++++ .../jackcess/impl/CursorImpl.java | 9 +++- .../jackcess/impl/TableImpl.java | 49 ++++++++++++++++++- 5 files changed, 103 insertions(+), 7 deletions(-) diff --git a/TODO.txt b/TODO.txt index 31ae396..a757bda 100644 --- a/TODO.txt +++ b/TODO.txt @@ -42,3 +42,4 @@ Refactor goals: - clean up javadocs - enhance public api classes - add @usage tags to util classes +- add unit tests for Row update/delete methods, add/update *FromMap methods diff --git a/src/java/com/healthmarketscience/jackcess/Cursor.java b/src/java/com/healthmarketscience/jackcess/Cursor.java index 636d604..55efb06 100644 --- a/src/java/com/healthmarketscience/jackcess/Cursor.java +++ b/src/java/com/healthmarketscience/jackcess/Cursor.java @@ -34,10 +34,9 @@ import com.healthmarketscience.jackcess.util.ColumnMatcher; * traversed, row updates may or may not be seen). Multiple cursors may * traverse the same table simultaneously. *

- * The Cursor provides a variety of static utility methods to construct - * cursors with given characteristics or easily search for specific values. - * For even friendlier and more flexible construction, see - * {@link CursorBuilder}. + * The {@link CursorBuilder} provides a variety of static utility methods to + * construct cursors with given characteristics or easily search for specific + * values as well as friendly and flexible construction options. *

* Is not thread-safe. * @@ -224,8 +223,10 @@ public interface Cursor extends Iterable /** * Delete the current row. + *

+ * Note, re-deleting an already deleted row is allowed (it does nothing). * @throws IllegalStateException if the current row is not valid (at - * beginning or end of table), or already deleted. + * beginning or end of table) */ public void deleteCurrentRow() throws IOException; @@ -236,6 +237,13 @@ public interface Cursor extends Iterable */ public void updateCurrentRow(Object... row) throws IOException; + /** + * Update the current row. + * @throws IllegalStateException if the current row is not valid (at + * beginning or end of table), or deleted. + */ + public void updateCurrentRowFromMap(Map row) throws IOException; + /** * Moves to the next row in the table and returns it. * @return The next row in this table (Column name -> Column value), or diff --git a/src/java/com/healthmarketscience/jackcess/Table.java b/src/java/com/healthmarketscience/jackcess/Table.java index 20d9e4f..616cac8 100644 --- a/src/java/com/healthmarketscience/jackcess/Table.java +++ b/src/java/com/healthmarketscience/jackcess/Table.java @@ -171,6 +171,15 @@ public interface Table extends Iterable */ public void addRow(Object... row) throws IOException; + /** + * Calls {@link #asRow} on the given row map and passes the result to {@link + * #addRow}. + *

+ * Note, if this table has an auto-number column, the value generated will be + * put back into the given row map. + */ + public void addRowFromMap(Map row) throws IOException; + /** * Add multiple rows to this table, only writing to disk after all * rows have been written, and every time a data page is filled. This @@ -189,6 +198,30 @@ public interface Table extends Iterable */ public void addRows(List rows) throws IOException; + /** + * Calls {@link #asRow} on the given row maps and passes the results to + * {@link #addRows}. + *

+ * Note, if this table has an auto-number column, the values generated will + * be put back into the appropriate row maps. + */ + public void addRowsFromMaps(List> rows) + throws IOException; + + /** + * Update the given row. Provided Row must have previously been returned + * from this Table. + * @throws IllegalStateException if the given row is not valid, or deleted. + */ + public void updateRow(Row row) throws IOException; + + /** + * Delete the given row. Provided Row must have previously been returned + * from this Table. + * @throws IllegalStateException if the given row is not valid + */ + public void deleteRow(Row row) throws IOException; + /** * After calling this method, {@link #getNextRow} will return the first row * in the table, see {@link Cursor#reset} (uses the default cursor). diff --git a/src/java/com/healthmarketscience/jackcess/impl/CursorImpl.java b/src/java/com/healthmarketscience/jackcess/impl/CursorImpl.java index 8d0b4b6..77e4798 100644 --- a/src/java/com/healthmarketscience/jackcess/impl/CursorImpl.java +++ b/src/java/com/healthmarketscience/jackcess/impl/CursorImpl.java @@ -43,7 +43,6 @@ import com.healthmarketscience.jackcess.util.ErrorHandler; import com.healthmarketscience.jackcess.util.ColumnMatcher; import com.healthmarketscience.jackcess.Column; import com.healthmarketscience.jackcess.Row; -import com.healthmarketscience.jackcess.RowId; import com.healthmarketscience.jackcess.Index; import com.healthmarketscience.jackcess.util.SimpleColumnMatcher; @@ -287,6 +286,10 @@ public abstract class CursorImpl implements Cursor } return null; } + + public RowState getRowState() { + return _rowState; + } public IdImpl getId() { return _id; @@ -588,6 +591,10 @@ public abstract class CursorImpl implements Cursor _table.updateRow(_rowState, _curPos.getRowId(), row); } + public void updateCurrentRowFromMap(Map row) throws IOException { + _table.updateRow(_rowState, _curPos.getRowId(), _table.asUpdateRow(row)); + } + public Row getNextRow() throws IOException { return getNextRow(null); } diff --git a/src/java/com/healthmarketscience/jackcess/impl/TableImpl.java b/src/java/com/healthmarketscience/jackcess/impl/TableImpl.java index 7f99138..140f5d4 100644 --- a/src/java/com/healthmarketscience/jackcess/impl/TableImpl.java +++ b/src/java/com/healthmarketscience/jackcess/impl/TableImpl.java @@ -466,6 +466,10 @@ public class TableImpl implements Table getDefaultCursor().reset(); } + public void deleteRow(Row row) throws IOException { + deleteRow(getDefaultCursor().getRowState(), (RowIdImpl)row.getId()); + } + /** * Delete the row on which the given rowState is currently positioned. *

@@ -482,6 +486,10 @@ public class TableImpl implements Table // ensure that the relevant row state is up-to-date ByteBuffer rowBuffer = positionAtRowHeader(rowState, rowId); + if(rowState.isDeleted()) { + // don't care about duplicate deletion + return; + } requireNonDeletedRow(rowState, rowId); // delete flag always gets set in the "header" row (even if data is on @@ -1284,11 +1292,45 @@ public class TableImpl implements Table public void addRow(Object... row) throws IOException { addRows(Collections.singletonList(row), _singleRowBufferH); } - + + public void addRowFromMap(Map row) throws IOException { + Object[] rowValues = asRow(row); + + addRow(rowValues); + + returnAutoNumValues(row, rowValues); + } + public void addRows(List rows) throws IOException { addRows(rows, _multiRowBufferH); } + public void addRowsFromMaps(List> rows) + throws IOException + { + List rowValuesList = new ArrayList(rows.size()); + for(Map row : rows) { + rowValuesList.add(asRow(row)); + } + + addRows(rowValuesList); + + if(!_autoNumColumns.isEmpty()) { + for(int i = 0; i < rowValuesList.size(); ++i) { + Map row = rows.get(i); + Object[] rowValues = rowValuesList.get(i); + returnAutoNumValues(row, rowValues); + } + } + } + + private void returnAutoNumValues(Map row, Object[] rowValues) + { + for(ColumnImpl col : _autoNumColumns) { + col.setRowValue(row, col.getRowValue(rowValues)); + } + } + /** * Add multiple rows to this table, only writing to disk after all * rows have been written, and every time a data page is filled. @@ -1364,6 +1406,11 @@ public class TableImpl implements Table updateTableDefinition(rows.size()); } + public void updateRow(Row row) throws IOException { + updateRow(getDefaultCursor().getRowState(), (RowIdImpl)row.getId(), + asUpdateRow(row)); + } + /** * Update the row on which the given rowState is currently positioned. *

-- 2.39.5