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.
-
#initialize(adapter, **options) ⇒ Client
constructor
Initialize the client and internal connection pool using the specified adapter.
-
#session(statement = nil, **options) ⇒ Object
Acquires a connection and sends the specified statement if given.
-
#transaction(statement = "BEGIN", **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.
35 36 37 38 39 |
# File 'lib/db/client.rb', line 35 def initialize(adapter, **) @adapter = adapter @pool = connect(**) end |
Instance Attribute Details
#adapter ⇒ Object
The adapter used for making connections.
43 44 45 |
# File 'lib/db/client.rb', line 43 def adapter @adapter end |
Instance Method Details
#close ⇒ Object
Close all open connections in the connection pool.
46 47 48 |
# File 'lib/db/client.rb', line 46 def close @pool.close end |
#session(statement = nil, **options) ⇒ Object
Acquires a connection and sends the specified statement if given.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/db/client.rb', line 55 def session(statement = nil, **) session = Context::Session.new(@pool, **) if statement session.send_query(statement) end return session unless block_given? begin yield session ensure session.close end end |
#transaction(statement = "BEGIN", **options) ⇒ Object
Acquires a connection and starts a transaction.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/db/client.rb', line 76 def transaction(statement = "BEGIN", **) transaction = Context::Transaction.new(@pool, **) if statement transaction.call("BEGIN") end return transaction unless block_given? begin yield transaction transaction.commit ensure transaction.abort if $! end end |