add dbengine
This commit is contained in:
parent
53cbd7917a
commit
85e2a966c8
94
server/gameserver/dbengine.cc
Normal file
94
server/gameserver/dbengine.cc
Normal file
@ -0,0 +1,94 @@
|
||||
#include "precompile.h"
|
||||
|
||||
#include <a8/mysql.h>
|
||||
#include <a8/list.h>
|
||||
#include <a8/timer.h>
|
||||
#include <a8/udplog.h>
|
||||
|
||||
#include "dbengine.h"
|
||||
#include "app.h"
|
||||
|
||||
#include "framework/cpp/utils.h"
|
||||
#include "framework/cpp/msgqueue.h"
|
||||
|
||||
class DBEngineImp
|
||||
{
|
||||
public:
|
||||
list_head query_list_;
|
||||
a8::mysql::Query* query_;
|
||||
a8::mysql::Connection* mysql_conn_;
|
||||
|
||||
};
|
||||
|
||||
void DBEngine::Init()
|
||||
{
|
||||
imp_ = new DBEngineImp();
|
||||
INIT_LIST_HEAD(&imp_->query_list_);
|
||||
imp_->mysql_conn_ = new a8::mysql::Connection();
|
||||
imp_->query_ = imp_->mysql_conn_->CreateQuery();
|
||||
assert(imp_->mysql_conn_->Connect(
|
||||
"127.0.0.1",
|
||||
3306,
|
||||
"root",
|
||||
"keji178",
|
||||
"zero_relam"
|
||||
)
|
||||
);
|
||||
f8::InitMysqlConnection(imp_->query_);
|
||||
auto checkdb_func = [] (const a8::XParams& param)
|
||||
{
|
||||
f8::CheckMysqlConnection(
|
||||
DBEngine::Instance()->imp_->mysql_conn_,
|
||||
DBEngine::Instance()->imp_->query_,
|
||||
"127.0.0.1",
|
||||
3306,
|
||||
"root",
|
||||
"keji178",
|
||||
"zero_relam"
|
||||
);
|
||||
};
|
||||
a8::Timer::Instance()->AddRepeatTimer(1000 * 60 * 5, {}, checkdb_func);
|
||||
}
|
||||
|
||||
void DBEngine::UnInit()
|
||||
{
|
||||
delete imp_;
|
||||
imp_ = nullptr;
|
||||
}
|
||||
|
||||
int DBEngine::ExecQuery(const char* querystr, std::initializer_list<a8::XValue> args)
|
||||
{
|
||||
int ret = imp_->query_->ExecQuery(querystr, args);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool DBEngine::ExecScript(const char* scriptstr, std::initializer_list<a8::XValue> args)
|
||||
{
|
||||
bool ret = imp_->query_->ExecScript(scriptstr, args);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool DBEngine::Eof()
|
||||
{
|
||||
return imp_->query_->Eof();
|
||||
}
|
||||
|
||||
void DBEngine::Next()
|
||||
{
|
||||
imp_->query_->Next();
|
||||
}
|
||||
|
||||
a8::XValue DBEngine::GetValue(short idx)
|
||||
{
|
||||
return imp_->query_->GetValue(idx);
|
||||
}
|
||||
|
||||
std::string DBEngine::GetLastError()
|
||||
{
|
||||
return imp_->mysql_conn_->GetError();
|
||||
}
|
||||
|
||||
std::string DBEngine::FormatSqlEx(const char* fmt, std::initializer_list<a8::XValue> args)
|
||||
{
|
||||
return imp_->query_->FormatSqlEx(fmt, args);
|
||||
}
|
24
server/gameserver/dbengine.h
Normal file
24
server/gameserver/dbengine.h
Normal file
@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
class DBEngineImp;
|
||||
class DBEngine : public a8::Singleton<DBEngine>
|
||||
{
|
||||
private:
|
||||
DBEngine() {};
|
||||
friend class a8::Singleton<DBEngine>;
|
||||
|
||||
public:
|
||||
void Init();
|
||||
void UnInit();
|
||||
|
||||
int ExecQuery(const char* querystr, std::initializer_list<a8::XValue> args);
|
||||
bool ExecScript(const char* scriptstr, std::initializer_list<a8::XValue> args);
|
||||
bool Eof();
|
||||
void Next();
|
||||
a8::XValue GetValue(short idx);
|
||||
std::string GetLastError();
|
||||
std::string FormatSqlEx(const char* fmt, std::initializer_list<a8::XValue> args);
|
||||
|
||||
private:
|
||||
DBEngineImp* imp_ = nullptr;
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user