module Sequel::Postgres::AutoParameterizeInArray

def _bound_variable_type_for_array(r)

Returns nil if a bound variable should not be used for the array.
The bound variable type string to use for the bound variable array.
def _bound_variable_type_for_array(r)
  return unless Array === r && r.size >= pg_auto_parameterize_min_array_size
  classes = r.map(&:class)
  classes.uniq!
  classes.delete(NilClass)
  return unless classes.size == 1
  klass = classes[0]
  if klass == Integer
    # This branch is not taken on Ruby <2.4, because of the Fixnum/Bignum split.
    # However, that causes no problems as pg_auto_parameterize handles integer
    # arrays natively (though the SQL used is different)
    "int8"
  elsif klass == String
    _bound_variable_type_for_string_array(r)
  elsif klass == BigDecimal
    "numeric"
  elsif klass == Date
    "date"
  elsif klass == Time
    @db.cast_type_literal(Time)
  elsif klass == Float
    # PostgreSQL treats literal floats as numeric, not double precision
    # But older versions of PostgreSQL don't handle Infinity/NaN in numeric
    r.all?{|v| v.nil? || v.finite?} ? "numeric" : "double precision"
  elsif klass == Sequel::SQLTime
    "time"
  elsif klass == DateTime
    @db.cast_type_literal(DateTime)
  elsif klass == Sequel::SQL::Blob
    "bytea"
  end
end

def _bound_variable_type_for_string_array(r)

Do not auto parameterize string arrays by default.
def _bound_variable_type_for_string_array(r)
  nil
end

def _convert_array_to_pg_array_with_type(r, type)

Convert RHS of IN/NOT IN operator to PGArray with given type.
def _convert_array_to_pg_array_with_type(r, type)
  Sequel.pg_array(r, type)
end

def complex_expression_sql_append(sql, op, args)

but this reduces the number of bound variables.
This is the same optimization PostgreSQL performs internally,
the type is handled by the extension.
if all values inside the predicate are of the same type and
using an array bound variable for the ANY/ALL argument,
and column NOT IN (...) expressions into column != ALL($)
Transform column IN (...) expressions into column = ANY($)
def complex_expression_sql_append(sql, op, args)
  case op
  when :IN, :"NOT IN"
    l, r = args
    if auto_param?(sql) && (type = _bound_variable_type_for_array(r))
      if op == :IN 
        op = :"="
        func = :ANY
      else
        op = :!=
        func = :ALL
      end
      args = [l, Sequel.function(func, _convert_array_to_pg_array_with_type(r, type))]
    end
  end
  super
end

def pg_auto_parameterize_min_array_size

The minimium size of array to auto parameterize.
def pg_auto_parameterize_min_array_size
  2
end