-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
238 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#pragma once | ||
|
||
#include <podrm/sqlite/detail/cursor.hpp> | ||
|
||
#include <cassert> | ||
#include <cstddef> | ||
#include <functional> | ||
#include <iterator> | ||
#include <utility> | ||
|
||
namespace podrm::sqlite { | ||
|
||
template <typename T> class Cursor { | ||
public: | ||
class Iterator; | ||
class Sentinel {}; | ||
|
||
Iterator begin() { return Iterator{this->impl}; } | ||
Sentinel end() { return Sentinel{}; } | ||
|
||
private: | ||
detail::Cursor impl; | ||
|
||
explicit Cursor(detail::Cursor impl) : impl(std::move(impl)) {} | ||
|
||
friend class Database; | ||
}; | ||
|
||
template <typename T> class Cursor<T>::Iterator { | ||
public: | ||
using iterator_category = std::input_iterator_tag; | ||
using value_type = T; | ||
using difference_type = std::ptrdiff_t; | ||
using pointer = T *; | ||
using reference = T &; | ||
|
||
T operator*() const { | ||
T result; | ||
assert(this->cursor.get().extract(&result)); | ||
return result; | ||
} | ||
|
||
Iterator &operator++() { | ||
this->cursor.get().nextRow(); | ||
|
||
return *this; | ||
} | ||
|
||
Iterator operator++(int) { return ++(*this); } | ||
|
||
friend bool operator==(const Iterator &lhs, const Sentinel /*sentinel*/) { | ||
return !lhs.cursor.get().valid(); | ||
} | ||
|
||
private: | ||
std::reference_wrapper<detail::Cursor> cursor; | ||
|
||
explicit Iterator(detail::Cursor &cursor) : cursor(cursor) {} | ||
|
||
friend class Cursor<T>; | ||
}; | ||
|
||
} // namespace podrm::sqlite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
|
||
#include <podrm/reflection.hpp> | ||
#include <podrm/span.hpp> | ||
#include <podrm/sqlite/detail/result.hpp> | ||
|
||
namespace podrm::sqlite::detail { | ||
|
||
class Cursor { | ||
public: | ||
Cursor(Result result, span<const FieldDescription> description); | ||
|
||
/// @param[out] data data to be initialized | ||
[[nodiscard]] bool extract(void *data) const; | ||
|
||
bool nextRow(); | ||
|
||
[[nodiscard]] bool valid() const; | ||
|
||
private: | ||
Result result; | ||
|
||
span<const FieldDescription> description; | ||
}; | ||
|
||
} // namespace podrm::sqlite::detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#include "../detail/multilambda.hpp" | ||
|
||
#include <podrm/reflection.hpp> | ||
#include <podrm/span.hpp> | ||
#include <podrm/sqlite/detail/cursor.hpp> | ||
#include <podrm/sqlite/detail/result.hpp> | ||
#include <podrm/sqlite/detail/row.hpp> | ||
|
||
#include <cassert> | ||
#include <cstdint> | ||
#include <optional> | ||
#include <utility> | ||
#include <variant> | ||
|
||
namespace podrm::sqlite::detail { | ||
|
||
namespace { | ||
|
||
void init(const FieldDescription description, const Row row, int ¤tColumn, | ||
void *field) { | ||
const auto initPrimitive = [field, row, ¤tColumn]( | ||
const PrimitiveFieldDescription description) { | ||
switch (description.imageType) { | ||
case ImageType::Int: | ||
description.fromImage(row.get(currentColumn).bigint(), field); | ||
++currentColumn; | ||
return; | ||
case ImageType::Uint: | ||
description.fromImage( | ||
static_cast<std::uint64_t>(row.get(currentColumn).bigint()), field); | ||
++currentColumn; | ||
return; | ||
case ImageType::String: | ||
description.fromImage(row.get(currentColumn).text(), field); | ||
++currentColumn; | ||
return; | ||
case ImageType::Float: | ||
description.fromImage(row.get(currentColumn).real(), field); | ||
++currentColumn; | ||
return; | ||
case ImageType::Bool: | ||
description.fromImage(row.get(currentColumn).boolean(), field); | ||
++currentColumn; | ||
return; | ||
case ImageType::Bytes: | ||
description.fromImage(row.get(currentColumn).bytes(), field); | ||
++currentColumn; | ||
return; | ||
} | ||
assert(false); | ||
}; | ||
|
||
const auto initComposite = [field, row, ¤tColumn]( | ||
const CompositeFieldDescription description) { | ||
for (const FieldDescription fieldDescr : description.fields) { | ||
init(fieldDescr, row, currentColumn, fieldDescr.memberPtr(field)); | ||
} | ||
}; | ||
|
||
std::visit(podrm::detail::MultiLambda{initPrimitive, initComposite}, | ||
description.field); | ||
} | ||
|
||
} // namespace | ||
|
||
Cursor::Cursor(Result result, const span<const FieldDescription> description) | ||
: result(std::move(result)), description(description) {} | ||
|
||
bool Cursor::extract(void *data) const { | ||
std::optional<Row> row = this->result.getRow(); | ||
if (!row.has_value()) { | ||
return false; | ||
} | ||
|
||
int currentColumn = 0; | ||
for (const FieldDescription field : description) { | ||
init(field, row.value(), currentColumn, field.memberPtr(data)); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool Cursor::nextRow() { return this->result.nextRow(); } | ||
|
||
bool Cursor::valid() const { | ||
return this->result.valid(); | ||
} | ||
|
||
} // namespace podrm::sqlite::detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.