From 2c1136bd6d3a772f13cf4aacdca0d17baeb79ffe Mon Sep 17 00:00:00 2001 From: Peter Kozak Date: Tue, 26 Mar 2013 15:52:02 +0100 Subject: defaults added --- .../scripts/common/configuration_table.lua | 59 ++++++++++++++++++---- 1 file changed, 48 insertions(+), 11 deletions(-) (limited to 'misc/freeswitch/scripts/common') diff --git a/misc/freeswitch/scripts/common/configuration_table.lua b/misc/freeswitch/scripts/common/configuration_table.lua index 85bc014..1b8d8b7 100644 --- a/misc/freeswitch/scripts/common/configuration_table.lua +++ b/misc/freeswitch/scripts/common/configuration_table.lua @@ -4,12 +4,43 @@ 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) +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 .. '"'; @@ -17,7 +48,7 @@ function get(database, entity, section) sql_query = sql_query .. ' AND `section` = "' .. section .. '"'; end - local root = {} + local root = defaults[1] or {} local parameter_class = ''; database:query(sql_query, function(parameters) @@ -26,21 +57,27 @@ function get(database, entity, section) local p_name = common.str.strip(parameters.name); if not root[p_section] then - root[p_section] = {}; + root[p_section] = defaults[p_section] or {}; 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 + root[p_section][p_name] = cast(p_class_type, parameters.value); end) if section then - return root[section]; + 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 -- cgit v1.2.3