mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
上传项目代码
This commit is contained in:
122
src/database/MysqlClient.cpp
Normal file
122
src/database/MysqlClient.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
#include "MysqlClient.h"
|
||||
#include "common/Utils.h"
|
||||
//#include "Spdlogger.h"
|
||||
#include "Logger.h"
|
||||
|
||||
MysqlClient::MysqlClient(MysqlOptions opts) : options_(opts)
|
||||
{
|
||||
conn();
|
||||
}
|
||||
|
||||
MysqlClient::~MysqlClient()
|
||||
{
|
||||
this->close();
|
||||
}
|
||||
|
||||
int MysqlClient::conn()
|
||||
{
|
||||
if (mysql_)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
mysql_ = mysql_init(nullptr);
|
||||
MYSQL* ret = mysql_real_connect(mysql_, options_.host.c_str(), options_.user.c_str(), options_.password.c_str(), options_.dbname.c_str(), options_.port, NULL, 0);
|
||||
if (ret == NULL)
|
||||
{
|
||||
//Spdlogger::info("[mysql] connect failed: {}", mysql_error(mysql_));
|
||||
mysql_ = nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
mysql_query(mysql_, "set names 'utf8';");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool MysqlClient::isConnected()
|
||||
{
|
||||
return (mysql_ != nullptr);
|
||||
}
|
||||
|
||||
void MysqlClient::close()
|
||||
{
|
||||
if (mysql_)
|
||||
{
|
||||
mysql_close(mysql_);
|
||||
mysql_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool MysqlClient::exec(std::string sql)
|
||||
{
|
||||
XLOGD() << "Mysql exec sql=" << sql;
|
||||
if (!mysql_)
|
||||
{
|
||||
XLOGE() << "Mysql exec error, database is not connected.";
|
||||
return false;
|
||||
}
|
||||
int ret = mysql_query(mysql_, sql.c_str());
|
||||
if (0 != ret)
|
||||
{
|
||||
XLOGE() << "Mysql exec error: " << mysql_error(mysql_) << ", sql=" << sql;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MysqlClient::exec(std::string sql, vector<DataFields>& result)
|
||||
{
|
||||
result.clear();
|
||||
if (!mysql_)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
int ret = mysql_query(mysql_, sql.c_str());
|
||||
if (0 != ret)
|
||||
{
|
||||
//Spdlogger::error("[mysql] mysql_query failed!! error ret={:d}, sql={}", ret, sql);
|
||||
XLOGE() << "mysql error: " << sql;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
//Spdlogger::info("[mysql] query success. sql={}", sql);
|
||||
}
|
||||
|
||||
MYSQL_RES* res = mysql_store_result(mysql_);
|
||||
if (!res)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
vector<string> field_names;
|
||||
while (true)
|
||||
{
|
||||
MYSQL_FIELD* field = mysql_fetch_field(res);
|
||||
if (!field)
|
||||
{
|
||||
break;
|
||||
}
|
||||
field_names.push_back(field->name);
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
MYSQL_ROW row = mysql_fetch_row(res);
|
||||
if (!row)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
DataFields row_data;
|
||||
for (size_t i = 0; i < field_names.size(); ++i)
|
||||
{
|
||||
string field_text = (row[i] == NULL) ? "" : row[i];
|
||||
row_data.set(field_names[i], field_text);
|
||||
}
|
||||
result.push_back(row_data);
|
||||
}
|
||||
|
||||
// 释放结果集
|
||||
mysql_free_result(res);
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user