diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb index c454152d0d98bc5a7737689f9d6a2a8d72fe0a43..db2f22750ae1a26256dad2ce9efc07551e3e67c2 100644 --- a/lib/arel/nodes.rb +++ b/lib/arel/nodes.rb @@ -24,6 +24,8 @@ require 'arel/nodes/count' require 'arel/nodes/values' require 'arel/nodes/offset' +require 'arel/nodes/limit' +require 'arel/nodes/top' require 'arel/nodes/sum' require 'arel/nodes/exists' require 'arel/nodes/max' diff --git a/lib/arel/nodes/limit.rb b/lib/arel/nodes/limit.rb new file mode 100644 index 0000000000000000000000000000000000000000..68ea95daf50ebfa3d185a723c55f68af5650650d --- /dev/null +++ b/lib/arel/nodes/limit.rb @@ -0,0 +1,7 @@ +module Arel + module Nodes + class Limit < Arel::Nodes::Unary + end + end +end + diff --git a/lib/arel/nodes/select_core.rb b/lib/arel/nodes/select_core.rb index acc6bb9815d8cd9621208ed518601708d11af06e..501a2aaf7c0ea20cbc9e5bf4f91bd58017ef569c 100644 --- a/lib/arel/nodes/select_core.rb +++ b/lib/arel/nodes/select_core.rb @@ -1,10 +1,11 @@ module Arel module Nodes class SelectCore < Arel::Nodes::Node - attr_accessor :froms, :projections, :wheres, :groups + attr_accessor :top, :froms, :projections, :wheres, :groups attr_accessor :having def initialize + @top = nil @froms = nil @projections = [] @wheres = [] diff --git a/lib/arel/nodes/top.rb b/lib/arel/nodes/top.rb new file mode 100644 index 0000000000000000000000000000000000000000..56e2e97e8dd6957344eb63344306b7693b93731a --- /dev/null +++ b/lib/arel/nodes/top.rb @@ -0,0 +1,6 @@ +module Arel + module Nodes + class Top < Arel::Nodes::Unary + end + end +end diff --git a/lib/arel/select_manager.rb b/lib/arel/select_manager.rb index 10d23378aeb4b9fb7e41a5b1c36acdaf77c8f1df..cb656d5340f6f584e02ecaed3ee8bd0eb24ef4d5 100644 --- a/lib/arel/select_manager.rb +++ b/lib/arel/select_manager.rb @@ -10,7 +10,7 @@ def initialize engine, table = nil end def taken - @ast.limit + @ast.limit && @ast.limit.expr end def constraints @@ -131,7 +131,8 @@ def where_sql end def take limit - @ast.limit = limit + @ast.limit = Nodes::Limit.new(limit) + @ctx.top = Nodes::Top.new(limit) self end diff --git a/lib/arel/visitors.rb b/lib/arel/visitors.rb index 2b0a06d5340f4c399fdf26193438830b4f0109f8..8410b2b912eaa43ff395ceaaf3fbe1d20982bccd 100644 --- a/lib/arel/visitors.rb +++ b/lib/arel/visitors.rb @@ -4,6 +4,7 @@ require 'arel/visitors/sqlite' require 'arel/visitors/postgresql' require 'arel/visitors/mysql' +require 'arel/visitors/mssql' require 'arel/visitors/oracle' require 'arel/visitors/join_sql' require 'arel/visitors/where_sql' @@ -16,6 +17,8 @@ module Visitors 'postgresql' => Arel::Visitors::PostgreSQL, 'mysql' => Arel::Visitors::MySQL, 'mysql2' => Arel::Visitors::MySQL, + 'mssql' => Arel::Visitors::MSSQL, + 'sqlserver' => Arel::Visitors::MSSQL, 'oracle_enhanced' => Arel::Visitors::Oracle, 'sqlite' => Arel::Visitors::SQLite, 'sqlite3' => Arel::Visitors::SQLite, diff --git a/lib/arel/visitors/mssql.rb b/lib/arel/visitors/mssql.rb new file mode 100644 index 0000000000000000000000000000000000000000..9b0e77c19bc8478d32ab8df8063b6b1bfe12d820 --- /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 ace8fb0979714dcdba94a2dffd3542a44244a93d..b37d76f710dd8887fd3a0949dfc8f8528f7493b6 100644 --- a/lib/arel/visitors/mysql.rb +++ b/lib/arel/visitors/mysql.rb @@ -10,7 +10,7 @@ def visit_Arel_Nodes_Lock o # :'( # 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 c45160851d17f67e4b778b5edca6494348d3fc2a..237ae913bbb4998e9348ac4d9a6c225b5e21c379 100644 --- a/lib/arel/visitors/sqlite.rb +++ b/lib/arel/visitors/sqlite.rb @@ -3,7 +3,7 @@ module Visitors 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 a2105d704334d2d0e63ea97a9d5e7452c23d97ac..7435e4156176440bb2d681e40708741354d52656 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -76,7 +76,7 @@ def visit_Arel_Nodes_SelectStatement o [ 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 @@ def visit_Arel_Nodes_SelectStatement o 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 @@ def visit_Arel_Nodes_Offset o "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 diff --git a/test/visitors/test_mssql.rb b/test/visitors/test_mssql.rb new file mode 100644 index 0000000000000000000000000000000000000000..b658e46828e7ea45bd8432a39da6c2968392f44b --- /dev/null +++ b/test/visitors/test_mssql.rb @@ -0,0 +1,18 @@ +require 'helper' + +module Arel + module Visitors + describe 'the mssql visitor' do + before do + @visitor = MSSQL.new Table.engine + end + + it 'uses TOP to limit results' do + stmt = Nodes::SelectStatement.new + stmt.cores.last.top = Nodes::Top.new(1) + sql = @visitor.accept(stmt) + sql.must_be_like "SELECT TOP 1" + end + end + end +end diff --git a/test/visitors/test_to_sql.rb b/test/visitors/test_to_sql.rb index e08dc89793b0eb368faec72be24c57c1d320d229..33783f7d234bd0186eba2e418e7bc581000bc23d 100644 --- a/test/visitors/test_to_sql.rb +++ b/test/visitors/test_to_sql.rb @@ -32,7 +32,7 @@ module Visitors it "should escape LIMIT" do sc = Arel::Nodes::SelectStatement.new - sc.limit = "omg" + sc.limit = Arel::Nodes::Limit.new("omg") assert_match(/LIMIT 'omg'/, @visitor.accept(sc)) end