Commit ae3736e8 authored by yangjian's avatar yangjian

add mysql function: clear data

parent 4583f5fe
#include "custom_mysql.hpp"
#include "logger.hpp"
#include "udsf_config.hpp"
#include <mysql/mysql.h>
using namespace config;
extern udsf_config udsf_cfg;
CustomMysql::CustomMysql(){}
CustomMysql::~CustomMysql(){}
bool CustomMysql::clearing_tables_of_db(std::vector<std::string> & tables)
{
MYSQL mysql;
mysql_init(&mysql);
if (!mysql_real_connect(&mysql, udsf_cfg.mysql.mysql_server.c_str(), udsf_cfg.mysql.mysql_user.c_str(),udsf_cfg.mysql.mysql_pass.c_str(), udsf_cfg.mysql.mysql_db.c_str(), udsf_cfg.mysql.mysql_port, 0, 0))
{
Logger::udsf_app().error("An error occurred while connecting to db: %s", mysql_error(&mysql));
return false;
}
std::string query = "";
for(int i=0;i<tables.size();i++)
{
query = "TRUNCATE TABLE `" + tables[i] + "`";
if (mysql_query(&mysql, query.c_str()))
{
Logger::udsf_app().error("mysql_query failure!SQL(%s)", query.c_str());
Logger::udsf_app().error("mysql error(%s)", mysql_error(&mysql));
mysql_close(&mysql);
return false;
}
}
mysql_close(&mysql);
return true;
}
#ifndef _CUSTOM_MYSQL_H_
#define _CUSTOM_MYSQL_H_
#include <vector>
#include <string>
class CustomMysql{
public:
CustomMysql();
~CustomMysql();
static bool clearing_tables_of_db(std::vector<std::string> & tables);
};
#endif
......@@ -21,10 +21,13 @@
#include "Connection.h"
#include "ConnectionPool.h"
#include "custom_mysql.hpp"
using namespace config;
udsf_config udsf_cfg;
int main(int argc, char **argv) {
std::vector<std::string> db_tables;
if (!Options::parse(argc, argv)) {
std::cout << "Options::parse() failed" << std::endl;
return 1;
......@@ -35,6 +38,13 @@ int main(int argc, char **argv) {
if(udsf_cfg.load(Options::getlibconfigConfig()) < 0) return 0;
udsf_cfg.display();
db_tables.push_back("gnb_context");
db_tables.push_back("nas_context");
db_tables.push_back("pdu_session_context");
db_tables.push_back("ue_context");
db_tables.push_back("ue_ngap_context");
CustomMysql::clearing_tables_of_db(db_tables);
Logger::udsf_app().debug("Initiating UDSF server endpoints");
Pistache::Address addr(udsf_cfg.sbi.addr4, Pistache::Port(udsf_cfg.sbi.port));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment