util.sql: Import type too (fix global access)
[prosody.git] / util / sql.lua
index f64e8e10a141c4cc0a9583bfa0e6e0819e589a0f..6fed1373ae9f027af12aa6cb92ce901dfcbb674e 100644 (file)
@@ -2,7 +2,8 @@
 local setmetatable, getmetatable = setmetatable, getmetatable;
 local ipairs, unpack, select = ipairs, table.unpack or unpack, select; --luacheck: ignore 113
 local tonumber, tostring = tonumber, tostring;
-local assert, xpcall, debug_traceback = assert, xpcall, debug.traceback;
+local type = type;
+local assert, pcall, xpcall, debug_traceback = assert, pcall, xpcall, debug.traceback;
 local t_concat = table.concat;
 local s_char = string.char;
 local log = require "util.logger".init("sql");
@@ -202,6 +203,10 @@ function engine:debug(enable)
                engine.update = engine.execute_update;
        end
 end
+local function handleerr(err)
+       log("error", "Error in SQL transaction: %s", debug_traceback(err, 3));
+       return err;
+end
 function engine:_transaction(func, ...)
        if not self.conn then
                local ok, err = self:connect();
@@ -212,7 +217,7 @@ function engine:_transaction(func, ...)
        local function f() return func(unpack(args, 1, n_args)); end
        log("debug", "SQL transaction begin [%s]", tostring(func));
        self.__transaction = true;
-       local success, a, b, c = xpcall(f, debug_traceback);
+       local success, a, b, c = xpcall(f, handleerr);
        self.__transaction = nil;
        if success then
                log("debug", "SQL transaction success [%s]", tostring(func));
@@ -328,7 +333,7 @@ function engine:set_encoding() -- to UTF-8
                local ok, actual_charset = self:transaction(function ()
                        return self:select"SHOW SESSION VARIABLES LIKE 'character_set_client'";
                end);
-               local charset_ok;
+               local charset_ok = true;
                for row in actual_charset do
                        if row[2] ~= charset then
                                log("error", "MySQL %s is actually %q (expected %q)", row[1], row[2], charset);