Start moving out URI from ConnectionContext

Create connections with an initial context. Database will set itself as context after connection has been created
This commit is contained in:
Brian J. Cardiff 2023-05-27 22:42:51 -03:00
parent da7494b5ba
commit 3828e17a40
6 changed files with 15 additions and 19 deletions

View file

@ -48,7 +48,7 @@ class FooDriver < DB::Driver
end
def build_connection(context : DB::ConnectionContext) : DB::Connection
FooConnection.new(context)
FooConnection.new
end
class FooConnection < DB::Connection
@ -111,7 +111,7 @@ class BarDriver < DB::Driver
end
def build_connection(context : DB::ConnectionContext) : DB::Connection
BarConnection.new(context)
BarConnection.new
end
class BarConnection < DB::Connection

View file

@ -3,12 +3,11 @@ require "../src/db"
class DummyDriver < DB::Driver
def build_connection(context : DB::ConnectionContext) : DB::Connection
DummyConnection.new(context)
DummyConnection.new
end
class DummyConnection < DB::Connection
def initialize(context)
super(context)
def initialize
@connected = true
@@connections ||= [] of DummyConnection
@@connections.not_nil! << self

View file

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

View file

@ -24,15 +24,14 @@ module DB
include BeginTransaction
# :nodoc:
getter context
property context : ConnectionContext = SingleConnectionContext.default
@statements_cache = StringKeyCache(Statement).new
@transaction = false
getter? prepared_statements : Bool
# :nodoc:
property auto_release : Bool = true
def initialize(@context : ConnectionContext)
@prepared_statements = @context.prepared_statements?
def prepared_statements? : Bool
context.prepared_statements?
end
# :nodoc:
@ -59,7 +58,7 @@ module DB
protected def do_close
@statements_cache.each_value &.close
@statements_cache.clear
@context.discard self
context.discard self
end
# :nodoc:
@ -75,7 +74,7 @@ module DB
# managed by the database. Should be used
# only if the connection was obtained by `Database#checkout`.
def release
@context.release(self)
context.release(self)
end
# :nodoc:

View file

@ -1,8 +1,5 @@
module DB
module ConnectionContext
# Returns the uri with the connection settings to the database
abstract def uri : URI
# Return whether the statements should be prepared by default
abstract def prepared_statements? : Bool
@ -19,12 +16,11 @@ module DB
class SingleConnectionContext
include ConnectionContext
getter uri : URI
class_getter default : SingleConnectionContext = SingleConnectionContext.new(true)
getter? prepared_statements : Bool
def initialize(@uri : URI)
params = HTTP::Params.parse(uri.query || "")
@prepared_statements = DB.fetch_bool(params, "prepared_statements", true)
def initialize(@prepared_statements : Bool)
end
def discard(connection : Connection)

View file

@ -56,6 +56,7 @@ module DB
@pool = Pool.new(**pool_options) {
conn = @driver.build_connection(self).as(Connection)
conn.auto_release = false
conn.context = self
@setup_connection.call conn
conn
}