2025-08-22 19:06:50 +08:00
|
|
|
|
#include "Fields.h"
|
|
|
|
|
|
#include "common/Utils.h"
|
2025-08-28 18:42:37 +08:00
|
|
|
|
#include "common/JsonN.h"
|
2025-08-26 18:36:25 +08:00
|
|
|
|
|
2025-08-22 19:06:50 +08:00
|
|
|
|
std::string& Fields::value(std::string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
static std::string tmp;
|
|
|
|
|
|
auto it = mapFields.find(key);
|
|
|
|
|
|
return (it != mapFields.end()) ? it->second : (tmp = "");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 18:36:25 +08:00
|
|
|
|
bool Fields::contains(std::string key)
|
2025-08-22 19:06:50 +08:00
|
|
|
|
{
|
2025-08-26 18:36:25 +08:00
|
|
|
|
return (mapFields.find(key) != mapFields.end());
|
2025-08-22 19:06:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-28 18:42:37 +08:00
|
|
|
|
std::string Fields::remove(string key)
|
2025-08-22 19:06:50 +08:00
|
|
|
|
{
|
2025-08-28 18:42:37 +08:00
|
|
|
|
std::string val;
|
2025-08-22 19:06:50 +08:00
|
|
|
|
auto it = mapFields.find(key);
|
|
|
|
|
|
if (it != mapFields.end())
|
|
|
|
|
|
{
|
2025-08-28 18:42:37 +08:00
|
|
|
|
val = it->second;
|
|
|
|
|
|
mapFields.erase(it);
|
2025-08-22 19:06:50 +08:00
|
|
|
|
}
|
2025-08-28 18:42:37 +08:00
|
|
|
|
return val;
|
2025-08-22 19:06:50 +08:00
|
|
|
|
}
|
2025-08-26 18:36:25 +08:00
|
|
|
|
|
2025-08-22 19:06:50 +08:00
|
|
|
|
void Fields::append(Fields& datafield)
|
|
|
|
|
|
{
|
2025-08-26 18:36:25 +08:00
|
|
|
|
auto& mapVal = datafield.map();
|
|
|
|
|
|
for (auto it = mapVal.begin(); it != mapVal.end(); it++)
|
2025-08-22 19:06:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
mapFields[it->first] = it->second;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-26 18:36:25 +08:00
|
|
|
|
|
|
|
|
|
|
std::unordered_map<string, string>& Fields::map()
|
2025-08-22 19:06:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
return mapFields;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 18:36:25 +08:00
|
|
|
|
int Fields::size()
|
|
|
|
|
|
{
|
|
|
|
|
|
return mapFields.size();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Fields::isEmpty()
|
|
|
|
|
|
{
|
|
|
|
|
|
return mapFields.empty();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Fields::clear()
|
|
|
|
|
|
{
|
|
|
|
|
|
mapFields.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-22 19:06:50 +08:00
|
|
|
|
void Fields::check(string key, string val, string d)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (mapFields.count(key) > 0 && mapFields[key] == val)
|
|
|
|
|
|
{
|
|
|
|
|
|
mapFields[key] = d;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 18:36:25 +08:00
|
|
|
|
void Fields::foreachItem(function<void(string key, string& val)> onForaach)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (auto it = mapFields.begin(); it != mapFields.end(); it++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (onForaach)
|
|
|
|
|
|
{
|
|
|
|
|
|
onForaach(it->first, it->second);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Fields::is_float_number(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& s = mapFields[key];
|
|
|
|
|
|
if (s.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
for (auto& c : s)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (std::isdigit(c) == 0 && c != '.')
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string Fields::toStr()
|
|
|
|
|
|
{
|
|
|
|
|
|
string s;
|
|
|
|
|
|
for (auto it = mapFields.begin(); it != mapFields.end(); it++)
|
|
|
|
|
|
{
|
|
|
|
|
|
s += ("{" + it->first + ":" + it->second + "} ");
|
|
|
|
|
|
}
|
|
|
|
|
|
return s;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string Fields::toSqlInsert(string tableName)
|
2025-08-22 19:06:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
string key;
|
|
|
|
|
|
string val;
|
|
|
|
|
|
for (auto it = mapFields.begin(); it != mapFields.end(); it++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!key.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
key += ",";
|
|
|
|
|
|
val += ",";
|
|
|
|
|
|
}
|
|
|
|
|
|
key += ("`" + it->first + "`");
|
|
|
|
|
|
if (it->second == "null" || it->second == "NULL")
|
|
|
|
|
|
{
|
|
|
|
|
|
val += "NULL";
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
val += ("'" + it->second + "'");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-26 18:36:25 +08:00
|
|
|
|
return "INSERT INTO `" + tableName + "` (" + key + ") VALUES(" + val + ");";
|
2025-08-22 19:06:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 18:36:25 +08:00
|
|
|
|
string Fields::toSqlUpdate(string tableName, string sql_c)
|
2025-08-22 19:06:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
ostringstream oss;
|
2025-08-26 18:36:25 +08:00
|
|
|
|
oss << "update " << tableName << " set ";
|
2025-08-22 19:06:50 +08:00
|
|
|
|
for (auto iter = mapFields.begin(); iter != mapFields.end(); iter++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (iter != mapFields.begin())
|
|
|
|
|
|
{
|
|
|
|
|
|
oss << ",";
|
|
|
|
|
|
};
|
|
|
|
|
|
oss << "`" << iter->first << "`=";
|
|
|
|
|
|
if (iter->second == "null" || iter->second == "NULL")
|
|
|
|
|
|
{
|
|
|
|
|
|
oss << "NULL";
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
oss << "'" << iter->second << "'";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
oss << " " << sql_c << ";";
|
|
|
|
|
|
return oss.str();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 18:36:25 +08:00
|
|
|
|
string Fields::toSqlUpdate(string tableName, std::vector<std::string> vecKeys, string condition)
|
2025-08-22 19:06:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::map<std::string, bool> map_keys;
|
2025-08-26 18:36:25 +08:00
|
|
|
|
for (auto& k : vecKeys) { map_keys[k] = true; }
|
2025-08-22 19:06:50 +08:00
|
|
|
|
|
|
|
|
|
|
ostringstream oss;
|
2025-08-26 18:36:25 +08:00
|
|
|
|
oss << "update " << tableName << " set ";
|
2025-08-22 19:06:50 +08:00
|
|
|
|
for (auto iter = mapFields.begin(); iter != mapFields.end(); iter++)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& k = iter->first;
|
|
|
|
|
|
auto& v = iter->second;
|
|
|
|
|
|
if (!map_keys[k]) { continue; }
|
|
|
|
|
|
if (iter != mapFields.begin())
|
|
|
|
|
|
{
|
|
|
|
|
|
oss << ",";
|
|
|
|
|
|
};
|
|
|
|
|
|
oss << "`" << k << "`=";
|
|
|
|
|
|
if (v == "null" || v == "NULL")
|
|
|
|
|
|
{
|
|
|
|
|
|
oss << "NULL";
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
oss << "'" << v << "'";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-26 18:36:25 +08:00
|
|
|
|
oss << " " << condition << ";";
|
2025-08-22 19:06:50 +08:00
|
|
|
|
return oss.str();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-28 18:42:37 +08:00
|
|
|
|
void Fields::parseJson(std::string jsonstr)
|
|
|
|
|
|
{
|
|
|
|
|
|
NJsonNode jsonroot;
|
|
|
|
|
|
NJson::parse(jsonstr, jsonroot);
|
2025-08-22 19:06:50 +08:00
|
|
|
|
|
2025-08-28 18:42:37 +08:00
|
|
|
|
for (auto& item : jsonroot.items())
|
|
|
|
|
|
{
|
|
|
|
|
|
this->set(item.key(), item.value());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|