diff options
Diffstat (limited to 'src/data.cpp')
-rw-r--r-- | src/data.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/data.cpp b/src/data.cpp new file mode 100644 index 0000000..78cb500 --- /dev/null +++ b/src/data.cpp | |||
@@ -0,0 +1,54 @@ | |||
1 | #include <memory> | ||
2 | #include <string> | ||
3 | #include <expected> | ||
4 | |||
5 | #include <mw/database.hpp> | ||
6 | #include <mw/error.hpp> | ||
7 | #include <mw/utils.hpp> | ||
8 | |||
9 | #include "data.hpp" | ||
10 | |||
11 | namespace | ||
12 | { | ||
13 | |||
14 | } // namespace | ||
15 | |||
16 | mw::E<std::unique_ptr<DataSourceSQLite>> | ||
17 | DataSourceSQLite::fromFile(const std::string& db_file) | ||
18 | { | ||
19 | auto data_source = std::make_unique<DataSourceSQLite>(); | ||
20 | ASSIGN_OR_RETURN(data_source->db, mw::SQLite::connectFile(db_file)); | ||
21 | |||
22 | // Perform schema upgrade here. | ||
23 | // | ||
24 | // data_source->upgradeSchema1To2(); | ||
25 | |||
26 | // Update this line when schema updates. | ||
27 | DO_OR_RETURN(data_source->setSchemaVersion(1)); | ||
28 | DO_OR_RETURN(data_source->db->execute( | ||
29 | "CREATE TABLE IF NOT EXISTS Users " | ||
30 | "(id INTEGER PRIMARY KEY, openid_uid TEXT, name TEXT);")); | ||
31 | DO_OR_RETURN(data_source->db->execute( | ||
32 | "CREATE TABLE IF NOT EXISTS LinkItems " | ||
33 | "(id INTEGER PRIMARY KEY," | ||
34 | " FOREIGN KEY(owner_id) REFERENCES Users(id) NOT NULL," | ||
35 | " FOREIGN KEY(parent_id) REFERENCES LinkItems(id)," | ||
36 | " name TEXT NOT NULL, url TEXT, description TEXT," | ||
37 | " visibility INTEGER NOT NULL, time INTEGER NOT NULL);")); | ||
38 | return data_source; | ||
39 | } | ||
40 | |||
41 | mw::E<std::unique_ptr<DataSourceSQLite>> DataSourceSQLite::newFromMemory() | ||
42 | { | ||
43 | return fromFile(":memory:"); | ||
44 | } | ||
45 | |||
46 | mw::E<int64_t> DataSourceSQLite::getSchemaVersion() const | ||
47 | { | ||
48 | return db->evalToValue<int64_t>("PRAGMA user_version;"); | ||
49 | } | ||
50 | |||
51 | mw::E<void> DataSourceSQLite::setSchemaVersion(int64_t v) const | ||
52 | { | ||
53 | return db->execute(std::format("PRAGMA user_version = {};", v)); | ||
54 | } | ||