<commit_before>/**
 * Copyright (C) 2011 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */

package com.opengamma.integration.copier.sheet.reader;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import com.opengamma.OpenGammaRuntimeException;
import com.opengamma.util.ArgumentChecker;

/**
 * A class to facilitate importing portfolio data from a JDBC query result
 */
public class JdbcSheetReader extends SheetReader {

  private DataSource _dataSource;
  private JdbcTemplate _jdbcTemplate;
  private ResultSet _resultSet;
  private String[] _row;
  private List<Map<String, String>> _results;
  private Iterator<Map<String, String>> _iterator;
  private static final Logger s_logger = LoggerFactory.getLogger(JdbcSheetReader.class);
  
  public JdbcSheetReader(DataSource dataSource, String query) {
    init(dataSource, query, null);
  }
  
  public JdbcSheetReader(DataSource dataSource, String query, PreparedStatementSetter preparedStatementSetter) {
    ArgumentChecker.notNull(preparedStatementSetter, "prepared statement setter");
    init(dataSource, query, preparedStatementSetter);
  }
  
  protected void init(DataSource dataSource, String query, PreparedStatementSetter preparedStatementSetter) {
    ArgumentChecker.notNull(dataSource, "dataSource");
    
    _jdbcTemplate = new JdbcTemplate(dataSource);
     
    ResultSetExtractor<List<Map<String, String>>> extractor = new ResultSetExtractor<List<Map<String, String>>>() {
      @Override
      public List<Map<String, String>> extractData(ResultSet rs) throws SQLException, DataAccessException {
        String[] columns = new String[rs.getMetaData().getColumnCount()];
        for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
          columns[i] = rs.getMetaData().getColumnName(i + 1);
        }
        setColumns(columns);
        List<Map<String, String>> entries = new ArrayList<Map<String, String>>();
        while (rs.next()) {
          s_logger.info("Read a row");
          String[] rawRow = new String[rs.getMetaData().getColumnCount()];
          for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
            rawRow[i] = rs.getString(i + 1);
          }
          Map<String, String> result = new HashMap<String, String>();
          // Map read-in row onto expected columns
          for (int i = 0; i < getColumns().length; i++) {
            if (i >= rawRow.length) {
              break;
            }
            if (rawRow[i] != null && rawRow[i].trim().length() > 0) {
              result.put(getColumns()[i], rawRow[i]);
            }
          }
          entries.add(result);
        }
        return entries;
      }
    };
    if (preparedStatementSetter != null) {
      _results = getJDBCTemplate().query(query, preparedStatementSetter, extractor);
    } else {
      _results = getJDBCTemplate().query(query, extractor);
    }
    _iterator = _results.iterator();
  }
  
  private JdbcTemplate getJDBCTemplate() {
    return _jdbcTemplate;
  }

  @Override
  public Map<String, String> loadNextRow() {
    if (_iterator.hasNext()) {
      s_logger.info("Returned a row");
      return _iterator.next();
    } else {
      return null;
    }
  }

  @Override<commit_msg>Allow prepared statement setter argument to be null in JDBC sheet reader constructor<commit_after>/**
 * Copyright (C) 2011 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */

package com.opengamma.integration.copier.sheet.reader;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import com.opengamma.OpenGammaRuntimeException;
import com.opengamma.util.ArgumentChecker;

/**
 * A class to facilitate importing portfolio data from a JDBC query result
 */
public class JdbcSheetReader extends SheetReader {

  private DataSource _dataSource;
  private JdbcTemplate _jdbcTemplate;
  private ResultSet _resultSet;
  private String[] _row;
  private List<Map<String, String>> _results;
  private Iterator<Map<String, String>> _iterator;
  private static final Logger s_logger = LoggerFactory.getLogger(JdbcSheetReader.class);

  public JdbcSheetReader(DataSource dataSource, String query) {
    init(dataSource, query, null);
  }

  public JdbcSheetReader(DataSource dataSource, String query, PreparedStatementSetter preparedStatementSetter) {
    init(dataSource, query, preparedStatementSetter);
  }

  protected void init(DataSource dataSource, String query, PreparedStatementSetter preparedStatementSetter) {
    ArgumentChecker.notNull(dataSource, "dataSource");
    ArgumentChecker.notEmpty(query, "query");

    _jdbcTemplate = new JdbcTemplate(dataSource);

    ResultSetExtractor<List<Map<String, String>>> extractor = new ResultSetExtractor<List<Map<String, String>>>() {
      @Override
      public List<Map<String, String>> extractData(ResultSet rs) throws SQLException, DataAccessException {
        String[] columns = new String[rs.getMetaData().getColumnCount()];
        for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
          columns[i] = rs.getMetaData().getColumnName(i + 1);
        }
        setColumns(columns);
        List<Map<String, String>> entries = new ArrayList<Map<String, String>>();
        while (rs.next()) {
          s_logger.info("Read a row");
          String[] rawRow = new String[rs.getMetaData().getColumnCount()];
          for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
            rawRow[i] = rs.getString(i + 1);
          }
          Map<String, String> result = new HashMap<String, String>();
          // Map read-in row onto expected columns
          for (int i = 0; i < getColumns().length; i++) {
            if (i >= rawRow.length) {
              break;
            }
            if (rawRow[i] != null && rawRow[i].trim().length() > 0) {
              result.put(getColumns()[i], rawRow[i]);
            }
          }
          entries.add(result);
        }
        return entries;
      }
    };
    if (preparedStatementSetter != null) {
      _results = getJDBCTemplate().query(query, preparedStatementSetter, extractor);
    } else {
      _results = getJDBCTemplate().query(query, extractor);
    }
    _iterator = _results.iterator();
  }

  private JdbcTemplate getJDBCTemplate() {
    return _jdbcTemplate;
  }

  @Override
  public Map<String, String> loadNextRow() {
    if (_iterator.hasNext()) {
      s_logger.info("Returned a row");
      return _iterator.next();
    } else {
      return null;
    }
  }

  @Override
{'fraction_non_alphanumeric': 0.11257941550190598, 'fraction_numerical': 0.0030495552731893264, 'mean_word_length': 3.557614360162131, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 45, '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': '7492608', 'n_tokens_mistral': 2283, 'n_tokens_neox': 2229, 'n_words': 535}