Class: Corona::DB

Inherits:
Object
  • Object
show all
Defined in:
lib/corona/db.rb

Instance Method Summary collapse

Constructor Details

#initializeDB

Returns a new instance of DB.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/corona/db.rb', line 5

def initialize 
  @db = Sequel.sqlite(CFG.db, :max_connections => 1, :pool_timeout => 60)

  @db.create_table :device do
    primary_key :id
    String      :ip
    String      :ptr
    String      :model
    String      :oid_ifDescr
    Boolean     :active
    Time        :first_seen
    Time        :last_seen
    String      :oid_sysName
    String      :oid_sysLocation
    String      :oid_sysDescr
  end unless @db.table_exists? :device
end

Instance Method Details

#[](primary_key) ⇒ Object



40
41
42
# File 'lib/corona/db.rb', line 40

def [] primary_key
  @db[:device].where('id == ?', primary_key).first
end

#add(record) ⇒ Object



26
27
28
29
30
31
# File 'lib/corona/db.rb', line 26

def add record
  record[:first_seen] = record[:last_seen] = Time.now.utc
  record[:active] = true
  #Log.debug "adding: #{record}"
  @db[:device].insert record
end

#old(primary_key, oid_sysName) ⇒ Object



44
45
46
47
48
# File 'lib/corona/db.rb', line 44

def old primary_key, oid_sysName
  ip       = self[primary_key]
  sysName  = @db[:device].where('oid_sysName == ?', oid_sysName).first
  [ip, sysName]
end

#update(record, where) ⇒ Object



33
34
35
36
37
38
# File 'lib/corona/db.rb', line 33

def update record, where
  record[:last_seen] = Time.now.utc
  record[:active] = true
  #Log.debug "updating (where: #{where}): #{record}"
  @db[:device].where('? == ?', where.first, where.last).update record
end