diff options
| author | Rutger Broekhoff | 2026-11-30 11:47:36 +0100 |
|---|---|---|
| committer | Rutger Broekhoff | 2026-11-30 11:47:36 +0100 |
| commit | 02475e05ccd1aa15c6a6163834f28bfebbef796f (patch) | |
| tree | 6bc6f453f07dc76f763310e1e5eade9a3e1115b7 /server/src/sqlite3.cppm | |
| parent | 5732f6c14618f50ffaf315b93795340219a67c66 (diff) | |
| download | routemon-02475e05ccd1aa15c6a6163834f28bfebbef796f.tar.gz routemon-02475e05ccd1aa15c6a6163834f28bfebbef796f.zip | |
RWGPS webhook and such
Diffstat (limited to 'server/src/sqlite3.cppm')
| -rw-r--r-- | server/src/sqlite3.cppm | 149 |
1 files changed, 104 insertions, 45 deletions
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<T> || optional_of<T, scannable_prim>; | |||
| 28 | 28 | ||
| 29 | class mutex_guard | 29 | class mutex_guard |
| 30 | { | 30 | { |
| 31 | explicit mutex_guard(::sqlite3_mutex* mut) noexcept; | 31 | ::sqlite3* dbc_; |
| 32 | |||
| 33 | friend auto | ||
| 34 | do_guarded(::sqlite3_mutex* mut, std::invocable<mutex_guard const&> auto f) | ||
| 35 | -> decltype(f(std::declval<mutex_guard const&>())); | ||
| 36 | 32 | ||
| 37 | public: | 33 | public: |
| 34 | explicit mutex_guard(::sqlite3* dbc) noexcept; | ||
| 38 | mutex_guard(mutex_guard const&) = delete; | 35 | mutex_guard(mutex_guard const&) = delete; |
| 36 | mutex_guard(mutex_guard&& rhs) = delete; | ||
| 37 | auto operator=(mutex_guard const&) -> mutex_guard& = delete; | ||
| 38 | auto operator=(mutex_guard&& rhs) -> mutex_guard& = delete; | ||
| 39 | ~mutex_guard(); | 39 | ~mutex_guard(); |
| 40 | 40 | ||
| 41 | private: | 41 | auto for_db(::sqlite3 const* dbc) const -> bool { return dbc_ == dbc; } |
| 42 | ::sqlite3_mutex* mut_; | ||
| 43 | }; | 42 | }; |
| 44 | 43 | ||
| 45 | class error : public std::exception | 44 | class error : public std::exception |
| @@ -48,40 +47,68 @@ class error : public std::exception | |||
| 48 | std::string message_; | 47 | std::string message_; |
| 49 | 48 | ||
| 50 | public: | 49 | public: |
| 51 | explicit error(mutex_guard const&, int code, ::sqlite3* dbc); | 50 | explicit error(int code, ::sqlite3* dbc); |
| 52 | explicit error(int code); | 51 | explicit error(int code); |
| 53 | 52 | ||
| 54 | [[nodiscard]] auto what() const noexcept -> char const* override; | 53 | [[nodiscard]] auto what() const noexcept -> char const* override; |
| 55 | [[nodiscard]] auto code() const noexcept -> int; | 54 | [[nodiscard]] auto code() const noexcept -> int; |
| 56 | }; | 55 | }; |
| 57 | 56 | ||
| 58 | class statement | 57 | class statement; |
| 58 | class row_reader; | ||
| 59 | |||
| 60 | // TODO: make consumable and mark release_guard only callable when unconsumed? | ||
| 61 | // Same might have to for release_guard in row_reader. | ||
| 62 | class locked_statement | ||
| 59 | { | 63 | { |
| 60 | ::sqlite3_stmt* stmt_; | 64 | ::sqlite3_stmt* stmt_; |
| 65 | std::shared_ptr<mutex_guard> guard_; | ||
| 66 | |||
| 67 | friend class statement; | ||
| 68 | friend class row_reader; | ||
| 69 | |||
| 70 | public: | ||
| 71 | explicit locked_statement( | ||
| 72 | ::sqlite3_stmt* stmt, std::shared_ptr<mutex_guard> dbc_locked); | ||
| 73 | locked_statement(locked_statement const&) = delete; | ||
| 74 | locked_statement(locked_statement&& s) noexcept; | ||
| 75 | ~locked_statement(); | ||
| 76 | }; | ||
| 77 | |||
| 78 | class statement | ||
| 79 | { | ||
| 80 | locked_statement stmt_; | ||
| 81 | |||
| 82 | auto param_idx(util::zstring_view name) -> int; | ||
| 83 | auto bind_null(util::zstring_view param_name) -> statement&; | ||
| 61 | 84 | ||
| 62 | public: | 85 | public: |
| 63 | explicit statement(::sqlite3_stmt* stmt); | 86 | explicit statement(locked_statement&& stmt); |
| 64 | statement(statement const&) = delete; | 87 | |
| 65 | statement(statement&& s) noexcept; | 88 | auto bind(util::zstring_view param_name, std::string_view str) -> statement&; |
| 66 | ~statement(); | 89 | auto bind(util::zstring_view param_name, std::int64_t x) -> statement&; |
| 67 | auto get() -> ::sqlite3_stmt*; | 90 | auto bind(util::zstring_view param_name, std::optional<std::string_view> str) |
| 91 | -> statement&; | ||
| 92 | auto bind(util::zstring_view param_name, std::optional<std::int64_t> x) | ||
| 93 | -> statement&; | ||
| 94 | auto into_reader() && -> row_reader; | ||
| 68 | }; | 95 | }; |
| 69 | 96 | ||
| 70 | class row_reader | 97 | class row_reader |
| 71 | { | 98 | { |
| 72 | statement stmt_; | 99 | locked_statement stmt_; |
| 73 | 100 | ||
| 74 | explicit row_reader(statement stmt); | 101 | friend class statement; |
| 75 | 102 | ||
| 76 | friend class connection; | 103 | explicit row_reader(locked_statement&& stmt); |
| 77 | 104 | ||
| 78 | auto is_null(int col) -> bool; | 105 | auto is_null(int col) -> bool; |
| 79 | auto ncols() -> std::size_t; | 106 | auto ncols() -> std::size_t; |
| 80 | 107 | ||
| 81 | auto scan(int col, std::string& s) -> void; | 108 | auto scan_col(int col, std::string& s) -> void; |
| 82 | auto scan(int col, double& v) -> void; | 109 | auto scan_col(int col, double& v) -> void; |
| 83 | auto scan(int col, std::int64_t& v) -> void; | 110 | auto scan_col(int col, std::int64_t& v) -> void; |
| 84 | auto scan(int col, optional_of<scannable> auto& v) -> void | 111 | auto scan_col(int col, optional_of<scannable_prim> auto& v) -> void |
| 85 | { | 112 | { |
| 86 | if (is_null(col)) | 113 | if (is_null(col)) |
| 87 | { | 114 | { |
| @@ -90,7 +117,7 @@ class row_reader | |||
| 90 | else | 117 | else |
| 91 | { | 118 | { |
| 92 | typename std::remove_cvref_t<decltype(v)>::value_type tmp; | 119 | typename std::remove_cvref_t<decltype(v)>::value_type tmp; |
| 93 | scan(col, tmp); | 120 | scan_col(col, tmp); |
| 94 | v = std::move(tmp); | 121 | v = std::move(tmp); |
| 95 | } | 122 | } |
| 96 | } | 123 | } |
| @@ -105,10 +132,19 @@ public: | |||
| 105 | "more scanning arguments provided than columns in result set" | 132 | "more scanning arguments provided than columns in result set" |
| 106 | }; | 133 | }; |
| 107 | auto col = 0; | 134 | auto col = 0; |
| 108 | (..., scan(col++, args)); | 135 | (..., scan_col(col++, args)); |
| 109 | } | 136 | } |
| 110 | 137 | ||
| 111 | auto scan_single(scannable auto&... args) -> void | 138 | template <scannable... args> |
| 139 | auto scan() -> std::tuple<args...> | ||
| 140 | { | ||
| 141 | std::tuple<args...> res; | ||
| 142 | auto& [... elems] = res; | ||
| 143 | scan(elems...); | ||
| 144 | return res; | ||
| 145 | } | ||
| 146 | |||
| 147 | auto scan_row_max1(scannable auto&... args) -> void | ||
| 112 | { | 148 | { |
| 113 | if (!next()) | 149 | if (!next()) |
| 114 | throw std::logic_error{"no row in result set"}; | 150 | throw std::logic_error{"no row in result set"}; |
| @@ -118,46 +154,69 @@ public: | |||
| 118 | throw std::logic_error{"more than one row in result set"}; | 154 | throw std::logic_error{"more than one row in result set"}; |
| 119 | } | 155 | } |
| 120 | } | 156 | } |
| 121 | }; | ||
| 122 | |||
| 123 | class binder | ||
| 124 | { | ||
| 125 | statement& stmt_; | ||
| 126 | 157 | ||
| 127 | explicit binder(statement& stmt); | 158 | template <scannable... args> |
| 159 | auto scan_row_max1() -> std::tuple<args...> | ||
| 160 | { | ||
| 161 | std::tuple<args...> res; | ||
| 162 | auto& [... elems] = res; | ||
| 163 | scan_row_max1(elems...); | ||
| 164 | return res; | ||
| 165 | } | ||
| 166 | }; | ||
| 128 | 167 | ||
| 129 | friend class connection; | 168 | // TODO: consider making this a concept instead. |
| 169 | using bindfn = std::function<auto(statement&)->void>; | ||
| 130 | 170 | ||
| 131 | public: | 171 | auto noop_binder(statement&) -> void; |
| 132 | auto text(std::string const& param_name, std::string_view str) -> void; | ||
| 133 | |||
| 134 | static auto noop(binder&) -> void; | ||
| 135 | }; | ||
| 136 | 172 | ||
| 137 | export class connection | 173 | export class connection |
| 138 | { | 174 | { |
| 139 | ::sqlite3* dbc_; | 175 | ::sqlite3* dbc_; |
| 140 | ::sqlite3_mutex* mut_; | ||
| 141 | 176 | ||
| 142 | explicit connection(::sqlite3* dbc); | 177 | explicit connection(::sqlite3* dbc); |
| 143 | 178 | ||
| 144 | friend auto open(std::string const& filename) -> connection; | 179 | friend auto open(util::zstring_view filename) -> connection; |
| 180 | friend class tx; | ||
| 145 | 181 | ||
| 146 | public: | 182 | public: |
| 147 | connection(connection const&) = delete; | 183 | connection(connection const&) = delete; |
| 148 | connection(connection&& c) noexcept; | 184 | connection(connection&& c) noexcept; |
| 149 | 185 | ||
| 150 | [[nodiscard]] auto query( | 186 | [[nodiscard]] auto |
| 151 | std::string const& sql, | 187 | query(util::zstring_view sql, bindfn const& bf = noop_binder) -> row_reader; |
| 152 | std::function<void(binder&)> const& bf = binder::noop) -> row_reader; | ||
| 153 | 188 | ||
| 154 | auto exec( | 189 | auto exec(util::zstring_view sql, bindfn const& bf = noop_binder) -> void; |
| 155 | std::string const& sql, | ||
| 156 | std::function<void(binder&)> const& bf = binder::noop) -> void; | ||
| 157 | 190 | ||
| 158 | ~connection(); | 191 | ~connection(); |
| 159 | }; | 192 | }; |
| 160 | 193 | ||
| 161 | export auto open(std::string const& filename) -> connection; | 194 | // TODO: define behavior when user issues queries that affect the |
| 195 | // transaction itself. | ||
| 196 | export class tx | ||
| 197 | { | ||
| 198 | bool commit_ = true; | ||
| 199 | util::not_null<connection*> dbc_; | ||
| 200 | std::shared_ptr<mutex_guard> guard_; | ||
| 201 | |||
| 202 | public: | ||
| 203 | explicit tx(connection& dbc); | ||
| 204 | |||
| 205 | ~tx() | ||
| 206 | { | ||
| 207 | if (commit_) | ||
| 208 | exec("COMMIT"); | ||
| 209 | else | ||
| 210 | exec("ROLLBACK"); | ||
| 211 | } | ||
| 212 | |||
| 213 | [[nodiscard]] auto | ||
| 214 | query(util::zstring_view sql, bindfn const& bf = noop_binder) -> row_reader; | ||
| 215 | |||
| 216 | auto exec(util::zstring_view sql, bindfn const& bf = noop_binder) -> void; | ||
| 217 | auto exec_first(util::zstring_view& sql) -> void; | ||
| 218 | }; | ||
| 219 | |||
| 220 | export auto open(util::zstring_view filename) -> connection; | ||
| 162 | 221 | ||
| 163 | } // namespace routemon::sqlite3 | 222 | } // namespace routemon::sqlite3 |