Allow prepared_statements_cache=false option to disable prepared statements cache (#194)

* Add prepared_statements_cache in connection to opt-out

* Honor prepared_statements_cache option in database also
This commit is contained in:
Brian J. Cardiff 2023-11-03 23:04:14 -03:00 committed by GitHub
parent 9b52a65752
commit 285e865e3a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 4 deletions

View file

@ -239,6 +239,24 @@ describe DB::Database do
end
end
describe "prepared_statements_cache connection option" do
it "should reuse prepared statements if true" do
with_dummy "dummy://localhost:1027?prepared_statements=true&prepared_statements_cache=true" do |db|
stmt1 = db.build("the query")
stmt2 = db.build("the query")
stmt1.object_id.should eq(stmt2.object_id)
end
end
it "should not reuse prepared statements if false" do
with_dummy "dummy://localhost:1027?prepared_statements=true&prepared_statements_cache=false" do |db|
stmt1 = db.build("the query")
stmt2 = db.build("the query")
stmt1.object_id.should_not eq(stmt2.object_id)
end
end
end
describe "unprepared statements in pool" do
it "creating statements should not create new connections" do
with_dummy "dummy://localhost:1027?initial_pool_size=1" do |db|

View file

@ -34,6 +34,24 @@ describe DB::Statement do
end
end
describe "prepared_statements_cache flag" do
it "should reuse prepared statements if true" do
with_dummy_connection("prepared_statements=true&prepared_statements_cache=true") do |cnn|
stmt1 = cnn.query("the query").statement
stmt2 = cnn.query("the query").statement
stmt1.object_id.should eq(stmt2.object_id)
end
end
it "should not reuse prepared statements if false" do
with_dummy_connection("prepared_statements=true&prepared_statements_cache=false") do |cnn|
stmt1 = cnn.query("the query").statement
stmt2 = cnn.query("the query").statement
stmt1.object_id.should_not eq(stmt2.object_id)
end
end
end
it "should initialize positional params in query" do
with_dummy_connection do |cnn|
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)