iwongu / sqlite3pp

SQLite3++ - C++ wrapper of SQLite3 API
MIT License
609 stars 177 forks source link

*Resets a command each time when construct an iterator. #43

Closed paladin-t closed 7 years ago

paladin-t commented 7 years ago

Each time a query::query_iterator is constructed, the command should be reset. I found this issue when I was trying to check whether a query results empty, then practically iterate with it, but the iteration state is reserved crossing iterators:

// Gets an iterator and checks whether it's empty.
auto qit = query.begin();
if (qit == query.end()) return false;

// Walks through all result data.
for (auto i : query) { // FIXED : this should begin from the first queried element.
    result.add(
        i.get<const char*>(1),
        i.get<const char*>(2),
        (unsigned long)i.get<long long int>(4),
        i.get<const char*>(3),
        i.get<RecordId>(0)
    );
}
iwongu commented 7 years ago

It's intended behavior. The iterator is marked as input_iterator_tag. https://github.com/iwongu/sqlite3pp/blob/master/headeronly_src/sqlite3pp.h#L283 http://en.cppreference.com/w/cpp/concept/InputIterator

It's unfortunate that we cannot catch it in compile time.