2025-05-19 09:54:33 +08:00
|
|
|
|
#include "MysqlClient.h"
|
|
|
|
|
|
#include "common/Utils.h"
|
|
|
|
|
|
//#include "Spdlogger.h"
|
|
|
|
|
|
#include "Logger.h"
|
|
|
|
|
|
|
2025-07-18 09:08:09 +08:00
|
|
|
|
MysqlClient::MysqlClient(MysqlOption option) : option_(option)
|
2025-05-19 09:54:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
conn();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MysqlClient::~MysqlClient()
|
|
|
|
|
|
{
|
|
|
|
|
|
this->close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int MysqlClient::conn()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (mysql_)
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
mysql_ = mysql_init(nullptr);
|
2025-07-18 09:08:09 +08:00
|
|
|
|
MYSQL* ret = mysql_real_connect(mysql_, option_.host.c_str(), option_.user.c_str(), option_.password.c_str(), option_.dbname.c_str(), option_.port, NULL, 0);
|
2025-05-19 09:54:33 +08:00
|
|
|
|
if (ret == NULL)
|
|
|
|
|
|
{
|
2025-07-18 09:08:09 +08:00
|
|
|
|
std::string err = mysql_error(mysql_);
|
2025-05-19 09:54:33 +08:00
|
|
|
|
//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)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!mysql_)
|
|
|
|
|
|
{
|
|
|
|
|
|
XLOGE() << "Mysql exec error, database is not connected.";
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-08-26 18:36:25 +08:00
|
|
|
|
if (sql.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
XLOGE() << "Mysql exec error, sql is empty.";
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-05-19 09:54:33 +08:00
|
|
|
|
int ret = mysql_query(mysql_, sql.c_str());
|
|
|
|
|
|
if (0 != ret)
|
|
|
|
|
|
{
|
|
|
|
|
|
XLOGE() << "Mysql exec error: " << mysql_error(mysql_) << ", sql=" << sql;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-22 19:06:50 +08:00
|
|
|
|
bool MysqlClient::exec(std::string sql, vector<Fields>& result)
|
2025-05-19 09:54:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
result.clear();
|
2025-08-26 18:36:25 +08:00
|
|
|
|
bool ret = MysqlClient::exec(sql);
|
|
|
|
|
|
if (!ret)
|
2025-05-19 09:54:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MYSQL_RES* res = mysql_store_result(mysql_);
|
|
|
|
|
|
if (!res)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-08-26 18:36:25 +08:00
|
|
|
|
vector<string> fieldNames;
|
2025-05-19 09:54:33 +08:00
|
|
|
|
while (true)
|
|
|
|
|
|
{
|
|
|
|
|
|
MYSQL_FIELD* field = mysql_fetch_field(res);
|
|
|
|
|
|
if (!field)
|
|
|
|
|
|
{
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2025-08-26 18:36:25 +08:00
|
|
|
|
fieldNames.push_back(field->name);
|
2025-05-19 09:54:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
|
{
|
|
|
|
|
|
MYSQL_ROW row = mysql_fetch_row(res);
|
|
|
|
|
|
if (!row)
|
|
|
|
|
|
{
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 18:36:25 +08:00
|
|
|
|
Fields rowData;
|
|
|
|
|
|
for (size_t i = 0; i < fieldNames.size(); ++i)
|
2025-05-19 09:54:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
string field_text = (row[i] == NULL) ? "" : row[i];
|
2025-08-26 18:36:25 +08:00
|
|
|
|
rowData.set(fieldNames[i], field_text);
|
2025-05-19 09:54:33 +08:00
|
|
|
|
}
|
2025-08-26 18:36:25 +08:00
|
|
|
|
result.push_back(rowData);
|
2025-05-19 09:54:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 释放结果集
|
|
|
|
|
|
mysql_free_result(res);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|