summaryrefslogtreecommitdiffstats
path: root/server/src/database/sqlite3.cppm
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/database/sqlite3.cppm')
-rw-r--r--server/src/database/sqlite3.cppm222
1 files changed, 222 insertions, 0 deletions
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 @@
1export module routemon:database.sqlite3;
2
3import std;
4import :util;
5
6extern "C"
7{
8 using sqlite3 = struct sqlite3;
9 using sqlite3_mutex = struct sqlite3_mutex;
10 using sqlite3_stmt = struct sqlite3_stmt;
11}
12
13namespace routemon::database::sqlite3 {
14
15template <class T, template <class U> concept C>
16concept optional_of = requires {
17 typename T::value_type;
18 requires std::same_as<T, std::optional<typename T::value_type>>;
19 requires C<typename T::value_type>;
20};
21
22template <class T>
23concept scannable_prim = std::same_as<T, std::string> || std::same_as<T, double>
24 || std::same_as<T, std::int64_t>;
25
26template <class T>
27concept scannable = scannable_prim<T> || optional_of<T, scannable_prim>;
28
29class mutex_guard
30{
31 ::sqlite3* dbc_;
32
33public:
34 explicit mutex_guard(::sqlite3* dbc) noexcept;
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();
40
41 auto for_db(::sqlite3 const* dbc) const -> bool { return dbc_ == dbc; }
42};
43
44class error : public std::exception
45{
46 int code_;
47 std::string message_;
48
49public:
50 explicit error(int code, ::sqlite3* dbc);
51 explicit error(int code);
52
53 [[nodiscard]] auto what() const noexcept -> char const* override;
54 [[nodiscard]] auto code() const noexcept -> int;
55};
56
57class statement;
58class 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.
62class locked_statement
63{
64 ::sqlite3_stmt* stmt_;
65 std::shared_ptr<mutex_guard> guard_;
66
67 friend class statement;
68 friend class row_reader;
69
70public:
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
78class 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&;
84
85public:
86 explicit statement(locked_statement&& stmt);
87
88 auto bind(util::zstring_view param_name, std::string_view str) -> statement&;
89 auto bind(util::zstring_view param_name, std::int64_t x) -> statement&;
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;
95};
96
97class row_reader
98{
99 locked_statement stmt_;
100
101 friend class statement;
102
103 explicit row_reader(locked_statement&& stmt);
104
105 auto is_null(int col) -> bool;
106 auto ncols() -> std::size_t;
107
108 auto scan_col(int col, std::string& s) -> void;
109 auto scan_col(int col, double& v) -> void;
110 auto scan_col(int col, std::int64_t& v) -> void;
111 auto scan_col(int col, optional_of<scannable_prim> auto& v) -> void
112 {
113 if (is_null(col))
114 {
115 v.reset();
116 }
117 else
118 {
119 typename std::remove_cvref_t<decltype(v)>::value_type tmp;
120 scan_col(col, tmp);
121 v = std::move(tmp);
122 }
123 }
124
125public:
126 auto next() -> bool;
127
128 auto scan(scannable auto&... args) -> void
129 {
130 if (sizeof...(args) > ncols())
131 throw std::invalid_argument{
132 "more scanning arguments provided than columns in result set"
133 };
134 auto col = 0;
135 (..., scan_col(col++, args));
136 }
137
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
148 {
149 if (!next())
150 throw std::logic_error{"no row in result set"};
151 scan(args...);
152 if (next())
153 {
154 throw std::logic_error{"more than one row in result set"};
155 }
156 }
157
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};
167
168// TODO: consider making this a concept instead.
169using bindfn = std::function<auto(statement&)->void>;
170
171auto noop_binder(statement&) -> void;
172
173export class connection
174{
175 ::sqlite3* dbc_;
176
177 explicit connection(::sqlite3* dbc);
178
179 friend auto open(std::filesystem::path const& filename) -> connection;
180 friend class tx;
181
182public:
183 connection(connection const&) = delete;
184 connection(connection&& c) noexcept;
185
186 [[nodiscard]] auto
187 query(util::zstring_view sql, bindfn const& bf = noop_binder) -> row_reader;
188
189 auto exec(util::zstring_view sql, bindfn const& bf = noop_binder) -> void;
190
191 ~connection();
192};
193
194// TODO: define behavior when user issues queries that affect the
195// transaction itself.
196export class tx
197{
198 bool commit_ = true;
199 util::not_null<connection*> dbc_;
200 std::shared_ptr<mutex_guard> guard_;
201
202public:
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
220export auto open(std::filesystem::path const& filename) -> connection;
221
222} // namespace routemon::database::sqlite3