diff --git a/spec/custom_drivers_types_spec.cr b/spec/custom_drivers_types_spec.cr index bb1eaf8..9345317 100644 --- a/spec/custom_drivers_types_spec.cr +++ b/spec/custom_drivers_types_spec.cr @@ -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 diff --git a/spec/dummy_driver.cr b/spec/dummy_driver.cr index 0d44ce9..01de1e5 100644 --- a/spec/dummy_driver.cr +++ b/spec/dummy_driver.cr @@ -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 diff --git a/src/db.cr b/src/db.cr index 2d8274d..c0e0b64 100644 --- a/src/db.cr +++ b/src/db.cr @@ -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 diff --git a/src/db/connection.cr b/src/db/connection.cr index cb029ef..f38fd58 100644 --- a/src/db/connection.cr +++ b/src/db/connection.cr @@ -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: diff --git a/src/db/connection_context.cr b/src/db/connection_context.cr index ba8823f..297d0cd 100644 --- a/src/db/connection_context.cr +++ b/src/db/connection_context.cr @@ -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