lua-users home
lua-l archive

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


Am 20.01.2011 18:08, schrieb Terry Bayne:
> Hi,

Hi!

> 
> I've been using the luaSQL module a lot lately.  The documentation for
> luaSQL suggests the use of an iterator to 'walk' over the result set:
> 
>  function rows (connection, sql_statement)
>      local cursor = assert (connection:execute (sql_statement))
>      return function ()
>        return cursor:fetch()
>      end
>    end
> 
> This works, but often causes an "there are open cursors" error when
> closing the connection.
> 
> My naive attempt to solve this doesn't appear to work:
> 
> function rows(connection, sql_statement)
>   local cursor = assert (connection:execute (sql_statement))
>   return function ()
> 	local rc = cursor:fetch()
> 	if (rc == nil) then
> 		cursor:close()
> 		cursor = nil
> 	end
>     return rc
>   end
> end
> 
> 
> Has anyone else solved this issue, if so, any pointers on how I can do so?

IIRC, I used something like this (untested):

do

  local function cursor_finalizer( cursor, v1, ... )
    if v1 == nil then
      cursor:close()
    end
    return v1, ...
  end

  function rows( connection, sql_statement )
    local cursor = assert( connection:execute( sql_statement ) )
    return function()
      return cursor_finalizer( cursor, cursor:fetch() )
    end
  end

end


> 
> Thanks
> Terry

HTH,
Philipp