20
20
import io .r2dbc .postgresql .message .Format ;
21
21
import io .r2dbc .postgresql .util .Assert ;
22
22
import org .reactivestreams .Publisher ;
23
+ import reactor .core .publisher .Flux ;
23
24
import reactor .core .publisher .Mono ;
24
25
25
26
import java .util .ArrayList ;
@@ -39,6 +40,8 @@ public final class Binding {
39
40
40
41
private final List <Parameter > parameters ;
41
42
43
+ private final int [] types ;
44
+
42
45
/**
43
46
* Create a new instance.
44
47
*
@@ -47,6 +50,7 @@ public final class Binding {
47
50
public Binding (int expectedSize ) {
48
51
this .expectedSize = expectedSize ;
49
52
this .parameters = new ArrayList <>(Collections .nCopies (expectedSize , UNSPECIFIED ));
53
+ this .types = new int [expectedSize ];
50
54
}
51
55
52
56
/**
@@ -57,15 +61,15 @@ public Binding(int expectedSize) {
57
61
* @return this {@link Binding}
58
62
* @throws IllegalArgumentException if {@code index} or {@code parameter} is {@code null}
59
63
*/
60
- public Binding add (Integer index , Parameter parameter ) {
61
- Assert .requireNonNull (index , "index must not be null" );
64
+ public Binding add (int index , Parameter parameter ) {
62
65
Assert .requireNonNull (parameter , "parameter must not be null" );
63
66
64
67
if (index >= this .expectedSize ) {
65
68
throw new IndexOutOfBoundsException (String .format ("Binding index %d when only %d parameters are expected" , index , this .expectedSize ));
66
69
}
67
70
68
71
this .parameters .set (index , parameter );
72
+ this .types [index ] = parameter .getType ();
69
73
70
74
return this ;
71
75
}
@@ -97,8 +101,15 @@ public List<Format> getParameterFormats() {
97
101
*
98
102
* @return the types of the parameters in the binding
99
103
*/
100
- public List <Integer > getParameterTypes () {
101
- return getTransformedParameters (Parameter ::getType );
104
+ public int [] getParameterTypes () {
105
+
106
+ for (int i = 0 ; i < this .parameters .size (); i ++) {
107
+ Parameter parameter = this .parameters .get (i );
108
+ if (parameter == UNSPECIFIED ) {
109
+ throw new IllegalStateException (String .format ("No parameter specified for index %d" , i ));
110
+ }
111
+ }
112
+ return this .types ;
102
113
}
103
114
104
115
/**
@@ -110,6 +121,10 @@ public List<Publisher<? extends ByteBuf>> getParameterValues() {
110
121
return getTransformedParameters (Parameter ::getValue );
111
122
}
112
123
124
+ Flux <Publisher <? extends ByteBuf >> parameterValues () {
125
+ return Flux .fromIterable (this .parameters ).map (Parameter ::getValue );
126
+ }
127
+
113
128
@ Override
114
129
public int hashCode () {
115
130
return Objects .hash (this .parameters );
@@ -119,6 +134,10 @@ public boolean isEmpty() {
119
134
return this .parameters .isEmpty ();
120
135
}
121
136
137
+ public int size () {
138
+ return this .parameters .size ();
139
+ }
140
+
122
141
@ Override
123
142
public String toString () {
124
143
return "Binding{" +
@@ -140,14 +159,28 @@ public void validate() {
140
159
}
141
160
142
161
private <T > List <T > getTransformedParameters (Function <Parameter , T > transformer ) {
143
- List <T > transformed = new ArrayList <>(this .parameters .size ());
162
+
163
+ if (this .parameters .isEmpty ()) {
164
+ return Collections .emptyList ();
165
+ }
166
+
167
+ List <T > transformed = null ;
144
168
145
169
for (int i = 0 ; i < this .parameters .size (); i ++) {
146
170
Parameter parameter = this .parameters .get (i );
147
171
if (parameter == UNSPECIFIED ) {
148
172
throw new IllegalStateException (String .format ("No parameter specified for index %d" , i ));
149
173
}
150
174
175
+ if (transformed == null ) {
176
+ if (this .parameters .size () == 1 ) {
177
+ return Collections .singletonList (transformer .apply (parameter ));
178
+ }
179
+
180
+ transformed = new ArrayList <>(this .parameters .size ());
181
+ }
182
+
183
+
151
184
transformed .add (transformer .apply (parameter ));
152
185
}
153
186
0 commit comments