databases now get separate handles for persistent connections

removed GetHandle from IDatabase
simplified IDatabase::Close

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40881
This commit is contained in:
David Anderson 2007-06-01 21:26:52 +00:00
parent 3ed301e9af
commit a3687d9258
4 changed files with 22 additions and 49 deletions

View File

@ -94,6 +94,13 @@ static cell_t SQL_Connect(IPluginContext *pContext, const cell_t *params)
return BAD_HANDLE; return BAD_HANDLE;
} }
Handle_t hndl = g_DBMan.CreateHandle(DBHandle_Database, db, pContext->GetIdentity());
if (!hndl)
{
db->Close();
return BAD_HANDLE;
}
/* HACK! Add us to the dependency list */ /* HACK! Add us to the dependency list */
CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity()); CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity());
if (pExt) if (pExt)
@ -101,7 +108,7 @@ static cell_t SQL_Connect(IPluginContext *pContext, const cell_t *params)
g_Extensions.BindChildPlugin(pExt, g_PluginSys.FindPluginByContext(pContext->GetContext())); g_Extensions.BindChildPlugin(pExt, g_PluginSys.FindPluginByContext(pContext->GetContext()));
} }
return db->GetHandle(); return hndl;
} }
static cell_t SQL_ConnectEx(IPluginContext *pContext, const cell_t *params) static cell_t SQL_ConnectEx(IPluginContext *pContext, const cell_t *params)
@ -146,6 +153,13 @@ static cell_t SQL_ConnectEx(IPluginContext *pContext, const cell_t *params)
if (db) if (db)
{ {
Handle_t hndl = g_DBMan.CreateHandle(DBHandle_Database, db, pContext->GetIdentity());
if (!hndl)
{
db->Close();
return BAD_HANDLE;
}
/* HACK! Add us to the dependency list */ /* HACK! Add us to the dependency list */
CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity()); CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity());
if (pExt) if (pExt)
@ -153,7 +167,7 @@ static cell_t SQL_ConnectEx(IPluginContext *pContext, const cell_t *params)
g_Extensions.BindChildPlugin(pExt, g_PluginSys.FindPluginByContext(pContext->GetContext())); g_Extensions.BindChildPlugin(pExt, g_PluginSys.FindPluginByContext(pContext->GetContext()));
} }
return db->GetHandle(); return hndl;
} }
return BAD_HANDLE; return BAD_HANDLE;

View File

@ -56,7 +56,7 @@ DBType GetOurType(enum_field_types type)
} }
MyDatabase::MyDatabase(MYSQL *mysql, const DatabaseInfo *info, bool persistent) MyDatabase::MyDatabase(MYSQL *mysql, const DatabaseInfo *info, bool persistent)
: m_mysql(mysql), m_refcount(1), m_handle(BAD_HANDLE), m_bPersistent(persistent) : m_mysql(mysql), m_refcount(1), m_bPersistent(persistent)
{ {
m_Host.assign(info->host); m_Host.assign(info->host);
m_Database.assign(info->database); m_Database.assign(info->database);
@ -83,7 +83,7 @@ void MyDatabase::IncRefCount()
m_refcount++; m_refcount++;
} }
bool MyDatabase::Close(bool fromHndlSys) bool MyDatabase::Close()
{ {
if (m_refcount > 1) if (m_refcount > 1)
{ {
@ -91,48 +91,18 @@ bool MyDatabase::Close(bool fromHndlSys)
return false; return false;
} }
/* If we don't have a Handle and the Handle is
* is from the Handle System, it means we need
* to block a re-entrant call from our own
* FreeHandle().
*/
if (fromHndlSys && (m_handle == BAD_HANDLE))
{
return false;
}
/* Remove us from the search list */ /* Remove us from the search list */
if (m_bPersistent) if (m_bPersistent)
{ {
g_MyDriver.RemoveFromList(this, true); g_MyDriver.RemoveFromList(this, true);
} }
/* If we're not from the Handle system, and
* we have a Handle, we need to free it first.
*/
if (!fromHndlSys && m_handle != BAD_HANDLE)
{
Handle_t hndl = m_handle;
m_handle = BAD_HANDLE;
dbi->ReleaseHandle(hndl, DBHandle_Database, myself->GetIdentity());
}
/* Finally, free our resource(s) */ /* Finally, free our resource(s) */
delete this; delete this;
return true; return true;
} }
Handle_t MyDatabase::GetHandle()
{
if (m_handle == BAD_HANDLE)
{
m_handle = dbi->CreateHandle(DBHandle_Database, this, myself->GetIdentity());
}
return m_handle;
}
const DatabaseInfo &MyDatabase::GetInfo() const DatabaseInfo &MyDatabase::GetInfo()
{ {
return m_Info; return m_Info;

View File

@ -14,7 +14,7 @@ public:
MyDatabase(MYSQL *mysql, const DatabaseInfo *info, bool persistent); MyDatabase(MYSQL *mysql, const DatabaseInfo *info, bool persistent);
~MyDatabase(); ~MyDatabase();
public: //IDatabase public: //IDatabase
bool Close(bool fromHndlSys=false); bool Close();
const char *GetError(int *errorCode=NULL); const char *GetError(int *errorCode=NULL);
bool DoSimpleQuery(const char *query); bool DoSimpleQuery(const char *query);
IQuery *DoQuery(const char *query); IQuery *DoQuery(const char *query);
@ -22,14 +22,12 @@ public: //IDatabase
bool QuoteString(const char *str, char buffer[], size_t maxlen, size_t *newSize); bool QuoteString(const char *str, char buffer[], size_t maxlen, size_t *newSize);
unsigned int GetAffectedRows(); unsigned int GetAffectedRows();
unsigned int GetInsertID(); unsigned int GetInsertID();
Handle_t GetHandle();
public: public:
const DatabaseInfo &GetInfo(); const DatabaseInfo &GetInfo();
void IncRefCount(); void IncRefCount();
private: private:
MYSQL *m_mysql; MYSQL *m_mysql;
unsigned int m_refcount; unsigned int m_refcount;
Handle_t m_handle;
/* ---------- */ /* ---------- */
DatabaseInfo m_Info; DatabaseInfo m_Info;

View File

@ -24,7 +24,7 @@
#include <string.h> #include <string.h>
#define SMINTERFACE_DBI_NAME "IDBI" #define SMINTERFACE_DBI_NAME "IDBI"
#define SMINTERFACE_DBI_VERSION 2 #define SMINTERFACE_DBI_VERSION 3
namespace SourceMod namespace SourceMod
{ {
@ -409,13 +409,11 @@ namespace SourceMod
* *
* It is guaranteed that an IDatabase pointer won't be destroyed until * It is guaranteed that an IDatabase pointer won't be destroyed until
* all open IQuery or IPreparedQuery pointers are closed. * all open IQuery or IPreparedQuery pointers are closed.
* *
* @param Only pass true if being called from an
* IHandleTypeDispatch destructor.
* @return True if object was destroyed, false if * @return True if object was destroyed, false if
* references are remaining. * references are remaining.
*/ */
virtual bool Close(bool fromHndlSys=false) =0; virtual bool Close() =0;
/** /**
* @brief Error code and string returned by the last operation on this * @brief Error code and string returned by the last operation on this
@ -486,13 +484,6 @@ namespace SourceMod
* @return Row insertion ID of the last execute, if any. * @return Row insertion ID of the last execute, if any.
*/ */
virtual unsigned int GetInsertID() =0; virtual unsigned int GetInsertID() =0;
/**
* @brief Returns an IDatabase Handle to this IDatabase.
*
* @return Handle_t of type IDatabase.
*/
virtual Handle_t GetHandle() =0;
}; };
/** /**