2024-06-05 20:52:33 +08:00
|
|
|
|
#include "opmysql.h"
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <mysql_driver.h>
|
|
|
|
|
#include <mysql_connection.h>
|
|
|
|
|
#include <cppconn/prepared_statement.h>
|
|
|
|
|
#include <cppconn/resultset.h>
|
|
|
|
|
#include <cppconn/statement.h>
|
|
|
|
|
#include <cppconn/exception.h>
|
|
|
|
|
#include <hv/hlog.h>
|
|
|
|
|
#include <hv/md5.h>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include "openjson.h"
|
|
|
|
|
|
|
|
|
|
OpDatabase OpDatabase::m_instance;
|
|
|
|
|
|
|
|
|
|
OpDatabase::OpDatabase()
|
|
|
|
|
:m_pDbConnection(nullptr)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OpDatabase::~OpDatabase()
|
|
|
|
|
{
|
|
|
|
|
//CloseDatabase();
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-07 16:44:21 +08:00
|
|
|
|
|
2024-06-05 20:52:33 +08:00
|
|
|
|
std::string OpDatabase::CalculateMD5(const std::string& data)
|
|
|
|
|
{
|
|
|
|
|
char md5_str[33] = { 0 };
|
|
|
|
|
hv_md5_hex((unsigned char*)data.c_str(), data.size(), md5_str, 33);
|
|
|
|
|
|
|
|
|
|
std::string smd5(md5_str);
|
|
|
|
|
std::transform(smd5.begin(), smd5.end(), smd5.begin(),
|
|
|
|
|
[](unsigned char c)
|
|
|
|
|
{
|
|
|
|
|
return std::toupper(c);
|
|
|
|
|
});
|
|
|
|
|
return smd5;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OpDatabase* OpDatabase::getInstance()
|
|
|
|
|
{
|
|
|
|
|
return &m_instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool OpDatabase::OpenDatabase(const std::string& server, const std::string& dbuser, const std::string& database)
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//std::string server = "tcp://127.0.0.1:3306";
|
|
|
|
|
//std::string dbuser = "root";
|
|
|
|
|
std::string password = "Hj57471000";
|
|
|
|
|
//std::string database = "hjems";
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
sql::mysql::MySQL_Driver* driver;
|
|
|
|
|
driver = sql::mysql::get_mysql_driver_instance();
|
|
|
|
|
//m_pDbConnection.reset(driver->connect(server, dbuser, password));
|
|
|
|
|
m_pDbConnection = driver->connect(server, dbuser, password);
|
|
|
|
|
|
|
|
|
|
if (!m_pDbConnection)
|
|
|
|
|
{
|
|
|
|
|
hloge("Failed to connect to database.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>Ϊʹ<CEAA><CAB9>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>
|
|
|
|
|
m_pDbConnection->setSchema(database);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (sql::SQLException& e)
|
|
|
|
|
{
|
|
|
|
|
std::ostringstream ss;
|
|
|
|
|
ss << "SQLException: " << e.what();
|
|
|
|
|
ss<< " (MySQL error code: " << e.getErrorCode();
|
|
|
|
|
ss << ", SQLState: " << e.getSQLState() << " )";
|
|
|
|
|
hloge("Failed to connect to database: %s",ss.str().c_str());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20>ر<EFBFBD><D8B1><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
void OpDatabase::CloseDatabase()
|
|
|
|
|
{
|
|
|
|
|
m_pDbConnection->close();
|
|
|
|
|
//delete m_pDbConnection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void OpDatabase::InsertMessage(const std::string& ts, const std::string& msg_type, const std::string& fsu, const std::string& content, int topic, int dev_id)
|
|
|
|
|
{
|
|
|
|
|
const char* insertSql = "INSERT INTO tbl_data (data_timestamp, data_type, FsuCode, data_content, topic, device_id) VALUES (?,?,?,?,?,?);";
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>prepared statement
|
|
|
|
|
std::unique_ptr<sql::PreparedStatement> insertStmt(m_pDbConnection->prepareStatement(insertSql));
|
|
|
|
|
|
|
|
|
|
// <20><EFBFBD><F3B6A8B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵIJ<DDB5><C4B2><EFBFBD>
|
|
|
|
|
insertStmt->setString(1, ts);
|
|
|
|
|
insertStmt->setString(2, msg_type);
|
|
|
|
|
insertStmt->setString(3, fsu);
|
|
|
|
|
insertStmt->setString(4, content);
|
|
|
|
|
insertStmt->setInt(5, topic);
|
|
|
|
|
insertStmt->setInt(6, dev_id);
|
|
|
|
|
|
|
|
|
|
// ִ<>в<EFBFBD><D0B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int rowsAffected = insertStmt->executeUpdate();
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ɹ<EFBFBD>
|
|
|
|
|
if (rowsAffected > 0)
|
|
|
|
|
{
|
|
|
|
|
hlogi( "New message inserted successfully." );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
hloge( "No rows affected during insert operation." );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool OpDatabase::queryUser(const std::string& user_id, const std::string& passwd_md5, std::string& jsonResult)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-06-07 16:44:21 +08:00
|
|
|
|
#if 0
|
2024-06-05 20:52:33 +08:00
|
|
|
|
// <20><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
std::string server = "tcp://127.0.0.1:3306";
|
|
|
|
|
std::string dbuser = "root";
|
|
|
|
|
std::string password = "Hj57471000";
|
|
|
|
|
std::string database = "hjems";
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
sql::mysql::MySQL_Driver* driver;
|
|
|
|
|
driver = sql::mysql::get_mysql_driver_instance();
|
|
|
|
|
//m_pDbConnection.reset(driver->connect(server, dbuser, password));
|
|
|
|
|
sql::Connection* pDbConnection = driver->connect(server, dbuser, password);
|
|
|
|
|
|
|
|
|
|
if (!pDbConnection)
|
|
|
|
|
{
|
|
|
|
|
hloge("Failed to connect to database.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>Ϊʹ<CEAA><CAB9>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>
|
|
|
|
|
pDbConnection->setSchema(database);
|
|
|
|
|
hloge("%s : %s", user_id.c_str(), passwd_md5.c_str());
|
2024-06-07 16:44:21 +08:00
|
|
|
|
#endif
|
2024-06-05 20:52:33 +08:00
|
|
|
|
// <><D7BC>SQL<51><4C>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>
|
|
|
|
|
std::string sql = "SELECT uid,uname, upasswd,usalt,email,mobile1,mobile2,memo FROM tbl_user WHERE uid = ?";
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>prepared statement
|
2024-06-07 16:44:21 +08:00
|
|
|
|
std::unique_ptr<sql::PreparedStatement> pstmt(m_pDbConnection->prepareStatement(sql));
|
2024-06-05 20:52:33 +08:00
|
|
|
|
|
|
|
|
|
// <20><EFBFBD><F3B6A8B2><EFBFBD>
|
|
|
|
|
pstmt->setString(1, user_id); // <20>滻Ϊ<E6BBBB><CEAA>Ҫ<EFBFBD><D2AA>ѯ<EFBFBD><D1AF><EFBFBD>û<EFBFBD><C3BB><EFBFBD>
|
|
|
|
|
|
|
|
|
|
// ִ<>в<EFBFBD>ѯ
|
|
|
|
|
std::unique_ptr<sql::ResultSet> res(pstmt->executeQuery());
|
|
|
|
|
|
|
|
|
|
bool ret = false;
|
|
|
|
|
OpenJson json;
|
|
|
|
|
auto& nodeRoot = json["user"];
|
|
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
while (res->next())
|
|
|
|
|
{
|
|
|
|
|
auto& node = nodeRoot[i++];
|
|
|
|
|
node["valid"] = 0;
|
|
|
|
|
node["uid"] = res->getString("uid");
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>username<6D><65>password<72><64><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
std::string pass = res->getString("upasswd");
|
|
|
|
|
std::string salt = res->getString("usalt");
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>passwd<77><64>salt֮<74><D6AE><EFBFBD>Ĺ<EFBFBD>ϵ
|
|
|
|
|
//!passwd=md5(passwd_md5+salt+salt)
|
2024-06-07 16:44:21 +08:00
|
|
|
|
// passwd_md5 <20>Ǵ<EFBFBD>д<EFBFBD>ģ<EFBFBD><C4A3><EFBFBD>Ҫȷ<D2AA><C8B7>һ<EFBFBD><D2BB>
|
|
|
|
|
std::string tmp2(passwd_md5);
|
|
|
|
|
std::transform(tmp2.begin(), tmp2.end(), tmp2.begin(),
|
|
|
|
|
[](unsigned char c)
|
|
|
|
|
{
|
|
|
|
|
return std::toupper(c);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
std::string tmp = tmp2 + salt + salt;
|
2024-06-05 20:52:33 +08:00
|
|
|
|
std::string smd5 = CalculateMD5(tmp);
|
|
|
|
|
|
|
|
|
|
if (pass == smd5)
|
|
|
|
|
{
|
|
|
|
|
node["valid"] = 1;
|
|
|
|
|
|
|
|
|
|
node["uname"] = res->getString("uname");
|
|
|
|
|
node["email"] = res->getString("email");
|
|
|
|
|
node["mobile1"] = res->getString("mobile1");
|
|
|
|
|
node["mobile2"] = res->getString("mobile2");
|
|
|
|
|
node["memo"] = res->getString("memo");
|
|
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-06-07 16:44:21 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
hloge("upass=[%s],calc=[%s],src=[%s]", pass.c_str(), smd5.c_str(), tmp.c_str());
|
|
|
|
|
}
|
2024-06-05 20:52:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jsonResult = json.encode();
|
|
|
|
|
|
2024-06-07 16:44:21 +08:00
|
|
|
|
//pDbConnection->close();
|
2024-06-05 20:52:33 +08:00
|
|
|
|
//delete pDbConnection;
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
catch (sql::SQLException& e)
|
|
|
|
|
{
|
|
|
|
|
std::ostringstream ss;
|
|
|
|
|
ss << "SQLException: " << e.what();
|
|
|
|
|
ss << " (MySQL error code: " << e.getErrorCode();
|
|
|
|
|
ss << ", SQLState: " << e.getSQLState() << " )";
|
|
|
|
|
hloge("Failed to connect to database: %s", ss.str().c_str());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-09-10 16:12:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool OpDatabase::queryInnerDevice(int dev_id, std::string& jsonResult)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
#if 0
|
|
|
|
|
// <20><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
std::string server = "tcp://127.0.0.1:3306";
|
|
|
|
|
std::string dbuser = "root";
|
|
|
|
|
std::string password = "Hj57471000";
|
|
|
|
|
std::string database = "hjems";
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
sql::mysql::MySQL_Driver* driver;
|
|
|
|
|
driver = sql::mysql::get_mysql_driver_instance();
|
|
|
|
|
//m_pDbConnection.reset(driver->connect(server, dbuser, password));
|
|
|
|
|
sql::Connection* pDbConnection = driver->connect(server, dbuser, password);
|
|
|
|
|
|
|
|
|
|
if( !pDbConnection )
|
|
|
|
|
{
|
|
|
|
|
hloge("Failed to connect to database.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>Ϊʹ<CEAA><CAB9>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>
|
|
|
|
|
pDbConnection->setSchema(database);
|
|
|
|
|
hloge("%s : %s", user_id.c_str(), passwd_md5.c_str());
|
|
|
|
|
#endif
|
|
|
|
|
// <><D7BC>SQL<51><4C>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>
|
|
|
|
|
std::string sql = "SELECT uid,uname, upasswd,usalt,email,mobile1,mobile2,memo FROM tbl_user WHERE uid = ?";
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>prepared statement
|
|
|
|
|
std::unique_ptr<sql::PreparedStatement> pstmt(m_pDbConnection->prepareStatement(sql));
|
|
|
|
|
|
|
|
|
|
// <20><EFBFBD><F3B6A8B2><EFBFBD>
|
|
|
|
|
pstmt->setString(1, user_id); // <20>滻Ϊ<E6BBBB><CEAA>Ҫ<EFBFBD><D2AA>ѯ<EFBFBD><D1AF><EFBFBD>û<EFBFBD><C3BB><EFBFBD>
|
|
|
|
|
|
|
|
|
|
// ִ<>в<EFBFBD>ѯ
|
|
|
|
|
std::unique_ptr<sql::ResultSet> res(pstmt->executeQuery());
|
|
|
|
|
|
|
|
|
|
bool ret = false;
|
|
|
|
|
OpenJson json;
|
|
|
|
|
auto& nodeRoot = json["user"];
|
|
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
while( res->next() )
|
|
|
|
|
{
|
|
|
|
|
auto& node = nodeRoot[i++];
|
|
|
|
|
node["valid"] = 0;
|
|
|
|
|
node["uid"] = res->getString("uid");
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>username<6D><65>password<72><64><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
std::string pass = res->getString("upasswd");
|
|
|
|
|
std::string salt = res->getString("usalt");
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>passwd<77><64>salt֮<74><D6AE><EFBFBD>Ĺ<EFBFBD>ϵ
|
|
|
|
|
//!passwd=md5(passwd_md5+salt+salt)
|
|
|
|
|
// passwd_md5 <20>Ǵ<EFBFBD>д<EFBFBD>ģ<EFBFBD><C4A3><EFBFBD>Ҫȷ<D2AA><C8B7>һ<EFBFBD><D2BB>
|
|
|
|
|
std::string tmp2(passwd_md5);
|
|
|
|
|
std::transform(tmp2.begin(), tmp2.end(), tmp2.begin(),
|
|
|
|
|
[](unsigned char c)
|
|
|
|
|
{
|
|
|
|
|
return std::toupper(c);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
std::string tmp = tmp2 + salt + salt;
|
|
|
|
|
std::string smd5 = CalculateMD5(tmp);
|
|
|
|
|
|
|
|
|
|
if( pass == smd5 )
|
|
|
|
|
{
|
|
|
|
|
node["valid"] = 1;
|
|
|
|
|
|
|
|
|
|
node["uname"] = res->getString("uname");
|
|
|
|
|
node["email"] = res->getString("email");
|
|
|
|
|
node["mobile1"] = res->getString("mobile1");
|
|
|
|
|
node["mobile2"] = res->getString("mobile2");
|
|
|
|
|
node["memo"] = res->getString("memo");
|
|
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
hloge("upass=[%s],calc=[%s],src=[%s]", pass.c_str(), smd5.c_str(), tmp.c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jsonResult = json.encode();
|
|
|
|
|
|
|
|
|
|
//pDbConnection->close();
|
|
|
|
|
//delete pDbConnection;
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
catch( sql::SQLException& e )
|
|
|
|
|
{
|
|
|
|
|
std::ostringstream ss;
|
|
|
|
|
ss << "SQLException: " << e.what();
|
|
|
|
|
ss << " (MySQL error code: " << e.getErrorCode();
|
|
|
|
|
ss << ", SQLState: " << e.getSQLState() << " )";
|
|
|
|
|
hloge("Failed to connect to database: %s", ss.str().c_str());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-06-05 20:52:33 +08:00
|
|
|
|
}
|