Setup SQLite cache index
FossilOrigin-Name: 50ad6c1df26eea21aad896587d998dce0b7a090a1b96df3a47aef642b3c5bcaf
This commit is contained in:
parent
f52889537c
commit
4d40aebe2f
2 changed files with 26 additions and 23 deletions
|
@ -49,39 +49,40 @@ SQLite::~SQLite()
|
|||
void SQLite::sql_exec(SQLiteCacheIndex idx)
|
||||
{
|
||||
sql_cache[idx];
|
||||
if (code == SQLITE_DONE) sqlite3_reset(stmt);
|
||||
}
|
||||
|
||||
SQLiteCacheIndex SQLite::sql_compile_to_cache(SQLiteCacheIndex idx, std::string_view sql)
|
||||
{
|
||||
using namespace std::string_literals;
|
||||
int rc;
|
||||
if (sql_cache[idx])
|
||||
{
|
||||
rc = sqlite3_prepare_v2(db, &*sql.begin(), sql.length(), &sql_cache[idx], nullptr);
|
||||
if (rc == SQLITE_ERROR)
|
||||
{
|
||||
const std::string err_msg = "Couldn't prepare SQL statement: "s + std::string(sqlite3_errmsg(db));
|
||||
throw std::runtime_error(err_msg);
|
||||
}
|
||||
}
|
||||
return idx;
|
||||
}
|
||||
|
||||
Types::User SQLite::get_user(unsigned long id)
|
||||
{
|
||||
// Setup function
|
||||
constexpr std::string_view sql{"SELECT acct FROM users WHERE id=?1"};
|
||||
auto begin = sql.begin();
|
||||
if (sql_cache[GET_USER_BY_ID]) sqlite3_prepare_v2(db, &*begin, sql.length(), &sql_cache[GET_USER_BY_ID], nullptr);
|
||||
|
||||
|
||||
Types::User luser{id, {}, false, {}, {}, {}};
|
||||
int code = 0;
|
||||
|
||||
static sqlite3_stmt* stmt = nullptr;
|
||||
// Setup function
|
||||
sql_compile_to_cache("SELECT acct FROM users WHERE id=?1");
|
||||
|
||||
|
||||
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_int64(stmt, 1, id);
|
||||
|
||||
while ((code = sqlite3_step(stmt)) == SQLITE_ROW)
|
||||
{
|
||||
sql_exec({id}, [luser](){
|
||||
luser.acct = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
if (code == SQLITE_DONE) sqlite3_reset(stmt);
|
||||
|
||||
return luser;
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include <functional>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <sqlite3.h>
|
||||
|
@ -42,7 +43,8 @@ namespace DB
|
|||
virtual Types::User get_user(unsigned long id) override;
|
||||
virtual Types::User get_user(const std::string& acct) override;
|
||||
private:
|
||||
void sql_exec(SQLiteCacheIndex idx);
|
||||
void sql_exec(SQLiteCacheIndex idx, std::function<void(int row)> func);
|
||||
SQLiteCacheIndex sql_compile_to_cache(SQLiteCacheIndex idx, std::string_view sql);
|
||||
|
||||
std::array<sqlite3_stmt*, SQLiteCacheIndex::COUNT> sql_cache;
|
||||
std::filesystem::path path;
|
||||
|
|
Loading…
Reference in a new issue