Skip to content

Commit b22a86d

Browse files
committed
Pass QueryIntent to #perform_query
This moves intent-unpacking to the same boundary where we start talking to the raw connection.
1 parent e92a4f6 commit b22a86d

6 files changed

Lines changed: 45 additions & 45 deletions

File tree

‎activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,14 +569,14 @@ def raw_execute(intent)
569569
log(intent) do |notification_payload|
570570
intent.notification_payload = notification_payload
571571
with_raw_connection(allow_retry: intent.allow_retry, materialize_transactions: intent.materialize_transactions) do |conn|
572-
result = perform_query(conn, intent.sql, intent.binds, intent.type_casted_binds, prepare: intent.prepare, notification_payload: intent.notification_payload, batch: intent.batch)
572+
result = perform_query(conn, intent)
573573
handle_warnings(result, intent.sql)
574574
result
575575
end
576576
end
577577
end
578578

579-
def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notification_payload:, batch:)
579+
def perform_query(raw_connection, intent)
580580
raise NotImplementedError
581581
end
582582

‎activerecord/lib/active_record/connection_adapters/mysql2/database_statements.rb‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ def multi_statements_enabled?
4848
end
4949
end
5050

51-
def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notification_payload:, batch: false)
52-
reset_multi_statement = if batch && !multi_statements_enabled?
51+
def perform_query(raw_connection, intent)
52+
reset_multi_statement = if intent.batch && !multi_statements_enabled?
5353
raw_connection.set_server_option(::Mysql2::Client::OPTION_MULTI_STATEMENTS_ON)
5454
true
5555
end
@@ -59,22 +59,22 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
5959
raw_connection.query_options[:database_timezone] = default_timezone
6060

6161
result = nil
62-
if binds.nil? || binds.empty?
63-
result = raw_connection.query(sql)
62+
if intent.binds.nil? || intent.binds.empty?
63+
result = raw_connection.query(intent.sql)
6464
# Ref: https://github.com/brianmario/mysql2/pull/1383
6565
# As of mysql2 0.5.6 `#affected_rows` might raise Mysql2::Error if a prepared statement
6666
# from that same connection was GCed while `#query` released the GVL.
6767
# By avoiding to call `#affected_rows` when we have a result, we reduce the likeliness
6868
# of hitting the bug.
6969
@affected_rows_before_warnings = result&.size || raw_connection.affected_rows
70-
elsif prepare
70+
elsif intent.prepare
7171
retry_count = 1
7272
begin
73-
stmt = @statements[sql] ||= raw_connection.prepare(sql)
74-
result = stmt.execute(*type_casted_binds)
73+
stmt = @statements[intent.sql] ||= raw_connection.prepare(intent.sql)
74+
result = stmt.execute(*intent.type_casted_binds)
7575
@affected_rows_before_warnings = stmt.affected_rows
7676
rescue ::Mysql2::Error => error
77-
@statements.delete(sql)
77+
@statements.delete(intent.sql)
7878
# Sometimes for an unknown reason, we get that error.
7979
# It suggest somehow that the prepared statement was deallocated
8080
# but the client doesn't know it.
@@ -89,10 +89,10 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
8989
raise
9090
end
9191
else
92-
stmt = raw_connection.prepare(sql)
92+
stmt = raw_connection.prepare(intent.sql)
9393

9494
begin
95-
result = stmt.execute(*type_casted_binds)
95+
result = stmt.execute(*intent.type_casted_binds)
9696
@affected_rows_before_warnings = stmt.affected_rows
9797

9898
# Ref: https://github.com/brianmario/mysql2/pull/1383
@@ -110,8 +110,8 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
110110
end
111111
end
112112

113-
notification_payload[:affected_rows] = @affected_rows_before_warnings
114-
notification_payload[:row_count] = result&.size || 0
113+
intent.notification_payload[:affected_rows] = @affected_rows_before_warnings
114+
intent.notification_payload[:row_count] = result&.size || 0
115115

116116
raw_connection.abandon_results!
117117

‎activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ def cancel_any_running_query
139139
rescue PG::Error
140140
end
141141

142-
def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notification_payload:, batch: false)
142+
def perform_query(raw_connection, intent)
143143
update_typemap_for_default_timezone
144-
result = if prepare
144+
result = if intent.prepare
145145
begin
146-
stmt_key = prepare_statement(sql, binds, raw_connection)
147-
notification_payload[:statement_name] = stmt_key
148-
raw_connection.exec_prepared(stmt_key, type_casted_binds)
146+
stmt_key = prepare_statement(intent.sql, intent.binds, raw_connection)
147+
intent.notification_payload[:statement_name] = stmt_key
148+
raw_connection.exec_prepared(stmt_key, intent.type_casted_binds)
149149
rescue PG::FeatureNotSupported => error
150150
if is_cached_plan_failure?(error)
151151
# Nothing we can do if we are in a transaction because all commands
@@ -155,24 +155,24 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
155155
else
156156
@lock.synchronize do
157157
# outside of transactions we can simply flush this query and retry
158-
@statements.delete sql_key(sql)
158+
@statements.delete sql_key(intent.sql)
159159
end
160160
retry
161161
end
162162
end
163163

164164
raise
165165
end
166-
elsif binds.nil? || binds.empty?
167-
raw_connection.async_exec(sql)
166+
elsif intent.binds.nil? || intent.binds.empty?
167+
raw_connection.async_exec(intent.sql)
168168
else
169-
raw_connection.exec_params(sql, type_casted_binds)
169+
raw_connection.exec_params(intent.sql, intent.type_casted_binds)
170170
end
171171

172172
verified!
173173

174-
notification_payload[:affected_rows] = result.cmd_tuples
175-
notification_payload[:row_count] = result.ntuples
174+
intent.notification_payload[:affected_rows] = result.cmd_tuples
175+
intent.notification_payload[:row_count] = result.ntuples
176176
result
177177
end
178178

‎activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,23 @@ def internal_begin_transaction(mode, isolation)
8383
end
8484
end
8585

86-
def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notification_payload:, batch: false)
86+
def perform_query(raw_connection, intent)
8787
total_changes_before_query = raw_connection.total_changes
8888
affected_rows = nil
8989

90-
if batch
91-
raw_connection.execute_batch2(sql)
90+
if intent.batch
91+
raw_connection.execute_batch2(intent.sql)
9292
else
93-
stmt = if prepare
94-
@statements[sql] ||= raw_connection.prepare(sql)
95-
@statements[sql].reset!
93+
stmt = if intent.prepare
94+
@statements[intent.sql] ||= raw_connection.prepare(intent.sql)
95+
@statements[intent.sql].reset!
9696
else
9797
# Don't cache statements if they are not prepared.
98-
raw_connection.prepare(sql)
98+
raw_connection.prepare(intent.sql)
9999
end
100100
begin
101-
unless binds.nil? || binds.empty?
102-
stmt.bind_params(type_casted_binds)
101+
unless intent.binds.nil? || intent.binds.empty?
102+
stmt.bind_params(intent.type_casted_binds)
103103
end
104104
result = if stmt.column_count.zero? # No return
105105
stmt.step
@@ -123,13 +123,13 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
123123
ActiveRecord::Result.new(stmt.columns, rows, stmt.types.map { |t| type_map.lookup(t) }, affected_rows: affected_rows)
124124
end
125125
ensure
126-
stmt.close unless prepare
126+
stmt.close unless intent.prepare
127127
end
128128
end
129129
verified!
130130

131-
notification_payload[:affected_rows] = affected_rows
132-
notification_payload[:row_count] = result&.length || 0
131+
intent.notification_payload[:affected_rows] = affected_rows
132+
intent.notification_payload[:row_count] = result&.length || 0
133133
result
134134
end
135135

‎activerecord/lib/active_record/connection_adapters/trilogy/database_statements.rb‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ def exec_insert(sql, name, binds, pk = nil, sequence_name = nil, returning: nil)
1010
end
1111

1212
private
13-
def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notification_payload:, batch: false)
14-
reset_multi_statement = if batch && !@config[:multi_statement]
13+
def perform_query(raw_connection, intent)
14+
reset_multi_statement = if intent.batch && !@config[:multi_statement]
1515
raw_connection.set_server_option(::Trilogy::SET_SERVER_MULTI_STATEMENTS_ON)
1616
true
1717
end
@@ -24,14 +24,14 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
2424
raw_connection.query_flags &= ~::Trilogy::QUERY_FLAGS_LOCAL_TIMEZONE
2525
end
2626

27-
result = raw_connection.query(sql)
27+
result = raw_connection.query(intent.sql)
2828
while raw_connection.more_results_exist?
2929
raw_connection.next_result
3030
end
3131
verified!
3232

33-
notification_payload[:affected_rows] = result.affected_rows
34-
notification_payload[:row_count] = result.count
33+
intent.notification_payload[:affected_rows] = result.affected_rows
34+
intent.notification_payload[:row_count] = result.count
3535
result
3636
ensure
3737
if reset_multi_statement && active?

‎activerecord/test/cases/adapters/abstract_mysql_adapter/transaction_test.rb‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,13 @@ class Sample < ActiveRecord::Base
175175
end
176176

177177
first_begin_failed = false
178-
@connection.singleton_class.define_method(:perform_query) do |raw_connection, sql, *args, **kwargs|
178+
@connection.singleton_class.define_method(:perform_query) do |raw_connection, intent, *args, **kwargs|
179179
# Simulates the first BEGIN attempt failing
180-
if sql.include?("BEGIN") && !first_begin_failed
180+
if intent.sql.include?("BEGIN") && !first_begin_failed
181181
first_begin_failed = true
182182
raise ActiveRecord::ConnectionFailed, "Simulated failure"
183183
end
184-
super(raw_connection, sql, *args, **kwargs)
184+
super(raw_connection, intent, *args, **kwargs)
185185
end
186186

187187
Sample.transaction(isolation: :read_committed) do

0 commit comments

Comments
 (0)