]> source.dussan.org Git - jackcess.git/commitdiff
add addRow FromMap methods, add Row update/delete methods
authorJames Ahlborn <jtahlborn@yahoo.com>
Fri, 15 Mar 2013 18:48:34 +0000 (18:48 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Fri, 15 Mar 2013 18:48:34 +0000 (18:48 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/jackcess-2@689 f203690c-595d-4dc9-a70b-905162fa7fd2

TODO.txt
src/java/com/healthmarketscience/jackcess/Cursor.java
src/java/com/healthmarketscience/jackcess/Table.java
src/java/com/healthmarketscience/jackcess/impl/CursorImpl.java
src/java/com/healthmarketscience/jackcess/impl/TableImpl.java

index 31ae396b5328e5f1a94373d19ac809f021d956ee..a757bdac6babe7f0b8bb2c867b39e80d1d7d7867 100644 (file)
--- 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
index 636d604c94520a2c54bfd698e1e9b52bedb586b2..55efb065cb473a6fc18ff429e7030430aab4b3bd 100644 (file)
@@ -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;
 
@@ -236,6 +237,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
index 20d9e4fc0e85b54cfa8b9e046690da729e9e292f..616cac81e4b3bfb3434f6b527378897a4011c061 100644 (file)
@@ -171,6 +171,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
@@ -189,6 +198,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).
index 8d0b4b6e8358840c6f87d8a21d3e8aebb69c65a8..77e47980139097fe48eb5cfdf5fc16f9b819cb0c 100644 (file)
@@ -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);
   }
index 7f991384dc85f95fe788e49b9886c3e7dc3a5e06..140f5d4f25d89fd7ff8b63de4879072cae8f1579 100644 (file)
@@ -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>