diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-09-23 15:26:08 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-09-23 15:26:08 -0700 |
commit | 80ad95bae41f4f5923faeb2e7b9cab4287e54fe9 (patch) | |
tree | 606c0335a71eb4f7a3223da3aad8a37a32cb761d /lib/arel/visitors | |
parent | 445a0ea342c28b8a6598343db40ad776cffd3a2e (diff) | |
download | rails-80ad95bae41f4f5923faeb2e7b9cab4287e54fe9.tar.gz rails-80ad95bae41f4f5923faeb2e7b9cab4287e54fe9.tar.bz2 rails-80ad95bae41f4f5923faeb2e7b9cab4287e54fe9.zip |
moving visitors around
Diffstat (limited to 'lib/arel/visitors')
-rw-r--r-- | lib/arel/visitors/mysql.rb | 16 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 18 | ||||
-rw-r--r-- | lib/arel/visitors/update_sql.rb | 26 |
3 files changed, 30 insertions, 30 deletions
diff --git a/lib/arel/visitors/mysql.rb b/lib/arel/visitors/mysql.rb new file mode 100644 index 0000000000..4551223591 --- /dev/null +++ b/lib/arel/visitors/mysql.rb @@ -0,0 +1,16 @@ +module Arel + module Visitors + class MySQL < Arel::Visitors::ToSql + def visit_Arel_Nodes_UpdateStatement o + [ + "UPDATE #{visit o.relation}", + ("SET #{o.values.map { |value| visit value }.join ', '}" unless o.values.empty?), + ("WHERE #{o.wheres.map { |x| visit x }.join ' AND '}" unless o.wheres.empty?), + ("ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?), + ("LIMIT #{o.limit}" if o.limit), + ].compact.join ' ' + end + + end + end +end diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 2ccc9d7d3f..d1cb115238 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -22,15 +22,25 @@ module Arel end def visit_Arel_Nodes_UpdateStatement o + if o.orders.empty? && o.limit.nil? + wheres = o.wheres + else + stmt = Nodes::SelectStatement.new + core = stmt.cores.first + core.froms = o.relation + core.projections = [o.relation.primary_key] + stmt.limit = o.limit + stmt.orders = o.orders + + wheres = [Nodes::In.new(o.relation.primary_key, [stmt])] + end + [ "UPDATE #{visit o.relation}", ("SET #{o.values.map { |value| visit value }.join ', '}" unless o.values.empty?), - ("WHERE #{o.wheres.map { |x| visit x }.join ' AND '}" unless o.wheres.empty?), - ("ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?), - ("LIMIT #{o.limit}" if o.limit), + ("WHERE #{wheres.map { |x| visit x }.join ' AND '}" unless wheres.empty?) ].compact.join ' ' end - def visit_Arel_Nodes_InsertStatement o [ "INSERT INTO #{visit o.relation}", diff --git a/lib/arel/visitors/update_sql.rb b/lib/arel/visitors/update_sql.rb deleted file mode 100644 index 02196a0fb0..0000000000 --- a/lib/arel/visitors/update_sql.rb +++ /dev/null @@ -1,26 +0,0 @@ -module Arel - module Visitors - module UpdateSql - def visit_Arel_Nodes_UpdateStatement o - if o.orders.empty? && o.limit.nil? - wheres = o.wheres - else - stmt = Nodes::SelectStatement.new - core = stmt.cores.first - core.froms = o.relation - core.projections = [o.relation.primary_key] - stmt.limit = o.limit - stmt.orders = o.orders - - wheres = [Nodes::In.new(o.relation.primary_key, [stmt])] - end - - [ - "UPDATE #{visit o.relation}", - ("SET #{o.values.map { |value| visit value }.join ', '}" unless o.values.empty?), - ("WHERE #{wheres.map { |x| visit x }.join ' AND '}" unless wheres.empty?) - ].compact.join ' ' - end - end - end -end |