Skip to content

Commit 70f06d2

Browse files
committed
Fix row table projections when all projections are aliased.
1 parent 226e6fe commit 70f06d2

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

lib/arel/visitors/sqlserver.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ def single_distinct_select_statement?(o)
200200
((first_prjn.respond_to?(:distinct) && first_prjn.distinct) || first_prjn.include?('DISTINCT'))
201201
end
202202

203+
def all_projections_aliased_in_select_statement?(o)
204+
projections = o.cores.first.projections
205+
projections.all? do |x|
206+
x.split(',').all? { |y| y.include?(' AS ') }
207+
end
208+
end
209+
203210
def function_select_statement?(o)
204211
core = o.cores.first
205212
core.projections.any? { |x| Arel::Nodes::Function === x }
@@ -248,11 +255,10 @@ def rowtable_projections(o)
248255
p.strip!
249256
end
250257
end
251-
elsif false # join_in_select_statement?(o) && all_select_clauses_aliased?
252-
raise 'TODO: join_in_select_statement?(o) && all_select_clauses_aliased?'
253-
# relation.select_clauses.map do |sc|
254-
# sc.split(',').map { |c| c.split(' AS ').last.strip }.join(', ')
255-
# end
258+
elsif join_in_select_statement?(o) && all_projections_aliased_in_select_statement?(o)
259+
core.projections.map do |x|
260+
Arel::Nodes::SqlLiteral.new x.split(',').map{ |y| y.split(' AS ').last.strip }.join(', ')
261+
end
256262
elsif function_select_statement?(o)
257263
[Arel.star]
258264
else

test/cases/scratch_test_sqlserver.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ class ScratchTestSqlserver < ActiveRecord::TestCase
2626
:developers, :projects, :developers_projects
2727

2828
should 'pass' do
29-
posts = Post.count(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ])
30-
assert_equal 0, posts
29+
comments = Comment.find(:all, :include => :post, :limit => 3, :offset => 2, :order => 'comments.id')
30+
assert_equal 3, comments.length
31+
assert_equal [3,5,6], comments.collect { |c| c.id }
3132
end
3233

3334

0 commit comments

Comments
 (0)