aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/relations/insertion.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/arel/relations/insertion.rb')
-rw-r--r--lib/arel/relations/insertion.rb28
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/arel/relations/insertion.rb b/lib/arel/relations/insertion.rb
new file mode 100644
index 0000000000..37e7be8757
--- /dev/null
+++ b/lib/arel/relations/insertion.rb
@@ -0,0 +1,28 @@
+module Arel
+ class Insertion < Writing
+ attr_reader :record
+
+ def initialize(relation, record)
+ @relation, @record = relation, record.bind(relation)
+ end
+
+ def to_sql(formatter = nil)
+ [
+ "INSERT",
+ "INTO #{table_sql}",
+ "(#{record.keys.collect(&:to_sql).join(', ')})",
+ "VALUES (#{record.collect { |key, value| key.format(value) }.join(', ')})"
+ ].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 \ No newline at end of file