From ea0d3220db995018335c48eb06b9794235ff436b Mon Sep 17 00:00:00 2001 From: MetroWind Date: Sun, 7 Sep 2025 09:42:33 -0700 Subject: Initial commit, mostly just copied from shrt. --- src/data.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/data.cpp (limited to 'src/data.cpp') 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 @@ +#include +#include +#include + +#include +#include +#include + +#include "data.hpp" + +namespace +{ + +} // namespace + +mw::E> +DataSourceSQLite::fromFile(const std::string& db_file) +{ + auto data_source = std::make_unique(); + 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> DataSourceSQLite::newFromMemory() +{ + return fromFile(":memory:"); +} + +mw::E DataSourceSQLite::getSchemaVersion() const +{ + return db->evalToValue("PRAGMA user_version;"); +} + +mw::E DataSourceSQLite::setSchemaVersion(int64_t v) const +{ + return db->execute(std::format("PRAGMA user_version = {};", v)); +} -- cgit v1.2.3-70-g09d2