databasemanager.cpp

来自「天之炼狱1服务器端源文件游戏服务端不完整」· C++ 代码 · 共 200 行

CPP
200
字号
//////////////////////////////////////////////////////////////////////////////// Filename    : DatabaseManager.cpp// Written By  : elca@ewestsoft.com// Description ://////////////////////////////////////////////////////////////////////////////#include <stdio.h>#include "DatabaseManager.h"#include "Properties.h"#include "Utility.h"#include "Thread.h"#include "Timeval.h"#include "Result.h"#include "Statement.h"#include "Properties.h"#include "Assert.h"#include "DB.h"DatabaseManager::DatabaseManager () 	throw(){	__BEGIN_TRY	m_Mutex.setName("DatabaseManager");	m_pDefaultConnection = NULL;	__END_CATCH}DatabaseManager::~DatabaseManager () 	throw (){	__BEGIN_TRY		// 葛电 Connection 甫 昏力秦具 茄促.	hash_map<int, Connection*>::iterator itr = m_Connections.begin();	for (; itr != m_Connections.end(); itr++)		SAFE_DELETE(itr->second);	// 秦浆甘救俊 乐绰 葛电 pair 甸阑 昏力茄促.	m_Connections.clear();	SAFE_DELETE(m_pDefaultConnection);	__END_CATCH}void DatabaseManager::init ()	throw ( Error ){	__BEGIN_TRY	try 	{		cout << "--------------------------------------------------" << endl;		cout << "            Init DatabaseManager " << endl;		cout << "--------------------------------------------------" << endl;		string host     = g_pConfig->getProperty("DB_HOST");		string db       = DB_DB;		string user     = DB_USER;		string password = DB_PASSWORD;		m_pDefaultConnection = new Connection(host, db, user, password);		Assert(m_pDefaultConnection!=NULL);		Statement * pStmt = NULL;		pStmt = m_pDefaultConnection->createStatement();		Result * pResult = NULL;		// 矫埃 眉农		// 单捞磐 海捞胶客 辑滚埃俊 矫埃瞒捞啊 1矫埃 捞惑捞搁 救夺促.		pResult = pStmt->executeQuery( "SELECT now()" );		if ( pResult->next() )		{			// 0123456789012345678			// 2003-08-25 18:25:11			string sDBTime = pResult->getString( 1 );			tm tDBTime;			cout << sDBTime << endl;			tDBTime.tm_year	= atoi( sDBTime.substr( 0,4).c_str() ) - 1900;			tDBTime.tm_mon	= atoi( sDBTime.substr( 5,2).c_str() );			tDBTime.tm_mday	= atoi( sDBTime.substr( 8,2).c_str() );			tDBTime.tm_hour	= atoi( sDBTime.substr(11,2).c_str() );			tDBTime.tm_min	= atoi( sDBTime.substr(14,2).c_str() );			tDBTime.tm_sec	= atoi( sDBTime.substr(17,2).c_str() );			time_t tSYSTime = time(0);			double dbDiff = difftime( tSYSTime, mktime(&tDBTime) );			if ( (int)dbDiff > 3600 )			{				// 单捞磐 海捞胶客 辑滚埃俊 矫埃瞒捞啊 1矫埃 捞惑捞促.				cout << "======================================================" << endl;				cout << "!!! Time Check Error !!!" << endl;				cout << "!!! Please check DB server and service server time !!!" << endl;				cout << "======================================================" << endl;				throw Error("Time Check Error");			}		}		SAFE_DELETE(pStmt);	} 	catch ( SQLConnectException & sce ) 	{		throw Error( sce.toString() );	}	__END_CATCH}void DatabaseManager::addConnection ( int TID,  Connection * pConnection ) 	throw ( DuplicatedException ){	__BEGIN_TRY	cout << "Adding TID connection BEGIN" << endl;	__ENTER_CRITICAL_SECTION(m_Mutex)	hash_map< int , Connection * >::iterator itr = m_Connections.find( TID );		if ( itr != m_Connections.end() )	{		cout << "duplicated connection info id" << endl;		throw DuplicatedException("duplicated connection info id");	}	m_Connections[ TID ] = pConnection;	__LEAVE_CRITICAL_SECTION(m_Mutex)	cout << "Adding TID connection END" << endl;	__END_CATCH}Connection * DatabaseManager::getConnection ( const string& connName ) 	throw ( NoSuchElementException ){	__BEGIN_TRY			Connection * pTempConnection = NULL;	hash_map<int, Connection*>::iterator itr;	// connName俊 措秦辑 阿扁 促弗 DB Server肺 盒扁窍档废 茄促.	//if(connName == "DIST_DARKEDEN")	//{	//	itr = m_DistConnections.find(Thread::self());	//	if(itr == m_DistConnections.end())	//		pTempConnection = m_pDistConnection;	//	else	//		pTempConnection = itr->second;	//}	//else	//{		itr = m_Connections.find(Thread::self());		if(itr == m_Connections.end())			pTempConnection = m_pDefaultConnection;		else			pTempConnection = itr->second;	//}	//Assert(pTempConnection!=NULL);	return pTempConnection;	__END_CATCH}void DatabaseManager::executeDummyQuery(Connection* pConnection)	throw (Error){	__BEGIN_TRY	Statement* pStmt = NULL;	BEGIN_DB	{		pStmt = pConnection->createStatement();		pStmt->executeQuery("SELECT 1");		SAFE_DELETE(pStmt);	} END_DB(pStmt) {		 SAFE_DELETE(pStmt);	} 	__END_CATCH}//////////////////////////////////////////////////////////////////////////////// global variable definition//////////////////////////////////////////////////////////////////////////////DatabaseManager * g_pDatabaseManager = NULL;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?