2024-05-29 14:46:44 +08:00
|
|
|
|
#include "opmysql.h"
|
2024-11-15 18:22:43 +08:00
|
|
|
|
|
|
|
|
|
#ifndef _USING_MYSQL_51_LIB_
|
2024-05-29 14:46:44 +08:00
|
|
|
|
#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>
|
2024-11-15 18:22:43 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
2024-05-29 14:46:44 +08:00
|
|
|
|
#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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 18:22:43 +08:00
|
|
|
|
bool OpDatabase::OpenDatabase(const std::string& server /*= "127.0.0.1"*/, int dbport /*= 3306*/, const std::string& dbuser/* = "root"*/, const std::string& dbpasswd /*= "Hj57471000"*/, const std::string& database /*= "hjems"*/)
|
2024-05-29 14:46:44 +08:00
|
|
|
|
{
|
2024-11-15 18:22:43 +08:00
|
|
|
|
#ifndef _USING_MYSQL_51_LIB_
|
2024-05-29 14:46:44 +08:00
|
|
|
|
// <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";
|
2024-09-02 16:17:12 +08:00
|
|
|
|
//std::string password = "Hj57471000";
|
|
|
|
|
std::string password = "L2ysc1s1kr";
|
2024-05-29 14:46:44 +08:00
|
|
|
|
//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;
|
|
|
|
|
}
|
2024-11-15 18:22:43 +08:00
|
|
|
|
#else
|
|
|
|
|
bool ok = false;
|
|
|
|
|
m_pDbConnection = mysql_init(NULL);
|
|
|
|
|
//<2F><><EFBFBD>ӵ<EFBFBD>MySQL<51><4C><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
if (!mysql_real_connect(m_pDbConnection, server.c_str(), dbuser.c_str(), dbpasswd.c_str(), database.c_str(), dbport, NULL, 0))
|
|
|
|
|
{
|
|
|
|
|
hloge("MySQL connection error: %s", mysql_error(m_pDbConnection));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ok = true;
|
|
|
|
|
mysql_set_character_set(m_pDbConnection, "utf8");
|
|
|
|
|
}
|
|
|
|
|
if (!ok)
|
|
|
|
|
{
|
|
|
|
|
hloge("Failed to connect to database.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
hlogd("new database connection created successfully!");
|
|
|
|
|
}
|
|
|
|
|
return ok;
|
|
|
|
|
#endif
|
2024-05-29 14:46:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20>ر<EFBFBD><D8B1><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
void OpDatabase::CloseDatabase()
|
|
|
|
|
{
|
2024-11-15 18:22:43 +08:00
|
|
|
|
#ifdef _USING_MYSQL_51_LIB_
|
|
|
|
|
mysql_close(m_pDbConnection);
|
|
|
|
|
#else
|
2024-05-29 15:13:20 +08:00
|
|
|
|
m_pDbConnection->close();
|
2024-05-29 14:46:44 +08:00
|
|
|
|
//delete m_pDbConnection;
|
2024-11-15 18:22:43 +08:00
|
|
|
|
#endif
|
2024-05-29 14:46:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2024-11-15 18:22:43 +08:00
|
|
|
|
#ifndef _USING_MYSQL_51_LIB_
|
2024-05-29 14:46:44 +08:00
|
|
|
|
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." );
|
|
|
|
|
}
|
2024-11-15 18:22:43 +08:00
|
|
|
|
#else
|
|
|
|
|
#endif
|
2024-05-29 14:46:44 +08:00
|
|
|
|
}
|