Class: DataObjects::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/data_objects/transaction.rb

Direct Known Subclasses

SavePoint

Constant Summary collapse

HOST =

The host name. Note, this relies on the host name being configured and resolvable using DNS

"#{Socket::gethostbyname(Socket::gethostname)[0]}" rescue "localhost"
@@counter =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, connection = nil) ⇒ Transaction

Creates a Transaction bound to a connection for the given DataObjects::URI



27
28
29
30
31
# File 'lib/data_objects/transaction.rb', line 27

def initialize(uri, connection = nil)
  @connection = connection || DataObjects::Connection.new(uri)
  # PostgreSQL can't handle the full 64 bytes.  This should be enough for everyone.
  @id = Digest::SHA256.hexdigest("#{HOST}:#{$$}:#{Time.now.to_f}:#{@@counter += 1}")[0..-2]
end

Instance Attribute Details

#connectionObject (readonly)

The connection object allocated for this transaction



14
15
16
# File 'lib/data_objects/transaction.rb', line 14

def connection
  @connection
end

#idObject (readonly)

A unique ID for this transaction



16
17
18
# File 'lib/data_objects/transaction.rb', line 16

def id
  @id
end

Class Method Details

.create_for_uri(uri) ⇒ Object

Instantiate the Transaction subclass that’s appropriate for this uri scheme



19
20
21
22
# File 'lib/data_objects/transaction.rb', line 19

def self.create_for_uri(uri)
  uri = uri.is_a?(String) ? URI::parse(uri) : uri
  DataObjects.const_get(uri.scheme.capitalize)::Transaction.new(uri)
end

Instance Method Details

#beginObject



38
39
40
# File 'lib/data_objects/transaction.rb', line 38

def begin
  run "BEGIN"
end

#begin_preparedObject



51
# File 'lib/data_objects/transaction.rb', line 51

def begin_prepared; not_implemented; end

#closeObject

Close the connection for this Transaction



34
35
36
# File 'lib/data_objects/transaction.rb', line 34

def close
  @connection.close
end

#commitObject



42
43
44
# File 'lib/data_objects/transaction.rb', line 42

def commit
  run "COMMIT"
end

#commit_preparedObject



52
# File 'lib/data_objects/transaction.rb', line 52

def commit_prepared; not_implemented; end

#prepareObject



50
# File 'lib/data_objects/transaction.rb', line 50

def prepare; not_implemented; end

#rollbackObject



46
47
48
# File 'lib/data_objects/transaction.rb', line 46

def rollback
  run "ROLLBACK"
end

#rollback_preparedObject



53
# File 'lib/data_objects/transaction.rb', line 53

def rollback_prepared; not_implemented; end