From 7bba7176194c4d1d9c7066f8eda7a94491d924e6 Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Mon, 30 Nov 2026 21:27:55 +0100 Subject: Cleanups and updates --- server/src/database/sqlite3.cppm | 222 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 server/src/database/sqlite3.cppm (limited to 'server/src/database/sqlite3.cppm') diff --git a/server/src/database/sqlite3.cppm b/server/src/database/sqlite3.cppm new file mode 100644 index 0000000..2216109 --- /dev/null +++ b/server/src/database/sqlite3.cppm @@ -0,0 +1,222 @@ +export module routemon:database.sqlite3; + +import std; +import :util; + +extern "C" +{ + using sqlite3 = struct sqlite3; + using sqlite3_mutex = struct sqlite3_mutex; + using sqlite3_stmt = struct sqlite3_stmt; +} + +namespace routemon::database::sqlite3 { + +template concept C> +concept optional_of = requires { + typename T::value_type; + requires std::same_as>; + requires C; +}; + +template +concept scannable_prim = std::same_as || std::same_as + || std::same_as; + +template +concept scannable = scannable_prim || optional_of; + +class mutex_guard +{ + ::sqlite3* dbc_; + +public: + explicit mutex_guard(::sqlite3* dbc) noexcept; + mutex_guard(mutex_guard const&) = delete; + mutex_guard(mutex_guard&& rhs) = delete; + auto operator=(mutex_guard const&) -> mutex_guard& = delete; + auto operator=(mutex_guard&& rhs) -> mutex_guard& = delete; + ~mutex_guard(); + + auto for_db(::sqlite3 const* dbc) const -> bool { return dbc_ == dbc; } +}; + +class error : public std::exception +{ + int code_; + std::string message_; + +public: + explicit error(int code, ::sqlite3* dbc); + explicit error(int code); + + [[nodiscard]] auto what() const noexcept -> char const* override; + [[nodiscard]] auto code() const noexcept -> int; +}; + +class statement; +class row_reader; + +// TODO: make consumable and mark release_guard only callable when unconsumed? +// Same might have to for release_guard in row_reader. +class locked_statement +{ + ::sqlite3_stmt* stmt_; + std::shared_ptr guard_; + + friend class statement; + friend class row_reader; + +public: + explicit locked_statement( + ::sqlite3_stmt* stmt, std::shared_ptr dbc_locked); + locked_statement(locked_statement const&) = delete; + locked_statement(locked_statement&& s) noexcept; + ~locked_statement(); +}; + +class statement +{ + locked_statement stmt_; + + auto param_idx(util::zstring_view name) -> int; + auto bind_null(util::zstring_view param_name) -> statement&; + +public: + explicit statement(locked_statement&& stmt); + + auto bind(util::zstring_view param_name, std::string_view str) -> statement&; + auto bind(util::zstring_view param_name, std::int64_t x) -> statement&; + auto bind(util::zstring_view param_name, std::optional str) + -> statement&; + auto bind(util::zstring_view param_name, std::optional x) + -> statement&; + auto into_reader() && -> row_reader; +}; + +class row_reader +{ + locked_statement stmt_; + + friend class statement; + + explicit row_reader(locked_statement&& stmt); + + auto is_null(int col) -> bool; + auto ncols() -> std::size_t; + + auto scan_col(int col, std::string& s) -> void; + auto scan_col(int col, double& v) -> void; + auto scan_col(int col, std::int64_t& v) -> void; + auto scan_col(int col, optional_of auto& v) -> void + { + if (is_null(col)) + { + v.reset(); + } + else + { + typename std::remove_cvref_t::value_type tmp; + scan_col(col, tmp); + v = std::move(tmp); + } + } + +public: + auto next() -> bool; + + auto scan(scannable auto&... args) -> void + { + if (sizeof...(args) > ncols()) + throw std::invalid_argument{ + "more scanning arguments provided than columns in result set" + }; + auto col = 0; + (..., scan_col(col++, args)); + } + + template + auto scan() -> std::tuple + { + std::tuple res; + auto& [... elems] = res; + scan(elems...); + return res; + } + + auto scan_row_max1(scannable auto&... args) -> void + { + if (!next()) + throw std::logic_error{"no row in result set"}; + scan(args...); + if (next()) + { + throw std::logic_error{"more than one row in result set"}; + } + } + + template + auto scan_row_max1() -> std::tuple + { + std::tuple res; + auto& [... elems] = res; + scan_row_max1(elems...); + return res; + } +}; + +// TODO: consider making this a concept instead. +using bindfn = std::functionvoid>; + +auto noop_binder(statement&) -> void; + +export class connection +{ + ::sqlite3* dbc_; + + explicit connection(::sqlite3* dbc); + + friend auto open(std::filesystem::path const& filename) -> connection; + friend class tx; + +public: + connection(connection const&) = delete; + connection(connection&& c) noexcept; + + [[nodiscard]] auto + query(util::zstring_view sql, bindfn const& bf = noop_binder) -> row_reader; + + auto exec(util::zstring_view sql, bindfn const& bf = noop_binder) -> void; + + ~connection(); +}; + +// TODO: define behavior when user issues queries that affect the +// transaction itself. +export class tx +{ + bool commit_ = true; + util::not_null dbc_; + std::shared_ptr guard_; + +public: + explicit tx(connection& dbc); + + ~tx() + { + if (commit_) + exec("COMMIT"); + else + exec("ROLLBACK"); + } + + [[nodiscard]] auto + query(util::zstring_view sql, bindfn const& bf = noop_binder) -> row_reader; + + auto exec(util::zstring_view sql, bindfn const& bf = noop_binder) -> void; + auto exec_first(util::zstring_view& sql) -> void; +}; + +export auto open(std::filesystem::path const& filename) -> connection; + +} // namespace routemon::database::sqlite3 -- cgit v1.3.1