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