Skip to content

Commit 19a58ea

Browse files
authored
Merge pull request rails#53745 from byroot/mysql2-prepared-extra-fixes
More Mysql2 adapter fixes to support prepared statements
2 parents 723ca0c + 1079724 commit 19a58ea

File tree

8 files changed

+36
-30
lines changed

8 files changed

+36
-30
lines changed

activerecord/lib/active_record/connection_adapters/mysql/quoting.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,13 @@ def type_cast(value) # :nodoc:
102102
else
103103
value.getlocal
104104
end
105-
when Date, Time
105+
when Time
106+
if default_timezone == :utc
107+
value.utc? ? value : value.getutc
108+
else
109+
value.utc? ? value.getlocal : value
110+
end
111+
when Date
106112
value
107113
else
108114
super

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
4848
# made since we established the connection
4949
raw_connection.query_options[:database_timezone] = default_timezone
5050

51-
result = if !prepared_statements || binds.nil? || binds.empty?
51+
result = if binds.nil? || binds.empty?
5252
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
5353
result = raw_connection.query(sql)
5454
@affected_rows_before_warnings = raw_connection.affected_rows
@@ -74,14 +74,15 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
7474
else
7575
stmt.close
7676
end
77+
7778
raise
7879
end
80+
7981
verified!
8082

8183
result
8284
end
8385

84-
8586
notification_payload[:affected_rows] = @affected_rows_before_warnings
8687
notification_payload[:row_count] = result&.size || 0
8788

activerecord/test/cases/adapters/postgresql/quoting_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ def test_quote_float_infinity
2929
assert_equal "'Infinity'", @conn.quote(infinity)
3030
end
3131

32-
def test_cast_bound_integer
32+
def test_quote_integer
3333
assert_equal "42", @conn.quote(42)
3434
end
3535

36-
def test_cast_bound_big_decimal
36+
def test_quote_big_decimal
3737
assert_equal "4.2", @conn.quote(BigDecimal("4.2"))
3838
end
3939

40-
def test_cast_bound_rational
40+
def test_quote_rational
4141
assert_equal "3/4", @conn.quote(Rational(3, 4))
4242
end
4343

activerecord/test/cases/bind_parameter_test.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,11 @@ def assert_bind_params_to_sql
238238
#
239239
# SELECT `authors`.* FROM `authors` WHERE `authors`.`id` IN (1, 2, 3)
240240
#
241-
if current_adapter?(:Mysql2Adapter)
242-
params = bind_params((1..3).map(&:to_s))
241+
params = if current_adapter?(:Mysql2Adapter, :TrilogyAdapter)
242+
# With MySQL integers are casted as string for security.
243+
bind_params((1..3).map(&:to_s))
243244
else
244-
params = bind_params(1..3)
245+
bind_params(1..3)
245246
end
246247

247248
sql = "SELECT #{table}.* FROM #{table} WHERE #{pk} IN (#{params})"

activerecord/test/cases/explain_test.rb

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,22 @@ def test_relation_explain_with_sum
8989
assert_match(expected_query, message)
9090
end
9191

92-
unless current_adapter?(:Mysql2Adapter) && ActiveRecord::Base.lease_connection.prepared_statements
93-
def test_relation_explain_with_first
94-
expected_query = capture_sql {
95-
Car.all.first
96-
}.first
97-
message = Car.all.explain.first
98-
assert_match(/^EXPLAIN/, message)
99-
assert_match(expected_query, message)
100-
end
92+
def test_relation_explain_with_first
93+
expected_query = capture_sql {
94+
Car.all.first
95+
}.first
96+
message = Car.all.explain.first
97+
assert_match(/^EXPLAIN/, message)
98+
assert_match(expected_query.sub(/LIMIT.*/, ""), message)
99+
end
101100

102-
def test_relation_explain_with_last
103-
expected_query = capture_sql {
104-
Car.all.last
105-
}.first
106-
message = Car.all.explain.last
107-
assert_match(/^EXPLAIN/, message)
108-
assert_match(expected_query, message)
109-
end
101+
def test_relation_explain_with_last
102+
expected_query = capture_sql {
103+
Car.all.last
104+
}.first
105+
message = Car.all.explain.last
106+
assert_match(/^EXPLAIN/, message)
107+
assert_match(expected_query.sub(/LIMIT.*/, ""), message)
110108
end
111109

112110
def test_relation_explain_with_pluck

activerecord/test/cases/finder_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ def test_condition_utc_time_interpolation_with_default_timezone_local
14061406
with_env_tz "America/New_York" do
14071407
with_timezone_config default: :local do
14081408
topic = Topic.first
1409-
assert_equal topic, Topic.where(["written_on = ?", topic.written_on]).first
1409+
assert_equal topic, Topic.where(["written_on = ?", topic.written_on.getutc]).first
14101410
end
14111411
end
14121412
end
@@ -1424,7 +1424,7 @@ def test_condition_local_time_interpolation_with_default_timezone_utc
14241424
with_env_tz "America/New_York" do
14251425
with_timezone_config default: :utc do
14261426
topic = Topic.first
1427-
assert_equal topic, Topic.where(["written_on = ?", topic.written_on]).first
1427+
assert_equal topic, Topic.where(["written_on = ?", topic.written_on.getlocal]).first
14281428
end
14291429
end
14301430
end

activerecord/test/cases/insert_all_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ def test_upsert_all_updates_using_provided_sql
787787
assert_equal "written", Book.find(2).status
788788
end
789789

790-
if current_adapter?(:Mysql2Adapter) || current_adapter?(:TrilogyAdapter)
790+
if current_adapter?(:Mysql2Adapter, :TrilogyAdapter)
791791
def test_upsert_all_updates_using_values_function_on_duplicate_raw_sql
792792
skip unless supports_insert_on_duplicate_update?
793793

activerecord/test/cases/test_case.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def self.run(*args)
308308

309309
class AbstractMysqlTestCase < TestCase
310310
def self.run(*args)
311-
super if current_adapter?(:Mysql2Adapter) || current_adapter?(:TrilogyAdapter)
311+
super if current_adapter?(:Mysql2Adapter, :TrilogyAdapter)
312312
end
313313
end
314314

0 commit comments

Comments
 (0)