diff options
Diffstat (limited to 'lib/arel/relations/writes')
-rw-r--r-- | lib/arel/relations/writes/delete.rb | 25 | ||||
-rw-r--r-- | lib/arel/relations/writes/insert.rb | 28 | ||||
-rw-r--r-- | lib/arel/relations/writes/update.rb | 30 |
3 files changed, 83 insertions, 0 deletions
diff --git a/lib/arel/relations/writes/delete.rb b/lib/arel/relations/writes/delete.rb new file mode 100644 index 0000000000..2eaad6d1da --- /dev/null +++ b/lib/arel/relations/writes/delete.rb @@ -0,0 +1,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
\ No newline at end of file diff --git a/lib/arel/relations/writes/insert.rb b/lib/arel/relations/writes/insert.rb new file mode 100644 index 0000000000..a1c4c93de5 --- /dev/null +++ b/lib/arel/relations/writes/insert.rb @@ -0,0 +1,28 @@ +module Arel + class Insert < Compound + 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) + Insert === other and + relation == other.relation and + record == other.record + end + end +end
\ No newline at end of file diff --git a/lib/arel/relations/writes/update.rb b/lib/arel/relations/writes/update.rb new file mode 100644 index 0000000000..760f4e931f --- /dev/null +++ b/lib/arel/relations/writes/update.rb @@ -0,0 +1,30 @@ +module Arel + class Update < Compound + 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 #{wheres.collect(&:to_sql).join('\n\tAND ')}" unless wheres.blank? ), + ("LIMIT #{taken}" unless taken.blank? ) + ].join("\n") + end + + def call(connection = engine.connection) + connection.update(to_sql) + end + + def ==(other) + Update === other and + relation == other.relation and + assignments == other.assignments + end + end +end
\ No newline at end of file |