Make pool statement a struct

This commit is contained in:
Brian J. Cardiff 2023-12-01 23:51:01 -03:00
parent cd107ca635
commit aea76a6392
6 changed files with 7 additions and 47 deletions

View file

@ -57,14 +57,6 @@ describe DB::Database do
end end
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 it "should not reconnect if connection is lost and retry_attempts=0" do
DummyDriver::DummyConnection.clear_connections DummyDriver::DummyConnection.clear_connections
DB.open "dummy://localhost:1027?initial_pool_size=1&max_pool_size=1&retry_attempts=0" do |db| 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
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 describe "unprepared statements in pool" do
it "creating statements should not create new connections" do it "creating statements should not create new connections" do
with_dummy "dummy://localhost:1027?initial_pool_size=1" do |db| with_dummy "dummy://localhost:1027?initial_pool_size=1" do |db|

View file

@ -38,7 +38,6 @@ module DB
@connection_options : Connection::Options @connection_options : Connection::Options
@pool : Pool(Connection) @pool : Pool(Connection)
@setup_connection : Connection -> Nil @setup_connection : Connection -> Nil
@statements_cache = StringKeyCache(PoolPreparedStatement).new
# Initialize a database with the specified options and connection factory. # 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. # 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. # Closes all connection to the database.
def close def close
@statements_cache.each_value &.close
@statements_cache.clear
@pool.close @pool.close
end end
@ -99,15 +95,6 @@ module DB
# :nodoc: # :nodoc:
def fetch_or_build_prepared_statement(query) : PoolStatement 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) PoolPreparedStatement.new(self, query)
end end

View file

@ -4,7 +4,7 @@ module DB
# The execution of the statement is retried according to the pool configuration. # The execution of the statement is retried according to the pool configuration.
# #
# See `PoolStatement` # See `PoolStatement`
class PoolPreparedStatement < PoolStatement struct PoolPreparedStatement < PoolStatement
def initialize(db : Database, query : String) def initialize(db : Database, query : String)
super super
end end

View file

@ -3,7 +3,7 @@ module DB
# a statement from the DB needs to be able to represent a statement in any # 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 # of the connections of the pool. Otherwise the user will need to deal with
# actual connections in some point. # actual connections in some point.
abstract class PoolStatement abstract struct PoolStatement
include StatementMethods include StatementMethods
def initialize(@db : Database, @query : String) def initialize(@db : Database, @query : String)

View file

@ -4,7 +4,7 @@ module DB
# The execution of the statement is retried according to the pool configuration. # The execution of the statement is retried according to the pool configuration.
# #
# See `PoolStatement` # See `PoolStatement`
class PoolUnpreparedStatement < PoolStatement struct PoolUnpreparedStatement < PoolStatement
def initialize(db : Database, query : String) def initialize(db : Database, query : String)
super super
end end

View file

@ -2,11 +2,6 @@ module DB
# Common interface for connection based statements # Common interface for connection based statements
# and for connection pool statements. # and for connection pool statements.
module StatementMethods module StatementMethods
include Disposable
protected def do_close
end
# See `QueryMethods#scalar` # See `QueryMethods#scalar`
def scalar(*args_, args : Array? = nil) def scalar(*args_, args : Array? = nil)
query(*args_, args: args) do |rs| query(*args_, args: args) do |rs|
@ -47,6 +42,10 @@ module DB
# 6. `#do_close` is called to release the statement resources. # 6. `#do_close` is called to release the statement resources.
abstract class Statement abstract class Statement
include StatementMethods include StatementMethods
include Disposable
protected def do_close
end
# :nodoc: # :nodoc:
getter connection getter connection