lua-users home
lua-l archive

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


On Tue, Jul 26, 2011 at 12:10 PM, Antonio Vieiro
<antonio@antonioshome.net> wrote:
> I'm thinking of using iterator to iterate over the results of a SQL query,
> something like:
>
> for row in result("SELECT id,name FROM USER") do
>  if row.id == 3 then break end
> end
>
> but I need to "clean-up" the iterator when it finishes (i.e. after the loop)
> or whenever I have a "break" or a "return" statement.
>
> As far as I understand there's no way to do this, right?

Not right when it finishes. You could use gc to do this, but it would
happen "sometime", not right away.

You could do an API that allowed explict-close or close-on-gc:

q = ctx:query"SELECT id,name FROM USER"

for row in q:iter() do
  ...
end

q:close() -- explicit close

for row in ctx:query"SELECT id,name FROM USER":iter()
...
end

-- q is out of scope, now, and will get gced eventually.

Cheers,
Sam