Class: Simple::SQL::Connection::RawConnection
- Inherits:
-
Simple::SQL::Connection
- Object
- Simple::SQL::Connection
- Simple::SQL::Connection::RawConnection
- Defined in:
- lib/simple/sql/connection/raw_connection.rb
Constant Summary collapse
- SELF =
self
Constants included from Simple::SQL::ConnectionAdapter
Simple::SQL::ConnectionAdapter::Logging, Simple::SQL::ConnectionAdapter::Scope
Instance Attribute Summary collapse
-
#raw_connection ⇒ Object
readonly
Returns the value of attribute raw_connection.
Class Method Summary collapse
Instance Method Summary collapse
- #disconnect! ⇒ Object
-
#initialize(database_url) ⇒ RawConnection
constructor
A new instance of RawConnection.
- #transaction(&_block) ⇒ Object
Methods inherited from Simple::SQL::Connection
create, #duplicate, #insert, #reflection
Methods included from Simple::SQL::ConnectionAdapter
#all, #ask, #costs, #each, #exec, #locked, #print, #resolve_type
Constructor Details
#initialize(database_url) ⇒ RawConnection
Returns a new instance of RawConnection.
8 9 10 11 12 |
# File 'lib/simple/sql/connection/raw_connection.rb', line 8 def initialize(database_url) @database_url = database_url @raw_connection = PG::Connection.new(@database_url) ObjectSpace.define_finalizer(self, SELF.finalizer(@raw_connection)) end |
Instance Attribute Details
#raw_connection ⇒ Object (readonly)
Returns the value of attribute raw_connection.
6 7 8 |
# File 'lib/simple/sql/connection/raw_connection.rb', line 6 def raw_connection @raw_connection end |
Class Method Details
.finalizer(raw_connection) ⇒ Object
14 15 16 17 18 |
# File 'lib/simple/sql/connection/raw_connection.rb', line 14 def self.finalizer(raw_connection) proc do raw_connection.finish unless raw_connection.finished? end end |
Instance Method Details
#disconnect! ⇒ Object
20 21 22 23 24 25 |
# File 'lib/simple/sql/connection/raw_connection.rb', line 20 def disconnect! return unless @raw_connection @raw_connection.finish unless @raw_connection.finished? @raw_connection = nil end |
#transaction(&_block) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/simple/sql/connection/raw_connection.rb', line 27 def transaction(&_block) @tx_nesting_level ||= 0 # Notes: by using "ensure" (as opposed to rescue) we are rolling back # both when an exception was raised and when a value was thrown. This # also means we have to track whether or not to rollback. i.e. do roll # back when we yielded to &block but not otherwise. # # Also the transaction support is a bit limited: you cannot rollback. # Rolling back from inside a nested transaction would require SAVEPOINT # support; without the code is simpler at least :) if @tx_nesting_level == 0 exec "BEGIN" tx_started = true end @tx_nesting_level += 1 return_value = yield # Only commit if we started a transaction here. if tx_started exec "COMMIT" tx_committed = true end return_value ensure @tx_nesting_level -= 1 if tx_started && !tx_committed exec "ROLLBACK" end end |