2021-12-05 17:54:36 +00:00
|
|
|
#include "ObjectIDManager.h"
|
|
|
|
|
|
|
|
// Custom Classes
|
|
|
|
#include "Database.h"
|
|
|
|
#include "dLogger.h"
|
|
|
|
|
|
|
|
// Static Variables
|
2022-07-28 13:39:57 +00:00
|
|
|
ObjectIDManager* ObjectIDManager::m_Address = nullptr;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
//! Initializes the manager
|
2022-07-28 13:39:57 +00:00
|
|
|
void ObjectIDManager::Initialize(dLogger* logger) {
|
|
|
|
this->mLogger = logger;
|
|
|
|
this->currentPersistentID = 1;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
try {
|
|
|
|
sql::PreparedStatement* stmt = Database::CreatePreppedStmt(
|
|
|
|
"SELECT last_object_id FROM object_id_tracker");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
sql::ResultSet* result = stmt->executeQuery();
|
|
|
|
auto next = result->next();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!next) {
|
|
|
|
sql::PreparedStatement* insertStmt = Database::CreatePreppedStmt(
|
|
|
|
"INSERT INTO object_id_tracker VALUES (1)");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
insertStmt->execute();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
delete insertStmt;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
while (next) {
|
|
|
|
this->currentPersistentID =
|
|
|
|
result->getInt(1) > 0 ? result->getInt(1) : 1;
|
|
|
|
next = result->next();
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
delete result;
|
|
|
|
delete stmt;
|
|
|
|
} catch (sql::SQLException& e) {
|
|
|
|
mLogger->Log("ObjectIDManager", "Unable to fetch max persistent object "
|
|
|
|
"ID in use. Defaulting to 1.");
|
|
|
|
mLogger->Log("ObjectIDManager", "SQL error: %s", e.what());
|
|
|
|
this->currentPersistentID = 1;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Generates a new persistent ID
|
|
|
|
uint32_t ObjectIDManager::GeneratePersistentID(void) {
|
2022-07-28 13:39:57 +00:00
|
|
|
uint32_t toReturn = ++this->currentPersistentID;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
// So we peroidically save our ObjID to the database:
|
2022-11-28 00:48:46 +00:00
|
|
|
// if (toReturn % 25 == 0) { // TEMP: DISABLED FOR DEBUG / DEVELOPMENT!
|
2022-07-28 13:39:57 +00:00
|
|
|
sql::PreparedStatement* stmt = Database::CreatePreppedStmt(
|
|
|
|
"UPDATE object_id_tracker SET last_object_id=?");
|
|
|
|
stmt->setUInt(1, toReturn);
|
|
|
|
stmt->execute();
|
|
|
|
delete stmt;
|
2022-11-28 00:48:46 +00:00
|
|
|
// }
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
return toReturn;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectIDManager::SaveToDatabase() {
|
2022-07-28 13:39:57 +00:00
|
|
|
sql::PreparedStatement* stmt = Database::CreatePreppedStmt(
|
|
|
|
"UPDATE object_id_tracker SET last_object_id=?");
|
|
|
|
stmt->setUInt(1, currentPersistentID);
|
|
|
|
stmt->execute();
|
|
|
|
delete stmt;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|