summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/common/database.lua
diff options
context:
space:
mode:
authorStefan Wintermeyer <stefan.wintermeyer@amooma.de>2013-02-12 13:53:28 +0100
committerStefan Wintermeyer <stefan.wintermeyer@amooma.de>2013-02-12 13:53:28 +0100
commitc9066760fd1f5f2f892ce2be5cf2a83bb5210246 (patch)
tree82ecdd528e803ccd1b469dc13482e06cf8ea0b8e /misc/freeswitch/scripts/common/database.lua
parent0b97717b2171820dea41de8df705f8f0e4b71464 (diff)
parentfb66a5e5a4c5d5f9eac4a5e8de6a286482cb55d5 (diff)
Release a new beta.5.1-beta4
Diffstat (limited to 'misc/freeswitch/scripts/common/database.lua')
-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();