From 85e2a966c863fe3f5c05f663259883609159a7a9 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Mon, 16 Sep 2019 22:01:20 +0800 Subject: [PATCH] add dbengine --- server/gameserver/dbengine.cc | 94 +++++++++++++++++++++++++++++++++++ server/gameserver/dbengine.h | 24 +++++++++ 2 files changed, 118 insertions(+) create mode 100644 server/gameserver/dbengine.cc create mode 100644 server/gameserver/dbengine.h diff --git a/server/gameserver/dbengine.cc b/server/gameserver/dbengine.cc new file mode 100644 index 0000000..4b65612 --- /dev/null +++ b/server/gameserver/dbengine.cc @@ -0,0 +1,94 @@ +#include "precompile.h" + +#include +#include +#include +#include + +#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 args) +{ + int ret = imp_->query_->ExecQuery(querystr, args); + return ret; +} + +bool DBEngine::ExecScript(const char* scriptstr, std::initializer_list 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 args) +{ + return imp_->query_->FormatSqlEx(fmt, args); +} diff --git a/server/gameserver/dbengine.h b/server/gameserver/dbengine.h new file mode 100644 index 0000000..6e8b436 --- /dev/null +++ b/server/gameserver/dbengine.h @@ -0,0 +1,24 @@ +#pragma once + +class DBEngineImp; +class DBEngine : public a8::Singleton +{ + private: + DBEngine() {}; + friend class a8::Singleton; + + public: + void Init(); + void UnInit(); + + int ExecQuery(const char* querystr, std::initializer_list args); + bool ExecScript(const char* scriptstr, std::initializer_list args); + bool Eof(); + void Next(); + a8::XValue GetValue(short idx); + std::string GetLastError(); + std::string FormatSqlEx(const char* fmt, std::initializer_list args); + + private: + DBEngineImp* imp_ = nullptr; +};