summaryrefslogtreecommitdiff
path: root/src/database/async_result.hpp
blob: f4109c09b888fa526b15270b8b918c2dc555d514 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <database/row.hpp>

template <typename... T>
class AsyncResult
{
  std::unique_ptr<Statement> statement{};
  std::string table_name;

public:
  AsyncResult(std::unique_ptr<Statement> s, const std::string& table_name):
    statement{std::move(s)},
    table_name{table_name}
  {}

  class iterator
  {
    using iterator_category = std::input_iterator_tag;
    using value_type = Row<T...>;
    using difference_type = std::ptrdiff_t;
    using pointer = Row<T...>*;
    using reference = Row<T...>&;

    Row<T...> row{};
    Statement* statement;
    bool is_end;

  public:
    iterator(Statement* s, const std::string& table_name, bool end=false):
      row{table_name},
      statement{s},
      is_end{end}
    {}

    reference operator*()
    {
      extract_row_values(this->row, *statement);
      return this->row;
    }

    bool operator==(const iterator& o) const
    {
      if (this->is_end && o.is_end)
        return true;
      return false;
    }
    bool operator!=(const iterator& o) const
    {
      return !(*this == o);
    }

    iterator& operator++()
    {
      if (statement->step() != StepResult::Row)
        this->is_end = true;
      return *this;
    }

    iterator& operator++(int)
    {
      iterator old = *this;
      if (statement->step() != StepResult::Row)
        this->is_end = true;
      return old;
    }
  };

  iterator begin() const
  {
    iterator it{this->statement.get(), this->table_name};
    ++it;
    return it;
  }
  iterator end() const
  {
    return {this->statement.get(), this->table_name, true};
  }
};