Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Don't use a subquery when there are joins. Use a required alias for t…

…he subquery.
  • Loading branch information...
commit a040b1c0e68f7a5145a6decf6731be9c371fa24a 1 parent 71e9299
@miloops miloops authored
View
7 lib/arel/engines/sql/relations/compiler.rb
@@ -8,9 +8,10 @@ def initialize(relation)
end
def select_sql
- if relation.projections.first.is_a?(Count) && relation.projections.size == 1 && (taken.present? || wheres.present?)
+ if relation.projections.first.is_a?(Count) && relation.projections.size == 1 &&
+ (taken.present? || wheres.present?) && joins(self).blank?
subquery = build_query("SELECT 1 FROM #{from_clauses}", build_clauses)
- query = "SELECT #{select_clauses.join(', ')} FROM (#{subquery})"
+ query = "SELECT COUNT(*) AS count_id FROM (#{subquery}) AS subquery"
else
query = build_query \
"SELECT #{select_clauses.join(', ')}",
@@ -27,8 +28,8 @@ def build_clauses
("GROUP BY #{group_clauses.join(', ')}" unless groupings.blank? ),
("HAVING #{having_clauses.join(' AND ')}" unless havings.blank? ),
("ORDER BY #{order_clauses.join(', ')}" unless orders.blank? )
- clauses << " #{locked}" unless locked.blank?
engine.add_limit_offset!(clauses,{ :limit => taken, :offset => skipped }) if taken || skipped
+ clauses << " #{locked}" unless locked.blank?
clauses unless clauses.blank?
end
View
8 spec/engines/sql/unit/relations/take_spec.rb
@@ -58,15 +58,15 @@ module Arel
adapter_is :mysql do
sql.should be_like(%Q{
- SELECT COUNT(`users`.`id`) AS count_id
- FROM (SELECT 1 FROM `users` LIMIT 4)
+ SELECT COUNT(*) AS count_id
+ FROM (SELECT 1 FROM `users` LIMIT 4) AS subquery
})
end
adapter_is_not :mysql, :oracle do
sql.should be_like(%Q{
- SELECT COUNT("users"."id") AS count_id
- FROM (SELECT 1 FROM "users" LIMIT 4)
+ SELECT COUNT(*) AS count_id
+ FROM (SELECT 1 FROM "users" LIMIT 4) AS subquery
})
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.