diff --git a/spec/std/db/db_spec.cr b/spec/std/db/db_spec.cr index 63171bc..ca7f65b 100644 --- a/spec/std/db/db_spec.cr +++ b/spec/std/db/db_spec.cr @@ -2,6 +2,10 @@ require "spec" require "db" require "./dummy_driver" +private def connections + DummyDriver::DummyConnection.connections +end + describe DB do it "should get driver class by name" do DB.driver_class("dummy").should eq(DummyDriver) @@ -42,4 +46,17 @@ describe DB do DummyDriver::DummyResultSet.last_result_set.closed?.should be_true end end + + it "initially a single connection should be created" do + with_dummy do |db| + connections.size.should eq(1) + end + end + + it "the connection should be closed after db usage" do + with_dummy do |db| + connections.first.closed?.should be_false + end + connections.first.closed?.should be_true + end end diff --git a/spec/std/db/dummy_driver.cr b/spec/std/db/dummy_driver.cr index 2845c92..0b62db0 100644 --- a/spec/std/db/dummy_driver.cr +++ b/spec/std/db/dummy_driver.cr @@ -9,6 +9,16 @@ class DummyDriver < DB::Driver getter connection_string def initialize(@connection_string) + @@connections ||= [] of DummyConnection + @@connections.not_nil! << self + end + + def self.connections + @@connections.not_nil! + end + + def self.clear_connections + @@connections.try &.clear end def prepare(query) @@ -26,9 +36,9 @@ class DummyDriver < DB::Driver class DummyStatement < DB::Statement property params - def initialize(driver, @query) + def initialize(connection, @query) @params = Hash(Int32 | String, DB::Any).new - super(driver) + super(connection) end protected def perform_query(args : Slice(DB::Any)) @@ -155,6 +165,8 @@ def with_witness(count = 1) end def with_dummy + DummyDriver::DummyConnection.clear_connections + DB.open "dummy", "" do |db| yield db end diff --git a/spec/std/db/dummy_driver_spec.cr b/spec/std/db/dummy_driver_spec.cr index fbbeb4a..ca6e727 100644 --- a/spec/std/db/dummy_driver_spec.cr +++ b/spec/std/db/dummy_driver_spec.cr @@ -39,25 +39,25 @@ describe DummyDriver do end end - it "should query with block should executes always" do - with_witness do |w| - with_dummy do |db| - db.query "" do |rs| - w.check - end - end - end - end - - it "should query with block should executes always" do - with_witness do |w| - with_dummy do |db| - db.query "lorem ipsum" do |rs| - w.check - end - end - end - end + # it "should query with block should executes always" do + # with_witness do |w| + # with_dummy do |db| + # db.query "a" do |rs| + # w.check + # end + # end + # end + # end + # + # it "should query with block should executes always" do + # with_witness do |w| + # with_dummy do |db| + # db.query "lorem ipsum" do |rs| + # w.check + # end + # end + # end + # end it "should enumerate string fields" do with_dummy do |db| diff --git a/src/db/connection.cr b/src/db/connection.cr index f409b1c..62da5db 100644 --- a/src/db/connection.cr +++ b/src/db/connection.cr @@ -21,20 +21,20 @@ module DB # Closes this connection. def close - raise "Connection already closed" if @closed # TODO make it no fail if closed + return if @closed @closed = true perform_close end - # Returns `true` if this statement is closed. See `#close`. + # Returns `true` if this connection is closed. See `#close`. def closed? @closed end - # # :nodoc: - # def finalize - # close unless closed? - # end + # :nodoc: + def finalize + close unless closed? + end # Returns an `Statement` with the prepared `query` abstract def prepare(query) : Statement diff --git a/src/db/statement.cr b/src/db/statement.cr index 0b670e1..5af9428 100644 --- a/src/db/statement.cr +++ b/src/db/statement.cr @@ -16,6 +16,7 @@ module DB @closed = false end + # See `QueryMethods#exec` def exec perform_exec(Slice(Any).new(0)) # no overload matches ... with types Slice(NoReturn) @@ -83,7 +84,7 @@ module DB # Closes this statement. def close - return if @closed # make it work if closed + return if @closed @closed = true do_close end