Class: DB::Context::Session
- Inherits:
-
Object
- Object
- DB::Context::Session
- Defined in:
- lib/db/context/session.rb
Overview
A connected context for sending queries and reading results.
Direct Known Subclasses
Instance Method Summary collapse
-
#call(statement, **options) ⇒ Object
Send a query to the server.
- #clause(fragment = String.new) ⇒ Object
-
#close ⇒ Object
Flush the connection and then return it to the connection pool.
- #closed? ⇒ Boolean
-
#connection ⇒ Object
Lazy initialize underlying connection.
- #connection? ⇒ Boolean
-
#initialize(pool, **options) ⇒ Session
constructor
Initialize the query context attached to the given connection pool.
- #query(fragment = String.new, **parameters) ⇒ Object
Constructor Details
#initialize(pool, **options) ⇒ Session
Initialize the query context attached to the given connection pool.
14 15 16 17 |
# File 'lib/db/context/session.rb', line 14 def initialize(pool, **) @pool = pool @connection = nil end |
Instance Method Details
#call(statement, **options) ⇒ Object
Send a query to the server.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/db/context/session.rb', line 54 def call(statement, **) connection = self.connection connection.send_query(statement, **) if block_given? yield connection elsif result = connection.next_result return Records.wrap(result) end end |
#clause(fragment = String.new) ⇒ Object
48 49 50 |
# File 'lib/db/context/session.rb', line 48 def clause(fragment = String.new) Query.new(self, fragment) end |
#close ⇒ Object
Flush the connection and then return it to the connection pool.
29 30 31 32 33 34 |
# File 'lib/db/context/session.rb', line 29 def close if @connection @pool.release(@connection) @connection = nil end end |
#closed? ⇒ Boolean
36 37 38 |
# File 'lib/db/context/session.rb', line 36 def closed? @connection.nil? end |
#connection ⇒ Object
Lazy initialize underlying connection.
24 25 26 |
# File 'lib/db/context/session.rb', line 24 def connection @connection ||= @pool.acquire end |
#connection? ⇒ Boolean
19 20 21 |
# File 'lib/db/context/session.rb', line 19 def connection? @connection != nil end |
#query(fragment = String.new, **parameters) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/db/context/session.rb', line 40 def query(fragment = String.new, **parameters) if parameters.empty? Query.new(self, fragment) else Query.new(self).interpolate(fragment, **parameters) end end |