class DB::Context::Generic

A connected context for sending queries and reading results.

Public Class Methods

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

Iniitalize the query context attached to the given connection pool.

# File lib/db/context/generic.rb, line 30
def initialize(pool, **options)
        @pool = pool
        @connection = nil
end

Public Instance Methods

call(statement, **options) { |connection| ... } click to toggle source

Send a query to the server. @parameter statement [String] The SQL query to send.

# File lib/db/context/generic.rb, line 70
def call(statement, **options)
        connection.send_query(statement, **options)
        
        yield connection if block_given?
ensure
        self.close
end
clause(fragment = String.new) click to toggle source
# File lib/db/context/generic.rb, line 64
def clause(fragment = String.new)
        Query.new(self, fragment)
end
close() click to toggle source

Flush the connection and then return it to the connection pool.

# File lib/db/context/generic.rb, line 45
def close
        if @connection
                @pool.release(@connection)
                @connection = nil
        end
end
closed?() click to toggle source
# File lib/db/context/generic.rb, line 52
def closed?
        @connection.nil?
end
connection() click to toggle source

Lazy initialize underlying connection.

# File lib/db/context/generic.rb, line 40
def connection
        @connection ||= @pool.acquire
end
connection?() click to toggle source
# File lib/db/context/generic.rb, line 35
def connection?
        @connection != nil
end
query(fragment = String.new, **parameters) click to toggle source
# File lib/db/context/generic.rb, line 56
def query(fragment = String.new, **parameters)
        if parameters.empty?
                Query.new(self, fragment)
        else
                Query.new(self).interpolate(fragment, **parameters)
        end
end