# frozen_string_literal: true
require 'forwardable'

module ActiveRecord
  module PgGenerateSeries
    # SQL builder using PostgreSQL GENERATE_SERIES function.
    class SqlBuilder
      extend Forwardable
      def_delegators :@ar_class, :connection, :sanitize, :quoted_table_name

      def initialize(ar_class, first, last, step, seq_name)
        @ar_class = ar_class
        @first = first
        @last = last
        @step = step
        @seq_name = seq_name
        @select_items = {}

        ar_class.column_names.each do |col|
          define_singleton_method "#{col}=" do |val|
            @select_items[col] = val
          end
        end
      end

      def to_sql
        <<EOS
INSERT INTO
  #{quoted_table_name} (#{@select_items.keys.map { |col| connection.quote_column_name(col) }.join(',')})
SELECT
  #{@select_items.values.map { |val| val.is_a?(Raw) ? val.str : sanitize(val) }.join(",\n  ")}
FROM
  GENERATE_SERIES(#{@first.to_i}, #{@last.to_i}, #{@step.to_i}) AS #{connection.quote_column_name(@seq_name)}
;
EOS
      end

      private

      # no doc
      class Raw
        attr_reader :str

        def initialize(str)
          @str = str
        end
      end

      def raw(str)
        Raw.new(str)
      end
    end
  end
end

# frozen_string_literal: true
require 'forwardable'

module ActiveRecord
  module PgGenerateSeries
    # SQL builder using PostgreSQL GENERATE_SERIES function.
    class SqlBuilder
      extend Forwardable
      def_delegators :@ar_class, :connection, :quoted_table_name

      def initialize(ar_class, first, last, step, seq_name)
        @ar_class = ar_class
        @first = first
        @last = last
        @step = step
        @seq_name = seq_name
        @select_items = {}

        ar_class.column_names.each do |col|
          define_singleton_method "#{col}=" do |val|
            @select_items[col] = val
          end
        end
      end

      def to_sql
        <<EOS
INSERT INTO
  #{quoted_table_name} (#{@select_items.keys.map { |col| connection.quote_column_name(col) }.join(',')})
SELECT
  #{@select_items.values.map { |val| val.is_a?(Raw) ? val.str : connection.quote(val) }.join(",\n  ")}
FROM
  GENERATE_SERIES(#{@first.to_i}, #{@last.to_i}, #{@step.to_i}) AS #{connection.quote_column_name(@seq_name)}
;
EOS
      end

      private

      # no doc
      class Raw
        attr_reader :str

        def initialize(str)
          @str = str
        end
      end

      def raw(str)
        Raw.new(str)
      end
    end
  end
end

Fix undefined method error in rails 5.1.0.rc1
Fix undefined method error in rails 5.1.0.rc1

* ActiveRecord::Base.sanitize is removed by following PR.
    https://github.com/rails/rails/pull/26000
  So use ActiveRecord::Base.connection#quote instead of
  sanitize method.
