diff --git a/include/sqlpp11/result.h b/include/sqlpp11/result.h index 527a6e08..b93cd84c 100644 --- a/include/sqlpp11/result.h +++ b/include/sqlpp11/result.h @@ -27,9 +27,17 @@ #ifndef SQLPP_RESULT_H #define SQLPP_RESULT_H -// FIXME: include for move? +#include +#include + namespace sqlpp { + template + struct iterator_category + { + using type = std::input_iterator_tag; + }; + template class result_t { @@ -60,16 +68,22 @@ namespace sqlpp class iterator { public: + using iterator_category = typename iterator_category::type; + using value_type = result_row_t; + using pointer = const result_row_t*; + using reference = const result_row_t&; + using difference_type = std::ptrdiff_t; + iterator(db_result_t& result, result_row_t& result_row) : _result(result), _result_row(result_row) { } - const result_row_t& operator*() const + reference operator*() const { return _result_row; } - const result_row_t* operator->() const + pointer operator->() const { return &_result_row; } @@ -84,9 +98,17 @@ namespace sqlpp return not(operator==(rhs)); } - void operator++() + iterator& operator++() { _result.next(_result_row); + return *this; + } + + iterator operator++(int) + { + auto previous_it = *this; + _result.next(_result_row); + return previous_it; } db_result_t& _result; diff --git a/tests/Select.cpp b/tests/Select.cpp index 268488f2..74edacfc 100644 --- a/tests/Select.cpp +++ b/tests/Select.cpp @@ -26,6 +26,7 @@ #include "MockDb.h" #include "Sample.h" #include "is_regular.h" +#include #include #include #include @@ -52,6 +53,14 @@ struct to_cerr } }; +template +void print_row(Row const& row) +{ + int64_t a = row.alpha; + const std::string b = row.beta; + std::cout << a << ", " << b << std::endl; +} + int Select(int, char* []) { MockDb db = {}; @@ -69,6 +78,13 @@ int Select(int, char* []) std::cout << row.a << std::endl; } + { + // using stl algorithms + auto rows = db(select(all_of(t)).from(t).unconditionally()); + // nicer in C++14 + std::for_each(rows.begin(), rows.end(), &print_row); + } + for (const auto& row : db(select(all_of(t)).from(t).unconditionally())) { int64_t a = row.alpha;