3131import io .r2dbc .postgresql .util .GeneratedValuesUtils ;
3232import io .r2dbc .postgresql .util .Operators ;
3333import io .r2dbc .postgresql .util .sql .BasicPostgresqlSqlLexer ;
34- import io .r2dbc .postgresql .util .sql .TokenType ;
3534import io .r2dbc .postgresql .util .sql .TokenizedSql ;
3635import io .r2dbc .spi .Statement ;
3736import reactor .core .publisher .Flux ;
5251import static io .r2dbc .postgresql .util .PredicateUtils .or ;
5352
5453/**
55- * {@link Statement} using the <a href="https://www.postgresql.org/docs/current/static/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY">Extended Query Flow</a> .
54+ * {@link Statement}.
5655 */
5756final class PostgresqlStatement implements io .r2dbc .postgresql .api .PostgresqlStatement {
5857
@@ -85,6 +84,10 @@ final class PostgresqlStatement implements io.r2dbc.postgresql.api.PostgresqlSta
8584
8685 @ Override
8786 public PostgresqlStatement add () {
87+ Binding binding = bindings .peekLast ();
88+ if (binding != null ) {
89+ binding .validate ();
90+ }
8891 this .bindings .add (new Binding (tokenizedSql .getParameterCount ()));
8992 return this ;
9093 }
@@ -99,7 +102,7 @@ public PostgresqlStatement bind(int index, Object value) {
99102 Assert .requireNonNull (value , "value must not be null" );
100103
101104 BindingLogger .logBind (this .connectionContext , index , value );
102- getCurrentOrNewBinding ().add (index , this .resources .getCodecs ().encode (value ));
105+ getCurrentOrFirstBinding ().add (index , this .resources .getCodecs ().encode (value ));
103106 return this ;
104107 }
105108
@@ -112,17 +115,17 @@ public PostgresqlStatement bindNull(String identifier, Class<?> type) {
112115 public PostgresqlStatement bindNull (int index , Class <?> type ) {
113116 Assert .requireNonNull (type , "type must not be null" );
114117
115- if (index >= tokenizedSql .getParameterCount ()){
118+ if (index >= tokenizedSql .getParameterCount ()) {
116119 throw new UnsupportedOperationException (String .format ("Cannot bind parameter %d, statement has %d parameters" , index , this .tokenizedSql .getParameterCount ()));
117120 }
118121
119122 BindingLogger .logBindNull (this .connectionContext , index , type );
120- getCurrentOrNewBinding ().add (index , this .resources .getCodecs ().encodeNull (type ));
123+ getCurrentOrFirstBinding ().add (index , this .resources .getCodecs ().encodeNull (type ));
121124 return this ;
122125 }
123126
124127 @ Nonnull
125- private Binding getCurrentOrNewBinding () {
128+ private Binding getCurrentOrFirstBinding () {
126129 Binding binding = bindings .peekLast ();
127130 if (binding == null ) {
128131 Binding newBinding = new Binding (tokenizedSql .getParameterCount ());
@@ -145,20 +148,12 @@ public Flux<io.r2dbc.postgresql.api.PostgresqlResult> execute() {
145148 public PostgresqlStatement returnGeneratedValues (String ... columns ) {
146149 Assert .requireNonNull (columns , "columns must not be null" );
147150
148- boolean hasReturning = this .tokenizedSql .getStatements ().stream ()
149- .flatMap (s -> s .getTokens ().stream ())
150- .anyMatch (token -> (token .getType () == TokenType .DEFAULT )
151- && (token .getValue ().equalsIgnoreCase ("RETURNING" )));
151+ boolean hasReturning = this .tokenizedSql .hasDefaultTokenValue ("RETURNING" );
152152 if (hasReturning ) {
153153 throw new IllegalStateException ("Statement already includes RETURNING clause" );
154154 }
155- boolean isSupporting = this .tokenizedSql .getStatements ().stream ()
156- .flatMap (s -> s .getTokens ().stream ())
157- .anyMatch (e -> e .getType () == TokenType .DEFAULT
158- && (e .getValue ().equalsIgnoreCase ("DELETE" )
159- || e .getValue ().equalsIgnoreCase ("INSERT" )
160- || e .getValue ().equalsIgnoreCase ("UPDATE" )));
161155
156+ boolean isSupporting = this .tokenizedSql .hasDefaultTokenValue ("DELETE" , "INSERT" , "UPDATE" );
162157 if (!isSupporting ) {
163158 throw new IllegalStateException ("Statement is not a DELETE, INSERT, or UPDATE command" );
164159 }
@@ -185,13 +180,13 @@ public String toString() {
185180 }
186181
187182 Binding getCurrentBinding () {
188- return getCurrentOrNewBinding ();
183+ return getCurrentOrFirstBinding ();
189184 }
190185
191186 private int getIdentifierIndex (String identifier ) {
192187 Assert .requireNonNull (identifier , "identifier must not be null" );
193188 Assert .requireType (identifier , String .class , "identifier must be a String" );
194- if (!identifier .startsWith ("$" )){
189+ if (!identifier .startsWith ("$" )) {
195190 throw new NoSuchElementException (String .format ("\" %s\" is not a valid identifier" , identifier ));
196191 }
197192 try {
0 commit comments