mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
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:
parent
da7494b5ba
commit
3828e17a40
6 changed files with 15 additions and 19 deletions
|
@ -48,7 +48,7 @@ class FooDriver < DB::Driver
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_connection(context : DB::ConnectionContext) : DB::Connection
|
def build_connection(context : DB::ConnectionContext) : DB::Connection
|
||||||
FooConnection.new(context)
|
FooConnection.new
|
||||||
end
|
end
|
||||||
|
|
||||||
class FooConnection < DB::Connection
|
class FooConnection < DB::Connection
|
||||||
|
@ -111,7 +111,7 @@ class BarDriver < DB::Driver
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_connection(context : DB::ConnectionContext) : DB::Connection
|
def build_connection(context : DB::ConnectionContext) : DB::Connection
|
||||||
BarConnection.new(context)
|
BarConnection.new
|
||||||
end
|
end
|
||||||
|
|
||||||
class BarConnection < DB::Connection
|
class BarConnection < DB::Connection
|
||||||
|
|
|
@ -3,12 +3,11 @@ require "../src/db"
|
||||||
|
|
||||||
class DummyDriver < DB::Driver
|
class DummyDriver < DB::Driver
|
||||||
def build_connection(context : DB::ConnectionContext) : DB::Connection
|
def build_connection(context : DB::ConnectionContext) : DB::Connection
|
||||||
DummyConnection.new(context)
|
DummyConnection.new
|
||||||
end
|
end
|
||||||
|
|
||||||
class DummyConnection < DB::Connection
|
class DummyConnection < DB::Connection
|
||||||
def initialize(context)
|
def initialize
|
||||||
super(context)
|
|
||||||
@connected = true
|
@connected = true
|
||||||
@@connections ||= [] of DummyConnection
|
@@connections ||= [] of DummyConnection
|
||||||
@@connections.not_nil! << self
|
@@connections.not_nil! << self
|
||||||
|
|
|
@ -160,7 +160,8 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
private def self.build_connection(uri : URI)
|
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
|
end
|
||||||
|
|
||||||
private def self.build_driver(uri : URI)
|
private def self.build_driver(uri : URI)
|
||||||
|
|
|
@ -24,15 +24,14 @@ module DB
|
||||||
include BeginTransaction
|
include BeginTransaction
|
||||||
|
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
getter context
|
property context : ConnectionContext = SingleConnectionContext.default
|
||||||
@statements_cache = StringKeyCache(Statement).new
|
@statements_cache = StringKeyCache(Statement).new
|
||||||
@transaction = false
|
@transaction = false
|
||||||
getter? prepared_statements : Bool
|
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
property auto_release : Bool = true
|
property auto_release : Bool = true
|
||||||
|
|
||||||
def initialize(@context : ConnectionContext)
|
def prepared_statements? : Bool
|
||||||
@prepared_statements = @context.prepared_statements?
|
context.prepared_statements?
|
||||||
end
|
end
|
||||||
|
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
|
@ -59,7 +58,7 @@ module DB
|
||||||
protected def do_close
|
protected def do_close
|
||||||
@statements_cache.each_value &.close
|
@statements_cache.each_value &.close
|
||||||
@statements_cache.clear
|
@statements_cache.clear
|
||||||
@context.discard self
|
context.discard self
|
||||||
end
|
end
|
||||||
|
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
|
@ -75,7 +74,7 @@ module DB
|
||||||
# managed by the database. Should be used
|
# managed by the database. Should be used
|
||||||
# only if the connection was obtained by `Database#checkout`.
|
# only if the connection was obtained by `Database#checkout`.
|
||||||
def release
|
def release
|
||||||
@context.release(self)
|
context.release(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
module DB
|
module DB
|
||||||
module ConnectionContext
|
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
|
# Return whether the statements should be prepared by default
|
||||||
abstract def prepared_statements? : Bool
|
abstract def prepared_statements? : Bool
|
||||||
|
|
||||||
|
@ -19,12 +16,11 @@ module DB
|
||||||
class SingleConnectionContext
|
class SingleConnectionContext
|
||||||
include ConnectionContext
|
include ConnectionContext
|
||||||
|
|
||||||
getter uri : URI
|
class_getter default : SingleConnectionContext = SingleConnectionContext.new(true)
|
||||||
|
|
||||||
getter? prepared_statements : Bool
|
getter? prepared_statements : Bool
|
||||||
|
|
||||||
def initialize(@uri : URI)
|
def initialize(@prepared_statements : Bool)
|
||||||
params = HTTP::Params.parse(uri.query || "")
|
|
||||||
@prepared_statements = DB.fetch_bool(params, "prepared_statements", true)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def discard(connection : Connection)
|
def discard(connection : Connection)
|
||||||
|
|
|
@ -56,6 +56,7 @@ module DB
|
||||||
@pool = Pool.new(**pool_options) {
|
@pool = Pool.new(**pool_options) {
|
||||||
conn = @driver.build_connection(self).as(Connection)
|
conn = @driver.build_connection(self).as(Connection)
|
||||||
conn.auto_release = false
|
conn.auto_release = false
|
||||||
|
conn.context = self
|
||||||
@setup_connection.call conn
|
@setup_connection.call conn
|
||||||
conn
|
conn
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue