mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Introduce DB::Connection::Options
Move prepared_statements out from ConnectionContext
This commit is contained in:
parent
0328767b98
commit
bc0200e178
5 changed files with 26 additions and 15 deletions
|
@ -48,7 +48,9 @@ class FooDriver < DB::Driver
|
|||
end
|
||||
|
||||
def connection_builder(uri : URI) : Proc(DB::Connection)
|
||||
-> { FooConnection.new.as(DB::Connection) }
|
||||
params = HTTP::Params.parse(uri.query || "")
|
||||
options = DB::Connection::Options.from_http_params(params)
|
||||
->{ FooConnection.new(options).as(DB::Connection) }
|
||||
end
|
||||
|
||||
class FooConnection < DB::Connection
|
||||
|
@ -111,7 +113,9 @@ class BarDriver < DB::Driver
|
|||
end
|
||||
|
||||
def connection_builder(uri : URI) : Proc(DB::Connection)
|
||||
-> { BarConnection.new.as(DB::Connection) }
|
||||
params = HTTP::Params.parse(uri.query || "")
|
||||
options = DB::Connection::Options.from_http_params(params)
|
||||
->{ BarConnection.new(options).as(DB::Connection) }
|
||||
end
|
||||
|
||||
class BarConnection < DB::Connection
|
||||
|
|
|
@ -3,11 +3,14 @@ require "../src/db"
|
|||
|
||||
class DummyDriver < DB::Driver
|
||||
def connection_builder(uri : URI) : Proc(DB::Connection)
|
||||
-> { DummyConnection.new.as(DB::Connection) }
|
||||
params = HTTP::Params.parse(uri.query || "")
|
||||
options = DB::Connection::Options.from_http_params(params)
|
||||
->{ DummyConnection.new(options).as(DB::Connection) }
|
||||
end
|
||||
|
||||
class DummyConnection < DB::Connection
|
||||
def initialize
|
||||
def initialize(options : DB::Connection::Options)
|
||||
super(options)
|
||||
@connected = true
|
||||
@@connections ||= [] of DummyConnection
|
||||
@@connections.not_nil! << self
|
||||
|
|
|
@ -160,7 +160,6 @@ module DB
|
|||
end
|
||||
|
||||
private def self.build_connection(uri : URI)
|
||||
# PENDING: parse connection options from uri and set the right connection context
|
||||
build_driver(uri).connection_builder(uri).call
|
||||
end
|
||||
|
||||
|
|
|
@ -23,6 +23,16 @@ module DB
|
|||
include SessionMethods(Connection, Statement)
|
||||
include BeginTransaction
|
||||
|
||||
record Options,
|
||||
# Return whether the statements should be prepared by default
|
||||
prepared_statements : Bool = true do
|
||||
def self.from_http_params(params : HTTP::Params, default = Options.new)
|
||||
Options.new(
|
||||
prepared_statements: DB.fetch_bool(params, "prepared_statements", default.prepared_statements)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
# :nodoc:
|
||||
property context : ConnectionContext = SingleConnectionContext.default
|
||||
@statements_cache = StringKeyCache(Statement).new
|
||||
|
@ -30,8 +40,11 @@ module DB
|
|||
# :nodoc:
|
||||
property auto_release : Bool = true
|
||||
|
||||
def initialize(@options : Options)
|
||||
end
|
||||
|
||||
def prepared_statements? : Bool
|
||||
context.prepared_statements?
|
||||
@options.prepared_statements
|
||||
end
|
||||
|
||||
# :nodoc:
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
module DB
|
||||
module ConnectionContext
|
||||
# Return whether the statements should be prepared by default
|
||||
abstract def prepared_statements? : Bool
|
||||
|
||||
# Indicates that the *connection* was permanently closed
|
||||
# and should not be used in the future.
|
||||
abstract def discard(connection : Connection)
|
||||
|
@ -16,12 +13,7 @@ module DB
|
|||
class SingleConnectionContext
|
||||
include ConnectionContext
|
||||
|
||||
class_getter default : SingleConnectionContext = SingleConnectionContext.new(true)
|
||||
|
||||
getter? prepared_statements : Bool
|
||||
|
||||
def initialize(@prepared_statements : Bool)
|
||||
end
|
||||
class_getter default : SingleConnectionContext = SingleConnectionContext.new
|
||||
|
||||
def discard(connection : Connection)
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue