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
@@ -23,311 +23,45 @@
#include "../database.h"
#include "../string_util.h"
#include "base/base_faction_base_data_repository.h"
class FactionBaseDataRepository {
class FactionBaseDataRepository: public BaseFactionBaseDataRepository {
public:
struct FactionBaseData {
int16 client_faction_id;
int16 min;
int16 max;
int16 unk_hero1;
int16 unk_hero2;
int16 unk_hero3;
};
static std::string PrimaryKey()
{
return std::string("client_faction_id");
}
/**
* 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
*
* FactionBaseDataRepository::GetByZoneAndVersion(int zone_id, int zone_version)
* FactionBaseDataRepository::GetWhereNeverExpires()
* FactionBaseDataRepository::GetWhereXAndY()
* FactionBaseDataRepository::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 {
"client_faction_id",
"min",
"max",
"unk_hero1",
"unk_hero2",
"unk_hero3",
};
}
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("faction_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 FactionBaseData NewEntity()
{
FactionBaseData entry{};
entry.client_faction_id = 0;
entry.min = -2000;
entry.max = 2000;
entry.unk_hero1 = 0;
entry.unk_hero2 = 0;
entry.unk_hero3 = 0;
return entry;
}
static FactionBaseData GetFactionBaseDataEntry(
const std::vector<FactionBaseData> &faction_base_datas,
int faction_base_data_id
)
{
for (auto &faction_base_data : faction_base_datas) {
if (faction_base_data.client_faction_id == faction_base_data_id) {
return faction_base_data;
}
}
return NewEntity();
}
static FactionBaseData FindOne(
int faction_base_data_id
)
{
auto results = content_db.QueryDatabase(
fmt::format(
"{} WHERE id = {} LIMIT 1",
BaseSelect(),
faction_base_data_id
)
);
auto row = results.begin();
if (results.RowCount() == 1) {
FactionBaseData entry{};
entry.client_faction_id = atoi(row[0]);
entry.min = atoi(row[1]);
entry.max = atoi(row[2]);
entry.unk_hero1 = atoi(row[3]);
entry.unk_hero2 = atoi(row[4]);
entry.unk_hero3 = atoi(row[5]);
return entry;
}
return NewEntity();
}
static int DeleteOne(
int faction_base_data_id
)
{
auto results = content_db.QueryDatabase(
fmt::format(
"DELETE FROM {} WHERE {} = {}",
TableName(),
PrimaryKey(),
faction_base_data_id
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static int UpdateOne(
FactionBaseData faction_base_data_entry
)
{
std::vector<std::string> update_values;
auto columns = Columns();
update_values.push_back(columns[1] + " = " + std::to_string(faction_base_data_entry.min));
update_values.push_back(columns[2] + " = " + std::to_string(faction_base_data_entry.max));
update_values.push_back(columns[3] + " = " + std::to_string(faction_base_data_entry.unk_hero1));
update_values.push_back(columns[4] + " = " + std::to_string(faction_base_data_entry.unk_hero2));
update_values.push_back(columns[5] + " = " + std::to_string(faction_base_data_entry.unk_hero3));
auto results = content_db.QueryDatabase(
fmt::format(
"UPDATE {} SET {} WHERE {} = {}",
TableName(),
implode(", ", update_values),
PrimaryKey(),
faction_base_data_entry.client_faction_id
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static FactionBaseData InsertOne(
FactionBaseData faction_base_data_entry
)
{
std::vector<std::string> insert_values;
insert_values.push_back(std::to_string(faction_base_data_entry.min));
insert_values.push_back(std::to_string(faction_base_data_entry.max));
insert_values.push_back(std::to_string(faction_base_data_entry.unk_hero1));
insert_values.push_back(std::to_string(faction_base_data_entry.unk_hero2));
insert_values.push_back(std::to_string(faction_base_data_entry.unk_hero3));
auto results = content_db.QueryDatabase(
fmt::format(
"{} VALUES ({})",
BaseInsert(),
implode(",", insert_values)
)
);
if (results.Success()) {
faction_base_data_entry.id = results.LastInsertedID();
return faction_base_data_entry;
}
faction_base_data_entry = FactionBaseDataRepository::NewEntity();
return faction_base_data_entry;
}
static int InsertMany(
std::vector<FactionBaseData> faction_base_data_entries
)
{
std::vector<std::string> insert_chunks;
for (auto &faction_base_data_entry: faction_base_data_entries) {
std::vector<std::string> insert_values;
insert_values.push_back(std::to_string(faction_base_data_entry.min));
insert_values.push_back(std::to_string(faction_base_data_entry.max));
insert_values.push_back(std::to_string(faction_base_data_entry.unk_hero1));
insert_values.push_back(std::to_string(faction_base_data_entry.unk_hero2));
insert_values.push_back(std::to_string(faction_base_data_entry.unk_hero3));
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<FactionBaseData> All()
{
std::vector<FactionBaseData> all_entries;
auto results = content_db.QueryDatabase(
fmt::format(
"{}",
BaseSelect()
)
);
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row) {
FactionBaseData entry{};
entry.client_faction_id = atoi(row[0]);
entry.min = atoi(row[1]);
entry.max = atoi(row[2]);
entry.unk_hero1 = atoi(row[3]);
entry.unk_hero2 = atoi(row[4]);
entry.unk_hero3 = atoi(row[5]);
all_entries.push_back(entry);
}
return all_entries;
}
static std::vector<FactionBaseData> GetWhere(std::string where_filter)
{
std::vector<FactionBaseData> 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) {
FactionBaseData entry{};
entry.client_faction_id = atoi(row[0]);
entry.min = atoi(row[1]);
entry.max = atoi(row[2]);
entry.unk_hero1 = atoi(row[3]);
entry.unk_hero2 = atoi(row[4]);
entry.unk_hero3 = atoi(row[5]);
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
};