Allow statements to auto close when consumed if no cache (#198)

This commit is contained in:
Brian J. Cardiff 2023-11-29 18:39:44 -03:00 committed by GitHub
parent 76d8bb6a6e
commit d3dd978e24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 100 additions and 4 deletions

View file

@ -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

View file

@ -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,31 @@ 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
it "should not close statements if false and created explicitly" do
with_dummy_connection("prepared_statements=true&prepared_statements_cache=false") do |cnn|
stmt = cnn.prepared("the query")
rs = stmt.query
# do not close while iterating
stmt.closed?.should be_false
rs.close
# do not close after iterating
stmt.closed?.should be_false
end
end
end
it "should initialize positional params in query" do