3

"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 Answers1

2

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

Abhinaba Chakraborty
  • 3,488
  • 2
  • 16
  • 37