Skip to content
Browse files

Make #table_from_select_statement go thru a matrix of most to least i…

…mportant routes.
  • Loading branch information...
1 parent 70f06d2 commit c473cfaf838be6ac46155f9524b2a4ff1f84ec0e @metaskills metaskills committed Jan 15, 2011
Showing with 22 additions and 29 deletions.
  1. +12 −5 lib/arel/visitors/sqlserver.rb
  2. +10 −24 test/cases/scratch_test_sqlserver.rb
View
17 lib/arel/visitors/sqlserver.rb
@@ -189,8 +189,15 @@ def visit_Arel_Nodes_SelectStatementForComplexCount(o)
# SQLServer Helpers
- def table_name_from_select_statement(o)
- o.cores.first.source.left.name
+ def table_from_select_statement(o)
+ core = o.cores.first
+ if Arel::Table === core.from
+ core.from
+ elsif Arel::Nodes::SqlLiteral === core.from
+ Arel::Table.new(core.from, @engine)
+ elsif Arel::Nodes::JoinSource === core.source
+ Arel::Nodes::SqlLiteral === core.source.left ? Arel::Table.new(core.source.left, @engine) : core.source.left
+ end
end
def single_distinct_select_statement?(o)
@@ -247,7 +254,7 @@ def find_and_fix_uncorrelated_joins_in_select_statement(o)
def rowtable_projections(o)
core = o.cores.first
if single_distinct_select_statement?(o)
- tn = table_name_from_select_statement(o)
+ tn = table_from_select_statement(o).name
core.projections.map do |x|
x.dup.tap do |p|
p.sub! 'DISTINCT', "DISTINCT #{visit(o.limit)}".strip if o.limit
@@ -262,7 +269,7 @@ def rowtable_projections(o)
elsif function_select_statement?(o)
[Arel.star]
else
- tn = table_name_from_select_statement(o)
+ tn = table_from_select_statement(o).name
core.projections.map { |x| x.gsub /\[#{tn}\]\./, '[__rnt].' }
end
end
@@ -274,7 +281,7 @@ def rowtable_orders(o)
elsif join_in_select_statement?(o)
[core.from.primary_key.asc]
else
- [core.from.primary_key.asc]
+ [table_from_select_statement(o).primary_key.asc]
end.reverse.uniq.reverse
end
View
34 test/cases/scratch_test_sqlserver.rb
@@ -1,34 +1,20 @@
require 'cases/sqlserver_helper'
-require 'models/post'
-require 'models/tagging'
-require 'models/tag'
-require 'models/comment'
-require 'models/author'
-require 'models/category'
require 'models/company'
-require 'models/person'
-require 'models/reader'
-require 'models/owner'
-require 'models/pet'
-require 'models/reference'
-require 'models/job'
-require 'models/subscriber'
-require 'models/subscription'
-require 'models/book'
-require 'models/developer'
-require 'models/project'
+require 'models/topic'
+require 'models/edge'
+require 'models/club'
+require 'models/organization'
class ScratchTestSqlserver < ActiveRecord::TestCase
- fixtures :posts, :comments, :authors, :author_addresses, :categories, :categories_posts,
- :companies, :accounts, :tags, :taggings, :people, :readers,
- :owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books,
- :developers, :projects, :developers_projects
+ fixtures :companies, :accounts, :topics
should 'pass' do
- comments = Comment.find(:all, :include => :post, :limit => 3, :offset => 2, :order => 'comments.id')
- assert_equal 3, comments.length
- assert_equal [3,5,6], comments.collect { |c| c.id }
+ assert_equal Company.count(:all), Company.count(:all, :from => 'companies')
+ assert_equal Account.count(:all, :conditions => "credit_limit = 50"),
+ Account.count(:all, :from => 'accounts', :conditions => "credit_limit = 50")
+ assert_equal Company.count(:type, :conditions => {:type => "Firm"}),
+ Company.count(:type, :conditions => {:type => "Firm"}, :from => 'companies')
end

0 comments on commit c473cfa

Please sign in to comment.
Something went wrong with that request. Please try again.