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
|
end
|
||||||
|
|
||||||
def connection_builder(uri : URI) : Proc(DB::Connection)
|
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
|
end
|
||||||
|
|
||||||
class FooConnection < DB::Connection
|
class FooConnection < DB::Connection
|
||||||
|
@ -111,7 +113,9 @@ class BarDriver < DB::Driver
|
||||||
end
|
end
|
||||||
|
|
||||||
def connection_builder(uri : URI) : Proc(DB::Connection)
|
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
|
end
|
||||||
|
|
||||||
class BarConnection < DB::Connection
|
class BarConnection < DB::Connection
|
||||||
|
|
|
@ -3,11 +3,14 @@ require "../src/db"
|
||||||
|
|
||||||
class DummyDriver < DB::Driver
|
class DummyDriver < DB::Driver
|
||||||
def connection_builder(uri : URI) : Proc(DB::Connection)
|
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
|
end
|
||||||
|
|
||||||
class DummyConnection < DB::Connection
|
class DummyConnection < DB::Connection
|
||||||
def initialize
|
def initialize(options : DB::Connection::Options)
|
||||||
|
super(options)
|
||||||
@connected = true
|
@connected = true
|
||||||
@@connections ||= [] of DummyConnection
|
@@connections ||= [] of DummyConnection
|
||||||
@@connections.not_nil! << self
|
@@connections.not_nil! << self
|
||||||
|
|
|
@ -160,7 +160,6 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
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
|
|
||||||
build_driver(uri).connection_builder(uri).call
|
build_driver(uri).connection_builder(uri).call
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,16 @@ module DB
|
||||||
include SessionMethods(Connection, Statement)
|
include SessionMethods(Connection, Statement)
|
||||||
include BeginTransaction
|
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:
|
# :nodoc:
|
||||||
property context : ConnectionContext = SingleConnectionContext.default
|
property context : ConnectionContext = SingleConnectionContext.default
|
||||||
@statements_cache = StringKeyCache(Statement).new
|
@statements_cache = StringKeyCache(Statement).new
|
||||||
|
@ -30,8 +40,11 @@ module DB
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
property auto_release : Bool = true
|
property auto_release : Bool = true
|
||||||
|
|
||||||
|
def initialize(@options : Options)
|
||||||
|
end
|
||||||
|
|
||||||
def prepared_statements? : Bool
|
def prepared_statements? : Bool
|
||||||
context.prepared_statements?
|
@options.prepared_statements
|
||||||
end
|
end
|
||||||
|
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
module DB
|
module DB
|
||||||
module ConnectionContext
|
module ConnectionContext
|
||||||
# Return whether the statements should be prepared by default
|
|
||||||
abstract def prepared_statements? : Bool
|
|
||||||
|
|
||||||
# Indicates that the *connection* was permanently closed
|
# Indicates that the *connection* was permanently closed
|
||||||
# and should not be used in the future.
|
# and should not be used in the future.
|
||||||
abstract def discard(connection : Connection)
|
abstract def discard(connection : Connection)
|
||||||
|
@ -16,12 +13,7 @@ module DB
|
||||||
class SingleConnectionContext
|
class SingleConnectionContext
|
||||||
include ConnectionContext
|
include ConnectionContext
|
||||||
|
|
||||||
class_getter default : SingleConnectionContext = SingleConnectionContext.new(true)
|
class_getter default : SingleConnectionContext = SingleConnectionContext.new
|
||||||
|
|
||||||
getter? prepared_statements : Bool
|
|
||||||
|
|
||||||
def initialize(@prepared_statements : Bool)
|
|
||||||
end
|
|
||||||
|
|
||||||
def discard(connection : Connection)
|
def discard(connection : Connection)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue