Class: DB::Context::Generic
- Inherits:
-
Object
- Object
- DB::Context::Generic
- Defined in:
- lib/db/context/generic.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.
-
#connection ⇒ Object
Lazy initialize underlying connection.
- #connection? ⇒ Boolean
-
#initialize(pool, **options) ⇒ Generic
constructor
Iniitalize the query context attached to the given connection pool.
- #query(fragment = String.new, **parameters) ⇒ Object
Constructor Details
#initialize(pool, **options) ⇒ Generic
Iniitalize the query context attached to the given connection pool.
30 31 32 33 |
# File 'lib/db/context/generic.rb', line 30 def initialize(pool, **) @pool = pool @connection = nil end |
Instance Method Details
#call(statement, **options) ⇒ Object
Send a query to the server.
66 67 68 69 70 71 72 |
# File 'lib/db/context/generic.rb', line 66 def call(statement, **) connection.send_query(statement, **) yield connection if block_given? ensure self.close end |
#clause(fragment = String.new) ⇒ Object
60 61 62 |
# File 'lib/db/context/generic.rb', line 60 def clause(fragment = String.new) Query.new(self, fragment) end |
#close ⇒ Object
Flush the connection and then return it to the connection pool.
45 46 47 48 49 50 |
# File 'lib/db/context/generic.rb', line 45 def close if @connection @pool.release(@connection) @connection = nil end end |
#connection ⇒ Object
Lazy initialize underlying connection.
40 41 42 |
# File 'lib/db/context/generic.rb', line 40 def connection @connection ||= @pool.acquire end |
#connection? ⇒ Boolean
35 36 37 |
# File 'lib/db/context/generic.rb', line 35 def connection? @connection != nil end |
#query(fragment = String.new, **parameters) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/db/context/generic.rb', line 52 def query(fragment = String.new, **parameters) if parameters.empty? Query.new(self, fragment) else Query.new(self).interpolate(fragment, **parameters) end end |