diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-09-14 13:39:33 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-09-14 13:39:33 -0700 |
commit | b93a23827a2244ec730be1b46ec44fb368d00396 (patch) | |
tree | 6fe68c4c927512b5f3914c5c8e795ce998208491 /lib | |
parent | de5f2916aa47fb1274d56a2c5c5ba636f5fe2cc4 (diff) | |
download | rails-b93a23827a2244ec730be1b46ec44fb368d00396.tar.gz rails-b93a23827a2244ec730be1b46ec44fb368d00396.tar.bz2 rails-b93a23827a2244ec730be1b46ec44fb368d00396.zip |
adding an EXISTS node, update method will generate an IN clause
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/crud.rb | 17 | ||||
-rw-r--r-- | lib/arel/nodes.rb | 1 | ||||
-rw-r--r-- | lib/arel/nodes/exists.rb | 11 | ||||
-rw-r--r-- | lib/arel/nodes/select_statement.rb | 2 | ||||
-rw-r--r-- | lib/arel/table.rb | 5 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 4 |
6 files changed, 36 insertions, 4 deletions
diff --git a/lib/arel/crud.rb b/lib/arel/crud.rb index b1269bd1da..196dc56554 100644 --- a/lib/arel/crud.rb +++ b/lib/arel/crud.rb @@ -7,12 +7,23 @@ module Arel um = UpdateManager.new @engine if Nodes::SqlLiteral === values - um.table @ctx.froms.last + relation = @ctx.froms.last else - um.table values.first.first.relation + relation = values.first.first.relation end + um.table relation um.set values - um.wheres = @ctx.wheres + + if @head.orders.empty? && @head.limit.nil? + um.wheres = @ctx.wheres + else + head = @head.clone + core = head.cores.first + core.projections = [relation.primary_key] + + um.wheres = [Nodes::In.new(relation.primary_key, [head])] + end + @engine.connection.update um.to_sql, 'AREL' end diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb index 3ffc81d7df..99a34c3b2b 100644 --- a/lib/arel/nodes.rb +++ b/lib/arel/nodes.rb @@ -14,6 +14,7 @@ require 'arel/nodes/count' require 'arel/nodes/values' require 'arel/nodes/offset' require 'arel/nodes/sum' +require 'arel/nodes/exists' require 'arel/nodes/max' require 'arel/nodes/min' require 'arel/nodes/avg' diff --git a/lib/arel/nodes/exists.rb b/lib/arel/nodes/exists.rb new file mode 100644 index 0000000000..167a345006 --- /dev/null +++ b/lib/arel/nodes/exists.rb @@ -0,0 +1,11 @@ +module Arel + module Nodes + class Exists + attr_reader :select_stmt + + def initialize select_stmt + @select_stmt = select_stmt + end + end + end +end diff --git a/lib/arel/nodes/select_statement.rb b/lib/arel/nodes/select_statement.rb index 6272fd126d..637ba5d1d0 100644 --- a/lib/arel/nodes/select_statement.rb +++ b/lib/arel/nodes/select_statement.rb @@ -14,7 +14,7 @@ module Arel def initialize_copy other super - @cores = @cores.clone + @cores = @cores.map { |x| x.clone } end end end diff --git a/lib/arel/table.rb b/lib/arel/table.rb index 1519efa63b..06bbe7b99e 100644 --- a/lib/arel/table.rb +++ b/lib/arel/table.rb @@ -14,6 +14,7 @@ module Arel @columns = nil @aliases = [] @table_alias = nil + @primary_key = nil # Sometime AR sends an :as parameter to table, to let the table know that # it is an Alias. We may want to override new, and return a TableAlias @@ -21,6 +22,10 @@ module Arel @table_alias = engine[:as] if Hash === engine end + def primary_key + @primary_key ||= self[@engine.connection.primary_key(name)] + end + def alias Nodes::TableAlias.new("#{name}_2", self).tap do |node| @aliases << node diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 2660c7d284..dc65e86219 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -41,6 +41,10 @@ module Arel ].compact.join ' ' end + def visit_Arel_Nodes_Exists o + "EXISTS (#{visit o.select_stmt})" + end + def visit_Arel_Nodes_Values o "VALUES (#{o.expressions.map { |value| value.nil? ? 'NULL' : visit(value) |