aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mod_storage_sql.lua
diff options
context:
space:
mode:
authorWaqas Hussain <waqas20@gmail.com>2010-12-27 06:10:35 +0500
committerWaqas Hussain <waqas20@gmail.com>2010-12-27 06:10:35 +0500
commitef3729312cb9d7394f4dbf01cd717fe61dac7698 (patch)
treec790e3120d446225d9153c35396fa9c061dad60f /plugins/mod_storage_sql.lua
parentaa3093c81a91f9bc37e586d54dad36f312b62920 (diff)
downloadprosody-ef3729312cb9d7394f4dbf01cd717fe61dac7698.tar.gz
prosody-ef3729312cb9d7394f4dbf01cd717fe61dac7698.zip
mod_storage_sql: Quote identifiers in SQL with backquotes, and use the empty string for NULL, and '=' instead of 'IS' for comparison, to work with MySQL's limitations...
Diffstat (limited to 'plugins/mod_storage_sql.lua')
-rw-r--r--plugins/mod_storage_sql.lua30
1 files changed, 15 insertions, 15 deletions
diff --git a/plugins/mod_storage_sql.lua b/plugins/mod_storage_sql.lua
index 3d5aa0a3..e5d5e86c 100644
--- a/plugins/mod_storage_sql.lua
+++ b/plugins/mod_storage_sql.lua
@@ -49,11 +49,11 @@ do -- process options to get a db connection
connection = dbh;
if params.driver == "SQLite3" then -- auto initialize
- local stmt = assert(connection:prepare("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='Prosody';"));
+ local stmt = assert(connection:prepare("SELECT COUNT(*) FROM `sqlite_master` WHERE `type`='table' AND `name`='Prosody';"));
local ok = assert(stmt:execute());
local count = stmt:fetch()[1];
if count == 0 then
- local stmt = assert(connection:prepare("CREATE TABLE Prosody (host TEXT, user TEXT, store TEXT, key TEXT, subkey TEXT, type TEXT, value TEXT);"));
+ local stmt = assert(connection:prepare("CREATE TABLE `Prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `subkey` TEXT, `type` TEXT, `value` TEXT);"));
assert(stmt:execute());
assert(connection:commit());
module:log("debug", "Initialized new SQLite3 database");
@@ -89,7 +89,7 @@ local function getsql(sql, ...)
local stmt, err = connection:prepare(sql);
if not stmt then return nil, err; end
-- run query
- local ok, err = stmt:execute(host, user, store, ...);
+ local ok, err = stmt:execute(host or "", user or "", store or "", ...);
if not ok then return nil, err; end
return stmt;
@@ -115,7 +115,7 @@ local keyval_store = {};
keyval_store.__index = keyval_store;
function keyval_store:get(username)
user,store = username,self.store;
- local stmt, err = getsql("SELECT * FROM Prosody WHERE host IS ? AND user IS ? AND store IS ? AND subkey IS NULL");
+ local stmt, err = getsql("SELECT * FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `subkey`=''");
if not stmt then return nil, err; end
local haveany;
@@ -137,15 +137,15 @@ end
function keyval_store:set(username, data)
user,store = username,self.store;
-- start transaction
- local affected, err = setsql("DELETE FROM Prosody WHERE host IS ? AND user IS ? AND store IS ? AND subkey IS NULL");
+ local affected, err = setsql("DELETE FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `subkey`=''");
if data and next(data) ~= nil then
local extradata = {};
for key, value in pairs(data) do
- if type(key) == "string" then
+ if type(key) == "string" and key ~= "" then
local t, value = serialize(value);
if not t then return rollback(t, value); end
- local ok, err = setsql("INSERT INTO Prosody (host,user,store,key,type,value) VALUES (?,?,?,?,?,?)", key, t, value);
+ local ok, err = setsql("INSERT INTO `Prosody` (`host`,`user`,`store`,`key`,`type`,`value`,`subkey`) VALUES (?,?,?,?,?,?,'')", key or "", t, value);
if not ok then return rollback(ok, err); end
else
extradata[key] = value;
@@ -154,7 +154,7 @@ function keyval_store:set(username, data)
if next(extradata) ~= nil then
local t, extradata = serialize(extradata);
if not t then return rollback(t, extradata); end
- local ok, err = setsql("INSERT INTO Prosody (host,user,store,key,type,value) VALUES (?,?,?,?,?,?)", nil, t, extradata);
+ local ok, err = setsql("INSERT INTO `Prosody` (`host`,`user`,`store`,`key`,`type`,`value`,`subkey`) VALUES (?,?,?,?,?,?,'')", "", t, extradata);
if not ok then return rollback(ok, err); end
end
end
@@ -165,7 +165,7 @@ local map_store = {};
map_store.__index = map_store;
function map_store:get(username, key)
user,store = username,self.store;
- local stmt, err = getsql("SELECT * FROM Prosody WHERE host IS ? AND user IS ? AND store IS ? AND key IS ?", key);
+ local stmt, err = getsql("SELECT * FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or "");
if not stmt then return nil, err; end
local haveany;
@@ -187,15 +187,15 @@ end
function map_store:set(username, key, data)
user,store = username,self.store;
-- start transaction
- local affected, err = setsql("DELETE FROM Prosody WHERE host IS ? AND user IS ? AND store IS ? AND key IS ?", key);
+ local affected, err = setsql("DELETE FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or "");
if data and next(data) ~= nil then
local extradata = {};
for subkey, value in pairs(data) do
- if type(subkey) == "string" then
+ if type(subkey) == "string" and key ~= "" then
local t, value = serialize(value);
if not t then return rollback(t, value); end
- local ok, err = setsql("INSERT INTO Prosody (host,user,store,key,subkey,type,value) VALUES (?,?,?,?,?,?)", key, subkey, t, value);
+ local ok, err = setsql("INSERT INTO `Prosody` (`host`,`user`,`store`,`key`,`subkey`,`type`,`value`) VALUES (?,?,?,?,?,?,?)", key or "", subkey or "", t, value);
if not ok then return rollback(ok, err); end
else
extradata[subkey] = value;
@@ -204,7 +204,7 @@ function map_store:set(username, key, data)
if next(extradata) ~= nil then
local t, extradata = serialize(extradata);
if not t then return rollback(t, extradata); end
- local ok, err = setsql("INSERT INTO Prosody (host,user,store,key,subkey,type,value) VALUES (?,?,?,?,?,?)", key, nil, t, extradata);
+ local ok, err = setsql("INSERT INTO `Prosody` (`host`,`user`,`store`,`key`,`subkey`,`type`,`value`) VALUES (?,?,?,?,?,?,?)", key or "", "", t, extradata);
if not ok then return rollback(ok, err); end
end
end
@@ -219,10 +219,10 @@ function list_store:scan(username, from, to, jid, typ)
local cols = {"from", "to", "jid", "typ"};
local vals = { from , to , jid , typ };
local stmt, err;
- local query = "SELECT * FROM ProsodyArchive WHERE host IS ? AND user IS ? AND store IS ?";
+ local query = "SELECT * FROM `ProsodyArchive` WHERE `host`=? AND `user`=? AND `store`=?";
query = query.." ORDER BY time";
- --local stmt, err = getsql("SELECT * FROM Prosody WHERE host IS ? AND user IS ? AND store IS ? AND key IS ?", key);
+ --local stmt, err = getsql("SELECT * FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or "");
return nil, "not-implemented"
end