Split repositories out between a base immutable repository (that does not change) and an extended repository of which developers can add more custom methods to

This commit is contained in:
Akkadius
2020-04-05 22:19:36 -05:00
parent fe7e850a04
commit c978ca65a4
324 changed files with 70882 additions and 58053 deletions
+35 -334
View File
@@ -23,344 +23,45 @@
#include "../database.h"
#include "../string_util.h"
#include "base/base_base_data_repository.h"
class BaseDataRepository {
class BaseDataRepository: public BaseBaseDataRepository {
public:
struct BaseData {
int level;
int class;
float hp;
float mana;
float end;
float unk1;
float unk2;
float hp_fac;
float mana_fac;
float end_fac;
};
static std::string PrimaryKey()
{
return std::string("class");
}
/**
* This file was auto generated on Apr 5, 2020 and can be modified and extended upon
*
* Base repository methods are automatically
* generated in the "base" version of this repository. The base repository
* is immutable and to be left untouched, while methods in this class
* are used as extension methods for more specific persistence-layer
* accessors or mutators
*
* Base Methods (Subject to be expanded upon in time)
*
* InsertOne
* UpdateOne
* DeleteOne
* FindOne
* GetWhere(std::string where_filter)
* DeleteWhere(std::string where_filter)
* InsertMany
* All
*
* Example custom methods in a repository
*
* BaseDataRepository::GetByZoneAndVersion(int zone_id, int zone_version)
* BaseDataRepository::GetWhereNeverExpires()
* BaseDataRepository::GetWhereXAndY()
* BaseDataRepository::DeleteWhereXAndY()
*
* Most of the above could be covered by base methods, but if you as a developer
* find yourself re-using logic for other parts of the code, its best to just make a
* method that can be re-used easily elsewhere especially if it can use a base repository
* method and encapsulate filters there
*/
static std::vector<std::string> Columns()
{
return {
"level",
"class",
"hp",
"mana",
"end",
"unk1",
"unk2",
"hp_fac",
"mana_fac",
"end_fac",
};
}
static std::string ColumnsRaw()
{
return std::string(implode(", ", Columns()));
}
static std::string InsertColumnsRaw()
{
std::vector<std::string> insert_columns;
for (auto &column : Columns()) {
if (column == PrimaryKey()) {
continue;
}
insert_columns.push_back(column);
}
return std::string(implode(", ", insert_columns));
}
static std::string TableName()
{
return std::string("base_data");
}
static std::string BaseSelect()
{
return fmt::format(
"SELECT {} FROM {}",
ColumnsRaw(),
TableName()
);
}
static std::string BaseInsert()
{
return fmt::format(
"INSERT INTO {} ({}) ",
TableName(),
InsertColumnsRaw()
);
}
static BaseData NewEntity()
{
BaseData entry{};
entry.level = 0;
entry.class = 0;
entry.hp = 0;
entry.mana = 0;
entry.end = 0;
entry.unk1 = 0;
entry.unk2 = 0;
entry.hp_fac = 0;
entry.mana_fac = 0;
entry.end_fac = 0;
return entry;
}
static BaseData GetBaseDataEntry(
const std::vector<BaseData> &base_datas,
int base_data_id
)
{
for (auto &base_data : base_datas) {
if (base_data.class == base_data_id) {
return base_data;
}
}
return NewEntity();
}
static BaseData FindOne(
int base_data_id
)
{
auto results = content_db.QueryDatabase(
fmt::format(
"{} WHERE id = {} LIMIT 1",
BaseSelect(),
base_data_id
)
);
auto row = results.begin();
if (results.RowCount() == 1) {
BaseData entry{};
entry.level = atoi(row[0]);
entry.class = atoi(row[1]);
entry.hp = atof(row[2]);
entry.mana = atof(row[3]);
entry.end = atof(row[4]);
entry.unk1 = atof(row[5]);
entry.unk2 = atof(row[6]);
entry.hp_fac = atof(row[7]);
entry.mana_fac = atof(row[8]);
entry.end_fac = atof(row[9]);
return entry;
}
return NewEntity();
}
static int DeleteOne(
int base_data_id
)
{
auto results = content_db.QueryDatabase(
fmt::format(
"DELETE FROM {} WHERE {} = {}",
TableName(),
PrimaryKey(),
base_data_id
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static int UpdateOne(
BaseData base_data_entry
)
{
std::vector<std::string> update_values;
auto columns = Columns();
update_values.push_back(columns[2] + " = " + std::to_string(base_data_entry.hp));
update_values.push_back(columns[3] + " = " + std::to_string(base_data_entry.mana));
update_values.push_back(columns[4] + " = " + std::to_string(base_data_entry.end));
update_values.push_back(columns[5] + " = " + std::to_string(base_data_entry.unk1));
update_values.push_back(columns[6] + " = " + std::to_string(base_data_entry.unk2));
update_values.push_back(columns[7] + " = " + std::to_string(base_data_entry.hp_fac));
update_values.push_back(columns[8] + " = " + std::to_string(base_data_entry.mana_fac));
update_values.push_back(columns[9] + " = " + std::to_string(base_data_entry.end_fac));
auto results = content_db.QueryDatabase(
fmt::format(
"UPDATE {} SET {} WHERE {} = {}",
TableName(),
implode(", ", update_values),
PrimaryKey(),
base_data_entry.class
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static BaseData InsertOne(
BaseData base_data_entry
)
{
std::vector<std::string> insert_values;
insert_values.push_back(std::to_string(base_data_entry.hp));
insert_values.push_back(std::to_string(base_data_entry.mana));
insert_values.push_back(std::to_string(base_data_entry.end));
insert_values.push_back(std::to_string(base_data_entry.unk1));
insert_values.push_back(std::to_string(base_data_entry.unk2));
insert_values.push_back(std::to_string(base_data_entry.hp_fac));
insert_values.push_back(std::to_string(base_data_entry.mana_fac));
insert_values.push_back(std::to_string(base_data_entry.end_fac));
auto results = content_db.QueryDatabase(
fmt::format(
"{} VALUES ({})",
BaseInsert(),
implode(",", insert_values)
)
);
if (results.Success()) {
base_data_entry.id = results.LastInsertedID();
return base_data_entry;
}
base_data_entry = BaseDataRepository::NewEntity();
return base_data_entry;
}
static int InsertMany(
std::vector<BaseData> base_data_entries
)
{
std::vector<std::string> insert_chunks;
for (auto &base_data_entry: base_data_entries) {
std::vector<std::string> insert_values;
insert_values.push_back(std::to_string(base_data_entry.hp));
insert_values.push_back(std::to_string(base_data_entry.mana));
insert_values.push_back(std::to_string(base_data_entry.end));
insert_values.push_back(std::to_string(base_data_entry.unk1));
insert_values.push_back(std::to_string(base_data_entry.unk2));
insert_values.push_back(std::to_string(base_data_entry.hp_fac));
insert_values.push_back(std::to_string(base_data_entry.mana_fac));
insert_values.push_back(std::to_string(base_data_entry.end_fac));
insert_chunks.push_back("(" + implode(",", insert_values) + ")");
}
std::vector<std::string> insert_values;
auto results = content_db.QueryDatabase(
fmt::format(
"{} VALUES {}",
BaseInsert(),
implode(",", insert_chunks)
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static std::vector<BaseData> All()
{
std::vector<BaseData> all_entries;
auto results = content_db.QueryDatabase(
fmt::format(
"{}",
BaseSelect()
)
);
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row) {
BaseData entry{};
entry.level = atoi(row[0]);
entry.class = atoi(row[1]);
entry.hp = atof(row[2]);
entry.mana = atof(row[3]);
entry.end = atof(row[4]);
entry.unk1 = atof(row[5]);
entry.unk2 = atof(row[6]);
entry.hp_fac = atof(row[7]);
entry.mana_fac = atof(row[8]);
entry.end_fac = atof(row[9]);
all_entries.push_back(entry);
}
return all_entries;
}
static std::vector<BaseData> GetWhere(std::string where_filter)
{
std::vector<BaseData> all_entries;
auto results = content_db.QueryDatabase(
fmt::format(
"{} WHERE {}",
BaseSelect(),
where_filter
)
);
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row) {
BaseData entry{};
entry.level = atoi(row[0]);
entry.class = atoi(row[1]);
entry.hp = atof(row[2]);
entry.mana = atof(row[3]);
entry.end = atof(row[4]);
entry.unk1 = atof(row[5]);
entry.unk2 = atof(row[6]);
entry.hp_fac = atof(row[7]);
entry.mana_fac = atof(row[8]);
entry.end_fac = atof(row[9]);
all_entries.push_back(entry);
}
return all_entries;
}
static int DeleteWhere(std::string where_filter)
{
auto results = content_db.QueryDatabase(
fmt::format(
"DELETE FROM {} WHERE {}",
TableName(),
PrimaryKey(),
where_filter
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
// Custom extended repository methods here
};