diff --git a/src/database/database.cpp b/src/database/database.cpp index 9a18464..79d52ce 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -41,3 +41,8 @@ User Database::get_user(const std::string& acct) std::cerr << "Database::get_user()" << std::endl; return {}; } + +void Database::create_user(const User& props) +{ + std::cerr << "Database::create_user()" << std::endl; +} diff --git a/src/database/database.h b/src/database/database.h index 4241a14..d298d6b 100644 --- a/src/database/database.h +++ b/src/database/database.h @@ -39,6 +39,10 @@ namespace DB virtual User get_user(unsigned long id); virtual User get_user(const std::string& acct); + virtual void create_user(const User& props); + + /// These are just enums for Configuration and stuff, but Polymorphism + /// handles the typing stuff already enum class Type { SQLite, diff --git a/src/database/sqlite/sqlite.cpp b/src/database/sqlite/sqlite.cpp index 267dff6..ebe5a79 100644 --- a/src/database/sqlite/sqlite.cpp +++ b/src/database/sqlite/sqlite.cpp @@ -94,7 +94,7 @@ int SQLite::sqlite_exec(SQLiteCacheIndex idx, while ((code = sqlite3_step(stmt)) == SQLITE_ROW) { - func(stmt); + if (func) func(stmt); ++query_count; } @@ -104,6 +104,7 @@ int SQLite::sqlite_exec(SQLiteCacheIndex idx, Logger::instance().log("SQLite database is busy!", Logger::Level::DEBUG); break; case SQLITE_ERROR: + default: case SQLITE_MISUSE: { const std::string err_msg = "Couldn't exec query: "s + std::string(sqlite3_errmsg(db)); @@ -111,7 +112,6 @@ int SQLite::sqlite_exec(SQLiteCacheIndex idx, throw std::runtime_error(err_msg); } case SQLITE_DONE: - default: break; } @@ -161,6 +161,19 @@ User SQLite::get_user(unsigned long id) return luser; } +void SQLite::create_user(const User& p) +{ + // INSERT INTO users (local, display_name, acct, bio, created_at) VALUES (TRUE, 'Test user', 'test1', 'sneed!!!', unixepoch()); + + sqlite_compile_to_cache(CREATE_USER, "INSERT INTO users ( local, email, display_name, acct, bio, created_at, updated_at, birthday, key )" + "VALUES ( ?1, ?2, ?3, ?4, ?5, unixepoch(), unixepoch(), ?6, ?7 )"); + + sqlite_exec(CREATE_USER, + {p.local, p.email, p.display_name, p.acct, + p.bio, p.birthday, "the key lole"}, + nullptr); +} + User SQLite::get_user(const std::string& acct) { User luser{0, {}, false, {}, acct, {}, {}, {}}; @@ -228,6 +241,7 @@ CREATE TABLE IF NOT EXISTS users ( janny_level INTEGER DEFAULT 0, follow_move BOOLEAN DEFAULT FALSE, instance_id INTEGER, + unique (acct), FOREIGN KEY(instance_id) REFERENCES instances(id) ) )sql", diff --git a/src/database/sqlite/sqlite.h b/src/database/sqlite/sqlite.h index dcd53c6..8425e40 100644 --- a/src/database/sqlite/sqlite.h +++ b/src/database/sqlite/sqlite.h @@ -37,6 +37,7 @@ namespace DB enum SQLiteCacheIndex { GET_USER_BY_ID = 0, GET_USER_BY_ACCT, + CREATE_USER, COUNT, }; @@ -46,9 +47,10 @@ namespace DB explicit SQLite(std::filesystem::path path); virtual ~SQLite(); - virtual bool try_migration() override; - virtual User get_user(unsigned long id) override; - virtual User get_user(const std::string& acct) override; + virtual bool try_migration() override final; + virtual User get_user(unsigned long id) override final; + virtual User get_user(const std::string& acct) override final; + virtual void create_user(const User& props) override final; private: /** * @brief Executes an SQL statement diff --git a/src/user.h b/src/user.h index c642bf7..eb9f9b5 100644 --- a/src/user.h +++ b/src/user.h @@ -25,10 +25,10 @@ class User public: unsigned long id; std::string email; - bool is_local; + bool local; std::string bio; std::string acct; - std::string birthday; + int birthday; std::string display_name; std::string key; };