1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-02 16:38:41 +00:00

Some cleanups in Database_SQLite3

This commit is contained in:
sfan5 2025-02-08 16:23:15 +01:00
parent 166e02955e
commit e8728acc5c
2 changed files with 111 additions and 112 deletions

View file

@ -13,6 +13,7 @@ extern "C" {
#include "sqlite3.h"
}
// Template class for SQLite3 based data storage
class Database_SQLite3 : public Database
{
public:
@ -22,18 +23,25 @@ public:
void endSave();
bool initialized() const { return m_initialized; }
protected:
Database_SQLite3(const std::string &savedir, const std::string &dbname);
// Open and initialize the database if needed
// Open and initialize the database if needed (not thread-safe)
void verifyDatabase();
// Convertors
/* Value conversion helpers */
inline void str_to_sqlite(sqlite3_stmt *s, int iCol, std::string_view str) const
{
sqlite3_vrfy(sqlite3_bind_text(s, iCol, str.data(), str.size(), NULL));
}
inline void blob_to_sqlite(sqlite3_stmt *s, int iCol, std::string_view str) const
{
sqlite3_vrfy(sqlite3_bind_blob(s, iCol, str.data(), str.size(), NULL));
}
inline void int_to_sqlite(sqlite3_stmt *s, int iCol, int val) const
{
sqlite3_vrfy(sqlite3_bind_int(s, iCol, val));
@ -104,12 +112,14 @@ protected:
sqlite_to_float(s, iCol + 2));
}
// Query verifiers helpers
// Helper for verifying result of sqlite3_step() and such
inline void sqlite3_vrfy(int s, std::string_view m = "", int r = SQLITE_OK) const
{
if (s != r) {
std::string msg(m);
msg.append(": ").append(sqlite3_errmsg(m_database));
if (!msg.empty())
msg.append(": ");
msg.append(sqlite3_errmsg(m_database));
throw DatabaseException(msg);
}
}
@ -119,24 +129,27 @@ protected:
sqlite3_vrfy(s, m, r);
}
// Create the database structure
// Called after opening a fresh database file. Should create tables and indices.
virtual void createDatabase() = 0;
// Should prepare the necessary statements.
virtual void initStatements() = 0;
sqlite3 *m_database = nullptr;
private:
// Open the database
void openDatabase();
bool m_initialized = false;
std::string m_savedir = "";
std::string m_dbname = "";
const std::string m_savedir;
const std::string m_dbname;
sqlite3_stmt *m_stmt_begin = nullptr;
sqlite3_stmt *m_stmt_end = nullptr;
s64 m_busy_handler_data[2];
u64 m_busy_handler_data[2];
static int busyHandler(void *data, int count);
};