do we a shorthand to get a count of number of rows are present in db with respect to a where clause in r2bc

Viewed 1180

"Is there any short hand to no .of rows in a database against a where condition" if we have a criteria definition present against which want number of rows present in database. is is any function in r2dbc that give that?

1 Answers

You can have something like this:

@Repository
public class MyCustomRepositoryImpl {

  private final R2dbcEntityTemplate r2dbcEntityTemplate;

  @Autowired
  public MyCustomRepositoryImpl(DatabaseClient databaseClient) {
    this.r2dbcEntityTemplate = new R2dbcEntityTemplate(databaseClient);
  }

  public Mono<Long> getCountOfAllRows() {
    return r2dbcEntityTemplate.count(query(Criteria.empty()),MyEntity.class);
  }

  public Mono<Long> getCountOfRowsForACondition(String myVal) {
    return r2dbcEntityTemplate.select(query(Criteria.where("mycolumn").is(myVal)),MyEntity.class);
  }
}

Note here query is the static method of org.springframework.data.relational.core.query.Query

Related