<commit_before>package databaseconnector.impl;

import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.mendix.systemwideinterfaces.core.meta.IMetaPrimitive.PrimitiveType;

/**
 * ResultSetReader converts a given instance of {@link ResultSet} into a list of instances of Map<String, Object>, with key for column name
 * and value for column value.
 */
public class ResultSetReader {
  private final ResultSetIterator rsIter;
  private final Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
  
  public ResultSetReader(final ResultSet resultSet, final List<PrimitiveType> primitiveTypes) {
    this.rsIter = new ResultSetIterator(resultSet, primitiveTypes);
  }

  /**
   * Read all records into list of maps.
   *
   * @return list of records, where records are represented as map
   * @throws SQLException
   */
  public List<Map<String, Object>> readAll() throws SQLException {
    // Force the stream to read the whole ResultSet, so that the connection can be closed.
    return rsIter.stream().map(rs -> getRowResult(rs)).collect(Collectors.toList());
  }

  private Map<String, Object> getRowResult(final ResultSet rs) {
    return rsIter.getColumnInfos().collect(Collectors.toMap(ColumnInfo::getName, curryGetColumnResult(rs)));
  }

  private Function<ColumnInfo, Object> curryGetColumnResult(final ResultSet rs) {
    return ci -> getColumnResult(rs, ci);
  }

  private Object getColumnResult(final ResultSet rs, final ColumnInfo columnInfo) {
    try {
      final int index = columnInfo.getIndex();
	  Object columnValue = null;
	  switch (columnInfo.getType()) {
	    case Integer:
	      columnValue = rs.getInt(index);
		  break;
	    case AutoNumber:
	    case Long:
		  columnValue = rs.getLong(index);
		  break;
	    case DateTime:
          Timestamp timeStamp = rs.getTimestamp(index, calendar);
          columnValue = (timeStamp != null) ? new Date(timeStamp.getTime()) : null;
		  break;
	    case Boolean:
		  columnValue = rs.getBoolean(index);
		  break;
	    case Decimal:
		  columnValue = rs.getBigDecimal(index);
		  break;
	    case Float:
	    case Currency:
		  columnValue = rs.getDouble(index);
		  break;
	    case HashString:
	    case Enum:
	    case String:
		  columnValue = rs.getString(index);
		  break;
	    case Binary:
	       try (InputStream inputStream = rs.getBinaryStream(index)) {
        	columnValue = inputStream;
          } catch (IOException e) {
        	throw new RuntimeException(e);
          }
		  break;
		default: throw new RuntimeException("Meta object primitive type '" + columnInfo.getType().name() + "' is unknown.");
      }
      return columnValue;
    } catch (SQLException e) {
      throw new RuntimeException(e);
    }
  }
}<commit_msg>Handle null case in result set values. Fix indentation of getColumnResult method.<commit_after>package databaseconnector.impl;

import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.mendix.systemwideinterfaces.core.meta.IMetaPrimitive.PrimitiveType;

/**
 * ResultSetReader converts a given instance of {@link ResultSet} into a list of instances of Map<String, Object>, with key for column name
 * and value for column value.
 */
public class ResultSetReader {
  private final ResultSetIterator rsIter;
  private final Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
  
  public ResultSetReader(final ResultSet resultSet, final List<PrimitiveType> primitiveTypes) {
    this.rsIter = new ResultSetIterator(resultSet, primitiveTypes);
  }

  /**
   * Read all records into list of maps.
   *
   * @return list of records, where records are represented as map
   * @throws SQLException
   */
  public List<Map<String, Object>> readAll() throws SQLException {
    // Force the stream to read the whole ResultSet, so that the connection can be closed.
    return rsIter.stream().map(rs -> getRowResult(rs)).collect(Collectors.toList());
  }

  private Map<String, Object> getRowResult(final ResultSet rs) {
    return rsIter.getColumnInfos().collect(Collectors.toMap(ColumnInfo::getName, curryGetColumnResult(rs)));
  }

  private Function<ColumnInfo, Object> curryGetColumnResult(final ResultSet rs) {
    return ci -> getColumnResult(rs, ci);
  }

  private Object getColumnResult(final ResultSet rs, final ColumnInfo columnInfo) {
    try {
      int index = columnInfo.getIndex();
  	  Object columnValue = null;
  	  switch (columnInfo.getType()) {
  	    case Integer:
  	      columnValue = rs.getInt(index);
  	      break;
  	    case AutoNumber:
  	    case Long:
    		  columnValue = rs.getLong(index);
    		  break;
  	    case DateTime:
          Timestamp timeStamp = rs.getTimestamp(index, calendar);
          columnValue = (timeStamp != null) ? new Date(timeStamp.getTime()) : null;
    		  break;
  	    case Boolean:
    		  columnValue = rs.getBoolean(index);
    		  break;
  	    case Decimal:
    		  columnValue = rs.getBigDecimal(index);
    		  break;
  	    case Float:
  	    case Currency:
    		  columnValue = rs.getDouble(index);
    		  break;
  	    case HashString:
  	    case Enum:
  	    case String:
    		  columnValue = rs.getString(index);
    		  break;
  	    case Binary:
          try (InputStream inputStream = rs.getBinaryStream(index)) {
            columnValue = inputStream;
          } catch (IOException e) {
            throw new RuntimeException(e);
          }
  	      break;
  	    default: throw new RuntimeException("Meta object primitive type '" + columnInfo.getType().name() + "' is unknown.");
      }
      return rs.wasNull() ? null : columnValue;
    } catch (SQLException e) {
      throw new RuntimeException(e);
    }
  }
}
{'fraction_non_alphanumeric': 0.09841065981698507, 'fraction_numerical': 0.0, 'mean_word_length': 3.2995169082125604, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3517669', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1779, 'n_words': 494}