Forum OpenACS Development: Re: Can someone explain why nested queries such as db_foreach are not recommended?

I agree completely with everything Gustaf said above. But I'd emphasize some of it even more strongly:

I don't recall ever needing to use db_foreach. There are probably a tiny number of cases where using it is actually a good idea, but offhand I don't remember those ever occurring for me. IMO, db_foreach is in the API mostly as a crutch and a convenience, but you probably should not use it. In cases where you think you want it, you probably should be using db_list_of_lists or something else similar instead.

Thanks Andrew, that's good info.

Out of curiosity, I just checked my local copy of OpenACS 5.9.1. and found 334 occurrences of db_foreach in 170 files, so it seems to be quite popular!

Brian

db_foreach is mostly convenient when one has a query returning some big number of columns. In such case, when using e.g. db_list_of_lists, one would have to "peel" the results with a cumbersome lassign.

Using db_list_of_ns_sets would be a nicer replacement in this case, as it maintains a notion of which columns have been extracted in the ns_set. In fact, latest db_foreach implementation is just a wrapper around this api.