initial import of skeletal DBI code
--HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40672
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
#include "Database.h"
|
||||
#include "HandleSys.h"
|
||||
#include "sourcemod.h"
|
||||
|
||||
DBManager g_DBMan;
|
||||
|
||||
void DBManager::OnSourceModAllInitialized()
|
||||
{
|
||||
HandleAccess sec;
|
||||
|
||||
g_HandleSys.InitAccessDefaults(NULL, &sec);
|
||||
sec.access[HandleAccess_Delete] |= HANDLE_RESTRICT_IDENTITY;
|
||||
sec.access[HandleAccess_Clone] |= HANDLE_RESTRICT_IDENTITY;
|
||||
|
||||
m_DriverType = g_HandleSys.CreateType("IDriver", this, 0, NULL, &sec, g_pCoreIdent, NULL);
|
||||
m_DatabaseType = g_HandleSys.CreateType("IDatabase", this, 0, NULL, NULL, g_pCoreIdent, NULL);
|
||||
m_QueryType = g_HandleSys.CreateType("IQuery", this, 0, NULL, NULL, g_pCoreIdent, NULL);
|
||||
}
|
||||
|
||||
void DBManager::OnHandleDestroy(HandleType_t type, void *object)
|
||||
{
|
||||
if (type == m_DriverType)
|
||||
{
|
||||
/* Ignore */
|
||||
return;
|
||||
}
|
||||
|
||||
if (g_HandleSys.TypeCheck(type, m_QueryType))
|
||||
{
|
||||
IQuery *pQuery = (IQuery *)object;
|
||||
pQuery->Release();
|
||||
} else if (g_HandleSys.TypeCheck(type, m_DatabaseType)) {
|
||||
IDatabase *pdb = (IDatabase *)object;
|
||||
pdb->Close();
|
||||
}
|
||||
}
|
||||
|
||||
void DBManager::AddDriver(IDBDriver *pDriver, IdentityToken_t *ident)
|
||||
{
|
||||
if (!pDriver)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_drivers.size() >= HANDLESYS_MAX_SUBTYPES)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
db_driver *driver = new db_driver;
|
||||
|
||||
driver->driver = pDriver;
|
||||
driver->ident = ident;
|
||||
|
||||
HandleSecurity sec;
|
||||
|
||||
sec.pIdentity = ident;
|
||||
sec.pOwner = g_pCoreIdent;
|
||||
driver->hDriver = g_HandleSys.CreateHandleEx(m_DriverType, driver, &sec, NULL, NULL);
|
||||
|
||||
driver->htDatabase = g_HandleSys.CreateType(NULL, this, m_DatabaseType, NULL, NULL, g_pCoreIdent, NULL);
|
||||
driver->htQuery = g_HandleSys.CreateType(NULL, this, m_QueryType, NULL, NULL, g_pCoreIdent, NULL);
|
||||
|
||||
m_drivers.push_back(driver);
|
||||
}
|
||||
|
||||
void DBManager::RemoveDriver(IDBDriver *pDriver)
|
||||
{
|
||||
CVector<db_driver *>::iterator iter;
|
||||
for (iter = m_drivers.begin();
|
||||
iter != m_drivers.end();
|
||||
iter++)
|
||||
{
|
||||
if ((*iter)->driver == pDriver)
|
||||
{
|
||||
db_driver *driver = (*iter);
|
||||
HandleSecurity sec;
|
||||
|
||||
sec.pIdentity = driver->ident;
|
||||
sec.pOwner = g_pCoreIdent;
|
||||
g_HandleSys.RemoveType(driver->htQuery, driver->ident);
|
||||
g_HandleSys.RemoveType(driver->htDatabase, driver->ident);
|
||||
g_HandleSys.FreeHandle(driver->hDriver, &sec);
|
||||
|
||||
delete driver;
|
||||
m_drivers.erase(iter);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const char *DBManager::GetInterfaceName()
|
||||
{
|
||||
return SMINTERFACE_DBI_NAME;
|
||||
}
|
||||
|
||||
unsigned int DBManager::GetInterfaceVersion()
|
||||
{
|
||||
return SMINTERFACE_DBI_VERSION;
|
||||
}
|
||||
|
||||
unsigned int DBManager::GetDriverCount()
|
||||
{
|
||||
return (unsigned int)m_drivers.size();
|
||||
}
|
||||
|
||||
IDBDriver *DBManager::GetDriver(unsigned int index, IdentityToken_t **pToken)
|
||||
{
|
||||
if (index >= GetDriverCount())
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (pToken)
|
||||
{
|
||||
*pToken = m_drivers[index]->ident;
|
||||
}
|
||||
|
||||
return m_drivers[index]->driver;
|
||||
}
|
||||
|
||||
const DatabaseInfo *DBManager::FindDatabaseConf(const char *name)
|
||||
{
|
||||
/* :TODO: implement */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool DBManager::Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool persistent)
|
||||
{
|
||||
const DatabaseInfo *pInfo = FindDatabaseConf(name);
|
||||
|
||||
unsigned int count = GetDriverCount();
|
||||
for (unsigned int i=0; i<count; i++)
|
||||
{
|
||||
IDBDriver *driver = GetDriver(i);
|
||||
if (strcmp(driver->GetIdentifier(), pInfo->driver) == 0)
|
||||
{
|
||||
*pdr = driver;
|
||||
if (persistent)
|
||||
{
|
||||
*pdb = driver->PConnect(pInfo);
|
||||
} else {
|
||||
*pdb = driver->Connect(pInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*pdr = NULL;
|
||||
*pdb = NULL;
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#ifndef _INCLUDE_DATABASE_MANAGER_H_
|
||||
#define _INCLUDE_DATABASE_MANAGER_H_
|
||||
|
||||
#include <IDBDriver.h>
|
||||
#include <IHandleSys.h>
|
||||
#include "sm_globals.h"
|
||||
#include <sh_vector.h>
|
||||
|
||||
using namespace SourceHook;
|
||||
|
||||
struct db_driver
|
||||
{
|
||||
IDBDriver *driver;
|
||||
IdentityToken_t *ident;
|
||||
HandleType_t htDatabase;
|
||||
HandleType_t htQuery;
|
||||
Handle_t hDriver;
|
||||
};
|
||||
|
||||
class DBManager :
|
||||
public IDBManager,
|
||||
public SMGlobalClass,
|
||||
public IHandleTypeDispatch
|
||||
{
|
||||
const char *GetInterfaceName();
|
||||
unsigned int GetInterfaceVersion();
|
||||
public: //SMGlobalClass
|
||||
void OnSourceModAllInitialized();
|
||||
public: //IHandleTypeDispatch
|
||||
void OnHandleDestroy(HandleType_t type, void *object);
|
||||
public: //IDBManager
|
||||
void AddDriver(IDBDriver *pDriver, IdentityToken_t *ident);
|
||||
void RemoveDriver(IDBDriver *pDriver);
|
||||
const DatabaseInfo *FindDatabaseConf(const char *name);
|
||||
bool Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool persistent);
|
||||
unsigned int GetDriverCount();
|
||||
IDBDriver *GetDriver(unsigned int index, IdentityToken_t **pToken=NULL);
|
||||
public:
|
||||
db_driver *GetDriverInfo(unsigned int index);
|
||||
private:
|
||||
CVector<db_driver *> m_drivers;
|
||||
HandleType_t m_DriverType;
|
||||
HandleType_t m_DatabaseType;
|
||||
HandleType_t m_QueryType;
|
||||
};
|
||||
|
||||
extern DBManager g_DBMan;
|
||||
|
||||
#endif //_INCLUDE_DATABASE_MANAGER_H_
|
||||
+100
-88
@@ -199,6 +199,10 @@
|
||||
RelativePath="..\CoreConfig.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\Database.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\DebugReporter.cpp"
|
||||
>
|
||||
@@ -297,6 +301,10 @@
|
||||
RelativePath="..\CoreConfig.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\Database.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\DebugReporter.h"
|
||||
>
|
||||
@@ -393,6 +401,10 @@
|
||||
RelativePath="..\..\public\IDataPack.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\public\IDBDriver.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\public\IExtensionSys.h"
|
||||
>
|
||||
@@ -445,6 +457,94 @@
|
||||
RelativePath="..\..\public\IUserMessages.h"
|
||||
>
|
||||
</File>
|
||||
<Filter
|
||||
Name="Natives"
|
||||
>
|
||||
<File
|
||||
RelativePath="..\smn_admin.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_bitbuffer.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_console.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_core.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_datapacks.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_entities.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_events.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_fakenatives.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_filesystem.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_float.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_functions.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_halflife.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_handles.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_keyvalues.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_lang.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_player.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_sorting.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_string.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_textparse.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_timers.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_usermsgs.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Systems"
|
||||
@@ -690,94 +790,6 @@
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Natives"
|
||||
>
|
||||
<File
|
||||
RelativePath="..\smn_admin.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_bitbuffer.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_console.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_core.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_datapacks.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_entities.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_events.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_fakenatives.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_filesystem.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_float.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_functions.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_halflife.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_handles.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_keyvalues.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_lang.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_player.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_sorting.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_string.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_textparse.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_timers.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\smn_usermsgs.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
</Files>
|
||||
<Globals>
|
||||
</Globals>
|
||||
|
||||
Reference in New Issue
Block a user