24
24
import io .r2dbc .postgresql .message .backend .RowDescription ;
25
25
import io .r2dbc .postgresql .util .Assert ;
26
26
import io .r2dbc .spi .Row ;
27
+ import java .util .HashMap ;
28
+ import java .util .Locale ;
29
+ import java .util .Map ;
27
30
import reactor .core .publisher .Mono ;
28
31
import reactor .util .annotation .Nullable ;
29
32
@@ -47,6 +50,8 @@ final class PostgresqlRow implements io.r2dbc.postgresql.api.PostgresqlRow {
47
50
48
51
private volatile boolean isReleased = false ;
49
52
53
+ private Map <String , Integer > columnNameIndexCacheMap ;
54
+
50
55
PostgresqlRow (ConnectionResources context , io .r2dbc .postgresql .api .PostgresqlRowMetadata metadata , List <RowDescription .Field > fields , ByteBuf [] data ) {
51
56
this .context = Assert .requireNonNull (context , "context must not be null" );
52
57
this .metadata = Assert .requireNonNull (metadata , "metadata must not be null" );
@@ -138,6 +143,15 @@ public String toString() {
138
143
'}' ;
139
144
}
140
145
146
+ static Map <String , Integer > createColumnNameIndexMap (List <RowDescription .Field > fields ) {
147
+ Map <String , Integer > columnNameIndexMap = new HashMap <>(fields .size () * 2 );
148
+ for (int i = fields .size () - 1 ; i >= 0 ; i --) {
149
+ columnNameIndexMap .put (fields .get (i ).getName ().toLowerCase (Locale .US ), i );
150
+ }
151
+
152
+ return columnNameIndexMap ;
153
+ }
154
+
141
155
static PostgresqlRow toRow (ConnectionResources context , DataRow dataRow , Codecs codecs , RowDescription rowDescription ) {
142
156
Assert .requireNonNull (dataRow , "dataRow must not be null" );
143
157
Assert .requireNonNull (codecs , "rowDescription must not be null" );
@@ -165,12 +179,19 @@ void release() {
165
179
}
166
180
167
181
private int getColumn (String name ) {
168
- for (int i = 0 ; i < this .fields .size (); i ++) {
169
- RowDescription .Field field = this .fields .get (i );
182
+ if (this .columnNameIndexCacheMap == null ) {
183
+ this .columnNameIndexCacheMap = createColumnNameIndexMap (this .fields );
184
+ }
170
185
171
- if (field .getName ().equalsIgnoreCase (name )) {
172
- return i ;
173
- }
186
+ Integer index = this .columnNameIndexCacheMap .get (name );
187
+ if (index != null ) {
188
+ return index ;
189
+ }
190
+
191
+ index = this .columnNameIndexCacheMap .get (name .toLowerCase (Locale .US ));
192
+ if (index != null ) {
193
+ this .columnNameIndexCacheMap .put (name , index );
194
+ return index ;
174
195
}
175
196
176
197
throw new NoSuchElementException (String .format ("Column name '%s' does not exist in column names %s" , name , toColumnNames ()));
0 commit comments