From 0a8c6e5ad9c6345549202c033c6dfc590a5936c6 Mon Sep 17 00:00:00 2001 From: Jett <55758076+Jettford@users.noreply.github.com> Date: Tue, 1 Nov 2022 22:18:07 +0000 Subject: [PATCH] Make changes to certain database functions and a debug assert - Replace all interaction of std::string and sqlString. - Add a return before a debug assertion can be triggered by lvl chunks being loaded on server start. --- dCommon/GeneralUtils.cpp | 1 + dDatabase/Database.cpp | 4 ++-- dDatabase/MigrationRunner.cpp | 10 +++++----- dMasterServer/MasterServer.cpp | 4 ++-- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/dCommon/GeneralUtils.cpp b/dCommon/GeneralUtils.cpp index 4a6c47399..98495a044 100644 --- a/dCommon/GeneralUtils.cpp +++ b/dCommon/GeneralUtils.cpp @@ -50,6 +50,7 @@ bool _IsSuffixChar(uint8_t c) { bool GeneralUtils::_NextUTF8Char(std::string_view& slice, uint32_t& out) { size_t rem = slice.length(); + if (rem == 0) return false; const uint8_t* bytes = (const uint8_t*)&slice.front(); if (rem > 0) { uint8_t first = bytes[0]; diff --git a/dDatabase/Database.cpp b/dDatabase/Database.cpp index c301cbd90..915893778 100644 --- a/dDatabase/Database.cpp +++ b/dDatabase/Database.cpp @@ -35,8 +35,8 @@ void Database::Connect(const string& host, const string& database, const string& } void Database::Connect() { - con = driver->connect(Database::props); - con->setSchema(Database::database); + con = driver->connect(Database::props["hostName"].c_str(), Database::props["user"].c_str(), Database::props["password"].c_str()); + con->setSchema(Database::database.c_str()); } void Database::Destroy(std::string source, bool log) { diff --git a/dDatabase/MigrationRunner.cpp b/dDatabase/MigrationRunner.cpp index 017ebe322..253126088 100644 --- a/dDatabase/MigrationRunner.cpp +++ b/dDatabase/MigrationRunner.cpp @@ -45,7 +45,7 @@ void MigrationRunner::RunMigrations() { } stmt = Database::CreatePreppedStmt("SELECT name FROM migration_history WHERE name = ?;"); - stmt->setString(1, migration.name); + stmt->setString(1, migration.name.c_str()); auto* res = stmt->executeQuery(); bool doExit = res->next(); delete res; @@ -56,11 +56,11 @@ void MigrationRunner::RunMigrations() { if (migration.name == "5_brick_model_sd0.sql") { runSd0Migrations = true; } else { - finalSQL.append(migration.data); + finalSQL.append(migration.data.c_str()); } stmt = Database::CreatePreppedStmt("INSERT INTO migration_history (name) VALUES (?);"); - stmt->setString(1, migration.name); + stmt->setString(1, migration.name.c_str()); stmt->execute(); delete stmt; } @@ -76,7 +76,7 @@ void MigrationRunner::RunMigrations() { for (auto& query : migration) { try { if (query.empty()) continue; - simpleStatement->execute(query); + simpleStatement->execute(query.c_str()); } catch (sql::SQLException& e) { Game::logger->Log("MigrationRunner", "Encountered error running migration: %s", e.what()); } @@ -103,7 +103,7 @@ void MigrationRunner::RunSQLiteMigrations() { if (migration.data.empty()) continue; stmt = Database::CreatePreppedStmt("SELECT name FROM migration_history WHERE name = ?;"); - stmt->setString(1, migration.name); + stmt->setString(1, migration.name.c_str()); auto* res = stmt->executeQuery(); bool doExit = res->next(); delete res; diff --git a/dMasterServer/MasterServer.cpp b/dMasterServer/MasterServer.cpp index cd54913a3..e5203a7d0 100644 --- a/dMasterServer/MasterServer.cpp +++ b/dMasterServer/MasterServer.cpp @@ -230,7 +230,7 @@ int main(int argc, char** argv) { //If we found a server, update it's IP and port to the current one. if (result->next()) { auto* updateStatement = Database::CreatePreppedStmt("UPDATE `servers` SET `ip` = ?, `port` = ? WHERE `id` = ?"); - updateStatement->setString(1, master_server_ip); + updateStatement->setString(1, master_server_ip.c_str()); updateStatement->setInt(2, Game::server->GetPort()); updateStatement->setInt(3, result->getInt("id")); updateStatement->execute(); @@ -238,7 +238,7 @@ int main(int argc, char** argv) { } else { //If we didn't find a server, create one. auto* insertStatement = Database::CreatePreppedStmt("INSERT INTO `servers` (`name`, `ip`, `port`, `state`, `version`) VALUES ('master', ?, ?, 0, 171023)"); - insertStatement->setString(1, master_server_ip); + insertStatement->setString(1, master_server_ip.c_str()); insertStatement->setInt(2, Game::server->GetPort()); insertStatement->execute(); delete insertStatement;