module Sequel::Postgres::AutoParameterizeInArray

Enable automatically parameterizing queries.

Public Instance Methods

complex_expression_sql_append(sql, op, args) click to toggle source

Transform column IN (…) expressions into column = ANY($) and column NOT IN (…) expressions into column != ALL($) using an array bound variable for the ANY/ALL argument, if all values inside the predicate are of the same type and the type is handled by the extension. This is the same optimization PostgreSQL performs internally, but this reduces the number of bound variables.

Calls superclass method
# File lib/sequel/extensions/pg_auto_parameterize_in_array.rb, line 46
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, Sequel.pg_array(r, type))]
    end
  end

  super
end

Private Instance Methods

_bound_variable_type_for_array(r) click to toggle source

The bound variable type string to use for the bound variable array. Returns nil if a bound variable should not be used for the array.

# File lib/sequel/extensions/pg_auto_parameterize_in_array.rb, line 69
def _bound_variable_type_for_array(r)
  return unless Array === r && r.size > 1
  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
    "text" if db.typecast_value(:boolean, db.opts[:treat_string_list_as_text_array])
  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