<commit_before>module ClosureTree
  module NumericOrderSupport

    def self.adapter_for_connection(connection)
      case connection.adapter_name.downcase.to_sym
        when :postgresql
          ::ClosureTree::NumericOrderSupport::PostgreSQLAdapter
        when :mysql, :mysql2
          ::ClosureTree::NumericOrderSupport::MysqlAdapter
        else
          ::ClosureTree::NumericOrderSupport::GenericAdapter
      end
    end

    module MysqlAdapter
      def reorder_with_parent_id(parent_id, minimum_sort_order_value = nil, delta = 0)
        min_where = if minimum_sort_order_value
          "AND #{quoted_order_column} >= #{minimum_sort_order_value}"
        else
          ""
        end
        connection.execute "SET @i = 0"
        connection.execute <<-SQL
          UPDATE #{quoted_table_name}
            SET #{quoted_order_column} = (@i := @i + 1) + #{minimum_sort_order_value.to_i + delta - 1}
          WHERE #{quoted_parent_column_name} = #{quoted_value(parent_id)} #{min_where}
          ORDER BY #{order_by}
        SQL
      end
    end

    module PostgreSQLAdapter
      def reorder_with_parent_id(parent_id, minimum_sort_order_value = nil, delta = 0)
        min_where = if minimum_sort_order_value
          "AND #{quoted_order_column} >= #{minimum_sort_order_value}"
        else
          ""
        end
        connection.execute <<-SQL
          UPDATE #{quoted_table_name}
          SET #{quoted_order_column(false)} = t.seq + #{minimum_sort_order_value.to_i + delta - 1}
          FROM (
            SELECT #{quoted_id_column_name} AS id, row_number() OVER(ORDER BY #{order_by}) AS seq
            FROM #{quoted_table_name}
            WHERE #{quoted_parent_column_name} = #{quoted_value(parent_id)} #{min_where}) AS t
          WHERE #{quoted_table_name}.#{quoted_id_column_name} = t.id
        SQL
      end
    end

    module GenericAdapter
      def reorder_with_parent_id(parent_id, minimum_sort_order_value = nil, delta = 0)
        scope = model_class.where(parent_column_sym => parent_id)
        if minimum_sort_order_value
          scope = scope.where("#{quoted_order_column} >= #{minimum_sort_order_value}")
        end
        scope.each_with_index do |ea, idx|
          ea.update_attribute(order_column_sym, idx + minimum_sort_order_value.to_i + delta)
        end
      end
    end
  end
end
<commit_msg>Postgis Adapter support.<commit_after>module ClosureTree
  module NumericOrderSupport

    def self.adapter_for_connection(connection)
      case connection.adapter_name.downcase.to_sym
        when :postgresql, :postgis #Postgis is based on Postgresql
          ::ClosureTree::NumericOrderSupport::PostgreSQLAdapter
        when :mysql, :mysql2
          ::ClosureTree::NumericOrderSupport::MysqlAdapter
        else
          ::ClosureTree::NumericOrderSupport::GenericAdapter
      end
    end

    module MysqlAdapter
      def reorder_with_parent_id(parent_id, minimum_sort_order_value = nil, delta = 0)
        min_where = if minimum_sort_order_value
          "AND #{quoted_order_column} >= #{minimum_sort_order_value}"
        else
          ""
        end
        connection.execute "SET @i = 0"
        connection.execute <<-SQL
          UPDATE #{quoted_table_name}
            SET #{quoted_order_column} = (@i := @i + 1) + #{minimum_sort_order_value.to_i + delta - 1}
          WHERE #{quoted_parent_column_name} = #{quoted_value(parent_id)} #{min_where}
          ORDER BY #{order_by}
        SQL
      end
    end

    module PostgreSQLAdapter
      def reorder_with_parent_id(parent_id, minimum_sort_order_value = nil, delta = 0)
        min_where = if minimum_sort_order_value
          "AND #{quoted_order_column} >= #{minimum_sort_order_value}"
        else
          ""
        end
        connection.execute <<-SQL
          UPDATE #{quoted_table_name}
          SET #{quoted_order_column(false)} = t.seq + #{minimum_sort_order_value.to_i + delta - 1}
          FROM (
            SELECT #{quoted_id_column_name} AS id, row_number() OVER(ORDER BY #{order_by}) AS seq
            FROM #{quoted_table_name}
            WHERE #{quoted_parent_column_name} = #{quoted_value(parent_id)} #{min_where}) AS t
          WHERE #{quoted_table_name}.#{quoted_id_column_name} = t.id
        SQL
      end
    end

    module GenericAdapter
      def reorder_with_parent_id(parent_id, minimum_sort_order_value = nil, delta = 0)
        scope = model_class.where(parent_column_sym => parent_id)
        if minimum_sort_order_value
          scope = scope.where("#{quoted_order_column} >= #{minimum_sort_order_value}")
        end
        scope.each_with_index do |ea, idx|
          ea.update_attribute(order_column_sym, idx + minimum_sort_order_value.to_i + delta)
        end
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.08852527795259073, 'fraction_numerical': 0.003356408642752255, 'mean_word_length': 2.6733436055469952, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '1776018', 'n_tokens_mistral': 1511, 'n_tokens_neox': 1456, 'n_words': 292}