class DB::Context::Session
A connected context for sending queries and reading results.
Attributes
connection[R]
pool[R]
Public Class Methods
new(pool, **options)
click to toggle source
Initialize the query context attached to the given connection pool.
# File lib/db/context/session.rb, line 14 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/session.rb, line 55 def call(statement, **options) self.with_connection do |connection| connection.send_query(statement, **options) if block_given? yield connection elsif result = connection.next_result return Records.wrap(result) end end end
clause(fragment = String.new)
click to toggle source
# File lib/db/context/session.rb, line 77 def clause(fragment = String.new) with_connection do Query.new(self, fragment) end end
close()
click to toggle source
Flush the connection and then return it to the connection pool.
# File lib/db/context/session.rb, line 28 def close if @connection @pool.release(@connection) @connection = nil end end
closed?()
click to toggle source
# File lib/db/context/session.rb, line 35 def closed? @connection.nil? end
connect!()
click to toggle source
Pin a connection to the current session.
# File lib/db/context/session.rb, line 23 def connect! @connection ||= @pool.acquire end
query(fragment = String.new, **parameters)
click to toggle source
# File lib/db/context/session.rb, line 67 def query(fragment = String.new, **parameters) with_connection do if parameters.empty? Query.new(self, fragment) else Query.new(self).interpolate(fragment, **parameters) end end end
with_connection() { |connection| ... }
click to toggle source
# File lib/db/context/session.rb, line 39 def with_connection(&block) if @connection yield @connection else @pool.acquire do |connection| @connection = connection yield connection ensure @connection = nil end end end