Skip to content

Commit 06d94a0

Browse files
committed
We don't need to start new transaction for each table
1 parent 99b545a commit 06d94a0

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

addons/sourcemod/scripting/surftimer/db/updater.sp

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,11 @@ void CheckDatabaseForUpdates()
9999
delete results;
100100
return;
101101
}
102-
103102
// Version 13 - End
104103

105104
if (!SQL_FastQuery(g_hDb, "SELECT accountid FROM ck_players LIMIT 1"))
106105
{
107-
db_upgradeDatabase(14);
106+
db_upgradeDatabase(14, true);
108107
return;
109108
}
110109

@@ -222,8 +221,11 @@ void db_upgradeDatabase(int ver, bool skipErrorCheck = false)
222221
}
223222
else if (ver == 14)
224223
{
225-
SQL_FastQuery(g_hDb, sql_createPlayers);
226-
SelectPlayersStuff();
224+
if (SQL_FastQuery(g_hDb, sql_createPlayers))
225+
{
226+
RequestFrame(StartLoadingPlayerStuff);
227+
return;
228+
}
227229
}
228230

229231
CheckDatabaseForUpdates();
@@ -387,6 +389,11 @@ public void SQLCleanUpTables(Handle owner, Handle hndl, const char[] error, any
387389
}
388390
}
389391

392+
public void StartLoadingPlayerStuff()
393+
{
394+
SelectPlayersStuff();
395+
}
396+
390397
void SelectPlayersStuff()
391398
{
392399
Transaction tTransaction = new Transaction();
@@ -424,10 +431,11 @@ void SelectPlayersStuff()
424431

425432
public void SQLTxn_GetPlayerDataSuccess(Database db, any data, int numQueries, DBResultSet[] results, any[] queryData)
426433
{
434+
int iQueries = 0;
435+
Transaction tTransaction = new Transaction();
436+
427437
for (int i = 0; i < numQueries; i++)
428438
{
429-
int iQueries = 0;
430-
Transaction tTransaction = new Transaction();
431439
char sSteamId2[32], sName[64], sSteamId64[128], sQuery[1024];
432440
// ck_bonus
433441
if (g_sSteamIdTablesCleanup[i][3] == 'b')
@@ -561,15 +569,17 @@ public void SQLTxn_GetPlayerDataSuccess(Database db, any data, int numQueries, D
561569
}
562570
}
563571

564-
if (iQueries == 0)
565-
{
566-
delete tTransaction;
567-
continue;
568-
}
572+
PrintToServer("Added %d Queries to Transaction for table %s", iQueries, g_sSteamIdTablesCleanup[i]);
573+
}
569574

570-
PrintToServer("Transaction for %s with %d queries started...", g_sSteamIdTablesCleanup[i], iQueries);
571-
SQL_ExecuteTransaction(g_hDb, tTransaction, SQLTxn_InsertToPlayersSuccess, SQLTxn_InsertToPlayersFailed, .priority=DBPrio_High);
575+
if (iQueries == 0)
576+
{
577+
CheckDatabaseForUpdates();
578+
return;
572579
}
580+
581+
PrintToServer("Transaction started with %d queries started...", iQueries);
582+
SQL_ExecuteTransaction(g_hDb, tTransaction, SQLTxn_InsertToPlayersSuccess, SQLTxn_InsertToPlayersFailed, .priority=DBPrio_High);
573583
}
574584

575585
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,
580590
public void SQLTxn_InsertToPlayersFailed(Database db, any data, int numQueries, const char[] error, int failIndex, any[] queryData)
581591
{
582592
SQL_FastQuery(g_hDb, "DROP TABLE IF EXISTS ck_players;");
583-
593+
584594
SetFailState("[SurfTimer] Failed while adding data to table ck_players! Error: %s", error);
585595
}
586596

587597
public void SQLTxn_GetPlayerDataFailed(Database db, any data, int numQueries, const char[] error, int failIndex, any[] queryData)
588598
{
589599
SQL_FastQuery(g_hDb, "DROP TABLE IF EXISTS ck_players;");
590-
600+
601+
if (failIndex == -1)
602+
{
603+
SetFailState("[SurfTimer] Failed while getting data! Error: %s", error);
604+
return;
605+
}
606+
591607
SetFailState("[SurfTimer] Failed while getting data from table %s! Error: %s", g_sSteamIdTablesCleanup[failIndex], error);
592608
}

addons/sourcemod/scripting/surftimer/sql.sp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,14 @@ public void db_setupDatabase()
4242
// If updating from a previous version
4343
SQL_LockDatabase(g_hDb);
4444
SQL_FastQuery(g_hDb, "SET NAMES 'utf8mb4'");
45+
SQL_UnlockDatabase(g_hDb);
4546

4647
// Check if tables need to be Created or database needs to be upgraded
4748
g_bRenaming = false;
4849
g_bInTransactionChain = false;
4950

5051
GetDatabaseName(g_sDatabaseName, sizeof(g_sDatabaseName));
5152
CheckDatabaseForUpdates();
52-
53-
SQL_UnlockDatabase(g_hDb);
54-
SQL_UnlockDatabase(g_hDb_Updates);
5553
LoopFloatDecimalTables();
5654
CleanUpTablesRetvalsSteamId();
5755

0 commit comments

Comments
 (0)