aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--History.txt4
-rw-r--r--lib/arel/visitors/mssql.rb7
-rw-r--r--lib/arel/visitors/to_sql.rb21
3 files changed, 23 insertions, 9 deletions
diff --git a/History.txt b/History.txt
index 51e950512f..aa5bbd77e2 100644
--- a/History.txt
+++ b/History.txt
@@ -7,6 +7,10 @@
* SQL Literals may be used as Attribute names
* Added Arel::Nodes::NamedFunction for representing generic SQL functions
+* Bug fixes
+
+ * MSSQL adds TOP to sub selects
+
* Deprecations
* Calls to `insert` are deprecated. Please use `compile_insert` then call
diff --git a/lib/arel/visitors/mssql.rb b/lib/arel/visitors/mssql.rb
index 9b0e77c19b..ea7ab6394c 100644
--- a/lib/arel/visitors/mssql.rb
+++ b/lib/arel/visitors/mssql.rb
@@ -3,6 +3,13 @@ module Arel
class MSSQL < Arel::Visitors::ToSql
private
+ def build_subselect key, o
+ stmt = super
+ core = stmt.cores.first
+ core.top = Nodes::Top.new(o.limit.expr) if o.limit
+ stmt
+ end
+
def visit_Arel_Nodes_Limit o
""
end
diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb
index a395b7f765..61f68a8c09 100644
--- a/lib/arel/visitors/to_sql.rb
+++ b/lib/arel/visitors/to_sql.rb
@@ -38,6 +38,17 @@ module Arel
].compact.join ' '
end
+ # FIXME: we should probably have a 2-pass visitor for this
+ def build_subselect key, o
+ stmt = Nodes::SelectStatement.new
+ core = stmt.cores.first
+ core.froms = o.relation
+ core.projections = [key]
+ stmt.limit = o.limit
+ stmt.orders = o.orders
+ stmt
+ end
+
def visit_Arel_Nodes_UpdateStatement o
if o.orders.empty? && o.limit.nil?
wheres = o.wheres
@@ -52,15 +63,7 @@ key on UpdateManager using UpdateManager#key=
key = o.relation.primary_key
end
- wheres = o.wheres
- stmt = Nodes::SelectStatement.new
- core = stmt.cores.first
- core.froms = o.relation
- core.projections = [key]
- stmt.limit = o.limit
- stmt.orders = o.orders
-
- wheres = [Nodes::In.new(key, [stmt])]
+ wheres = [Nodes::In.new(key, [build_subselect(key, o)])]
end
[