mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-28 03:09:24 +08:00
实现HTTP服务架构
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
#include "Dao.h"
|
||||
#include "common/Utils.h"
|
||||
#include "common/Snowflake.h"
|
||||
#include "common/JsonN.h"
|
||||
#include "app/Application.h"
|
||||
#include "app/AppData.h"
|
||||
|
||||
std::string DAO::sqlPageLimit(int index, int size)
|
||||
{
|
||||
@@ -8,19 +12,19 @@ std::string DAO::sqlPageLimit(int index, int size)
|
||||
return " LIMIT " + std::to_string(startIndex) + "," + std::to_string(size);
|
||||
}
|
||||
|
||||
bool DAO::count(DaoEntity& dao, std::string tableName, std::string condition, int& count)
|
||||
Errcode DAO::count(DaoEntity& dao, std::string tableName, std::string condition, int& count)
|
||||
{
|
||||
std::string sql = "SELECT COUNT(*) count FROM " + tableName;
|
||||
if (!condition.empty()) { sql += " WHERE " + condition; };
|
||||
sql += ";";
|
||||
|
||||
std::vector<Fields> result;
|
||||
bool ret = dao.exec(sql, result);
|
||||
if (ret)
|
||||
int ret = dao.exec(sql, result);
|
||||
if (ret == 0)
|
||||
{
|
||||
count = (result.size() > 0) ? result[0].get<int>("count") : 0;
|
||||
}
|
||||
return ret;
|
||||
return Errcode(ret);
|
||||
}
|
||||
|
||||
Errcode DAO::exec(std::shared_ptr<DaoEntity> dao, std::string sql)
|
||||
@@ -31,7 +35,7 @@ Errcode DAO::exec(std::shared_ptr<DaoEntity> dao, std::string sql)
|
||||
return Errcode::ERR_DB_CONN;
|
||||
}
|
||||
auto ret = dao->exec(sql);
|
||||
return ret ? Errcode::OK : Errcode::ERR_DB_SQL;
|
||||
return Errcode(ret);
|
||||
}
|
||||
|
||||
Errcode DAO::exec(std::shared_ptr<DaoEntity> dao, std::string sql, vector<Fields>& result)
|
||||
@@ -42,68 +46,109 @@ Errcode DAO::exec(std::shared_ptr<DaoEntity> dao, std::string sql, vector<Fields
|
||||
return Errcode::ERR_DB_CONN;
|
||||
}
|
||||
auto ret = dao->exec(sql, result);
|
||||
return ret ? Errcode::OK : Errcode::ERR_DB_SQL;
|
||||
return Errcode(ret);
|
||||
}
|
||||
|
||||
static bool QueryCount(DaoEntity& dao, std::string sqlFrom, int& count)
|
||||
Errcode DAO::query(std::shared_ptr<DaoEntity> dao, std::string tableName, std::vector<std::string> keys, std::string condition)
|
||||
{
|
||||
std::string sql;
|
||||
return DAO::exec(dao, sql);
|
||||
}
|
||||
Errcode DAO::queryPagination(std::shared_ptr<DaoEntity> dao, std::string tableName, std::vector<std::string> keys, std::string condition)
|
||||
{
|
||||
std::string sql;
|
||||
return DAO::exec(dao, sql);
|
||||
}
|
||||
Errcode DAO::insert(std::shared_ptr<DaoEntity> dao, std::string tableName, Fields params)
|
||||
{
|
||||
return DAO::exec(dao, params.toSqlInsert(tableName));
|
||||
}
|
||||
Errcode DAO::update(std::shared_ptr<DaoEntity> dao, std::string tableName, Fields params, std::string primaryKey)
|
||||
{
|
||||
std::string primaryVal = params.remove(primaryKey);
|
||||
if (primaryVal.empty())
|
||||
{
|
||||
XLOGE() << "DAO update [" + tableName + "] failed, " << primaryKey << "=NULL.";
|
||||
return Errcode::ERR_PARAM;
|
||||
}
|
||||
if (params.size() == 0)
|
||||
{
|
||||
XLOGE() << "DAO update [" + tableName + "] failed, params size=0.";
|
||||
return Errcode::ERR_PARAM_NUL;
|
||||
}
|
||||
std::string condition = "WHERE " + primaryKey + "='" + primaryVal + "'";
|
||||
string sql = params.toSqlUpdate(tableName, condition);
|
||||
return DAO::exec(dao, sql);
|
||||
}
|
||||
Errcode DAO::remove(std::shared_ptr<DaoEntity> dao, std::string tableName, std::string primaryKey, std::string val)
|
||||
{
|
||||
std::string sql = "DELETE from `" + tableName + "` WHERE " + primaryKey + "='" + val + "';";
|
||||
return DAO::exec(dao, sql);
|
||||
}
|
||||
static Errcode QueryCount(DaoEntity& dao, std::string sqlFrom, int& count)
|
||||
{
|
||||
std::vector<Fields> result;
|
||||
bool ret = dao.exec("SELECT COUNT(*) count " + sqlFrom, result);
|
||||
if (ret)
|
||||
int ret = dao.exec("SELECT COUNT(*) count " + sqlFrom, result);
|
||||
if (ret != 0)
|
||||
{
|
||||
count = (result.size() > 0) ? result[0].get<int>("count") : 0;
|
||||
}
|
||||
return ret;
|
||||
return Errcode(ret);
|
||||
}
|
||||
|
||||
|
||||
static bool QueryPagination(std::string sqlFields, std::string sqlCondition, PageInfo& page, vector<Fields>& result)
|
||||
static Errcode QueryPagination(std::string sqlFields, std::string sqlCondition, PageInfo& page, vector<Fields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
|
||||
int count {0};
|
||||
if (!QueryCount(dao, sqlCondition, count))
|
||||
if (!dao.isConnected())
|
||||
{
|
||||
return false;
|
||||
return Errcode::ERR_DB_CONN;
|
||||
}
|
||||
int count {0};
|
||||
Errcode err = QueryCount(dao, sqlCondition, count);
|
||||
if (err != Errcode::OK)
|
||||
{
|
||||
return err;
|
||||
}
|
||||
|
||||
page.total = count;
|
||||
std::string sql = "SELECT " + sqlFields + " " + sqlCondition + DAO::sqlPageLimit(page.index, page.size);
|
||||
bool ret = dao.exec(sql, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: sql=" << sql;
|
||||
}
|
||||
return ret;
|
||||
int ret = dao.exec(sql, result);
|
||||
return Errcode(ret);
|
||||
}
|
||||
|
||||
// 新增用户信息
|
||||
Errcode DAO::insertUser(Fields& params)
|
||||
{
|
||||
std::string createTime = Utils::timeStr();
|
||||
auto dao = DaoEntity::create(DMUser::TABLENAME);
|
||||
std::string account = params.value(DMUser::ACCOUNT);
|
||||
|
||||
std::string userRoleId = params.remove(DMRole::ROLE_ID);
|
||||
|
||||
|
||||
// step1: 查询
|
||||
std::vector<Fields> result;
|
||||
bool ret = dao->exec("SELECT * from user WHERE account='" + account + "';", result);
|
||||
if (!ret)
|
||||
if (account.empty())
|
||||
{
|
||||
return Errcode::ERR_DB_CONN;
|
||||
return Errcode::ERR_PARAM;
|
||||
}
|
||||
|
||||
// step1: 查询检查登录名是否已经存在
|
||||
std::vector<Fields> result;
|
||||
auto dao = DaoEntity::create(DMUser::TABLENAME);
|
||||
int ret = dao->exec("SELECT * from user WHERE account='" + account + "';", result);
|
||||
if (ret != 0)
|
||||
{
|
||||
return Errcode(ret);
|
||||
}
|
||||
if (result.size() > 0)
|
||||
{
|
||||
return Errcode::ERR_DB_DUPLICATE;
|
||||
}
|
||||
|
||||
std::string createTime = Utils::timeStr();
|
||||
params.set(DMUser::USER_ID, Snowflake::instance().getIdStr());
|
||||
params.set(DMUser::CREATETIME, createTime);
|
||||
params.set(DMUser::PASSWD, "123456");
|
||||
ret = dao->insertFields(params);
|
||||
if (!ret)
|
||||
if (ret != 0)
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
return Errcode(ret);
|
||||
}
|
||||
|
||||
std::string userRoleId = params.remove(DMRole::ROLE_ID);
|
||||
if (!userRoleId.empty())
|
||||
{
|
||||
Fields paramsUserRole;
|
||||
@@ -115,20 +160,79 @@ Errcode DAO::insertUser(Fields& params)
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
Errcode DAO::login(std::shared_ptr<DaoEntity> dao, std::string account, std::string passwd, Fields& fields)
|
||||
{
|
||||
if (!dao) { dao = std::make_shared<DaoEntity>(""); }
|
||||
if (!dao->isConnected())
|
||||
{
|
||||
|
||||
//DAO1::writeSystemLog(dao, 2, "", account, "用户登录失败:" + err);
|
||||
return Errcode::ERR_DB_CONN;
|
||||
}
|
||||
std::string t = Utils::timeStr();
|
||||
|
||||
std::string sql = "SELECT u.*, ur.role_id FROM `user` u"
|
||||
" LEFT JOIN user_role ur ON u.user_id = ur.user_id WHERE u.account='" + account + "';";
|
||||
std::vector<Fields> result;
|
||||
int ret = dao->exec(sql, result);
|
||||
if (ret != 0)
|
||||
{
|
||||
//DAO1::writeSystemLog(dao, 2, "", account, "用户登录失败:" + err);
|
||||
return Errcode(ret);
|
||||
}
|
||||
if (result.size() <=0)
|
||||
{
|
||||
//DAO1::writeSystemLog(dao, 2, "", account, "用户登录失败:" + err);
|
||||
return Errcode::ERR_LOGIN_USER_NOTEXIST;
|
||||
}
|
||||
fields = result[0];
|
||||
std::string userId = fields.value("user_id");
|
||||
int loginCount = fields.get<int>("login_count");
|
||||
|
||||
// 判断密码
|
||||
if (passwd != fields.remove("passwd"))
|
||||
{
|
||||
//DAO1::writeSystemLog(dao, 2, userId, account, "用户登录失败:" + err);
|
||||
return Errcode::ERR_LOGIN_PASSWD;
|
||||
}
|
||||
|
||||
// 数据库更新用户登录信息
|
||||
//sql = "UPDATE user SET login_time='" + t + "', login_count='" + std::to_string(loginCount + 1) + "' WHERE user_id = '" + userId + "'; ";
|
||||
//ret = dao->exec(sql);
|
||||
//if (ret != 0)
|
||||
//{
|
||||
// XLOGE() << "更新用户登录信息失败:sql=" << sql;
|
||||
//}
|
||||
|
||||
//DAO1::writeSystemLog(dao, 2, userId, account, "用户登录成功");
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
Errcode DAO::queryRolePermission(std::shared_ptr<DaoEntity> dao, int roleId, vector<Fields>& result)
|
||||
{
|
||||
std::string sql = "SELECT rp.role_id, rp.permission_id, p.name FROM role_permission rp"
|
||||
" LEFT JOIN permission p ON p.permission_id = rp.permission_id"
|
||||
" WHERE rp.is_open='1' AND rp.role_id ='" + std::to_string(roleId) + "';";
|
||||
return DAO::exec(dao, sql, result);
|
||||
}
|
||||
|
||||
Errcode DAO::queryRolePermission(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
|
||||
{
|
||||
// 查询 role 的 permission
|
||||
std::string sql = "SELECT rp.role_id, rp.permission_id, rp.is_open, r.name role_name, p.name permission_name FROM role_permission rp"
|
||||
" LEFT JOIN `role` r ON r.role_id = rp.role_id"
|
||||
" LEFT JOIN permission p ON p.permission_id = rp.permission_id"
|
||||
" WHERE rp.is_open='1';";
|
||||
return DAO::exec(dao, sql, result);
|
||||
}
|
||||
|
||||
// 分页查询用户信息列表
|
||||
bool DAO::queryUserList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
Errcode DAO::queryUserList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
{
|
||||
std::string sqlFields = "u.*, r.role_id , r.name role_name";
|
||||
std::string sqlFields = "u.user_id, u.account, u.name, u.phone, u.age, u.email, u.gender, u.is_open, u.create_time, u.update_time, r.role_id , r.name role_name";
|
||||
std::string sqlCondition = "FROM USER u LEFT JOIN user_role ur ON ur.user_id = u.user_id LEFT JOIN ROLE r ON r.role_id =ur.role_id";
|
||||
|
||||
DaoEntity dao("");
|
||||
bool ret = QueryPagination(sqlFields, sqlCondition, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: queryUserList failed.";
|
||||
}
|
||||
return ret;
|
||||
return QueryPagination(sqlFields, sqlCondition, pageInfo, result);
|
||||
}
|
||||
|
||||
Errcode DAO::updateUserById(Fields& params)
|
||||
@@ -141,10 +245,10 @@ Errcode DAO::updateUserById(Fields& params)
|
||||
|
||||
if (params.size() > 0)
|
||||
{
|
||||
bool ret = dao->updateFields(params, "WHERE " + DMUser::USER_ID + "='" + userId + "'");
|
||||
if (!ret)
|
||||
int ret = dao->updateFields(params, "WHERE " + DMUser::USER_ID + "='" + userId + "'");
|
||||
if (ret != 0)
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
return Errcode(ret);
|
||||
}
|
||||
}
|
||||
if (!roleId.empty())
|
||||
@@ -154,43 +258,137 @@ Errcode DAO::updateUserById(Fields& params)
|
||||
paramsUserRole.set(DMUserRole::USER_ID, userId);
|
||||
paramsUserRole.set(DMUserRole::ROLE_ID, roleId);
|
||||
paramsUserRole.set(DMUserRole::UPDATETIME, createTime);
|
||||
bool ret = dao->duplicateUpdate(paramsUserRole, {DMUserRole::ROLE_ID});
|
||||
if (!ret)
|
||||
int ret = dao->duplicateUpdate(paramsUserRole, {DMUserRole::ROLE_ID});
|
||||
if (ret != 0)
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
return Errcode(ret);
|
||||
}
|
||||
}
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
Errcode DAO::deleteUserById(std::string userId)
|
||||
{
|
||||
std::string sql = "DELETE from user WHERE user_id='" + userId + "';";
|
||||
return DAO::exec(NULL, sql);
|
||||
}
|
||||
|
||||
Errcode DAO::queryPermissionList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
{
|
||||
std::string sqlFrom = "FROM " + DMPermission::TABLENAME;
|
||||
return QueryPagination("*", sqlFrom, pageInfo, result);
|
||||
}
|
||||
|
||||
Errcode DAO::insertPermission(Fields& params)
|
||||
{
|
||||
return DAO::exec(NULL, params.toSqlInsert(DMPermission::TABLENAME));
|
||||
}
|
||||
|
||||
Errcode DAO::updatePermissionById(Fields& params)
|
||||
{
|
||||
std::string primaryKey = DMPermission::PERMISSION_ID;
|
||||
std::string primaryVal = params.remove(primaryKey);
|
||||
if (primaryVal.empty())
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
}
|
||||
std::string condition = "WHERE " + primaryKey + "='" + primaryVal + "'";
|
||||
string sql = params.toSqlUpdate(DMPermission::TABLENAME, condition);
|
||||
return DAO::exec(NULL, sql);
|
||||
}
|
||||
|
||||
Errcode DAO::deletePermissionById(std::string permissionId)
|
||||
{
|
||||
std::string sql = "DELETE from permission WHERE permission_id='" + permissionId + "';";
|
||||
return DAO::exec(NULL, sql);
|
||||
}
|
||||
|
||||
Errcode DAO::queryRoleList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
|
||||
{
|
||||
std::string sql = "SELECT * FROM " + DMRole::TABLENAME + ";";
|
||||
return DAO::exec(dao, sql, result);
|
||||
}
|
||||
|
||||
bool DAO::queryRoleList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
Errcode DAO::queryRoleList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
std::string sqlFrom = "FROM " + DMRole::TABLENAME;
|
||||
bool ret = QueryPagination("*", sqlFrom, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: queryRoleList failed.";
|
||||
}
|
||||
return ret;
|
||||
std::string sqlFields = "";
|
||||
std::string sqlCondition = "FROM " + DMRole::TABLENAME;
|
||||
return QueryPagination("*", sqlCondition, pageInfo, result);
|
||||
}
|
||||
|
||||
bool DAO::queryPermissionList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
Errcode DAO::updateRolePermission(std::shared_ptr<DaoEntity> dao, std::string roleId, std::string permission)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
std::string sqlFrom = "FROM " + DMPermission::TABLENAME;
|
||||
bool ret = QueryPagination("*", sqlFrom, pageInfo, result);
|
||||
if (!ret)
|
||||
NJsonNode jnode;
|
||||
NJson::parse(permission, jnode);
|
||||
std::vector<Fields> vec;
|
||||
for (auto& item: jnode)
|
||||
{
|
||||
XLOGE() << "DAO database error: queryPermissionList failed.";
|
||||
std::string permissionId;
|
||||
if (item.is_number()) { permissionId = std::to_string(item.get<int>()); }
|
||||
if (item.is_string()) { permissionId = item.get<std::string>(); }
|
||||
if (!permissionId.empty())
|
||||
{
|
||||
Fields field;
|
||||
field.set("role_id", roleId);
|
||||
field.set("permission_id", permissionId);
|
||||
vec.emplace_back(field);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
dao->setTableName(DMRolePermission::TABLENAME);
|
||||
std::string sqlDel = "DELETE from " + DMRolePermission::TABLENAME + " WHERE role_id='" + roleId + "';";
|
||||
int ret = dao->exec(sqlDel);
|
||||
if (ret != 0 ){ return Errcode(ret); };
|
||||
ret = dao->insertFields(vec);
|
||||
if (ret != 0) { return Errcode(ret); };
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
// 新增角色
|
||||
Errcode DAO::insertRole(Fields& params)
|
||||
{
|
||||
std::string permission = params.remove("permission");
|
||||
std::string name = params.value("name");
|
||||
auto dao = DaoEntity::create(DMRole::TABLENAME);
|
||||
auto err = DAO::insert(dao, DMRole::TABLENAME, params);
|
||||
|
||||
if (err == Errcode::OK && !permission.empty())
|
||||
{
|
||||
// 查询获取 roleId
|
||||
std::vector<Fields> res;
|
||||
std::string sql = "SELECT * FROM " + DMRole::TABLENAME + " WHERE name='" + name + "';";
|
||||
err = DAO::exec(dao, sql, res);
|
||||
if (err == Errcode::OK && res.size() > 0)
|
||||
{
|
||||
std::string roleId = res[0].value("role_id");
|
||||
err = DAO::updateRolePermission(dao, roleId, permission);
|
||||
}
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
// 更新角色
|
||||
Errcode DAO::updateRoleById(Fields& params)
|
||||
{
|
||||
auto roleId = params.value(DMRole::ROLE_ID);
|
||||
if (roleId.empty())
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
}
|
||||
auto permission = params.remove("permission");
|
||||
|
||||
auto dao = DaoEntity::create(DMRole::TABLENAME);
|
||||
auto err = DAO::update(dao, DMRole::TABLENAME, params, DMRole::ROLE_ID);
|
||||
if (err == Errcode::OK && !permission.empty())
|
||||
{
|
||||
err = DAO::updateRolePermission(dao, roleId, permission);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
// 删除角色
|
||||
Errcode DAO::deleteRoleById(std::string userId)
|
||||
{
|
||||
return DAO::remove(NULL, DMRole::TABLENAME, DMRole::ROLE_ID, userId);
|
||||
}
|
||||
|
||||
Errcode DAO::insertStation(Fields& params)
|
||||
@@ -201,7 +399,7 @@ Errcode DAO::insertStation(Fields& params)
|
||||
params.check(DMStation::LONGITUDE, "", "NULL");
|
||||
|
||||
std::string sql = params.toSqlInsert(DMStation::TABLENAME);
|
||||
return DAO::exec(dao, sql);
|
||||
return DAO::insert(NULL, DMStation::TABLENAME, params);
|
||||
}
|
||||
|
||||
// 查询场站信息列表
|
||||
@@ -212,33 +410,16 @@ Errcode DAO::queryStationList(std::shared_ptr<DaoEntity> dao, vector<Fields>& re
|
||||
}
|
||||
|
||||
// 分页查询场站信息列表
|
||||
bool DAO::queryStationList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
Errcode DAO::queryStationList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
std::string sqlFrom = "FROM " + DMStation::TABLENAME;
|
||||
bool ret = QueryPagination("*", sqlFrom, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: queryStationList failed.";
|
||||
}
|
||||
return ret;
|
||||
return QueryPagination("*", sqlFrom, pageInfo, result);
|
||||
}
|
||||
|
||||
Errcode DAO::updateStationById(Fields& params)
|
||||
{
|
||||
std::string stationId = params.value(DMStation::STATION_ID);
|
||||
if (stationId.empty())
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
}
|
||||
params.remove(DMStation::STATION_ID);
|
||||
auto dao = DaoEntity::create(DMStation::TABLENAME);
|
||||
bool ret = dao->updateFields(params, "WHERE " + DMStation::STATION_ID + "='" + stationId + "'");
|
||||
if (!ret)
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
}
|
||||
return Errcode::OK;
|
||||
std::string primaryKey = DMStation::STATION_ID;
|
||||
return DAO::update(NULL, DMStation::TABLENAME, params, primaryKey);
|
||||
}
|
||||
|
||||
// 查询设备信息列表
|
||||
@@ -249,16 +430,10 @@ Errcode DAO::queryDeviceList(std::shared_ptr<DaoEntity> dao, vector<Fields>& res
|
||||
}
|
||||
|
||||
// 分页查询设备信息列表
|
||||
bool DAO::queryDeviceList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
Errcode DAO::queryDeviceList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
std::string sqlFrom = "FROM " + DMDevice::TABLENAME;
|
||||
bool ret = QueryPagination("*", sqlFrom, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: queryDeviceList failed.";
|
||||
}
|
||||
return ret;
|
||||
return QueryPagination("*", sqlFrom, pageInfo, result);
|
||||
}
|
||||
|
||||
// 查询设备类型定义
|
||||
@@ -275,26 +450,14 @@ Errcode DAO::insertDevice(Fields& params)
|
||||
|
||||
Errcode DAO::updateDeviceById(Fields& params)
|
||||
{
|
||||
std::string deviceId = params.value(DMDevice::DEVICE_ID);
|
||||
if (deviceId.empty())
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
}
|
||||
std::string sql = params.toSqlUpdate(DMDevice::TABLENAME, "WHERE " + DMDevice::DEVICE_ID + "='" + deviceId + "'");
|
||||
return DAO::exec(NULL, sql);
|
||||
return DAO::update(NULL, DMDevice::TABLENAME, params, DMDevice::DEVICE_ID);
|
||||
}
|
||||
|
||||
// 策略管理
|
||||
bool DAO::queryPolicyList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
Errcode DAO::queryPolicyList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
{
|
||||
auto dao = DaoEntity::create("");
|
||||
std::string sqlFrom = "FROM " + DMPolicy::TABLENAME;
|
||||
bool ret = QueryPagination("*", sqlFrom, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: queryPolicyList failed.";
|
||||
}
|
||||
return ret;
|
||||
return QueryPagination("*", sqlFrom, pageInfo, result);
|
||||
}
|
||||
|
||||
Errcode DAO::queryPolicyList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
|
||||
@@ -304,31 +467,77 @@ Errcode DAO::queryPolicyList(std::shared_ptr<DaoEntity> dao, vector<Fields>& res
|
||||
}
|
||||
Errcode DAO::insertPolicy(Fields& params)
|
||||
{
|
||||
return DAO::exec(NULL, params.toSqlInsert(DMPolicy::TABLENAME));
|
||||
return DAO::insert(NULL, DMPolicy::TABLENAME, params);
|
||||
}
|
||||
Errcode DAO::updatePolicyById(Fields& params)
|
||||
{
|
||||
std::string policyId = params.value(DMPolicy::POLICY_ID);
|
||||
if (policyId.empty())
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
}
|
||||
std::string sql = params.toSqlUpdate(DMPolicy::TABLENAME, "WHERE " + DMPolicy::POLICY_ID + "='" + policyId + "'");
|
||||
return DAO::exec(NULL, sql);
|
||||
return DAO::update(NULL, DMPolicy::TABLENAME, params, DMPolicy::POLICY_ID);
|
||||
}
|
||||
Errcode DAO::deletePolicyById(std::string policyId)
|
||||
{
|
||||
return DAO::remove(NULL, DMPolicy::TABLENAME, DMPolicy::POLICY_ID, policyId);
|
||||
}
|
||||
|
||||
// 系统日志管理
|
||||
bool DAO::querySystemLogList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
Errcode DAO::querySystemLogList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
std::string sqlFrom = "FROM " + DMSystemLog::TABLENAME;
|
||||
bool ret = QueryPagination("*", sqlFrom, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: querySystemLogList failed.";
|
||||
}
|
||||
return ret;
|
||||
std::string sqlFrom = "FROM " + DMLogSystem::TABLENAME;
|
||||
return QueryPagination("*", sqlFrom, pageInfo, result);
|
||||
}
|
||||
Errcode DAO::insertSystemLog(Fields& params)
|
||||
{
|
||||
params.set(DMLogAlert::LOG_ID, Snowflake::instance().getIdStr());
|
||||
return DAO::insert(NULL, DMLogSystem::TABLENAME, params);
|
||||
}
|
||||
Errcode DAO::updateSystemLogById(Fields& params)
|
||||
{
|
||||
return DAO::update(NULL, DMLogSystem::TABLENAME, params, DMLogSystem::LOG_ID);
|
||||
}
|
||||
|
||||
Errcode DAO::insertSystemLogSys(std::string content, int status)
|
||||
{
|
||||
Fields fields;
|
||||
fields.set("type", 1);
|
||||
fields.set("content", content);
|
||||
fields.set("status", status);
|
||||
return DAO::insertSystemLog(fields);
|
||||
}
|
||||
Errcode DAO::insertSystemLogUser(std::string token, std::string content, int status)
|
||||
{
|
||||
User user = Application::data().getUser(token);
|
||||
Fields fields;
|
||||
fields.set("type", 2);
|
||||
fields.set("content", content);
|
||||
fields.set("status", status);
|
||||
fields.set("user_id", user.userId);
|
||||
fields.set("user_account", user.account);
|
||||
return DAO::insertSystemLog(fields);
|
||||
}
|
||||
Errcode DAO::insertSystemLogDevice(std::string deviceId, std::string content, int status)
|
||||
{
|
||||
Fields fields;
|
||||
fields.set("type", 3);
|
||||
fields.set("content", content);
|
||||
fields.set("status", status);
|
||||
fields.set("device_id", deviceId);
|
||||
return DAO::insertSystemLog(fields);
|
||||
}
|
||||
|
||||
Errcode DAO::queryAlertLogList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
{
|
||||
std::string sqlFrom = "FROM " + DMLogAlert::TABLENAME;
|
||||
return QueryPagination("*", sqlFrom, pageInfo, result);
|
||||
}
|
||||
Errcode DAO::insertAlertLog(Fields& params)
|
||||
{
|
||||
params.set(DMLogAlert::LOG_ID, Snowflake::instance().getIdStr());
|
||||
return DAO::insert(NULL, DMLogAlert::TABLENAME, params);
|
||||
}
|
||||
Errcode DAO::updateAlertLogById(Fields& params)
|
||||
{
|
||||
return DAO::update(NULL, DMLogAlert::TABLENAME, params, DMLogAlert::LOG_ID);
|
||||
}
|
||||
|
||||
|
||||
Errcode DAO::queryStatDataList(std::shared_ptr<DaoEntity> dao, std::string startDate, std::string endDate, vector<Fields>& result)
|
||||
{
|
||||
@@ -342,7 +551,6 @@ Errcode DAO::queryWorkModeDef(std::shared_ptr<DaoEntity> dao, vector<Fields>& re
|
||||
return DAO::exec(dao, sql, result);
|
||||
}
|
||||
|
||||
|
||||
Errcode DAO::queryPolicyTypeDef(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
|
||||
{
|
||||
std::string sql = "SELECT * FROM " + DMDefPolicyType::TABLENAME + ";";
|
||||
|
||||
@@ -9,38 +9,60 @@ class DAO
|
||||
public:
|
||||
static std::string sqlPageLimit(int index, int size);
|
||||
|
||||
static bool count(DaoEntity& dao, std::string tableName, std::string condition, int& count);
|
||||
static Errcode count(DaoEntity& dao, std::string tableName, std::string condition, int& count);
|
||||
|
||||
static Errcode exec(std::shared_ptr<DaoEntity> dao, std::string sql);
|
||||
static Errcode exec(std::shared_ptr<DaoEntity> dao, std::string sql, vector<Fields>& result);
|
||||
static Errcode query(std::shared_ptr<DaoEntity> dao, std::string tableName, std::vector<std::string> keys, std::string condition);
|
||||
static Errcode queryPagination(std::shared_ptr<DaoEntity> dao, std::string tableName, std::vector<std::string> keys, std::string condition);
|
||||
static Errcode insert(std::shared_ptr<DaoEntity> dao, std::string tableName, Fields params);
|
||||
static Errcode update(std::shared_ptr<DaoEntity> dao, std::string tableName, Fields params, std::string primaryKey);
|
||||
static Errcode remove(std::shared_ptr<DaoEntity> dao, std::string tableName, std::string primaryKey, std::string val);
|
||||
|
||||
static Errcode login(std::shared_ptr<DaoEntity> dao, std::string account, std::string passwd, Fields& fields);
|
||||
|
||||
// 查询用户信息列表(分页)
|
||||
static bool queryUserList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
static Errcode queryRolePermission(std::shared_ptr<DaoEntity> dao, int roleId, vector<Fields>& result);
|
||||
static Errcode queryRolePermission(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
|
||||
// 新增用户信息
|
||||
// 查询用户列表(分页)
|
||||
static Errcode queryUserList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
|
||||
// 新增用户
|
||||
static Errcode insertUser(Fields& params);
|
||||
// 更新用户信息
|
||||
// 更新用户
|
||||
static Errcode updateUserById(Fields& params);
|
||||
|
||||
|
||||
|
||||
// 查询角色信息列表(分页)
|
||||
static bool queryRoleList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
|
||||
// 查询角色信息列表
|
||||
static Errcode queryRoleList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
|
||||
// 删除用户
|
||||
static Errcode deleteUserById(std::string userId);
|
||||
|
||||
|
||||
// 查询权限信息列表(分页)
|
||||
static bool queryPermissionList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
static Errcode queryPermissionList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
// 新增权限
|
||||
static Errcode insertPermission(Fields& params);
|
||||
// 更新权限
|
||||
static Errcode updatePermissionById(Fields& params);
|
||||
// 删除权限
|
||||
static Errcode deletePermissionById(std::string userId);
|
||||
|
||||
|
||||
static Errcode updateRolePermission(std::shared_ptr<DaoEntity> dao, std::string roleId, std::string permission);
|
||||
|
||||
// 查询角色列表(分页)
|
||||
static Errcode queryRoleList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
|
||||
// 查询角色列表
|
||||
static Errcode queryRoleList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
// 新增角色
|
||||
static Errcode insertRole(Fields& params);
|
||||
// 更新角色
|
||||
static Errcode updateRoleById(Fields& params);
|
||||
// 删除角色
|
||||
static Errcode deleteRoleById(std::string userId);
|
||||
|
||||
|
||||
|
||||
// 查询场站信息列表(分页)
|
||||
static bool queryStationList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
static Errcode queryStationList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
|
||||
// 查询场站信息列表
|
||||
static Errcode queryStationList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
@@ -52,7 +74,7 @@ public:
|
||||
|
||||
|
||||
// 查询设备信息列表(分页)
|
||||
static bool queryDeviceList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
static Errcode queryDeviceList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
// 查询设备信息列表
|
||||
static Errcode queryDeviceList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
// 查询设备类型定义
|
||||
@@ -65,17 +87,28 @@ public:
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 策略管理 ===
|
||||
// 分页查询策略信息列表
|
||||
static bool queryPolicyList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
static Errcode queryPolicyList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
|
||||
static Errcode queryPolicyList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
|
||||
static Errcode insertPolicy(Fields& params);
|
||||
static Errcode updatePolicyById(Fields& params);
|
||||
static Errcode deletePolicyById(std::string policyId);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 系统日志管理 ===
|
||||
// 分页查询系统日志列表
|
||||
static bool querySystemLogList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
static Errcode querySystemLogList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
static Errcode insertSystemLog(Fields& params);
|
||||
static Errcode updateSystemLogById(Fields& params);
|
||||
|
||||
static Errcode insertSystemLogSys(std::string content, int status);
|
||||
static Errcode insertSystemLogUser(std::string token, std::string content, int status);
|
||||
static Errcode insertSystemLogDevice(std::string deviceId, std::string content, int status);
|
||||
|
||||
static Errcode queryAlertLogList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
static Errcode insertAlertLog(Fields& params);
|
||||
static Errcode updateAlertLogById(Fields& params);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 统计数据管理 ===
|
||||
|
||||
@@ -66,23 +66,23 @@ bool DaoEntity::isConnected()
|
||||
return db_->isConnected();
|
||||
}
|
||||
|
||||
bool DaoEntity::exec(string sql)
|
||||
int DaoEntity::exec(string sql)
|
||||
{
|
||||
return db_->exec(sql);
|
||||
}
|
||||
|
||||
bool DaoEntity::exec(string sql, vector<Fields>& result)
|
||||
int DaoEntity::exec(string sql, vector<Fields>& result)
|
||||
{
|
||||
return db_->exec(sql, result);
|
||||
}
|
||||
|
||||
bool DaoEntity::insertFields(Fields& fields)
|
||||
int DaoEntity::insertFields(Fields& fields)
|
||||
{
|
||||
string sql = fields.toSqlInsert(tableName_);
|
||||
return this->db_->exec(sql);
|
||||
}
|
||||
|
||||
bool DaoEntity::insertFields(vector<Fields>& vec_fields)
|
||||
int DaoEntity::insertFields(vector<Fields>& vec_fields)
|
||||
{
|
||||
//"insert into TABLE () values ()";
|
||||
string sql = "insert into " + tableName_;
|
||||
@@ -127,7 +127,7 @@ bool DaoEntity::insertFields(vector<Fields>& vec_fields)
|
||||
return this->db_->exec(sql);
|
||||
}
|
||||
|
||||
bool DaoEntity::duplicateUpdate(Fields& fields, const vector<string>& keys)
|
||||
int DaoEntity::duplicateUpdate(Fields& fields, const vector<string>& keys)
|
||||
{
|
||||
//insert into device_attr(device_id, attr_id, attr_val) values('26', 'model', '型号1') on duplicate key update attr_val='型号1';
|
||||
string key;
|
||||
@@ -161,33 +161,34 @@ bool DaoEntity::duplicateUpdate(Fields& fields, const vector<string>& keys)
|
||||
// });
|
||||
//}
|
||||
|
||||
bool DaoEntity::queryFields(string keys, const string& condition, vector<Fields>& result)
|
||||
int DaoEntity::queryFields(string keys, const string& condition, vector<Fields>& result)
|
||||
{
|
||||
ostringstream oss;
|
||||
oss << "SELECT " + keys + " FROM " << tableName_ << (" " + condition) << "; ";
|
||||
return this->db_->exec(oss.str(), result);
|
||||
}
|
||||
|
||||
bool DaoEntity::queryFields(string keys, const string& condition, PageInfo& page, vector<Fields>& result)
|
||||
int DaoEntity::queryFields(string keys, const string& condition, PageInfo& page, vector<Fields>& result)
|
||||
{
|
||||
int err = 0;
|
||||
ostringstream oss;
|
||||
oss << "SELECT count(1) total FROM `" << tableName_ << "` " << condition << ";";
|
||||
|
||||
vector<Fields> res_total;
|
||||
if (!this->db_->exec(oss.str().c_str(), res_total))
|
||||
if (err = this->db_->exec(oss.str().c_str(), res_total))
|
||||
{
|
||||
return false;
|
||||
return err;
|
||||
}
|
||||
|
||||
if (res_total.size() <= 0)
|
||||
{
|
||||
page.total = 0;
|
||||
return true;
|
||||
return err;
|
||||
}
|
||||
page.total = res_total[0].get<int>("total");
|
||||
if (page.total <= 0)
|
||||
{
|
||||
return true;
|
||||
return err;
|
||||
}
|
||||
|
||||
oss.str("");
|
||||
@@ -200,13 +201,13 @@ bool DaoEntity::queryFields(string keys, const string& condition, PageInfo& page
|
||||
return this->db_->exec(oss.str().c_str(), result);
|
||||
}
|
||||
|
||||
bool DaoEntity::updateFields(Fields& fields, const string& condition)
|
||||
int DaoEntity::updateFields(Fields& fields, const string& condition)
|
||||
{
|
||||
string sql = fields.toSqlUpdate(tableName_, condition);
|
||||
return this->db_->exec(sql);
|
||||
}
|
||||
|
||||
bool DaoEntity::updateFields(Fields& fields, vector<string> vecKeys, const string& condition)
|
||||
int DaoEntity::updateFields(Fields& fields, vector<string> vecKeys, const string& condition)
|
||||
{
|
||||
string sql = fields.toSqlUpdate(tableName_, vecKeys, condition);
|
||||
return this->db_->exec(sql);
|
||||
|
||||
@@ -42,39 +42,39 @@ public:
|
||||
/**
|
||||
* 执行sql语句
|
||||
*/
|
||||
bool exec(string sql);
|
||||
int exec(string sql);
|
||||
|
||||
/**
|
||||
* 执行sql语句并返回执行(查询)的结果集
|
||||
*/
|
||||
bool exec(string sql, vector<Fields>& result);
|
||||
int exec(string sql, vector<Fields>& result);
|
||||
|
||||
|
||||
/**
|
||||
* 数据库插入一条数据, 需要先指定数据表名称
|
||||
* @param: fields 写入的数据字段和值
|
||||
*/
|
||||
bool insertFields(Fields& vecFields);
|
||||
int insertFields(Fields& vecFields);
|
||||
|
||||
/**
|
||||
* 数据库插入多条数据, 需要先指定数据表名称
|
||||
* @param: vecFields 写入的数据字段和值的集合
|
||||
*/
|
||||
bool insertFields(vector<Fields>& vecFields);
|
||||
int insertFields(vector<Fields>& vecFields);
|
||||
|
||||
/**
|
||||
* 数据库插入多条数据,UNIQUE索引或PRIMARY KEY重复时执行更新数据, 需要先指定数据表名称
|
||||
* @param: vecFields 写入的数据字段和值的集合
|
||||
* @param: keys 数据重复时需要更新的字段
|
||||
*/
|
||||
bool duplicateUpdate(Fields& vecFields, const vector<string>& keys);
|
||||
int duplicateUpdate(Fields& vecFields, const vector<string>& keys);
|
||||
|
||||
/**
|
||||
* 数据库查询,需要先指定数据表名称
|
||||
* @param: sql_c 查询条件,例:"where id='1'"
|
||||
* @param: result 查询的数据结果集
|
||||
*/
|
||||
bool queryFields(string keys, const string& sql_c, vector<Fields>& result);
|
||||
int queryFields(string keys, const string& sql_c, vector<Fields>& result);
|
||||
|
||||
/**
|
||||
* 数据库查询,需要先指定数据表名称
|
||||
@@ -82,14 +82,14 @@ public:
|
||||
* @param: pageinfo 分页信息
|
||||
* @param: result 查询的数据结果集
|
||||
*/
|
||||
bool queryFields(string keys, const string& sql_c, PageInfo& pageinfo, vector<Fields>& result);
|
||||
int queryFields(string keys, const string& sql_c, PageInfo& pageinfo, vector<Fields>& result);
|
||||
|
||||
/**
|
||||
* 数据库更新,需要先指定数据表名称
|
||||
* @param: fields 要更新的数据字段和值
|
||||
* @param: sql_c 更新条件
|
||||
*/
|
||||
bool updateFields(Fields& fields, const string& sql_c);
|
||||
int updateFields(Fields& fields, const string& sql_c);
|
||||
|
||||
/**
|
||||
* 数据库更新,需要先指定数据表名称
|
||||
@@ -97,7 +97,7 @@ public:
|
||||
* @param: vecKeys 要更新的字段名称
|
||||
* @param: cond 更新条件
|
||||
*/
|
||||
bool updateFields(Fields& fields, vector<string> vecKeys, const string& cond);
|
||||
int updateFields(Fields& fields, vector<string> vecKeys, const string& cond);
|
||||
|
||||
protected:
|
||||
static MysqlOption option;
|
||||
|
||||
@@ -140,9 +140,9 @@ namespace DMPolicy
|
||||
const string UPDATE_TIME = "update_time";
|
||||
}
|
||||
|
||||
namespace DMSystemLog
|
||||
namespace DMLogSystem
|
||||
{
|
||||
const string TABLENAME = "system_log";
|
||||
const string TABLENAME = "log_system";
|
||||
const string LOG_ID = "log_id";
|
||||
const string TYPE = "type";
|
||||
const string USER_ID = "user_id";
|
||||
@@ -153,9 +153,9 @@ namespace DMSystemLog
|
||||
const string UPDATE_TIME = "update_time";
|
||||
}
|
||||
|
||||
namespace DMAlertLog
|
||||
namespace DMLogAlert
|
||||
{
|
||||
const string TABLENAME = "alert_log";
|
||||
const string TABLENAME = "log_alert";
|
||||
const string LOG_ID = "log_id";
|
||||
const string TYPE = "type";
|
||||
const string DEVICE_ID = "device_id";
|
||||
|
||||
@@ -2,6 +2,13 @@
|
||||
#include "common/Utils.h"
|
||||
//#include "Spdlogger.h"
|
||||
#include "Logger.h"
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
static int64_t GetTimestamp()
|
||||
{
|
||||
return chrono::duration_cast<chrono::seconds>(chrono::system_clock::now().time_since_epoch()).count();
|
||||
}
|
||||
|
||||
MysqlClient::MysqlClient(MysqlOption option) : option(option)
|
||||
{
|
||||
@@ -13,22 +20,35 @@ MysqlClient::~MysqlClient()
|
||||
this->close();
|
||||
}
|
||||
|
||||
static int64_t g_tickErr {0};
|
||||
|
||||
int MysqlClient::conn()
|
||||
{
|
||||
if (mysql_)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (GetTimestamp() - g_tickErr <= 5)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
mysql_ = mysql_init(nullptr);
|
||||
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);
|
||||
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);
|
||||
if (ret == NULL)
|
||||
{
|
||||
std::string err = mysql_error(mysql_);
|
||||
//Spdlogger::info("[mysql] connect failed: {}", mysql_error(mysql_));
|
||||
mysql_ = nullptr;
|
||||
g_tickErr = GetTimestamp();
|
||||
}
|
||||
else
|
||||
{
|
||||
g_tickErr = 0;
|
||||
mysql_query(mysql_, "set names 'utf8';");
|
||||
}
|
||||
return 0;
|
||||
@@ -48,70 +68,80 @@ void MysqlClient::close()
|
||||
}
|
||||
}
|
||||
|
||||
bool MysqlClient::exec(std::string sql)
|
||||
static int MysqlQuery(MYSQL* mysql, const std::string& sql)
|
||||
{
|
||||
if (!mysql_)
|
||||
int err = 0;
|
||||
if (!mysql)
|
||||
{
|
||||
XLOGE() << "Mysql exec error, database is not connected.";
|
||||
return false;
|
||||
return err;
|
||||
}
|
||||
if (sql.empty())
|
||||
{
|
||||
XLOGE() << "Mysql exec error, sql is empty.";
|
||||
return false;
|
||||
return err;
|
||||
}
|
||||
int ret = mysql_query(mysql_, sql.c_str());
|
||||
if (0 != ret)
|
||||
err = mysql_query(mysql, sql.c_str());
|
||||
if (0 != err)
|
||||
{
|
||||
XLOGE() << "Mysql exec error: " << mysql_error(mysql_) << ", sql=" << sql;
|
||||
return false;
|
||||
err = mysql_errno(mysql);
|
||||
XLOGE() << "Mysql exec error: " << err << "," << mysql_error(mysql) << ", sql=" << sql;
|
||||
return err;
|
||||
}
|
||||
return true;
|
||||
return err;
|
||||
}
|
||||
|
||||
bool MysqlClient::exec(std::string sql, vector<Fields>& result)
|
||||
int MysqlClient::exec(std::string sql)
|
||||
{
|
||||
int err = MysqlQuery(mysql_, sql);
|
||||
// 确保读取并释放结果集,否则会产生 [2014,Commands out of sync;] 错误
|
||||
MYSQL_RES* res = mysql_store_result(mysql_);
|
||||
if (res) { mysql_free_result(res); }
|
||||
return err;
|
||||
}
|
||||
|
||||
int MysqlClient::exec(std::string sql, vector<Fields>& result)
|
||||
{
|
||||
result.clear();
|
||||
bool ret = MysqlClient::exec(sql);
|
||||
if (!ret)
|
||||
int err = MysqlQuery(mysql_, sql);
|
||||
if (err != 0)
|
||||
{
|
||||
return false;
|
||||
return err;
|
||||
}
|
||||
|
||||
MYSQL_RES* res = mysql_store_result(mysql_);
|
||||
if (!res)
|
||||
if (res)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
vector<string> fieldNames;
|
||||
while (true)
|
||||
{
|
||||
MYSQL_FIELD* field = mysql_fetch_field(res);
|
||||
if (!field)
|
||||
vector<string> fieldNames;
|
||||
while (true)
|
||||
{
|
||||
break;
|
||||
}
|
||||
fieldNames.push_back(field->name);
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
MYSQL_ROW row = mysql_fetch_row(res);
|
||||
if (!row)
|
||||
{
|
||||
break;
|
||||
MYSQL_FIELD* field = mysql_fetch_field(res);
|
||||
if (!field)
|
||||
{
|
||||
break;
|
||||
}
|
||||
fieldNames.push_back(field->name);
|
||||
}
|
||||
|
||||
Fields rowData;
|
||||
for (size_t i = 0; i < fieldNames.size(); ++i)
|
||||
while (true)
|
||||
{
|
||||
string field_text = (row[i] == NULL) ? "" : row[i];
|
||||
rowData.set(fieldNames[i], field_text);
|
||||
}
|
||||
result.push_back(rowData);
|
||||
}
|
||||
MYSQL_ROW row = mysql_fetch_row(res);
|
||||
if (!row)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// 释放结果集
|
||||
mysql_free_result(res);
|
||||
return true;
|
||||
Fields rowData;
|
||||
for (size_t i = 0; i < fieldNames.size(); ++i)
|
||||
{
|
||||
string field_text = (row[i] == NULL) ? "" : row[i];
|
||||
rowData.set(fieldNames[i], field_text);
|
||||
}
|
||||
result.push_back(rowData);
|
||||
}
|
||||
|
||||
// 释放结果集
|
||||
mysql_free_result(res);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -50,12 +50,12 @@ public:
|
||||
/**
|
||||
* @brief: 执行sql语句
|
||||
*/
|
||||
bool exec(std::string sql);
|
||||
int exec(std::string sql);
|
||||
|
||||
/**
|
||||
* @brief: 执行sql语句, 获取查询结果集
|
||||
*/
|
||||
bool exec(std::string, vector<Fields>& result);
|
||||
int exec(std::string, vector<Fields>& result);
|
||||
|
||||
private:
|
||||
// mysql数据库连接对象
|
||||
|
||||
Reference in New Issue
Block a user