Database implementation mostly complete.

This commit is contained in:
2024-04-09 01:44:15 -07:00
parent beba947c69
commit d13fe81ac5
20 changed files with 768 additions and 106 deletions

View File

@@ -10,27 +10,44 @@ namespace ehs
private:
UInt_64 hashId;
Str_8 id;
Version version;
Array<DbTable> tables;
public:
Database();
Database(Str_8 id, const Version& version);
Database(const Str_8& filePath);
Database(Database&& db) noexcept;
Database(const Database& db);
Database& operator=(Database&& db) noexcept;
Database& operator=(const Database& db);
UInt_64 GetHashId() const;
void SetId(Str_8 newId);
Str_8 GetId() const;
void SetVersion(const Version& newVersion);
Version GetVersion() const;
bool HasTable(UInt_64 hashId) const;
bool HasTable(Str_8& id) const;
bool HasTable(const Str_8& id) const;
bool CreateTable(Str_8 id);
DbTable* CreateTable(Str_8 id);
DbTable* GetTable(UInt_64 hashId) const;
DbTable* GetTable(Str_8& id) const;
DbTable* GetTable(const Str_8& id) const;
void Save();
void Save(const Str_8& directory) const;
};
}

View File

@@ -11,15 +11,17 @@ namespace ehs
{
private:
friend class DbTable;
friend class DbVar;
UInt_64 id;
Array<DbVar> vars;
bool loaded;
DbTable* parent;
Array<DbVar> vars;
public:
DbObject();
DbObject(UInt_64 id);
DbObject(DbObject&& obj) noexcept;
DbObject(const DbObject& obj);
@@ -28,13 +30,13 @@ namespace ehs
DbObject& operator=(const DbObject& obj);
UInt_64 GetId();
UInt_64 GetId() const;
bool HasVariable(UInt_64 hashId);
bool HasVariable(UInt_64 hashId) const;
DbVar* GetVariable(UInt_64 hashId) const;
void Save();
void Save() const;
void Load();
@@ -42,6 +44,7 @@ namespace ehs
void Free();
DbTable* GetParent() const;
private:
void CreateVariable(DbVarTmpl* master);
};
}

View File

@@ -4,33 +4,55 @@
#include "ehs/Array.h"
#include "DbVarTmpl.h"
#include "DbObject.h"
#include "ehs/Serializer.h"
namespace ehs
{
class DbTable
{
private:
friend class Database;
friend class DbVar;
UInt_64 hashId;
Str_8 id;
Version version;
Array<DbVarTmpl> varTmpls;
Array<DbObject> objects;
public:
DbTable();
DbTable();
DbTable(Str_8 id);
DbTable(DbTable&& table) noexcept;
DbTable(const DbTable& table);
DbTable& operator=(DbTable&& table) noexcept;
DbTable& operator=(const DbTable& table);
UInt_64 GetHashId() const;
void SetId(Str_8 newId);
Str_8 GetId() const;
Version GetVersion() const;
bool HasVariable(UInt_64 hashId) const;
bool CreateVariable(Str_8 id, DbType type);
bool HasVariable(const Str_8& id) const;
bool CreateVariable(Str_8 id, DbType type, UInt_64 size);
bool CreateVariable(Str_8 id, DbType type, const Byte* defaultValue);
void Save();
bool CreateVariable(Str_8 id, DbType type, UInt_64 size, const Byte* defaultValue);
DbObject *CreateObject();
private:
DbVarTmpl* GetVariableTemplate(UInt_64 hashId) const;
void Serialize(Serializer<UInt_64>& data) const;
void Deserialize(Serializer<UInt_64>& data);
};
}

View File

@@ -22,5 +22,40 @@ namespace ehs
CHAR_8
};
constexpr UInt_8 DbTypeToSize(DbType type);
constexpr UInt_8 DbTypeToSize(const DbType type)
{
switch (type)
{
case DbType::SINT_64:
return 8;
case DbType::UINT_64:
return 8;
case DbType::SINT_32:
return 4;
case DbType::UINT_32:
return 4;
case DbType::SINT_16:
return 2;
case DbType::UINT_16:
return 2;
case DbType::SINT_8:
return 1;
case DbType::UINT_8:
return 1;
case DbType::FLOAT:
return 4;
case DbType::DOUBLE:
return 8;
case DbType::BOOLEAN:
return 1;
case DbType::CHAR_32:
return 4;
case DbType::CHAR_16:
return 2;
case DbType::CHAR_8:
return 1;
default:
return 0;
}
}
}

View File

@@ -1,37 +1,56 @@
#pragma once
#include "ehs/Types.h"
#include "DbType.h"
#include "ehs/Serializer.h"
namespace ehs
{
class DbVarTmpl;
class DbObject;
class DbVar
{
private:
friend class DbObject;
friend class DbVarTmpl;
UInt_64 hashId;
DbObject *parent;
DbVarTmpl *master;
UInt_64 size;
Byte* data;
Byte *data;
public:
~DbVar();
DbVar();
DbVar(UInt_64 hashId, UInt_64 size, Byte* data);
DbVar(UInt_64 hashId, DbVarTmpl *master, UInt_64 size, const Byte *data);
DbVar(DbVar&& var) noexcept;
DbVar(DbVar &&var) noexcept;
DbVar(const DbVar& var);
DbVar(const DbVar &var);
DbVar& operator=(DbVar&& var) noexcept;
DbVar &operator=(DbVar &&var) noexcept;
DbVar& operator=(const DbVar& var);
DbVar &operator=(const DbVar &var);
explicit operator Byte*() const;
explicit operator Byte *() const;
UInt_64 GetHashId() const;
UInt_64 GetSize() const;
void SetData(UInt_64 newSize, const Byte* newData);
void SetData(const Byte* newData);
Byte* GetData() const;
private:
void Serialize(Serializer<UInt_64> &data) const;
void Deserialize(Serializer<UInt_64> &data);
};
}
}

View File

@@ -2,23 +2,35 @@
#include "DbType.h"
#include "ehs/EHS.h"
#include "ehs/BaseObj.h"
#include "ehs/Serializer.h"
#include "ehs/Str.h"
namespace ehs
{
class DbVarTmpl : public BaseObj
class DbVar;
class DbVarTmpl
{
private:
friend class DbTable;
friend class DbVar;
UInt_64 hashId;
Str_8 id;
DbType type;
bool array;
UInt_64 size;
Byte* def;
Array<DbVar*> slaves;
public:
~DbVarTmpl();
DbVarTmpl();
DbVarTmpl(Str_8 id, DbType type, bool array);
DbVarTmpl(Str_8 id, DbType type, UInt_64 size, const Byte* def);
DbVarTmpl(Str_8 id, DbType type, const Byte* def);
DbVarTmpl(DbVarTmpl&& varTmpl) noexcept;
@@ -41,5 +53,16 @@ namespace ehs
void SetIsArray(bool value);
bool IsArray() const;
UInt_64 GetSize() const;
Byte* GetDefault() const;
private:
void UpdateSlave(const DbVar *oldSlave, DbVar *newSlave) const;
void Serialize(Serializer<UInt_64> &data) const;
void Deserialize(Serializer<UInt_64> &data);
};
}