mirror of
https://github.com/aimrebirth/tools.git
synced 2026-04-15 01:43:25 +00:00
Add member function to process db into more friendly form.
This commit is contained in:
parent
5f6c3e1fcf
commit
fd543790dd
2 changed files with 54 additions and 2 deletions
|
|
@ -18,7 +18,8 @@
|
||||||
|
|
||||||
#include "db.h"
|
#include "db.h"
|
||||||
|
|
||||||
#include <buffer.h>
|
#include "buffer.h"
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
std::string getSqlType(FieldType type)
|
std::string getSqlType(FieldType type)
|
||||||
{
|
{
|
||||||
|
|
@ -146,3 +147,45 @@ void db::open(const path &p)
|
||||||
for (auto &v : values)
|
for (auto &v : values)
|
||||||
v.load_fields(t, b);
|
v.load_fields(t, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
polygon4::tools::db::processed_db db::process() const
|
||||||
|
{
|
||||||
|
auto process_string = [](const auto &s)
|
||||||
|
{
|
||||||
|
return str2utf8(s);
|
||||||
|
};
|
||||||
|
|
||||||
|
polygon4::tools::db::processed_db pdb;
|
||||||
|
for (auto &v : values)
|
||||||
|
{
|
||||||
|
auto tbl = t.tables.find(v.table_id);
|
||||||
|
if (tbl == t.tables.end())
|
||||||
|
continue;
|
||||||
|
polygon4::tools::db::record r;
|
||||||
|
for (auto &f : v.fields)
|
||||||
|
{
|
||||||
|
auto fld = t.fields.find(f.field_id);
|
||||||
|
if (fld == t.fields.end())
|
||||||
|
continue;
|
||||||
|
auto name = process_string(fld->second.name);
|
||||||
|
switch (fld->second.type)
|
||||||
|
{
|
||||||
|
case FieldType::String:
|
||||||
|
r[name] = process_string(f.s.c_str());
|
||||||
|
break;
|
||||||
|
case FieldType::Integer:
|
||||||
|
r[name] = f.i;
|
||||||
|
break;
|
||||||
|
case FieldType::Float:
|
||||||
|
r[name] = f.f;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
auto table_name = process_string(tbl->second.name);
|
||||||
|
auto row_name = process_string(v.name);
|
||||||
|
pdb[table_name][row_name] = r;
|
||||||
|
}
|
||||||
|
return pdb;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -90,6 +90,14 @@ struct value
|
||||||
void load_fields(const tab &tab, buffer &b);
|
void load_fields(const tab &tab, buffer &b);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
namespace polygon4::tools::db
|
||||||
|
{
|
||||||
|
using value = std::variant<std::string, int, float>;
|
||||||
|
using record = std::map<std::string, value>;
|
||||||
|
using table = std::map<std::string, record>;
|
||||||
|
using processed_db = std::map<std::string, table>;
|
||||||
|
};
|
||||||
|
|
||||||
struct db
|
struct db
|
||||||
{
|
{
|
||||||
uint32_t number_of_values = 0;
|
uint32_t number_of_values = 0;
|
||||||
|
|
@ -99,4 +107,5 @@ struct db
|
||||||
|
|
||||||
void load(const buffer &b);
|
void load(const buffer &b);
|
||||||
void open(const path &basename);
|
void open(const path &basename);
|
||||||
|
polygon4::tools::db::processed_db process() const;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue