Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add column name index mapping in PostgresqlRow #640

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/main/java/io/r2dbc/postgresql/PostgresqlRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import io.r2dbc.postgresql.message.backend.RowDescription;
import io.r2dbc.postgresql.util.Assert;
import io.r2dbc.spi.Row;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import reactor.core.publisher.Mono;
import reactor.util.annotation.Nullable;

Expand All @@ -47,6 +50,8 @@ final class PostgresqlRow implements io.r2dbc.postgresql.api.PostgresqlRow {

private volatile boolean isReleased = false;

private Map<String, Integer> columnNameIndexCacheMap;

PostgresqlRow(ConnectionResources context, io.r2dbc.postgresql.api.PostgresqlRowMetadata metadata, List<RowDescription.Field> fields, ByteBuf[] data) {
this.context = Assert.requireNonNull(context, "context must not be null");
this.metadata = Assert.requireNonNull(metadata, "metadata must not be null");
Expand Down Expand Up @@ -138,6 +143,15 @@ public String toString() {
'}';
}

static Map<String, Integer> createColumnNameIndexMap(List<RowDescription.Field> fields) {
Map<String, Integer> columnNameIndexMap = new HashMap<>(fields.size() * 2);
for (int i = fields.size() - 1; i >= 0; i--) {
columnNameIndexMap.put(fields.get(i).getName().toLowerCase(Locale.US), i);
}

return columnNameIndexMap;
}

static PostgresqlRow toRow(ConnectionResources context, DataRow dataRow, Codecs codecs, RowDescription rowDescription) {
Assert.requireNonNull(dataRow, "dataRow must not be null");
Assert.requireNonNull(codecs, "rowDescription must not be null");
Expand Down Expand Up @@ -165,12 +179,19 @@ void release() {
}

private int getColumn(String name) {
for (int i = 0; i < this.fields.size(); i++) {
RowDescription.Field field = this.fields.get(i);
if (this.columnNameIndexCacheMap == null) {
this.columnNameIndexCacheMap = createColumnNameIndexMap(this.fields);
}

if (field.getName().equalsIgnoreCase(name)) {
return i;
}
Integer index = this.columnNameIndexCacheMap.get(name);
if (index != null) {
return index;
}

index = this.columnNameIndexCacheMap.get(name.toLowerCase(Locale.US));
if (index != null) {
this.columnNameIndexCacheMap.put(name, index);
return index;
}

throw new NoSuchElementException(String.format("Column name '%s' does not exist in column names %s", name, toColumnNames()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ void getName() {
.build();

assertThat(new PostgresqlRow(MockContext.builder().codecs(codecs).build(), new PostgresqlRowMetadata(Collections.emptyList()), this.columns, this.data).get("test-name-2", Object.class)).isSameAs(value);
assertThat(new PostgresqlRow(MockContext.builder().codecs(codecs).build(), new PostgresqlRowMetadata(Collections.emptyList()), this.columns, this.data).get("tEsT-nAme-2", Object.class)).isSameAs(value);
}

@Test
Expand Down