package com.healthmarketscience.jackcess;
import java.io.IOException;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
return _table.getRowValue(_rowState, _curPos.getRowId(), column);
}
+ /**
+ * 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.
* 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,
}
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);
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());
}
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) {
}
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();
}