[db] Write sql from processed db.

This commit is contained in:
lzwdgc 2020-09-18 23:31:15 +03:00
parent 217ba181c0
commit 1f3ebf67e7

View file

@ -27,7 +27,7 @@
#include <fstream> #include <fstream>
void create_sql(path p, const db &db) void create_sql(path p, const polygon4::tools::db::processed_db &db)
{ {
std::ofstream ofile(p += ".sql"); std::ofstream ofile(p += ".sql");
if (!ofile) if (!ofile)
@ -41,82 +41,77 @@ void create_sql(path p, const db &db)
ofile << "drop table if exists " << master_table_name << ";\n"; ofile << "drop table if exists " << master_table_name << ";\n";
ofile << "create table \"" << master_table_name << "\"\n"; ofile << "create table \"" << master_table_name << "\"\n";
ofile << "(\n"; ofile << "(\n";
ofile << " \"" + str2utf8(id) + "\" INTEGER,\n"; ofile << " \"" + id + "\" INTEGER,\n";
ofile << " \"" + str2utf8(row_type) + "\" TEXT\n"; ofile << " \"" + row_type + "\" TEXT\n";
ofile << ");\n"; ofile << ");\n";
ofile << "\n"; ofile << "\n";
// db tables for (auto &[name,table] : db)
for (auto &table : db.t.tables) {
static int idx = 1;
ofile << "insert into \"" << master_table_name << "\" values (";
ofile << "'" << idx++ << "', ";
ofile << "'" << name << "'";
ofile << ");\n";
}
ofile << "\n";
// db tables
for (auto &[name, t] : db)
{ {
auto &t = table.second;
std::string name = str2utf8(t.name);
ofile << "drop table if exists " << name << ";\n"; ofile << "drop table if exists " << name << ";\n";
ofile << "create table \"" << name << "\"\n"; ofile << "create table \"" << name << "\"\n";
ofile << "(\n"; ofile << "(\n";
std::string s; std::string s;
s += " \"" + str2utf8(id) + "\" INTEGER,\n"; s += " \"" + id + "\" INTEGER,\n";
s += " \"" + str2utf8(row_type) + "\" TEXT,\n"; s += " \"" + row_type + "\" TEXT,\n";
for (auto &f : db.t.fields) std::map<std::string, FieldType> fields;
for (auto &[_, f] : t)
{ {
if (f.second.table_id != t.id) for (auto &[n, v] : f)
continue; fields[n] = (FieldType)v.index();
s += " \"" + str2utf8(f.second.name) + "\" " + getSqlType(f.second.type) + ",\n";
} }
for (auto &[n, t] : fields)
s += " \"" + n + "\" " + getSqlType(t) + ",\n";
s.resize(s.size() - 2); s.resize(s.size() - 2);
ofile << s << "\n"; ofile << s << "\n";
ofile << ");\n"; ofile << ");\n";
ofile << "\n"; ofile << "\n";
} }
// db master table
for (auto &table : db.t.tables)
{
static int idx = 1;
ofile << "insert into \"" << master_table_name << "\" values (";
ofile << "'" << idx++ << "', ";
ofile << "'" << str2utf8(table.second.name) << "'";
ofile << ");\n";
}
// db tables // db tables
std::map<int,int> idx; std::map<std::string,int> idx;
for (auto &v : db.values) for (auto &[tn, t] : db)
{ {
auto tbl = db.t.tables.find(v.table_id); for (auto &[rn, row] : t)
if (tbl == db.t.tables.end()) {
continue; ofile << "insert into \"" << tn << "\" (";
ofile << "insert into \"" << str2utf8(tbl->second.name) << "\" (";
std::string s; std::string s;
s += "'" + str2utf8(id) + "', "; s += "'" + id + "', ";
s += "'" + str2utf8(row_type) + "', "; s += "'" + row_type + "', ";
for (auto &f : v.fields) for (auto &[n, v] : row)
{ s += "'" + n + "', ";
auto fld = db.t.fields.find(f.field_id);
s += "'" + str2utf8(fld->second.name) + "', ";
}
s.resize(s.size() - 2); s.resize(s.size() - 2);
ofile << s << ") values ("; ofile << s << ") values (";
s.clear(); s.clear();
s += "'" + std::to_string(idx[v.table_id]++) + "', "; s += "'" + std::to_string(++idx[tn]) + "', ";
s += "'" + str2utf8(v.name) + "', "; s += "'" + rn + "', ";
for (auto &f : v.fields) for (auto &[n, v] : row)
{ {
s += "'"; s += "'";
auto fld = db.t.fields.find(f.field_id); switch ((FieldType)v.index())
switch (fld->second.type)
{ {
case FieldType::String: case FieldType::String:
s += str2utf8(f.s.c_str()); s += std::get<std::string>(v);
break; break;
case FieldType::Integer: case FieldType::Integer:
s += std::to_string(f.i); s += std::to_string(std::get<int>(v));
break; break;
case FieldType::Float: case FieldType::Float:
s += std::to_string(f.f); s += std::to_string(std::get<float>(v));
break; break;
default: default:
assert(false); SW_UNIMPLEMENTED;
} }
s += "', "; s += "', ";
} }
@ -124,6 +119,7 @@ void create_sql(path p, const db &db)
ofile << s << ");\n"; ofile << s << ");\n";
} }
} }
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -133,6 +129,6 @@ int main(int argc, char *argv[])
db db; db db;
db.open(db_fn); db.open(db_fn);
create_sql(db_fn, db); create_sql(db_fn, db.process());
return 0; return 0;
} }