[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Cleaning up iterators
- From: Petite Abeille <petite.abeille@...>
- Date: Tue, 26 Jul 2011 22:02:27 +0200
On Jul 26, 2011, at 9:10 PM, Antonio Vieiro 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?
Well, there is always a way, no?
For example, in the first case, the iterator can automatically clause the result set upon exhausting it.
Say:
local aDB = DB( 'sqlite3://localhost/test.db' )
for aContact in aDB( 'select * from contact' ) do
print( aContact.name, aContact.email )
end
Otherwise, you could always do it explicitly:
local aCursor = aDB( 'select * from contact' )
for aContact in aCursor do
print( aContact.name, aContact.email )
break
end
aCursor.close = true
Implementation details:
http://dev.alt.textdrive.com/browser/HTTP/DB.lua#L194