core/Database.h
author Scott Ehlert <ds@alliedmods.net>
Sat Jul 12 09:05:19 2008 +0000 (2008-07-12)
changeset 2201 d2b8bc01ccd8
parent 2134 f1d8a7cd3c94
child 2289 44045ead61cd
permissions -rw-r--r--
Builds against latest OB SDK now
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#ifndef _INCLUDE_DATABASE_MANAGER_H_
ds@1952
    33
#define _INCLUDE_DATABASE_MANAGER_H_
ds@1952
    34
ds@1952
    35
#include <IDBDriver.h>
ds@1952
    36
#include "sm_globals.h"
ds@1952
    37
#include <sh_vector.h>
ds@1952
    38
#include <sh_string.h>
ds@1952
    39
#include <sh_list.h>
ds@1952
    40
#include <ITextParsers.h>
ds@1952
    41
#include "sm_memtable.h"
ds@1952
    42
#include <IThreader.h>
ds@1952
    43
#include "sm_simple_prioqueue.h"
ds@1952
    44
#include "PluginSys.h"
ds@1952
    45
ds@1952
    46
using namespace SourceHook;
ds@1952
    47
ds@1952
    48
struct ConfDbInfo
ds@1952
    49
{
ds@1952
    50
	ConfDbInfo() : name(-1), driver(-1), host(-1), user(-1), pass(-1), 
ds@1952
    51
		database(-1), realDriver(NULL)
ds@1952
    52
	{
ds@1952
    53
	}
ds@1952
    54
	int name;
ds@1952
    55
	int driver;
ds@1952
    56
	int host;
ds@1952
    57
	int user;
ds@1952
    58
	int pass;
ds@1952
    59
	int database;
ds@1952
    60
	IDBDriver *realDriver;
ds@1952
    61
	DatabaseInfo info;
ds@1952
    62
};
ds@1952
    63
ds@1952
    64
class DBManager : 
ds@1952
    65
	public IDBManager,
ds@1952
    66
	public SMGlobalClass,
ds@1952
    67
	public IHandleTypeDispatch,
ds@1952
    68
	public ITextListener_SMC,
ds@1952
    69
	public IThread,
ds@1952
    70
	public IThreadWorkerCallbacks,
ds@1952
    71
	public IPluginsListener
ds@1952
    72
{
ds@1952
    73
public:
ds@1952
    74
	DBManager();
ds@1952
    75
public:
ds@1952
    76
	const char *GetInterfaceName();
ds@1952
    77
	unsigned int GetInterfaceVersion();
ds@1952
    78
public: //SMGlobalClass
ds@1952
    79
	void OnSourceModAllInitialized();
ds@1952
    80
	void OnSourceModLevelChange(const char *mapName);
ds@1952
    81
	void OnSourceModIdentityDropped(IdentityToken_t *pToken);
ds@1952
    82
	void OnSourceModShutdown();
ds@1952
    83
public: //IHandleTypeDispatch
ds@1952
    84
	void OnHandleDestroy(HandleType_t type, void *object);
ds@1952
    85
public: //IDBManager
ds@1952
    86
	void AddDriver(IDBDriver *pDrivera);
ds@1952
    87
	void RemoveDriver(IDBDriver *pDriver);
ds@1952
    88
	const DatabaseInfo *FindDatabaseConf(const char *name);
ds@1952
    89
	bool Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool persistent, char *error, size_t maxlength);
ds@1952
    90
	unsigned int GetDriverCount();
ds@1952
    91
	IDBDriver *GetDriver(unsigned int index);
ds@1952
    92
	Handle_t CreateHandle(DBHandleType type, void *ptr, IdentityToken_t *pToken);
ds@1952
    93
	HandleError ReadHandle(Handle_t hndl, DBHandleType type, void **ptr);
ds@1952
    94
	HandleError ReleaseHandle(Handle_t hndl, DBHandleType type, IdentityToken_t *token);
dvander@2134
    95
	void AddDependency(IExtension *myself, IDBDriver *driver);
ds@1952
    96
public: //ITextListener_SMC
ds@1952
    97
	void ReadSMC_ParseStart();
ds@1952
    98
	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
ds@1952
    99
	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
ds@1952
   100
	SMCResult ReadSMC_LeavingSection(const SMCStates *states);
ds@1952
   101
	void ReadSMC_ParseEnd(bool halted, bool failed);
ds@1952
   102
public: //IThread
ds@1952
   103
	void RunThread(IThreadHandle *pThread);
ds@1952
   104
	void OnTerminate(IThreadHandle *pThread, bool cancel);
ds@1952
   105
public: //IThreadWorkerCallbacks
ds@1952
   106
	void OnWorkerStart(IThreadWorker *pWorker);
ds@1952
   107
	void OnWorkerStop(IThreadWorker *pWorker);
ds@1952
   108
public: //IPluginsListener
ds@1952
   109
	void OnPluginUnloaded(IPlugin *plugin);
ds@1952
   110
public:
ds@1952
   111
	ConfDbInfo *GetDatabaseConf(const char *name);
ds@1952
   112
	IDBDriver *FindOrLoadDriver(const char *name);
ds@1952
   113
	IDBDriver *GetDefaultDriver();
ds@1952
   114
	const char *GetDefaultDriverName();
ds@1952
   115
	bool AddToThreadQueue(IDBThreadOperation *op, PrioQueueLevel prio);
ds@1952
   116
	void LockConfig();
ds@1952
   117
	void UnlockConfig();
ds@1952
   118
	void RunFrame();
ds@1952
   119
	inline HandleType_t GetDatabaseType()
ds@1952
   120
	{
ds@1952
   121
		return m_DatabaseType;
ds@1952
   122
	}
ds@1952
   123
private:
ds@1952
   124
	void KillWorkerThread();
ds@1952
   125
private:
ds@1952
   126
	CVector<IDBDriver *> m_drivers;
ds@1952
   127
ds@1952
   128
	/* Threading stuff */
ds@1952
   129
	PrioQueue<IDBThreadOperation *> m_OpQueue;
ds@1952
   130
	Queue<IDBThreadOperation *> m_ThinkQueue;
ds@1952
   131
	CVector<bool> m_drSafety;			/* which drivers are safe? */
ds@1952
   132
	IThreadWorker *m_pWorker;			/* Worker thread object */
ds@1952
   133
	IMutex *m_pConfigLock;				/* Configuration lock */
ds@1952
   134
	IMutex *m_pQueueLock;				/* Queue safety lock */
ds@1952
   135
	IMutex *m_pThinkLock;				/* Think-queue lock */
ds@1952
   136
ds@1952
   137
	List<ConfDbInfo> m_confs;
ds@1952
   138
	HandleType_t m_DriverType;
ds@1952
   139
	HandleType_t m_DatabaseType;
ds@1952
   140
	String m_DefDriver;
ds@1952
   141
	BaseStringTable m_StrTab;
ds@1952
   142
	char m_Filename[PLATFORM_MAX_PATH];
ds@1952
   143
	unsigned int m_ParseLevel;
ds@1952
   144
	unsigned int m_ParseState;
ds@1952
   145
	IDBDriver *m_pDefault;
ds@1952
   146
};
ds@1952
   147
ds@1952
   148
extern DBManager g_DBMan;
ds@1952
   149
ds@1952
   150
#endif //_INCLUDE_DATABASE_MANAGER_H_