Skip to content

Commit

Permalink
Merge pull request rails#53745 from byroot/mysql2-prepared-extra-fixes
Browse files Browse the repository at this point in the history
More Mysql2 adapter fixes to support prepared statements
  • Loading branch information
byroot authored Nov 26, 2024
2 parents 723ca0c + 1079724 commit 19a58ea
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ def type_cast(value) # :nodoc:
else
value.getlocal
end
when Date, Time
when Time
if default_timezone == :utc
value.utc? ? value : value.getutc
else
value.utc? ? value.getlocal : value
end
when Date
value
else
super
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
# made since we established the connection
raw_connection.query_options[:database_timezone] = default_timezone

result = if !prepared_statements || binds.nil? || binds.empty?
result = if binds.nil? || binds.empty?
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
result = raw_connection.query(sql)
@affected_rows_before_warnings = raw_connection.affected_rows
Expand All @@ -74,14 +74,15 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
else
stmt.close
end

raise
end

verified!

result
end


notification_payload[:affected_rows] = @affected_rows_before_warnings
notification_payload[:row_count] = result&.size || 0

Expand Down
6 changes: 3 additions & 3 deletions activerecord/test/cases/adapters/postgresql/quoting_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ def test_quote_float_infinity
assert_equal "'Infinity'", @conn.quote(infinity)
end

def test_cast_bound_integer
def test_quote_integer
assert_equal "42", @conn.quote(42)
end

def test_cast_bound_big_decimal
def test_quote_big_decimal
assert_equal "4.2", @conn.quote(BigDecimal("4.2"))
end

def test_cast_bound_rational
def test_quote_rational
assert_equal "3/4", @conn.quote(Rational(3, 4))
end

Expand Down
7 changes: 4 additions & 3 deletions activerecord/test/cases/bind_parameter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,11 @@ def assert_bind_params_to_sql
#
# SELECT `authors`.* FROM `authors` WHERE `authors`.`id` IN (1, 2, 3)
#
if current_adapter?(:Mysql2Adapter)
params = bind_params((1..3).map(&:to_s))
params = if current_adapter?(:Mysql2Adapter, :TrilogyAdapter)
# With MySQL integers are casted as string for security.
bind_params((1..3).map(&:to_s))
else
params = bind_params(1..3)
bind_params(1..3)
end

sql = "SELECT #{table}.* FROM #{table} WHERE #{pk} IN (#{params})"
Expand Down
32 changes: 15 additions & 17 deletions activerecord/test/cases/explain_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,22 @@ def test_relation_explain_with_sum
assert_match(expected_query, message)
end

unless current_adapter?(:Mysql2Adapter) && ActiveRecord::Base.lease_connection.prepared_statements
def test_relation_explain_with_first
expected_query = capture_sql {
Car.all.first
}.first
message = Car.all.explain.first
assert_match(/^EXPLAIN/, message)
assert_match(expected_query, message)
end
def test_relation_explain_with_first
expected_query = capture_sql {
Car.all.first
}.first
message = Car.all.explain.first
assert_match(/^EXPLAIN/, message)
assert_match(expected_query.sub(/LIMIT.*/, ""), message)
end

def test_relation_explain_with_last
expected_query = capture_sql {
Car.all.last
}.first
message = Car.all.explain.last
assert_match(/^EXPLAIN/, message)
assert_match(expected_query, message)
end
def test_relation_explain_with_last
expected_query = capture_sql {
Car.all.last
}.first
message = Car.all.explain.last
assert_match(/^EXPLAIN/, message)
assert_match(expected_query.sub(/LIMIT.*/, ""), message)
end

def test_relation_explain_with_pluck
Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/cases/finder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1406,7 +1406,7 @@ def test_condition_utc_time_interpolation_with_default_timezone_local
with_env_tz "America/New_York" do
with_timezone_config default: :local do
topic = Topic.first
assert_equal topic, Topic.where(["written_on = ?", topic.written_on]).first
assert_equal topic, Topic.where(["written_on = ?", topic.written_on.getutc]).first
end
end
end
Expand All @@ -1424,7 +1424,7 @@ def test_condition_local_time_interpolation_with_default_timezone_utc
with_env_tz "America/New_York" do
with_timezone_config default: :utc do
topic = Topic.first
assert_equal topic, Topic.where(["written_on = ?", topic.written_on]).first
assert_equal topic, Topic.where(["written_on = ?", topic.written_on.getlocal]).first
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/insert_all_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ def test_upsert_all_updates_using_provided_sql
assert_equal "written", Book.find(2).status
end

if current_adapter?(:Mysql2Adapter) || current_adapter?(:TrilogyAdapter)
if current_adapter?(:Mysql2Adapter, :TrilogyAdapter)
def test_upsert_all_updates_using_values_function_on_duplicate_raw_sql
skip unless supports_insert_on_duplicate_update?

Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def self.run(*args)

class AbstractMysqlTestCase < TestCase
def self.run(*args)
super if current_adapter?(:Mysql2Adapter) || current_adapter?(:TrilogyAdapter)
super if current_adapter?(:Mysql2Adapter, :TrilogyAdapter)
end
end

Expand Down

0 comments on commit 19a58ea

Please sign in to comment.