mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Allow io_provider on DB.open Db.connect
This commit is contained in:
parent
87dc8aafaf
commit
7817f52b5b
4 changed files with 38 additions and 18 deletions
33
src/db.cr
33
src/db.cr
|
@ -115,13 +115,13 @@ module DB
|
|||
# to each database driver's specific format.
|
||||
#
|
||||
# The returned database must be closed by `Database#close`.
|
||||
def self.open(uri : URI | String)
|
||||
build_database(uri)
|
||||
def self.open(uri : URI | String, *, io_provider : IOProvider? = nil)
|
||||
build_database(uri, io_provider: io_provider)
|
||||
end
|
||||
|
||||
# Same as `#open` but the database is yielded and closed automatically at the end of the block.
|
||||
def self.open(uri : URI | String, &block)
|
||||
db = build_database(uri)
|
||||
def self.open(uri : URI | String, *, io_provider : IOProvider? = nil, &block)
|
||||
db = build_database(uri, io_provider: io_provider)
|
||||
begin
|
||||
yield db
|
||||
ensure
|
||||
|
@ -133,13 +133,13 @@ module DB
|
|||
# The scheme of the *uri* determines the driver to use.
|
||||
# Returned connection must be closed by `Connection#close`.
|
||||
# If a block is used the connection is yielded and closed automatically.
|
||||
def self.connect(uri : URI | String)
|
||||
build_connection(uri)
|
||||
def self.connect(uri : URI | String, *, io_provider : IOProvider? = nil)
|
||||
build_connection(uri, io_provider: io_provider)
|
||||
end
|
||||
|
||||
# :ditto:
|
||||
def self.connect(uri : URI | String, &block)
|
||||
cnn = build_connection(uri)
|
||||
def self.connect(uri : URI | String, *, io_provider : IOProvider? = nil, &block)
|
||||
cnn = build_connection(uri, io_provider: io_provider)
|
||||
begin
|
||||
yield cnn
|
||||
ensure
|
||||
|
@ -147,20 +147,20 @@ module DB
|
|||
end
|
||||
end
|
||||
|
||||
private def self.build_database(connection_string : String)
|
||||
build_database(URI.parse(connection_string))
|
||||
private def self.build_database(connection_string : String, *, io_provider : IOProvider?)
|
||||
build_database(URI.parse(connection_string), io_provider: io_provider)
|
||||
end
|
||||
|
||||
private def self.build_database(uri : URI)
|
||||
Database.new(build_driver(uri), uri)
|
||||
private def self.build_database(uri : URI, *, io_provider : IOProvider?)
|
||||
Database.new(build_driver(uri), uri, io_provider)
|
||||
end
|
||||
|
||||
private def self.build_connection(connection_string : String)
|
||||
build_connection(URI.parse(connection_string))
|
||||
private def self.build_connection(connection_string : String, *, io_provider : IOProvider?)
|
||||
build_connection(URI.parse(connection_string), io_provider: io_provider)
|
||||
end
|
||||
|
||||
private def self.build_connection(uri : URI)
|
||||
build_driver(uri).build_connection(SingleConnectionContext.new(uri)).as(Connection)
|
||||
private def self.build_connection(uri : URI, *, io_provider : IOProvider?)
|
||||
build_driver(uri).build_connection(SingleConnectionContext.new(uri, io_provider)).as(Connection)
|
||||
end
|
||||
|
||||
private def self.build_driver(uri : URI)
|
||||
|
@ -182,6 +182,7 @@ module DB
|
|||
end
|
||||
end
|
||||
|
||||
require "./db/io_provider"
|
||||
require "./db/pool"
|
||||
require "./db/string_key_cache"
|
||||
require "./db/enumerable_concat"
|
||||
|
|
|
@ -3,6 +3,9 @@ module DB
|
|||
# Returns the uri with the connection settings to the database
|
||||
abstract def uri : URI
|
||||
|
||||
# Returns the io_provider to use
|
||||
abstract def io_provider : IOProvider?
|
||||
|
||||
# Return whether the statements should be prepared by default
|
||||
abstract def prepared_statements? : Bool
|
||||
|
||||
|
@ -20,9 +23,10 @@ module DB
|
|||
include ConnectionContext
|
||||
|
||||
getter uri : URI
|
||||
getter io_provider : IOProvider?
|
||||
getter? prepared_statements : Bool
|
||||
|
||||
def initialize(@uri : URI)
|
||||
def initialize(@uri : URI, @io_provider : IOProvider?)
|
||||
params = HTTP::Params.parse(uri.query || "")
|
||||
@prepared_statements = DB.fetch_bool(params, "prepared_statements", true)
|
||||
end
|
||||
|
|
|
@ -39,6 +39,9 @@ module DB
|
|||
# Returns the uri with the connection settings to the database
|
||||
getter uri : URI
|
||||
|
||||
# :nodoc:
|
||||
getter io_provider : IOProvider?
|
||||
|
||||
getter? prepared_statements : Bool
|
||||
|
||||
@pool : Pool(Connection)
|
||||
|
@ -46,7 +49,7 @@ module DB
|
|||
@statements_cache = StringKeyCache(PoolPreparedStatement).new
|
||||
|
||||
# :nodoc:
|
||||
def initialize(@driver : Driver, @uri : URI)
|
||||
def initialize(@driver : Driver, @uri : URI, @io_provider : IOProvider?)
|
||||
params = HTTP::Params.parse(uri.query || "")
|
||||
@prepared_statements = DB.fetch_bool(params, "prepared_statements", true)
|
||||
pool_options = @driver.connection_pool_options(params)
|
||||
|
|
12
src/db/io_provider.cr
Normal file
12
src/db/io_provider.cr
Normal file
|
@ -0,0 +1,12 @@
|
|||
module DB
|
||||
# An `IOProvider` can be used to customize
|
||||
# how underlying IO for connections are created.
|
||||
# Not all drivers are backed by IO.
|
||||
abstract class IOProvider
|
||||
abstract def setup : Void
|
||||
|
||||
abstract def teardown : Void
|
||||
|
||||
abstract def build_io : IO
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue