Skip to content

Commit

Permalink
Merge pull request #41300 from CodingAnarchy/pg-adapter-fix
Browse files Browse the repository at this point in the history
Pass binds on PostgreSQL exec_query even when prepared statements off globally
  • Loading branch information
rafaelfranca committed Feb 2, 2021
1 parent 42ad010 commit f5a87fe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -649,9 +649,7 @@ def execute_and_clear(sql, name, binds, prepare: false)
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,35 +254,33 @@ def test_exec_no_binds
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

Expand Down

0 comments on commit f5a87fe

Please sign in to comment.