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 Attribute Summary collapse
-
#connection ⇒ Object
readonly
The underlying connection.
Instance Method Summary collapse
-
#call(statement, **options) ⇒ Object
Send a query to the server and read the next result.
- #clause(fragment = String.new) ⇒ Object
-
#close ⇒ Object
Flush the connection and then return it to the connection pool.
-
#flush ⇒ Object
Flush all outstanding results.
-
#initialize(pool, **options) ⇒ Session
constructor
Iniitalize the query context attached to the given connection pool.
-
#next_result ⇒ Object
Read the next result.
- #query(fragment = String.new, **parameters) ⇒ Object
-
#results ⇒ Object
Enumerate all results.
-
#send_query(statement, **options) ⇒ Object
Send a query to the server.
Constructor Details
#initialize(pool, **options) ⇒ Session
Iniitalize the query context attached to the given connection pool.
30 31 32 33 |
# File 'lib/db/context/session.rb', line 30 def initialize(pool, **) @pool = pool @connection = pool.acquire end |
Instance Attribute Details
#connection ⇒ Object (readonly)
The underlying connection.
36 37 38 |
# File 'lib/db/context/session.rb', line 36 def connection @connection end |
Instance Method Details
#call(statement, **options) ⇒ Object
Send a query to the server and read the next result.
76 77 78 79 80 81 |
# File 'lib/db/context/session.rb', line 76 def call(statement, **) # Console.logger.info(self, statement) @connection.send_query(statement, **) return @connection.next_result end |
#clause(fragment = String.new) ⇒ Object
49 50 51 |
# File 'lib/db/context/session.rb', line 49 def clause(fragment = String.new) Query.new(self, fragment) end |
#close ⇒ Object
Flush the connection and then return it to the connection pool.
39 40 41 42 43 44 45 46 47 |
# File 'lib/db/context/session.rb', line 39 def close if @connection self.flush @pool.release(@connection) @connection = nil end end |
#flush ⇒ Object
Flush all outstanding results.
95 96 97 98 |
# File 'lib/db/context/session.rb', line 95 def flush until @connection.next_result.nil? end end |
#next_result ⇒ Object
Read the next result. Sending a query usually generates 1 or more results.
70 71 72 |
# File 'lib/db/context/session.rb', line 70 def next_result @connection.next_result end |
#query(fragment = String.new, **parameters) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/db/context/session.rb', line 53 def query(fragment = String.new, **parameters) if parameters.empty? Query.new(self, fragment) else Query.new(self).interpolate(fragment, **parameters) end end |
#results ⇒ Object
Enumerate all results.
86 87 88 89 90 91 92 |
# File 'lib/db/context/session.rb', line 86 def results while result = self.next_result yield result end return nil end |
#send_query(statement, **options) ⇒ Object
Send a query to the server.
63 64 65 66 |
# File 'lib/db/context/session.rb', line 63 def send_query(statement, **) # Console.logger.info(self, statement) @connection.send_query(statement, **) end |