diff --git a/spec/custom_drivers_types_spec.cr b/spec/custom_drivers_types_spec.cr index 563352f..bb1eaf8 100644 --- a/spec/custom_drivers_types_spec.cr +++ b/spec/custom_drivers_types_spec.cr @@ -47,8 +47,8 @@ class FooDriver < DB::Driver @@row end - def build_connection(context : DB::ConnectionContext) : DB::Connection - FooConnection.new + def connection_builder(uri : URI) : Proc(DB::Connection) + -> { FooConnection.new.as(DB::Connection) } end class FooConnection < DB::Connection @@ -110,8 +110,8 @@ class BarDriver < DB::Driver @@row end - def build_connection(context : DB::ConnectionContext) : DB::Connection - BarConnection.new + def connection_builder(uri : URI) : Proc(DB::Connection) + -> { BarConnection.new.as(DB::Connection) } end class BarConnection < DB::Connection diff --git a/spec/dummy_driver.cr b/spec/dummy_driver.cr index f72a511..0d44ce9 100644 --- a/spec/dummy_driver.cr +++ b/spec/dummy_driver.cr @@ -2,8 +2,8 @@ require "spec" require "../src/db" class DummyDriver < DB::Driver - def build_connection(context : DB::ConnectionContext) : DB::Connection - DummyConnection.new + def connection_builder(uri : URI) : Proc(DB::Connection) + -> { DummyConnection.new.as(DB::Connection) } end class DummyConnection < DB::Connection diff --git a/src/db.cr b/src/db.cr index 92e9b99..2d8274d 100644 --- a/src/db.cr +++ b/src/db.cr @@ -161,7 +161,7 @@ module DB private def self.build_connection(uri : URI) # 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 private def self.build_driver(uri : URI) diff --git a/src/db/database.cr b/src/db/database.cr index b6a5cc0..88fe820 100644 --- a/src/db/database.cr +++ b/src/db/database.cr @@ -52,9 +52,10 @@ module DB pool_options = @driver.connection_pool_options(params) @setup_connection = ->(conn : Connection) {} + factory = @driver.connection_builder(@uri) @pool = uninitialized Pool(Connection) # in order to use self in the factory proc @pool = Pool.new(**pool_options) { - conn = @driver.build_connection(self).as(Connection) + conn = factory.call conn.auto_release = false conn.context = self @setup_connection.call conn diff --git a/src/db/driver.cr b/src/db/driver.cr index 5df46a2..2a998d7 100644 --- a/src/db/driver.cr +++ b/src/db/driver.cr @@ -26,7 +26,11 @@ module DB # Refer to `Connection`, `Statement` and `ResultSet` for further # driver implementation instructions. 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) {