diff options
author | MetroWind <chris.corsair@gmail.com> | 2025-09-07 09:42:33 -0700 |
---|---|---|
committer | MetroWind <chris.corsair@gmail.com> | 2025-09-07 09:42:33 -0700 |
commit | ea0d3220db995018335c48eb06b9794235ff436b (patch) | |
tree | 892564cdd4946c6ee9c1051bc31ff5c7bba6ddf1 /src/data.hpp |
Initial commit, mostly just copied from shrt.
Diffstat (limited to 'src/data.hpp')
-rw-r--r-- | src/data.hpp | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/data.hpp b/src/data.hpp new file mode 100644 index 0000000..dc64e6f --- /dev/null +++ b/src/data.hpp | |||
@@ -0,0 +1,73 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include <memory> | ||
4 | #include <string> | ||
5 | #include <optional> | ||
6 | #include <vector> | ||
7 | |||
8 | #include <mw/database.hpp> | ||
9 | #include <mw/error.hpp> | ||
10 | #include <mw/utils.hpp> | ||
11 | |||
12 | struct LinkItem | ||
13 | { | ||
14 | enum Visibility {PUBLIC, PRIVATE}; | ||
15 | |||
16 | int64_t id; | ||
17 | int64_t owner_id; | ||
18 | // Top-level items don’t have parents. | ||
19 | std::optional<int64_t> parent_id; | ||
20 | std::string name; | ||
21 | // If this is empty, it’s a parent. | ||
22 | std::string url; | ||
23 | std::string description; | ||
24 | Visibility visibility; | ||
25 | mw::Time time; | ||
26 | }; | ||
27 | |||
28 | struct User | ||
29 | { | ||
30 | int64_t id; | ||
31 | std::string openid_uid; | ||
32 | std::string name; | ||
33 | }; | ||
34 | |||
35 | class DataSourceInterface | ||
36 | { | ||
37 | public: | ||
38 | virtual ~DataSourceInterface() = default; | ||
39 | |||
40 | // The schema version is the version of the database. It starts | ||
41 | // from 1. Every time the schema change, someone should increase | ||
42 | // this number by 1, manually, by hand. The intended use is to | ||
43 | // help with database migration. | ||
44 | virtual mw::E<int64_t> getSchemaVersion() const = 0; | ||
45 | |||
46 | virtual std::vector<LinkItem> items(std::optional<int64_t> parent) = 0; | ||
47 | |||
48 | protected: | ||
49 | virtual mw::E<void> setSchemaVersion(int64_t v) const = 0; | ||
50 | }; | ||
51 | |||
52 | class DataSourceSQLite : public DataSourceInterface | ||
53 | { | ||
54 | public: | ||
55 | explicit DataSourceSQLite(std::unique_ptr<mw::SQLite> conn) | ||
56 | : db(std::move(conn)) {} | ||
57 | ~DataSourceSQLite() override = default; | ||
58 | |||
59 | static mw::E<std::unique_ptr<DataSourceSQLite>> | ||
60 | fromFile(const std::string& db_file); | ||
61 | static mw::E<std::unique_ptr<DataSourceSQLite>> newFromMemory(); | ||
62 | |||
63 | mw::E<int64_t> getSchemaVersion() const override; | ||
64 | |||
65 | // Do not use. | ||
66 | DataSourceSQLite() = default; | ||
67 | |||
68 | protected: | ||
69 | mw::E<void> setSchemaVersion(int64_t v) const override; | ||
70 | |||
71 | private: | ||
72 | std::unique_ptr<mw::SQLite> db; | ||
73 | }; | ||