Skip to content

Statement.add() is Awkward #259

@Michael-A-McMahon

Description

@Michael-A-McMahon

I'm finding it a bit awkward to use add() correctly. It requires coding in a branch to have the first set of binds are handled differently, like this:

  /**
   * Binds multiple arrays of {@code values} to a {@code statement} by calling
   * {@link Statement#add()}
   */
  static Publisher<Void> bindMany(
    Statement statement, Publisher<Object[]> values) {

    AtomicBoolean isFirst = new AtomicBoolean(true);
    return Flux.from(values)
      .doOnNext(nextValues -> {
        // Don't call add() if this is the first set of bind values
        if (! isFirst.getAndSet(false))
          statement.add();

        for (int i = 0; i < nextValues.length; i++)
          statement.bind(i, nextValues[i]);
      })
      .then();
  }

I think the SPI would be easier to use and less error prone if it allowed the trailing add(). As a JDBC programmer, I also find the trailing add() to be more intuitive, because this is how JDBC's PreparedStatement.addBatch() works.

With a trailing add(), the code above could be written as:

  /**
   * Binds multiple arrays of {@code values} to a {@code statement} by calling
   * {@link Statement#add()}
   */
  static Publisher<Void> bindMany(
    Statement statement, Publisher<Object[]> values) {
    return Flux.from(values)
      .doOnNext(nextValues -> {
        for (int i = 0; i < nextValues.length; i++)
          statement.bind(i, nextValues[i]);

        statement.add();
      })
      .then();
  }

Am I the only one who thinks this? I'd like to see what others think now, before the 1.0 release solidifies the behavior of add().

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: team-attentionAn issue we need to discuss as a team to make progresstarget: R2DBC.nextA future major revision of R2DBC

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions