From ba2dcd4fe8fbfec9c02cd03130ab7a9b57c7e6a2 Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Sun, 19 Nov 2023 17:39:52 -0300 Subject: [PATCH] Add specs for prepared statements life cycle --- spec/dummy_driver.cr | 20 ++++++++++++++++++++ spec/statement_spec.cr | 22 ++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/spec/dummy_driver.cr b/spec/dummy_driver.cr index 9fe64dc..da6417c 100644 --- a/spec/dummy_driver.cr +++ b/spec/dummy_driver.cr @@ -34,14 +34,20 @@ class DummyDriver < DB::Driver end def build_prepared_statement(query) : DB::Statement + assert_not_closed! + DummyStatement.new(self, query, true) end def build_unprepared_statement(query) : DB::Statement + assert_not_closed! + DummyStatement.new(self, query, false) end def last_insert_id : Int64 + assert_not_closed! + 0 end @@ -54,12 +60,18 @@ class DummyDriver < DB::Driver end def create_transaction + assert_not_closed! + DummyTransaction.new(self) end protected def do_close super end + + private def assert_not_closed! + raise "Statement is closed" if closed? + end end class DummyTransaction < DB::TopLevelTransaction @@ -114,6 +126,8 @@ class DummyDriver < DB::Driver end protected def perform_query(args : Enumerable) : DB::ResultSet + assert_not_closed! + Fiber.yield @connection.as(DummyConnection).check set_params args @@ -121,6 +135,8 @@ class DummyDriver < DB::Driver end protected def perform_exec(args : Enumerable) : DB::ExecResult + assert_not_closed! + @connection.as(DummyConnection).check set_params args raise DB::Error.new("forced exception due to query") if command == "raise" @@ -153,6 +169,10 @@ class DummyDriver < DB::Driver protected def do_close super end + + private def assert_not_closed! + raise "Statement is closed" if closed? + end end class DummyResultSet < DB::ResultSet diff --git a/spec/statement_spec.cr b/spec/statement_spec.cr index 1444862..a8c86f4 100644 --- a/spec/statement_spec.cr +++ b/spec/statement_spec.cr @@ -43,6 +43,17 @@ describe DB::Statement do end end + it "should leave statements open to be reused if true" do + with_dummy_connection("prepared_statements=true&prepared_statements_cache=true") do |cnn| + rs = cnn.query("the query") + # do not close while iterating + rs.statement.closed?.should be_false + rs.close + # do not close to be reused + rs.statement.closed?.should be_false + end + end + it "should not reuse prepared statements if false" do with_dummy_connection("prepared_statements=true&prepared_statements_cache=false") do |cnn| stmt1 = cnn.query("the query").statement @@ -50,6 +61,17 @@ describe DB::Statement do stmt1.object_id.should_not eq(stmt2.object_id) end end + + it "should close statements if false" do + with_dummy_connection("prepared_statements=true&prepared_statements_cache=false") do |cnn| + rs = cnn.query("the query") + # do not close while iterating + rs.statement.closed?.should be_false + rs.close + # do close after iterating + rs.statement.closed?.should be_true + end + end end it "should initialize positional params in query" do