aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/relations/writes/delete.rb
blob: 2eaad6d1da5f5ef8824f3a2f3792081979fa52fd (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
module Arel
  class Deletion < Compound
    def initialize(relation)
      @relation = relation
    end

    def to_sql(formatter = nil)
      [
        "DELETE",
        "FROM #{table_sql}",
        ("WHERE #{wheres.collect(&:to_sql).join('\n\tAND ')}" unless wheres.blank?  ),
        ("LIMIT     #{taken}"                                  unless taken.blank?    ),
      ].compact.join("\n")
    end
    
    def call(connection = engine.connection)
      connection.delete(to_sql)
    end
    
    def ==(other)
      Deletion    === other and
      relation    ==  other.relation
    end
  end
end