<commit_before>require "preserves/selection"
require "preserves/mapping"
require "preserves/mapper"


module Preserves
  class Repository

    attr_accessor :model_class

    def initialize(options={})
      self.model_class = options[:model]
    end

    def fetch(primary_key_value)
      select(fetch_query, primary_key_value).only
    end

    def fetch!(primary_key_value)
      select(fetch_query, primary_key_value).only!
    end

    alias_method :[], :fetch

    def query(sql_string, *params)
      pg_result = data_store.exec_params(sql_string, params)
      SQL::ResultSet.new(pg_result)
    end

    def select(sql_string, *params)
      if params && params.last.is_a?(Hash)
        relations = params.pop
        Selection.new(map(query(sql_string, *params), relations))
      else
        Selection.new(map(query(sql_string, *params)))
      end
      Selection.new(map(query(sql_string, *params), relations))
    end

    def map(result, relations={})
      mapper.map(result, relations)
    end

  protected

    def mapping(&block)
      @mapping ||= Mapping.new(self, model_class, &block)
    end

  private

    def mapper
      @mapper ||= Mapper.new(@mapping)
    end

    # NOTE: We'll allow overriding this default on a per-repository basis later.
    def data_store
      Preserves.data_store
    end

    def fetch_query<commit_msg>Refactor Repository#select to remove duplicate code<commit_after>require "preserves/selection"
require "preserves/mapping"
require "preserves/mapper"


module Preserves
  class Repository

    attr_accessor :model_class

    def initialize(options={})
      self.model_class = options[:model]
    end

    def fetch(primary_key_value)
      select(fetch_query, primary_key_value).only
    end

    def fetch!(primary_key_value)
      select(fetch_query, primary_key_value).only!
    end

    alias_method :[], :fetch

    def query(sql_string, *params)
      pg_result = data_store.exec_params(sql_string, params)
      SQL::ResultSet.new(pg_result)
    end

    def select(sql_string, *params)
      if params && params.last.is_a?(Hash)
        relations = params.pop
      else
        relations = {}
      end
      Selection.new(map(query(sql_string, *params), relations))
    end

    def map(result, relations={})
      mapper.map(result, relations)
    end

  protected

    def mapping(&block)
      @mapping ||= Mapping.new(self, model_class, &block)
    end

  private

    def mapper
      @mapper ||= Mapper.new(@mapping)
    end

    # NOTE: We'll allow overriding this default on a per-repository basis later.
    def data_store
      Preserves.data_store
    end

    def fetch_query
{'fraction_non_alphanumeric': 0.09600903614457831, 'fraction_numerical': 0.0, 'mean_word_length': 3.0379939209726445, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3683055', 'n_tokens_mistral': 841, 'n_tokens_neox': 808, 'n_words': 186}