Restrict targets in remove method. Add removeMany method and test.

Update devinsy-utils library.
This commit is contained in:
Christian P. MOMON 2014-02-02 19:21:42 +01:00
parent ae511ea528
commit 9c0026da51
7 changed files with 111 additions and 41 deletions

View file

@ -21,8 +21,8 @@
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/mysql-jdbc-5.0.8.jar"/>
<classpathentry kind="lib" path="lib/devinsy-utils-0.2.6.jar" sourcepath="lib/devinsy-utils-0.2.6-sources.zip"/>
<classpathentry kind="lib" path="lib/joda-time-2.3.jar" sourcepath="lib/joda-time-2.3-sources.jar"/>
<classpathentry kind="lib" path="lib/hsqldb-2.3.0.jar"/>
<classpathentry kind="lib" path="lib/devinsy-utils-0.2.12.jar" sourcepath="lib/devinsy-utils-0.2.12-sources.zip"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View file

@ -337,6 +337,12 @@ public class FileSikevaDB implements SikevaDB {
}
@Override
public void removeMany(final String key, final String... subkeys) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void renameKey(final String oldKey, final String newKey) throws Exception {
// TODO Auto-generated method stub

View file

@ -1987,31 +1987,25 @@ public class SQLSikevaDB implements SikevaDB {
@Override
public void remove(final String key) throws SQLException {
//
Element element = getElement(key);
if (element == null) {
throw new NullPointerException("Undefined element [key=" + key + "]");
} else {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
//
element.archive();
connection = getConnection();
connection.setAutoCommit(true);
statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL");
//
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
//
connection = getConnection();
connection.setAutoCommit(true);
statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY IS NULL");
statement.setString(1, key);
statement.setString(1, element.getKey());
int rowCount = statement.executeUpdate();
statement.executeUpdate();
} finally {
close(connection, statement, resultSet);
if (rowCount == 0) {
logger.warn("Remove action without existing target [key=" + key + "]");
}
} finally {
close(connection, statement, resultSet);
}
}
@ -2021,31 +2015,25 @@ public class SQLSikevaDB implements SikevaDB {
@Override
public void remove(final String key, final String subkey) throws SQLException {
//
Element element = getElement(key, subkey);
if (element == null) {
throw new NullPointerException("Undefined element [key=" + key + "][subkey=" + subkey + "]");
} else {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
//
element.archive();
connection = getConnection();
statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY=? AND ARCHIVE_DATE IS NULL");
//
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
//
connection = getConnection();
statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY=?");
statement.setString(1, key);
statement.setString(2, subkey);
statement.setString(1, element.getKey());
statement.setString(2, element.getSubkey());
int rowCount = statement.executeUpdate();
statement.executeUpdate();
} finally {
close(connection, statement, resultSet);
if (rowCount == 0) {
logger.warn("Remove action without existing target [key=" + key + "][subkey=" + subkey + "]");
}
} finally {
close(connection, statement, resultSet);
}
}
@ -2083,6 +2071,45 @@ public class SQLSikevaDB implements SikevaDB {
}
}
/**
* {@inheritDoc}
*/
@Override
public void removeMany(final String key, final String... subkeys) throws SQLException {
//
if ((subkeys != null) && (subkeys.length > 0)) {
//
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
//
connection = getConnection();
//
String questionMarks = new StringList(subkeys.length).append('?').repeatLast(subkeys.length - 1).toStringWithCommas();
statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY IN (" + questionMarks
+ ") AND ARCHIVE_DATE IS NULL");
//
statement.setString(1, key);
//
for (int index = 0; index < subkeys.length; index++) {
statement.setString(2 + index, subkeys[index]);
}
//
int rowCount = statement.executeUpdate();
if (rowCount == 0) {
logger.warn("Remove action without existing target [key=" + key + "][subkeys=" + new StringList(subkeys).toStringWithCommas() + "]");
}
} finally {
close(connection, statement, resultSet);
}
}
}
/**
* {@inheritDoc}
*/

View file

@ -126,6 +126,8 @@ public interface SikevaDB {
public void removeAll(final String key) throws Exception;
public void removeMany(final String key, final String... subkeys) throws Exception;
public void renameKey(final String oldKey, final String newKey) throws Exception;
public void replaceInValue(final String key, final String... tokens) throws Exception;

View file

@ -529,6 +529,41 @@ public class SQLSikevaDBTest {
logger.debug("===== test done.");
}
/**
* @throws Exception
*
*/
@Test
public void testRemoveMany01() throws Exception {
//
logger.debug("===== test starting...");
database.clearDatabase();
database.put("alpha01", "alpha", "Allo");
database.put("alpha01", "bravo", "Bonjour");
database.put("alpha01", "charlie", "Courage");
database.put("alpha01", "delta", "Droiture");
database.put("alpha01", "echo", "Europe");
database.put("alpha01", "fox", "Force");
database.put("alpha02", "alpha", "Allo");
database.put("alpha02", "bravo", "Bonjour");
database.put("alpha02", "charlie", "Courage");
database.put("alpha02", "delta", "Droiture");
database.put("alpha02", "echo", "Europe");
database.put("alpha02", "fox", "Force");
Assert.assertEquals(12, database.countOfElements());
database.removeMany("alpha01", "bravo", "delta", "fox");
Assert.assertEquals(9, database.countOfElements());
//
logger.debug("===== test done.");
}
/**
* @throws Exception
*