aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/relations/update.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/arel/relations/update.rb')
-rw-r--r--lib/arel/relations/update.rb30
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/arel/relations/update.rb b/lib/arel/relations/update.rb
new file mode 100644
index 0000000000..f1f6776f15
--- /dev/null
+++ b/lib/arel/relations/update.rb
@@ -0,0 +1,30 @@
+module Arel
+ class Update < Writing
+ attr_reader :assignments
+
+ def initialize(relation, assignments)
+ @relation, @assignments = relation, assignments.bind(relation)
+ end
+
+ def to_sql(formatter = nil)
+ [
+ "UPDATE #{table_sql} SET",
+ assignments.collect do |attribute, value|
+ "#{value.format(attribute)} = #{attribute.format(value)}"
+ end.join(",\n"),
+ ("WHERE #{selects.collect(&:to_sql).join('\n\tAND ')}" unless selects.blank? ),
+ ("LIMIT #{taken}" unless taken.blank? )
+ ].join("\n")
+ end
+
+ def call(connection = engine.connection)
+ connection.update(to_sql)
+ end
+
+ def ==(other)
+ self.class == other.class and
+ relation == other.relation and
+ assignments == other.assignments
+ end
+ end
+end \ No newline at end of file