<commit_before>require 'facets/hash/revalue'
require 'facets/hash/symbolize_keys'

module ConceptQL
  class Database
    attr :db, :opts

    def initialize(db, opts={})
      @db = db
      db_type = :impala
      if db
        extensions.each do |extension|
          db.extension extension
        end
        db_type = db.database_type.to_sym
      end

      @opts = opts.revalue { |v| v ? v.to_sym : v }.symbolize_keys
      @opts[:data_model] ||= (ENV["CONCEPTQL_DATA_MODEL"] || :omopv4).to_sym
      @opts[:database_type] ||= db_type
      db.set(db_opts) if db.respond_to?(:set)
    end

    def query(statement, opts={})
      NullQuery.new if statement.nil? || statement.empty?
      Query.new(db, statement, @opts.merge(opts))
    end

    def db_opts
      opt_regexp = /^#{opts[:database_type]}_db_opt_/i

      env_hash = ENV.to_hash.rekey { |k| k.to_s.downcase }
      opts_hash = opts.rekey { |k| k.to_s.downcase }
      all_opts = env_hash.merge(opts_hash)

      matching_opts = all_opts.select { |k, _| k.match(opt_regexp) }

      matching_opts.each_with_object({}) do |(k,v), h|
        new_key = k.sub(opt_regexp, '')
        h[new_key] = prep_value(k, v)
      end
    end

    def prep_value(k, v)
      v =~ /\W/ ? %Q|"#{v}"| : v
    end

    def extensions
      [:date_arithmetic, :error_sql]
    end
  end
end<commit_msg>Database: Move DB_OPT processing to Sequelizer<commit_after>module ConceptQL
  class Database
    attr :db, :opts

    def initialize(db, opts={})
      @db = db
      db_type = :impala
      if db
        extensions.each do |extension|
          db.extension extension
        end
        db_type = db.database_type.to_sym
      end

      # Symbolize all keys and values
      @opts = Hash[opts.map { |k,v| [k.to_sym, v ? v.to_sym : v]}]

      @opts[:data_model] ||= (ENV["CONCEPTQL_DATA_MODEL"] || :omopv4).to_sym
      @opts[:database_type] ||= db_type
    end

    def query(statement, opts={})
      NullQuery.new if statement.nil? || statement.empty?
      Query.new(db, statement, @opts.merge(opts))
    end

    def extensions
      [:date_arithmetic, :error_sql]
    end
  end
end
{'fraction_non_alphanumeric': 0.1293787949556282, 'fraction_numerical': 0.0009341429238673517, 'mean_word_length': 2.488599348534202, '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': '630930', 'n_tokens_mistral': 781, 'n_tokens_neox': 763, 'n_words': 165}