From 06d94a0e88473e364f890582299a11254f52eab8 Mon Sep 17 00:00:00 2001 From: Bara Date: Sat, 4 Mar 2023 02:45:48 +0100 Subject: [PATCH] We don't need to start new transaction for each table --- .../scripting/surftimer/db/updater.sp | 46 +++++++++++++------ addons/sourcemod/scripting/surftimer/sql.sp | 4 +- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/addons/sourcemod/scripting/surftimer/db/updater.sp b/addons/sourcemod/scripting/surftimer/db/updater.sp index 349871c4..682ce0b1 100644 --- a/addons/sourcemod/scripting/surftimer/db/updater.sp +++ b/addons/sourcemod/scripting/surftimer/db/updater.sp @@ -99,12 +99,11 @@ void CheckDatabaseForUpdates() delete results; return; } - // Version 13 - End if (!SQL_FastQuery(g_hDb, "SELECT accountid FROM ck_players LIMIT 1")) { - db_upgradeDatabase(14); + db_upgradeDatabase(14, true); return; } @@ -222,8 +221,11 @@ void db_upgradeDatabase(int ver, bool skipErrorCheck = false) } else if (ver == 14) { - SQL_FastQuery(g_hDb, sql_createPlayers); - SelectPlayersStuff(); + if (SQL_FastQuery(g_hDb, sql_createPlayers)) + { + RequestFrame(StartLoadingPlayerStuff); + return; + } } CheckDatabaseForUpdates(); @@ -387,6 +389,11 @@ public void SQLCleanUpTables(Handle owner, Handle hndl, const char[] error, any } } +public void StartLoadingPlayerStuff() +{ + SelectPlayersStuff(); +} + void SelectPlayersStuff() { Transaction tTransaction = new Transaction(); @@ -424,10 +431,11 @@ void SelectPlayersStuff() public void SQLTxn_GetPlayerDataSuccess(Database db, any data, int numQueries, DBResultSet[] results, any[] queryData) { + int iQueries = 0; + Transaction tTransaction = new Transaction(); + for (int i = 0; i < numQueries; i++) { - int iQueries = 0; - Transaction tTransaction = new Transaction(); char sSteamId2[32], sName[64], sSteamId64[128], sQuery[1024]; // ck_bonus if (g_sSteamIdTablesCleanup[i][3] == 'b') @@ -561,15 +569,17 @@ public void SQLTxn_GetPlayerDataSuccess(Database db, any data, int numQueries, D } } - if (iQueries == 0) - { - delete tTransaction; - continue; - } + PrintToServer("Added %d Queries to Transaction for table %s", iQueries, g_sSteamIdTablesCleanup[i]); + } - PrintToServer("Transaction for %s with %d queries started...", g_sSteamIdTablesCleanup[i], iQueries); - SQL_ExecuteTransaction(g_hDb, tTransaction, SQLTxn_InsertToPlayersSuccess, SQLTxn_InsertToPlayersFailed, .priority=DBPrio_High); + if (iQueries == 0) + { + CheckDatabaseForUpdates(); + return; } + + PrintToServer("Transaction started with %d queries started...", iQueries); + SQL_ExecuteTransaction(g_hDb, tTransaction, SQLTxn_InsertToPlayersSuccess, SQLTxn_InsertToPlayersFailed, .priority=DBPrio_High); } public void SQLTxn_InsertToPlayersSuccess(Database db, any data, int numQueries, DBResultSet[] results, any[] queryData) @@ -580,13 +590,19 @@ public void SQLTxn_InsertToPlayersSuccess(Database db, any data, int numQueries, public void SQLTxn_InsertToPlayersFailed(Database db, any data, int numQueries, const char[] error, int failIndex, any[] queryData) { SQL_FastQuery(g_hDb, "DROP TABLE IF EXISTS ck_players;"); - + SetFailState("[SurfTimer] Failed while adding data to table ck_players! Error: %s", error); } public void SQLTxn_GetPlayerDataFailed(Database db, any data, int numQueries, const char[] error, int failIndex, any[] queryData) { SQL_FastQuery(g_hDb, "DROP TABLE IF EXISTS ck_players;"); - + + if (failIndex == -1) + { + SetFailState("[SurfTimer] Failed while getting data! Error: %s", error); + return; + } + SetFailState("[SurfTimer] Failed while getting data from table %s! Error: %s", g_sSteamIdTablesCleanup[failIndex], error); } diff --git a/addons/sourcemod/scripting/surftimer/sql.sp b/addons/sourcemod/scripting/surftimer/sql.sp index 17b2393c..fc17e34e 100644 --- a/addons/sourcemod/scripting/surftimer/sql.sp +++ b/addons/sourcemod/scripting/surftimer/sql.sp @@ -42,6 +42,7 @@ public void db_setupDatabase() // If updating from a previous version SQL_LockDatabase(g_hDb); SQL_FastQuery(g_hDb, "SET NAMES 'utf8mb4'"); + SQL_UnlockDatabase(g_hDb); // Check if tables need to be Created or database needs to be upgraded g_bRenaming = false; @@ -49,9 +50,6 @@ public void db_setupDatabase() GetDatabaseName(g_sDatabaseName, sizeof(g_sDatabaseName)); CheckDatabaseForUpdates(); - - SQL_UnlockDatabase(g_hDb); - SQL_UnlockDatabase(g_hDb_Updates); LoopFloatDecimalTables(); CleanUpTablesRetvalsSteamId();