Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow deleting multiple records of a model at once #243

Merged
merged 1 commit into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions jack-core/src/com/rapleaf/jack/AbstractDatabaseModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -786,13 +787,18 @@ public String getTableName() {

@Override
public boolean delete(long id) throws IOException {
return delete(Collections.singleton(id));
}

@Override
public boolean delete(Collection<Long> ids) throws IOException {
PreparedStatement stmt = conn.getPreparedStatement(String.format(
"DELETE FROM %s WHERE id=%d", tableName, id));
"DELETE FROM %s WHERE id IN (%s)", tableName, ids.stream().map(Object::toString).collect(Collectors.joining(","))));
try {
boolean success = stmt.executeUpdate() == 1;
boolean success = stmt.executeUpdate() == ids.size();
stmt.close();
if (success) {
cachedById.remove(id);
ids.forEach(cachedById::remove);
}
clearForeignKeyCache();
return success;
Expand Down
5 changes: 5 additions & 0 deletions jack-core/src/com/rapleaf/jack/IModelPersistence.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ interface RecordSelector<T extends ModelWithId> {
*/
boolean delete(long id) throws IOException;

/**
* @return true if all records deleted, false otherwise
*/
boolean delete(Collection<Long> ids) throws IOException;

/**
* Delete all records in this persistence.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,14 +501,24 @@ public void testNullTreatment() throws Exception {
}

@Test
public void testDelete() throws Exception {
public void testDeleteSingle() throws Exception {
IPostPersistence posts = dbs.getDatabase1().posts();
Post post = posts.create(null, 10L, 1, 0l);
long id = post.getId();
posts.delete(id);
assertNull(posts.find(id));
}

@Test
public void testDeleteMultiple() throws Exception {
IPostPersistence posts = dbs.getDatabase1().posts();
long post1 = posts.create(null, 10L, 1, 0l).getId();
long post2 = posts.create(null, 10L, 1, 0l).getId();
posts.delete(Arrays.asList(post1, post2));
assertTrue(posts.find(Arrays.asList(post1, post2)).isEmpty());
}


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: extra newline here...

@Test
public void testSave() throws Exception {
IPostPersistence posts = dbs.getDatabase1().posts();
Expand Down