<commit_before>require 'quick_count/version'
require 'quick_count/railtie'
require 'active_record'

module QuickCount

  def self.root
    @root ||= Pathname.new(File.dirname(File.expand_path(File.dirname(__FILE__), '/../')))
  end

  def self.load
    ::ActiveRecord::Base.send :include, QuickCount::ActiveRecord
    ::ActiveRecord::Relation.send :include, CountEstimate::ActiveRecord
  end

  def self.install(threshold: 500000, schema: 'public')
    ::ActiveRecord::Base.connection.execute(quick_count_sql(schema: schema, threshold: threshold))
    ::ActiveRecord::Base.connection.execute(count_estimate_sql(schema: schema))
  end

  def self.uninstall(schema: 'public')
    ::ActiveRecord::Base.connection.execute("DROP FUNCTION IF EXISTS #{schema}.quick_count(text, bigint);")
    ::ActiveRecord::Base.connection.execute("DROP FUNCTION IF EXISTS #{schema}.quick_count(text);")
    ::ActiveRecord::Base.connection.execute("DROP FUNCTION IF EXISTS #{schema}.count_estimate(text);")
  end

private

  def self.quick_count_sql(threshold: 500000, schema: 'public')
    <<~SQL
      CREATE OR REPLACE FUNCTION #{schema}.quick_count(table_name text, threshold bigint default #{threshold}) RETURNS bigint AS
      $func$
      DECLARE count bigint;
      BEGIN
        EXECUTE 'SELECT
          CASE<commit_msg>Allow install on different database connections<commit_after>require 'quick_count/version'
require 'quick_count/railtie'
require 'active_record'

module QuickCount

  def self.root
    @root ||= Pathname.new(File.dirname(File.expand_path(File.dirname(__FILE__), '/../')))
  end

  def self.load
    ::ActiveRecord::Base.send :include, QuickCount::ActiveRecord
    ::ActiveRecord::Relation.send :include, CountEstimate::ActiveRecord
  end

  def self.install(threshold: 500000, schema: 'public', connection: ::ActiveRecord::Base.connection)
    connection.execute(quick_count_sql(schema: schema, threshold: threshold))
    connection.execute(count_estimate_sql(schema: schema))
  end

  def self.uninstall(schema: 'public', connection: ::ActiveRecord::Base.connection)
    connection.execute("DROP FUNCTION IF EXISTS #{schema}.quick_count(text, bigint);")
    connection.execute("DROP FUNCTION IF EXISTS #{schema}.quick_count(text);")
    connection.execute("DROP FUNCTION IF EXISTS #{schema}.count_estimate(text);")
  end

private

  def self.quick_count_sql(threshold: 500000, schema: 'public')
    <<~SQL
      CREATE OR REPLACE FUNCTION #{schema}.quick_count(table_name text, threshold bigint default #{threshold}) RETURNS bigint AS
      $func$
      DECLARE count bigint;
      BEGIN
        EXECUTE 'SELECT
          CASE
{'fraction_non_alphanumeric': 0.12409867172675522, 'fraction_numerical': 0.009108159392789373, 'mean_word_length': 4.977324263038549, 'pattern_counts': {'":': 0, '<': 7, '<?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': '2439832', 'n_tokens_mistral': 822, 'n_tokens_neox': 771, 'n_words': 172}