mod_log_messages_sql/mod_log_messages_sql.lua
changeset 1343 7dbde05b48a9
parent 953 2c38d7d8b332
child 1642 398c4aaccf6d
equal deleted inserted replaced
1342:0ae065453dc9 1343:7dbde05b48a9
    60 	if not ok or not DBI.Connect then
    60 	if not ok or not DBI.Connect then
    61 		return; -- Halt loading of this module
    61 		return; -- Halt loading of this module
    62 	end
    62 	end
    63 
    63 
    64 	params = params or { driver = "SQLite3" };
    64 	params = params or { driver = "SQLite3" };
    65 	
    65 
    66 	if params.driver == "SQLite3" then
    66 	if params.driver == "SQLite3" then
    67 		params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite");
    67 		params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite");
    68 	end
    68 	end
    69 	
    69 
    70 	assert(params.driver and params.database, "Both the SQL driver and the database need to be specified");
    70 	assert(params.driver and params.database, "Both the SQL driver and the database need to be specified");
    71 
    71 
    72 	assert(connect());
    72 	assert(connect());
    73 	
    73 
    74 end
    74 end
    75 
    75 
    76 function getsql(sql, ...)
    76 function getsql(sql, ...)
    77 	if params.driver == "PostgreSQL" then
    77 	if params.driver == "PostgreSQL" then
    78 		sql = sql:gsub("`", "\"");
    78 		sql = sql:gsub("`", "\"");
    83 	if not stmt then module:log("error", "QUERY FAILED: %s %s", err, debug.traceback()); return nil, err; end
    83 	if not stmt then module:log("error", "QUERY FAILED: %s %s", err, debug.traceback()); return nil, err; end
    84 	-- run query
    84 	-- run query
    85 	local ok, err = stmt:execute(...);
    85 	local ok, err = stmt:execute(...);
    86 	if not ok and not test_connection() then error("connection failed"); end
    86 	if not ok and not test_connection() then error("connection failed"); end
    87 	if not ok then return nil, err; end
    87 	if not ok then return nil, err; end
    88 	
    88 
    89 	return stmt;
    89 	return stmt;
    90 end
    90 end
    91 function setsql(sql, ...)
    91 function setsql(sql, ...)
    92 	local stmt, err = getsql(sql, ...);
    92 	local stmt, err = getsql(sql, ...);
    93 	if not stmt then return stmt, err; end
    93 	if not stmt then return stmt, err; end