summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/common/configuration_table.lua
blob: 1b8d8b78d28c088ce5c4b8937589c934c8f426b8 (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
74
75
76
77
78
79
80
81
82
83
-- Gemeinschaft 5 module: configuration table
-- (c) AMOOMA GmbH 2013
-- 

module(...,package.seeall)


function cast(variable_type, value, default)
  require 'common.str';

  if variable_type == 'boolean' then
    return common.str.to_b(value);
  elseif variable_type == 'integer' then
    if default and not tonumber(value) then
      return default;
    end
    return common.str.to_i(value);
  elseif variable_type == 'float' then
    if default and not tonumber(value) then
      return default;
    end
    return common.str.to_n(value);
  elseif variable_type == 'string' then
    if default and not value then
      return default;
    end
    return common.str.to_s(value);
  elseif variable_type == 'array' then
    if default and not value then
      return default;
    end
    return common.str.to_a(value, ',');
  end
end

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

  defaults = defaults or {};

  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 = defaults[1] or {}
  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] = defaults[p_section] or {};
    end

    root[p_section][p_name] = cast(p_class_type, parameters.value);
  end)

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


function settings(database, table_name, key, value, defaults)
  local sql_query = 'SELECT * FROM ' .. database:escape(table_name, '`') .. ' WHERE ' .. database:escape(key, '`') .. ' = ' .. database:escape(value, '"');

  local settings_entries = defaults or {};
  database:query(sql_query, function(record)
    settings_entries[record.name] = cast(record.class_type:lower(), record.value, settings_entries[record.name]);
  end);

  return settings_entries;
end