aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2011-01-03 10:31:25 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2011-01-03 10:31:25 -0800
commitd4259c5ce2f6b05226a8b934537678ade1606e97 (patch)
tree9059008ad935fcf93e979a3b057e1fdc19994dc9 /lib/arel
parentcefad1e2139c4bba6e45f3281a6d54394aaeb366 (diff)
parent1215c28311aab325c55c083d15cbf3e16599d502 (diff)
downloadrails-d4259c5ce2f6b05226a8b934537678ade1606e97.tar.gz
rails-d4259c5ce2f6b05226a8b934537678ade1606e97.tar.bz2
rails-d4259c5ce2f6b05226a8b934537678ade1606e97.zip
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
Diffstat (limited to 'lib/arel')
-rw-r--r--lib/arel/nodes/limit.rb7
-rw-r--r--lib/arel/nodes/select_core.rb3
-rw-r--r--lib/arel/nodes/top.rb6
-rw-r--r--lib/arel/nodes/unary.rb6
-rw-r--r--lib/arel/select_manager.rb5
-rw-r--r--lib/arel/visitors.rb3
-rw-r--r--lib/arel/visitors/mssql.rb16
-rw-r--r--lib/arel/visitors/mysql.rb2
-rw-r--r--lib/arel/visitors/sqlite.rb2
-rw-r--r--lib/arel/visitors/to_sql.rb19
10 files changed, 60 insertions, 9 deletions
diff --git a/lib/arel/nodes/limit.rb b/lib/arel/nodes/limit.rb
new file mode 100644
index 0000000000..68ea95daf5
--- /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 8eb7c0d3e7..7f577e0a05 100644
--- a/lib/arel/nodes/select_core.rb
+++ b/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 = []
diff --git a/lib/arel/nodes/top.rb b/lib/arel/nodes/top.rb
new file mode 100644
index 0000000000..56e2e97e8d
--- /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/nodes/unary.rb b/lib/arel/nodes/unary.rb
index f679017597..e1576b9c99 100644
--- a/lib/arel/nodes/unary.rb
+++ b/lib/arel/nodes/unary.rb
@@ -10,12 +10,14 @@ module Arel
end
%w{
- Not
Group
Grouping
- Offset
Having
+ Limit
+ Not
+ Offset
On
+ Top
}.each do |name|
const_set(name, Class.new(Unary))
end
diff --git a/lib/arel/select_manager.rb b/lib/arel/select_manager.rb
index 274633c9db..3e0d4038d7 100644
--- a/lib/arel/select_manager.rb
+++ b/lib/arel/select_manager.rb
@@ -10,7 +10,7 @@ module Arel
end
def limit
- @ast.limit
+ @ast.limit && @ast.limit.expr
end
alias :taken :limit
@@ -137,7 +137,8 @@ module Arel
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 2b0a06d534..8410b2b912 100644
--- a/lib/arel/visitors.rb
+++ b/lib/arel/visitors.rb
@@ -4,6 +4,7 @@ require 'arel/visitors/to_sql'
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 Arel
'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 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 f25c0ee69f..9830e559d5 100644
--- a/lib/arel/visitors/to_sql.rb
+++ b/lib/arel/visitors/to_sql.rb
@@ -131,7 +131,7 @@ eowarn
[
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 ' '
@@ -139,7 +139,9 @@ eowarn
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 @@ eowarn
"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 @@ eowarn
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