summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Ahlborn <jtahlborn@yahoo.com>2009-11-17 01:33:30 +0000
committerJames Ahlborn <jtahlborn@yahoo.com>2009-11-17 01:33:30 +0000
commitefca0b593bc32e274b0923b87ecba989ca0406b4 (patch)
treebfed8b267c8e0a434a569eb902dd2e6c8b0f5b82
parenta49de754bce849d94719a473567e78b595fbdc9a (diff)
downloadjackcess-efca0b593bc32e274b0923b87ecba989ca0406b4.tar.gz
jackcess-efca0b593bc32e274b0923b87ecba989ca0406b4.zip
add some more tests/utility code for row updates
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@410 f203690c-595d-4dc9-a70b-905162fa7fd2
-rw-r--r--src/java/com/healthmarketscience/jackcess/Cursor.java19
-rw-r--r--src/java/com/healthmarketscience/jackcess/Table.java18
-rw-r--r--test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java64
3 files changed, 81 insertions, 20 deletions
diff --git a/src/java/com/healthmarketscience/jackcess/Cursor.java b/src/java/com/healthmarketscience/jackcess/Cursor.java
index 8e7f61f..f05c689 100644
--- a/src/java/com/healthmarketscience/jackcess/Cursor.java
+++ b/src/java/com/healthmarketscience/jackcess/Cursor.java
@@ -28,6 +28,7 @@ King of Prussia, PA 19406
package com.healthmarketscience.jackcess;
import java.io.IOException;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
@@ -885,6 +886,20 @@ public abstract class Cursor implements Iterable<Map<String, Object>>
}
/**
+ * Updates a single value in the current row.
+ * @throws IllegalStateException if the current row is not valid (at
+ * beginning or end of table), or deleted.
+ */
+ public void setCurrentRowValue(Column column, Object value)
+ throws IOException
+ {
+ Object[] row = new Object[_table.getColumnCount()];
+ Arrays.fill(row, Column.KEEP_VALUE);
+ row[column.getColumnIndex()] = value;
+ _table.updateRow(_rowState, _curPos.getRowId(), row);
+ }
+
+ /**
* Returns {@code true} if this cursor is up-to-date with respect to the
* relevant table and related table objects, {@code false} otherwise.
*/
@@ -902,8 +917,8 @@ public abstract class Cursor implements Iterable<Map<String, Object>>
* Finds the next non-deleted row after the given row (as defined by this
* cursor) and returns the id of the row, where "next" may be backwards if
* moveForward is {@code false}. If there are no more rows, the returned
- * rowId should equal the value returned by {@link #getLastPosition} if moving
- * forward and {@link #getFirstPosition} if moving backward.
+ * rowId should equal the value returned by {@link #getLastPosition} if
+ * moving forward and {@link #getFirstPosition} if moving backward.
*/
protected abstract Position findAnotherPosition(RowState rowState,
Position curPos,
diff --git a/src/java/com/healthmarketscience/jackcess/Table.java b/src/java/com/healthmarketscience/jackcess/Table.java
index efc9a1a..a358406 100644
--- a/src/java/com/healthmarketscience/jackcess/Table.java
+++ b/src/java/com/healthmarketscience/jackcess/Table.java
@@ -1152,6 +1152,24 @@ public class Table
}
/**
+ * Converts a map of columnName -> columnValue to an array of row values
+ * appropriate for a call to {@link #updateCurrentRow(Object...)}.
+ */
+ public Object[] asUpdateRow(Map<String,Object> rowMap) {
+ Object[] row = new Object[_columns.size()];
+ Arrays.fill(row, Column.KEEP_VALUE);
+ if(rowMap == null) {
+ return row;
+ }
+ for(Column col : _columns) {
+ if(rowMap.containsKey(col.getName())) {
+ row[col.getColumnIndex()] = rowMap.get(col.getName());
+ }
+ }
+ return row;
+ }
+
+ /**
* Add a single row to this table and write it to disk
* <p>
* Note, if this table has an auto-number column, the value written will be
diff --git a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java
index fac88f3..cb42a90 100644
--- a/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java
+++ b/test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java
@@ -160,7 +160,7 @@ public class DatabaseTest extends TestCase {
}
public void testGetColumns() throws Exception {
- List columns = open().getTable("Table1").getColumns();
+ List<Column> columns = open().getTable("Table1").getColumns();
assertEquals(9, columns.size());
checkColumn(columns, 0, "A", DataType.TEXT);
checkColumn(columns, 1, "B", DataType.TEXT);
@@ -173,11 +173,11 @@ public class DatabaseTest extends TestCase {
checkColumn(columns, 8, "I", DataType.BOOLEAN);
}
- static void checkColumn(List columns, int columnNumber, String name,
+ static void checkColumn(List<Column> columns, int columnNumber, String name,
DataType dataType)
throws Exception
{
- Column column = (Column) columns.get(columnNumber);
+ Column column = columns.get(columnNumber);
assertEquals(name, column.getName());
assertEquals(dataType, column.getType());
}
@@ -461,7 +461,7 @@ public class DatabaseTest extends TestCase {
File bogusFile = new File("fooby-dooby.mdb");
assertTrue(!bogusFile.exists());
try {
- Database db = open(bogusFile);
+ open(bogusFile);
fail("FileNotFoundException should have been thrown");
} catch(FileNotFoundException e) {
}
@@ -930,46 +930,74 @@ public class DatabaseTest extends TestCase {
t.addRow("row" + i, Column.AUTO_NUMBER, "initial data");
}
- t.reset();
- t.getNextRow();
- Map<String,Object> row = t.getNextRow();
+ Cursor c = Cursor.createCursor(t);
+ c.reset();
+ c.moveNextRows(2);
+ Map<String,Object> row = c.getCurrentRow();
assertEquals(createExpectedRow("name", "row1",
"id", 2,
"data", "initial data"),
row);
- t.updateCurrentRow(Column.KEEP_VALUE, Column.AUTO_NUMBER, "new data");
+ c.updateCurrentRow(Column.KEEP_VALUE, Column.AUTO_NUMBER, "new data");
- t.getNextRow();
- t.getNextRow();
- row = t.getNextRow();
+ c.moveNextRows(3);
+ row = c.getCurrentRow();
assertEquals(createExpectedRow("name", "row4",
"id", 5,
"data", "initial data"),
row);
- t.updateCurrentRow(Column.KEEP_VALUE, Column.AUTO_NUMBER, "a larger amount of new data");
+ c.updateCurrentRow(Column.KEEP_VALUE, Column.AUTO_NUMBER, "a larger amount of new data");
- t.reset();
- t.getNextRow();
- row = t.getNextRow();
+ c.reset();
+ c.moveNextRows(2);
+ row = c.getCurrentRow();
assertEquals(createExpectedRow("name", "row1",
"id", 2,
"data", "new data"),
row);
- t.getNextRow();
- t.getNextRow();
- row = t.getNextRow();
+ c.moveNextRows(3);
+ row = c.getCurrentRow();
assertEquals(createExpectedRow("name", "row4",
"id", 5,
"data", "a larger amount of new data"),
row);
+ t.reset();
+
+ String str = createString(100);
+ for(int i = 10; i < 50; ++i) {
+ t.addRow("row" + i, Column.AUTO_NUMBER, "big data_" + str);
+ }
+
+ c.reset();
+ c.moveNextRows(9);
+ row = c.getCurrentRow();
+
+ assertEquals(createExpectedRow("name", "row8",
+ "id", 9,
+ "data", "initial data"),
+ row);
+
+ String newText = "updated big data_" + createString(200);
+
+ c.setCurrentRowValue(t.getColumn("data"), newText);
+
+ c.reset();
+ c.moveNextRows(9);
+ row = c.getCurrentRow();
+
+ assertEquals(createExpectedRow("name", "row8",
+ "id", 9,
+ "data", newText),
+ row);
+
db.close();
}