<commit_before>require "active_model/conversion"

module Pancakes
  class Table
    include ActiveModel::Conversion

    HSTORE_DATA_TYPE = 'USER-DEFINED'

    attr_reader :database, :connection, :name

    def initialize(database,name)
      @database = database
      @connection = database.connection
      @name = name
    end

    def records
      @records ||= self.connection.records(name)
      hstore_columns.any? ? parse_hstore(@records) : @records
    end

    def sorted_records(params)
      @sorted_records ||= self.connection.sorted_records(name, params)
      hstore_columns.any? ? parse_hstore(@sorted_records) : @sorted_records
    end

    def columns
      @columns ||= self.connection.columns(name)
    end

    def count
      result = self.connection.count(name)
      result.first['count'].to_i
    end

    def primary_keys
      @primary_keys ||= self.connection.primary_keys(name)
    end

    def insert(attributes)
      self.connection.insert(name, attributes)
    end

    def update(id, attributes)
      key = attributes.keys.first
      if key.in? hstore_columns
        attributes[key] = if attributes[key].present?
          PgHstore.dump(JSON.parse(attributes[key]), true)
        end
      end
      self.connection.update(name, id, attributes)<commit_msg>Parse hstore records on insert.<commit_after>require "active_model/conversion"

module Pancakes
  class Table
    include ActiveModel::Conversion

    HSTORE_DATA_TYPE = 'USER-DEFINED'

    attr_reader :database, :connection, :name

    def initialize(database,name)
      @database = database
      @connection = database.connection
      @name = name
    end

    def records
      @records ||= self.connection.records(name)
      hstore_columns.any? ? parse_hstore(@records) : @records
    end

    def sorted_records(params)
      @sorted_records ||= self.connection.sorted_records(name, params)
      hstore_columns.any? ? parse_hstore(@sorted_records) : @sorted_records
    end

    def columns
      @columns ||= self.connection.columns(name)
    end

    def count
      result = self.connection.count(name)
      result.first['count'].to_i
    end

    def primary_keys
      @primary_keys ||= self.connection.primary_keys(name)
    end

    def insert(attributes)
      attributes.keys.each do |attribute_name|
        if attribute_name.in? hstore_columns
          attributes[attribute_name] = if attributes[attribute_name].present?
            PgHstore.dump(JSON.parse(attributes[attribute_name]), true)
          end
        end
      end
      records = self.connection.insert(name, attributes)
      parse_hstore(records)
    end

    def update(id, attributes)
      key = attributes.keys.first
      if key.in? hstore_columns
        attributes[key] = if attributes[key].present?
          PgHstore.dump(JSON.parse(attributes[key]), true)
        end
      end
      self.connection.update(name, id, attributes)
{'fraction_non_alphanumeric': 0.09514031485284052, 'fraction_numerical': 0.0, 'mean_word_length': 2.8973333333333335, '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': '7380194', 'n_tokens_mistral': 871, 'n_tokens_neox': 843, 'n_words': 177}