2016-01-28 22:41:12 +00:00
|
|
|
module DB
|
2016-01-31 22:40:02 +00:00
|
|
|
# Database driver implementors must subclass `Driver`,
|
|
|
|
# register with a driver_name using `DB#register_driver` and
|
2023-06-23 01:03:08 +00:00
|
|
|
# override the factory method `#connection_builder`.
|
2016-01-31 22:40:02 +00:00
|
|
|
#
|
|
|
|
# ```
|
|
|
|
# require "db"
|
|
|
|
#
|
2017-03-20 18:08:30 +00:00
|
|
|
# class FakeDriver < DB::Driver
|
2023-08-15 00:11:35 +00:00
|
|
|
# class FakeConnectionBuilder < DB::ConnectionBuilder
|
|
|
|
# def initialize(@options : DB::Connection::Options)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def build : DB::Connection
|
|
|
|
# FakeConnection.new(@options)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def connection_builder(uri : URI) : ConnectionBuilder
|
2023-06-23 01:03:08 +00:00
|
|
|
# params = HTTP::Params.parse(uri.query || "")
|
|
|
|
# options = connection_options(params)
|
2023-08-15 00:11:35 +00:00
|
|
|
# # If needed, parse custom options from uri here
|
|
|
|
# # so they are parsed only once.
|
|
|
|
# FakeConnectionBuilder.new(options)
|
2016-01-31 22:40:02 +00:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# DB.register_driver "fake", FakeDriver
|
|
|
|
# ```
|
|
|
|
#
|
2023-06-23 01:03:08 +00:00
|
|
|
# Access to this fake database will be available with
|
2016-01-31 22:40:02 +00:00
|
|
|
#
|
|
|
|
# ```
|
2016-02-03 22:30:51 +00:00
|
|
|
# DB.open "fake://..." do |db|
|
2016-01-31 22:40:02 +00:00
|
|
|
# # ... use db ...
|
|
|
|
# end
|
|
|
|
# ```
|
|
|
|
#
|
|
|
|
# Refer to `Connection`, `Statement` and `ResultSet` for further
|
|
|
|
# driver implementation instructions.
|
2023-06-23 01:03:08 +00:00
|
|
|
#
|
|
|
|
# Override `#connection_options` and `#pool_options` to provide custom
|
|
|
|
# defaults or parsing of the connection string URI.
|
2016-01-28 22:41:12 +00:00
|
|
|
abstract class Driver
|
2023-06-23 01:03:08 +00:00
|
|
|
# Returns a new connection factory.
|
|
|
|
#
|
|
|
|
# NOTE: For implementors *uri* should be parsed once. If all the options
|
|
|
|
# are sound a ConnectionBuilder is returned.
|
|
|
|
abstract def connection_builder(uri : URI) : ConnectionBuilder
|
|
|
|
|
|
|
|
def connection_options(params : HTTP::Params) : Connection::Options
|
|
|
|
Connection::Options.from_http_params(params)
|
|
|
|
end
|
2016-07-07 18:50:09 +00:00
|
|
|
|
2023-06-23 01:03:08 +00:00
|
|
|
def pool_options(params : HTTP::Params) : Pool::Options
|
|
|
|
Pool::Options.from_http_params(params)
|
2016-07-07 18:50:09 +00:00
|
|
|
end
|
2016-01-28 22:41:12 +00:00
|
|
|
end
|
|
|
|
end
|