summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/common/configuration_table.lua
blob: 85bc0140d8df5eabd50f056dec40c2df78ab270c (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
-- Gemeinschaft 5 module: configuration table
-- (c) AMOOMA GmbH 2013
-- 

module(...,package.seeall)

-- retrieve configuration from database
function get(database, entity, section)
  if not database or not entity then
    return {};
  end

  require 'common.str'

  local sql_query = 'SELECT * FROM `gs_parameters` WHERE `entity` = "' .. entity .. '"';
  if section then
    sql_query = sql_query .. ' AND `section` = "' .. section .. '"';
  end

  local root = {}
  local parameter_class = '';

  database:query(sql_query, function(parameters)
    local p_section = common.str.strip(parameters.section):lower();
    local p_class_type = common.str.strip(parameters.class_type):lower();
    local p_name = common.str.strip(parameters.name);

    if not root[p_section] then
      root[p_section] = {};
    end

    if p_class_type == 'boolean' then
      root[p_section][p_name] = common.str.to_b(parameters.value);
    elseif p_class_type == 'integer' then
      root[p_section][p_name] = common.str.to_i(parameters.value);
    else
      root[p_section][p_name] = tostring(parameters.value);
    end
  end)

  if section then
    return root[section];
  end
  
  return root;
end