mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Use ConnectionBuilder instead of procs
This commit is contained in:
parent
83c3e91ec2
commit
9370f5784b
5 changed files with 46 additions and 12 deletions
|
@ -36,6 +36,15 @@ class FooValue
|
||||||
end
|
end
|
||||||
|
|
||||||
class FooDriver < DB::Driver
|
class FooDriver < DB::Driver
|
||||||
|
class FooConnectionBuilder < DB::ConnectionBuilder
|
||||||
|
def initialize(@options : DB::Connection::Options)
|
||||||
|
end
|
||||||
|
|
||||||
|
def build : DB::Connection
|
||||||
|
FooConnection.new(@options)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
alias Any = DB::Any | FooValue
|
alias Any = DB::Any | FooValue
|
||||||
@@row = [] of Any
|
@@row = [] of Any
|
||||||
|
|
||||||
|
@ -47,10 +56,9 @@ class FooDriver < DB::Driver
|
||||||
@@row
|
@@row
|
||||||
end
|
end
|
||||||
|
|
||||||
def connection_builder(uri : URI) : Proc(DB::Connection)
|
def connection_builder(uri : URI) : DB::ConnectionBuilder
|
||||||
params = HTTP::Params.parse(uri.query || "")
|
params = HTTP::Params.parse(uri.query || "")
|
||||||
options = connection_options(params)
|
FooConnectionBuilder.new(connection_options(params))
|
||||||
->{ FooConnection.new(options).as(DB::Connection) }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class FooConnection < DB::Connection
|
class FooConnection < DB::Connection
|
||||||
|
@ -101,6 +109,15 @@ class BarValue
|
||||||
end
|
end
|
||||||
|
|
||||||
class BarDriver < DB::Driver
|
class BarDriver < DB::Driver
|
||||||
|
class BarConnectionBuilder < DB::ConnectionBuilder
|
||||||
|
def initialize(@options : DB::Connection::Options)
|
||||||
|
end
|
||||||
|
|
||||||
|
def build : DB::Connection
|
||||||
|
BarConnection.new(@options)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
alias Any = DB::Any | BarValue
|
alias Any = DB::Any | BarValue
|
||||||
@@row = [] of Any
|
@@row = [] of Any
|
||||||
|
|
||||||
|
@ -112,10 +129,9 @@ class BarDriver < DB::Driver
|
||||||
@@row
|
@@row
|
||||||
end
|
end
|
||||||
|
|
||||||
def connection_builder(uri : URI) : Proc(DB::Connection)
|
def connection_builder(uri : URI) : DB::ConnectionBuilder
|
||||||
params = HTTP::Params.parse(uri.query || "")
|
params = HTTP::Params.parse(uri.query || "")
|
||||||
options = connection_options(params)
|
BarConnectionBuilder.new(connection_options(params))
|
||||||
->{ BarConnection.new(options).as(DB::Connection) }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class BarConnection < DB::Connection
|
class BarConnection < DB::Connection
|
||||||
|
|
|
@ -2,10 +2,18 @@ require "spec"
|
||||||
require "../src/db"
|
require "../src/db"
|
||||||
|
|
||||||
class DummyDriver < DB::Driver
|
class DummyDriver < DB::Driver
|
||||||
def connection_builder(uri : URI) : Proc(DB::Connection)
|
class DummyConnectionBuilder < DB::ConnectionBuilder
|
||||||
|
def initialize(@options : DB::Connection::Options)
|
||||||
|
end
|
||||||
|
|
||||||
|
def build : DB::Connection
|
||||||
|
DummyConnection.new(@options)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def connection_builder(uri : URI) : DB::ConnectionBuilder
|
||||||
params = HTTP::Params.parse(uri.query || "")
|
params = HTTP::Params.parse(uri.query || "")
|
||||||
options = connection_options(params)
|
DummyConnectionBuilder.new(connection_options(params))
|
||||||
->{ DummyConnection.new(options).as(DB::Connection) }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class DummyConnection < DB::Connection
|
class DummyConnection < DB::Connection
|
||||||
|
|
|
@ -156,7 +156,8 @@ module DB
|
||||||
params = HTTP::Params.parse(uri.query || "")
|
params = HTTP::Params.parse(uri.query || "")
|
||||||
connection_options = driver.connection_options(params)
|
connection_options = driver.connection_options(params)
|
||||||
pool_options = driver.pool_options(params)
|
pool_options = driver.pool_options(params)
|
||||||
factory = driver.connection_builder(uri)
|
builder = driver.connection_builder(uri)
|
||||||
|
factory = ->{ builder.build }
|
||||||
Database.new(connection_options, pool_options, &factory)
|
Database.new(connection_options, pool_options, &factory)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -165,7 +166,7 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
private def self.build_connection(uri : URI)
|
private def self.build_connection(uri : URI)
|
||||||
build_driver(uri).connection_builder(uri).call
|
build_driver(uri).connection_builder(uri).build
|
||||||
end
|
end
|
||||||
|
|
||||||
private def self.build_driver(uri : URI)
|
private def self.build_driver(uri : URI)
|
||||||
|
@ -193,6 +194,7 @@ require "./db/enumerable_concat"
|
||||||
require "./db/query_methods"
|
require "./db/query_methods"
|
||||||
require "./db/session_methods"
|
require "./db/session_methods"
|
||||||
require "./db/disposable"
|
require "./db/disposable"
|
||||||
|
require "./db/connection_builder"
|
||||||
require "./db/driver"
|
require "./db/driver"
|
||||||
require "./db/statement"
|
require "./db/statement"
|
||||||
require "./db/begin_transaction"
|
require "./db/begin_transaction"
|
||||||
|
|
8
src/db/connection_builder.cr
Normal file
8
src/db/connection_builder.cr
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
module DB
|
||||||
|
# A connection factory with a specific configuration.
|
||||||
|
#
|
||||||
|
# See `Driver#connection_builder`.
|
||||||
|
abstract class ConnectionBuilder
|
||||||
|
abstract def build : Connection
|
||||||
|
end
|
||||||
|
end
|
|
@ -35,7 +35,7 @@ module DB
|
||||||
#
|
#
|
||||||
# NOTE: For implementors *uri* should be parsed once. If all the options
|
# NOTE: For implementors *uri* should be parsed once. If all the options
|
||||||
# are sound a factory Proc is returned.
|
# are sound a factory Proc is returned.
|
||||||
abstract def connection_builder(uri : URI) : Proc(Connection)
|
abstract def connection_builder(uri : URI) : ConnectionBuilder
|
||||||
|
|
||||||
def connection_options(params : HTTP::Params) : Connection::Options
|
def connection_options(params : HTTP::Params) : Connection::Options
|
||||||
Connection::Options.from_http_params(params)
|
Connection::Options.from_http_params(params)
|
||||||
|
|
Loading…
Reference in a new issue