2025-05-19 09:54:33 +08:00
|
|
|
|
#include "DaoEntity.h"
|
|
|
|
|
|
//#include "PvInstance.h"
|
|
|
|
|
|
//#include "spdlogger.h"
|
|
|
|
|
|
|
2025-07-18 09:08:09 +08:00
|
|
|
|
MysqlOption DaoEntity::option_;
|
2025-05-19 09:54:33 +08:00
|
|
|
|
|
|
|
|
|
|
DaoEntity::DaoEntity(string tb_name)
|
|
|
|
|
|
{
|
|
|
|
|
|
//MysqlOptions opts;
|
|
|
|
|
|
//opts.host = "localhost";
|
|
|
|
|
|
//opts.user = "root";
|
|
|
|
|
|
//opts.password = "123456";
|
|
|
|
|
|
//opts.port = 3306;
|
|
|
|
|
|
//opts.dbname = "pvb";
|
2025-07-18 09:08:09 +08:00
|
|
|
|
db_ = make_shared<MysqlClient>(DaoEntity::option_);
|
2025-05-19 09:54:33 +08:00
|
|
|
|
if (!db_->isConnected())
|
|
|
|
|
|
{
|
|
|
|
|
|
//Global::data().status_msg = "数据库连接异常!";
|
|
|
|
|
|
//PvInstance::send_user_event(nullptr, EUserEvent::ALARM_DB, "数据库连接异常!");
|
|
|
|
|
|
}
|
|
|
|
|
|
tableName_ = tb_name;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DaoEntity::~DaoEntity()
|
|
|
|
|
|
{
|
|
|
|
|
|
db_ = nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-18 09:08:09 +08:00
|
|
|
|
MysqlOption& DaoEntity::mysqlOption()
|
2025-05-19 09:54:33 +08:00
|
|
|
|
{
|
2025-07-18 09:08:09 +08:00
|
|
|
|
return DaoEntity::option_;
|
|
|
|
|
|
}
|
|
|
|
|
|
void DaoEntity::setOption(std::string host, int port, std::string user, std::string pwd, std::string dbname)
|
|
|
|
|
|
{
|
|
|
|
|
|
option_.host = host;
|
|
|
|
|
|
option_.port = port;
|
|
|
|
|
|
option_.user = user;
|
|
|
|
|
|
option_.password = pwd;
|
|
|
|
|
|
option_.dbname = dbname;
|
2025-05-19 09:54:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<DaoEntity> DaoEntity::create(string tb_name)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::shared_ptr<DaoEntity> dao = std::make_shared<DaoEntity>(tb_name);
|
|
|
|
|
|
return (dao->isConnected() ? dao : nullptr);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DaoEntity::execOnce(string sql)
|
|
|
|
|
|
{
|
2025-07-18 09:08:09 +08:00
|
|
|
|
auto db = make_shared<MysqlClient>(DaoEntity::option_);
|
2025-05-19 09:54:33 +08:00
|
|
|
|
return db->exec(sql);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-22 19:06:50 +08:00
|
|
|
|
bool DaoEntity::execOnce(string sql, vector<Fields>& result)
|
2025-05-19 09:54:33 +08:00
|
|
|
|
{
|
2025-07-18 09:08:09 +08:00
|
|
|
|
auto db = make_shared<MysqlClient>(DaoEntity::option_);
|
2025-05-19 09:54:33 +08:00
|
|
|
|
return db->exec(sql, result);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DaoEntity::setTableName(string tb_name)
|
|
|
|
|
|
{
|
|
|
|
|
|
tableName_ = tb_name;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DaoEntity::isConnected()
|
|
|
|
|
|
{
|
|
|
|
|
|
return db_->isConnected();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DaoEntity::exec(string sql)
|
|
|
|
|
|
{
|
|
|
|
|
|
return db_->exec(sql);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-22 19:06:50 +08:00
|
|
|
|
bool DaoEntity::exec(string sql, vector<Fields>& result)
|
2025-05-19 09:54:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
return db_->exec(sql, result);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-22 19:06:50 +08:00
|
|
|
|
bool DaoEntity::insertFields(Fields& fields)
|
2025-05-19 09:54:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
string sql = fields.get_insert_sql(tableName_);
|
|
|
|
|
|
return this->db_->exec(sql);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-22 19:06:50 +08:00
|
|
|
|
bool DaoEntity::insertFields(vector<Fields>& vec_fields)
|
2025-05-19 09:54:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
//"insert into TABLE () values ()";
|
|
|
|
|
|
string sql = "insert into " + tableName_;
|
|
|
|
|
|
bool first = true;
|
|
|
|
|
|
string keys;
|
|
|
|
|
|
string values;
|
|
|
|
|
|
|
|
|
|
|
|
for (auto& field : vec_fields)
|
|
|
|
|
|
{
|
|
|
|
|
|
keys = "";
|
|
|
|
|
|
values = "";
|
|
|
|
|
|
for (auto& item : field.fields())
|
|
|
|
|
|
{
|
|
|
|
|
|
const string& k = item.first;
|
|
|
|
|
|
const string& v = item.second;
|
|
|
|
|
|
if (first)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!keys.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
keys += ",";
|
|
|
|
|
|
}
|
|
|
|
|
|
keys += k;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!values.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
values += ",";
|
|
|
|
|
|
}
|
|
|
|
|
|
values += ("'" + v + "'");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (first)
|
|
|
|
|
|
{
|
|
|
|
|
|
sql += (" (" + keys + ")");
|
|
|
|
|
|
sql += (" values (" + values + ")");
|
|
|
|
|
|
first = !first;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
sql += (",(" + values + ")");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
sql += ";";
|
|
|
|
|
|
return this->db_->exec(sql);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-22 19:06:50 +08:00
|
|
|
|
bool DaoEntity::duplicateUpdate(Fields& fields, const vector<string>& keys)
|
2025-05-19 09:54:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
//insert into device_attr(device_id, attr_id, attr_val) values('26', 'model', '型号1') on duplicate key update attr_val='型号1';
|
|
|
|
|
|
string s_key;
|
|
|
|
|
|
string s_val;
|
|
|
|
|
|
for (auto& item : fields.fields())
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!s_key.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
s_key += ","; s_val += ",";
|
|
|
|
|
|
}
|
|
|
|
|
|
s_key += (item.first);
|
|
|
|
|
|
s_val += ("'" + item.second + "'");
|
|
|
|
|
|
}
|
|
|
|
|
|
string s_data;
|
|
|
|
|
|
for (auto& k : keys)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!s_data.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
s_data += ",";
|
|
|
|
|
|
}
|
2025-08-22 19:06:50 +08:00
|
|
|
|
s_data += (k + "='" + fields.value(k) + "'");
|
2025-05-19 09:54:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
string sql = "INSERT INTO " + tableName_ + "(" + s_key + ") VALUES (" + s_val + ") ON duplicate KEY UPDATE " + s_data;
|
|
|
|
|
|
return this->db_->exec(sql);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//void DaoEntity::queryFields(const string& condition, DaoPageinfo& pageinfo, vector<map<string, string>>& result)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// this->query_by_page(condition, pageinfo, [&](map<string, string>& row) mutable {
|
|
|
|
|
|
// result.push_back(row);
|
|
|
|
|
|
// });
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-08-22 19:06:50 +08:00
|
|
|
|
bool DaoEntity::queryFields(string keys, const string& sql_c, vector<Fields>& result)
|
2025-05-19 09:54:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
ostringstream oss;
|
|
|
|
|
|
oss << "SELECT " + keys + " FROM " << tableName_ << (" " + sql_c) << "; ";
|
|
|
|
|
|
return this->db_->exec(oss.str(), result);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-22 19:06:50 +08:00
|
|
|
|
bool DaoEntity::queryFields(string keys, const string& sql_c, PageInfo& page, vector<Fields>& result)
|
2025-05-19 09:54:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
ostringstream oss;
|
|
|
|
|
|
oss << "SELECT count(1) total FROM `" << tableName_ << "` " << sql_c << ";";
|
|
|
|
|
|
|
2025-08-22 19:06:50 +08:00
|
|
|
|
vector<Fields> res_total;
|
2025-05-19 09:54:33 +08:00
|
|
|
|
if (!this->db_->exec(oss.str().c_str(), res_total))
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (res_total.size() <= 0)
|
|
|
|
|
|
{
|
2025-08-22 19:06:50 +08:00
|
|
|
|
page.total = 0;
|
2025-05-19 09:54:33 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2025-08-22 19:06:50 +08:00
|
|
|
|
page.total = res_total[0].getInt("total");
|
|
|
|
|
|
if (page.total <= 0)
|
2025-05-19 09:54:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
oss.str("");
|
2025-08-22 19:06:50 +08:00
|
|
|
|
if (page.index <= 0)
|
2025-05-19 09:54:33 +08:00
|
|
|
|
{
|
2025-08-22 19:06:50 +08:00
|
|
|
|
page.index = 1;
|
2025-05-19 09:54:33 +08:00
|
|
|
|
}
|
2025-08-22 19:06:50 +08:00
|
|
|
|
int start = (page.index - 1) * page.size;
|
|
|
|
|
|
oss << "SELECT " << keys << " FROM `" << tableName_ << "` " << sql_c << " LIMIT " << start << "," << page.size << ";";
|
2025-05-19 09:54:33 +08:00
|
|
|
|
return this->db_->exec(oss.str().c_str(), result);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-22 19:06:50 +08:00
|
|
|
|
bool DaoEntity::updateFields(Fields& fields, const string& sql_c)
|
2025-05-19 09:54:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
string sql = fields.get_update_sql(tableName_, sql_c);
|
2025-08-20 19:00:22 +08:00
|
|
|
|
std::cout << sql;
|
2025-05-19 09:54:33 +08:00
|
|
|
|
if (sql_c.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
//Spdlogger::error("[DB] update condition is empty, not exec, sql={}", sql);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return this->db_->exec(sql.c_str());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-22 19:06:50 +08:00
|
|
|
|
bool DaoEntity::updateFields(Fields& fields, vector<string> vec_keys, const string& sql_c)
|
2025-05-19 09:54:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
string sql = fields.get_update_sql(tableName_, vec_keys, sql_c);
|
|
|
|
|
|
if (sql_c.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
//Spdlogger::error("[DB] update condition is empty, not exec, sql={}", sql);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return this->db_->exec(sql.c_str());
|
|
|
|
|
|
}
|