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

Improve error handling of map database creation

This commit is contained in:
sfan5 2025-02-26 18:50:41 +01:00
parent 7abaa8d4cd
commit d54646d342
7 changed files with 71 additions and 32 deletions

View file

@ -97,7 +97,7 @@ void Database_PostgreSQL::ping()
bool Database_PostgreSQL::initialized() const
{
return (PQstatus(m_conn) == CONNECTION_OK);
return m_conn && PQstatus(m_conn) == CONNECTION_OK;
}
PGresult *Database_PostgreSQL::checkResults(PGresult *result, bool clear)

View file

@ -9,20 +9,20 @@
#include "database.h"
#include "util/basic_macros.h"
class Settings;
class Database_PostgreSQL: public Database
// Template class for PostgreSQL based data storage
class Database_PostgreSQL : public Database
{
public:
Database_PostgreSQL(const std::string &connect_string, const char *type);
~Database_PostgreSQL();
void beginSave();
void endSave();
void beginSave() override;
void endSave() override;
void rollback();
bool initialized() const;
bool initialized() const override;
void verifyDatabase() override;
protected:
// Conversion helpers
@ -73,7 +73,6 @@ protected:
}
void createTableIfNotExists(const std::string &table_name, const std::string &definition);
void verifyDatabase();
// Database initialization
void connectToDatabase();
@ -99,6 +98,12 @@ private:
int m_pgversion = 0;
};
// Not sure why why we have to do this. can't C++ figure it out on its own?
#define PARENT_CLASS_FUNCS \
void beginSave() { Database_PostgreSQL::beginSave(); } \
void endSave() { Database_PostgreSQL::endSave(); } \
void verifyDatabase() { Database_PostgreSQL::verifyDatabase(); }
class MapDatabasePostgreSQL : private Database_PostgreSQL, public MapDatabase
{
public:
@ -110,8 +115,7 @@ public:
bool deleteBlock(const v3s16 &pos);
void listAllLoadableBlocks(std::vector<v3s16> &dst);
void beginSave() { Database_PostgreSQL::beginSave(); }
void endSave() { Database_PostgreSQL::endSave(); }
PARENT_CLASS_FUNCS
protected:
virtual void createDatabase();
@ -129,6 +133,8 @@ public:
bool removePlayer(const std::string &name);
void listPlayers(std::vector<std::string> &res);
PARENT_CLASS_FUNCS
protected:
virtual void createDatabase();
virtual void initStatements();
@ -143,8 +149,6 @@ public:
AuthDatabasePostgreSQL(const std::string &connect_string);
virtual ~AuthDatabasePostgreSQL() = default;
virtual void verifyDatabase() { Database_PostgreSQL::verifyDatabase(); }
virtual bool getAuth(const std::string &name, AuthEntry &res);
virtual bool saveAuth(const AuthEntry &authEntry);
virtual bool createAuth(AuthEntry &authEntry);
@ -152,6 +156,8 @@ public:
virtual void listNames(std::vector<std::string> &res);
virtual void reload();
PARENT_CLASS_FUNCS
protected:
virtual void createDatabase();
virtual void initStatements();
@ -176,10 +182,11 @@ public:
bool removeModEntries(const std::string &modname);
void listMods(std::vector<std::string> *res);
void beginSave() { Database_PostgreSQL::beginSave(); }
void endSave() { Database_PostgreSQL::endSave(); }
PARENT_CLASS_FUNCS
protected:
virtual void createDatabase();
virtual void initStatements();
};
#undef PARENT_CLASS_FUNCS

View file

@ -19,17 +19,17 @@ class Database_SQLite3 : public Database
public:
virtual ~Database_SQLite3();
void beginSave();
void endSave();
void beginSave() override;
void endSave() override;
bool initialized() const { return m_initialized; }
bool initialized() const override { return m_initialized; }
/// @note not thread-safe
void verifyDatabase() override;
protected:
Database_SQLite3(const std::string &savedir, const std::string &dbname);
// Open and initialize the database if needed (not thread-safe)
void verifyDatabase();
// Check if a specific table exists
bool checkTable(const char *table);
@ -160,6 +160,12 @@ private:
static int busyHandler(void *data, int count);
};
// Not sure why why we have to do this. can't C++ figure it out on its own?
#define PARENT_CLASS_FUNCS \
void beginSave() { Database_SQLite3::beginSave(); } \
void endSave() { Database_SQLite3::endSave(); } \
void verifyDatabase() { Database_SQLite3::verifyDatabase(); }
class MapDatabaseSQLite3 : private Database_SQLite3, public MapDatabase
{
public:
@ -171,8 +177,8 @@ public:
bool deleteBlock(const v3s16 &pos);
void listAllLoadableBlocks(std::vector<v3s16> &dst);
void beginSave() { Database_SQLite3::beginSave(); }
void endSave() { Database_SQLite3::endSave(); }
PARENT_CLASS_FUNCS
protected:
virtual void createDatabase();
virtual void initStatements();
@ -201,6 +207,8 @@ public:
bool removePlayer(const std::string &name);
void listPlayers(std::vector<std::string> &res);
PARENT_CLASS_FUNCS
protected:
virtual void createDatabase();
virtual void initStatements();
@ -238,6 +246,8 @@ public:
virtual void listNames(std::vector<std::string> &res);
virtual void reload();
PARENT_CLASS_FUNCS
protected:
virtual void createDatabase();
virtual void initStatements();
@ -273,8 +283,7 @@ public:
virtual bool removeModEntries(const std::string &modname);
virtual void listMods(std::vector<std::string> *res);
virtual void beginSave() { Database_SQLite3::beginSave(); }
virtual void endSave() { Database_SQLite3::endSave(); }
PARENT_CLASS_FUNCS
protected:
virtual void createDatabase();
@ -289,3 +298,5 @@ private:
sqlite3_stmt *m_stmt_remove = nullptr;
sqlite3_stmt *m_stmt_remove_all = nullptr;
};
#undef PARENT_CLASS_FUNCS

View file

@ -16,7 +16,12 @@ class Database
public:
virtual void beginSave() = 0;
virtual void endSave() = 0;
/// @return true if database connection is open
virtual bool initialized() const { return true; }
/// Open and initialize the database if needed
virtual void verifyDatabase() {};
};
class MapDatabase : public Database