diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2012-08-26 19:45:17 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2012-08-26 19:45:17 +0000 |
commit | 3e547e1d59bd61e8d1ad9bab945af5d0eafba061 (patch) | |
tree | b2e062d7c1e87129fcfd901fbe33e64f460b7524 | |
parent | 64f98b1d1067fbaa4d7cf2a825e682f845c600a7 (diff) | |
download | jackcess-3e547e1d59bd61e8d1ad9bab945af5d0eafba061.tar.gz jackcess-3e547e1d59bd61e8d1ad9bab945af5d0eafba061.zip |
Add some more functionality to Joiner to facilitate integrity enforcement (hasRows and deleteRows)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@640 f203690c-595d-4dc9-a70b-905162fa7fd2
-rw-r--r-- | src/changes/changes.xml | 6 | ||||
-rw-r--r-- | src/java/com/healthmarketscience/jackcess/Joiner.java | 34 | ||||
-rw-r--r-- | test/src/java/com/healthmarketscience/jackcess/JoinerTest.java | 28 |
3 files changed, 62 insertions, 6 deletions
diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 1572375..b27fa1e 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -4,6 +4,12 @@ <author email="javajedi@users.sf.net">Tim McCune</author> </properties> <body> + <release version="1.2.9" date="TBD"> + <action dev="jahlborn" type="update"> + Add some more functionality to Joiner to facilitate integrity + enforcement (hasRows and deleteRows). + </action> + </release> <release version="1.2.8" date="2012-07-10"> <action dev="jahlborn" type="update" issue="3523179"> Add osgi header information to the manifest. diff --git a/src/java/com/healthmarketscience/jackcess/Joiner.java b/src/java/com/healthmarketscience/jackcess/Joiner.java index 3b51854..8e5bf7c 100644 --- a/src/java/com/healthmarketscience/jackcess/Joiner.java +++ b/src/java/com/healthmarketscience/jackcess/Joiner.java @@ -21,6 +21,7 @@ package com.healthmarketscience.jackcess; import java.io.IOException; import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -115,6 +116,15 @@ public class Joiner } /** + * Returns {@code true} if the "to" table has any rows based on the given + * columns in the "from" table, {@code false} otherwise. + */ + public boolean hasRows(Map<String,?> fromRow) throws IOException { + toEntryValues(fromRow); + return _toCursor.findFirstRowByEntry(_entryValues); + } + + /** * Returns the first row in the "to" table based on the given columns in the * "from" table if any, {@code null} if there is no matching row. * @@ -140,9 +150,7 @@ public class Joiner Collection<String> columnNames) throws IOException { - toEntryValues(fromRow); - return ((_toCursor.findFirstRowByEntry(_entryValues) ? - _toCursor.getCurrentRow(columnNames) : null)); + return (hasRows(fromRow) ? _toCursor.getCurrentRow(columnNames) : null); } /** @@ -207,6 +215,26 @@ public class Joiner } /** + * Deletes any rows in the "to" table based on the given columns in the + * "from" table. + * + * @param fromRow row from the "from" table (which must include the relevant + * columns for this join relationship) + * @return {@code true} if any "to" rows were deleted, {@code false} + * otherwise + */ + public boolean deleteRows(Map<String,?> fromRow) throws IOException { + boolean removed = false; + for(Iterator<Map<String,Object>> iter = findRows( + fromRow, Collections.<String>emptySet()); iter.hasNext(); ) { + iter.next(); + iter.remove(); + removed = true; + } + return removed; + } + + /** * Fills in the _entryValues with the relevant info from the given "from" * table row. */ diff --git a/test/src/java/com/healthmarketscience/jackcess/JoinerTest.java b/test/src/java/com/healthmarketscience/jackcess/JoinerTest.java index 87f70fd..d2049c3 100644 --- a/test/src/java/com/healthmarketscience/jackcess/JoinerTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/JoinerTest.java @@ -45,7 +45,7 @@ public class JoinerTest extends TestCase { { for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) { - Database db = open(testDB); + Database db = openCopy(testDB); Table t1 = db.getTable("Table1"); Table t2 = db.getTable("Table2"); Table t3 = db.getTable("Table3"); @@ -74,11 +74,13 @@ public class JoinerTest extends TestCase { assertSame(t1t3, t3t1Join.getToIndex()); doTestJoiner(t3t1Join, createT3T1Data()); + + doTestJoinerDelete(t2t1Join); } } - private void doTestJoiner(Joiner join, - Map<Integer,List<Map<String,Object>>> expectedData) + private static void doTestJoiner( + Joiner join, Map<Integer,List<Map<String,Object>>> expectedData) throws Exception { final Set<String> colNames = new HashSet<String>( @@ -98,10 +100,12 @@ public class JoinerTest extends TestCase { assertEquals(expectedData.get(id), joinedRows); if(!expectedRows.isEmpty()) { + assertTrue(join.hasRows(row)); assertEquals(expectedRows.get(0), join.findFirstRow(row)); assertEquals(row, revJoin.findFirstRow(expectedRows.get(0))); } else { + assertFalse(join.hasRows(row)); assertNull(join.findFirstRow(row)); } @@ -128,6 +132,24 @@ public class JoinerTest extends TestCase { } } + private static void doTestJoinerDelete(Joiner t2t1Join) throws Exception + { + assertEquals(4, countRows(t2t1Join.getToTable())); + + Map<String,Object> row = createExpectedRow("id", 1); + assertTrue(t2t1Join.hasRows(row)); + + assertTrue(t2t1Join.deleteRows(row)); + + assertFalse(t2t1Join.hasRows(row)); + assertFalse(t2t1Join.deleteRows(row)); + + assertEquals(2, countRows(t2t1Join.getToTable())); + for(Map<String,Object> t1Row : t2t1Join.getToTable()) { + assertFalse(t1Row.get("otherfk1").equals(1)); + } + } + private static Map<Integer,List<Map<String,Object>>> createT2T1Data() { Map<Integer,List<Map<String,Object>>> data = new |