Module: Sequel::Postgres::AutoParameterizeInArray

Defined in:
lib/sequel/extensions/pg_auto_parameterize_in_array.rb

Overview

Enable automatically parameterizing queries.

Instance Method Summary collapse

Instance Method Details

#complex_expression_sql_append(sql, op, args) ⇒ Object

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.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sequel/extensions/pg_auto_parameterize_in_array.rb', line 47

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