212 lines
5.2 KiB
C++
212 lines
5.2 KiB
C++
|
#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();
|
|||
|
}
|
|||
|
|
|||
|
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
|
|||
|
{
|
|||
|
// <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());
|
|||
|
|
|||
|
// <><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(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)
|
|||
|
std::string tmp = passwd_md5 + salt + salt;
|
|||
|
std::string smd5 = CalculateMD5(tmp);
|
|||
|
hloge("upass=[%s],cakc=[%s],src=[%s]", pass.c_str(),smd5.c_str(), tmp.c_str());
|
|||
|
|
|||
|
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;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
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;
|
|||
|
}
|
|||
|
}
|