boost_sqlite 1
A sqlite C++ library
Loading...
Searching...
No Matches
resultset.hpp
1// Copyright (c) 2022 Klemens D. Morgenstern
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5#ifndef BOOST_SQLITE_RESULTSET_HPP
6#define BOOST_SQLITE_RESULTSET_HPP
7
8#include <memory>
9#include <boost/sqlite/row.hpp>
10#include <boost/describe/members.hpp>
11
12#include <boost/system/result.hpp>
13
14BOOST_SQLITE_BEGIN_NAMESPACE
15struct connection ;
41{
43 row current() const &
44 {
45 row r;
46 r.stm_ = impl_.get();
47 return r;
48 }
50 bool done() const {return done_;}
51
54 BOOST_SQLITE_DECL bool read_next(system::error_code & ec, error_info & ei);
55 BOOST_SQLITE_DECL bool read_next();
57
59 std::size_t column_count() const
60 {
61 return sqlite3_column_count(impl_.get());
62 }
64 core::string_view column_name(std::size_t idx) const
65 {
66 return sqlite3_column_name(impl_.get(), idx);
67 }
69 core::string_view table_name(std::size_t idx) const
70 {
71 return sqlite3_column_table_name(impl_.get(), idx);
72 }
74 core::string_view column_origin_name(std::size_t idx) const
75 {
76 return sqlite3_column_origin_name(impl_.get(), idx);
77 }
78
80 struct iterator
81 {
82 using value_type = value;
83 using difference_type = int;
84 using reference = value&;
85 using iterator_category = std::forward_iterator_tag;
86
87 iterator() {}
88 explicit iterator(sqlite3_stmt * stmt, bool sentinel) : sentinel_(sentinel )
89 {
90 row_.stm_ = stmt;
91 }
92
93 bool operator!=(iterator rhs) const
94 {
95 return sentinel_ != rhs.sentinel_;
96 }
97
98 row &operator*() { return row_; }
99 row *operator->() { return &row_; }
100
101 BOOST_SQLITE_DECL
102 iterator operator++();
103
104 iterator operator++(int)
105 {
106 auto l = *this;
107 ++(*this);
108 return l;
109 }
110
111 private:
112 row row_;
113 bool sentinel_ = true;
114 };
115
117 iterator begin() { return iterator(impl_.get(), done_);}
119 iterator end() { return iterator(impl_.get(), true); }
120
121 private:
122 friend struct connection;
123 friend struct statement;
124
125 struct deleter_
126 {
127 constexpr deleter_() noexcept {}
128 bool delete_ = true;
129 void operator()(sqlite3_stmt * sm)
130 {
131 if (sqlite3_data_count(sm) > 0)
132 while ( sqlite3_step(sm) == SQLITE_ROW);
133 if (delete_)
134 sqlite3_finalize(sm);
135 else
136 {
137 sqlite3_clear_bindings(sm);
138 sqlite3_reset(sm);
139 }
140
141 }
142 };
143 std::unique_ptr<sqlite3_stmt, deleter_> impl_;
144 bool done_ = false;
145};
146
147BOOST_SQLITE_END_NAMESPACE
148
149
150#endif //BOOST_SQLITE_RESULTSET_HPP
main object for a connection to a database.
Additional information about error conditions stored in an sqlite-allocate string.
Definition error.hpp:33
The input iterator can be used to read every row in a for-loop.
Definition resultset.hpp:81
Representation of a result from a database.
Definition resultset.hpp:41
iterator begin()
Return an input iterator to the currently unread row.
core::string_view column_origin_name(std::size_t idx) const
Returns the origin name of the column for column idx.
Definition resultset.hpp:74
iterator end()
Sentinel iterator.
row current() const &
Returns the current row.
Definition resultset.hpp:43
core::string_view table_name(std::size_t idx) const
Returns the name of the source table for column idx.
Definition resultset.hpp:69
core::string_view column_name(std::size_t idx) const
Returns the name of the column idx.
Definition resultset.hpp:64
bool done() const
Checks if the last row has been reached.
Definition resultset.hpp:50
bool read_next(system::error_code &ec, error_info &ei)
Representation of a row in a database.
Definition row.hpp:21
A statement used for a prepared-statement.
A holder for a sqlite values used for internal APIs.
Definition value.hpp:39