Merge pull request #221 from alliedmodders/tr-db

Transitional syntax support for DBI.
This commit is contained in:
David Anderson 2014-12-14 18:37:53 -08:00
commit b4199e5aa3
5 changed files with 867 additions and 538 deletions

View File

@ -289,14 +289,20 @@ private:
Handle_t m_MyHandle;
};
enum AsyncCallbackMode {
ACM_Old,
ACM_New
};
class TConnectOp : public IDBThreadOperation
{
public:
TConnectOp(IPluginFunction *func, IDBDriver *driver, const char *_dbname, cell_t data)
TConnectOp(IPluginFunction *func, IDBDriver *driver, const char *_dbname, AsyncCallbackMode acm, cell_t data)
{
m_pFunction = func;
m_pDriver = driver;
m_pDatabase = NULL;
m_ACM = acm;
m_Data = data;
error[0] = '\0';
strncopy(dbname, _dbname, sizeof(dbname));
@ -329,7 +335,8 @@ public:
m_pDatabase->Close();
}
m_pFunction->PushCell(BAD_HANDLE);
m_pFunction->PushCell(BAD_HANDLE);
if (m_ACM == ACM_Old)
m_pFunction->PushCell(BAD_HANDLE);
m_pFunction->PushString("Driver is unloading");
m_pFunction->PushCell(m_Data);
m_pFunction->Execute(NULL);
@ -349,7 +356,8 @@ public:
}
m_pFunction->PushCell(m_pDriver->GetHandle());
m_pFunction->PushCell(hndl);
if (m_ACM == ACM_Old)
m_pFunction->PushCell(hndl);
m_pFunction->PushString(hndl == BAD_HANDLE ? error : "");
m_pFunction->PushCell(m_Data);
m_pFunction->Execute(NULL);
@ -363,6 +371,7 @@ private:
IPluginFunction *m_pFunction;
IDBDriver *m_pDriver;
IDatabase *m_pDatabase;
AsyncCallbackMode m_ACM;
char dbname[64];
char error[255];
cell_t m_Data;
@ -401,7 +410,7 @@ static cell_t SQL_Connect(IPluginContext *pContext, const cell_t *params)
return hndl;
}
static cell_t SQL_TConnect(IPluginContext *pContext, const cell_t *params)
static cell_t ConnectToDbAsync(IPluginContext *pContext, const cell_t *params, AsyncCallbackMode acm)
{
IPluginFunction *pf = pContext->GetFunctionById(params[1]);
if (!pf)
@ -442,7 +451,8 @@ static cell_t SQL_TConnect(IPluginContext *pContext, const cell_t *params)
if (!pInfo || !driver)
{
pf->PushCell(BAD_HANDLE);
pf->PushCell(BAD_HANDLE);
if (acm == ACM_Old)
pf->PushCell(BAD_HANDLE);
pf->PushString(error);
pf->PushCell(0);
pf->Execute(NULL);
@ -457,7 +467,7 @@ static cell_t SQL_TConnect(IPluginContext *pContext, const cell_t *params)
}
/* Finally, add to the thread if we can */
TConnectOp *op = new TConnectOp(pf, driver, conf, params[3]);
TConnectOp *op = new TConnectOp(pf, driver, conf, acm, params[3]);
IPlugin *pPlugin = scripts->FindPluginByContext(pContext->GetContext());
if (pPlugin->GetProperty("DisallowDBThreads", NULL)
|| !g_DBMan.AddToThreadQueue(op, PrioQueue_High))
@ -471,6 +481,16 @@ static cell_t SQL_TConnect(IPluginContext *pContext, const cell_t *params)
return 1;
}
static cell_t SQL_TConnect(IPluginContext *pContext, const cell_t *params)
{
return ConnectToDbAsync(pContext, params, ACM_Old);
}
static cell_t Database_Connect(IPluginContext *pContext, const cell_t *params)
{
return ConnectToDbAsync(pContext, params, ACM_New);
}
static cell_t SQL_ConnectEx(IPluginContext *pContext, const cell_t *params)
{
IDBDriver *driver;
@ -1350,6 +1370,20 @@ static cell_t SQL_ReadDriver(IPluginContext *pContext, const cell_t *params)
return driver->GetHandle();
}
static cell_t Database_Driver_get(IPluginContext *pContext, const cell_t *params)
{
IDatabase *db1=NULL;
HandleError err;
if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db1))
!= HandleError_None)
{
return pContext->ThrowNativeError("Invalid database Handle 1/%x (error: %d)", params[1], err);
}
return db1->GetDriver()->GetHandle();
}
static cell_t SQL_CheckConfig(IPluginContext *pContext, const cell_t *params)
{
char *name;
@ -1743,9 +1777,48 @@ static cell_t SQL_ExecuteTransaction(IPluginContext *pContext, const cell_t *par
REGISTER_NATIVES(dbNatives)
{
// Transitional syntax support.
{"DBDriver.Find", SQL_GetDriver},
{"DBDriver.GetIdentifier", SQL_GetDriverIdent},
{"DBDriver.GetProduct", SQL_GetDriverProduct},
{"DBResultSet.FetchMoreResults", SQL_FetchMoreResults},
{"DBResultSet.HasResults.get", SQL_HasResultSet},
{"DBResultSet.RowCount.get", SQL_GetRowCount},
{"DBResultSet.FieldCount.get", SQL_GetFieldCount},
{"DBResultSet.AffectedRows.get", SQL_GetAffectedRows},
{"DBResultSet.InsertId.get", SQL_GetInsertId},
{"DBResultSet.FieldNameToNum", SQL_FieldNameToNum},
{"DBResultSet.FieldNumToName", SQL_FieldNumToName},
{"DBResultSet.FetchRow", SQL_FetchRow},
{"DBResultSet.MoreRows.get", SQL_MoreRows},
{"DBResultSet.Rewind", SQL_Rewind},
{"DBResultSet.FetchString", SQL_FetchString},
{"DBResultSet.FetchFloat", SQL_FetchFloat},
{"DBResultSet.FetchInt", SQL_FetchInt},
{"DBResultSet.IsFieldNull", SQL_IsFieldNull},
{"DBResultSet.FetchSize", SQL_FetchSize},
{"Transaction.Transaction", SQL_CreateTransaction},
{"Transaction.AddQuery", SQL_AddQuery},
{"DBStatement.BindInt", SQL_BindParamInt},
{"DBStatement.BindFloat", SQL_BindParamFloat},
{"DBStatement.BindString", SQL_BindParamString},
{"Database.Connect", Database_Connect},
{"Database.Driver.get", Database_Driver_get},
{"Database.SetCharset", SQL_SetCharset},
{"Database.Escape", SQL_QuoteString},
{"Database.IsSameConnection", SQL_IsSameConnection},
{"Database.Execute", SQL_ExecuteTransaction},
// Note: The callback is ABI compatible so we can re-use the native.
{"Database.Query", SQL_TQuery},
{"SQL_BindParamInt", SQL_BindParamInt},
{"SQL_BindParamFloat", SQL_BindParamFloat},
{"SQL_BindParamString", SQL_BindParamString},\
{"SQL_BindParamString", SQL_BindParamString},
{"SQL_CheckConfig", SQL_CheckConfig},
{"SQL_Connect", SQL_Connect},
{"SQL_ConnectEx", SQL_ConnectEx},

View File

@ -48,8 +48,8 @@ public Plugin:myinfo =
public OnRebuildAdminCache(AdminCachePart:part)
{
/* First try to get a database connection */
decl String:error[255];
new Handle:db;
char error[255];
Database db;
if (SQL_CheckConfig("admins"))
{
@ -76,13 +76,13 @@ public OnRebuildAdminCache(AdminCachePart:part)
delete db;
}
FetchUsers(Handle:db)
void FetchUsers(Database db)
{
decl String:query[255], String:error[255];
new Handle:hQuery;
char query[255], error[255];
DBResultSet rs;
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));
LogError("FetchUsers() query failed: %s", query);
@ -90,30 +90,31 @@ FetchUsers(Handle:db)
return;
}
decl String:authtype[16];
decl String:identity[80];
decl String:password[80];
decl String:flags[32];
decl String:name[80];
new immunity;
new AdminId:adm, id;
new GroupId:gid;
char authtype[16];
char identity[80];
char password[80];
char flags[32];
char name[80];
int immunity;
AdminId adm;
GroupId gid;
int id;
/* Keep track of a mapping from admin DB IDs to internal AdminIds to
* enable group lookups en masse */
new Handle:htAdmins = CreateTrie();
decl String:key[16];
StringMap htAdmins = new StringMap();
char key[16];
while (SQL_FetchRow(hQuery))
while (rs.FetchRow())
{
id = SQL_FetchInt(hQuery, 0);
id = rs.FetchInt(0);
IntToString(id, key, sizeof(key));
SQL_FetchString(hQuery, 1, authtype, sizeof(authtype));
SQL_FetchString(hQuery, 2, identity, sizeof(identity));
SQL_FetchString(hQuery, 3, password, sizeof(password));
SQL_FetchString(hQuery, 4, flags, sizeof(flags));
SQL_FetchString(hQuery, 5, name, sizeof(name));
immunity = SQL_FetchInt(hQuery, 6);
rs.FetchString(1, authtype, sizeof(authtype));
rs.FetchString(2, identity, sizeof(identity));
rs.FetchString(3, password, sizeof(password));
rs.FetchString(4, flags, sizeof(flags));
rs.FetchString(5, name, sizeof(name));
immunity = rs.FetchInt(6);
/* Use a pre-existing admin if we can */
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);
#endif
#endif
/* See if this admin wants a password */
if (password[0] != '\0')
@ -139,8 +140,8 @@ FetchUsers(Handle:db)
}
/* Apply each flag */
new len = strlen(flags);
new AdminFlag:flag;
int len = strlen(flags);
AdminFlag flag;
for (new i=0; i<len; i++)
{
if (!FindFlagByChar(flags[i], flag))
@ -153,9 +154,10 @@ FetchUsers(Handle:db)
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");
if ((hGroupQuery = SQL_Query(db, query)) == null)
if ((rs = SQL_Query(db, query)) == null)
{
SQL_GetError(db, error, sizeof(error));
LogError("FetchUsers() query failed: %s", query);
@ -163,13 +165,13 @@ FetchUsers(Handle:db)
return;
}
decl String:group[80];
while (SQL_FetchRow(hGroupQuery))
char group[80];
while (rs.FetchRow())
{
IntToString(SQL_FetchInt(hGroupQuery, 0), key, sizeof(key));
SQL_FetchString(hGroupQuery, 1, group, sizeof(group));
IntToString(rs.FetchInt(0), key, sizeof(key));
rs.FetchString(1, group, sizeof(group));
if (GetTrieValue(htAdmins, key, adm))
if (htAdmins.GetValue(key, adm))
{
if ((gid = FindAdmGroup(group)) == INVALID_GROUP_ID)
{
@ -181,21 +183,20 @@ FetchUsers(Handle:db)
}
}
delete hQuery;
delete hGroupQuery;
delete rs;
delete htAdmins;
}
FetchGroups(Handle:db)
FetchGroups(Database db)
{
decl String:query[255];
new Handle:hQuery;
char query[255];
DBResultSet rs;
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));
LogError("FetchGroups() query failed: %s", query);
LogError("Query error: %s", error);
@ -203,28 +204,28 @@ FetchGroups(Handle:db)
}
/* Now start fetching groups */
decl String:flags[32];
decl String:name[128];
new immunity;
while (SQL_FetchRow(hQuery))
char flags[32];
char name[128];
int immunity;
while (rs.FetchRow())
{
SQL_FetchString(hQuery, 0, flags, sizeof(flags));
SQL_FetchString(hQuery, 1, name, sizeof(name));
immunity = SQL_FetchInt(hQuery, 2);
rs.FetchString(0, flags, sizeof(flags));
rs.FetchString(1, name, sizeof(name));
immunity = rs.FetchInt(2);
#if defined _DEBUG
PrintToServer("Adding group (%d, %s, %s)", immunity, flags, name);
#endif
/* Find or create the group */
new GroupId:gid;
GroupId gid;
if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID)
{
gid = CreateAdmGroup(name);
}
/* 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++)
{
decl AdminFlag:flag;
@ -239,7 +240,7 @@ FetchGroups(Handle:db)
SetAdmGroupImmunityLevel(gid, immunity);
}
delete hQuery;
delete rs;
/**
* 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 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));
LogError("FetchGroups() query failed: %s", query);
LogError("Query error: %s", error);
return;
}
while (SQL_FetchRow(hQuery))
while (rs.FetchRow())
{
decl String:group1[80];
decl String:group2[80];
new GroupId:gid1, GroupId:gid2;
char group1[80];
char group2[80];
GroupId gid1, gid2;
SQL_FetchString(hQuery, 0, group1, sizeof(group1));
SQL_FetchString(hQuery, 1, group2, sizeof(group2));
rs.FetchString(0, group1, sizeof(group1));
rs.FetchString(1, group2, sizeof(group2));
if (((gid1 = FindAdmGroup(group1)) == INVALID_GROUP_ID)
|| (gid2 = FindAdmGroup(group2)) == INVALID_GROUP_ID)
@ -279,85 +280,85 @@ FetchGroups(Handle:db)
#endif
}
delete hQuery;
delete rs;
/**
* 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");
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));
LogError("FetchGroups() query failed: %s", query);
LogError("Query error: %s", error);
return;
}
decl String:type[16];
decl String:cmd[64];
decl String:access[16];
while (SQL_FetchRow(hQuery))
char type[16];
char cmd[64];
char access[16];
while (rs.FetchRow())
{
SQL_FetchString(hQuery, 0, name, sizeof(name));
SQL_FetchString(hQuery, 1, type, sizeof(type));
SQL_FetchString(hQuery, 2, cmd, sizeof(cmd));
SQL_FetchString(hQuery, 3, access, sizeof(access));
rs.FetchString(0, name, sizeof(name));
rs.FetchString(1, type, sizeof(type));
rs.FetchString(2, cmd, sizeof(cmd));
rs.FetchString(3, access, sizeof(access));
new GroupId:gid;
GroupId gid;
if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID)
{
continue;
}
new OverrideType:o_type = Override_Command;
OverrideType o_type = Override_Command;
if (StrEqual(type, "group"))
{
o_type = Override_CommandGroup;
}
new OverrideRule:o_rule = Command_Deny;
OverrideRule o_rule = Command_Deny;
if (StrEqual(access, "allow"))
{
o_rule = Command_Allow;
}
#if defined _DEBUG
#if defined _DEBUG
PrintToServer("AddAdmGroupCmdOverride(%d, %s, %d, %d)", gid, cmd, o_type, o_rule);
#endif
#endif
AddAdmGroupCmdOverride(gid, cmd, o_type, o_rule);
}
delete hQuery;
delete rs;
}
FetchOverrides(Handle:db)
FetchOverrides(Database db)
{
decl String:query[255];
new Handle:hQuery;
char query[255];
DBResultSet rs;
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));
LogError("FetchOverrides() query failed: %s", query);
LogError("Query error: %s", error);
return;
}
decl String:type[64];
decl String:name[64];
decl String:flags[32];
new flag_bits;
while (SQL_FetchRow(hQuery))
char type[64];
char name[64];
char flags[32];
int flag_bits;
while (rs.FetchRow())
{
SQL_FetchString(hQuery, 0, type, sizeof(type));
SQL_FetchString(hQuery, 1, name, sizeof(name));
SQL_FetchString(hQuery, 2, flags, sizeof(flags));
rs.FetchString(0, type, sizeof(type));
rs.FetchString(1, name, sizeof(name));
rs.FetchString(2, flags, sizeof(flags));
#if defined _DEBUG
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.
*/
new Handle:hDatabase = null; /** Database connection */
Database hDatabase = null; /** Database connection */
new g_sequence = 0; /** Global unique sequence number */
new ConnectLock = 0; /** Connect sequence number */
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.
*/
if (hDatabase != null)
{
delete hDatabase;
hDatabase = null;
}
delete hDatabase;
}
public bool:OnClientConnect(client, String:rejectmsg[], maxlen)
@ -101,10 +97,10 @@ public OnClientDisconnect(client)
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
PrintToServer("OnDatabaseConnect(%x,%x,%d) ConnectLock=%d", owner, hndl, data, ConnectLock);
PrintToServer("OnDatabaseConnect(%x, %d) ConnectLock=%d", db, data, ConnectLock);
#endif
/**
@ -112,15 +108,12 @@ public OnDatabaseConnect(Handle:owner, Handle:hndl, const String:error[], any:da
*/
if (data != ConnectLock || hDatabase != null)
{
if (hndl != null)
{
delete hndl;
}
delete db;
return;
}
ConnectLock = 0;
hDatabase = hndl;
hDatabase = db;
/**
* See if the connection is valid. If not, don't un-mark the caches
@ -155,20 +148,20 @@ RequestDatabaseConnection()
ConnectLock = ++g_sequence;
if (SQL_CheckConfig("admins"))
{
SQL_TConnect(OnDatabaseConnect, "admins", ConnectLock);
Database.Connect(OnDatabaseConnect, "admins", ConnectLock);
} 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
* callback system to determine whether the results should still be
* used.
*/
new sequence = ++g_sequence;
int sequence = ++g_sequence;
RebuildCachePart[_:part] = sequence;
/**
@ -196,7 +189,7 @@ public OnRebuildAdminCache(AdminCachePart:part)
}
}
public Action:OnClientPreAdminCheck(client)
public Action OnClientPreAdminCheck(client)
{
PlayerAuth[client] = true;
@ -235,13 +228,13 @@ public Action:OnClientPreAdminCheck(client)
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;
ResetPack(pk);
DataPack pk = view_as<DataPack>(data);
pk.Reset();
new client = ReadPackCell(pk);
new sequence = ReadPackCell(pk);
int client = pk.ReadCell();
int sequence = pk.ReadCell();
/**
* Make sure it's the same client.
@ -252,7 +245,7 @@ public OnReceiveUserGroups(Handle:owner, Handle:hndl, const String:error[], any:
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.
@ -267,10 +260,10 @@ public OnReceiveUserGroups(Handle:owner, Handle:hndl, const String:error[], any:
/**
* See if we got results.
*/
if (hndl == null)
if (rs == null)
{
decl String:query[255];
ReadPackString(pk, query, sizeof(query));
char query[255];
pk.ReadString(query, sizeof(query));
LogError("SQL error receiving user: %s", error);
LogError("Query dump: %s", query);
NotifyPostAdminCheck(client);
@ -278,12 +271,12 @@ public OnReceiveUserGroups(Handle:owner, Handle:hndl, const String:error[], any:
return;
}
decl String:name[80];
new GroupId:gid;
char name[80];
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)
{
@ -304,17 +297,17 @@ public OnReceiveUserGroups(Handle:owner, Handle:hndl, const String:error[], any:
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;
ResetPack(pk);
new client = ReadPackCell(pk);
DataPack pk = view_as<DataPack>(data);
pk.Reset();
int client = pk.ReadCell();
/**
* Check if this is the latest result request.
*/
new sequence = ReadPackCell(pk);
int sequence = pk.ReadCell();
if (PlayerSeq[client] != 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 (hndl == null)
if (rs == null)
{
decl String:query[255];
ReadPackString(pk, query, sizeof(query));
char query[255];
pk.ReadString(query, sizeof(query));
LogError("SQL error receiving user: %s", error);
LogError("Query dump: %s", query);
RunAdminCacheChecks(client);
@ -337,7 +330,7 @@ public OnReceiveUser(Handle:owner, Handle:hndl, const String:error[], any:data)
return;
}
new num_accounts = SQL_GetRowCount(hndl);
int num_accounts = rs.RowCount;
if (num_accounts == 0)
{
RunAdminCacheChecks(client);
@ -346,29 +339,29 @@ public OnReceiveUser(Handle:owner, Handle:hndl, const String:error[], any:data)
return;
}
decl String:authtype[16];
decl String:identity[80];
decl String:password[80];
decl String:flags[32];
decl String:name[80];
new AdminId:adm, id;
new immunity;
char authtype[16];
char identity[80];
char password[80];
char flags[32];
char name[80];
int immunity, id;
AdminId adm;
/**
* Cache user info -- [0] = db id, [1] = cache id, [2] = groups
*/
decl user_lookup[num_accounts][3];
new total_users = 0;
char[][] user_lookup = new char[num_accounts][3];
int total_users = 0;
while (SQL_FetchRow(hndl))
while (rs.FetchRow())
{
id = SQL_FetchInt(hndl, 0);
SQL_FetchString(hndl, 1, authtype, sizeof(authtype));
SQL_FetchString(hndl, 2, identity, sizeof(identity));
SQL_FetchString(hndl, 3, password, sizeof(password));
SQL_FetchString(hndl, 4, flags, sizeof(flags));
SQL_FetchString(hndl, 5, name, sizeof(name));
immunity = SQL_FetchInt(hndl, 7);
id = rs.FetchInt(0);
rs.FetchString(1, authtype, sizeof(authtype));
rs.FetchString(2, identity, sizeof(identity));
rs.FetchString(3, password, sizeof(password));
rs.FetchString(4, flags, sizeof(flags));
rs.FetchString(5, name, sizeof(name));
immunity = rs.FetchInt(7);
/* For dynamic admins we clear anything already in the cache. */
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][1] = _:adm;
user_lookup[total_users][2] = SQL_FetchInt(hndl, 6);
user_lookup[total_users][2] = rs.FetchInt(6);
total_users++;
#if defined _DEBUG
@ -401,8 +394,8 @@ public OnReceiveUser(Handle:owner, Handle:hndl, const String:error[], any:data)
SetAdminImmunityLevel(adm, immunity);
/* Apply each flag */
new len = strlen(flags);
new AdminFlag:flag;
int len = strlen(flags);
AdminFlag flag;
for (new i=0; i<len; i++)
{
if (!FindFlagByChar(flags[i], flag))
@ -416,7 +409,7 @@ public OnReceiveUser(Handle:owner, Handle:hndl, const String:error[], any:data)
/**
* Try binding the user.
*/
new group_count = 0;
int group_count = 0;
RunAdminCacheChecks(client);
adm = GetUserAdmin(client);
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!
*/
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);
ResetPack(pk);
WritePackCell(pk, client);
WritePackCell(pk, sequence);
WritePackCell(pk, _:adm);
WritePackString(pk, query);
pk.Reset();
pk.WriteCell(client);
pk.WriteCell(sequence);
pk.WriteCell(_:adm);
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];
decl String:safe_name[140];
decl String:steamid[32];
decl String:steamidalt[32];
decl String:ipaddr[24];
char name[65];
char safe_name[140];
char steamid[32];
char steamidalt[32];
char ipaddr[24];
/**
* 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.
*/
decl String:query[512];
char query[512];
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");
@ -511,22 +504,21 @@ FetchUser(Handle:db, client)
*/
PlayerSeq[client] = ++g_sequence;
new Handle:pk;
pk = CreateDataPack();
WritePackCell(pk, client);
WritePackCell(pk, PlayerSeq[client]);
WritePackString(pk, query);
DataPack pk = new DataPack();
pk.WriteCell(client);
pk.WriteCell(PlayerSeq[client]);
pk.WriteString(query);
#if defined _DEBUG
PrintToServer("Sending user query: %s", query);
#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)
{
@ -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;
ResetPack(pk);
DataPack pk = view_as<DataPack>(data);
pk.Reset();
/**
* Check if this is the latest result request.
*/
new sequence = ReadPackCell(pk);
int sequence = pk.ReadCell();
if (RebuildCachePart[_:AdminCache_Groups] != 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 (hndl == null)
if (rs == null)
{
decl String:query[255];
ReadPackString(pk, query, sizeof(query));
char query[255];
pk.ReadString(query, sizeof(query));
LogError("SQL error receiving group immunity: %s", error);
LogError("Query dump: %s", query);
delete pk;
@ -573,14 +565,14 @@ public OnReceiveGroupImmunity(Handle:owner, Handle:hndl, const String:error[], a
/* We're done with the pack forever. */
delete pk;
while (SQL_FetchRow(hndl))
while (rs.FetchRow())
{
decl String:group1[80];
decl String:group2[80];
new GroupId:gid1, GroupId:gid2;
char group1[80];
char group2[80];
GroupId gid1, gid2;
SQL_FetchString(hndl, 0, group1, sizeof(group1));
SQL_FetchString(hndl, 1, group2, sizeof(group2));
rs.FetchString(0, group1, sizeof(group1));
rs.FetchString(1, group2, sizeof(group2));
if (((gid1 = FindAdmGroup(group1)) == 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;
}
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;
ResetPack(pk);
DataPack pk = view_as<DataPack>(data);
pk.Reset();
/**
* Check if this is the latest result request.
*/
new sequence = ReadPackCell(pk);
int sequence = pk.ReadCell();
if (RebuildCachePart[_:AdminCache_Groups] != 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 (hndl == null)
if (rs == null)
{
decl String:query[255];
ReadPackString(pk, query, sizeof(query));
char query[255];
pk.ReadString(query, sizeof(query));
LogError("SQL error receiving group overrides: %s", error);
LogError("Query dump: %s", query);
delete pk;
@ -630,17 +622,17 @@ public OnReceiveGroupOverrides(Handle:owner, Handle:hndl, const String:error[],
/**
* Fetch the overrides.
*/
decl String:name[80];
decl String:type[16];
decl String:command[64];
decl String:access[16];
new GroupId:gid;
while (SQL_FetchRow(hndl))
char name[80];
char type[16];
char command[64];
char access[16];
GroupId gid;
while (rs.FetchRow())
{
SQL_FetchString(hndl, 0, name, sizeof(name));
SQL_FetchString(hndl, 1, type, sizeof(type));
SQL_FetchString(hndl, 2, command, sizeof(command));
SQL_FetchString(hndl, 3, access, sizeof(access));
rs.FetchString(0, name, sizeof(name));
rs.FetchString(1, type, sizeof(type));
rs.FetchString(2, command, sizeof(command));
rs.FetchString(3, access, sizeof(access));
/* Find the group. This is actually faster than doing the ID lookup. */
if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID)
@ -649,13 +641,13 @@ public OnReceiveGroupOverrides(Handle:owner, Handle:hndl, const String:error[],
continue;
}
new OverrideType:o_type = Override_Command;
OverrideType o_type = Override_Command;
if (StrEqual(type, "group"))
{
o_type = Override_CommandGroup;
}
new OverrideRule:o_rule = Command_Deny;
OverrideRule o_rule = Command_Deny;
if (StrEqual(access, "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.
*/
new len = 0;
decl String:query[256];
int len = 0;
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, " 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");
ResetPack(pk);
WritePackCell(pk, sequence);
WritePackString(pk, query);
pk.Reset();
pk.WriteCell(sequence);
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;
ResetPack(pk);
DataPack pk = view_as<DataPack>(data);
pk.Reset();
/**
* Check if this is the latest result request.
*/
new sequence = ReadPackCell(pk);
int sequence = pk.ReadCell();
if (RebuildCachePart[_:AdminCache_Groups] != 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 (hndl == null)
if (rs == null)
{
decl String:query[255];
ReadPackString(pk, query, sizeof(query));
char query[255];
pk.ReadString(query, sizeof(query));
LogError("SQL error receiving groups: %s", error);
LogError("Query dump: %s", query);
delete pk;
@ -716,29 +708,29 @@ public OnReceiveGroups(Handle:owner, Handle:hndl, const String:error[], any:data
/**
* Now start fetching groups.
*/
decl String:flags[32];
decl String:name[128];
new immunity;
while (SQL_FetchRow(hndl))
char flags[32];
char name[128];
int immunity;
while (rs.FetchRow())
{
SQL_FetchString(hndl, 0, flags, sizeof(flags));
SQL_FetchString(hndl, 1, name, sizeof(name));
immunity = SQL_FetchInt(hndl, 2);
rs.FetchString(0, flags, sizeof(flags));
rs.FetchString(1, name, sizeof(name));
immunity = rs.FetchInt(2);
#if defined _DEBUG
PrintToServer("Adding group (%d, %s, %s)", immunity, flags, name);
#endif
/* Find or create the group */
new GroupId:gid;
GroupId gid;
if ((gid = FindAdmGroup(name)) == INVALID_GROUP_ID)
{
gid = CreateAdmGroup(name);
}
/* Add flags from the database to the group */
new num_flag_chars = strlen(flags);
for (new i=0; i<num_flag_chars; i++)
int num_flag_chars = strlen(flags);
for (int i=0; i<num_flag_chars; i++)
{
decl AdminFlag: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.
*/
decl String:query[255];
char query[255];
Format(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");
ResetPack(pk);
WritePackCell(pk, sequence);
WritePackString(pk, query);
pk.Reset();
pk.WriteCell(sequence);
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];
new Handle:pk;
char query[255];
Format(query, sizeof(query), "SELECT flags, name, immunity_level FROM sm_groups");
pk = CreateDataPack();
WritePackCell(pk, sequence);
WritePackString(pk, query);
DataPack pk = new DataPack();
pk.WriteCell(sequence);
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;
ResetPack(pk);
DataPack pk = view_as<DataPack>(data);
pk.Reset();
/**
* Check if this is the latest result request.
*/
new sequence = ReadPackCell(pk);
int sequence = pk.ReadCell();
if (RebuildCachePart[_:AdminCache_Overrides] != 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 (hndl == null)
if (rs == null)
{
decl String:query[255];
ReadPackString(pk, query, sizeof(query));
char query[255];
pk.ReadString(query, sizeof(query));
LogError("SQL error receiving overrides: %s", error);
LogError("Query dump: %s", query);
delete pk;
@ -814,15 +805,15 @@ public OnReceiveOverrides(Handle:owner, Handle:hndl, const String:error[], any:d
*/
delete pk;
decl String:type[64];
decl String:name[64];
decl String:flags[32];
new flag_bits;
while (SQL_FetchRow(hndl))
char type[64];
char name[64];
char flags[32];
int flag_bits;
while (rs.FetchRow())
{
SQL_FetchString(hndl, 0, type, sizeof(type));
SQL_FetchString(hndl, 1, name, sizeof(name));
SQL_FetchString(hndl, 2, flags, sizeof(flags));
rs.FetchString(0, type, sizeof(type));
rs.FetchString(1, name, sizeof(name));
rs.FetchString(2, flags, sizeof(flags));
#if defined _DEBUG
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;
}
FetchOverrides(Handle:db, sequence)
void FetchOverrides(Database db, sequence)
{
decl String:query[255];
new Handle:pk;
char query[255];
Format(query, sizeof(query), "SELECT type, name, flags FROM sm_overrides");
pk = CreateDataPack();
WritePackCell(pk, sequence);
WritePackString(pk, query);
DataPack pk = new DataPack();
pk.WriteCell(sequence);
pk.WriteString(query);
SQL_TQuery(db, OnReceiveOverrides, query, pk, DBPrio_High);
db.Query(OnReceiveOverrides, query, pk, DBPrio_High);
}

View File

@ -1,7 +1,7 @@
/**
* vim: set ts=4 :
* vim: set ts=4 sw=4 tw=99 noet :
* =============================================================================
* SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
* SourceMod (C)2004-2014 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This file is part of the SourceMod/SourcePawn SDK.
@ -35,35 +35,6 @@
#endif
#define _dbi_included
/**
* @handle Driver
*
* Contains information about an SQL driver.
*/
/**
* @handle Database
*
* Contains information about a database connection.
*/
/**
* @handle Query
*
* Contains information about an active query and its
* result sets.
*/
/**
* @handle Statement : Query
*
* Extends a Query Handle and can be used as a Query Handle.
* Statement Handles are for prepared queries and contain
* their own function for binding parameters. Statement
* Handles can be used instead of database Handles in a few
* select functions.
*/
/**
* Describes a database field fetch status.
*/
@ -95,6 +66,345 @@ enum DBPriority
DBPrio_Low = 2, /**< Low priority. */
};
// A Driver represents a database backend, currently MySQL or SQLite.
//
// Driver handles cannot be closed.
methodmap DBDriver < Handle
{
// Finds the driver associated with a name.
//
// Supported driver strings:
// mysql
// sqlite
//
// @param name Driver identification string, or an empty string
// to return the default driver.
// @return Driver handle, or null on failure.
public static native DBDriver Find(const char[] name = "");
// Retrieves a driver's identification string.
//
// Example: "mysql", "sqlite"
//
// @param ident Identification string buffer.
// @param maxlength Maximum length of the buffer.
public native void GetIdentifier(char[] ident, int maxlength);
// Retrieves a driver's product string.
//
// Example: "MySQL", "SQLite"
//
// @param product Product string buffer.
// @param maxlength Maximum length of the buffer.
public native void GetProduct(char[] product, int maxlength);
};
// Represents a set of results returned from executing a query.
methodmap DBResultSet < Handle
{
// Advances to the next set of results.
//
// In some SQL implementations, multiple result sets can exist on one query.
// This is possible in MySQL with simple queries when executing a CALL
// query. If this is the case, all result sets must be processed before
// another query is made.
//
// @return True if there was another result set, false otherwise.
public native bool FetchMoreResults();
// Returns whether or not a result set exists. This will
// return true even if 0 results were returned, but false
// on queries like UPDATE, INSERT, or DELETE.
property bool HasResults {
public native get();
}
// Retrieves the number of rows in the last result set.
//
// @param query A query (or statement) Handle.
// @return Number of rows in the current result set.
property int RowCount {
public native get();
}
// Retrieves the number of fields in the last result set.
property int FieldCount {
public native get();
}
// Returns the number of affected rows from the query that generated this
// result set.
property int AffectedRows {
public native get();
}
// Returns the insert id from the query that generated this result set.
property int InsertId {
public native get();
}
// Retrieves the name of a field by index.
//
// @param field Field number (starting from 0).
// @param name Name buffer.
// @param maxlength Maximum length of the name buffer.
// @error Invalid field index, or no current result set.
public native void FieldNumToName(int field, char[] name, int maxlength);
// Retrieves a field index by name.
//
// @param name Name of the field (case sensitive).
// @param field Variable to store field index in.
// @return True if found, false if not found.
// @error No current result set.
public native bool FieldNameToNum(const char[] name, int &field);
// Fetches a row from the current result set. This must be
// successfully called before any results are fetched.
//
// If this function fails, _MoreResults can be used to
// tell if there was an error or the result set is finished.
//
// @return True if a row was fetched, false otherwise.
public native bool FetchRow();
// Returns if there are more rows.
//
// @return True if there are more rows, false otherwise.
property bool MoreRows {
public native get();
}
// Rewinds a result set back to the first result.
//
// @return True on success, false otherwise.
// @error No current result set.
public native bool Rewind();
// Fetches a string from a field in the current row of a result set.
// If the result is NULL, an empty string will be returned. A NULL
// check can be done with the result parameter, or SQL_IsFieldNull().
//
// @param field The field index (starting from 0).
// @param buffer String buffer.
// @param maxlength Maximum size of the string buffer.
// @param result Optional variable to store the status of the return value.
// @return Number of bytes written.
// @error Invalid field index, invalid type conversion requested
// from the database, or no current result set.
public native int FetchString(int field, char[] buffer, int maxlength,
DBResult &result=DBVal_Error);
// Fetches a float from a field in the current row of a result set.
// If the result is NULL, a value of 0.0 will be returned. A NULL
// check can be done with the result parameter, or SQL_IsFieldNull().
//
// @param field The field index (starting from 0).
// @param result Optional variable to store the status of the return value.
// @return A float value.
// @error Invalid field index, invalid type conversion requested
// from the database, or no current result set.
public native float FetchFloat(int field, DBResult &result=DBVal_Error);
// Fetches an integer from a field in the current row of a result set.
// If the result is NULL, a value of 0 will be returned. A NULL
// check can be done with the result parameter, or SQL_IsFieldNull().
//
// @param field The field index (starting from 0).
// @param result Optional variable to store the status of the return value.
// @return An integer value.
// @error Invalid field index, invalid type conversion requested
// from the database, or no current result set.
public native int FetchInt(int field, DBResult &result=DBVal_Error);
// Returns whether a field's data in the current row of a result set is
// NULL or not. NULL is an SQL type which means "no data."
//
// @param field The field index (starting from 0).
// @return True if data is NULL, false otherwise.
// @error Invalid field index, or no current result set.
public native bool IsFieldNull(Handle query, int field);
// Returns the length of a field's data in the current row of a result
// set. This only needs to be called for strings to determine how many
// bytes to use. Note that the return value does not include the null
// terminator.
//
// @param field The field index (starting from 0).
// @return Number of bytes for the field's data size.
// @error Invalid field index or no current result set.
public native int FetchSize(int field);
};
// Callback for a successful transaction.
//
// @param db Database handle.
// @param data Data value passed to SQL_ExecuteTransaction().
// @param numQueries Number of queries executed in the transaction.
// @param results An array of Query handle results, one for each of numQueries. They are closed automatically.
// @param queryData An array of each data value passed to SQL_AddQuery().
typedef SQLTxnSuccess = function void (Database db, any data, int numQueries, Handle[] results, any[] queryData);
// Callback for a failed transaction.
//
// @param db Database handle.
// @param data Data value passed to SQL_ExecuteTransaction().
// @param numQueries Number of queries executed in the transaction.
// @param error Error message.
// @param failIndex Index of the query that failed, or -1 if something else.
// @param queryData An array of each data value passed to SQL_AddQuery().
typedef SQLTxnFailure = function void (Database db, any data, int numQueries, const char[] error, int failIndex, any[] queryData);
// A Transaction is a collection of SQL statements that must all execute
// successfully or not at all.
methodmap Transaction < Handle
{
// Create a new transaction.
public native Transaction();
// Adds a query to the transaction.
//
// @param query Query string.
// @param data Extra data value to pass to the final callback.
// @return The index of the query in the transaction's query list.
public native int AddQuery(const char[] query, any data=0);
};
// A DBStatement is a pre-compiled SQL query that may be executed multiple
// times with different parameters. A DBStatement holds a reference to the
// Database that prepared it.
methodmap DBStatement < Handle
{
// Binds a parameter in a prepared statement to a given integer value.
//
// @param param The parameter index (starting from 0).
// @param number The number to bind.
// @param signed True to bind the number as signed, false to
// bind it as unsigned.
// @error Invalid parameter index, or SQL error.
public native void BindInt(int param, int number, bool signed=true);
// Binds a parameter in a prepared statement to a given float value.
//
// @param param The parameter index (starting from 0).
// @param value The float number to bind.
// @error Invalid parameter index, or SQL error.
public native void BindFloat(int param, float value);
// Binds a parameter in a prepared statement to a given string value.
//
// @param param The parameter index (starting from 0).
// @param value The string to bind.
// @param copy Whether or not SourceMod should copy the value
// locally if necessary. If the string contents
// won't change before calling SQL_Execute(), this
// can be set to false for optimization.
// @error Invalid parameter index, or SQL error.
public native void BindString(int param, const char[] value, bool copy);
};
// Callback for receiving asynchronous database connections.
//
// @param db Handle to the database connection.
// @param error Error string if there was an error. The error could be
// empty even if an error condition exists, so it is important
// to check the actual Handle value instead.
// @param data Data passed in via the original threaded invocation.
typedef SQLConnectCallback = function void (Database db, const char[] error, any data);
// Callback for receiving asynchronous database query results.
//
// @param db Cloned handle to the database connection.
// @param results Result object, or null on failure.
// @param error Error string if there was an error. The error could be
// empty even if an error condition exists, so it is important
// to check the actual results value instead.
// @param data Data passed in via the original threaded invocation.
typedef SQLQueryCallback = function void (Database db, DBResultSet results, const char[] error, any data);
// A Database represents a live connection to a database, either over the
// wire, through a unix domain socket, or over an open file.
methodmap Database < Handle
{
// Connects to a database asynchronously, so the game thread is not blocked.
//
// @param callback Callback. If no driver was found, the owner is null.
// @param name Database configuration name.
// @param data Extra data value to pass to the callback.
public static native void Connect(SQLConnectCallback callback, const char[] name="default", any data=0);
// Returns the driver for this database connection.
property DBDriver Driver {
public native get();
}
// Sets the character set of the connection.
// Like SET NAMES .. in mysql, but stays after connection problems.
//
// Example: "utf8", "latin1"
//
// @param characterset The character set string to change to.
// @return True, if character set was changed, false otherwise.
public native bool SetCharset(const char[] charset);
// Escapes a database string for literal insertion. This is not needed
// for binding strings in prepared statements.
//
// Generally, database strings are inserted into queries enclosed in
// single quotes ('). If user input has a single quote in it, the
// quote needs to be escaped. This function ensures that any unsafe
// characters are safely escaped according to the database engine and
// the database's character set.
//
// NOTE: SourceMod only guarantees properly escaped strings when the query
// encloses the string in ''. While drivers tend to allow " instead, the string
// may be not be escaped (for example, on SQLite)!
//
// @param string String to quote.
// @param buffer Buffer to store quoted string in.
// @param maxlength Maximum length of the buffer.
// @param written Optionally returns the number of bytes written.
// @return True on success, false if buffer is not big enough.
// The buffer must be at least 2*strlen(string)+1.
public native bool Escape(const char[] string, char[] buffer, int maxlength, int &written=0);
// Returns whether a database is the same connection as another database.
public native bool IsSameConnection(Database other);
// Executes a query via a thread. The result handle is passed through the
// callback.
//
// The database handle returned through the callback is always a new Handle,
// and if necessary, IsSameConnection() should be used to test against other
// conenctions.
//
// The result handle returned through the callback is temporary and destroyed
// at the end of the callback.
//
// @param callback Callback.
// @param query Query string.
// @param data Extra data value to pass to the callback.
// @param prio Priority queue to use.
public native void Query(SQLQueryCallback callback, const char[] query,
any data = 0,
DBPriority prio = DBPrio_Normal);
// Sends a transaction to the database thread. The transaction handle is
// automatically closed. When the transaction completes, the optional
// callback is invoked.
//
// @param txn A transaction handle.
// @param onSuccess An optional callback to receive a successful transaction.
// @param onError An optional callback to receive an error message.
// @param data An optional value to pass to callbacks.
// @param prio Priority queue to use.
public native void Execute(Transaction txn,
SQLTxnSuccess:onSuccess = INVALID_FUNCTION,
SQLTxnFailure:onError = INVALID_FUNCTION,
any data = 0,
DBPriority priority = DBPrio_Normal);
};
/**
* Creates an SQL connection from a named configuration.
*
@ -105,7 +415,7 @@ enum DBPriority
* @param maxlength Maximum length of the error buffer.
* @return A database connection Handle, or INVALID_HANDLE on failure.
*/
native Handle:SQL_Connect(const String:confname[], bool:persistent, String:error[], maxlength);
native Database SQL_Connect(const char[] confname, bool persistent, char[] error, int maxlength);
/**
* Creates a default SQL connection.
@ -117,7 +427,7 @@ native Handle:SQL_Connect(const String:confname[], bool:persistent, String:error
* @return A database connection Handle, or INVALID_HANDLE on failure.
* On failure the error buffer will be filled with a message.
*/
stock Handle:SQL_DefConnect(String:error[], maxlength, bool:persistent=true)
stock Database SQL_DefConnect(char[] error, int maxlength, bool persistent=true)
{
return SQL_Connect("default", persistent, error, maxlength);
}
@ -143,10 +453,10 @@ stock Handle:SQL_DefConnect(String:error[], maxlength, bool:persistent=true)
* On failure the error buffer will be filled with a message.
* @error Invalid KeyValues handle.
*/
native Handle:SQL_ConnectCustom(Handle:keyvalues,
String:error[],
maxlength,
bool:persistent);
native Database SQL_ConnectCustom(Handle keyvalues,
char[] error,
maxlength,
bool persistent);
/**
* Grabs a handle to an SQLite database, creating one if it does not exist.
@ -163,17 +473,17 @@ native Handle:SQL_ConnectCustom(Handle:keyvalues,
* @return A database connection Handle, or INVALID_HANDLE on failure.
* On failure the error buffer will be filled with a message.
*/
stock Handle:SQLite_UseDatabase(const String:database[],
String:error[],
maxlength)
stock Database SQLite_UseDatabase(const char[] database,
char[] error,
maxlength)
{
new Handle:kv, Handle:db;
new Handle kv, Handle db;
kv = CreateKeyValues("");
KvSetString(kv, "driver", "sqlite");
KvSetString(kv, "database", database);
db = SQL_ConnectCustom(kv, error, maxlength, false);
db = SQL_ConnectCustom(kv, error, int maxlength, false);
CloseHandle(kv);
@ -184,14 +494,14 @@ stock Handle:SQLite_UseDatabase(const String:database[],
* This function is deprecated. Use SQL_ConnectCustom or SQLite_UseDatabase instead.
*/
#pragma deprecated Use SQL_ConnectCustom instead.
native Handle:SQL_ConnectEx(Handle:driver,
native Handle SQL_ConnectEx(Handle driver,
const String:host[],
const String:user[],
const String:pass[],
const String:database[],
String:error[],
char[] error,
maxlength,
bool:persistent=true,
bool persistent=true,
port=0,
maxTimeout=0);
@ -201,7 +511,7 @@ native Handle:SQL_ConnectEx(Handle:driver,
* @param name Configuration name.
* @return True if it exists, false otherwise.
*/
native bool:SQL_CheckConfig(const String:name[]);
native bool SQL_CheckConfig(const char[] name);
/**
* Returns a driver Handle from a name string.
@ -213,7 +523,7 @@ native bool:SQL_CheckConfig(const String:name[]);
* string to return the default driver.
* @return Driver Handle, or INVALID_HANDLE on failure.
*/
native Handle:SQL_GetDriver(const String:name[]="");
native Handle SQL_GetDriver(const char[] name="");
/**
* Reads the driver of an opened database.
@ -223,7 +533,7 @@ native Handle:SQL_GetDriver(const String:name[]="");
* @param ident_length Maximum length of the buffer.
* @return Driver Handle.
*/
native Handle:SQL_ReadDriver(Handle:database, String:ident[]="", ident_length=0);
native Handle SQL_ReadDriver(Handle database, char[] ident="", ident_length=0);
/**
* Retrieves a driver's identification string.
@ -236,7 +546,7 @@ native Handle:SQL_ReadDriver(Handle:database, String:ident[]="", ident_length=0)
* @noreturn
* @error Invalid Handle other than INVALID_HANDLE.
*/
native SQL_GetDriverIdent(Handle:driver, String:ident[], maxlength);
native void SQL_GetDriverIdent(Handle driver, char[] ident, int maxlength);
/**
* Retrieves a driver's product string.
@ -249,7 +559,7 @@ native SQL_GetDriverIdent(Handle:driver, String:ident[], maxlength);
* @noreturn
* @error Invalid Handle other than INVALID_HANDLE.
*/
native SQL_GetDriverProduct(Handle:driver, String:product[], maxlength);
native void SQL_GetDriverProduct(Handle driver, char[] product, int maxlength);
/**
* Sets the character set of the current connection.
@ -261,7 +571,7 @@ native SQL_GetDriverProduct(Handle:driver, String:product[], maxlength);
* @param characterset The character set string to change to.
* @return True, if character set was changed, false otherwise.
*/
native bool:SQL_SetCharset(Handle:database, const String:charset[]);
native bool SQL_SetCharset(Handle database, const char[] charset);
/**
* Returns the number of affected rows from the last query.
@ -270,7 +580,7 @@ native bool:SQL_SetCharset(Handle:database, const String:charset[]);
* @return Number of rows affected by the last query.
* @error Invalid database or statement Handle.
*/
native SQL_GetAffectedRows(Handle:hndl);
native int SQL_GetAffectedRows(Handle hndl);
/**
* Returns the last query's insertion id.
@ -279,7 +589,7 @@ native SQL_GetAffectedRows(Handle:hndl);
* @return Last query's insertion id.
* @error Invalid database, query, or statement Handle.
*/
native SQL_GetInsertId(Handle:hndl);
native int SQL_GetInsertId(Handle hndl);
/**
* Returns the error reported by the last query.
@ -290,7 +600,7 @@ native SQL_GetInsertId(Handle:hndl);
* @return True if there was an error, false otherwise.
* @error Invalid database, query, or statement Handle.
*/
native bool:SQL_GetError(Handle:hndl, String:error[], maxlength);
native bool SQL_GetError(Handle hndl, char[] error, int maxlength);
/**
* Escapes a database string for literal insertion. This is not needed
@ -315,23 +625,23 @@ native bool:SQL_GetError(Handle:hndl, String:error[], maxlength);
* The buffer must be at least 2*strlen(string)+1.
* @error Invalid database or statement Handle.
*/
native bool:SQL_EscapeString(Handle:database,
const String:string[],
String:buffer[],
maxlength,
&written=0);
native bool SQL_EscapeString(Handle database,
const char[] string,
char[] buffer,
int maxlength,
int &written=0);
/**
* This is a backwards compatibility stock. You should use SQL_EscapeString()
* instead, as this function will probably be deprecated in SourceMod 1.1.
*/
stock bool:SQL_QuoteString(Handle:database,
const String:string[],
String:buffer[],
stock bool SQL_QuoteString(Handle database,
const char[] string,
char[] buffer,
maxlength,
&written=0)
{
return SQL_EscapeString(database, string, buffer, maxlength, written);
return SQL_EscapeString(database, string, buffer, int maxlength, written);
}
/**
@ -346,7 +656,7 @@ stock bool:SQL_QuoteString(Handle:database,
* SQL_GetError to find the last error.
* @error Invalid database Handle.
*/
native bool:SQL_FastQuery(Handle:database, const String:query[], len=-1);
native bool SQL_FastQuery(Handle database, const char[] query, int len=-1);
/**
* Executes a simple query and returns a new query Handle for
@ -361,7 +671,7 @@ native bool:SQL_FastQuery(Handle:database, const String:query[], len=-1);
* otherwise. The Handle must be freed with CloseHandle().
* @error Invalid database Handle.
*/
native Handle:SQL_Query(Handle:database, const String:query[], len=-1);
native DBResultSet SQL_Query(Handle database, const char[] query, int len=-1);
/**
* Creates a new prepared statement query. Prepared statements can
@ -379,7 +689,7 @@ native Handle:SQL_Query(Handle:database, const String:query[], len=-1);
* otherwise. The Handle must be freed with CloseHandle().
* @error Invalid database Handle.
*/
native Handle:SQL_PrepareQuery(Handle:database, const String:query[], String:error[], maxlength);
native DBStatement SQL_PrepareQuery(Handle database, const char[] query, char[] error, int maxlength);
/**
* Advances to the next set of results.
@ -393,7 +703,7 @@ native Handle:SQL_PrepareQuery(Handle:database, const String:query[], String:err
* @return True if there was another result set, false otherwise.
* @error Invalid query Handle.
*/
native bool:SQL_FetchMoreResults(Handle:query);
native bool SQL_FetchMoreResults(Handle query);
/**
* Returns whether or not a result set exists. This will
@ -404,7 +714,7 @@ native bool:SQL_FetchMoreResults(Handle:query);
* @return True if there is a result set, false otherwise.
* @error Invalid query Handle.
*/
native bool:SQL_HasResultSet(Handle:query);
native bool SQL_HasResultSet(Handle query);
/**
* Retrieves the number of rows in the last result set.
@ -413,7 +723,7 @@ native bool:SQL_HasResultSet(Handle:query);
* @return Number of rows in the current result set.
* @error Invalid query Handle.
*/
native SQL_GetRowCount(Handle:query);
native int SQL_GetRowCount(Handle query);
/**
* Retrieves the number of fields in the last result set.
@ -422,7 +732,7 @@ native SQL_GetRowCount(Handle:query);
* @return Number of fields in the current result set.
* @error Invalid query Handle.
*/
native SQL_GetFieldCount(Handle:query);
native int SQL_GetFieldCount(Handle query);
/**
* Retrieves the name of a field by index.
@ -431,11 +741,10 @@ native SQL_GetFieldCount(Handle:query);
* @param field Field number (starting from 0).
* @param name Name buffer.
* @param maxlength Maximum length of the name buffer.
* @noreturn
* @error Invalid query Handle, invalid field index, or
* no current result set.
*/
native SQL_FieldNumToName(Handle:query, field, String:name[], maxlength);
native void SQL_FieldNumToName(Handle query, int field, String:name[], int maxlength);
/**
* Retrieves a field index by name.
@ -446,7 +755,7 @@ native SQL_FieldNumToName(Handle:query, field, String:name[], maxlength);
* @return True if found, false if not found.
* @error Invalid query Handle or no current result set.
*/
native bool:SQL_FieldNameToNum(Handle:query, const String:name[], &field);
native bool SQL_FieldNameToNum(Handle query, const char[] name, &field);
/**
* Fetches a row from the current result set. This must be
@ -459,7 +768,7 @@ native bool:SQL_FieldNameToNum(Handle:query, const String:name[], &field);
* @return True if a row was fetched, false otherwise.
* @error Invalid query Handle.
*/
native bool:SQL_FetchRow(Handle:query);
native bool SQL_FetchRow(Handle query);
/**
* Returns if there are more rows.
@ -468,7 +777,7 @@ native bool:SQL_FetchRow(Handle:query);
* @return True if there are more rows, false otherwise.
* @error Invalid query Handle.
*/
native bool:SQL_MoreRows(Handle:query);
native bool SQL_MoreRows(Handle query);
/**
* Rewinds a result set back to the first result.
@ -477,7 +786,7 @@ native bool:SQL_MoreRows(Handle:query);
* @return True on success, false otherwise.
* @error Invalid query Handle or no current result set.
*/
native bool:SQL_Rewind(Handle:query);
native bool SQL_Rewind(Handle query);
/**
* Fetches a string from a field in the current row of a result set.
@ -494,7 +803,7 @@ native bool:SQL_Rewind(Handle:query);
* type conversion requested from the database,
* or no current result set.
*/
native SQL_FetchString(Handle:query, field, String:buffer[], maxlength, &DBResult:result=DBVal_Error);
native int SQL_FetchString(Handle query, int field, char[] buffer, int maxlength, DBResult &result=DBVal_Error);
/**
* Fetches a float from a field in the current row of a result set.
@ -509,7 +818,7 @@ native SQL_FetchString(Handle:query, field, String:buffer[], maxlength, &DBResul
* type conversion requested from the database,
* or no current result set.
*/
native Float:SQL_FetchFloat(Handle:query, field, &DBResult:result=DBVal_Error);
native float SQL_FetchFloat(Handle query, int field, DBResult &result=DBVal_Error);
/**
* Fetches an integer from a field in the current row of a result set.
@ -524,7 +833,7 @@ native Float:SQL_FetchFloat(Handle:query, field, &DBResult:result=DBVal_Error);
* type conversion requested from the database,
* or no current result set.
*/
native SQL_FetchInt(Handle:query, field, &DBResult:result=DBVal_Error);
native int SQL_FetchInt(Handle query, int field, DBResult &result=DBVal_Error);
/**
* Returns whether a field's data in the current row of a result set is
@ -536,7 +845,7 @@ native SQL_FetchInt(Handle:query, field, &DBResult:result=DBVal_Error);
* @error Invalid query Handle or field index, or no
* current result set.
*/
native bool:SQL_IsFieldNull(Handle:query, field);
native bool SQL_IsFieldNull(Handle query, int field);
/**
* Returns the length of a field's data in the current row of a result
@ -550,7 +859,7 @@ native bool:SQL_IsFieldNull(Handle:query, field);
* @error Invalid query Handle or field index or no
* current result set.
*/
native SQL_FetchSize(Handle:query, field);
native int SQL_FetchSize(Handle query, int field);
/**
* Binds a parameter in a prepared statement to a given integer value.
@ -560,11 +869,10 @@ native SQL_FetchSize(Handle:query, field);
* @param number The number to bind.
* @param signed True to bind the number as signed, false to
* bind it as unsigned.
* @noreturn
* @error Invalid statement Handle or parameter index, or
* SQL error.
*/
native SQL_BindParamInt(Handle:statement, param, number, bool:signed=true);
native void SQL_BindParamInt(Handle statement, int param, int number, bool signed=true);
/**
* Binds a parameter in a prepared statement to a given float value.
@ -572,11 +880,10 @@ native SQL_BindParamInt(Handle:statement, param, number, bool:signed=true);
* @param statement A statement (prepared query) Handle.
* @param param The parameter index (starting from 0).
* @param value The float number to bind.
* @noreturn
* @error Invalid statement Handle or parameter index, or
* SQL error.
*/
native SQL_BindParamFloat(Handle:statement, param, Float:value);
native void SQL_BindParamFloat(Handle statement, int param, float value);
/**
* Binds a parameter in a prepared statement to a given string value.
@ -588,11 +895,10 @@ native SQL_BindParamFloat(Handle:statement, param, Float:value);
* locally if necessary. If the string contents
* won't change before calling SQL_Execute(), this
* can be set to false for optimization.
* @noreturn
* @error Invalid statement Handle or parameter index, or
* SQL error.
*/
native SQL_BindParamString(Handle:statement, param, const String:value[], bool:copy);
native void SQL_BindParamString(Handle statement, int param, const char[] value, bool copy);
/**
* Executes a prepared statement. All parameters must be bound beforehand.
@ -601,7 +907,7 @@ native SQL_BindParamString(Handle:statement, param, const String:value[], bool:c
* @return True on success, false on failure.
* @error Invalid statement Handle.
*/
native bool:SQL_Execute(Handle:statement);
native bool SQL_Execute(Handle statement);
/**
* Locks a database so threading operations will not interrupt.
@ -618,31 +924,26 @@ native bool:SQL_Execute(Handle:statement);
* threaded operation has concluded.
*
* @param database A database Handle.
* @noreturn
* @error Invalid database Handle.
*/
native SQL_LockDatabase(Handle:database);
native void SQL_LockDatabase(Handle database);
/**
* Unlocks a database so threading operations may continue.
*
* @param database A database Handle.
* @noreturn
* @error Invalid database Handle.
*/
native SQL_UnlockDatabase(Handle:database);
native void SQL_UnlockDatabase(Handle database);
/**
* General callback for threaded SQL stuff.
*
* @param owner Parent object of the Handle (or INVALID_HANDLE if none).
* @param hndl Handle to the child object (or INVALID_HANDLE if none).
* @param error Error string if there was an error. The error could be
* empty even if an error condition exists, so it is important
* to check the actual Handle value instead.
* @param data Data passed in via the original threaded invocation.
* @noreturn
*/
// General callback for threaded SQL stuff.
//
// @param owner Parent object of the Handle (or INVALID_HANDLE if none).
// @param hndl Handle to the child object (or INVALID_HANDLE if none).
// @param error Error string if there was an error. The error could be
// empty even if an error condition exists, so it is important
// to check the actual Handle value instead.
// @param data Data passed in via the original threaded invocation.
typedef SQLTCallback = function void (Handle owner, Handle hndl, const char[] error, any data);
/**
@ -655,7 +956,7 @@ typedef SQLTCallback = function void (Handle owner, Handle hndl, const char[] er
* connection, false otherwise.
* @error Invalid Handle.
*/
native bool:SQL_IsSameConnection(Handle:hndl1, Handle:hndl2);
native bool SQL_IsSameConnection(Handle hndl1, Handle hndl2);
/**
* Connects to a database via a thread. This can be used instead of
@ -669,9 +970,8 @@ native bool:SQL_IsSameConnection(Handle:hndl1, Handle:hndl2);
* If no driver was found, the owner is INVALID_HANDLE.
* @param name Database name.
* @param data Extra data value to pass to the callback.
* @noreturn
*/
native SQL_TConnect(SQLTCallback:callback, const String:name[]="default", any:data=0);
native void SQL_TConnect(SQLTCallback callback, const char[] name="default", any data=0);
/**
* Executes a simple query via a thread. The query Handle is passed through
@ -690,10 +990,9 @@ native SQL_TConnect(SQLTCallback:callback, const String:name[]="default", any:da
* @param query Query string.
* @param data Extra data value to pass to the callback.
* @param prio Priority queue to use.
* @noreturn
* @error Invalid database Handle.
*/
native SQL_TQuery(Handle:database, SQLTCallback:callback, const String:query[], any:data=0, DBPriority:prio=DBPrio_Normal);
native void SQL_TQuery(Handle database, SQLTCallback callback, const char[] query, any data=0, DBPriority prio=DBPrio_Normal);
/**
* Creates a new transaction object. A transaction object is a list of queries
@ -701,32 +1000,7 @@ native SQL_TQuery(Handle:database, SQLTCallback:callback, const String:query[],
*
* @return A transaction handle.
*/
native Transaction:SQL_CreateTransaction();
/**
* Callback for a successful transaction.
*
* @param db Database handle.
* @param data Data value passed to SQL_ExecuteTransaction().
* @param numQueries Number of queries executed in the transaction.
* @param results An array of Query handle results, one for each of numQueries. They are closed automatically.
* @param queryData An array of each data value passed to SQL_AddQuery().
* @noreturn
*/
typedef SQLTxnSuccess = function void (Handle db, any data, int numQueries, Handle[] results, any[] queryData);
/**
* Callback for a failed transaction.
*
* @param db Database handle.
* @param data Data value passed to SQL_ExecuteTransaction().
* @param numQueries Number of queries executed in the transaction.
* @param error Error message.
* @param failIndex Index of the query that failed, or -1 if something else.
* @param queryData An array of each data value passed to SQL_AddQuery().
* @noreturn
*/
typedef SQLTxnFailure = function void (Handle db, any data, int numQueries, const char[] error, int failIndex, any[] queryData);
native Transaction SQL_CreateTransaction();
/**
* Adds a query to a transaction object.
@ -737,13 +1011,7 @@ typedef SQLTxnFailure = function void (Handle db, any data, int numQueries, cons
* @return The index of the query in the transaction's query list.
* @error Invalid transaction handle.
*/
native SQL_AddQuery(Transaction:txn, const String:query[], any:data=0);
methodmap Transaction < Handle
{
public Transaction() = SQL_CreateTransaction;
public AddQuery() = SQL_AddQuery;
};
native int SQL_AddQuery(Transaction txn, const char[] query, any data=0);
/**
* Sends a transaction to the database thread. The transaction handle is
@ -760,9 +1028,9 @@ methodmap Transaction < Handle
* @error An invalid handle.
*/
native SQL_ExecuteTransaction(
Handle:db,
Handle db,
Transaction:txn,
SQLTxnSuccess:onSuccess = INVALID_FUNCTION,
SQLTxnFailure:onError = INVALID_FUNCTION,
any:data=0,
any data=0,
DBPriority:priority=DBPrio_Normal);

View File

@ -64,10 +64,10 @@ public OnPluginStart()
RegServerCmd("sm_update_adm_tables", Command_UpdateTables);
}
Handle:Connect()
Database Connect()
{
decl String:error[255];
new Handle:db;
char error[255];
Database db;
if (SQL_CheckConfig("admins"))
{
@ -157,16 +157,16 @@ CreateSQLite(client, Handle:db)
public Action:Command_CreateTables(args)
{
new client = 0;
new Handle:db = Connect();
int client = 0;
Database db = Connect();
if (db == null)
{
ReplyToCommand(client, "[SM] %t", "Could not connect to database");
return Plugin_Handled;
}
new String:ident[16];
SQL_ReadDriver(db, ident, sizeof(ident));
char ident[16];
db.Driver.GetIdentifier(ident, sizeof(ident));
if (strcmp(ident, "mysql") == 0)
{
@ -184,21 +184,21 @@ public Action:Command_CreateTables(args)
bool:GetUpdateVersion(client, Handle:db, versions[4])
{
decl String:query[256];
new Handle:hQuery;
char query[256];
DBResultSet rs;
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");
return false;
}
if (SQL_FetchRow(hQuery))
if (rs.FetchRow())
{
decl String:version_string[255];
SQL_FetchString(hQuery, 0, version_string, sizeof(version_string));
char version_string[255];
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)
{
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])
{
@ -226,21 +226,21 @@ bool:GetUpdateVersion(client, Handle:db, versions[4])
return true;
}
UpdateSQLite(client, Handle:db)
UpdateSQLite(client, Database db)
{
decl String:query[512];
new Handle:hQuery;
char query[512];
DBResultSet rs;
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");
return;
}
new bool:found = SQL_FetchRow(hQuery);
bool found = rs.FetchRow();
delete hQuery;
delete rs;
new versions[4];
if (found)
@ -256,7 +256,7 @@ UpdateSQLite(client, Handle:db)
*/
if (versions[3] < SCHEMA_UPGRADE_1)
{
new String:queries[8][] =
char queries[8][] =
{
"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)",
@ -292,29 +292,29 @@ UpdateSQLite(client, Handle:db)
ReplyToCommand(client, "[SM] Your tables are now up to date.");
}
UpdateMySQL(client, Handle:db)
UpdateMySQL(client, Database db)
{
decl String:query[512];
new Handle:hQuery;
char query[512];
DBResultSet rs;
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");
return;
}
decl String:table[64];
new bool:found = false;
while (SQL_FetchRow(hQuery))
char table[64];
bool found = false;
while (rs.FetchRow())
{
SQL_FetchString(hQuery, 0, table, sizeof(table));
rs.FetchString(0, table, sizeof(table));
if (strcmp(table, "sm_config") == 0)
{
found = true;
}
}
delete hQuery;
delete rs;
new versions[4];
@ -328,7 +328,7 @@ UpdateMySQL(client, Handle:db)
*/
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))",
"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(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)
{
new client = 0;
new Handle:db = Connect();
Database db = Connect();
if (db == null)
{
ReplyToCommand(client, "[SM] %t", "Could not connect to database");
return Plugin_Handled;
}
new String:ident[16];
SQL_ReadDriver(db, ident, sizeof(ident));
char ident[16];
db.Driver.GetIdentifier(ident, sizeof(ident));
if (strcmp(ident, "mysql") == 0)
{
@ -396,7 +396,7 @@ public Action:Command_SetAdminGroups(client, args)
return Plugin_Handled;
}
decl String:authtype[16];
char authtype[16];
GetCmdArg(1, authtype, sizeof(authtype));
if (!StrEqual(authtype, "steam")
@ -407,42 +407,42 @@ public Action:Command_SetAdminGroups(client, args)
return Plugin_Handled;
}
new Handle:db = Connect();
Database db = Connect();
if (db == null)
{
ReplyToCommand(client, "[SM] %t", "Could not connect to database");
return Plugin_Handled;
}
decl String:identity[65];
decl String:safe_identity[140];
char identity[65];
char safe_identity[140];
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,
sizeof(query),
"SELECT id FROM sm_admins WHERE authtype = '%s' AND identity = '%s'",
authtype,
safe_identity);
new Handle:hQuery;
if ((hQuery = SQL_Query(db, query)) == null)
DBResultSet rs;
if ((rs = SQL_Query(db, query)) == null)
{
return DoError(client, db, query, "Admin lookup query failed");
}
if (!SQL_FetchRow(hQuery))
if (!rs.FetchRow())
{
ReplyToCommand(client, "[SM] %t", "SQL Admin not found");
delete hQuery;
delete rs;
delete db;
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.
@ -455,13 +455,13 @@ public Action:Command_SetAdminGroups(client, args)
if (args < 3)
{
ReplyToCommand(client, "[SM] %t", "SQL Admin groups reset");
delete db;
ReplyToCommand(client, "[SM] %t", "SQL Admin groups reset");
return Plugin_Handled;
}
decl String:error[256];
new Handle:hAddQuery, Handle:hFindQuery;
char error[256];
DBStatement hAddQuery, hFindQuery;
Format(query, sizeof(query), "SELECT id FROM sm_groups WHERE name = ?");
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");
}
decl String:name[80];
new inherit_order = 0;
char name[80];
int inherit_order = 0;
for (new i=3; i<=args; i++)
{
GetCmdArg(i, name, sizeof(name));
SQL_BindParamString(hFindQuery, 0, name, false);
hFindQuery.BindString(0, name, false);
if (!SQL_Execute(hFindQuery) || !SQL_FetchRow(hFindQuery))
{
ReplyToCommand(client, "[SM] %t", "SQL Group X not found", name);
} else {
new gid = SQL_FetchInt(hFindQuery, 0);
SQL_BindParamInt(hAddQuery, 0, gid);
SQL_BindParamInt(hAddQuery, 1, ++inherit_order);
hAddQuery.BindInt(0, gid);
hAddQuery.BindInt(1, ++inherit_order);
if (!SQL_Execute(hAddQuery))
{
ReplyToCommand(client, "[SM] %t", "SQL Group X failed to bind", name);
@ -524,48 +524,47 @@ public Action:Command_DelGroup(client, args)
return Plugin_Handled;
}
new Handle:db = Connect();
Database db = Connect();
if (db == null)
{
ReplyToCommand(client, "[SM] %t", "Could not connect to database");
return Plugin_Handled;
}
new len;
decl String:name[80];
decl String:safe_name[180];
char name[80];
char safe_name[180];
GetCmdArgString(name, sizeof(name));
/* 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] == '"'))
{
name[--len] = '\0';
SQL_EscapeString(db, name[1], safe_name, sizeof(safe_name));
db.Escape(name[1], safe_name, sizeof(safe_name));
} 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);
if ((hQuery = SQL_Query(db, query)) == null)
if ((rs = SQL_Query(db, query)) == null)
{
return DoError(client, db, query, "Group retrieval query failed");
}
if (!SQL_FetchRow(hQuery))
if (!rs.FetchRow())
{
ReplyToCommand(client, "[SM] %t", "SQL Group not found");
delete hQuery;
delete rs;
delete db;
return Plugin_Handled;
}
new id = SQL_FetchInt(hQuery, 0);
int id = rs.FetchInt(0);
delete hQuery;
delete rs;
/* Delete admin inheritance for this group */
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");
delete db;
return Plugin_Handled;
}
@ -622,40 +620,40 @@ public Action:Command_AddGroup(client, args)
}
}
new Handle:db = Connect();
Database db = Connect();
if (db == null)
{
ReplyToCommand(client, "[SM] %t", "Could not connect to database");
return Plugin_Handled;
}
decl String:name[64];
decl String:safe_name[64];
char name[64];
char safe_name[64];
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;
decl String:query[256];
DBResultSet rs;
char query[256];
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");
}
if (SQL_GetRowCount(hQuery) > 0)
if (rs.RowCount > 0)
{
ReplyToCommand(client, "[SM] %t", "SQL Group already exists");
delete hQuery;
delete rs;
delete db;
return Plugin_Handled;
}
delete hQuery;
delete rs;
decl String:flags[30];
decl String:safe_flags[64];
char flags[30];
char safe_flags[64];
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,
sizeof(query),
@ -672,7 +670,6 @@ public Action:Command_AddGroup(client, args)
ReplyToCommand(client, "[SM] %t", "SQL Group added");
delete db;
return Plugin_Handled;
}
@ -685,7 +682,7 @@ public Action:Command_DelAdmin(client, args)
return Plugin_Handled;
}
decl String:authtype[16];
char authtype[16];
GetCmdArg(1, authtype, sizeof(authtype));
if (!StrEqual(authtype, "steam")
@ -696,42 +693,43 @@ public Action:Command_DelAdmin(client, args)
return Plugin_Handled;
}
new Handle:db = Connect();
Database db = Connect();
if (db == null)
{
ReplyToCommand(client, "[SM] %t", "Could not connect to database");
return Plugin_Handled;
}
decl String:identity[65];
decl String:safe_identity[140];
char identity[65];
char safe_identity[140];
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,
sizeof(query),
"SELECT id FROM sm_admins WHERE authtype = '%s' AND identity = '%s'",
authtype,
safe_identity);
new Handle:hQuery;
if ((hQuery = SQL_Query(db, query)) == null)
DBResultSet rs;
if ((rs = SQL_Query(db, query)) == null)
{
delete db;
return DoError(client, db, query, "Admin lookup query failed");
}
if (!SQL_FetchRow(hQuery))
if (!rs.FetchRow())
{
ReplyToCommand(client, "[SM] %t", "SQL Admin not found");
delete hQuery;
delete rs;
delete db;
return Plugin_Handled;
}
new id = SQL_FetchInt(hQuery, 0);
int id = rs.FetchInt(0);
delete hQuery;
delete rs;
/* Delete group bindings */
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;
}
decl String:authtype[16];
char authtype[16];
GetCmdArg(2, authtype, sizeof(authtype));
if (!StrEqual(authtype, "steam")
@ -773,10 +771,10 @@ public Action:Command_AddAdmin(client, args)
return Plugin_Handled;
}
new immunity;
int immunity;
if (args >= 5)
{
new String:arg5[32];
char arg5[32];
GetCmdArg(5, arg5, sizeof(arg5));
if (!StringToIntEx(arg5, immunity))
{
@ -785,59 +783,59 @@ public Action:Command_AddAdmin(client, args)
}
}
decl String:identity[65];
decl String:safe_identity[140];
char identity[65];
char safe_identity[140];
GetCmdArg(3, identity, sizeof(identity));
decl String:query[256];
new Handle:hQuery;
new Handle:db = Connect();
char query[256];
Database db = Connect();
if (db == null)
{
ReplyToCommand(client, "[SM] %t", "Could not connect to database");
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);
if ((hQuery = SQL_Query(db, query)) == null)
if ((rs = SQL_Query(db, query)) == null)
{
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");
delete hQuery;
delete rs;
delete db;
return Plugin_Handled;
}
delete hQuery;
delete rs;
decl String:alias[64];
decl String:safe_alias[140];
char alias[64];
char safe_alias[140];
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];
decl String:safe_flags[64];
char flags[30];
char safe_flags[64];
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];
decl String:safe_password[80];
char password[32];
char safe_password[80];
if (args >= 6)
{
GetCmdArg(6, password, sizeof(password));
SQL_EscapeString(db, password, safe_password, sizeof(safe_password));
db.Escape(password, safe_password, sizeof(safe_password));
} else {
safe_password[0] = '\0';
}
new len = 0;
len += Format(query[len], sizeof(query)-len, "INSERT INTO sm_admins (authtype, identity, password, flags, name, immunity) VALUES");
int len = Format(query, sizeof(query), "INSERT INTO sm_admins (authtype, identity, password, flags, name, immunity) VALUES");
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);
@ -853,7 +851,6 @@ public Action:Command_AddAdmin(client, args)
ReplyToCommand(client, "[SM] %t", "SQL Admin added");
delete db;
return Plugin_Handled;
}