summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Ahlborn <jtahlborn@yahoo.com>2013-03-15 18:48:34 +0000
committerJames Ahlborn <jtahlborn@yahoo.com>2013-03-15 18:48:34 +0000
commit274737e40f7fe274a2a2eacc51507bb31cd5b06f (patch)
tree254fc38655cfed3f37e5acc34fd7eba3e22d1d0a
parentc935d2bdbad770238f6d25208999ceab75197ffa (diff)
downloadjackcess-274737e40f7fe274a2a2eacc51507bb31cd5b06f.tar.gz
jackcess-274737e40f7fe274a2a2eacc51507bb31cd5b06f.zip
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
-rw-r--r--TODO.txt1
-rw-r--r--src/java/com/healthmarketscience/jackcess/Cursor.java18
-rw-r--r--src/java/com/healthmarketscience/jackcess/Table.java33
-rw-r--r--src/java/com/healthmarketscience/jackcess/impl/CursorImpl.java9
-rw-r--r--src/java/com/healthmarketscience/jackcess/impl/TableImpl.java49
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.
* <p>
- * 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.
* <p>
* Is not thread-safe.
*
@@ -224,8 +223,10 @@ public interface Cursor extends Iterable<Row>
/**
* Delete the current row.
+ * <p/>
+ * 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;
@@ -237,6 +238,13 @@ public interface Cursor extends Iterable<Row>
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<String,?> 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
* {@code null} if no next row is found
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
@@ -172,6 +172,15 @@ public interface Table extends Iterable<Row>
public void addRow(Object... row) throws IOException;
/**
+ * Calls {@link #asRow} on the given row map and passes the result to {@link
+ * #addRow}.
+ * <p/>
+ * 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<String,Object> 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
* is much more efficient than calling <code>addRow</code> multiple times.
@@ -190,6 +199,30 @@ public interface Table extends Iterable<Row>
public void addRows(List<? extends Object[]> rows) throws IOException;
/**
+ * Calls {@link #asRow} on the given row maps and passes the results to
+ * {@link #addRows}.
+ * <p/>
+ * 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<? extends Map<String,Object>> 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).
* @usage _general_method_
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<String,?> 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.
* <p>
@@ -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<String,Object> row) throws IOException {
+ Object[] rowValues = asRow(row);
+
+ addRow(rowValues);
+
+ returnAutoNumValues(row, rowValues);
+ }
+
public void addRows(List<? extends Object[]> rows) throws IOException {
addRows(rows, _multiRowBufferH);
}
+ public void addRowsFromMaps(List<? extends Map<String,Object>> rows)
+ throws IOException
+ {
+ List<Object[]> rowValuesList = new ArrayList<Object[]>(rows.size());
+ for(Map<String,Object> row : rows) {
+ rowValuesList.add(asRow(row));
+ }
+
+ addRows(rowValuesList);
+
+ if(!_autoNumColumns.isEmpty()) {
+ for(int i = 0; i < rowValuesList.size(); ++i) {
+ Map<String,Object> row = rows.get(i);
+ Object[] rowValues = rowValuesList.get(i);
+ returnAutoNumValues(row, rowValues);
+ }
+ }
+ }
+
+ private void returnAutoNumValues(Map<String,Object> 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.
* <p>