2024-05-29 14:46:44 +08:00
|
|
|
|
#include "opmysql.h"
|
|
|
|
|
#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 <sstream>
|
|
|
|
|
#include "openjson.h"
|
|
|
|
|
|
|
|
|
|
OpDatabase OpDatabase::m_instance;
|
|
|
|
|
|
|
|
|
|
OpDatabase::OpDatabase()
|
|
|
|
|
:m_pDbConnection(nullptr)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OpDatabase::~OpDatabase()
|
|
|
|
|
{
|
2024-05-29 15:13:20 +08:00
|
|
|
|
//CloseDatabase();
|
2024-05-29 14:46:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
2024-05-29 15:13:20 +08:00
|
|
|
|
m_pDbConnection->close();
|
2024-05-29 14:46:44 +08:00
|
|
|
|
//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." );
|
|
|
|
|
}
|
|
|
|
|
}
|