Skip to content

Commit 2306c10

Browse files
committed
Fix PostgresqlAdapter when prepared statements are disabled
Fix: rails#52428 (comment) Also get rid of `without_prepared_statements?`.
1 parent bd63d39 commit 2306c10

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,10 +1145,6 @@ def translate_exception(exception, message:, sql:, binds:)
11451145
end
11461146
end
11471147

1148-
def without_prepared_statement?(binds)
1149-
!prepared_statements || binds.nil? || binds.empty?
1150-
end
1151-
11521148
def column_for(table_name, column_name)
11531149
column_name = column_name.to_s
11541150
columns(table_name).detect { |c| c.name == column_name } ||

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
156156

157157
raise
158158
end
159-
elsif without_prepared_statement?(binds)
159+
elsif binds.nil? || binds.empty?
160160
raw_connection.async_exec(sql)
161161
else
162162
raw_connection.exec_params(sql, type_casted_binds)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
9393
# Don't cache statements if they are not prepared.
9494
stmt = raw_connection.prepare(sql)
9595
begin
96-
unless without_prepared_statement?(binds)
96+
unless binds.nil? || binds.empty?
9797
stmt.bind_params(type_casted_binds)
9898
end
9999
result = if stmt.column_count.zero? # No return

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ def test_statement_key_is_logged
141141
end
142142
end
143143

144+
def test_prepare_false_with_binds
145+
@connection.stub(:prepared_statements, false) do
146+
bind = Relation::QueryAttribute.new(nil, 42, Type::Value.new)
147+
result = @connection.exec_query("SELECT $1::integer", "SQL", [bind], prepare: false)
148+
assert_equal [[42]], result.rows
149+
end
150+
end
151+
144152
def test_reconnection_after_actual_disconnection_with_verify
145153
assert_predicate @connection, :active?
146154
cause_server_side_disconnect

0 commit comments

Comments
 (0)