Skip to content

Commit f081b08

Browse files
authored
Merge pull request rails#54941 from skipkayhil/hm-simplify-query-all
Pass existing connection to #arel in #to_sql
2 parents 8b00d09 + 6ff9e82 commit f081b08

File tree

4 files changed

+18
-23
lines changed

4 files changed

+18
-23
lines changed

activerecord/lib/active_record/associations/join_dependency/join_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def join_constraints(foreign_table, foreign_klass, join_type, alias_tracker)
5353
end
5454
end
5555

56-
arel = scope.arel(alias_tracker.aliases)
56+
arel = scope.arel(aliases: alias_tracker.aliases)
5757
nodes = arel.constraints.first
5858

5959
if nodes.is_a?(Arel::Nodes::And)

activerecord/lib/active_record/relation.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -625,17 +625,15 @@ def update_all(updates)
625625
end
626626

627627
model.with_connection do |c|
628-
arel = eager_loading? ? apply_join_dependency.arel : build_arel(c)
628+
arel = eager_loading? ? apply_join_dependency.arel : arel(c)
629629
arel.source.left = table
630630

631-
group_values_arel_columns = arel_columns(group_values.uniq)
632-
having_clause_ast = having_clause.ast unless having_clause.empty?
633631
key = if model.composite_primary_key?
634632
primary_key.map { |pk| table[pk] }
635633
else
636634
table[primary_key]
637635
end
638-
stmt = arel.compile_update(values, key, having_clause_ast, group_values_arel_columns)
636+
stmt = arel.compile_update(values, key)
639637
c.update(stmt, "#{model} Update All").tap { reset }
640638
end
641639
end
@@ -1042,17 +1040,15 @@ def delete_all
10421040
end
10431041

10441042
model.with_connection do |c|
1045-
arel = eager_loading? ? apply_join_dependency.arel : build_arel(c)
1043+
arel = eager_loading? ? apply_join_dependency.arel : arel(c)
10461044
arel.source.left = table
10471045

1048-
group_values_arel_columns = arel_columns(group_values.uniq)
1049-
having_clause_ast = having_clause.ast unless having_clause.empty?
10501046
key = if model.composite_primary_key?
10511047
primary_key.map { |pk| table[pk] }
10521048
else
10531049
table[primary_key]
10541050
end
1055-
stmt = arel.compile_delete(key, having_clause_ast, group_values_arel_columns)
1051+
stmt = arel.compile_delete(key)
10561052

10571053
c.delete(stmt, "#{model} Delete All").tap { reset }
10581054
end
@@ -1237,7 +1233,7 @@ def to_sql
12371233
end
12381234
else
12391235
model.with_connection do |conn|
1240-
conn.unprepared_statement { conn.to_sql(arel) }
1236+
conn.unprepared_statement { conn.to_sql(arel(conn)) }
12411237
end
12421238
end
12431239
end

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,8 +1591,12 @@ def excluding!(records) # :nodoc:
15911591
end
15921592

15931593
# Returns the Arel object associated with the relation.
1594-
def arel(aliases = nil) # :nodoc:
1595-
@arel ||= with_connection { |c| build_arel(c, aliases) }
1594+
def arel(conn = nil, aliases: nil) # :nodoc:
1595+
@arel ||= if conn
1596+
build_arel(conn, aliases)
1597+
else
1598+
with_connection { |c| build_arel(c, aliases) }
1599+
end
15961600
end
15971601

15981602
def construct_join_dependency(associations, join_type) # :nodoc:

activerecord/lib/arel/crud.rb

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ def create_insert
1414
InsertManager.new
1515
end
1616

17-
def compile_update(
18-
values,
19-
key = nil,
20-
having_clause = nil,
21-
group_values_columns = []
22-
)
17+
def compile_update(values, key = nil)
2318
um = UpdateManager.new(source)
2419
um.set(values)
2520
um.take(limit)
@@ -28,20 +23,20 @@ def compile_update(
2823
um.wheres = constraints
2924
um.key = key
3025

31-
um.group(group_values_columns) unless group_values_columns.empty?
32-
um.having(having_clause) unless having_clause.nil?
26+
um.ast.groups = @ctx.groups
27+
@ctx.havings.each { |h| um.having(h) }
3328
um
3429
end
3530

36-
def compile_delete(key = nil, having_clause = nil, group_values_columns = [])
31+
def compile_delete(key = nil)
3732
dm = DeleteManager.new(source)
3833
dm.take(limit)
3934
dm.offset(offset)
4035
dm.order(*orders)
4136
dm.wheres = constraints
4237
dm.key = key
43-
dm.group(group_values_columns) unless group_values_columns.empty?
44-
dm.having(having_clause) unless having_clause.nil?
38+
dm.ast.groups = @ctx.groups
39+
@ctx.havings.each { |h| dm.having(h) }
4540
dm
4641
end
4742
end

0 commit comments

Comments
 (0)