diff --git a/spec/database_spec.cr b/spec/database_spec.cr index a9ccef8..74373af 100644 --- a/spec/database_spec.cr +++ b/spec/database_spec.cr @@ -57,14 +57,6 @@ describe DB::Database do end end - it "should close pool statements when closing db" do - stmt = uninitialized DB::PoolStatement - with_dummy do |db| - stmt = db.build("query1") - end - stmt.closed?.should be_true - end - it "should not reconnect if connection is lost and retry_attempts=0" do DummyDriver::DummyConnection.clear_connections DB.open "dummy://localhost:1027?initial_pool_size=1&max_pool_size=1&retry_attempts=0" do |db| @@ -239,24 +231,6 @@ 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 79741d5..16e1e7b 100644 --- a/src/db/database.cr +++ b/src/db/database.cr @@ -38,7 +38,6 @@ module DB @connection_options : Connection::Options @pool : Pool(Connection) @setup_connection : Connection -> Nil - @statements_cache = StringKeyCache(PoolPreparedStatement).new # Initialize a database with the specified options and connection factory. # This covers more advanced use cases that might not be supported by an URI connection string such as tunneling connection. @@ -81,9 +80,6 @@ module DB # Closes all connection to the database. def close - @statements_cache.each_value &.close - @statements_cache.clear - @pool.close end @@ -99,15 +95,6 @@ module DB # :nodoc: def fetch_or_build_prepared_statement(query) : PoolStatement - if @connection_options.prepared_statements_cache - @statements_cache.fetch(query) { build_prepared_statement(query) } - else - build_prepared_statement(query) - end - end - - # :nodoc: - def build_prepared_statement(query) : PoolStatement PoolPreparedStatement.new(self, query) end diff --git a/src/db/pool_prepared_statement.cr b/src/db/pool_prepared_statement.cr index 164ae75..41564cd 100644 --- a/src/db/pool_prepared_statement.cr +++ b/src/db/pool_prepared_statement.cr @@ -4,7 +4,7 @@ module DB # The execution of the statement is retried according to the pool configuration. # # See `PoolStatement` - class PoolPreparedStatement < PoolStatement + struct PoolPreparedStatement < PoolStatement def initialize(db : Database, query : String) super end diff --git a/src/db/pool_statement.cr b/src/db/pool_statement.cr index f0fc2b2..20a8214 100644 --- a/src/db/pool_statement.cr +++ b/src/db/pool_statement.cr @@ -3,7 +3,7 @@ module DB # a statement from the DB needs to be able to represent a statement in any # of the connections of the pool. Otherwise the user will need to deal with # actual connections in some point. - abstract class PoolStatement + abstract struct PoolStatement include StatementMethods def initialize(@db : Database, @query : String) diff --git a/src/db/pool_unprepared_statement.cr b/src/db/pool_unprepared_statement.cr index c58fafd..7ba42e7 100644 --- a/src/db/pool_unprepared_statement.cr +++ b/src/db/pool_unprepared_statement.cr @@ -4,7 +4,7 @@ module DB # The execution of the statement is retried according to the pool configuration. # # See `PoolStatement` - class PoolUnpreparedStatement < PoolStatement + struct PoolUnpreparedStatement < PoolStatement def initialize(db : Database, query : String) super end diff --git a/src/db/statement.cr b/src/db/statement.cr index 6602e6d..b3b32f6 100644 --- a/src/db/statement.cr +++ b/src/db/statement.cr @@ -2,11 +2,6 @@ module DB # Common interface for connection based statements # and for connection pool statements. module StatementMethods - include Disposable - - protected def do_close - end - # See `QueryMethods#scalar` def scalar(*args_, args : Array? = nil) query(*args_, args: args) do |rs| @@ -47,6 +42,10 @@ module DB # 6. `#do_close` is called to release the statement resources. abstract class Statement include StatementMethods + include Disposable + + protected def do_close + end # :nodoc: getter connection