aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/visitors/mssql.rb
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2014-04-08 20:56:49 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2014-04-08 20:56:49 -0700
commitd3d7c218cb577919ce177f8155dc2f34d994f3cb (patch)
treecf8e936e643ff81ff6860fef43f2027409fad30b /lib/arel/visitors/mssql.rb
parent3cd723dee6abc52a508fabec432379b8b31621e7 (diff)
downloadrails-d3d7c218cb577919ce177f8155dc2f34d994f3cb.tar.gz
rails-d3d7c218cb577919ce177f8155dc2f34d994f3cb.tar.bz2
rails-d3d7c218cb577919ce177f8155dc2f34d994f3cb.zip
mssql visitor is working
Diffstat (limited to 'lib/arel/visitors/mssql.rb')
-rw-r--r--lib/arel/visitors/mssql.rb37
1 files changed, 21 insertions, 16 deletions
diff --git a/lib/arel/visitors/mssql.rb b/lib/arel/visitors/mssql.rb
index 4085af9069..0e5b75ec59 100644
--- a/lib/arel/visitors/mssql.rb
+++ b/lib/arel/visitors/mssql.rb
@@ -1,13 +1,7 @@
module Arel
module Visitors
class MSSQL < Arel::Visitors::ToSql
- class RowNumber
- attr_reader :children
-
- def initialize node
- @children = node
- end
- end
+ RowNumber = Struct.new :children
private
@@ -18,13 +12,14 @@ module Arel
""
end
- def visit_Arel_Visitors_MSSQL_RowNumber o
- "ROW_NUMBER() OVER (ORDER BY #{o.children.map { |x| visit x }.join ', '}) as _row_num"
+ def visit_Arel_Visitors_MSSQL_RowNumber o, collector
+ collector << "ROW_NUMBER() OVER (ORDER BY "
+ inject_join(o.children, collector, ', ') << ") as _row_num"
end
- def visit_Arel_Nodes_SelectStatement o
+ def visit_Arel_Nodes_SelectStatement o, collector
if !o.limit && !o.offset
- return super o
+ return super
end
is_select_count = false
@@ -38,12 +33,22 @@ module Arel
end
}
- sql = o.cores.map { |x| visit_Arel_Nodes_SelectCore x }.join
+ if is_select_count
+ # fixme count distinct wouldn't work with limit or offset
+ collector << "SELECT COUNT(1) as count_id FROM ("
+ end
+
+ collector << "SELECT _t.* FROM ("
+ collector = o.cores.inject(collector) { |c,x|
+ visit_Arel_Nodes_SelectCore x, c
+ }
+ collector << ") as _t WHERE #{get_offset_limit_clause(o)}"
- sql = "SELECT _t.* FROM (#{sql}) as _t WHERE #{get_offset_limit_clause(o)}"
- # fixme count distinct wouldn't work with limit or offset
- sql = "SELECT COUNT(1) as count_id FROM (#{sql}) AS subquery" if is_select_count
- sql
+ if is_select_count
+ collector << ") AS subquery"
+ else
+ collector
+ end
end
def get_offset_limit_clause o