summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts
diff options
context:
space:
mode:
authorspag <spag@golwen.net>2013-02-10 17:00:42 +0100
committerspag <spag@golwen.net>2013-02-10 17:00:42 +0100
commitd84e7208ae138e5e85a13286739e4364e02446e9 (patch)
treedcabb12a53e8f3e719f91ed2be356db2d195e87c /misc/freeswitch/scripts
parent70a6449fa176059e603a16a21699c8f5c60765e1 (diff)
database update or create added
Diffstat (limited to 'misc/freeswitch/scripts')
-rw-r--r--misc/freeswitch/scripts/common/database.lua40
1 files changed, 40 insertions, 0 deletions
diff --git a/misc/freeswitch/scripts/common/database.lua b/misc/freeswitch/scripts/common/database.lua
index 1f39135..345f69d 100644
--- a/misc/freeswitch/scripts/common/database.lua
+++ b/misc/freeswitch/scripts/common/database.lua
@@ -16,6 +16,7 @@ function Database.new(self, arg)
self.class = 'database';
self.log = arg.log;
self.conn = nil;
+ self.ignore_on_update = arg.ignore_on_update or {};
return object;
end
@@ -71,6 +72,45 @@ function Database.last_insert_id(self)
end
+function Database.insert_or_update(self, db_table, record, use_on_update)
+ ignore_on_update = ignore_on_update or self.ignore_on_update;
+ local record_sql_create = {};
+ local record_sql_update = {};
+
+ for key, value in pairs(record) do
+ if ignore_on_update[key] ~= false then
+ table.insert(record_sql_update, self:key_value(key, value));
+ end
+ table.insert(record_sql_create, self:key_value(key, value));
+ end
+
+ local sql_query = 'INSERT INTO `' .. db_table .. '` SET ' .. table.concat(record_sql_create, ', ') .. ' ON DUPLICATE KEY UPDATE ' .. table.concat(record_sql_update, ', ');
+
+ return self:query(sql_query);
+end
+
+
+function Database.key_value(self, key, value)
+ return self:escape(key, '`') .. ' = ' .. self:escape(value, '"');
+end
+
+
+function Database.escape(self, value, str_quotes)
+ str_quotes = str_quotes or '';
+ if type(value) == 'boolean' then
+ return tostring(value):upper();
+ elseif type(value) == 'number' then
+ return tostring(value);
+ elseif type(value) == 'string' then
+ return str_quotes .. value:gsub('"', '\\"'):gsub("'", "\\'") .. str_quotes;
+ elseif type(value) == 'table' and value.raw then
+ return tostring(value[1]);
+ else
+ return 'NULL';
+ end
+end
+
+
function Database.release(self)
if self.conn then
self.conn:release();