<commit_before>module ActiveRecord  # :nodoc:
  module ConnectionAdapters  # :nodoc:
    module PostGISAdapter  # :nodoc:
      class MainAdapter < PostgreSQLAdapter  # :nodoc:
        def initialize(*args_)
          # Overridden to change the visitor
          super
          @visitor = ::Arel::Visitors::PostGIS.new(self)
        end

        include PostGISAdapter::CommonAdapterMethods

        @@native_database_types = nil

        def native_database_types
          # Overridden to add the :spatial type
          @@native_database_types ||= super.merge(
            :spatial => {:name => 'geometry'},
            :geography => {:name => 'geography'})
        end

        def type_cast(value_, column_, array_member_=false)
          if ::RGeo::Feature::Geometry.check_type(value_)
            ::RGeo::WKRep::WKBGenerator.new(:hex_format => true, :type_format => :ewkb, :emit_ewkb_srid => true).generate(value_)
          else
            super
          end
        end

        def columns(table_name_, name_=nil)
          # FULL REPLACEMENT. RE-CHECK ON NEW VERSIONS.
          # We needed to return a spatial column subclass.
          table_name_ = table_name_.to_s
          spatial_info_ = spatial_column_info(table_name_)
          column_definitions(table_name_).collect do |col_name_, type_, default_, notnull_, oid_, fmod_|
            oid_ = type_map.fetch(oid_.to_i, fmod_.to_i) {
              OID::Identity.new
            }
            SpatialColumn.new(@rgeo_factory_settings, table_name_, col_name_, default_, oid_, type_,
              notnull_ == 'f', type_ =~ /geometry/i ? spatial_info_[col_name_] : nil)
          end
        end

        def indexes(table_name_, name_=nil)
          # FULL REPLACEMENT. RE-CHECK ON NEW VERSIONS.
          result_ = query(<<-SQL, 'SCHEMA')
            SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid
            FROM pg_class t
            INNER JOIN pg_index d ON t.oid = d.indrelid
            INNER JOIN pg_class i ON d.indexrelid = i.oid
            WHERE i.relkind = 'i'
              AND d.indisprimary = 'f'
              AND t.relname = '#{table_name_}'
              AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) )
            ORDER BY i.relname
          SQL

          result_.map do |row_|
            index_name_ = row_[0]
            unique_ = row_[1] == 't'
            indkey_ = row_[2].split(" ")
            inddef_ = row_[3]
            oid_ = row_[4]

            columns_ = query(<<-SQL, "SCHEMA")
              SELECT a.attnum, a.attname, t.typname
                FROM pg_attribute a, pg_type t
              WHERE a.attrelid = #{oid_}
                AND a.attnum IN (#{indkey_.join(",")})
                AND a.atttypid = t.oid
            SQL
            columns_ = columns_.inject({}){ |h_, r_| h_[r_[0].to_s] = [r_[1], r_[2]]; h_ }
            column_names_ = columns_.values_at(*indkey_).compact.map{ |a_| a_[0] }

            # add info on sort order for columns (only desc order is explicitly specified, asc is the default)
            desc_order_columns_ = inddef_.scan(/(\w+) DESC/).flatten
            orders_ = desc_order_columns_.any? ? Hash[desc_order_columns_.map {|order_column_| [order_column_, :desc]}] : {}
            where_ = inddef_.scan(/WHERE (.+)$/).flatten[0]
            spatial_ = inddef_ =~ /using\s+gist/i && columns_.size == 1 &&
              (columns_.values.first[1] == 'geometry' || columns_.values.first[1] == 'geography')

            if column_names_.empty?
              nil
            else
              ::RGeo::ActiveRecord::SpatialIndexDefinition.new(table_name_, index_name_, unique_, column_names_, [], orders_, where_, spatial_ ? true : false)
            end
          end.compact
        end

<<<<<<< HEAD

=======
>>>>>>> rgeo/master
        def create_table_definition(name_, temporary_, options_, as_=nil)
          # Override to create a spatial table definition (post-4.0.0.beta1)
          PostGISAdapter::TableDefinition.new(native_database_types, name_, temporary_, options_, as_, self)
        end

        def create_table(table_name_, options_={}, &block_)<commit_msg>fix merge<commit_after>module ActiveRecord  # :nodoc:
  module ConnectionAdapters  # :nodoc:
    module PostGISAdapter  # :nodoc:
      class MainAdapter < PostgreSQLAdapter  # :nodoc:
        def initialize(*args_)
          # Overridden to change the visitor
          super
          @visitor = ::Arel::Visitors::PostGIS.new(self)
        end

        include PostGISAdapter::CommonAdapterMethods

        @@native_database_types = nil

        def native_database_types
          # Overridden to add the :spatial type
          @@native_database_types ||= super.merge(
            :spatial => {:name => 'geometry'},
            :geography => {:name => 'geography'})
        end

        def type_cast(value_, column_, array_member_=false)
          if ::RGeo::Feature::Geometry.check_type(value_)
            ::RGeo::WKRep::WKBGenerator.new(:hex_format => true, :type_format => :ewkb, :emit_ewkb_srid => true).generate(value_)
          else
            super
          end
        end

        def columns(table_name_, name_=nil)
          # FULL REPLACEMENT. RE-CHECK ON NEW VERSIONS.
          # We needed to return a spatial column subclass.
          table_name_ = table_name_.to_s
          spatial_info_ = spatial_column_info(table_name_)
          column_definitions(table_name_).collect do |col_name_, type_, default_, notnull_, oid_, fmod_|
            oid_ = type_map.fetch(oid_.to_i, fmod_.to_i) {
              OID::Identity.new
            }
            SpatialColumn.new(@rgeo_factory_settings, table_name_, col_name_, default_, oid_, type_,
              notnull_ == 'f', type_ =~ /geometry/i ? spatial_info_[col_name_] : nil)
          end
        end

        def indexes(table_name_, name_=nil)
          # FULL REPLACEMENT. RE-CHECK ON NEW VERSIONS.
          result_ = query(<<-SQL, 'SCHEMA')
            SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid
            FROM pg_class t
            INNER JOIN pg_index d ON t.oid = d.indrelid
            INNER JOIN pg_class i ON d.indexrelid = i.oid
            WHERE i.relkind = 'i'
              AND d.indisprimary = 'f'
              AND t.relname = '#{table_name_}'
              AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) )
            ORDER BY i.relname
          SQL

          result_.map do |row_|
            index_name_ = row_[0]
            unique_ = row_[1] == 't'
            indkey_ = row_[2].split(" ")
            inddef_ = row_[3]
            oid_ = row_[4]

            columns_ = query(<<-SQL, "SCHEMA")
              SELECT a.attnum, a.attname, t.typname
                FROM pg_attribute a, pg_type t
              WHERE a.attrelid = #{oid_}
                AND a.attnum IN (#{indkey_.join(",")})
                AND a.atttypid = t.oid
            SQL
            columns_ = columns_.inject({}){ |h_, r_| h_[r_[0].to_s] = [r_[1], r_[2]]; h_ }
            column_names_ = columns_.values_at(*indkey_).compact.map{ |a_| a_[0] }

            # add info on sort order for columns (only desc order is explicitly specified, asc is the default)
            desc_order_columns_ = inddef_.scan(/(\w+) DESC/).flatten
            orders_ = desc_order_columns_.any? ? Hash[desc_order_columns_.map {|order_column_| [order_column_, :desc]}] : {}
            where_ = inddef_.scan(/WHERE (.+)$/).flatten[0]
            spatial_ = inddef_ =~ /using\s+gist/i && columns_.size == 1 &&
              (columns_.values.first[1] == 'geometry' || columns_.values.first[1] == 'geography')

            if column_names_.empty?
              nil
            else
              ::RGeo::ActiveRecord::SpatialIndexDefinition.new(table_name_, index_name_, unique_, column_names_, [], orders_, where_, spatial_ ? true : false)
            end
          end.compact
        end

        def create_table_definition(name_, temporary_, options_, as_=nil)
          # Override to create a spatial table definition (post-4.0.0.beta1)
          PostGISAdapter::TableDefinition.new(native_database_types, name_, temporary_, options_, as_, self)
        end

        def create_table(table_name_, options_={}, &block_)
{'fraction_non_alphanumeric': 0.11195989496299832, 'fraction_numerical': 0.00405824779183576, 'mean_word_length': 2.0907414238288453, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 24, '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': '3837843', 'n_tokens_mistral': 2610, 'n_tokens_neox': 2518, 'n_words': 592}