struct environmental_variable_table
{
std::string station_num;
std::string dev_type;
std::string dev_nid;
std::string dev_sn;
std::string dev_ip;
std::string key;
std::string m_value;
std::string value_desc;
};
REFLECTION(environmental_variable_table, station_num, dev_type, dev_nid, dev_sn, dev_ip, key, m_value, value_desc)
class CEnvironmentalVariableTable
{
private:
dbng<mysql> m_mysql;
bool connectDB()
{
return m_mysql.connect(MYSQL_IP, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);
}
public:
CEnvironmentalVariableTable()
{
if (!connectDB())
{
throw std::runtime_error("Failed to connect to the database.");
}
}
void addData(const environmental_variable_table &envVar)
{
if (!m_mysql.insert(envVar))
{
throw std::runtime_error("Failed to insert environmental variable.");
}
}
void addData(const std::vector<environmental_variable_table> &envVars)
{
if (!m_mysql.insert(envVars))
{
throw std::runtime_error("Failed to insert environmental variables.");
}
}
environmental_variable_table getData(const std::string &key, const std::string &dev_ip)
{
auto result = m_mysql.query<environmental_variable_table>("key='" + key + "' AND dev_ip='" + dev_ip + "'");
if (result.empty())
{
throw std::runtime_error("Failed to retrieve environmental variable or variable not found. key=" + key + ", dev_ip=" + dev_ip);
}
return result.front();
}
void updateData(const std::string &key, const std::string &dev_ip, const environmental_variable_table &updatedEnvVar)
{
if (!m_mysql.update(updatedEnvVar, "key='" + key + "' AND dev_ip='" + dev_ip + "'"))
{
throw std::runtime_error("Failed to update environmental variable.");
}
}
void removeData(const std::string &key, const std::string &dev_ip)
{
if (!m_mysql.execute("delete from environmental_variable_table where key='" + key + "' AND dev_ip='" + dev_ip + "'"))
{
throw std::runtime_error("Failed to remove environmental variable.");
}
}
};