2424import io .r2dbc .postgresql .message .backend .RowDescription ;
2525import io .r2dbc .postgresql .util .Assert ;
2626import io .r2dbc .spi .Row ;
27+ import java .util .HashMap ;
28+ import java .util .Locale ;
29+ import java .util .Map ;
2730import reactor .core .publisher .Mono ;
2831import reactor .util .annotation .Nullable ;
2932
@@ -47,6 +50,8 @@ final class PostgresqlRow implements io.r2dbc.postgresql.api.PostgresqlRow {
4750
4851 private volatile boolean isReleased = false ;
4952
53+ private Map <String , Integer > columnNameIndexCacheMap ;
54+
5055 PostgresqlRow (ConnectionResources context , io .r2dbc .postgresql .api .PostgresqlRowMetadata metadata , List <RowDescription .Field > fields , ByteBuf [] data ) {
5156 this .context = Assert .requireNonNull (context , "context must not be null" );
5257 this .metadata = Assert .requireNonNull (metadata , "metadata must not be null" );
@@ -138,6 +143,15 @@ public String toString() {
138143 '}' ;
139144 }
140145
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+
141155 static PostgresqlRow toRow (ConnectionResources context , DataRow dataRow , Codecs codecs , RowDescription rowDescription ) {
142156 Assert .requireNonNull (dataRow , "dataRow must not be null" );
143157 Assert .requireNonNull (codecs , "rowDescription must not be null" );
@@ -165,12 +179,19 @@ void release() {
165179 }
166180
167181 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+ }
170185
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 ;
174195 }
175196
176197 throw new NoSuchElementException (String .format ("Column name '%s' does not exist in column names %s" , name , toColumnNames ()));
0 commit comments