mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
taking advantage of the JoinSource node in the SQL AST
This commit is contained in:
@@ -47,24 +47,16 @@ module ActiveRecord
|
||||
end
|
||||
|
||||
def count_aliases_from_table_joins(name)
|
||||
return 0 if !@table_joins || Arel::Table === @table_joins
|
||||
return 0 if Arel::Table === @table_joins
|
||||
|
||||
# quoted_name should be downcased as some database adapters (Oracle) return quoted name in uppercase
|
||||
quoted_name = active_record.connection.quote_table_name(name).downcase
|
||||
|
||||
@table_joins.grep(Arel::Nodes::Join).map { |join|
|
||||
right = join.right
|
||||
case right
|
||||
when Arel::Table
|
||||
right.name.downcase == name ? 1 : 0
|
||||
when String
|
||||
# Table names + table aliases
|
||||
right.downcase.scan(
|
||||
/join(?:\s+\w+)?\s+(\S+\s+)?#{quoted_name}\son/
|
||||
).size
|
||||
else
|
||||
0
|
||||
end
|
||||
@table_joins.map { |join|
|
||||
# Table names + table aliases
|
||||
join.left.downcase.scan(
|
||||
/join(?:\s+\w+)?\s+(\S+\s+)?#{quoted_name}\son/
|
||||
).size
|
||||
}.sum
|
||||
end
|
||||
|
||||
|
||||
@@ -138,7 +138,6 @@ module ActiveRecord
|
||||
ands = relation.create_and(conditions)
|
||||
|
||||
join = relation.create_join(
|
||||
relation.froms.first,
|
||||
target_table,
|
||||
relation.create_on(ands),
|
||||
join_type)
|
||||
|
||||
@@ -187,7 +187,7 @@ module ActiveRecord
|
||||
|
||||
def find_with_associations
|
||||
including = (@eager_load_values + @includes_values).uniq
|
||||
join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, including, nil)
|
||||
join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, including, [])
|
||||
rows = construct_relation_for_association_find(join_dependency).to_a
|
||||
join_dependency.instantiate(rows)
|
||||
rescue ThrowResult
|
||||
|
||||
@@ -191,27 +191,19 @@ module ActiveRecord
|
||||
def custom_join_ast(table, joins)
|
||||
joins = joins.reject { |join| join.blank? }
|
||||
|
||||
return if joins.empty?
|
||||
return [] if joins.empty?
|
||||
|
||||
@implicit_readonly = true
|
||||
|
||||
joins.map! do |join|
|
||||
joins.map do |join|
|
||||
case join
|
||||
when Array
|
||||
join = Arel.sql(join.join(' ')) if array_of_strings?(join)
|
||||
when String
|
||||
join = Arel.sql(join)
|
||||
end
|
||||
join
|
||||
table.create_string_join(join)
|
||||
end
|
||||
|
||||
head = table.create_string_join(table, joins.shift)
|
||||
|
||||
joins.inject(head) do |ast, join|
|
||||
ast.right = table.create_string_join(ast.right, join)
|
||||
end
|
||||
|
||||
head
|
||||
end
|
||||
|
||||
def collapse_wheres(arel, wheres)
|
||||
@@ -256,9 +248,9 @@ module ActiveRecord
|
||||
stashed_association_joins = joins.grep(ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation)
|
||||
|
||||
non_association_joins = (joins - association_joins - stashed_association_joins)
|
||||
join_ast = custom_join_ast(manager.froms.first, non_association_joins)
|
||||
join_list = custom_join_ast(manager, non_association_joins)
|
||||
|
||||
join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, association_joins, join_ast)
|
||||
join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, association_joins, join_list)
|
||||
|
||||
join_dependency.graft(*stashed_association_joins)
|
||||
|
||||
@@ -269,10 +261,9 @@ module ActiveRecord
|
||||
association.join_to(manager)
|
||||
end
|
||||
|
||||
return manager unless join_ast
|
||||
return manager unless join_list
|
||||
|
||||
join_ast.left = manager.froms.first
|
||||
manager.from join_ast
|
||||
join_list.each { |j| manager.from j }
|
||||
manager
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user