class DB::Client
Binds a connection pool to the specified adapter.
Attributes
The adapter used for making connections. @attribute [Object]
Public Class Methods
Initialize the client and internal connection pool using the specified adapter. @parameter adapter [Object] The adapter instance.
# File lib/db/client.rb, line 36 def initialize(adapter, **options) @adapter = adapter @pool = connect(**options) end
Public Instance Methods
Close all open connections in the connection pool.
# File lib/db/client.rb, line 47 def close @pool.close end
Acquire a generic context which will acquire a connection on demand.
# File lib/db/client.rb, line 52 def context(**options) context = Context::Generic.new(@pool, **options) return context unless block_given? begin yield context ensure context.close end end
Acquires a connection and sends the specified statement if given. @parameters statement [String | Nil] An optional statement to send. @yields {|session| …} A connected session if a block is given. Implicitly closed.
@parameter session [Context::Session]
@returns [Context::Session] A connected session if no block is given.
# File lib/db/client.rb, line 69 def session(**options) session = Context::Session.new(@pool, **options) return session unless block_given? begin yield session ensure session.close end end
Acquires a connection and starts a transaction. @parameters statement [String | Nil] An optional statement to send. Defaults to `“BEGIN”`. @yields {|session| …} A connected session if a block is given. Implicitly commits, or aborts the connnection if an exception is raised.
@parameter session [Context::Transaction]
@returns [Context::Transaction] A connected and started transaction if no block is given.
# File lib/db/client.rb, line 86 def transaction(**options) transaction = Context::Transaction.new(@pool, **options) transaction.call("BEGIN") return transaction unless block_given? begin yield transaction rescue transaction.abort raise ensure transaction.commit? end end
Protected Instance Methods
# File lib/db/client.rb, line 106 def connect(**options) Async::Pool::Controller.new(@adapter, **options) end