summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/common/node.lua
blob: 544ede98196f4886134688c6111f9d16a149dd4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
-- CommonModule: Node
--
module(...,package.seeall)

Node = {}

-- Create Node object
function Node.new(self, arg)
  arg = arg or {}
  object = arg.object or {}
  setmetatable(object, self)
  self.__index = self
  self.log = arg.log
  self.database = arg.database
  self.record = arg.record
  self.session = arg.session
  return object
end

-- Find Node account by name
function Node.find_by_id(self, node_id)
  
  if not tonumber(node_id) then
    return nil
  end

  local sql_query = 'SELECT * FROM `gs_nodes` WHERE `id`= ' .. node_id .. ' LIMIT 1';
  local record = nil

  self.database:query(sql_query, function(node_entry)
    record = node_entry
  end)

  if record then
    local node_object = Node:new(self);
    node_object.record = record
    
    return node_object
  end

  return nil
end

-- Find Node account by name
function Node.find_by_address(self, address)
  local sql_query = 'SELECT * FROM `gs_nodes` WHERE `ip_address`= "' .. tostring(address):gsub('[^A-F0-9%.%:]', '') .. '" LIMIT 1';
  local record = nil

  self.database:query(sql_query, function(node_entry)
    record = node_entry
  end)

  if record then
    local node_object = Node:new(self);
    node_object.record = record
    
    return node_object
  end

  return nil
end

-- List Nodes
function Node.all(self)
  local sql_query = 'SELECT * FROM `gs_nodes`';
  nodes = {};

  self.database:query(sql_query, function(node_entry)
    nodes[tonumber(node_entry.id)] = node_entry;
  end)

  return nodes
end