mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-28 03:09:24 +08:00
完成系统管理web端功能,实现系统管理服务端接口,实现登录功能
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
//#include "PvInstance.h"
|
||||
//#include "spdlogger.h"
|
||||
|
||||
MysqlOptions DaoEntity::options_;
|
||||
MysqlOption DaoEntity::option_;
|
||||
|
||||
DaoEntity::DaoEntity(string tb_name)
|
||||
{
|
||||
@@ -12,7 +12,7 @@ DaoEntity::DaoEntity(string tb_name)
|
||||
//opts.password = "123456";
|
||||
//opts.port = 3306;
|
||||
//opts.dbname = "pvb";
|
||||
db_ = make_shared<MysqlClient>(DaoEntity::options_);
|
||||
db_ = make_shared<MysqlClient>(DaoEntity::option_);
|
||||
if (!db_->isConnected())
|
||||
{
|
||||
//Global::data().status_msg = "数据库连接异常!";
|
||||
@@ -26,9 +26,17 @@ DaoEntity::~DaoEntity()
|
||||
db_ = nullptr;
|
||||
}
|
||||
|
||||
MysqlOptions& DaoEntity::mysqlOptions()
|
||||
MysqlOption& DaoEntity::mysqlOption()
|
||||
{
|
||||
return DaoEntity::options_;
|
||||
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;
|
||||
}
|
||||
|
||||
std::shared_ptr<DaoEntity> DaoEntity::create(string tb_name)
|
||||
@@ -39,13 +47,13 @@ std::shared_ptr<DaoEntity> DaoEntity::create(string tb_name)
|
||||
|
||||
bool DaoEntity::execOnce(string sql)
|
||||
{
|
||||
auto db = make_shared<MysqlClient>(DaoEntity::options_);
|
||||
auto db = make_shared<MysqlClient>(DaoEntity::option_);
|
||||
return db->exec(sql);
|
||||
}
|
||||
|
||||
bool DaoEntity::execOnce(string sql, vector<DataFields>& result)
|
||||
{
|
||||
auto db = make_shared<MysqlClient>(DaoEntity::options_);
|
||||
auto db = make_shared<MysqlClient>(DaoEntity::option_);
|
||||
return db->exec(sql, result);
|
||||
}
|
||||
|
||||
@@ -141,7 +149,7 @@ bool DaoEntity::duplicateUpdate(DataFields& fields, vector<string>& keys)
|
||||
{
|
||||
s_data += ",";
|
||||
}
|
||||
s_data += (k + "='" + fields.get_str(k) + "'");
|
||||
s_data += (k + "='" + fields.getStr(k) + "'");
|
||||
}
|
||||
string sql = "INSERT INTO " + tableName_ + "(" + s_key + ") VALUES (" + s_val + ") ON duplicate KEY UPDATE " + s_data;
|
||||
return this->db_->exec(sql);
|
||||
@@ -178,7 +186,7 @@ bool DaoEntity::queryFields(string keys, const string& sql_c, PageInfo& pageinfo
|
||||
pageinfo.total = 0;
|
||||
return true;
|
||||
}
|
||||
pageinfo.total = res_total[0].get_int("total");
|
||||
pageinfo.total = res_total[0].getInt("total");
|
||||
if (pageinfo.total <= 0)
|
||||
{
|
||||
return true;
|
||||
|
||||
@@ -9,7 +9,8 @@ public:
|
||||
DaoEntity(string tableName);
|
||||
~DaoEntity();
|
||||
|
||||
static MysqlOptions& mysqlOptions();
|
||||
static MysqlOption& mysqlOption();
|
||||
static void setOption(std::string host, int port, std::string user, std::string pwd, std::string dbname);
|
||||
|
||||
static std::shared_ptr<DaoEntity> create(string tableName);
|
||||
|
||||
@@ -99,7 +100,7 @@ public:
|
||||
bool updateFields(DataFields& fields, vector<string> vecKeys, const string& cond);
|
||||
|
||||
protected:
|
||||
static MysqlOptions options_;
|
||||
static MysqlOption option_;
|
||||
|
||||
// mysql 数据库操作对象
|
||||
std::shared_ptr<MysqlClient> db_ = nullptr;
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
//#include "Spdlogger.h"
|
||||
#include "Logger.h"
|
||||
|
||||
MysqlClient::MysqlClient(MysqlOptions opts) : options_(opts)
|
||||
MysqlClient::MysqlClient(MysqlOption option) : option_(option)
|
||||
{
|
||||
conn();
|
||||
}
|
||||
@@ -20,9 +20,10 @@ int MysqlClient::conn()
|
||||
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);
|
||||
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;
|
||||
}
|
||||
@@ -49,7 +50,7 @@ void MysqlClient::close()
|
||||
|
||||
bool MysqlClient::exec(std::string sql)
|
||||
{
|
||||
XLOGD() << "Mysql exec sql=" << sql;
|
||||
//XLOGD() << "Mysql exec sql=" << sql;
|
||||
if (!mysql_)
|
||||
{
|
||||
XLOGE() << "Mysql exec error, database is not connected.";
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct MysqlOptions
|
||||
struct MysqlOption
|
||||
{
|
||||
std::string host;
|
||||
std::string user;
|
||||
@@ -27,7 +27,7 @@ struct MysqlOptions
|
||||
class MysqlClient
|
||||
{
|
||||
public:
|
||||
MysqlClient(MysqlOptions options);
|
||||
MysqlClient(MysqlOption option);
|
||||
~MysqlClient();
|
||||
|
||||
/**
|
||||
@@ -62,7 +62,7 @@ private:
|
||||
MYSQL* mysql_ = nullptr;
|
||||
|
||||
// 数据库连接信息
|
||||
MysqlOptions options_;
|
||||
MysqlOption option_;
|
||||
};
|
||||
|
||||
#endif // !!! _DbMysql_H_
|
||||
|
||||
Reference in New Issue
Block a user