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