22
33#include " database/SQLiteClient.hpp"
44
5- // =============== Constructor and Destructor ===============
65SQLiteClient::SQLiteClient (const nlohmann::json& config, const SQLProvider& sqlProvider)
76 : db_(nullptr ),
87 sqlProvider_(sqlProvider),
98 config_(config),
109 lastInsertId_(0 ),
1110 affectedRows_(0 )
1211{
13- // Determine database file path
1412 if (config.contains (" file" ) && config[" file" ].is_string ()) {
1513 dbPath_ = config[" file" ].get <std::string>();
1614 } else if (config.contains (" name" ) && config[" name" ].is_string ()) {
@@ -29,7 +27,6 @@ SQLiteClient::~SQLiteClient() {
2927 Logger::Debug (" SQLiteClient destroyed" );
3028}
3129
32- // =============== Connection Management ===============
3330bool SQLiteClient::Connect () {
3431 std::lock_guard<std::mutex> lock (dbMutex_);
3532 if (db_) return true ;
@@ -48,10 +45,16 @@ bool SQLiteClient::Connect() {
4845 return false ;
4946 }
5047
51- // --- New: Set busy timeout to 5 seconds ---
5248 sqlite3_busy_timeout (db_, 5000 );
5349
5450 char * errMsg = nullptr ;
51+
52+ rc = sqlite3_exec (db_, " PRAGMA synchronous = NORMAL;" , nullptr , nullptr , &errMsg);
53+ if (rc != SQLITE_OK) {
54+ Logger::Warn (" Failed to enable synchronous NORMAL: {}" , errMsg ? errMsg : " unknown error" );
55+ sqlite3_free (errMsg);
56+ }
57+
5558 rc = sqlite3_exec (db_, " PRAGMA journal_mode=WAL;" , nullptr , nullptr , &errMsg);
5659 if (rc != SQLITE_OK) {
5760 Logger::Warn (" Failed to enable WAL mode: {}" , errMsg ? errMsg : " unknown error" );
@@ -115,7 +118,6 @@ void SQLiteClient::ReconnectAll() {
115118 Reconnect ();
116119}
117120
118- // =============== Connection Pool Management (dummy) ===============
119121bool SQLiteClient::InitializeConnectionPool (size_t /* minConnections*/ , size_t /* maxConnections*/ ) {
120122 Logger::Debug (" SQLiteClient: connection pool not implemented" );
121123 return true ;
@@ -131,7 +133,6 @@ size_t SQLiteClient::GetIdleConnections() const {
131133 return 0 ;
132134}
133135
134- // =============== Helper Methods ===============
135136bool SQLiteClient::ExecuteSql (const std::string& sql, std::vector<std::vector<std::string>>* results) {
136137 std::lock_guard<std::mutex> lock (dbMutex_);
137138 if (!db_) {
@@ -226,7 +227,6 @@ bool SQLiteClient::TableExists(const std::string& tableName) {
226227 return ExecuteSql (sql, &results) && !results.empty ();
227228}
228229
229- // =============== Query Operations ===============
230230nlohmann::json SQLiteClient::Query (const std::string& sql) {
231231 std::lock_guard<std::mutex> lock (dbMutex_);
232232 if (!db_) {
@@ -312,7 +312,6 @@ bool SQLiteClient::ExecuteWithParams(const std::string& sql, const std::vector<s
312312 return Execute (processedSql);
313313}
314314
315- // =============== Shard Operations ===============
316315nlohmann::json SQLiteClient::QueryShard (int /* shardId*/ , const std::string& sql) {
317316 return Query (sql);
318317}
@@ -328,7 +327,6 @@ bool SQLiteClient::ExecuteShardWithParams(int /*shardId*/, const std::string& sq
328327 return ExecuteWithParams (sql, params);
329328}
330329
331- // =============== Utility Methods ===============
332330int SQLiteClient::GetShardId (uint64_t entityId) const {
333331 return static_cast <int >(entityId % totalShards_);
334332}
@@ -345,7 +343,6 @@ int SQLiteClient::GetAffectedRows() {
345343 return affectedRows_;
346344}
347345
348- // =============== Statistics ===============
349346nlohmann::json SQLiteClient::GetDatabaseStats () {
350347 nlohmann::json stats;
351348 auto now = std::chrono::steady_clock::now ();
@@ -377,7 +374,6 @@ void SQLiteClient::ResetStats() {
377374 Logger::Info (" SQLite statistics reset" );
378375}
379376
380- // =============== Transaction Operations ===============
381377bool SQLiteClient::BeginTransaction () {
382378 std::string sql = sqlProvider_.GetQuery (" begin_transaction" );
383379 if (sql.empty ()) sql = " BEGIN TRANSACTION;" ;
@@ -419,7 +415,6 @@ bool SQLiteClient::ExecuteTransaction(const std::function<bool()>& operation) {
419415 return success;
420416}
421417
422- // =============== Player Data Operations ===============
423418bool SQLiteClient::SavePlayerData (uint64_t playerId, const nlohmann::json& data) {
424419 std::string sql = sqlProvider_.GetQuery (" save_player_data" );
425420 if (sql.empty ()) {
@@ -506,7 +501,6 @@ nlohmann::json SQLiteClient::GetPlayer(uint64_t playerId) {
506501 return nlohmann::json ();
507502}
508503
509- // =============== Game State Operations ===============
510504bool SQLiteClient::SaveGameState (const std::string& key, const nlohmann::json& state) {
511505 std::string sql = sqlProvider_.GetQuery (" save_game_state" );
512506 if (sql.empty ()) {
@@ -548,7 +542,6 @@ std::vector<std::string> SQLiteClient::ListGameStates() {
548542 return keys;
549543}
550544
551- // =============== World Data Operations ===============
552545bool SQLiteClient::SaveChunkData (int chunkX, int chunkZ, const nlohmann::json& chunkData) {
553546 std::lock_guard<std::mutex> lock (dbMutex_);
554547 if (!db_) {
@@ -561,7 +554,6 @@ bool SQLiteClient::SaveChunkData(int chunkX, int chunkZ, const nlohmann::json& c
561554 sql = " INSERT OR REPLACE INTO world_chunks (chunk_x, chunk_z, biome, data, last_updated) VALUES (?, ?, ?, ?, datetime('now'));" ;
562555 }
563556
564- // Begin immediate transaction
565557 char * errMsg = nullptr ;
566558 int rc = sqlite3_exec (db_, " BEGIN IMMEDIATE;" , nullptr , nullptr , &errMsg);
567559 if (rc != SQLITE_OK) {
@@ -570,7 +562,6 @@ bool SQLiteClient::SaveChunkData(int chunkX, int chunkZ, const nlohmann::json& c
570562 return false ;
571563 }
572564
573- // Prepare and execute the INSERT statement
574565 sqlite3_stmt* stmt = nullptr ;
575566 rc = sqlite3_prepare_v2 (db_, sql.c_str (), -1 , &stmt, nullptr );
576567 if (rc != SQLITE_OK) {
@@ -596,7 +587,6 @@ bool SQLiteClient::SaveChunkData(int chunkX, int chunkZ, const nlohmann::json& c
596587
597588 sqlite3_finalize (stmt);
598589
599- // Commit or rollback
600590 if (success) {
601591 rc = sqlite3_exec (db_, " COMMIT;" , nullptr , nullptr , &errMsg);
602592 if (rc != SQLITE_OK) {
@@ -667,7 +657,6 @@ std::vector<std::pair<int, int>> SQLiteClient::ListChunksInRange(int centerX, in
667657 return chunks;
668658}
669659
670- // =============== Inventory Operations ===============
671660bool SQLiteClient::SaveInventory (uint64_t playerId, const nlohmann::json& inventory) {
672661 std::string sql = sqlProvider_.GetQuery (" save_inventory" );
673662 if (sql.empty ()) {
@@ -688,7 +677,6 @@ nlohmann::json SQLiteClient::LoadInventory(uint64_t playerId) {
688677 return nlohmann::json ();
689678}
690679
691- // =============== Quest Operations ===============
692680bool SQLiteClient::SaveQuestProgress (uint64_t playerId, const std::string& questId, const nlohmann::json& progress) {
693681 std::string sql = sqlProvider_.GetQuery (" save_quest_progress" );
694682 if (sql.empty ()) {
0 commit comments