Browse files

Merge branch '2-0-stable' into merge

* 2-0-stable:
  updating history
  Patched Arel v2.0.6 to support MSSQL SQL queries. Based on work of James Abley (https://github.com/jabley/arel).
  consolidating dot visitor methods
  refactoring where, fixing subselect
  implementation for passing a subquery to #in and #not_in
  tests for passing a subquery to #in and #not_in
  limit members of the AST are visited
  quoting limit nodes

Conflicts:
	History.txt
	lib/arel/nodes.rb
	lib/arel/nodes/select_core.rb
	lib/arel/select_manager.rb
	lib/arel/visitors/to_sql.rb
	test/visitors/test_to_sql.rb
  • Loading branch information...
2 parents cefad1e + 1215c28 commit d4259c5ce2f6b05226a8b934537678ade1606e97 @tenderlove tenderlove committed Jan 3, 2011
View
1 History.txt
@@ -39,6 +39,7 @@
* Bug Fixes
* Limit members are visited
+ * Fixing MSSQL TOP support
== 2.0.6 12/01/2010
View
7 lib/arel/nodes/limit.rb
@@ -0,0 +1,7 @@
+module Arel
+ module Nodes
+ class Limit < Arel::Nodes::Unary
+ end
+ end
+end
+
View
3 lib/arel/nodes/select_core.rb
@@ -1,11 +1,12 @@
module Arel
module Nodes
class SelectCore < Arel::Nodes::Node
- attr_accessor :projections, :wheres, :groups
+ attr_accessor :top, :projections, :wheres, :groups
attr_accessor :having, :source
def initialize
@source = JoinSource.new nil
+ @top = nil
@projections = []
@wheres = []
@groups = []
View
6 lib/arel/nodes/top.rb
@@ -0,0 +1,6 @@
+module Arel
+ module Nodes
+ class Top < Arel::Nodes::Unary
+ end
+ end
+end
View
6 lib/arel/nodes/unary.rb
@@ -10,12 +10,14 @@ def initialize expr
end
%w{
- Not
Group
Grouping
- Offset
Having
+ Limit
+ Not
+ Offset
On
+ Top
}.each do |name|
const_set(name, Class.new(Unary))
end
View
5 lib/arel/select_manager.rb
@@ -10,7 +10,7 @@ def initialize engine, table = nil
end
def limit
- @ast.limit
+ @ast.limit && @ast.limit.expr
end
alias :taken :limit
@@ -137,7 +137,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
View
3 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,
View
16 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
View
2 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
View
2 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
View
19 lib/arel/visitors/to_sql.rb
@@ -131,15 +131,17 @@ 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 ' '
end
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 ', '}",
visit(o.source),
("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?),
@@ -155,6 +157,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
@@ -357,6 +368,10 @@ def visit_Array o
o.empty? ? 'NULL' : o.map { |x| visit x }.join(', ')
end
+ def visit_Array o
+ o.empty? ? 'NULL' : o.map { |x| visit x }.join(', ')
+ end
+
def quote value, column = nil
@connection.quote value, column
end
View
18 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
View
2 test/visitors/test_to_sql.rb
@@ -39,7 +39,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

0 comments on commit d4259c5

Please sign in to comment.