aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_relation/relations/insertion.rb
blob: a0042a18a5fbe13fa4643f33ba410717bae9f98a (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 ActiveRelation
  module Relations
    class Insertion < Compound
      attr_reader :record
  
      def initialize(relation, record)
        @relation, @record = relation, record
      end

      def to_sql(options = {})
        [
          "INSERT",
          "INTO #{quote_table_name(table)}",
          "(#{record.keys.collect(&:to_sql).join(', ')})",
          "VALUES #{inserts.collect(&:to_sql).join(', ')}"
        ].join("\n")
      end  

      protected
      def inserts
        relation.inserts + [record]
      end
    end
  end
end