Class: DB::Client
- Inherits:
-
Object
- Object
- DB::Client
- Defined in:
- lib/db/client.rb
Overview
Binds a connection pool to the specified adapter.
Instance Attribute Summary collapse
-
#adapter ⇒ Object
The adapter used for making connections.
Instance Method Summary collapse
-
#close ⇒ Object
Close all open connections in the connection pool.
-
#context(**options) ⇒ Object
Acquire a generic context which will acquire a connection on demand.
-
#initialize(adapter, **options) ⇒ Client
constructor
Initialize the client and internal connection pool using the specified adapter.
-
#session(**options) ⇒ Object
Acquires a connection and sends the specified statement if given.
-
#transaction(**options) ⇒ Object
Acquires a connection and starts a transaction.
Constructor Details
#initialize(adapter, **options) ⇒ Client
Initialize the client and internal connection pool using the specified adapter.
36 37 38 39 40 |
# File 'lib/db/client.rb', line 36 def initialize(adapter, **) @adapter = adapter @pool = connect(**) end |
Instance Attribute Details
#adapter ⇒ Object
The adapter used for making connections.
44 45 46 |
# File 'lib/db/client.rb', line 44 def adapter @adapter end |
Instance Method Details
#close ⇒ Object
Close all open connections in the connection pool.
47 48 49 |
# File 'lib/db/client.rb', line 47 def close @pool.close end |
#context(**options) ⇒ Object
Acquire a generic context which will acquire a connection on demand.
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/db/client.rb', line 52 def context(**) context = Context::Generic.new(@pool, **) return context unless block_given? begin yield context ensure context.close end end |
#session(**options) ⇒ Object
Acquires a connection and sends the specified statement if given.
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/db/client.rb', line 69 def session(**) session = Context::Session.new(@pool, **) return session unless block_given? begin yield session ensure session.close end end |
#transaction(**options) ⇒ Object
Acquires a connection and starts a transaction.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/db/client.rb', line 86 def transaction(**) transaction = Context::Transaction.new(@pool, **) transaction.call("BEGIN") return transaction unless block_given? begin yield transaction transaction.commit rescue transaction.abort raise end end |