179 lines
3.6 KiB
C++
179 lines
3.6 KiB
C++
|
#include "ehs/db/DbTable.h"
|
||
|
#include "ehs/io/Directory.h"
|
||
|
#include "ehs/io/File_UNX.h"
|
||
|
|
||
|
namespace ehs
|
||
|
{
|
||
|
DbTable::DbTable()
|
||
|
: hashId(0)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
DbTable::DbTable(Str_8 id)
|
||
|
: hashId(id.Hash_64()), id((Str_8&&)id)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
DbTable::DbTable(DbTable&& table) noexcept
|
||
|
: hashId(table.hashId), id((Str_8&&)table.id), varTmpls((Array<DbVarTmpl>&&)table.varTmpls),
|
||
|
objects((Array<DbObject>&&)table.objects)
|
||
|
{
|
||
|
table.hashId = 0;
|
||
|
}
|
||
|
|
||
|
DbTable::DbTable(const DbTable& table)
|
||
|
: hashId(table.hashId), id(table.id), varTmpls(table.varTmpls), objects(table.objects)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
DbTable& DbTable::operator=(DbTable&& table) noexcept
|
||
|
{
|
||
|
if (this == &table)
|
||
|
return *this;
|
||
|
|
||
|
hashId = table.hashId;
|
||
|
id = (Str_8&&)table.id;
|
||
|
varTmpls = (Array<DbVarTmpl>&&)table.varTmpls;
|
||
|
objects = (Array<DbObject>&&)table.objects;
|
||
|
|
||
|
for (UInt_64 i = 0; i < objects.Size(); ++i)
|
||
|
objects[i].parent = this;
|
||
|
|
||
|
table.hashId = 0;
|
||
|
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
|
DbTable& DbTable::operator=(const DbTable& table)
|
||
|
{
|
||
|
if (this == &table)
|
||
|
return *this;
|
||
|
|
||
|
hashId = table.hashId;
|
||
|
id = table.id;
|
||
|
varTmpls = table.varTmpls;
|
||
|
objects = table.objects;
|
||
|
|
||
|
for (UInt_64 i = 0; i < objects.Size(); ++i)
|
||
|
objects[i].parent = this;
|
||
|
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
|
UInt_64 DbTable::GetHashId() const
|
||
|
{
|
||
|
return hashId;
|
||
|
}
|
||
|
|
||
|
void DbTable::SetId(Str_8 newId)
|
||
|
{
|
||
|
hashId = newId.Hash_64();
|
||
|
id = (Str_8&&)newId;
|
||
|
}
|
||
|
|
||
|
Str_8 DbTable::GetId() const
|
||
|
{
|
||
|
return id;
|
||
|
}
|
||
|
|
||
|
bool DbTable::HasVariable(const UInt_64 hashId) const
|
||
|
{
|
||
|
for (UInt_64 i = 0; i < varTmpls.Size(); ++i)
|
||
|
if (varTmpls[i].GetHashId() == hashId)
|
||
|
return true;
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
bool DbTable::HasVariable(const Str_8& id) const
|
||
|
{
|
||
|
return HasVariable(id.Hash_64());
|
||
|
}
|
||
|
|
||
|
bool DbTable::CreateVariable(Str_8 id, DbType type, const Byte* const defaultValue)
|
||
|
{
|
||
|
if (HasVariable(id))
|
||
|
return false;
|
||
|
|
||
|
varTmpls.Push(DbVarTmpl((Str_8&&)id, type, defaultValue));
|
||
|
|
||
|
DbVarTmpl* var = &varTmpls[varTmpls.End()];
|
||
|
|
||
|
for (UInt_64 i = 0; i < objects.Size(); ++i)
|
||
|
objects[i].CreateVariable(var);
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool DbTable::CreateVariable(Str_8 id, const DbType type, const UInt_64 size, const Byte* const defaultValue)
|
||
|
{
|
||
|
if (HasVariable(id))
|
||
|
return false;
|
||
|
|
||
|
varTmpls.Push(DbVarTmpl((Str_8&&)id, type, size, defaultValue));
|
||
|
|
||
|
DbVarTmpl* var = &varTmpls[varTmpls.End()];
|
||
|
|
||
|
for (UInt_64 i = 0; i < objects.Size(); ++i)
|
||
|
objects[i].CreateVariable(var);
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
DbObject* DbTable::CreateObject()
|
||
|
{
|
||
|
objects.Push(DbObject(objects.Size()));
|
||
|
|
||
|
DbObject* obj = &objects[objects.End()];
|
||
|
|
||
|
obj->parent = this;
|
||
|
|
||
|
for (UInt_64 i = 0; i < varTmpls.Size(); ++i)
|
||
|
obj->CreateVariable(&varTmpls[i]);
|
||
|
|
||
|
return obj;
|
||
|
}
|
||
|
|
||
|
DbVarTmpl* DbTable::GetVariableTemplate(UInt_64 hashId) const
|
||
|
{
|
||
|
for (UInt_64 i = 0; i < varTmpls.Size(); ++i)
|
||
|
if (varTmpls[i].GetHashId() == hashId)
|
||
|
return &varTmpls[i];
|
||
|
|
||
|
return nullptr;
|
||
|
}
|
||
|
|
||
|
void DbTable::Serialize(Serializer<UInt_64>& data) const
|
||
|
{
|
||
|
data.WriteStr(id);
|
||
|
data.Write(varTmpls.Size());
|
||
|
|
||
|
for (UInt_64 i = 0; i < varTmpls.Size(); ++i)
|
||
|
varTmpls[i].Serialize(data);
|
||
|
|
||
|
for (UInt_64 i = 0; i < objects.Size(); ++i)
|
||
|
objects[i].Save();
|
||
|
}
|
||
|
|
||
|
void DbTable::Deserialize(Serializer<UInt_64>& data)
|
||
|
{
|
||
|
id = data.ReadStr<Char_8, UInt_64>();
|
||
|
hashId = id.Hash_64();
|
||
|
|
||
|
varTmpls.Resize(data.Read<UInt_64>());
|
||
|
|
||
|
for (UInt_64 i = 0; i < varTmpls.Size(); ++i)
|
||
|
varTmpls[i].Deserialize(data);
|
||
|
|
||
|
Array<Str_8> files = Directory::GetAllFiles(id);
|
||
|
for (UInt_64 i = 0; i < files.Size(); ++i)
|
||
|
{
|
||
|
if (File::ParseExt_8(files[i]) != "eho")
|
||
|
continue;
|
||
|
|
||
|
objects.Push(DbObject(File::ParseName_8(files[i]).ToDecimal<UInt_64>()));
|
||
|
objects[objects.End()].parent = this;
|
||
|
}
|
||
|
}
|
||
|
}
|