From 40105685957e377c263cfdd59178d3efc3a64181 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 6 Dec 2010 20:26:08 -0800 Subject: quoting limit nodes --- lib/arel/visitors/mysql.rb | 2 +- lib/arel/visitors/postgresql.rb | 2 +- lib/arel/visitors/to_sql.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/arel/visitors') diff --git a/lib/arel/visitors/mysql.rb b/lib/arel/visitors/mysql.rb index 143b4d36f4..ace8fb0979 100644 --- a/lib/arel/visitors/mysql.rb +++ b/lib/arel/visitors/mysql.rb @@ -25,7 +25,7 @@ module Arel ("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), + ("LIMIT #{visit o.limit}" if o.limit), ].compact.join ' ' end diff --git a/lib/arel/visitors/postgresql.rb b/lib/arel/visitors/postgresql.rb index 553ee91bf9..01fbda75b9 100644 --- a/lib/arel/visitors/postgresql.rb +++ b/lib/arel/visitors/postgresql.rb @@ -17,7 +17,7 @@ module Arel [ "SELECT * FROM (#{sql}) AS id_list", "ORDER BY #{aliased_orders(o.orders).join(', ')}", - ("LIMIT #{o.limit}" if o.limit), + ("LIMIT #{visit o.limit}" if o.limit), (visit(o.offset) if o.offset), ].compact.join ' ' else diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 2713621a54..2fb464b265 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -76,7 +76,7 @@ module Arel [ o.cores.map { |x| visit_Arel_Nodes_SelectCore x }.join, ("ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?), - ("LIMIT #{o.limit}" if o.limit), + ("LIMIT #{visit o.limit}" if o.limit), (visit(o.offset) if o.offset), (visit(o.lock) if o.lock), ].compact.join ' ' -- cgit v1.2.3 From 16ee8f66bcc527406e44c28a441e8f25481c5a0d Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Tue, 7 Dec 2010 10:05:42 +0100 Subject: implementation for passing a subquery to #in and #not_in --- lib/arel/visitors/to_sql.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'lib/arel/visitors') diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 2fb464b265..6fc206c12c 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -72,6 +72,10 @@ module Arel }.join ', '})" end + def visit_Arel_SelectManager o + o.to_sql + end + def visit_Arel_Nodes_SelectStatement o [ o.cores.map { |x| visit_Arel_Nodes_SelectCore x }.join, @@ -204,17 +208,11 @@ module Arel end def visit_Arel_Nodes_In o - right = o.right - "#{visit o.left} IN (#{ - right.empty? ? 'NULL' : right.map { |x| visit x }.join(', ') - })" + "#{visit o.left} IN (#{visit o.right})" end def visit_Arel_Nodes_NotIn o - right = o.right - "#{visit o.left} NOT IN (#{ - right.empty? ? 'NULL' : right.map { |x| visit x }.join(', ') - })" + "#{visit o.left} NOT IN (#{visit o.right})" end def visit_Arel_Nodes_And o @@ -291,6 +289,10 @@ module Arel alias :visit_ActiveSupport_StringInquirer :visit_String alias :visit_Class :visit_String + def visit_Array o + o.empty? ? 'NULL' : o.map { |x| visit x }.join(', ') + end + def quote value, column = nil @connection.quote value, column end -- cgit v1.2.3 From 4147f4bbf8c47379e795acfe0b65d6260e23a197 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 7 Dec 2010 10:46:30 -0800 Subject: refactoring where, fixing subselect --- lib/arel/visitors/to_sql.rb | 4 ---- 1 file changed, 4 deletions(-) (limited to 'lib/arel/visitors') diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 6fc206c12c..a2105d7043 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -72,10 +72,6 @@ module Arel }.join ', '})" end - def visit_Arel_SelectManager o - o.to_sql - end - def visit_Arel_Nodes_SelectStatement o [ o.cores.map { |x| visit_Arel_Nodes_SelectCore x }.join, -- cgit v1.2.3 From ee3c55c84996ad2565762fa1f472259460d7d731 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 6 Dec 2010 20:16:16 -0800 Subject: consolidating dot visitor methods --- lib/arel/visitors/dot.rb | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'lib/arel/visitors') diff --git a/lib/arel/visitors/dot.rb b/lib/arel/visitors/dot.rb index c515cbe220..eab5e4afdc 100644 --- a/lib/arel/visitors/dot.rb +++ b/lib/arel/visitors/dot.rb @@ -28,10 +28,6 @@ module Arel end private - def visit_Arel_Nodes_Grouping o - visit_edge o, "expr" - end - def visit_Arel_Nodes_Ordering o visit_edge o, "expr" visit_edge o, "direction" @@ -55,10 +51,6 @@ module Arel visit_edge o, "distinct" end - def visit_Arel_Nodes_On o - visit_edge o, "expr" - end - def visit_Arel_Nodes_Values o visit_edge o, "expressions" end @@ -80,10 +72,6 @@ module Arel visit_edge o, "wheres" end - def visit_Arel_Nodes_UnqualifiedColumn o - visit_edge o, "attribute" - end - def unary o visit_edge o, "expr" end -- cgit v1.2.3 From 1a246f71616cf246a75ef6cbdb56032e43d4e643 Mon Sep 17 00:00:00 2001 From: Viacheslav Petrenko Date: Mon, 27 Dec 2010 18:16:18 +0200 Subject: Patched Arel v2.0.6 to support MSSQL SQL queries. Based on work of James Abley (https://github.com/jabley/arel). --- lib/arel/visitors/mssql.rb | 16 ++++++++++++++++ lib/arel/visitors/mysql.rb | 2 +- lib/arel/visitors/sqlite.rb | 2 +- lib/arel/visitors/to_sql.rb | 15 +++++++++++++-- 4 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 lib/arel/visitors/mssql.rb (limited to 'lib/arel/visitors') diff --git a/lib/arel/visitors/mssql.rb b/lib/arel/visitors/mssql.rb new file mode 100644 index 0000000000..9b0e77c19b --- /dev/null +++ b/lib/arel/visitors/mssql.rb @@ -0,0 +1,16 @@ +module Arel + module Visitors + class MSSQL < Arel::Visitors::ToSql + private + + def visit_Arel_Nodes_Limit o + "" + end + + def visit_Arel_Nodes_Top o + "TOP #{visit o.expr}" + end + + end + end +end diff --git a/lib/arel/visitors/mysql.rb b/lib/arel/visitors/mysql.rb index ace8fb0979..b37d76f710 100644 --- a/lib/arel/visitors/mysql.rb +++ b/lib/arel/visitors/mysql.rb @@ -10,7 +10,7 @@ module Arel # :'( # http://dev.mysql.com/doc/refman/5.0/en/select.html#id3482214 def visit_Arel_Nodes_SelectStatement o - o.limit = 18446744073709551615 if o.offset && !o.limit + o.limit = Arel::Nodes::Limit.new(18446744073709551615) if o.offset && !o.limit super end diff --git a/lib/arel/visitors/sqlite.rb b/lib/arel/visitors/sqlite.rb index c45160851d..237ae913bb 100644 --- a/lib/arel/visitors/sqlite.rb +++ b/lib/arel/visitors/sqlite.rb @@ -3,7 +3,7 @@ module Arel class SQLite < Arel::Visitors::ToSql private def visit_Arel_Nodes_SelectStatement o - o.limit = -1 if o.offset && !o.limit + o.limit = Arel::Nodes::Limit.new(-1) if o.offset && !o.limit super end end diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index a2105d7043..7435e41561 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -76,7 +76,7 @@ module Arel [ o.cores.map { |x| visit_Arel_Nodes_SelectCore x }.join, ("ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?), - ("LIMIT #{visit o.limit}" if o.limit), + (visit(o.limit) if o.limit), (visit(o.offset) if o.offset), (visit(o.lock) if o.lock), ].compact.join ' ' @@ -84,7 +84,9 @@ module Arel def visit_Arel_Nodes_SelectCore o [ - "SELECT #{o.projections.map { |x| visit x }.join ', '}", + "SELECT", + (visit(o.top) if o.top), + "#{o.projections.map { |x| visit x }.join ', '}", ("FROM #{visit o.froms}" if o.froms), ("WHERE #{o.wheres.map { |x| visit x }.join ' AND ' }" unless o.wheres.empty?), ("GROUP BY #{o.groups.map { |x| visit x }.join ', ' }" unless o.groups.empty?), @@ -100,6 +102,15 @@ module Arel "OFFSET #{visit o.expr}" end + def visit_Arel_Nodes_Limit o + "LIMIT #{visit o.expr}" + end + + # FIXME: this does nothing on most databases, but does on MSSQL + def visit_Arel_Nodes_Top o + "" + end + # FIXME: this does nothing on SQLLite3, but should do things on other # databases. def visit_Arel_Nodes_Lock o -- cgit v1.2.3