util.sql: Make set_encoding() return failure of SET NAMES
[prosody.git] / util / multitable.lua
index 388f60f33a5c1330f03b470eae2afeb434e8ed5d..caf25118e3f622ecd66dbea06b5c24de7abba968 100644 (file)
@@ -1,13 +1,13 @@
 -- Prosody IM
 -- Copyright (C) 2008-2010 Matthew Wild
 -- Copyright (C) 2008-2010 Waqas Hussain
--- 
+--
 -- This project is MIT/X11 licensed. Please see the
 -- COPYING file in the source package for more information.
 --
 
 local select = select;
-local t_insert, t_remove = table.insert, table.remove;
+local t_insert = table.insert;
 local unpack, pairs, next, type = unpack, pairs, next, type;
 
 module "multitable"
@@ -135,8 +135,7 @@ function iter(self, ...)
                local depth = #stack;
                local key = next(stack[depth], keys[depth]);
                if key == nil then -- Go up the stack
-                       t_remove(stack);
-                       t_remove(keys);
+                       stack[depth], keys[depth] = nil, nil;
                        if depth > 1 then
                                return it(self);
                        end
@@ -145,18 +144,19 @@ function iter(self, ...)
                        keys[depth] = key;
                end
                local value = stack[depth][key];
-               if depth == maxdepth then -- Result
-                       local result = {}; -- Collect keys forming path to result
-                       for i = 1, depth do
-                               result[i] = keys[i];
-                       end
-                       return unpack(result, 1, depth);
-               else
-                       if (query[depth] == nil or key == query[depth]) and type(value) == "table" then -- Descend
-                               t_insert(stack, value);
+               if query[depth] == nil or key == query[depth] then
+                       if depth == maxdepth then -- Result
+                               local result = {}; -- Collect keys forming path to result
+                               for i = 1, depth do
+                                       result[i] = keys[i];
+                               end
+                               result[depth+1] = value;
+                               return unpack(result, 1, depth+1);
+                       elseif type(value) == "table" then
+                               t_insert(stack, value); -- Descend
                        end
-                       return it(self);
                end
+               return it(self);
        end;
        return it, self;
 end