mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-28 03:09:24 +08:00
实现削峰套利策略的编辑页面
This commit is contained in:
@@ -18,18 +18,40 @@ bool DAO::count(DaoEntity& dao, std::string tableName, std::string condition, in
|
||||
bool ret = dao.exec(sql, result);
|
||||
if (ret)
|
||||
{
|
||||
count = (result.size() > 0) ? result[0].getInt("count") : 0;
|
||||
count = (result.size() > 0) ? result[0].get<int>("count") : 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
Errcode DAO::exec(std::shared_ptr<DaoEntity> dao, std::string sql)
|
||||
{
|
||||
if (!dao) { dao = DaoEntity::create(""); }
|
||||
if (!dao->isConnected())
|
||||
{
|
||||
return Errcode::ERR_DB_CONN;
|
||||
}
|
||||
auto ret = dao->exec(sql);
|
||||
return ret ? Errcode::OK : Errcode::ERR_DB_SQL;
|
||||
}
|
||||
|
||||
Errcode DAO::exec(std::shared_ptr<DaoEntity> dao, std::string sql, vector<Fields>& result)
|
||||
{
|
||||
if (!dao) { dao = DaoEntity::create(""); }
|
||||
if (!dao->isConnected())
|
||||
{
|
||||
return Errcode::ERR_DB_CONN;
|
||||
}
|
||||
auto ret = dao->exec(sql, result);
|
||||
return ret ? Errcode::OK : Errcode::ERR_DB_SQL;
|
||||
}
|
||||
|
||||
static bool QueryCount(DaoEntity& dao, std::string sqlFrom, int& count)
|
||||
{
|
||||
std::vector<Fields> result;
|
||||
bool ret = dao.exec("SELECT COUNT(*) count " + sqlFrom, result);
|
||||
if (ret)
|
||||
{
|
||||
count = (result.size() > 0) ? result[0].getInt("count") : 0;
|
||||
count = (result.size() > 0) ? result[0].get<int>("count") : 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -108,7 +130,7 @@ Errcode DAO::updateUserById(Fields& params)
|
||||
std::string createTime = Utils::timeStr();
|
||||
std::string userId = params.value(DMUser::USER_ID);
|
||||
std::string roleId = "";
|
||||
if (params.hasKey(DMRole::ROLE_ID))
|
||||
if (params.contains(DMRole::ROLE_ID))
|
||||
{
|
||||
roleId = params.value(DMRole::ROLE_ID);
|
||||
params.remove(DMUser::USER_ID);
|
||||
@@ -137,11 +159,10 @@ Errcode DAO::updateUserById(Fields& params)
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
bool DAO::queryRoleList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
|
||||
Errcode DAO::queryRoleList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
|
||||
{
|
||||
if (!dao) { dao = DaoEntity::create(""); }
|
||||
std::string sql = "SELECT * FROM " + DMRole::TABLENAME + ";";
|
||||
return dao->exec(sql, result);
|
||||
return DAO::exec(dao, sql, result);
|
||||
}
|
||||
|
||||
bool DAO::queryRoleList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
@@ -174,20 +195,16 @@ Errcode DAO::insertStation(Fields& params)
|
||||
params.remove(DMStation::STATION_ID);
|
||||
params.check(DMStation::LATITUDE, "", "NULL");
|
||||
params.check(DMStation::LONGITUDE, "", "NULL");
|
||||
bool ret = dao->insertFields(params);
|
||||
if (!ret)
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
}
|
||||
return Errcode::OK;
|
||||
|
||||
std::string sql = params.toSqlInsert(DMStation::TABLENAME);
|
||||
return DAO::exec(dao, sql);
|
||||
}
|
||||
|
||||
// 查询场站信息列表
|
||||
bool DAO::queryStationList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
|
||||
Errcode DAO::queryStationList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
|
||||
{
|
||||
if (!dao) { dao = DaoEntity::create(""); }
|
||||
std::string sql = "SELECT * FROM " + DMStation::TABLENAME;
|
||||
return DaoEntity::execOnce(sql, result);
|
||||
return DAO::exec(dao, sql, result);
|
||||
}
|
||||
|
||||
// 分页查询场站信息列表
|
||||
@@ -221,11 +238,10 @@ Errcode DAO::updateStationById(Fields& params)
|
||||
}
|
||||
|
||||
// 查询设备信息列表
|
||||
bool DAO::queryDeviceList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
|
||||
Errcode DAO::queryDeviceList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
|
||||
{
|
||||
if (!dao) { dao = DaoEntity::create(""); }
|
||||
std::string sql = "SELECT * FROM " + DMDevice::TABLENAME;
|
||||
return DaoEntity::execOnce(sql, result);
|
||||
return DAO::exec(dao, sql, result);
|
||||
}
|
||||
|
||||
// 分页查询设备信息列表
|
||||
@@ -242,23 +258,17 @@ bool DAO::queryDeviceList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
}
|
||||
|
||||
// 查询设备类型定义
|
||||
bool DAO::queryDeviceTypeDef(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
|
||||
Errcode DAO::queryDeviceTypeDef(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
|
||||
{
|
||||
if (!dao) { dao = DaoEntity::create(""); }
|
||||
std::string sql = "SELECT * FROM " + DMDeviceTypeDef::TABLENAME + ";";
|
||||
return DaoEntity::execOnce(sql, result);
|
||||
std::string sql = "SELECT * FROM " + DMDefDeviceType::TABLENAME + ";";
|
||||
return DAO::exec(dao, sql, result);
|
||||
}
|
||||
|
||||
Errcode DAO::insertDevice(Fields& params)
|
||||
{
|
||||
auto dao = DaoEntity::create(DMDevice::TABLENAME);
|
||||
bool ret = dao->insertFields(params);
|
||||
if (!ret)
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
}
|
||||
return Errcode::OK;
|
||||
return DAO::exec(NULL, params.toSqlInsert(DMDevice::TABLENAME));
|
||||
}
|
||||
|
||||
Errcode DAO::updateDeviceById(Fields& params)
|
||||
{
|
||||
std::string deviceId = params.value(DMDevice::DEVICE_ID);
|
||||
@@ -266,13 +276,8 @@ Errcode DAO::updateDeviceById(Fields& params)
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
}
|
||||
auto dao = DaoEntity::create(DMDevice::TABLENAME);
|
||||
bool ret = dao->updateFields(params, "WHERE " + DMDevice::DEVICE_ID + "='" + deviceId + "'");
|
||||
if (!ret)
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
}
|
||||
return Errcode::OK;
|
||||
std::string sql = params.toSqlUpdate(DMDevice::TABLENAME, "WHERE " + DMDevice::DEVICE_ID + "='" + deviceId + "'");
|
||||
return DAO::exec(NULL, sql);
|
||||
}
|
||||
|
||||
// 策略管理
|
||||
@@ -288,6 +293,12 @@ bool DAO::queryPolicyList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
return ret;
|
||||
}
|
||||
|
||||
Errcode DAO::queryPolicyList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
|
||||
{
|
||||
std::string sql = "SELECT * FROM " + DMPolicy::TABLENAME;
|
||||
return DAO::exec(dao, sql, result);
|
||||
}
|
||||
|
||||
// 系统日志管理
|
||||
bool DAO::querySystemLogList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
{
|
||||
@@ -301,8 +312,21 @@ bool DAO::querySystemLogList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool DAO::queryStatDataList(std::string startDate, std::string endDate, vector<Fields>& result)
|
||||
Errcode DAO::queryStatDataList(std::shared_ptr<DaoEntity> dao, std::string startDate, std::string endDate, vector<Fields>& result)
|
||||
{
|
||||
std::string sql = "SELECT * FROM " + DMStatStation::TABLENAME + " WHERE dt BETWEEN '" + startDate + "' AND '" + endDate + "';";
|
||||
return DaoEntity::execOnce(sql, result);
|
||||
return DAO::exec(dao, sql, result);
|
||||
}
|
||||
|
||||
Errcode DAO::queryWorkModeDef(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
|
||||
{
|
||||
std::string sql = "SELECT * FROM " + DMDefWorkMode::TABLENAME + ";";
|
||||
return DAO::exec(dao, sql, result);
|
||||
}
|
||||
|
||||
|
||||
Errcode DAO::queryPolicyTypeDef(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
|
||||
{
|
||||
std::string sql = "SELECT * FROM " + DMDefPolicyType::TABLENAME + ";";
|
||||
return DAO::exec(dao, sql, result);
|
||||
}
|
||||
@@ -11,51 +11,55 @@ public:
|
||||
|
||||
static bool count(DaoEntity& dao, std::string tableName, std::string condition, int& count);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 用户管理
|
||||
// 新增用户信息
|
||||
static Errcode insertUser(Fields& params);
|
||||
// 分页查询用户信息列表
|
||||
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 bool queryUserList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
|
||||
// 新增用户信息
|
||||
static Errcode insertUser(Fields& params);
|
||||
// 更新用户信息
|
||||
static Errcode updateUserById(Fields& params);
|
||||
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 角色管理 ===
|
||||
// 查询角色信息列表
|
||||
static bool queryRoleList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
// 分页查询角色信息列表
|
||||
|
||||
// 查询角色信息列表(分页)
|
||||
static bool queryRoleList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 权限管理 ===
|
||||
// 查询角色信息列表
|
||||
static Errcode queryRoleList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
|
||||
|
||||
|
||||
// 查询权限信息列表(分页)
|
||||
static bool queryPermissionList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 场站管理 ===
|
||||
// 新增场站信息
|
||||
static Errcode insertStation(Fields& params);
|
||||
// 查询场站信息列表
|
||||
static bool queryStationList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
// 分页查询场站信息列表
|
||||
|
||||
|
||||
|
||||
// 查询场站信息列表(分页)
|
||||
static bool queryStationList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
|
||||
// 查询场站信息列表
|
||||
static Errcode queryStationList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
// 新增场站信息
|
||||
static Errcode insertStation(Fields& params);
|
||||
// 更新场站信息
|
||||
static Errcode updateStationById(Fields& params);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 设备管理 ===
|
||||
// 查询设备信息列表
|
||||
static bool queryDeviceList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
// 分页查询设备信息列表
|
||||
static bool queryDeviceList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
// 查询设备类型定义
|
||||
static bool queryDeviceTypeDef(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
|
||||
|
||||
// 查询设备信息列表(分页)
|
||||
static bool queryDeviceList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
// 查询设备信息列表
|
||||
static Errcode queryDeviceList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
// 查询设备类型定义
|
||||
static Errcode queryDeviceTypeDef(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
// 新增设备信息
|
||||
static Errcode insertDevice(Fields& params);
|
||||
// 更新设备信息
|
||||
static Errcode updateDeviceById(Fields& params);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -63,6 +67,8 @@ public:
|
||||
// 分页查询策略信息列表
|
||||
static bool queryPolicyList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
|
||||
static Errcode queryPolicyList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 系统日志管理 ===
|
||||
// 分页查询系统日志列表
|
||||
@@ -70,7 +76,10 @@ public:
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 统计数据管理 ===
|
||||
static bool queryStatDataList(std::string startDate, std::string endDate, vector<Fields>& result);
|
||||
static Errcode queryStatDataList(std::shared_ptr<DaoEntity> dao, std::string startDate, std::string endDate, vector<Fields>& result);
|
||||
|
||||
|
||||
static Errcode queryWorkModeDef(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
|
||||
static Errcode queryPolicyTypeDef(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
};
|
||||
@@ -41,8 +41,7 @@ void DaoEntity::setOption(std::string host, int port, std::string user, std::str
|
||||
|
||||
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);
|
||||
return std::make_shared<DaoEntity>(tb_name);
|
||||
}
|
||||
|
||||
bool DaoEntity::execOnce(string sql)
|
||||
@@ -79,7 +78,7 @@ bool DaoEntity::exec(string sql, vector<Fields>& result)
|
||||
|
||||
bool DaoEntity::insertFields(Fields& fields)
|
||||
{
|
||||
string sql = fields.get_insert_sql(tableName_);
|
||||
string sql = fields.toSqlInsert(tableName_);
|
||||
return this->db_->exec(sql);
|
||||
}
|
||||
|
||||
@@ -95,7 +94,7 @@ bool DaoEntity::insertFields(vector<Fields>& vec_fields)
|
||||
{
|
||||
keys = "";
|
||||
values = "";
|
||||
for (auto& item : field.fields())
|
||||
for (auto& item : field.map())
|
||||
{
|
||||
const string& k = item.first;
|
||||
const string& v = item.second;
|
||||
@@ -133,7 +132,7 @@ bool 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 s_key;
|
||||
string s_val;
|
||||
for (auto& item : fields.fields())
|
||||
for (auto& item : fields.map())
|
||||
{
|
||||
if (!s_key.empty())
|
||||
{
|
||||
@@ -162,18 +161,17 @@ bool DaoEntity::duplicateUpdate(Fields& fields, const vector<string>& keys)
|
||||
// });
|
||||
//}
|
||||
|
||||
|
||||
bool DaoEntity::queryFields(string keys, const string& sql_c, vector<Fields>& result)
|
||||
bool DaoEntity::queryFields(string keys, const string& condition, vector<Fields>& result)
|
||||
{
|
||||
ostringstream oss;
|
||||
oss << "SELECT " + keys + " FROM " << tableName_ << (" " + sql_c) << "; ";
|
||||
oss << "SELECT " + keys + " FROM " << tableName_ << (" " + condition) << "; ";
|
||||
return this->db_->exec(oss.str(), result);
|
||||
}
|
||||
|
||||
bool DaoEntity::queryFields(string keys, const string& sql_c, PageInfo& page, vector<Fields>& result)
|
||||
bool DaoEntity::queryFields(string keys, const string& condition, PageInfo& page, vector<Fields>& result)
|
||||
{
|
||||
ostringstream oss;
|
||||
oss << "SELECT count(1) total FROM `" << tableName_ << "` " << sql_c << ";";
|
||||
oss << "SELECT count(1) total FROM `" << tableName_ << "` " << condition << ";";
|
||||
|
||||
vector<Fields> res_total;
|
||||
if (!this->db_->exec(oss.str().c_str(), res_total))
|
||||
@@ -186,7 +184,7 @@ bool DaoEntity::queryFields(string keys, const string& sql_c, PageInfo& page, ve
|
||||
page.total = 0;
|
||||
return true;
|
||||
}
|
||||
page.total = res_total[0].getInt("total");
|
||||
page.total = res_total[0].get<int>("total");
|
||||
if (page.total <= 0)
|
||||
{
|
||||
return true;
|
||||
@@ -198,29 +196,18 @@ bool DaoEntity::queryFields(string keys, const string& sql_c, PageInfo& page, ve
|
||||
page.index = 1;
|
||||
}
|
||||
int start = (page.index - 1) * page.size;
|
||||
oss << "SELECT " << keys << " FROM `" << tableName_ << "` " << sql_c << " LIMIT " << start << "," << page.size << ";";
|
||||
oss << "SELECT " << keys << " FROM `" << tableName_ << "` " << condition << " LIMIT " << start << "," << page.size << ";";
|
||||
return this->db_->exec(oss.str().c_str(), result);
|
||||
}
|
||||
|
||||
bool DaoEntity::updateFields(Fields& fields, const string& sql_c)
|
||||
bool DaoEntity::updateFields(Fields& fields, const string& condition)
|
||||
{
|
||||
string sql = fields.get_update_sql(tableName_, sql_c);
|
||||
std::cout << sql;
|
||||
if (sql_c.empty())
|
||||
{
|
||||
//Spdlogger::error("[DB] update condition is empty, not exec, sql={}", sql);
|
||||
return false;
|
||||
}
|
||||
return this->db_->exec(sql.c_str());
|
||||
string sql = fields.toSqlUpdate(tableName_, condition);
|
||||
return this->db_->exec(sql);
|
||||
}
|
||||
|
||||
bool DaoEntity::updateFields(Fields& fields, vector<string> vec_keys, const string& sql_c)
|
||||
bool DaoEntity::updateFields(Fields& fields, vector<string> vecKeys, const string& condition)
|
||||
{
|
||||
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());
|
||||
string sql = fields.toSqlUpdate(tableName_, vecKeys, condition);
|
||||
return this->db_->exec(sql);
|
||||
}
|
||||
@@ -3,6 +3,15 @@
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// 工作模式定义 表结构字段
|
||||
namespace DMDefWorkMode
|
||||
{
|
||||
const string TABLENAME = "def_work_mode";
|
||||
const string WORK_MODE_ID = "work_mode_id";
|
||||
const string NAME = "name";
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// 用户 表结构字段
|
||||
namespace DMUser
|
||||
@@ -80,6 +89,16 @@ namespace DMStation
|
||||
const string TEL = "tel";
|
||||
const string CAPACITY = "capacity";
|
||||
const string STATUS = "status";
|
||||
const string WORK_MODE_ID = "work_mode_id";
|
||||
const string POLICY_ID = "policy_id";
|
||||
}
|
||||
|
||||
namespace DMDefDeviceType
|
||||
{
|
||||
const string TABLENAME = "def_device_type";
|
||||
const string DEVICE_TYPE_ID = "device_type_id";
|
||||
const string NAME = "name";
|
||||
const string ATTRS = "attrs";
|
||||
}
|
||||
|
||||
namespace DMDevice
|
||||
@@ -87,7 +106,7 @@ namespace DMDevice
|
||||
const string TABLENAME = "device";
|
||||
const string DEVICE_ID = "device_id";
|
||||
const string STATION_ID = "station_id";
|
||||
const string TYPE_ID = "type_id";
|
||||
const string TYPE = "type";
|
||||
const string NAME = "name";
|
||||
const string CODE = "code";
|
||||
const string MODEL = "model";
|
||||
@@ -100,12 +119,11 @@ namespace DMDevice
|
||||
const string UPDATE_TIME = "update_time";
|
||||
}
|
||||
|
||||
namespace DMDeviceTypeDef
|
||||
namespace DMDefPolicyType
|
||||
{
|
||||
const string TABLENAME = "def_device_type";
|
||||
const string TYPE_ID = "type_id";
|
||||
const string TABLENAME = "def_policy_type";
|
||||
const string POLICY_TYPE_ID = "policy_type_id";
|
||||
const string NAME = "name";
|
||||
const string ATTRS = "attrs";
|
||||
}
|
||||
|
||||
namespace DMPolicy
|
||||
|
||||
@@ -50,12 +50,16 @@ void MysqlClient::close()
|
||||
|
||||
bool MysqlClient::exec(std::string sql)
|
||||
{
|
||||
//XLOGD() << "Mysql exec sql=" << sql;
|
||||
if (!mysql_)
|
||||
{
|
||||
XLOGE() << "Mysql exec error, database is not connected.";
|
||||
return false;
|
||||
}
|
||||
if (sql.empty())
|
||||
{
|
||||
XLOGE() << "Mysql exec error, sql is empty.";
|
||||
return false;
|
||||
}
|
||||
int ret = mysql_query(mysql_, sql.c_str());
|
||||
if (0 != ret)
|
||||
{
|
||||
@@ -68,28 +72,18 @@ bool MysqlClient::exec(std::string sql)
|
||||
bool MysqlClient::exec(std::string sql, vector<Fields>& result)
|
||||
{
|
||||
result.clear();
|
||||
if (!mysql_)
|
||||
bool ret = MysqlClient::exec(sql);
|
||||
if (!ret)
|
||||
{
|
||||
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;
|
||||
vector<string> fieldNames;
|
||||
while (true)
|
||||
{
|
||||
MYSQL_FIELD* field = mysql_fetch_field(res);
|
||||
@@ -97,7 +91,7 @@ bool MysqlClient::exec(std::string sql, vector<Fields>& result)
|
||||
{
|
||||
break;
|
||||
}
|
||||
field_names.push_back(field->name);
|
||||
fieldNames.push_back(field->name);
|
||||
}
|
||||
|
||||
while (true)
|
||||
@@ -108,13 +102,13 @@ bool MysqlClient::exec(std::string sql, vector<Fields>& result)
|
||||
break;
|
||||
}
|
||||
|
||||
Fields row_data;
|
||||
for (size_t i = 0; i < field_names.size(); ++i)
|
||||
Fields rowData;
|
||||
for (size_t i = 0; i < fieldNames.size(); ++i)
|
||||
{
|
||||
string field_text = (row[i] == NULL) ? "" : row[i];
|
||||
row_data.set(field_names[i], field_text);
|
||||
rowData.set(fieldNames[i], field_text);
|
||||
}
|
||||
result.push_back(row_data);
|
||||
result.push_back(rowData);
|
||||
}
|
||||
|
||||
// 释放结果集
|
||||
|
||||
64
src/database/SQL.cpp
Normal file
64
src/database/SQL.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#include "SQL.h"
|
||||
|
||||
std::string SQL::str()
|
||||
{
|
||||
if (type == TYPE::select)
|
||||
{
|
||||
// SELECT * from t_tabname WHERE id='1';
|
||||
if (sql_k.empty()) {}
|
||||
std::string s = "SELECT " + (sql_k.empty() ? "*" : sql_k) + " FROM `" + tabname + "`";
|
||||
if (!sql_c.empty()) { s += (" WHERE" + sql_c); }
|
||||
return s + ";";
|
||||
}
|
||||
else if (type == TYPE::update)
|
||||
{
|
||||
// UPDATE t_tabname SET a='1', b='2' WHERE id='1';
|
||||
std::string s = "UPDATE `" + tabname + "` SET " + sql_k + " WHERE" + sql_c;
|
||||
return s + ";";
|
||||
}
|
||||
else if (type == TYPE::insert)
|
||||
{
|
||||
// INSERT INTO t_tabname (a,b,c) VALUES('1','2','3');
|
||||
std::string s = "INSERT INTO `" + tabname + "` (" + sql_k + ") VALUES(" + sql_v + ");";
|
||||
return s;
|
||||
}
|
||||
else if (type == TYPE::del)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
SQL& SQL::table(std::string t)
|
||||
{
|
||||
tabname = t;
|
||||
return *this;
|
||||
}
|
||||
|
||||
SQL& SQL::select(std::string k)
|
||||
{
|
||||
if (!sql_k.empty()) { sql_k += ","; }
|
||||
sql_k += k;
|
||||
return *this;
|
||||
}
|
||||
SQL& SQL::where(std::string expr)
|
||||
{
|
||||
sql_c += (" " + expr);
|
||||
return *this;
|
||||
}
|
||||
|
||||
SQL& SQL::update(std::string k, std::string v)
|
||||
{
|
||||
if (!sql_k.empty()) { sql_k += ","; }
|
||||
sql_k += ("`" + k + "`='" + v + "'");
|
||||
return *this;
|
||||
}
|
||||
|
||||
SQL& SQL::insert(std::string k, std::string v)
|
||||
{
|
||||
if (!sql_k.empty()) { sql_k += ","; }
|
||||
sql_k += ("`" + k + "`");
|
||||
if (!sql_v.empty()) { sql_v += ","; }
|
||||
sql_v += ("'" + v + "'");
|
||||
return *this;
|
||||
}
|
||||
@@ -13,71 +13,19 @@ public:
|
||||
del,
|
||||
};
|
||||
|
||||
SQL(TYPE t = TYPE::select) : type(t)
|
||||
{
|
||||
}
|
||||
SQL(TYPE t = TYPE::select) : type(t) {}
|
||||
|
||||
std::string str()
|
||||
{
|
||||
if (type == TYPE::select)
|
||||
{
|
||||
// SELECT * from t_tabname WHERE id='1';
|
||||
if (sql_k.empty()) {}
|
||||
std::string s = "SELECT " + (sql_k.empty() ? "*" : sql_k) + " FROM `" + tabname + "`";
|
||||
if (!sql_c.empty()) { s += (" WHERE" + sql_c); }
|
||||
return s + ";";
|
||||
}
|
||||
else if (type == TYPE::update)
|
||||
{
|
||||
// UPDATE t_tabname SET a='1', b='2' WHERE id='1';
|
||||
std::string s = "UPDATE `" + tabname + "` SET " + sql_k + " WHERE" + sql_c;
|
||||
return s + ";";
|
||||
}
|
||||
else if (type == TYPE::insert)
|
||||
{
|
||||
// INSERT INTO t_tabname (a,b,c) VALUES('1','2','3');
|
||||
std::string s = "INSERT INTO `" + tabname + "` (" + sql_k + ") VALUES(" + sql_v + ");";
|
||||
return s;
|
||||
}
|
||||
else if (type == TYPE::del)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
SQL& table(std::string t)
|
||||
{
|
||||
tabname = t;
|
||||
return *this;
|
||||
}
|
||||
std::string str();
|
||||
|
||||
SQL& select(std::string k)
|
||||
{
|
||||
if (!sql_k.empty()) { sql_k += ","; }
|
||||
sql_k += k;
|
||||
return *this;
|
||||
}
|
||||
SQL& where(std::string expr)
|
||||
{
|
||||
sql_c += (" " + expr);
|
||||
return *this;
|
||||
}
|
||||
SQL& table(std::string t);
|
||||
|
||||
SQL& update(std::string k, std::string v)
|
||||
{
|
||||
if (!sql_k.empty()) { sql_k += ","; }
|
||||
sql_k += ("`" + k + "`='" + v + "'");
|
||||
return *this;
|
||||
}
|
||||
SQL& select(std::string k);
|
||||
|
||||
SQL& insert(std::string k, std::string v)
|
||||
{
|
||||
if (!sql_k.empty()) { sql_k += ","; }
|
||||
sql_k += ("`" + k + "`");
|
||||
if (!sql_v.empty()) { sql_v += ","; }
|
||||
sql_v += ("'" + v + "'");
|
||||
return *this;
|
||||
}
|
||||
SQL& where(std::string expr);
|
||||
|
||||
SQL& update(std::string k, std::string v);
|
||||
|
||||
SQL& insert(std::string k, std::string v);
|
||||
|
||||
private:
|
||||
TYPE type = TYPE::select;
|
||||
|
||||
Reference in New Issue
Block a user