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