Skip to content

Commit

Permalink
We don't need to start new transaction for each table
Browse files Browse the repository at this point in the history
  • Loading branch information
Bara committed Mar 4, 2023
1 parent 99b545a commit 06d94a0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
46 changes: 31 additions & 15 deletions addons/sourcemod/scripting/surftimer/db/updater.sp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
4 changes: 1 addition & 3 deletions addons/sourcemod/scripting/surftimer/sql.sp
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,14 @@ 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;
g_bInTransactionChain = false;

GetDatabaseName(g_sDatabaseName, sizeof(g_sDatabaseName));
CheckDatabaseForUpdates();

SQL_UnlockDatabase(g_hDb);
SQL_UnlockDatabase(g_hDb_Updates);
LoopFloatDecimalTables();
CleanUpTablesRetvalsSteamId();

Expand Down

0 comments on commit 06d94a0

Please sign in to comment.