From 20e155846f8dc0d132fc57e69abefa7be7cf386f Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Tue, 21 Nov 2023 22:51:36 -0300 Subject: [PATCH] Fix offending spec --- src/db/connection.cr | 8 +++++--- src/db/session_methods.cr | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/db/connection.cr b/src/db/connection.cr index 0f68f75..1bbb508 100644 --- a/src/db/connection.cr +++ b/src/db/connection.cr @@ -50,14 +50,16 @@ module DB @options.prepared_statements end + def prepared_statements_cache? : Bool + @options.prepared_statements_cache + end + # :nodoc: def fetch_or_build_prepared_statement(query) : Statement if @options.prepared_statements_cache @statements_cache.fetch(query) { build_prepared_statement(query) } else - stmt = build_prepared_statement(query) - stmt.auto_close = true - stmt + build_prepared_statement(query) end end diff --git a/src/db/session_methods.cr b/src/db/session_methods.cr index 1e56639..290d922 100644 --- a/src/db/session_methods.cr +++ b/src/db/session_methods.cr @@ -14,13 +14,27 @@ module DB # be prepared or not. abstract def prepared_statements? : Bool + abstract def prepared_statements_cache? : Bool + abstract def fetch_or_build_prepared_statement(query) : Stmt abstract def build_unprepared_statement(query) : Stmt def build(query) : Stmt if prepared_statements? - fetch_or_build_prepared_statement(query) + stmt = fetch_or_build_prepared_statement(query) + + # #build is a :nodoc: method used on QueryMethods where + # the statements are not exposed. As such if the cache + # is disabled we should auto_close the statement. + # When the statements are build explicitly the #prepared + # and #unprepared methods are used. In that case the + # statement is closed by the user explicitly also. + if !prepared_statements_cache? + stmt.auto_close = true if stmt.responds_to?(:auto_close=) + end + + stmt else build_unprepared_statement(query) end