[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Cleaning up iterators
- From: Antonio Vieiro <antonio@...>
- Date: Wed, 27 Jul 2011 07:02:32 +0200
El 27/07/11 03:05, lua-l-request@lists.lua.org escribió:
Message: 2
Date: Tue, 26 Jul 2011 22:02:27 +0200
From: Petite Abeille<petite.abeille@gmail.com>
Subject: Re: Cleaning up iterators
To: Lua mailing list<lua-l@lists.lua.org>
Message-ID:<097DD5DE-4C96-48B7-9628-8FA9E088F888@gmail.com>
Content-Type: text/plain; charset=us-ascii
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
Thanks all for your replies. The fact is that I need the cursor to be
automatically closed (to avoid programming errors/leaks) as soon as
possible (because I need to clean-up resources quickly due to high load).
I think I'll use closures instead:
local adb = DB('sqlite3://localhost/test.db')
local antonio_is_there = false
adb:foreachrow( 'select name, email from contact',
function(aContact)
print(aContact.name, aContact.email)
if aContact.name == 'antonio' then
antonio_is_there = true
return false -- end iteration
else
return true -- continue to next row
end
end
)
The 'foreachrow' function will be responsible for:
- sending rows to the function until the function returns false.
- closing the cursor when the function ends (or there're no more rows).
I know it's no so easy to use as a 'for' loop, but it's 'easier' for me
to always close things no matter what I do.
Cheers,
Antonio