I would like to treat a database query as a standard C++ input iterator.
On the other hand, one can view a database query as an input stream of query result items.
What do you think is a better model for a db query – an input iterator or an input stream?
Personally, I have an impression that C++ IO streams are supposed to operate on characters only, where I have never seen any example of a stream where characters would be something other than char or wchar_t. I understand, that the templated nature of the streams allows me to pass anything as a character, so theoretically, it seems that I can treat the query result item as a character for the sake of streaming, but I am not sure if it is a good idea.
Advices are welcome.
Thanks.
Anything you use as a stream character type needs to have character traits, and maybe some stuff to do with locales, since someone might try to imbue a locale into your stream. Which might be nonsense, but it’s still there in the interface and even if you make it an error you probably need to make it a sensible error.
I’d definitely use an input iterator, in C++ it’s the simple model for a sequence of objects.
Streams do a lot of other stuff as well as merely presenting a sequence (formatting, control of the streambuf, the arcane error state model). Much of that probably isn’t applicable to your database items, although I suppose some of it could be. For example controlling the buffer size of a DB query result stream would make sense, but formatted reads from it wouldn’t.
The fact that
istream_iteratorexists is proof that even if you offer someone a stream, they might well prefer/need the iterator interface.