From 02475e05ccd1aa15c6a6163834f28bfebbef796f Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Mon, 30 Nov 2026 11:47:36 +0100 Subject: RWGPS webhook and such --- server/src/sqlite3.cppm | 149 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 104 insertions(+), 45 deletions(-) (limited to 'server/src/sqlite3.cppm') diff --git a/server/src/sqlite3.cppm b/server/src/sqlite3.cppm index b771c49..3348b0b 100644 --- a/server/src/sqlite3.cppm +++ b/server/src/sqlite3.cppm @@ -28,18 +28,17 @@ concept scannable = scannable_prim || optional_of; class mutex_guard { - explicit mutex_guard(::sqlite3_mutex* mut) noexcept; - - friend auto - do_guarded(::sqlite3_mutex* mut, std::invocable auto f) - -> decltype(f(std::declval())); + ::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(); -private: - ::sqlite3_mutex* mut_; + auto for_db(::sqlite3 const* dbc) const -> bool { return dbc_ == dbc; } }; class error : public std::exception @@ -48,40 +47,68 @@ class error : public std::exception std::string message_; public: - explicit error(mutex_guard const&, int code, ::sqlite3* dbc); + 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 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(::sqlite3_stmt* stmt); - statement(statement const&) = delete; - statement(statement&& s) noexcept; - ~statement(); - auto get() -> ::sqlite3_stmt*; + 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 { - statement stmt_; + locked_statement stmt_; - explicit row_reader(statement stmt); + friend class statement; - friend class connection; + explicit row_reader(locked_statement&& stmt); auto is_null(int col) -> bool; auto ncols() -> std::size_t; - auto scan(int col, std::string& s) -> void; - auto scan(int col, double& v) -> void; - auto scan(int col, std::int64_t& v) -> void; - auto scan(int col, optional_of auto& v) -> void + 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)) { @@ -90,7 +117,7 @@ class row_reader else { typename std::remove_cvref_t::value_type tmp; - scan(col, tmp); + scan_col(col, tmp); v = std::move(tmp); } } @@ -105,10 +132,19 @@ public: "more scanning arguments provided than columns in result set" }; auto col = 0; - (..., scan(col++, args)); + (..., scan_col(col++, args)); } - auto scan_single(scannable auto&... args) -> void + 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"}; @@ -118,46 +154,69 @@ public: throw std::logic_error{"more than one row in result set"}; } } -}; - -class binder -{ - statement& stmt_; - explicit binder(statement& stmt); + template + auto scan_row_max1() -> std::tuple + { + std::tuple res; + auto& [... elems] = res; + scan_row_max1(elems...); + return res; + } +}; - friend class connection; +// TODO: consider making this a concept instead. +using bindfn = std::functionvoid>; -public: - auto text(std::string const& param_name, std::string_view str) -> void; - - static auto noop(binder&) -> void; -}; +auto noop_binder(statement&) -> void; export class connection { ::sqlite3* dbc_; - ::sqlite3_mutex* mut_; explicit connection(::sqlite3* dbc); - friend auto open(std::string const& filename) -> connection; + friend auto open(util::zstring_view filename) -> connection; + friend class tx; public: connection(connection const&) = delete; connection(connection&& c) noexcept; - [[nodiscard]] auto query( - std::string const& sql, - std::function const& bf = binder::noop) -> row_reader; + [[nodiscard]] auto + query(util::zstring_view sql, bindfn const& bf = noop_binder) -> row_reader; - auto exec( - std::string const& sql, - std::function const& bf = binder::noop) -> void; + auto exec(util::zstring_view sql, bindfn const& bf = noop_binder) -> void; ~connection(); }; -export auto open(std::string const& filename) -> 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(util::zstring_view filename) -> connection; } // namespace routemon::sqlite3 -- cgit v1.3.1