Update SQL plugins for transitional syntax.

This commit is contained in:
David Anderson 2014-12-13 16:35:12 -08:00
parent b1cb06c5ce
commit b60ef1669c
3 changed files with 376 additions and 388 deletions

View File

@ -48,8 +48,8 @@ public Plugin:myinfo =
public OnRebuildAdminCache(AdminCachePart:part) public OnRebuildAdminCache(AdminCachePart:part)
{ {
/* First try to get a database connection */ /* First try to get a database connection */
decl String:error[255]; char error[255];
new Handle:db; Database db;
if (SQL_CheckConfig("admins")) if (SQL_CheckConfig("admins"))
{ {
@ -76,13 +76,13 @@ public OnRebuildAdminCache(AdminCachePart:part)
delete db; delete db;
} }
FetchUsers(Handle:db) void FetchUsers(Database db)
{ {
decl String:query[255], String:error[255]; char query[255], error[255];
new Handle:hQuery; DBResultSet rs;
Format(query, sizeof(query), "SELECT id, authtype, identity, password, flags, name, immunity FROM sm_admins"); Format(query, sizeof(query), "SELECT id, authtype, identity, password, flags, name, immunity FROM sm_admins");
if ((hQuery = SQL_Query(db, query)) == null) if ((rs = SQL_Query(db, query)) == null)
{ {
SQL_GetError(db, error, sizeof(error)); SQL_GetError(db, error, sizeof(error));
LogError("FetchUsers() query failed: %s", query); LogError("FetchUsers() query failed: %s", query);
@ -90,30 +90,31 @@ FetchUsers(Handle:db)
return; return;
} }
decl String:authtype[16]; char authtype[16];
decl String:identity[80]; char identity[80];
decl String:password[80]; char password[80];
decl String:flags[32]; char flags[32];
decl String:name[80]; char name[80];
new immunity; int immunity;
new AdminId:adm, id; AdminId adm;
new GroupId:gid; GroupId gid;
int id;
/* Keep track of a mapping from admin DB IDs to internal AdminIds to /* Keep track of a mapping from admin DB IDs to internal AdminIds to
* enable group lookups en masse */ * enable group lookups en masse */
new Handle:htAdmins = CreateTrie(); StringMap htAdmins = new StringMap();
decl String:key[16]; char key[16];
while (SQL_FetchRow(hQuery)) while (rs.FetchRow())
{ {
id = SQL_FetchInt(hQuery, 0); id = rs.FetchInt(0);
IntToString(id, key, sizeof(key)); IntToString(id, key, sizeof(key));
SQL_FetchString(hQuery, 1, authtype, sizeof(authtype)); rs.FetchString(1, authtype, sizeof(authtype));
SQL_FetchString(hQuery, 2, identity, sizeof(identity)); rs.FetchString(2, identity, sizeof(identity));
SQL_FetchString(hQuery, 3, password, sizeof(password)); rs.FetchString(3, password, sizeof(password));
SQL_FetchString(hQuery, 4, flags, sizeof(flags)); rs.FetchString(4, flags, sizeof(flags));
SQL_FetchString(hQuery, 5, name, sizeof(name)); rs.FetchString(5, name, sizeof(name));
immunity = SQL_FetchInt(hQuery, 6); immunity = rs.FetchInt(6);
/* Use a pre-existing admin if we can */ /* Use a pre-existing admin if we can */
if ((adm = FindAdminByIdentity(authtype, identity)) == INVALID_ADMIN_ID) if ((adm = FindAdminByIdentity(authtype, identity)) == INVALID_ADMIN_ID)
@ -126,11 +127,11 @@ FetchUsers(Handle:db)
} }
} }
SetTrieValue(htAdmins, key, adm); htAdmins.SetValue(key, adm);
#if defined _DEBUG #if defined _DEBUG
PrintToServer("Found SQL admin (%d,%s,%s,%s,%s,%s,%d):%d", id, authtype, identity, password, flags, name, immunity, adm); PrintToServer("Found SQL admin (%d,%s,%s,%s,%s,%s,%d):%d", id, authtype, identity, password, flags, name, immunity, adm);
#endif #endif
/* See if this admin wants a password */ /* See if this admin wants a password */
if (password[0] != '\0') if (password[0] != '\0')
@ -139,8 +140,8 @@ FetchUsers(Handle:db)
} }
/* Apply each flag */ /* Apply each flag */
new len = strlen(flags); int len = strlen(flags);
new AdminFlag:flag; AdminFlag flag;
for (new i=0; i<len; i++) for (new i=0; i<len; i++)
{ {
if (!FindFlagByChar(flags[i], flag)) if (!FindFlagByChar(flags[i], flag))
@ -153,9 +154,10 @@ FetchUsers(Handle:db)
SetAdminImmunityLevel(adm, immunity); SetAdminImmunityLevel(adm, immunity);
} }
new Handle:hGroupQuery; delete rs;
Format(query, sizeof(query), "SELECT ag.admin_id AS id, g.name FROM sm_admins_groups ag JOIN sm_groups g ON ag.group_id = g.id ORDER BY id, inherit_order ASC"); Format(query, sizeof(query), "SELECT ag.admin_id AS id, g.name FROM sm_admins_groups ag JOIN sm_groups g ON ag.group_id = g.id ORDER BY id, inherit_order ASC");
if ((hGroupQuery = SQL_Query(db, query)) == null) if ((rs = SQL_Query(db, query)) == null)
{ {
SQL_GetError(db, error, sizeof(error)); SQL_GetError(db, error, sizeof(error));
LogError("FetchUsers() query failed: %s", query); LogError("FetchUsers() query failed: %s", query);
@ -163,13 +165,13 @@ FetchUsers(Handle:db)
return; return;
} }
decl String:group[80]; char group[80];
while (SQL_FetchRow(hGroupQuery)) while (rs.FetchRow())
{ {
IntToString(SQL_FetchInt(hGroupQuery, 0), key, sizeof(key)); IntToString(rs.FetchInt(0), key, sizeof(key));
SQL_FetchString(hGroupQuery, 1, group, sizeof(group)); rs.FetchString(1, group, sizeof(group));
if (GetTrieValue(htAdmins, key, adm)) if (htAdmins.GetValue(key, adm))
{ {
if ((gid = FindAdmGroup(group)) == INVALID_GROUP_ID) if ((gid = FindAdmGroup(group)) == INVALID_GROUP_ID)
{ {
@ -181,21 +183,20 @@ FetchUsers(Handle:db)
} }
} }
delete hQuery; delete rs;
delete hGroupQuery;
delete htAdmins; delete htAdmins;
} }
FetchGroups(Handle:db) FetchGroups(Database db)
{ {
decl String:query[255]; char query[255];
new Handle:hQuery; DBResultSet rs;
Format(query, sizeof(query), "SELECT flags, name, immunity_level FROM sm_groups"); Format(query, sizeof(query), "SELECT flags, name, immunity_level FROM sm_groups");
if ((hQuery = SQL_Query(db, query)) == null) if ((rs = SQL_Query(db, query)) == null)
{ {
decl String:error[255]; char error[255];
SQL_GetError(db, error, sizeof(error)); SQL_GetError(db, error, sizeof(error));
LogError("FetchGroups() query failed: %s", query); LogError("FetchGroups() query failed: %s", query);
LogError("Query error: %s", error); LogError("Query error: %s", error);
@ -203,28 +204,28 @@ FetchGroups(Handle:db)
} }
/* Now start fetching groups */ /* Now start fetching groups */
decl String:flags[32]; char flags[32];
decl String:name[128]; char name[128];
new immunity; int immunity;
while (SQL_FetchRow(hQuery)) while (rs.FetchRow())
{ {
SQL_FetchString(hQuery, 0, flags, sizeof(flags)); rs.FetchString(0, flags, sizeof(flags));
SQL_FetchString(hQuery, 1, name, sizeof(name)); rs.FetchString(1, name, sizeof(name));
immunity = SQL_FetchInt(hQuery, 2); immunity = rs.FetchInt(2);
#if defined _DEBUG #if defined _DEBUG
PrintToServer("Adding group (%d, %s, %s)", immunity, flags, name); PrintToServer("Adding group (%d, %s, %s)", immunity, flags, name);
#endif #endif
/* Find or create the group */ /* Find or create the group */
new GroupId:gid; GroupId gid;
if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID) if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID)
{ {
gid = CreateAdmGroup(name); gid = CreateAdmGroup(name);
} }
/* Add flags from the database to the group */ /* Add flags from the database to the group */
new num_flag_chars = strlen(flags); int num_flag_chars = strlen(flags);
for (new i=0; i<num_flag_chars; i++) for (new i=0; i<num_flag_chars; i++)
{ {
decl AdminFlag:flag; decl AdminFlag:flag;
@ -239,7 +240,7 @@ FetchGroups(Handle:db)
SetAdmGroupImmunityLevel(gid, immunity); SetAdmGroupImmunityLevel(gid, immunity);
} }
delete hQuery; delete rs;
/** /**
* Get immunity in a big lump. This is a nasty query but it gets the job done. * Get immunity in a big lump. This is a nasty query but it gets the job done.
@ -249,23 +250,23 @@ FetchGroups(Handle:db)
len += Format(query[len], sizeof(query)-len, " LEFT JOIN sm_groups g1 ON g1.id = gi.group_id "); len += Format(query[len], sizeof(query)-len, " LEFT JOIN sm_groups g1 ON g1.id = gi.group_id ");
len += Format(query[len], sizeof(query)-len, " LEFT JOIN sm_groups g2 ON g2.id = gi.other_id"); len += Format(query[len], sizeof(query)-len, " LEFT JOIN sm_groups g2 ON g2.id = gi.other_id");
if ((hQuery = SQL_Query(db, query)) == null) if ((rs = SQL_Query(db, query)) == null)
{ {
decl String:error[255]; char error[255];
SQL_GetError(db, error, sizeof(error)); SQL_GetError(db, error, sizeof(error));
LogError("FetchGroups() query failed: %s", query); LogError("FetchGroups() query failed: %s", query);
LogError("Query error: %s", error); LogError("Query error: %s", error);
return; return;
} }
while (SQL_FetchRow(hQuery)) while (rs.FetchRow())
{ {
decl String:group1[80]; char group1[80];
decl String:group2[80]; char group2[80];
new GroupId:gid1, GroupId:gid2; GroupId gid1, gid2;
SQL_FetchString(hQuery, 0, group1, sizeof(group1)); rs.FetchString(0, group1, sizeof(group1));
SQL_FetchString(hQuery, 1, group2, sizeof(group2)); rs.FetchString(1, group2, sizeof(group2));
if (((gid1 = FindAdmGroup(group1)) == INVALID_GROUP_ID) if (((gid1 = FindAdmGroup(group1)) == INVALID_GROUP_ID)
|| (gid2 = FindAdmGroup(group2)) == INVALID_GROUP_ID) || (gid2 = FindAdmGroup(group2)) == INVALID_GROUP_ID)
@ -279,85 +280,85 @@ FetchGroups(Handle:db)
#endif #endif
} }
delete hQuery; delete rs;
/** /**
* Fetch overrides in a lump query. * Fetch overrides in a lump query.
*/ */
Format(query, sizeof(query), "SELECT g.name, go.type, go.name, go.access FROM sm_group_overrides go LEFT JOIN sm_groups g ON go.group_id = g.id"); Format(query, sizeof(query), "SELECT g.name, go.type, go.name, go.access FROM sm_group_overrides go LEFT JOIN sm_groups g ON go.group_id = g.id");
if ((hQuery = SQL_Query(db, query)) == null) if ((rs = SQL_Query(db, query)) == null)
{ {
decl String:error[255]; char error[255];
SQL_GetError(db, error, sizeof(error)); SQL_GetError(db, error, sizeof(error));
LogError("FetchGroups() query failed: %s", query); LogError("FetchGroups() query failed: %s", query);
LogError("Query error: %s", error); LogError("Query error: %s", error);
return; return;
} }
decl String:type[16]; char type[16];
decl String:cmd[64]; char cmd[64];
decl String:access[16]; char access[16];
while (SQL_FetchRow(hQuery)) while (rs.FetchRow())
{ {
SQL_FetchString(hQuery, 0, name, sizeof(name)); rs.FetchString(0, name, sizeof(name));
SQL_FetchString(hQuery, 1, type, sizeof(type)); rs.FetchString(1, type, sizeof(type));
SQL_FetchString(hQuery, 2, cmd, sizeof(cmd)); rs.FetchString(2, cmd, sizeof(cmd));
SQL_FetchString(hQuery, 3, access, sizeof(access)); rs.FetchString(3, access, sizeof(access));
new GroupId:gid; GroupId gid;
if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID) if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID)
{ {
continue; continue;
} }
new OverrideType:o_type = Override_Command; OverrideType o_type = Override_Command;
if (StrEqual(type, "group")) if (StrEqual(type, "group"))
{ {
o_type = Override_CommandGroup; o_type = Override_CommandGroup;
} }
new OverrideRule:o_rule = Command_Deny; OverrideRule o_rule = Command_Deny;
if (StrEqual(access, "allow")) if (StrEqual(access, "allow"))
{ {
o_rule = Command_Allow; o_rule = Command_Allow;
} }
#if defined _DEBUG #if defined _DEBUG
PrintToServer("AddAdmGroupCmdOverride(%d, %s, %d, %d)", gid, cmd, o_type, o_rule); PrintToServer("AddAdmGroupCmdOverride(%d, %s, %d, %d)", gid, cmd, o_type, o_rule);
#endif #endif
AddAdmGroupCmdOverride(gid, cmd, o_type, o_rule); AddAdmGroupCmdOverride(gid, cmd, o_type, o_rule);
} }
delete hQuery; delete rs;
} }
FetchOverrides(Handle:db) FetchOverrides(Database db)
{ {
decl String:query[255]; char query[255];
new Handle:hQuery; DBResultSet rs;
Format(query, sizeof(query), "SELECT type, name, flags FROM sm_overrides"); Format(query, sizeof(query), "SELECT type, name, flags FROM sm_overrides");
if ((hQuery = SQL_Query(db, query)) == null) if ((rs = SQL_Query(db, query)) == null)
{ {
decl String:error[255]; char error[255];
SQL_GetError(db, error, sizeof(error)); SQL_GetError(db, error, sizeof(error));
LogError("FetchOverrides() query failed: %s", query); LogError("FetchOverrides() query failed: %s", query);
LogError("Query error: %s", error); LogError("Query error: %s", error);
return; return;
} }
decl String:type[64]; char type[64];
decl String:name[64]; char name[64];
decl String:flags[32]; char flags[32];
new flag_bits; int flag_bits;
while (SQL_FetchRow(hQuery)) while (rs.FetchRow())
{ {
SQL_FetchString(hQuery, 0, type, sizeof(type)); rs.FetchString(0, type, sizeof(type));
SQL_FetchString(hQuery, 1, name, sizeof(name)); rs.FetchString(1, name, sizeof(name));
SQL_FetchString(hQuery, 2, flags, sizeof(flags)); rs.FetchString(2, flags, sizeof(flags));
#if defined _DEBUG #if defined _DEBUG
PrintToServer("Adding override (%s, %s, %s)", type, name, flags); PrintToServer("Adding override (%s, %s, %s)", type, name, flags);
@ -372,6 +373,6 @@ FetchOverrides(Handle:db)
} }
} }
delete hQuery; delete rs;
} }

View File

@ -67,7 +67,7 @@ public Plugin:myinfo =
* powers. * powers.
*/ */
new Handle:hDatabase = null; /** Database connection */ Database hDatabase = null; /** Database connection */
new g_sequence = 0; /** Global unique sequence number */ new g_sequence = 0; /** Global unique sequence number */
new ConnectLock = 0; /** Connect sequence number */ new ConnectLock = 0; /** Connect sequence number */
new RebuildCachePart[3] = {0}; /** Cache part sequence numbers */ new RebuildCachePart[3] = {0}; /** Cache part sequence numbers */
@ -81,11 +81,7 @@ public OnMapEnd()
/** /**
* Clean up on map end just so we can start a fresh connection when we need it later. * Clean up on map end just so we can start a fresh connection when we need it later.
*/ */
if (hDatabase != null) delete hDatabase;
{
delete hDatabase;
hDatabase = null;
}
} }
public bool:OnClientConnect(client, String:rejectmsg[], maxlen) public bool:OnClientConnect(client, String:rejectmsg[], maxlen)
@ -101,10 +97,10 @@ public OnClientDisconnect(client)
PlayerAuth[client] = false; PlayerAuth[client] = false;
} }
public OnDatabaseConnect(Handle:owner, Handle:hndl, const String:error[], any:data) public void OnDatabaseConnect(Database db, const char[] error, any data)
{ {
#if defined _DEBUG #if defined _DEBUG
PrintToServer("OnDatabaseConnect(%x,%x,%d) ConnectLock=%d", owner, hndl, data, ConnectLock); PrintToServer("OnDatabaseConnect(%x, %d) ConnectLock=%d", db, data, ConnectLock);
#endif #endif
/** /**
@ -112,15 +108,12 @@ public OnDatabaseConnect(Handle:owner, Handle:hndl, const String:error[], any:da
*/ */
if (data != ConnectLock || hDatabase != null) if (data != ConnectLock || hDatabase != null)
{ {
if (hndl != null) delete db;
{
delete hndl;
}
return; return;
} }
ConnectLock = 0; ConnectLock = 0;
hDatabase = hndl; hDatabase = db;
/** /**
* See if the connection is valid. If not, don't un-mark the caches * See if the connection is valid. If not, don't un-mark the caches
@ -155,20 +148,20 @@ RequestDatabaseConnection()
ConnectLock = ++g_sequence; ConnectLock = ++g_sequence;
if (SQL_CheckConfig("admins")) if (SQL_CheckConfig("admins"))
{ {
SQL_TConnect(OnDatabaseConnect, "admins", ConnectLock); Database.Connect(OnDatabaseConnect, "admins", ConnectLock);
} else { } else {
SQL_TConnect(OnDatabaseConnect, "default", ConnectLock); Database.Connect(OnDatabaseConnect, "default", ConnectLock);
} }
} }
public OnRebuildAdminCache(AdminCachePart:part) public OnRebuildAdminCache(AdminCachePart part)
{ {
/** /**
* Mark this part of the cache as being rebuilt. This is used by the * Mark this part of the cache as being rebuilt. This is used by the
* callback system to determine whether the results should still be * callback system to determine whether the results should still be
* used. * used.
*/ */
new sequence = ++g_sequence; int sequence = ++g_sequence;
RebuildCachePart[_:part] = sequence; RebuildCachePart[_:part] = sequence;
/** /**
@ -196,7 +189,7 @@ public OnRebuildAdminCache(AdminCachePart:part)
} }
} }
public Action:OnClientPreAdminCheck(client) public Action OnClientPreAdminCheck(client)
{ {
PlayerAuth[client] = true; PlayerAuth[client] = true;
@ -235,13 +228,13 @@ public Action:OnClientPreAdminCheck(client)
return Plugin_Handled; return Plugin_Handled;
} }
public OnReceiveUserGroups(Handle:owner, Handle:hndl, const String:error[], any:data) public void OnReceiveUserGroups(Database db, DBResultSet rs, const char[] error, any data)
{ {
new Handle:pk = Handle:data; DataPack pk = view_as<DataPack>(data);
ResetPack(pk); pk.Reset();
new client = ReadPackCell(pk); int client = pk.ReadCell();
new sequence = ReadPackCell(pk); int sequence = pk.ReadCell();
/** /**
* Make sure it's the same client. * Make sure it's the same client.
@ -252,7 +245,7 @@ public OnReceiveUserGroups(Handle:owner, Handle:hndl, const String:error[], any:
return; return;
} }
new AdminId:adm = AdminId:ReadPackCell(pk); AdminId adm = view_as<AdminId>(pk.ReadCell());
/** /**
* Someone could have sneakily changed the admin id while we waited. * Someone could have sneakily changed the admin id while we waited.
@ -267,10 +260,10 @@ public OnReceiveUserGroups(Handle:owner, Handle:hndl, const String:error[], any:
/** /**
* See if we got results. * See if we got results.
*/ */
if (hndl == null) if (rs == null)
{ {
decl String:query[255]; char query[255];
ReadPackString(pk, query, sizeof(query)); pk.ReadString(query, sizeof(query));
LogError("SQL error receiving user: %s", error); LogError("SQL error receiving user: %s", error);
LogError("Query dump: %s", query); LogError("Query dump: %s", query);
NotifyPostAdminCheck(client); NotifyPostAdminCheck(client);
@ -278,12 +271,12 @@ public OnReceiveUserGroups(Handle:owner, Handle:hndl, const String:error[], any:
return; return;
} }
decl String:name[80]; char name[80];
new GroupId:gid; GroupId gid;
while (SQL_FetchRow(hndl)) while (rs.FetchRow())
{ {
SQL_FetchString(hndl, 0, name, sizeof(name)); rs.FetchString(0, name, sizeof(name));
if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID) if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID)
{ {
@ -304,17 +297,17 @@ public OnReceiveUserGroups(Handle:owner, Handle:hndl, const String:error[], any:
delete pk; delete pk;
} }
public OnReceiveUser(Handle:owner, Handle:hndl, const String:error[], any:data) public void OnReceiveUser(Database db, DBResultSet rs, const char[] error, any data)
{ {
new Handle:pk = Handle:data; DataPack pk = view_as<DataPack>(data);
ResetPack(pk); pk.Reset();
new client = ReadPackCell(pk); int client = pk.ReadCell();
/** /**
* Check if this is the latest result request. * Check if this is the latest result request.
*/ */
new sequence = ReadPackCell(pk); int sequence = pk.ReadCell();
if (PlayerSeq[client] != sequence) if (PlayerSeq[client] != sequence)
{ {
/* Discard everything, since we're out of sequence. */ /* Discard everything, since we're out of sequence. */
@ -325,10 +318,10 @@ public OnReceiveUser(Handle:owner, Handle:hndl, const String:error[], any:data)
/** /**
* If we need to use the results, make sure they succeeded. * If we need to use the results, make sure they succeeded.
*/ */
if (hndl == null) if (rs == null)
{ {
decl String:query[255]; char query[255];
ReadPackString(pk, query, sizeof(query)); pk.ReadString(query, sizeof(query));
LogError("SQL error receiving user: %s", error); LogError("SQL error receiving user: %s", error);
LogError("Query dump: %s", query); LogError("Query dump: %s", query);
RunAdminCacheChecks(client); RunAdminCacheChecks(client);
@ -337,7 +330,7 @@ public OnReceiveUser(Handle:owner, Handle:hndl, const String:error[], any:data)
return; return;
} }
new num_accounts = SQL_GetRowCount(hndl); int num_accounts = rs.RowCount;
if (num_accounts == 0) if (num_accounts == 0)
{ {
RunAdminCacheChecks(client); RunAdminCacheChecks(client);
@ -346,29 +339,29 @@ public OnReceiveUser(Handle:owner, Handle:hndl, const String:error[], any:data)
return; return;
} }
decl String:authtype[16]; char authtype[16];
decl String:identity[80]; char identity[80];
decl String:password[80]; char password[80];
decl String:flags[32]; char flags[32];
decl String:name[80]; char name[80];
new AdminId:adm, id; int immunity, id;
new immunity; AdminId adm;
/** /**
* Cache user info -- [0] = db id, [1] = cache id, [2] = groups * Cache user info -- [0] = db id, [1] = cache id, [2] = groups
*/ */
decl user_lookup[num_accounts][3]; char[][] user_lookup = new char[num_accounts][3];
new total_users = 0; int total_users = 0;
while (SQL_FetchRow(hndl)) while (rs.FetchRow())
{ {
id = SQL_FetchInt(hndl, 0); id = rs.FetchInt(0);
SQL_FetchString(hndl, 1, authtype, sizeof(authtype)); rs.FetchString(1, authtype, sizeof(authtype));
SQL_FetchString(hndl, 2, identity, sizeof(identity)); rs.FetchString(2, identity, sizeof(identity));
SQL_FetchString(hndl, 3, password, sizeof(password)); rs.FetchString(3, password, sizeof(password));
SQL_FetchString(hndl, 4, flags, sizeof(flags)); rs.FetchString(4, flags, sizeof(flags));
SQL_FetchString(hndl, 5, name, sizeof(name)); rs.FetchString(5, name, sizeof(name));
immunity = SQL_FetchInt(hndl, 7); immunity = rs.FetchInt(7);
/* For dynamic admins we clear anything already in the cache. */ /* For dynamic admins we clear anything already in the cache. */
if ((adm = FindAdminByIdentity(authtype, identity)) != INVALID_ADMIN_ID) if ((adm = FindAdminByIdentity(authtype, identity)) != INVALID_ADMIN_ID)
@ -385,7 +378,7 @@ public OnReceiveUser(Handle:owner, Handle:hndl, const String:error[], any:data)
user_lookup[total_users][0] = id; user_lookup[total_users][0] = id;
user_lookup[total_users][1] = _:adm; user_lookup[total_users][1] = _:adm;
user_lookup[total_users][2] = SQL_FetchInt(hndl, 6); user_lookup[total_users][2] = rs.FetchInt(6);
total_users++; total_users++;
#if defined _DEBUG #if defined _DEBUG
@ -401,8 +394,8 @@ public OnReceiveUser(Handle:owner, Handle:hndl, const String:error[], any:data)
SetAdminImmunityLevel(adm, immunity); SetAdminImmunityLevel(adm, immunity);
/* Apply each flag */ /* Apply each flag */
new len = strlen(flags); int len = strlen(flags);
new AdminFlag:flag; AdminFlag flag;
for (new i=0; i<len; i++) for (new i=0; i<len; i++)
{ {
if (!FindFlagByChar(flags[i], flag)) if (!FindFlagByChar(flags[i], flag))
@ -416,7 +409,7 @@ public OnReceiveUser(Handle:owner, Handle:hndl, const String:error[], any:data)
/** /**
* Try binding the user. * Try binding the user.
*/ */
new group_count = 0; int group_count = 0;
RunAdminCacheChecks(client); RunAdminCacheChecks(client);
adm = GetUserAdmin(client); adm = GetUserAdmin(client);
id = 0; id = 0;
@ -450,25 +443,25 @@ public OnReceiveUser(Handle:owner, Handle:hndl, const String:error[], any:data)
/** /**
* The user has groups -- we need to fetch them! * The user has groups -- we need to fetch them!
*/ */
decl String:query[255]; char query[255];
Format(query, sizeof(query), "SELECT g.name FROM sm_admins_groups ag JOIN sm_groups g ON ag.group_id = g.id WHERE ag.admin_id = %d", id); Format(query, sizeof(query), "SELECT g.name FROM sm_admins_groups ag JOIN sm_groups g ON ag.group_id = g.id WHERE ag.admin_id = %d", id);
ResetPack(pk); pk.Reset();
WritePackCell(pk, client); pk.WriteCell(client);
WritePackCell(pk, sequence); pk.WriteCell(sequence);
WritePackCell(pk, _:adm); pk.WriteCell(_:adm);
WritePackString(pk, query); pk.WriteString(query);
SQL_TQuery(owner, OnReceiveUserGroups, query, pk, DBPrio_High); db.Query(OnReceiveUserGroups, query, pk, DBPrio_High);
} }
FetchUser(Handle:db, client) FetchUser(Database db, client)
{ {
decl String:name[65]; char name[65];
decl String:safe_name[140]; char safe_name[140];
decl String:steamid[32]; char steamid[32];
decl String:steamidalt[32]; char steamidalt[32];
decl String:ipaddr[24]; char ipaddr[24];
/** /**
* Get authentication information from the client. * Get authentication information from the client.
@ -485,12 +478,12 @@ FetchUser(Handle:db, client)
} }
} }
SQL_EscapeString(db, name, safe_name, sizeof(safe_name)); db.Escape(name, safe_name, sizeof(safe_name));
/** /**
* Construct the query using the information the user gave us. * Construct the query using the information the user gave us.
*/ */
decl String:query[512]; char query[512];
new len = 0; new len = 0;
len += Format(query[len], sizeof(query)-len, "SELECT a.id, a.authtype, a.identity, a.password, a.flags, a.name, COUNT(ag.group_id), immunity"); len += Format(query[len], sizeof(query)-len, "SELECT a.id, a.authtype, a.identity, a.password, a.flags, a.name, COUNT(ag.group_id), immunity");
@ -511,22 +504,21 @@ FetchUser(Handle:db, client)
*/ */
PlayerSeq[client] = ++g_sequence; PlayerSeq[client] = ++g_sequence;
new Handle:pk; DataPack pk = new DataPack();
pk = CreateDataPack(); pk.WriteCell(client);
WritePackCell(pk, client); pk.WriteCell(PlayerSeq[client]);
WritePackCell(pk, PlayerSeq[client]); pk.WriteString(query);
WritePackString(pk, query);
#if defined _DEBUG #if defined _DEBUG
PrintToServer("Sending user query: %s", query); PrintToServer("Sending user query: %s", query);
#endif #endif
SQL_TQuery(db, OnReceiveUser, query, pk, DBPrio_High); db.Query(OnReceiveUser, query, pk, DBPrio_High);
} }
FetchUsersWeCan(Handle:db) FetchUsersWeCan(Database db)
{ {
for (new i=1; i<=MaxClients; i++) for (int i=1; i<=MaxClients; i++)
{ {
if (PlayerAuth[i] && GetUserAdmin(i) == INVALID_ADMIN_ID) if (PlayerAuth[i] && GetUserAdmin(i) == INVALID_ADMIN_ID)
{ {
@ -541,15 +533,15 @@ FetchUsersWeCan(Handle:db)
} }
public OnReceiveGroupImmunity(Handle:owner, Handle:hndl, const String:error[], any:data) public void OnReceiveGroupImmunity(Database db, DBResultSet rs, const char[] error, any data)
{ {
new Handle:pk = Handle:data; DataPack pk = view_as<DataPack>(data);
ResetPack(pk); pk.Reset();
/** /**
* Check if this is the latest result request. * Check if this is the latest result request.
*/ */
new sequence = ReadPackCell(pk); int sequence = pk.ReadCell();
if (RebuildCachePart[_:AdminCache_Groups] != sequence) if (RebuildCachePart[_:AdminCache_Groups] != sequence)
{ {
/* Discard everything, since we're out of sequence. */ /* Discard everything, since we're out of sequence. */
@ -560,10 +552,10 @@ public OnReceiveGroupImmunity(Handle:owner, Handle:hndl, const String:error[], a
/** /**
* If we need to use the results, make sure they succeeded. * If we need to use the results, make sure they succeeded.
*/ */
if (hndl == null) if (rs == null)
{ {
decl String:query[255]; char query[255];
ReadPackString(pk, query, sizeof(query)); pk.ReadString(query, sizeof(query));
LogError("SQL error receiving group immunity: %s", error); LogError("SQL error receiving group immunity: %s", error);
LogError("Query dump: %s", query); LogError("Query dump: %s", query);
delete pk; delete pk;
@ -573,14 +565,14 @@ public OnReceiveGroupImmunity(Handle:owner, Handle:hndl, const String:error[], a
/* We're done with the pack forever. */ /* We're done with the pack forever. */
delete pk; delete pk;
while (SQL_FetchRow(hndl)) while (rs.FetchRow())
{ {
decl String:group1[80]; char group1[80];
decl String:group2[80]; char group2[80];
new GroupId:gid1, GroupId:gid2; GroupId gid1, gid2;
SQL_FetchString(hndl, 0, group1, sizeof(group1)); rs.FetchString(0, group1, sizeof(group1));
SQL_FetchString(hndl, 1, group2, sizeof(group2)); rs.FetchString(1, group2, sizeof(group2));
if (((gid1 = FindAdmGroup(group1)) == INVALID_GROUP_ID) if (((gid1 = FindAdmGroup(group1)) == INVALID_GROUP_ID)
|| (gid2 = FindAdmGroup(group2)) == INVALID_GROUP_ID) || (gid2 = FindAdmGroup(group2)) == INVALID_GROUP_ID)
@ -598,15 +590,15 @@ public OnReceiveGroupImmunity(Handle:owner, Handle:hndl, const String:error[], a
RebuildCachePart[_:AdminCache_Groups] = 0; RebuildCachePart[_:AdminCache_Groups] = 0;
} }
public OnReceiveGroupOverrides(Handle:owner, Handle:hndl, const String:error[], any:data) public OnReceiveGroupOverrides(Database db, DBResultSet rs, const char[] error, any data)
{ {
new Handle:pk = Handle:data; DataPack pk = view_as<DataPack>(data);
ResetPack(pk); pk.Reset();
/** /**
* Check if this is the latest result request. * Check if this is the latest result request.
*/ */
new sequence = ReadPackCell(pk); int sequence = pk.ReadCell();
if (RebuildCachePart[_:AdminCache_Groups] != sequence) if (RebuildCachePart[_:AdminCache_Groups] != sequence)
{ {
/* Discard everything, since we're out of sequence. */ /* Discard everything, since we're out of sequence. */
@ -617,10 +609,10 @@ public OnReceiveGroupOverrides(Handle:owner, Handle:hndl, const String:error[],
/** /**
* If we need to use the results, make sure they succeeded. * If we need to use the results, make sure they succeeded.
*/ */
if (hndl == null) if (rs == null)
{ {
decl String:query[255]; char query[255];
ReadPackString(pk, query, sizeof(query)); pk.ReadString(query, sizeof(query));
LogError("SQL error receiving group overrides: %s", error); LogError("SQL error receiving group overrides: %s", error);
LogError("Query dump: %s", query); LogError("Query dump: %s", query);
delete pk; delete pk;
@ -630,17 +622,17 @@ public OnReceiveGroupOverrides(Handle:owner, Handle:hndl, const String:error[],
/** /**
* Fetch the overrides. * Fetch the overrides.
*/ */
decl String:name[80]; char name[80];
decl String:type[16]; char type[16];
decl String:command[64]; char command[64];
decl String:access[16]; char access[16];
new GroupId:gid; GroupId gid;
while (SQL_FetchRow(hndl)) while (rs.FetchRow())
{ {
SQL_FetchString(hndl, 0, name, sizeof(name)); rs.FetchString(0, name, sizeof(name));
SQL_FetchString(hndl, 1, type, sizeof(type)); rs.FetchString(1, type, sizeof(type));
SQL_FetchString(hndl, 2, command, sizeof(command)); rs.FetchString(2, command, sizeof(command));
SQL_FetchString(hndl, 3, access, sizeof(access)); rs.FetchString(3, access, sizeof(access));
/* Find the group. This is actually faster than doing the ID lookup. */ /* Find the group. This is actually faster than doing the ID lookup. */
if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID) if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID)
@ -649,13 +641,13 @@ public OnReceiveGroupOverrides(Handle:owner, Handle:hndl, const String:error[],
continue; continue;
} }
new OverrideType:o_type = Override_Command; OverrideType o_type = Override_Command;
if (StrEqual(type, "group")) if (StrEqual(type, "group"))
{ {
o_type = Override_CommandGroup; o_type = Override_CommandGroup;
} }
new OverrideRule:o_rule = Command_Deny; OverrideRule o_rule = Command_Deny;
if (StrEqual(access, "allow")) if (StrEqual(access, "allow"))
{ {
o_rule = Command_Allow; o_rule = Command_Allow;
@ -671,28 +663,28 @@ public OnReceiveGroupOverrides(Handle:owner, Handle:hndl, const String:error[],
/** /**
* It's time to get the group immunity list. * It's time to get the group immunity list.
*/ */
new len = 0; int len = 0;
decl String:query[256]; char query[256];
len += Format(query[len], sizeof(query)-len, "SELECT g1.name, g2.name FROM sm_group_immunity gi"); len += Format(query[len], sizeof(query)-len, "SELECT g1.name, g2.name FROM sm_group_immunity gi");
len += Format(query[len], sizeof(query)-len, " LEFT JOIN sm_groups g1 ON g1.id = gi.group_id "); len += Format(query[len], sizeof(query)-len, " LEFT JOIN sm_groups g1 ON g1.id = gi.group_id ");
len += Format(query[len], sizeof(query)-len, " LEFT JOIN sm_groups g2 ON g2.id = gi.other_id"); len += Format(query[len], sizeof(query)-len, " LEFT JOIN sm_groups g2 ON g2.id = gi.other_id");
ResetPack(pk); pk.Reset();
WritePackCell(pk, sequence); pk.WriteCell(sequence);
WritePackString(pk, query); pk.WriteString(query);
SQL_TQuery(owner, OnReceiveGroupImmunity, query, pk, DBPrio_High); db.Query(OnReceiveGroupImmunity, query, pk, DBPrio_High);
} }
public OnReceiveGroups(Handle:owner, Handle:hndl, const String:error[], any:data) public OnReceiveGroups(Database db, DBResultSet rs, const char[] error, any data)
{ {
new Handle:pk = Handle:data; DataPack pk = view_as<DataPack>(data);
ResetPack(pk); pk.Reset();
/** /**
* Check if this is the latest result request. * Check if this is the latest result request.
*/ */
new sequence = ReadPackCell(pk); int sequence = pk.ReadCell();
if (RebuildCachePart[_:AdminCache_Groups] != sequence) if (RebuildCachePart[_:AdminCache_Groups] != sequence)
{ {
/* Discard everything, since we're out of sequence. */ /* Discard everything, since we're out of sequence. */
@ -703,10 +695,10 @@ public OnReceiveGroups(Handle:owner, Handle:hndl, const String:error[], any:data
/** /**
* If we need to use the results, make sure they succeeded. * If we need to use the results, make sure they succeeded.
*/ */
if (hndl == null) if (rs == null)
{ {
decl String:query[255]; char query[255];
ReadPackString(pk, query, sizeof(query)); pk.ReadString(query, sizeof(query));
LogError("SQL error receiving groups: %s", error); LogError("SQL error receiving groups: %s", error);
LogError("Query dump: %s", query); LogError("Query dump: %s", query);
delete pk; delete pk;
@ -716,29 +708,29 @@ public OnReceiveGroups(Handle:owner, Handle:hndl, const String:error[], any:data
/** /**
* Now start fetching groups. * Now start fetching groups.
*/ */
decl String:flags[32]; char flags[32];
decl String:name[128]; char name[128];
new immunity; int immunity;
while (SQL_FetchRow(hndl)) while (rs.FetchRow())
{ {
SQL_FetchString(hndl, 0, flags, sizeof(flags)); rs.FetchString(0, flags, sizeof(flags));
SQL_FetchString(hndl, 1, name, sizeof(name)); rs.FetchString(1, name, sizeof(name));
immunity = SQL_FetchInt(hndl, 2); immunity = rs.FetchInt(2);
#if defined _DEBUG #if defined _DEBUG
PrintToServer("Adding group (%d, %s, %s)", immunity, flags, name); PrintToServer("Adding group (%d, %s, %s)", immunity, flags, name);
#endif #endif
/* Find or create the group */ /* Find or create the group */
new GroupId:gid; GroupId gid;
if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID) if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID)
{ {
gid = CreateAdmGroup(name); gid = CreateAdmGroup(name);
} }
/* Add flags from the database to the group */ /* Add flags from the database to the group */
new num_flag_chars = strlen(flags); int num_flag_chars = strlen(flags);
for (new i=0; i<num_flag_chars; i++) for (int i=0; i<num_flag_chars; i++)
{ {
decl AdminFlag:flag; decl AdminFlag:flag;
if (!FindFlagByChar(flags[i], flag)) if (!FindFlagByChar(flags[i], flag))
@ -754,41 +746,40 @@ public OnReceiveGroups(Handle:owner, Handle:hndl, const String:error[], any:data
/** /**
* It's time to get the group override list. * It's time to get the group override list.
*/ */
decl String:query[255]; char query[255];
Format(query, Format(query,
sizeof(query), sizeof(query),
"SELECT g.name, og.type, og.name, og.access FROM sm_group_overrides og JOIN sm_groups g ON og.group_id = g.id ORDER BY g.id DESC"); "SELECT g.name, og.type, og.name, og.access FROM sm_group_overrides og JOIN sm_groups g ON og.group_id = g.id ORDER BY g.id DESC");
ResetPack(pk); pk.Reset();
WritePackCell(pk, sequence); pk.WriteCell(sequence);
WritePackString(pk, query); pk.WriteString(query);
SQL_TQuery(owner, OnReceiveGroupOverrides, query, pk, DBPrio_High); db.Query(OnReceiveGroupOverrides, query, pk, DBPrio_High);
} }
FetchGroups(Handle:db, sequence) void FetchGroups(Database db, sequence)
{ {
decl String:query[255]; char query[255];
new Handle:pk;
Format(query, sizeof(query), "SELECT flags, name, immunity_level FROM sm_groups"); Format(query, sizeof(query), "SELECT flags, name, immunity_level FROM sm_groups");
pk = CreateDataPack(); DataPack pk = new DataPack();
WritePackCell(pk, sequence); pk.WriteCell(sequence);
WritePackString(pk, query); pk.WriteString(query);
SQL_TQuery(db, OnReceiveGroups, query, pk, DBPrio_High); db.Query(OnReceiveGroups, query, pk, DBPrio_High);
} }
public OnReceiveOverrides(Handle:owner, Handle:hndl, const String:error[], any:data) public void OnReceiveOverrides(Database db, DBResultSet rs, const char[] error, any data)
{ {
new Handle:pk = Handle:data; DataPack pk = view_as<DataPack>(data);
ResetPack(pk); pk.Reset();
/** /**
* Check if this is the latest result request. * Check if this is the latest result request.
*/ */
new sequence = ReadPackCell(pk); int sequence = pk.ReadCell();
if (RebuildCachePart[_:AdminCache_Overrides] != sequence) if (RebuildCachePart[_:AdminCache_Overrides] != sequence)
{ {
/* Discard everything, since we're out of sequence. */ /* Discard everything, since we're out of sequence. */
@ -799,10 +790,10 @@ public OnReceiveOverrides(Handle:owner, Handle:hndl, const String:error[], any:d
/** /**
* If we need to use the results, make sure they succeeded. * If we need to use the results, make sure they succeeded.
*/ */
if (hndl == null) if (rs == null)
{ {
decl String:query[255]; char query[255];
ReadPackString(pk, query, sizeof(query)); pk.ReadString(query, sizeof(query));
LogError("SQL error receiving overrides: %s", error); LogError("SQL error receiving overrides: %s", error);
LogError("Query dump: %s", query); LogError("Query dump: %s", query);
delete pk; delete pk;
@ -814,15 +805,15 @@ public OnReceiveOverrides(Handle:owner, Handle:hndl, const String:error[], any:d
*/ */
delete pk; delete pk;
decl String:type[64]; char type[64];
decl String:name[64]; char name[64];
decl String:flags[32]; char flags[32];
new flag_bits; int flag_bits;
while (SQL_FetchRow(hndl)) while (rs.FetchRow())
{ {
SQL_FetchString(hndl, 0, type, sizeof(type)); rs.FetchString(0, type, sizeof(type));
SQL_FetchString(hndl, 1, name, sizeof(name)); rs.FetchString(1, name, sizeof(name));
SQL_FetchString(hndl, 2, flags, sizeof(flags)); rs.FetchString(2, flags, sizeof(flags));
#if defined _DEBUG #if defined _DEBUG
PrintToServer("Adding override (%s, %s, %s)", type, name, flags); PrintToServer("Adding override (%s, %s, %s)", type, name, flags);
@ -841,17 +832,16 @@ public OnReceiveOverrides(Handle:owner, Handle:hndl, const String:error[], any:d
RebuildCachePart[_:AdminCache_Overrides] = 0; RebuildCachePart[_:AdminCache_Overrides] = 0;
} }
FetchOverrides(Handle:db, sequence) void FetchOverrides(Database db, sequence)
{ {
decl String:query[255]; char query[255];
new Handle:pk;
Format(query, sizeof(query), "SELECT type, name, flags FROM sm_overrides"); Format(query, sizeof(query), "SELECT type, name, flags FROM sm_overrides");
pk = CreateDataPack(); DataPack pk = new DataPack();
WritePackCell(pk, sequence); pk.WriteCell(sequence);
WritePackString(pk, query); pk.WriteString(query);
SQL_TQuery(db, OnReceiveOverrides, query, pk, DBPrio_High); db.Query(OnReceiveOverrides, query, pk, DBPrio_High);
} }

View File

@ -64,10 +64,10 @@ public OnPluginStart()
RegServerCmd("sm_update_adm_tables", Command_UpdateTables); RegServerCmd("sm_update_adm_tables", Command_UpdateTables);
} }
Handle:Connect() Database Connect()
{ {
decl String:error[255]; char error[255];
new Handle:db; Database db;
if (SQL_CheckConfig("admins")) if (SQL_CheckConfig("admins"))
{ {
@ -157,16 +157,16 @@ CreateSQLite(client, Handle:db)
public Action:Command_CreateTables(args) public Action:Command_CreateTables(args)
{ {
new client = 0; int client = 0;
new Handle:db = Connect(); Database db = Connect();
if (db == null) if (db == null)
{ {
ReplyToCommand(client, "[SM] %t", "Could not connect to database"); ReplyToCommand(client, "[SM] %t", "Could not connect to database");
return Plugin_Handled; return Plugin_Handled;
} }
new String:ident[16]; char ident[16];
SQL_ReadDriver(db, ident, sizeof(ident)); db.Driver.GetIdentifier(ident, sizeof(ident));
if (strcmp(ident, "mysql") == 0) if (strcmp(ident, "mysql") == 0)
{ {
@ -184,21 +184,21 @@ public Action:Command_CreateTables(args)
bool:GetUpdateVersion(client, Handle:db, versions[4]) bool:GetUpdateVersion(client, Handle:db, versions[4])
{ {
decl String:query[256]; char query[256];
new Handle:hQuery; DBResultSet rs;
Format(query, sizeof(query), "SELECT cfg_value FROM sm_config WHERE cfg_key = 'admin_version'"); Format(query, sizeof(query), "SELECT cfg_value FROM sm_config WHERE cfg_key = 'admin_version'");
if ((hQuery = SQL_Query(db, query)) == null) if ((rs = SQL_Query(db, query)) == null)
{ {
DoError(client, db, query, "Version lookup query failed"); DoError(client, db, query, "Version lookup query failed");
return false; return false;
} }
if (SQL_FetchRow(hQuery)) if (rs.FetchRow())
{ {
decl String:version_string[255]; char version_string[255];
SQL_FetchString(hQuery, 0, version_string, sizeof(version_string)); rs.FetchString(0, version_string, sizeof(version_string));
decl String:version_numbers[4][12]; char version_numbers[4][12];
if (ExplodeString(version_string, ".", version_numbers, 4, 12) == 4) if (ExplodeString(version_string, ".", version_numbers, 4, 12) == 4)
{ {
for (new i = 0; i < 4; i++) for (new i = 0; i < 4; i++)
@ -208,7 +208,7 @@ bool:GetUpdateVersion(client, Handle:db, versions[4])
} }
} }
delete hQuery; delete rs;
if (current_version[3] < versions[3]) if (current_version[3] < versions[3])
{ {
@ -226,21 +226,21 @@ bool:GetUpdateVersion(client, Handle:db, versions[4])
return true; return true;
} }
UpdateSQLite(client, Handle:db) UpdateSQLite(client, Database db)
{ {
decl String:query[512]; char query[512];
new Handle:hQuery; DBResultSet rs;
Format(query, sizeof(query), "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'sm_config'"); Format(query, sizeof(query), "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'sm_config'");
if ((hQuery = SQL_Query(db, query)) == null) if ((rs = SQL_Query(db, query)) == null)
{ {
DoError(client, db, query, "Table lookup query failed"); DoError(client, db, query, "Table lookup query failed");
return; return;
} }
new bool:found = SQL_FetchRow(hQuery); bool found = rs.FetchRow();
delete hQuery; delete rs;
new versions[4]; new versions[4];
if (found) if (found)
@ -256,7 +256,7 @@ UpdateSQLite(client, Handle:db)
*/ */
if (versions[3] < SCHEMA_UPGRADE_1) if (versions[3] < SCHEMA_UPGRADE_1)
{ {
new String:queries[8][] = char queries[8][] =
{ {
"ALTER TABLE sm_admins ADD immunity INTEGER DEFAULT 0 NOT NULL", "ALTER TABLE sm_admins ADD immunity INTEGER DEFAULT 0 NOT NULL",
"CREATE TABLE _sm_groups_temp (id INTEGER PRIMARY KEY AUTOINCREMENT, flags varchar(30) NOT NULL, name varchar(120) NOT NULL, immunity_level INTEGER DEFAULT 0 NOT NULL)", "CREATE TABLE _sm_groups_temp (id INTEGER PRIMARY KEY AUTOINCREMENT, flags varchar(30) NOT NULL, name varchar(120) NOT NULL, immunity_level INTEGER DEFAULT 0 NOT NULL)",
@ -292,29 +292,29 @@ UpdateSQLite(client, Handle:db)
ReplyToCommand(client, "[SM] Your tables are now up to date."); ReplyToCommand(client, "[SM] Your tables are now up to date.");
} }
UpdateMySQL(client, Handle:db) UpdateMySQL(client, Database db)
{ {
decl String:query[512]; char query[512];
new Handle:hQuery; DBResultSet rs;
Format(query, sizeof(query), "SHOW TABLES"); Format(query, sizeof(query), "SHOW TABLES");
if ((hQuery = SQL_Query(db, query)) == null) if ((rs = SQL_Query(db, query)) == null)
{ {
DoError(client, db, query, "Table lookup query failed"); DoError(client, db, query, "Table lookup query failed");
return; return;
} }
decl String:table[64]; char table[64];
new bool:found = false; bool found = false;
while (SQL_FetchRow(hQuery)) while (rs.FetchRow())
{ {
SQL_FetchString(hQuery, 0, table, sizeof(table)); rs.FetchString(0, table, sizeof(table));
if (strcmp(table, "sm_config") == 0) if (strcmp(table, "sm_config") == 0)
{ {
found = true; found = true;
} }
} }
delete hQuery; delete rs;
new versions[4]; new versions[4];
@ -328,7 +328,7 @@ UpdateMySQL(client, Handle:db)
*/ */
if (versions[3] < SCHEMA_UPGRADE_1) if (versions[3] < SCHEMA_UPGRADE_1)
{ {
new String:queries[6][] = char queries[6][] =
{ {
"CREATE TABLE IF NOT EXISTS sm_config (cfg_key varchar(32) NOT NULL, cfg_value varchar(255) NOT NULL, PRIMARY KEY (cfg_key))", "CREATE TABLE IF NOT EXISTS sm_config (cfg_key varchar(32) NOT NULL, cfg_value varchar(255) NOT NULL, PRIMARY KEY (cfg_key))",
"ALTER TABLE sm_admins ADD immunity INT UNSIGNED NOT NULL", "ALTER TABLE sm_admins ADD immunity INT UNSIGNED NOT NULL",
@ -346,7 +346,7 @@ UpdateMySQL(client, Handle:db)
} }
} }
decl String:upgr[48]; char upgr[48];
Format(upgr, sizeof(upgr), "1.0.0.%d", SCHEMA_UPGRADE_1); Format(upgr, sizeof(upgr), "1.0.0.%d", SCHEMA_UPGRADE_1);
Format(query, sizeof(query), "INSERT INTO sm_config (cfg_key, cfg_value) VALUES ('admin_version', '%s') ON DUPLICATE KEY UPDATE cfg_value = '%s'", upgr, upgr); Format(query, sizeof(query), "INSERT INTO sm_config (cfg_key, cfg_value) VALUES ('admin_version', '%s') ON DUPLICATE KEY UPDATE cfg_value = '%s'", upgr, upgr);
@ -364,15 +364,15 @@ UpdateMySQL(client, Handle:db)
public Action:Command_UpdateTables(args) public Action:Command_UpdateTables(args)
{ {
new client = 0; new client = 0;
new Handle:db = Connect(); Database db = Connect();
if (db == null) if (db == null)
{ {
ReplyToCommand(client, "[SM] %t", "Could not connect to database"); ReplyToCommand(client, "[SM] %t", "Could not connect to database");
return Plugin_Handled; return Plugin_Handled;
} }
new String:ident[16]; char ident[16];
SQL_ReadDriver(db, ident, sizeof(ident)); db.Driver.GetIdentifier(ident, sizeof(ident));
if (strcmp(ident, "mysql") == 0) if (strcmp(ident, "mysql") == 0)
{ {
@ -396,7 +396,7 @@ public Action:Command_SetAdminGroups(client, args)
return Plugin_Handled; return Plugin_Handled;
} }
decl String:authtype[16]; char authtype[16];
GetCmdArg(1, authtype, sizeof(authtype)); GetCmdArg(1, authtype, sizeof(authtype));
if (!StrEqual(authtype, "steam") if (!StrEqual(authtype, "steam")
@ -407,42 +407,42 @@ public Action:Command_SetAdminGroups(client, args)
return Plugin_Handled; return Plugin_Handled;
} }
new Handle:db = Connect(); Database db = Connect();
if (db == null) if (db == null)
{ {
ReplyToCommand(client, "[SM] %t", "Could not connect to database"); ReplyToCommand(client, "[SM] %t", "Could not connect to database");
return Plugin_Handled; return Plugin_Handled;
} }
decl String:identity[65]; char identity[65];
decl String:safe_identity[140]; char safe_identity[140];
GetCmdArg(2, identity, sizeof(identity)); GetCmdArg(2, identity, sizeof(identity));
SQL_EscapeString(db, identity, safe_identity, sizeof(safe_identity)); db.Escape(identity, safe_identity, sizeof(safe_identity));
decl String:query[255]; char query[255];
Format(query, Format(query,
sizeof(query), sizeof(query),
"SELECT id FROM sm_admins WHERE authtype = '%s' AND identity = '%s'", "SELECT id FROM sm_admins WHERE authtype = '%s' AND identity = '%s'",
authtype, authtype,
safe_identity); safe_identity);
new Handle:hQuery; DBResultSet rs;
if ((hQuery = SQL_Query(db, query)) == null) if ((rs = SQL_Query(db, query)) == null)
{ {
return DoError(client, db, query, "Admin lookup query failed"); return DoError(client, db, query, "Admin lookup query failed");
} }
if (!SQL_FetchRow(hQuery)) if (!rs.FetchRow())
{ {
ReplyToCommand(client, "[SM] %t", "SQL Admin not found"); ReplyToCommand(client, "[SM] %t", "SQL Admin not found");
delete hQuery; delete rs;
delete db; delete db;
return Plugin_Handled; return Plugin_Handled;
} }
new id = SQL_FetchInt(hQuery, 0); int id = rs.FetchInt(0);
delete hQuery; delete rs;
/** /**
* First delete all of the user's existing groups. * First delete all of the user's existing groups.
@ -455,13 +455,13 @@ public Action:Command_SetAdminGroups(client, args)
if (args < 3) if (args < 3)
{ {
ReplyToCommand(client, "[SM] %t", "SQL Admin groups reset");
delete db; delete db;
ReplyToCommand(client, "[SM] %t", "SQL Admin groups reset");
return Plugin_Handled; return Plugin_Handled;
} }
decl String:error[256]; char error[256];
new Handle:hAddQuery, Handle:hFindQuery; DBStatement hAddQuery, hFindQuery;
Format(query, sizeof(query), "SELECT id FROM sm_groups WHERE name = ?"); Format(query, sizeof(query), "SELECT id FROM sm_groups WHERE name = ?");
if ((hFindQuery = SQL_PrepareQuery(db, query, error, sizeof(error))) == null) if ((hFindQuery = SQL_PrepareQuery(db, query, error, sizeof(error))) == null)
@ -479,21 +479,21 @@ public Action:Command_SetAdminGroups(client, args)
return DoStmtError(client, db, query, error, "Add admin group prepare failed"); return DoStmtError(client, db, query, error, "Add admin group prepare failed");
} }
decl String:name[80]; char name[80];
new inherit_order = 0; int inherit_order = 0;
for (new i=3; i<=args; i++) for (new i=3; i<=args; i++)
{ {
GetCmdArg(i, name, sizeof(name)); GetCmdArg(i, name, sizeof(name));
SQL_BindParamString(hFindQuery, 0, name, false); hFindQuery.BindString(0, name, false);
if (!SQL_Execute(hFindQuery) || !SQL_FetchRow(hFindQuery)) if (!SQL_Execute(hFindQuery) || !SQL_FetchRow(hFindQuery))
{ {
ReplyToCommand(client, "[SM] %t", "SQL Group X not found", name); ReplyToCommand(client, "[SM] %t", "SQL Group X not found", name);
} else { } else {
new gid = SQL_FetchInt(hFindQuery, 0); new gid = SQL_FetchInt(hFindQuery, 0);
SQL_BindParamInt(hAddQuery, 0, gid); hAddQuery.BindInt(0, gid);
SQL_BindParamInt(hAddQuery, 1, ++inherit_order); hAddQuery.BindInt(1, ++inherit_order);
if (!SQL_Execute(hAddQuery)) if (!SQL_Execute(hAddQuery))
{ {
ReplyToCommand(client, "[SM] %t", "SQL Group X failed to bind", name); ReplyToCommand(client, "[SM] %t", "SQL Group X failed to bind", name);
@ -524,48 +524,47 @@ public Action:Command_DelGroup(client, args)
return Plugin_Handled; return Plugin_Handled;
} }
new Handle:db = Connect(); Database db = Connect();
if (db == null) if (db == null)
{ {
ReplyToCommand(client, "[SM] %t", "Could not connect to database"); ReplyToCommand(client, "[SM] %t", "Could not connect to database");
return Plugin_Handled; return Plugin_Handled;
} }
new len; char name[80];
decl String:name[80]; char safe_name[180];
decl String:safe_name[180];
GetCmdArgString(name, sizeof(name)); GetCmdArgString(name, sizeof(name));
/* Strip quotes in case the user tries to use them */ /* Strip quotes in case the user tries to use them */
len = strlen(name); int len = strlen(name);
if (len > 1 && (name[0] == '"' && name[len-1] == '"')) if (len > 1 && (name[0] == '"' && name[len-1] == '"'))
{ {
name[--len] = '\0'; name[--len] = '\0';
SQL_EscapeString(db, name[1], safe_name, sizeof(safe_name)); db.Escape(name[1], safe_name, sizeof(safe_name));
} else { } else {
SQL_EscapeString(db, name, safe_name, sizeof(safe_name)); db.Escape(name, safe_name, sizeof(safe_name));
} }
decl String:query[256]; char query[256];
new Handle:hQuery; DBResultSet rs;
Format(query, sizeof(query), "SELECT id FROM sm_groups WHERE name = '%s'", safe_name); Format(query, sizeof(query), "SELECT id FROM sm_groups WHERE name = '%s'", safe_name);
if ((hQuery = SQL_Query(db, query)) == null) if ((rs = SQL_Query(db, query)) == null)
{ {
return DoError(client, db, query, "Group retrieval query failed"); return DoError(client, db, query, "Group retrieval query failed");
} }
if (!SQL_FetchRow(hQuery)) if (!rs.FetchRow())
{ {
ReplyToCommand(client, "[SM] %t", "SQL Group not found"); ReplyToCommand(client, "[SM] %t", "SQL Group not found");
delete hQuery; delete rs;
delete db; delete db;
return Plugin_Handled; return Plugin_Handled;
} }
new id = SQL_FetchInt(hQuery, 0); int id = rs.FetchInt(0);
delete hQuery; delete rs;
/* Delete admin inheritance for this group */ /* Delete admin inheritance for this group */
Format(query, sizeof(query), "DELETE FROM sm_admins_groups WHERE group_id = %d", id); Format(query, sizeof(query), "DELETE FROM sm_admins_groups WHERE group_id = %d", id);
@ -598,7 +597,6 @@ public Action:Command_DelGroup(client, args)
ReplyToCommand(client, "[SM] %t", "SQL Group deleted"); ReplyToCommand(client, "[SM] %t", "SQL Group deleted");
delete db; delete db;
return Plugin_Handled; return Plugin_Handled;
} }
@ -622,40 +620,40 @@ public Action:Command_AddGroup(client, args)
} }
} }
new Handle:db = Connect(); Database db = Connect();
if (db == null) if (db == null)
{ {
ReplyToCommand(client, "[SM] %t", "Could not connect to database"); ReplyToCommand(client, "[SM] %t", "Could not connect to database");
return Plugin_Handled; return Plugin_Handled;
} }
decl String:name[64]; char name[64];
decl String:safe_name[64]; char safe_name[64];
GetCmdArg(1, name, sizeof(name)); GetCmdArg(1, name, sizeof(name));
SQL_EscapeString(db, name, safe_name, sizeof(safe_name)); db.Escape(name, safe_name, sizeof(safe_name));
new Handle:hQuery; DBResultSet rs;
decl String:query[256]; char query[256];
Format(query, sizeof(query), "SELECT id FROM sm_groups WHERE name = '%s'", safe_name); Format(query, sizeof(query), "SELECT id FROM sm_groups WHERE name = '%s'", safe_name);
if ((hQuery = SQL_Query(db, query)) == null) if ((rs = SQL_Query(db, query)) == null)
{ {
return DoError(client, db, query, "Group retrieval query failed"); return DoError(client, db, query, "Group retrieval query failed");
} }
if (SQL_GetRowCount(hQuery) > 0) if (rs.RowCount > 0)
{ {
ReplyToCommand(client, "[SM] %t", "SQL Group already exists"); ReplyToCommand(client, "[SM] %t", "SQL Group already exists");
delete hQuery; delete rs;
delete db; delete db;
return Plugin_Handled; return Plugin_Handled;
} }
delete hQuery; delete rs;
decl String:flags[30]; char flags[30];
decl String:safe_flags[64]; char safe_flags[64];
GetCmdArg(2, flags, sizeof(safe_flags)); GetCmdArg(2, flags, sizeof(safe_flags));
SQL_EscapeString(db, flags, safe_flags, sizeof(safe_flags)); db.Escape(flags, safe_flags, sizeof(safe_flags));
Format(query, Format(query,
sizeof(query), sizeof(query),
@ -672,7 +670,6 @@ public Action:Command_AddGroup(client, args)
ReplyToCommand(client, "[SM] %t", "SQL Group added"); ReplyToCommand(client, "[SM] %t", "SQL Group added");
delete db; delete db;
return Plugin_Handled; return Plugin_Handled;
} }
@ -685,7 +682,7 @@ public Action:Command_DelAdmin(client, args)
return Plugin_Handled; return Plugin_Handled;
} }
decl String:authtype[16]; char authtype[16];
GetCmdArg(1, authtype, sizeof(authtype)); GetCmdArg(1, authtype, sizeof(authtype));
if (!StrEqual(authtype, "steam") if (!StrEqual(authtype, "steam")
@ -696,42 +693,43 @@ public Action:Command_DelAdmin(client, args)
return Plugin_Handled; return Plugin_Handled;
} }
new Handle:db = Connect(); Database db = Connect();
if (db == null) if (db == null)
{ {
ReplyToCommand(client, "[SM] %t", "Could not connect to database"); ReplyToCommand(client, "[SM] %t", "Could not connect to database");
return Plugin_Handled; return Plugin_Handled;
} }
decl String:identity[65]; char identity[65];
decl String:safe_identity[140]; char safe_identity[140];
GetCmdArg(2, identity, sizeof(identity)); GetCmdArg(2, identity, sizeof(identity));
SQL_EscapeString(db, identity, safe_identity, sizeof(safe_identity)); db.Escape(identity, safe_identity, sizeof(safe_identity));
decl String:query[255]; char query[255];
Format(query, Format(query,
sizeof(query), sizeof(query),
"SELECT id FROM sm_admins WHERE authtype = '%s' AND identity = '%s'", "SELECT id FROM sm_admins WHERE authtype = '%s' AND identity = '%s'",
authtype, authtype,
safe_identity); safe_identity);
new Handle:hQuery; DBResultSet rs;
if ((hQuery = SQL_Query(db, query)) == null) if ((rs = SQL_Query(db, query)) == null)
{ {
delete db;
return DoError(client, db, query, "Admin lookup query failed"); return DoError(client, db, query, "Admin lookup query failed");
} }
if (!SQL_FetchRow(hQuery)) if (!rs.FetchRow())
{ {
ReplyToCommand(client, "[SM] %t", "SQL Admin not found"); ReplyToCommand(client, "[SM] %t", "SQL Admin not found");
delete hQuery; delete rs;
delete db; delete db;
return Plugin_Handled; return Plugin_Handled;
} }
new id = SQL_FetchInt(hQuery, 0); int id = rs.FetchInt(0);
delete hQuery; delete rs;
/* Delete group bindings */ /* Delete group bindings */
Format(query, sizeof(query), "DELETE FROM sm_admins_groups WHERE admin_id = %d", id); Format(query, sizeof(query), "DELETE FROM sm_admins_groups WHERE admin_id = %d", id);
@ -762,7 +760,7 @@ public Action:Command_AddAdmin(client, args)
return Plugin_Handled; return Plugin_Handled;
} }
decl String:authtype[16]; char authtype[16];
GetCmdArg(2, authtype, sizeof(authtype)); GetCmdArg(2, authtype, sizeof(authtype));
if (!StrEqual(authtype, "steam") if (!StrEqual(authtype, "steam")
@ -773,10 +771,10 @@ public Action:Command_AddAdmin(client, args)
return Plugin_Handled; return Plugin_Handled;
} }
new immunity; int immunity;
if (args >= 5) if (args >= 5)
{ {
new String:arg5[32]; char arg5[32];
GetCmdArg(5, arg5, sizeof(arg5)); GetCmdArg(5, arg5, sizeof(arg5));
if (!StringToIntEx(arg5, immunity)) if (!StringToIntEx(arg5, immunity))
{ {
@ -785,59 +783,59 @@ public Action:Command_AddAdmin(client, args)
} }
} }
decl String:identity[65]; char identity[65];
decl String:safe_identity[140]; char safe_identity[140];
GetCmdArg(3, identity, sizeof(identity)); GetCmdArg(3, identity, sizeof(identity));
decl String:query[256]; char query[256];
new Handle:hQuery; Database db = Connect();
new Handle:db = Connect();
if (db == null) if (db == null)
{ {
ReplyToCommand(client, "[SM] %t", "Could not connect to database"); ReplyToCommand(client, "[SM] %t", "Could not connect to database");
return Plugin_Handled; return Plugin_Handled;
} }
SQL_EscapeString(db, identity, safe_identity, sizeof(safe_identity)); db.Escape(identity, safe_identity, sizeof(safe_identity));
DBResultSet rs;
Format(query, sizeof(query), "SELECT id FROM sm_admins WHERE authtype = '%s' AND identity = '%s'", authtype, identity); Format(query, sizeof(query), "SELECT id FROM sm_admins WHERE authtype = '%s' AND identity = '%s'", authtype, identity);
if ((hQuery = SQL_Query(db, query)) == null) if ((rs = SQL_Query(db, query)) == null)
{ {
return DoError(client, db, query, "Admin retrieval query failed"); return DoError(client, db, query, "Admin retrieval query failed");
} }
if (SQL_GetRowCount(hQuery) > 0) if (rs.RowCount > 0)
{ {
ReplyToCommand(client, "[SM] %t", "SQL Admin already exists"); ReplyToCommand(client, "[SM] %t", "SQL Admin already exists");
delete hQuery; delete rs;
delete db; delete db;
return Plugin_Handled; return Plugin_Handled;
} }
delete hQuery; delete rs;
decl String:alias[64]; char alias[64];
decl String:safe_alias[140]; char safe_alias[140];
GetCmdArg(1, alias, sizeof(alias)); GetCmdArg(1, alias, sizeof(alias));
SQL_EscapeString(db, alias, safe_alias, sizeof(safe_alias)); db.Escape(alias, safe_alias, sizeof(safe_alias));
decl String:flags[30]; char flags[30];
decl String:safe_flags[64]; char safe_flags[64];
GetCmdArg(4, flags, sizeof(flags)); GetCmdArg(4, flags, sizeof(flags));
SQL_EscapeString(db, flags, safe_flags, sizeof(safe_flags)); db.Escape(flags, safe_flags, sizeof(safe_flags));
decl String:password[32]; char password[32];
decl String:safe_password[80]; char safe_password[80];
if (args >= 6) if (args >= 6)
{ {
GetCmdArg(6, password, sizeof(password)); GetCmdArg(6, password, sizeof(password));
SQL_EscapeString(db, password, safe_password, sizeof(safe_password)); db.Escape(password, safe_password, sizeof(safe_password));
} else { } else {
safe_password[0] = '\0'; safe_password[0] = '\0';
} }
new len = 0; int len = Format(query, sizeof(query), "INSERT INTO sm_admins (authtype, identity, password, flags, name, immunity) VALUES");
len += Format(query[len], sizeof(query)-len, "INSERT INTO sm_admins (authtype, identity, password, flags, name, immunity) VALUES");
if (safe_password[0] == '\0') if (safe_password[0] == '\0')
{ {
len += Format(query[len], sizeof(query)-len, " ('%s', '%s', NULL, '%s', '%s', %d)", authtype, safe_identity, safe_flags, safe_alias, immunity); len += Format(query[len], sizeof(query)-len, " ('%s', '%s', NULL, '%s', '%s', %d)", authtype, safe_identity, safe_flags, safe_alias, immunity);
@ -853,7 +851,6 @@ public Action:Command_AddAdmin(client, args)
ReplyToCommand(client, "[SM] %t", "SQL Admin added"); ReplyToCommand(client, "[SM] %t", "SQL Admin added");
delete db; delete db;
return Plugin_Handled; return Plugin_Handled;
} }