lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Tomas Guisasola Gorham wrote:
-- Untested code
-- You'll have to declare next, unpack and table as locals to
--   provide access to them
function select (self, columns, tabname, cond, extra, mode)
   local stmt = sql.select (columns, tabname, cond, extra)
   local cur = assertexec (self, stmt)
   return function ()
       -- This table must be created inside this function or it could
       -- make `selectall' to return the same row every time.
       local t
       if mode then t = {} end
       --return cur:fetch (t, mode)
       local results = { cur:fetch (t, mode) }
       if next(results) == nil then -- no more rows
           cur:close()
           return nil
       else
           return unpack (results, 1, table.maxn(results))
       end
   end
end

...

-- Untested code
function select (self, columns, tabname, cond, extra, mode)
    local stmt = sql.select (columns, tabname, cond, extra)
    local cur = assertexec (self, stmt)
    return function ()
        -- This table must be created inside this function or it could
        -- make `selectall' to return the same row every time.
        local t
        if mode then t = {} end
        return cur:fetch (t, mode)
    end, cur
end

...

local iter, cur = db:select ("*", "table", nil, nil, "a")
for row in iter do
...
end
cur:close()

     Regards,
         Tomás

Both work well.

Second one looks prettier, and runs faster, but the first on is cleaner, since it cleans up after itself. I don't mind the hit in speed, because I am just doing data migration scripts.

Thanks very much for your help

Mike