<commit_before>
module DmIsReflective::PostgresAdapter
  include DataMapper

  def storages
    sql = <<-SQL
      SELECT table_name FROM "information_schema"."tables"
      WHERE table_schema = current_schema()
    SQL

    select(Ext::String.compress_lines(sql))
  end

  private
  def reflective_query_storage storage
    sql = <<-SQL
      SELECT column_name FROM "information_schema"."key_column_usage"
      WHERE table_schema = current_schema() AND table_name = ?
    SQL

    keys = select(Ext::String.compress_lines(sql), storage)

    sql = <<-SQL
      SELECT column_name, column_default, is_nullable,
             character_maximum_length, udt_name,
             '#{keys.first}' AS key
      FROM "information_schema"."columns"
      WHERE table_schema = current_schema() AND table_name = ?
    SQL

    select(Ext::String.compress_lines(sql), storage)
  end

  def reflective_field_name field
    field.column_name
  end

  def reflective_primitive field
    field.udt_name
  end

  def reflective_attributes field, attrs = {}
    # strip data type
    field.column_default.gsub!(/(.*?)::[\w\s]*/, '\1') if
      field.column_default

    attrs[:serial] = true if field.column_default =~ /nextval\('\w+'\)/<commit_msg>postgres_adapter.rb: use subquery to reduce query number<commit_after>
module DmIsReflective::PostgresAdapter
  include DataMapper

  def storages
    sql = <<-SQL
      SELECT table_name FROM "information_schema"."tables"
      WHERE table_schema = current_schema()
    SQL

    select(Ext::String.compress_lines(sql))
  end

  private
  def reflective_query_storage storage
    sql_key = <<-SQL
      SELECT column_name FROM "information_schema"."key_column_usage"
      WHERE table_schema = current_schema() AND table_name = ?
    SQL

    sql = <<-SQL
      SELECT column_name, column_default, is_nullable,
             character_maximum_length, udt_name,
             (#{sql_key}) AS key
      FROM "information_schema"."columns"
      WHERE table_schema = current_schema() AND table_name = ?
    SQL

    select(Ext::String.compress_lines(sql), storage, storage)
  end

  def reflective_field_name field
    field.column_name
  end

  def reflective_primitive field
    field.udt_name
  end

  def reflective_attributes field, attrs = {}
    # strip data type
    field.column_default.gsub!(/(.*?)::[\w\s]*/, '\1') if
      field.column_default

    attrs[:serial] = true if field.column_default =~ /nextval\('\w+'\)/
{'fraction_non_alphanumeric': 0.10146699266503667, 'fraction_numerical': 0.0008149959250203749, 'mean_word_length': 3.2474048442906573, 'pattern_counts': {'":': 0, '<': 15, '<?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': '4662940', 'n_tokens_mistral': 769, 'n_tokens_neox': 734, 'n_words': 175}