class DB::Client

Binds a connection pool to the specified adapter.

Attributes

adapter[R]

The adapter used for making connections. @attribute [Object]

Public Class Methods

new(adapter, **options) click to toggle source

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() click to toggle source

Close all open connections in the connection pool.

# File lib/db/client.rb, line 47
def close
        @pool.close
end
context(**options) { |context| ... } click to toggle source

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
session(**options) { |session| ... } click to toggle source

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
transaction(**options) { |transaction| ... } click to toggle source

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

connect(**options) click to toggle source
# File lib/db/client.rb, line 106
def connect(**options)
        Async::Pool::Controller.new(@adapter, **options)
end