View Javadoc
1   package org.davidmoten.rxjava3.jdbc;
2   
3   import java.sql.Connection;
4   import java.sql.ResultSet;
5   
6   import javax.annotation.Nonnull;
7   
8   import org.davidmoten.rxjava3.jdbc.internal.Functions;
9   
10  import com.github.davidmoten.guavamini.Preconditions;
11  
12  import io.reactivex.rxjava3.core.Completable;
13  import io.reactivex.rxjava3.core.Flowable;
14  import io.reactivex.rxjava3.core.Single;
15  
16  public final class UpdateBuilder extends ParametersBuilder<UpdateBuilder> implements DependsOn<UpdateBuilder> {
17  
18      static final int DEFAULT_BATCH_SIZE = 1;
19  
20      final String sql;
21      final Single<Connection> connections;
22      private final Database db;
23      Flowable<?> dependsOn;
24      int batchSize = DEFAULT_BATCH_SIZE;
25      int queryTimeoutSec = Util.QUERY_TIMEOUT_NOT_SET;
26  
27      UpdateBuilder(String sql, Single<Connection> connections, Database db) {
28          super(sql);
29          this.sql = sql;
30          this.connections = connections;
31          this.db = db;
32      }
33  
34      @Override
35      public UpdateBuilder dependsOn(Flowable<?> flowable) {
36          Preconditions.checkArgument(dependsOn == null, "dependsOn can only be set once");
37          dependsOn = flowable;
38          return this;
39      }
40  
41      public UpdateBuilder batchSize(int batchSize) {
42          this.batchSize = batchSize;
43          return this;
44      }
45  
46      public UpdateBuilder queryTimeoutSec(int queryTimeoutSec) {
47          Preconditions.checkArgument(queryTimeoutSec >= 0);
48          this.queryTimeoutSec = queryTimeoutSec;
49          return this;
50      }
51  
52      /**
53       * Returns a builder used to specify how to process the generated keys
54       * {@link ResultSet}. Not all jdbc drivers support this functionality and
55       * some have limitations in their support (h2 for instance only returns the
56       * last generated key when multiple inserts happen in the one statement).
57       * 
58       * @return a builder used to specify how to process the generated keys
59       *         ResultSet
60       */
61      public ReturnGeneratedKeysBuilder returnGeneratedKeys() {
62          Preconditions.checkArgument(batchSize == 1, "Cannot return generated keys if batchSize > 1");
63          return new ReturnGeneratedKeysBuilder(this);
64      }
65  
66      public Flowable<Integer> counts() {
67          return startWithDependency(
68                  Update.create(connections, super.parameterGroupsToFlowable(), sql, batchSize, true, queryTimeoutSec).dematerialize(Functions.identity()));
69      }
70  
71      <T> Flowable<T> startWithDependency(@Nonnull Flowable<T> f) {
72          if (dependsOn != null) {
73              return dependsOn.ignoreElements().andThen(f);
74          } else {
75              return f;
76          }
77      }
78  
79      public TransactedUpdateBuilder transacted() {
80          return new TransactedUpdateBuilder(this, db);
81      }
82  
83      public Flowable<Tx<?>> transaction() {
84          return transacted().tx();
85      }
86  
87      public Completable complete() {
88          return counts().ignoreElements();
89      }
90  
91  }