Comparison

plugins/mod_storage_sql.lua @ 3976:16170a66e140

mod_storage_sql: Dynamically replace backquotes with double quotes when connecting to PostgreSQL...
author Waqas Hussain <waqas20@gmail.com>
date Mon, 27 Dec 2010 06:10:35 +0500
parent 3975:aa5e93e61760
child 3977:6724853adb80
comparison
equal deleted inserted replaced
3975:aa5e93e61760 3976:16170a66e140
27 local setmetatable = setmetatable; 27 local setmetatable = setmetatable;
28 local json = { stringify = function(s) return require"util.serialization".serialize(s) end, parse = require"util.serialization".deserialize }; 28 local json = { stringify = function(s) return require"util.serialization".serialize(s) end, parse = require"util.serialization".deserialize };
29 29
30 local connection = ...; 30 local connection = ...;
31 local host,user,store = module.host; 31 local host,user,store = module.host;
32 local params = module:get_option("sql");
32 33
33 do -- process options to get a db connection 34 do -- process options to get a db connection
34 local DBI = require "DBI"; 35 local DBI = require "DBI";
35 36
36 local params = module:get_option("sql") or { driver = "SQLite3", database = "prosody.sqlite" }; 37 params = params or { driver = "SQLite3", database = "prosody.sqlite" };
37 assert(params and params.driver and params.database, "invalid params"); 38 assert(params.driver and params.database, "invalid params");
38 39
39 prosody.unlock_globals(); 40 prosody.unlock_globals();
40 local dbh, err = DBI.Connect( 41 local dbh, err = DBI.Connect(
41 params.driver, params.database, 42 params.driver, params.database,
42 params.username, params.password, 43 params.username, params.password,
83 return json.parse(value); 84 return json.parse(value);
84 end 85 end
85 end 86 end
86 87
87 local function getsql(sql, ...) 88 local function getsql(sql, ...)
89 if params.driver == "PostgreSQL" then
90 sql = sql:gsub("`", "\"");
91 end
88 -- do prepared statement stuff 92 -- do prepared statement stuff
89 local stmt, err = connection:prepare(sql); 93 local stmt, err = connection:prepare(sql);
90 if not stmt then return nil, err; end 94 if not stmt then return nil, err; end
91 -- run query 95 -- run query
92 local ok, err = stmt:execute(host or "", user or "", store or "", ...); 96 local ok, err = stmt:execute(host or "", user or "", store or "", ...);