From 3aaccec20670274fc189c2181238d7d5077aa1b2 Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Thu, 2 Nov 2023 22:28:50 -0300 Subject: [PATCH] Honor prepared_statements_cache option in database also --- spec/database_spec.cr | 18 ++++++++++++++++++ src/db/database.cr | 6 +++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/spec/database_spec.cr b/spec/database_spec.cr index 8371d57..a9ccef8 100644 --- a/spec/database_spec.cr +++ b/spec/database_spec.cr @@ -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| diff --git a/src/db/database.cr b/src/db/database.cr index 221187e..47531a3 100644 --- a/src/db/database.cr +++ b/src/db/database.cr @@ -95,7 +95,11 @@ module DB # :nodoc: def fetch_or_build_prepared_statement(query) : PoolStatement - @statements_cache.fetch(query) { build_prepared_statement(query) } + if @connection_options.prepared_statements_cache + @statements_cache.fetch(query) { build_prepared_statement(query) } + else + build_prepared_statement(query) + end end # :nodoc: