Browse Source

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
tags/jackcess-1.1.20
James Ahlborn 14 years ago
parent
commit
efca0b593b

+ 17
- 2
src/java/com/healthmarketscience/jackcess/Cursor.java View File

@@ -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;
@@ -884,6 +885,20 @@ public abstract class Cursor implements Iterable<Map<String, Object>>
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.
@@ -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,

+ 18
- 0
src/java/com/healthmarketscience/jackcess/Table.java View File

@@ -1151,6 +1151,24 @@ public class Table
return row;
}
/**
* 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>

+ 46
- 18
test/src/java/com/healthmarketscience/jackcess/DatabaseTest.java View File

@@ -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();
}

Loading…
Cancel
Save