Class: ActiveRecord::ConnectionAdapters::AbstractAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/model_set/sql_base_query.rb

Instance Method Summary collapse

Instance Method Details

#ids_clause(ids, field, id_type) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/model_set/sql_base_query.rb', line 44

def ids_clause(ids, field, id_type)
  return "FALSE" if ids.empty?

  if id_type == :integer
    # Make sure all ids are integers to prevent SQL injection attacks.
    ids = ids.collect {|id| id.to_i}

    if kind_of?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
      "#{field} = ANY ('{#{ids.join(',')}}'::bigint[])"
    else
      "#{field} IN (#{ids.join(',')})"
    end
  elsif id_type == :string
    ids = ids.collect do |id|
      raise ArgumentError.new('Invalid id: #{id}') if id =~ /'/
      "'#{id}'"
    end
    "#{field} IN (#{ids.join(',')})"
  end
end