Class: Unidata::Connection
- Inherits:
-
Object
- Object
- Unidata::Connection
- Defined in:
- lib/unidata/connection.rb
Instance Attribute Summary collapse
-
#data_dir ⇒ Object
readonly
Returns the value of attribute data_dir.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #close ⇒ Object
- #delete_record(filename, record_id) ⇒ Object
- #exists?(filename, record_id) ⇒ Boolean
-
#initialize(user, password, host, data_dir) ⇒ Connection
constructor
A new instance of Connection.
- #open ⇒ Object
- #open? ⇒ Boolean
- #read(filename, record_id) ⇒ Object
- #read_field(filename, record_id, field) ⇒ Object
- #select(filename, condition = "", list_number = 0) ⇒ Object
- #with_record_lock(filename, record_id, lock_flag = 1) ⇒ Object
- #write(filename, record_id, record) ⇒ Object
- #write_field(filename, record_id, value, field) ⇒ Object
Constructor Details
#initialize(user, password, host, data_dir) ⇒ Connection
Returns a new instance of Connection.
5 6 7 8 9 10 |
# File 'lib/unidata/connection.rb', line 5 def initialize(user, password, host, data_dir) @user = user @password = password @host = host @data_dir = data_dir end |
Instance Attribute Details
#data_dir ⇒ Object (readonly)
Returns the value of attribute data_dir.
3 4 5 |
# File 'lib/unidata/connection.rb', line 3 def data_dir @data_dir end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
3 4 5 |
# File 'lib/unidata/connection.rb', line 3 def host @host end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
3 4 5 |
# File 'lib/unidata/connection.rb', line 3 def password @password end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
3 4 5 |
# File 'lib/unidata/connection.rb', line 3 def user @user end |
Instance Method Details
#close ⇒ Object
22 23 24 25 |
# File 'lib/unidata/connection.rb', line 22 def close Unidata.unijava.close_session @session @session = nil end |
#delete_record(filename, record_id) ⇒ Object
110 111 112 113 114 |
# File 'lib/unidata/connection.rb', line 110 def delete_record(filename, record_id) open_file(filename) do |file| file.delete_record(record_id) end end |
#exists?(filename, record_id) ⇒ Boolean
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/unidata/connection.rb', line 34 def exists?(filename, record_id) exists = false open_file(filename) do |file| begin file.read_field(record_id, 0) exists = true rescue Unidata::UniFileException end end exists end |
#open ⇒ Object
16 17 18 19 20 |
# File 'lib/unidata/connection.rb', line 16 def open @session = Unidata.unijava.open_session @session.set_data_source_type "UNIDATA" @session.connect(host, user, password, data_dir) end |
#open? ⇒ Boolean
12 13 14 |
# File 'lib/unidata/connection.rb', line 12 def open? !@session.nil? && @session.is_active end |
#read(filename, record_id) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/unidata/connection.rb', line 48 def read(filename, record_id) record = nil open_file(filename) do |file| begin record = Unidata::UniDynArray.new(file.read(record_id)) rescue Unidata::UniFileException end end record end |
#read_field(filename, record_id, field) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/unidata/connection.rb', line 61 def read_field(filename, record_id, field) value = nil open_file(filename) do |file| begin value = file.read_field(record_id, field).to_s rescue Unidata::UniFileException end end value end |
#select(filename, condition = "", list_number = 0) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/unidata/connection.rb', line 27 def select(filename, condition="", list_number=0) command = "SELECT #{filename} TO #{list_number}" command << " WITH #{condition}" unless condition.empty? @session.command(command).exec SelectList.new(@session.select_list list_number) end |
#with_record_lock(filename, record_id, lock_flag = 1) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/unidata/connection.rb', line 88 def with_record_lock(filename, record_id, lock_flag=1) open_file(filename) do |file| retry_count = 0 begin file.lock_record(record_id, lock_flag) yield rescue Unidata::UniFileException # try to obtain a record lock at most 3 times, then give up if retry_count < 2 sleep 5 retry_count += 1 retry else raise end ensure file.unlock_record(record_id) end end end |
#write(filename, record_id, record) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/unidata/connection.rb', line 74 def write(filename, record_id, record) record = record.to_unidata unless record.kind_of?(Unidata::UniDynArray) open_file(filename) do |file| file.write(record_id, record) end end |
#write_field(filename, record_id, value, field) ⇒ Object
82 83 84 85 86 |
# File 'lib/unidata/connection.rb', line 82 def write_field(filename, record_id, value, field) open_file(filename) do |file| file.write_field(record_id, value, field) end end |