Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding TOP to sub selects for mssql

  • Loading branch information...
commit 066d9a3f5d24feb628187117769aee81f8538159 1 parent 3456466
@tenderlove tenderlove authored
View
4 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
View
7 lib/arel/visitors/mssql.rb
@@ -3,6 +3,13 @@ module Visitors
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
View
21 lib/arel/visitors/to_sql.rb
@@ -38,6 +38,17 @@ def visit_Arel_Nodes_DeleteStatement o
].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 @@ def visit_Arel_Nodes_UpdateStatement o
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
[
Please sign in to comment.
Something went wrong with that request. Please try again.