Migrate to simpler/decoupled factory in driver

This allows more freedom on how the connection is created. It will no longer need to have an explicit reference to the connection URI
This commit is contained in:
Brian J. Cardiff 2023-05-27 22:51:38 -03:00
parent 3828e17a40
commit 0328767b98
5 changed files with 14 additions and 9 deletions

View file

@ -47,8 +47,8 @@ class FooDriver < DB::Driver
@@row @@row
end end
def build_connection(context : DB::ConnectionContext) : DB::Connection def connection_builder(uri : URI) : Proc(DB::Connection)
FooConnection.new -> { FooConnection.new.as(DB::Connection) }
end end
class FooConnection < DB::Connection class FooConnection < DB::Connection
@ -110,8 +110,8 @@ class BarDriver < DB::Driver
@@row @@row
end end
def build_connection(context : DB::ConnectionContext) : DB::Connection def connection_builder(uri : URI) : Proc(DB::Connection)
BarConnection.new -> { BarConnection.new.as(DB::Connection) }
end end
class BarConnection < DB::Connection class BarConnection < DB::Connection

View file

@ -2,8 +2,8 @@ require "spec"
require "../src/db" require "../src/db"
class DummyDriver < DB::Driver class DummyDriver < DB::Driver
def build_connection(context : DB::ConnectionContext) : DB::Connection def connection_builder(uri : URI) : Proc(DB::Connection)
DummyConnection.new -> { DummyConnection.new.as(DB::Connection) }
end end
class DummyConnection < DB::Connection class DummyConnection < DB::Connection

View file

@ -161,7 +161,7 @@ module DB
private def self.build_connection(uri : URI) private def self.build_connection(uri : URI)
# PENDING: parse connection options from uri and set the right connection context # PENDING: parse connection options from uri and set the right connection context
build_driver(uri).build_connection(SingleConnectionContext.default).as(Connection) build_driver(uri).connection_builder(uri).call
end end
private def self.build_driver(uri : URI) private def self.build_driver(uri : URI)

View file

@ -52,9 +52,10 @@ module DB
pool_options = @driver.connection_pool_options(params) pool_options = @driver.connection_pool_options(params)
@setup_connection = ->(conn : Connection) {} @setup_connection = ->(conn : Connection) {}
factory = @driver.connection_builder(@uri)
@pool = uninitialized Pool(Connection) # in order to use self in the factory proc @pool = uninitialized Pool(Connection) # in order to use self in the factory proc
@pool = Pool.new(**pool_options) { @pool = Pool.new(**pool_options) {
conn = @driver.build_connection(self).as(Connection) conn = factory.call
conn.auto_release = false conn.auto_release = false
conn.context = self conn.context = self
@setup_connection.call conn @setup_connection.call conn

View file

@ -26,7 +26,11 @@ module DB
# Refer to `Connection`, `Statement` and `ResultSet` for further # Refer to `Connection`, `Statement` and `ResultSet` for further
# driver implementation instructions. # driver implementation instructions.
abstract class Driver abstract class Driver
abstract def build_connection(context : ConnectionContext) : Connection # Returns a new connection factory.
#
# NOTE: For implementors *uri* should be parsed once. If all the options
# are sound a factory Proc is returned.
abstract def connection_builder(uri : URI) : Proc(Connection)
def connection_pool_options(params : HTTP::Params) def connection_pool_options(params : HTTP::Params)
{ {