Sha256: a2d0ee6578da090500fcba23a2749d1c4839ab5d122fbca1ade148d4d6ac7103

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

require 'digest'
require 'digest/sha2'

module DataObjects

  class Transaction

    # The host name. Note, this relies on the host name being configured and resolvable using DNS
    HOST = "#{Socket::gethostbyname(Socket::gethostname)[0]}" rescue "localhost"
    @@counter = 0

    # The connection object allocated for this transaction
    attr_reader :connection
    # A unique ID for this transaction
    attr_reader :id

    # Instantiate the Transaction subclass that's appropriate for this uri scheme
    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

    #
    # Creates a Transaction bound to a connection for the given DataObjects::URI
    #
    def initialize(uri)
      @connection = DataObjects::Connection.new(uri)
      @id = Digest::SHA256.hexdigest("#{HOST}:#{$$}:#{Time.now.to_f}:#{@@counter += 1}")
    end

    # Close the connection for this Transaction
    def close
      @connection.close
    end

    def begin
      cmd = "BEGIN"
      connection.create_command(cmd).execute_non_query
    end

    def commit
      cmd = "COMMIT"
      connection.create_command(cmd).execute_non_query
    end

    def rollback
      cmd = "ROLLBACK"
      connection.create_command(cmd).execute_non_query
    end

    def prepare; not_implemented; end;
    def begin_prepared; not_implemented; end;
    def commit_prepared; not_implemented; end;
    def rollback_prepared; not_implemented; end;
    def prepare; not_implemented; end;

  private
    def not_implemented
      raise NotImplementedError
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
data_objects-0.10.2 lib/data_objects/transaction.rb
data_objects-0.10.1 lib/data_objects/transaction.rb
data_objects-0.10.0 lib/data_objects/transaction.rb