Class: BtrieveRecord

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

Overview

Represents a single btrieve record for a particular BTR table.

Constant Summary

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

Instance Method Summary collapse

Methods included from Btrieve

#btr_op, create_string_buffer

Constructor Details

#initialize(btrieve_table, data_buffer = nil) ⇒ BtrieveRecord

Initializes a btrieve record.



10
11
12
13
14
# File 'lib/btrieve/btrieve_record.rb', line 10

def initialize(btrieve_table, data_buffer=nil)
  @btrieve_table = btrieve_table
  @data_buffer = data_buffer.nil? ? Btrieve.create_string_buffer(@btrieve_table.schema[:record_size]) : data_buffer
  reset_physical_position
end

Instance Attribute Details

#btrieve_tableObject (readonly)

Returns the value of attribute btrieve_table.



6
7
8
# File 'lib/btrieve/btrieve_record.rb', line 6

def btrieve_table
  @btrieve_table
end

#data_bufferObject (readonly)

Returns the value of attribute data_buffer.



6
7
8
# File 'lib/btrieve/btrieve_record.rb', line 6

def data_buffer
  @data_buffer
end

#positionObject

Returns the value of attribute position.



7
8
9
# File 'lib/btrieve/btrieve_record.rb', line 7

def position
  @position
end

Instance Method Details

#[](key_name) ⇒ Object

Returns this record’s value of the column passed in.



59
60
61
62
63
64
65
# File 'lib/btrieve/btrieve_record.rb', line 59

def [](key_name)
  column = @btrieve_table.schema[:columns][key_name]
  return nil if(column.nil?)
  val = @data_buffer.unpack(column[:unpacker])[0]
  val = val.strip if val.respond_to?('strip')
  val
end

#[]=(key_name, value) ⇒ Object

Sets this record’s value for a particular column.



68
69
70
71
72
73
74
# File 'lib/btrieve/btrieve_record.rb', line 68

def []=(key_name, value)
  column = @btrieve_table.schema[:columns][key_name]
  packed_value = @btrieve_table.pack_value(column, value)
  offset = column[:offset]
  range = (offset..offset+packed_value.size-1)
  @data_buffer[range] = packed_value
end

#deleteObject

Deletes an existing btrieve record through the transactional BTR engine.



32
33
34
35
36
37
# File 'lib/btrieve/btrieve_record.rb', line 32

def delete
  @data_buffer[0..3]=@position
  btr_op(GET_DIRECT, @btrieve_table.pos_buffer, @data_buffer, NULL_BUFFER, SYSTEM_LOG_KEY)
  btr_op(DELETE, @btrieve_table.pos_buffer, NULL_BUFFER, NULL_BUFFER, NULL_KEY)
  reset_physical_position
end

#insertObject

Inserts a new btrieve record through the transactional BTR engine.



17
18
19
20
# File 'lib/btrieve/btrieve_record.rb', line 17

def insert
  btr_op(INSERT, @btrieve_table.pos_buffer, @data_buffer, NULL_BUFFER, SYSTEM_LOG_KEY)
  set_physical_position
end

#nil?Boolean

Determines if a record is nil.

Returns:

  • (Boolean)


77
78
79
# File 'lib/btrieve/btrieve_record.rb', line 77

def nil?()
  @data_buffer.gsub("\x00", "").size==0
end

#primary_keyObject

Returns this record’s primary key value. Supports composite keys.



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

def primary_key()
  values(@btrieve_table.primary_key)
end

#reset_physical_positionObject

TODO - documentation



99
100
101
# File 'lib/btrieve/btrieve_record.rb', line 99

def reset_physical_position
  @position = Btrieve.create_string_buffer(RECORD_POSITION_SIZE)
end

#set_physical_positionObject

TODO - documentation



92
93
94
95
96
# File 'lib/btrieve/btrieve_record.rb', line 92

def set_physical_position
  reset_physical_position
  btr_op(GET_POSITION, @btrieve_table.pos_buffer, @position, NULL_BUFFER, NULL_KEY)
  self
end

#to_sObject

Returns a string representation of this record.



82
83
84
# File 'lib/btrieve/btrieve_record.rb', line 82

def to_s()
  @btrieve_table.schema[:columns].keys.inject("[position=>'#{position.unpack('i')[0]}'"){|pretty_print, key| pretty_print << ", #{key}=>'#{self[key]}'"; pretty_print } << "]"
end

#updateObject

Updates an existing btrieve record through the transactional BTR engine.



23
24
25
26
27
28
29
# File 'lib/btrieve/btrieve_record.rb', line 23

def update
  new_state="#{@data_buffer}"
  @data_buffer[0..3]=@position
  btr_op(GET_DIRECT, @btrieve_table.pos_buffer, @data_buffer, NULL_BUFFER, SYSTEM_LOG_KEY)
  btr_op(UPDATE, @btrieve_table.pos_buffer, new_state, NULL_BUFFER, SYSTEM_LOG_KEY)
  set_physical_position
end

#values(column_names = nil) ⇒ Object

Returns hash of column-value pairs for this btrieve record, given an array of named columns. If the array is nil, the hash will contain ALL the column-value pairs defined by the schema.



41
42
43
44
45
46
# File 'lib/btrieve/btrieve_record.rb', line 41

def values(column_names=nil)
  if(column_names.nil?)
    column_names=@btrieve_table.schema[:columns].keys.sort.inject([]){|array,column|array<< column;array}
  end
  column_names.inject({}){|vals, key| vals[key]=self[key]; vals}
end

#values=(column_values) ⇒ Object

Sets the column valuesof this record based on the values passed through the ‘column_values’ map.



49
50
51
# File 'lib/btrieve/btrieve_record.rb', line 49

def values=(column_values)
  column_values.each{ |key, value| self[key]=value }
end

#versionObject

Returns this record’s version as a MD5 Digest value.



87
88
89
# File 'lib/btrieve/btrieve_record.rb', line 87

def version()
  Digest::MD5.hexdigest(@data_buffer)
end