diff options
author | John Turner <jturner.usa@gmail.com> | 2025-09-14 00:16:10 -0400 |
---|---|---|
committer | John Turner <jturner.usa@gmail.com> | 2025-09-14 00:16:10 -0400 |
commit | efcea3a80da7c4479d5fe168435ecc9fd06bdc72 (patch) | |
tree | 5cb0177e17b1b00a177f2e830e809f606334571b /include/boost/sqlite/row.hpp | |
download | sqlite-kv-bench-efcea3a80da7c4479d5fe168435ecc9fd06bdc72.tar.gz |
Squashed 'subprojects/boost-sqlite/' content from commit 3378e35
git-subtree-dir: subprojects/boost-sqlite
git-subtree-split: 3378e353705271e569cf4ba15c467b840a39798c
Diffstat (limited to 'include/boost/sqlite/row.hpp')
-rw-r--r-- | include/boost/sqlite/row.hpp | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/include/boost/sqlite/row.hpp b/include/boost/sqlite/row.hpp new file mode 100644 index 0000000..6893f96 --- /dev/null +++ b/include/boost/sqlite/row.hpp @@ -0,0 +1,167 @@ +// Copyright (c) 2022 Klemens D. Morgenstern +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +#ifndef BOOST_SQLITE_ROW_HPP +#define BOOST_SQLITE_ROW_HPP + +#include <boost/sqlite/field.hpp> + +BOOST_SQLITE_BEGIN_NAMESPACE + +/** @brief Representation of a row in a database. + @ingroup reference + + Is a random-access range. + + All values that are obtained by view are valid until the next row is read. + + */ +struct row +{ + /// The size of the row + std::size_t size() const + { + return sqlite3_column_count(stm_); + } + /// Returns the field at `idx`, @throws std::out_of_range + BOOST_SQLITE_DECL + field at(std::size_t idx) const; + /// Returns the field at `idx`. + field operator[](std::size_t idx) const + { + field f; + f.stm_ = stm_; + f.col_ = static_cast<int>(idx); + return f; + } + /// Random access iterator used to iterate over the columns. + struct const_iterator + { + using difference_type = int; + using reference = field&; + using iterator_category = std::random_access_iterator_tag; + + const_iterator & operator++() + { + f_.col_++; + return *this; + } + + const_iterator operator++(int) + { + auto last = *this; + ++(*this); + return last; + } + + const_iterator & operator--() + { + f_.col_--; + return *this; + } + + const_iterator operator--(int) + { + auto last = *this; + --(*this); + return last; + } + + field operator[](int i) const + { + auto f = f_; + f.col_ += i; + return f; + } + + const_iterator operator+(int i) const + { + auto r = *this; + r.f_.col_ += i; + return r; + } + + const_iterator operator-(int i) const + { + auto r = *this; + r.f_.col_ -= i; + return r; + } + + const_iterator & operator+=(int i) + { + f_.col_ += i; + return *this; + } + + const_iterator & operator-=(int i) + { + f_.col_ -= i; + return *this; + } + + const field & operator*() const + { + return f_; + } + + const field * operator->() const + { + return &f_; + } + + bool operator==(const const_iterator& other) const + { + return f_.col_ == other.f_.col_ + && f_.stm_ == other.f_.stm_; + } + + bool operator!=(const const_iterator& other) const + { + return f_.col_ != other.f_.col_ + || f_.stm_ != other.f_.stm_; + } + + bool operator<(const const_iterator& other) const + { + return f_.col_ < other.f_.col_ + && f_.stm_ < other.f_.stm_; + } + + bool operator>(const const_iterator& other) const + { + return f_.col_ > other.f_.col_ + && f_.stm_ > other.f_.stm_; + } + + const_iterator() = default; + private: + friend struct row; + field f_; + }; + /// Returns the begin of the column-range. + const_iterator begin() const + { + const_iterator ci; + ci.f_.col_ = 0; + ci.f_.stm_ = stm_; + return ci; + } + /// Returns the end of the column-range. + const_iterator end() const + { + const_iterator ci; + ci.f_.col_ = sqlite3_column_count(stm_); + ci.f_.stm_ = stm_; + return ci; + } + private: + friend struct resultset; + sqlite3_stmt * stm_; + +}; + +BOOST_SQLITE_END_NAMESPACE + +#endif //BOOST_SQLITE_ROW_HPP |