package org.mariadb.jdbc.internal.common.queryresults;

import org.mariadb.jdbc.internal.common.ColumnInformation;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ColumnNameMap {
    Map<String, Integer> map;
    Map<String, Integer> labelMap;
    List<ColumnInformation> columnInfo;

    public ColumnNameMap(List<ColumnInformation> columnInfo) {
       this.columnInfo = columnInfo;
    }

    public int getIndex(String name) throws SQLException {

        if (columnInfo == null) {
           throw new SQLException("No such column :" + name);
        }
        if (map == null) {
            map = new HashMap<String, Integer>();
            int i=0;
            for(ColumnInformation ci : columnInfo) {
                String columnName = ci.getOriginalName().toLowerCase();
                if (columnName.equals("")) {
                    // for name-less columns (there CAN be some), use their alias
                    columnName = ci.getName().toLowerCase();
                }
                map.put(columnName, i);
                String tableName = ci.getTable().toLowerCase();
                if (!tableName.equals("")) {
                    map.put(tableName + "." + columnName, i);
                }
                i++;
            }
        }
        Integer res = map.get(name.toLowerCase());
        if (res == null) {
            // The specs in JDBC 4.0 specify that ResultSet.findColumn and
            // ResultSet.getXXX(String name) should use original column name rather than column alias (AS in the query)
            // However, in the past there was no consensus about it and different drivers implemented it differently
            // and for Connector/J the result might even differ from version to version, and is controlled by a special
            //  connection parameter.

            // To cope with this situation, we implement a fallback mechanism - we try the original column name as in
            // JDBC 4.0, if not found we try the label. The point is that user program continues to run rather than die
            // on exception.
            res = getLabelIndex(name);
        }
        if (res == null) {
            throw new SQLException("No such column :" + name);
        }
        return res;
    }

    private int getLabelIndex(String name) throws SQLException {
        if (labelMap == null) {
            labelMap = new HashMap<String, Integer>();
            int i=0;
            for(ColumnInformation ci : columnInfo) {
                String columnAlias = ci.getName().toLowerCase();
                labelMap.put(columnAlias, i);
                String tableName = ci.getTable().toLowerCase();
                if (!tableName.equals("")) {
                    labelMap.put(tableName + "." + columnAlias, i);
                }
                i++;
            }

        }
        Integer res = labelMap.get(name.toLowerCase());
        if (res == null) {
            throw new SQLException("No such column :" + name);
        }
        return res;
    }
}

@@ -533,18 +533,16 @@
 tion %7B%0D%0A
-%0D%0A
         
@@ -634,32 +634,355 @@
 e);%0D%0A        %7D%0D%0A
+        // The specs in JDBC 4.0 specify that ResultSet.findColumn and%0D%0A        // ResultSet.getXXX(String name) should use column alias (AS in the query). If label is not found, we use %0D%0A        // original table name.%0D%0A    %09Integer res = getLabelIndex(name);%0D%0A%0D%0A    %09%0D%0A    %09if (res != null) %7B%0D%0A    %09%09return res;%0D%0A    %09%7D%0D%0A
         if (map 
@@ -1689,32 +1689,24 @@
  %7D%0D%0A        
-Integer 
 res = map.ge
@@ -1740,825 +1740,8 @@
     
- if (res == null) %7B%0D%0A            // The specs in JDBC 4.0 specify that ResultSet.findColumn and%0D%0A            // ResultSet.getXXX(String name) should use original column name rather than column alias (AS in the query)%0D%0A            // However, in the past there was no consensus about it and different drivers implemented it differently%0D%0A            // and for Connector/J the result might even differ from version to version, and is controlled by a special%0D%0A            //  connection parameter.%0D%0A%0D%0A            // To cope with this situation, we implement a fallback mechanism - we try the original column name as in%0D%0A            // JDBC 4.0, if not found we try the label. The point is that user program continues to run rather than die%0D%0A            // on exception.%0D%0A            res = getLabelIndex(name);%0D%0A        %7D
 %0D%0A  

Fix findColumn() - the string parameter is meant to be column label, and only if label is missing column name It was implemented other way around