From 0ade3e421eaa6c73bc99ba0a87f4af124caa6561 Mon Sep 17 00:00:00 2001 From: nekobit Date: Sat, 8 Oct 2022 05:58:22 +0000 Subject: [PATCH] Cleanup ugly function FossilOrigin-Name: 5c0ec775ebba2b7c5926159a43ea037df373031e86b44acaee0c3606c0723c76 --- src/database/sqlite/sqlite.cpp | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/src/database/sqlite/sqlite.cpp b/src/database/sqlite/sqlite.cpp index 609d980..a85261d 100644 --- a/src/database/sqlite/sqlite.cpp +++ b/src/database/sqlite/sqlite.cpp @@ -131,9 +131,7 @@ Types::User SQLite::get_user(unsigned long id) // Setup function sqlite_compile_to_cache(GET_USER_BY_ID, "SELECT acct FROM users WHERE id=?1"); - sqlite_exec(GET_USER_BY_ID, - {id}, - [&luser](sqlite3_stmt* stmt){ + sqlite_exec(GET_USER_BY_ID, {id}, [&luser](sqlite3_stmt* stmt){ luser.acct = reinterpret_cast(sqlite3_column_text(stmt, 0)); }); @@ -143,30 +141,13 @@ Types::User SQLite::get_user(unsigned long id) Types::User SQLite::get_user(const std::string& acct) { Types::User luser{0, {}, false, acct, {}, {}}; - int code = 0; - constexpr std::string_view sql{"SELECT id FROM users WHERE acct=?1"}; - auto begin = sql.begin(); - static sqlite3_stmt* stmt = nullptr; - - if (!stmt) code = sqlite3_prepare_v2(db, &(*begin), sql.length(), &stmt, nullptr); - - if (code == SQLITE_ERROR) - { - using namespace std::string_literals; - const std::string err_msg = "Couldn't: "s + std::string(sqlite3_errmsg(db)); - throw std::runtime_error(err_msg); - } - - code = sqlite3_bind_text(stmt, 1, acct.c_str(), acct.length(), nullptr); - - while ((code = sqlite3_step(stmt)) == SQLITE_ROW) - { + // Setup function + sqlite_compile_to_cache(GET_USER_BY_ACCT, "SELECT acct FROM users WHERE acct=?1"); + + sqlite_exec(GET_USER_BY_ID, {acct}, [&luser](sqlite3_stmt* stmt){ luser.id = sqlite3_column_int64(stmt, 0); - } - - - if (code == SQLITE_DONE) sqlite3_reset(stmt); + }); return luser; }