aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_relation/relations/insertion.rb
blob: 901ff6eb70688fae8cc639c44992766786058c70 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module ActiveRelation
  class Insertion < Writing
    attr_reader :record

    def initialize(relation, record)
      @relation, @record = relation, record
    end

    def to_sql(formatter = nil)
      [
        "INSERT",
        "INTO #{table_sql}",
        "(#{record.keys.collect(&:to_sql)})",
        "VALUES (#{record.collect { |key, value| key.format(value) }})"
      ].join("\n")
    end
    
    def call(connection = engine.connection)
      connection.insert(to_sql)
    end
    
    def ==(other)
      self.class  == other.class    and
      relation    == other.relation and
      record      == other.record
    end
  end
end