Skip to content

Commit

Permalink
feat: create upsert method to template
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <[email protected]>
  • Loading branch information
otaviojava committed Nov 28, 2023
1 parent 9bf32b1 commit 1c542f2
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions core/src/main/java/expert/os/harperdb/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ public <T> boolean update(Iterable<T> entities) {
return server.execute(insert);
}

public <T> boolean upsert(T entity) {
Objects.requireNonNull(entity, "entity is required");
var insert = new Upsert(database, table(entity), Collections.singletonList(entity));
return server.execute(insert);
}

public <T> boolean upsert(Iterable<T> entities) {
Objects.requireNonNull(entities, "entities is required");
List<T> beans = StreamSupport.stream(entities.spliterator(), false)
.toList();
if(beans.isEmpty()){
return false;
}
String name = table(beans.get(0));
var insert = new Upsert(database, name, beans);
return server.execute(insert);
}

private <T> String table(T entity) {
return entity.getClass().getSimpleName().toLowerCase(Locale.US);
}
Expand Down

0 comments on commit 1c542f2

Please sign in to comment.