Class: BtrieveSession

Inherits:
Object
  • Object
show all
Includes:
Btrieve
Defined in:
lib/btrieve/btrieve_session.rb

Overview

Concept of a BTR session. Beware, this implementation is NOT thread safe!

Constant Summary collapse

@@instance =
nil

Constants included from BtrCodes

BtrCodes::ABORT_TRANSACTION, BtrCodes::ACCELERATED_MODE, BtrCodes::BEGIN_TRANSACTION, BtrCodes::CLOSE, BtrCodes::DELETE, BtrCodes::END_TRANSACTION, BtrCodes::EOF, BtrCodes::EXCEPTION_MESSAGES, BtrCodes::EXCLUSIVE_MODE, BtrCodes::GET_DIRECT, BtrCodes::GET_EQUAL, BtrCodes::GET_EQUAL_KEY, BtrCodes::GET_FIRST, BtrCodes::GET_GREATER_THAN_OR_EQUAL, BtrCodes::GET_LESS_THAN_OR_EQUAL, BtrCodes::GET_NEXT, BtrCodes::GET_NEXT_EXTENDED, BtrCodes::GET_NEXT_KEY, BtrCodes::GET_POSITION, BtrCodes::GET_PREVIOUS, BtrCodes::INSERT, BtrCodes::KEY_NOT_FOUND, BtrCodes::LOCAL_ACCELERATED_MODE, BtrCodes::LOCAL_EXCLUSIVE_MODE, BtrCodes::LOCAL_NORMAL_MODE, BtrCodes::LOCAL_READ_ONLY_MODE, BtrCodes::MAX_DATATYPE, BtrCodes::NORMAL_MODE, BtrCodes::NO_CURRENCY_CHANGE, BtrCodes::NO_LOGICAL_CURRENCY_KEY, BtrCodes::NULL_BUFFER, BtrCodes::NULL_KEY, BtrCodes::OK, BtrCodes::OPEN, BtrCodes::POS_BLOCK_SIZE, BtrCodes::READ_ONLY_MODE, BtrCodes::RECORD_POSITION_SIZE, BtrCodes::REMOTE_ACCELERATED_MODE, BtrCodes::REMOTE_EXCLUSIVE_MODE, BtrCodes::REMOTE_NORMAL_MODE, BtrCodes::REMOTE_READ_ONLY_MODE, BtrCodes::RESET, BtrCodes::STEP_NEXT, BtrCodes::STEP_NEXT_EXTENDED, BtrCodes::STOP, BtrCodes::SYSTEM_LOG_KEY, BtrCodes::UPDATE, BtrCodes::VERIFY_MODE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Btrieve

#btr_op, create_string_buffer

Instance Attribute Details

#btrieve_schemaObject (readonly)

Returns the value of attribute btrieve_schema.



4
5
6
# File 'lib/btrieve/btrieve_session.rb', line 4

def btrieve_schema
  @btrieve_schema
end

#cacheObject (readonly)

Returns the value of attribute cache.



4
5
6
# File 'lib/btrieve/btrieve_session.rb', line 4

def cache
  @cache
end

#client_idObject (readonly)

Returns the value of attribute client_id.



4
5
6
# File 'lib/btrieve/btrieve_session.rb', line 4

def client_id
  @client_id
end

#dbObject

Returns the value of attribute db.



5
6
7
# File 'lib/btrieve/btrieve_session.rb', line 5

def db
  @db
end

Class Method Details

.abort_transactionObject

Aborts a transaction when something goes wrong. The client has the responsibility of calling this method when a transaction was started without a block AND some unexpected event occured and the client wants to prevent any changes to the persistence to be commited.



61
62
63
# File 'lib/btrieve/btrieve_session.rb', line 61

def BtrieveSession.abort_transaction()
  @@instance.btr_op(ABORT_TRANSACTION, NULL_BUFFER, NULL_BUFFER, NULL_BUFFER, NULL_KEY)
end

.begin_transactionObject

Delineates the begining of a transaction boundary. If a client calls this method explicitely, the client also has the responsibility of explicitely ending the transaction once done, or abort the transaction if things did not go well.



48
49
50
# File 'lib/btrieve/btrieve_session.rb', line 48

def BtrieveSession.begin_transaction()
  @@instance.btr_op(BEGIN_TRANSACTION, NULL_BUFFER, NULL_BUFFER, NULL_BUFFER, NULL_KEY)
end

.create_session(db) ⇒ Object

Factory of BTR sessions. A block can be passed into it, or alternatively the factory returns the session instance to the caller, who then must call ‘dispose’ on it when done with it. The ‘host’ argument can be a named server or its IP address. The ‘data_share’ argument is typically the share name of the data folder. The ‘data_share’ argument can also be the database name when “IDS” is used on the client.



14
15
16
17
18
# File 'lib/btrieve/btrieve_session.rb', line 14

def BtrieveSession.create_session(db)
  raise "Cannot create more than one instance of session." if(@@instance != nil)
  @@instance = new(db)
  @@instance.set_schema(BtrieveSchema.new())
end

.destroy_sessionObject

Disposes of the session object, releasing any and all resources consumed/locked on the server.



26
27
28
29
# File 'lib/btrieve/btrieve_session.rb', line 26

def BtrieveSession.destroy_session()
  @@instance.btr_op(RESET, NULL_BUFFER, NULL_BUFFER, NULL_BUFFER, NULL_KEY)
  @@instance=nil
end

.end_transactionObject

Delineates the end of a transaction boundary. The client has the responsibility of calling this method when a transaction was started without a block.



54
55
56
# File 'lib/btrieve/btrieve_session.rb', line 54

def BtrieveSession.end_transaction()
  @@instance.btr_op(END_TRANSACTION, NULL_BUFFER, NULL_BUFFER, NULL_BUFFER, NULL_KEY)
end

.get_sessionObject

Returns the session instance created.



21
22
23
# File 'lib/btrieve/btrieve_session.rb', line 21

def BtrieveSession.get_session()
  @@instance
end

.transaction(&block) ⇒ Object

Wraps and executes the block passed in inside a transaction. The transaction is aborted if the block raises an exception.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/btrieve/btrieve_session.rb', line 33

def BtrieveSession.transaction(&block)
  begin
    begin_transaction
    yield
    end_transaction
  rescue Exception => exception
    # Oops! We must roll back the whole shebang!
    abort_transaction
    raise "PSQL TX ABORTED!!! - #{exception.message}\n#{exception.backtrace.join("\n\t")}"
  end
end

Instance Method Details

#set_schema(schema) ⇒ Object



65
66
67
# File 'lib/btrieve/btrieve_session.rb', line 65

def set_schema(schema)
  @btrieve_schema=schema
end