Skip to content
Merged
Changes from 1 commit
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
35 changes: 29 additions & 6 deletions java/client/src/main/java/io/vitess/client/cursor/FieldMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,36 @@
*/
public class FieldMap {
private final List<Field> fields;
private final Map<String, Integer> indexMap;
private final Map<String, Integer> labelMap;
private final Map<String, Integer> nameMap;
private final Map<String, Integer> fullNameMap;

public FieldMap(Iterable<Field> fields) {
this.fields = ImmutableList.copyOf(checkNotNull(fields));

indexMap = new CaseInsensitiveMap<String, Integer>();
labelMap = new CaseInsensitiveMap<String, Integer>();
nameMap = new CaseInsensitiveMap<String, Integer>();
fullNameMap = new CaseInsensitiveMap<String, Integer>();
// columnIndex is 1-based.
int columnIndex = 1;
for (Field field : this.fields) {
String columnLabel = field.getName();
// If multiple columns have the same name,
// prefer the earlier one as JDBC ResultSet does.
if (!indexMap.containsKey(columnLabel)) {
indexMap.put(columnLabel, columnIndex);
String columnLabel = field.getName();
if (!labelMap.containsKey(columnLabel)) {
labelMap.put(columnLabel, columnIndex);
}
String origName = field.getOrgName();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the Field may not have these extra values. This is the case when IncludedFields != ALL. So I would check whether the value is null/empty before doing the extra map work and such. Same goes for both new maps.

if (!nameMap.containsKey(origName)) {
nameMap.put(origName, columnIndex);
}
StringBuilder fullNameBuf = new StringBuilder();
fullNameBuf.append(field.getTableName());
fullNameBuf.append('.');
fullNameBuf.append(field.getName());
String fullName = fullNameBuf.toString();
if (!fullNameMap.containsKey(fullName)) {
fullNameMap.put(fullName, columnIndex);
}
++columnIndex;
}
Expand Down Expand Up @@ -81,6 +97,13 @@ public Field get(int columnIndex) {
*/
@Nullable
public Integer getIndex(String columnLabel) {
return indexMap.get(columnLabel);
Integer index = labelMap.get(columnLabel);
if (index == null) {
index = nameMap.get(columnLabel);
}
if (index == null) {
index = fullNameMap.get(columnLabel);
}
return index;
}
}