Class: Unidata::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/unidata/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_dirObject (readonly)

Returns the value of attribute data_dir.



3
4
5
# File 'lib/unidata/connection.rb', line 3

def data_dir
  @data_dir
end

#hostObject (readonly)

Returns the value of attribute host.



3
4
5
# File 'lib/unidata/connection.rb', line 3

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



3
4
5
# File 'lib/unidata/connection.rb', line 3

def password
  @password
end

#userObject (readonly)

Returns the value of attribute user.



3
4
5
# File 'lib/unidata/connection.rb', line 3

def user
  @user
end

Instance Method Details

#closeObject



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



100
101
102
103
104
# File 'lib/unidata/connection.rb', line 100

def delete_record(filename, record_id)
  open_file(filename) do |file|
    file.delete_record(record_id)
  end
end

#exists?(filename, record_id) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
# File 'lib/unidata/connection.rb', line 34

def exists?(filename, record_id)
  open_file(filename) do |file|
    begin
      !!file.read_field(record_id, 0)
    rescue Unidata::UniFileException
      false
    end
  end
end

#openObject



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

Returns:

  • (Boolean)


12
13
14
# File 'lib/unidata/connection.rb', line 12

def open?
  !@session.nil? && @session.is_active
end

#read(filename, record_id) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/unidata/connection.rb', line 44

def read(filename, record_id)
  open_file(filename) do |file|
    begin
      Unidata::UniDynArray.new(file.read(record_id))
    rescue Unidata::UniFileException
      nil
    end
  end
end

#read_field(filename, record_id, field) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/unidata/connection.rb', line 54

def read_field(filename, record_id, field)
  open_file(filename) do |file|
    begin
      file.read_field(record_id, field).to_s
    rescue Unidata::UniFileException
      nil
    end
  end
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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/unidata/connection.rb', line 78

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



64
65
66
67
68
69
70
# File 'lib/unidata/connection.rb', line 64

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



72
73
74
75
76
# File 'lib/unidata/connection.rb', line 72

def write_field(filename, record_id, value, field)
  open_file(filename) do |file|
    file.write_field(record_id, value, field)
  end
end