Skip to content

Commit fe1caf1

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

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

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

+33-15
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,13 @@ 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+
// Waiting a frame fixed "Lost connection" error for me...
227+
// maybe it was a random thing, but I'll keep it for now.
228+
RequestFrame(StartLoadingPlayerStuff);
229+
return;
230+
}
227231
}
228232

229233
CheckDatabaseForUpdates();
@@ -387,6 +391,11 @@ public void SQLCleanUpTables(Handle owner, Handle hndl, const char[] error, any
387391
}
388392
}
389393

394+
public void StartLoadingPlayerStuff()
395+
{
396+
SelectPlayersStuff();
397+
}
398+
390399
void SelectPlayersStuff()
391400
{
392401
Transaction tTransaction = new Transaction();
@@ -424,10 +433,11 @@ void SelectPlayersStuff()
424433

425434
public void SQLTxn_GetPlayerDataSuccess(Database db, any data, int numQueries, DBResultSet[] results, any[] queryData)
426435
{
436+
int iQueries = 0;
437+
Transaction tTransaction = new Transaction();
438+
427439
for (int i = 0; i < numQueries; i++)
428440
{
429-
int iQueries = 0;
430-
Transaction tTransaction = new Transaction();
431441
char sSteamId2[32], sName[64], sSteamId64[128], sQuery[1024];
432442
// ck_bonus
433443
if (g_sSteamIdTablesCleanup[i][3] == 'b')
@@ -561,15 +571,17 @@ public void SQLTxn_GetPlayerDataSuccess(Database db, any data, int numQueries, D
561571
}
562572
}
563573

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

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);
577+
if (iQueries == 0)
578+
{
579+
CheckDatabaseForUpdates();
580+
return;
572581
}
582+
583+
PrintToServer("Transaction started with %d queries started...", iQueries);
584+
SQL_ExecuteTransaction(g_hDb, tTransaction, SQLTxn_InsertToPlayersSuccess, SQLTxn_InsertToPlayersFailed, .priority=DBPrio_High);
573585
}
574586

575587
public void SQLTxn_InsertToPlayersSuccess(Database db, any data, int numQueries, DBResultSet[] results, any[] queryData)
@@ -580,13 +592,19 @@ public void SQLTxn_InsertToPlayersSuccess(Database db, any data, int numQueries,
580592
public void SQLTxn_InsertToPlayersFailed(Database db, any data, int numQueries, const char[] error, int failIndex, any[] queryData)
581593
{
582594
SQL_FastQuery(g_hDb, "DROP TABLE IF EXISTS ck_players;");
583-
595+
584596
SetFailState("[SurfTimer] Failed while adding data to table ck_players! Error: %s", error);
585597
}
586598

587599
public void SQLTxn_GetPlayerDataFailed(Database db, any data, int numQueries, const char[] error, int failIndex, any[] queryData)
588600
{
589601
SQL_FastQuery(g_hDb, "DROP TABLE IF EXISTS ck_players;");
590-
602+
603+
if (failIndex == -1)
604+
{
605+
SetFailState("[SurfTimer] Failed while getting data! Error: %s", error);
606+
return;
607+
}
608+
591609
SetFailState("[SurfTimer] Failed while getting data from table %s! Error: %s", g_sSteamIdTablesCleanup[failIndex], error);
592610
}

addons/sourcemod/scripting/surftimer/sql.sp

+1-3
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)