From 724b733c454a56d8a32be54fdbdf6a0a60379642 Mon Sep 17 00:00:00 2001 From: Matt Tanous Date: Mon, 1 Feb 2021 18:30:14 -0500 Subject: [PATCH] Pass binds through on PostgreSQL exec_query even when prepared statements are off globally --- .../connection_adapters/postgresql_adapter.rb | 4 +- .../postgresql/postgresql_adapter_test.rb | 40 +++++++++---------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index ca8b886c53..ed673202cd 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -653,9 +653,7 @@ module ActiveRecord raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}" end - if without_prepared_statement?(binds) - result = exec_no_cache(sql, name, []) - elsif !prepare + if !prepare || without_prepared_statement?(binds) result = exec_no_cache(sql, name, binds) else result = exec_cache(sql, name, binds) diff --git a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb index 8446a8c003..3802198c62 100644 --- a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb +++ b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb @@ -254,35 +254,33 @@ module ActiveRecord end end - if ActiveRecord::Base.connection.prepared_statements - def test_exec_with_binds - with_example_table do - string = @connection.quote("foo") - @connection.exec_query("INSERT INTO ex (id, data) VALUES (1, #{string})") + def test_exec_with_binds + with_example_table do + string = @connection.quote("foo") + @connection.exec_query("INSERT INTO ex (id, data) VALUES (1, #{string})") - bind = Relation::QueryAttribute.new("id", 1, Type::Value.new) - result = @connection.exec_query("SELECT id, data FROM ex WHERE id = $1", nil, [bind]) + bind = Relation::QueryAttribute.new("id", 1, Type::Value.new) + result = @connection.exec_query("SELECT id, data FROM ex WHERE id = $1", nil, [bind]) - assert_equal 1, result.rows.length - assert_equal 2, result.columns.length + assert_equal 1, result.rows.length + assert_equal 2, result.columns.length - assert_equal [[1, "foo"]], result.rows - end + assert_equal [[1, "foo"]], result.rows end + end - def test_exec_typecasts_bind_vals - with_example_table do - string = @connection.quote("foo") - @connection.exec_query("INSERT INTO ex (id, data) VALUES (1, #{string})") + def test_exec_typecasts_bind_vals + with_example_table do + string = @connection.quote("foo") + @connection.exec_query("INSERT INTO ex (id, data) VALUES (1, #{string})") - bind = Relation::QueryAttribute.new("id", "1-fuu", Type::Integer.new) - result = @connection.exec_query("SELECT id, data FROM ex WHERE id = $1", nil, [bind]) + bind = Relation::QueryAttribute.new("id", "1-fuu", Type::Integer.new) + result = @connection.exec_query("SELECT id, data FROM ex WHERE id = $1", nil, [bind]) - assert_equal 1, result.rows.length - assert_equal 2, result.columns.length + assert_equal 1, result.rows.length + assert_equal 2, result.columns.length - assert_equal [[1, "foo"]], result.rows - end + assert_equal [[1, "foo"]], result.rows end end