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,365 +23,45 @@
#include "../database.h"
#include "../string_util.h"
#include "base/base_npc_spells_entries_repository.h"
class NpcSpellsEntriesRepository {
class NpcSpellsEntriesRepository: public BaseNpcSpellsEntriesRepository {
public:
struct NpcSpellsEntries {
int id;
int npc_spells_id;
int16 spellid;
int type;
int8 minlevel;
int8 maxlevel;
int16 manacost;
int recast_delay;
int16 priority;
int resist_adjust;
int16 min_hp;
int16 max_hp;
};
static std::string PrimaryKey()
{
return std::string("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
*
* NpcSpellsEntriesRepository::GetByZoneAndVersion(int zone_id, int zone_version)
* NpcSpellsEntriesRepository::GetWhereNeverExpires()
* NpcSpellsEntriesRepository::GetWhereXAndY()
* NpcSpellsEntriesRepository::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 {
"id",
"npc_spells_id",
"spellid",
"type",
"minlevel",
"maxlevel",
"manacost",
"recast_delay",
"priority",
"resist_adjust",
"min_hp",
"max_hp",
};
}
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("npc_spells_entries");
}
static std::string BaseSelect()
{
return fmt::format(
"SELECT {} FROM {}",
ColumnsRaw(),
TableName()
);
}
static std::string BaseInsert()
{
return fmt::format(
"INSERT INTO {} ({}) ",
TableName(),
InsertColumnsRaw()
);
}
static NpcSpellsEntries NewEntity()
{
NpcSpellsEntries entry{};
entry.id = 0;
entry.npc_spells_id = 0;
entry.spellid = 0;
entry.type = 0;
entry.minlevel = 0;
entry.maxlevel = 255;
entry.manacost = -1;
entry.recast_delay = -1;
entry.priority = 0;
entry.resist_adjust = 0;
entry.min_hp = 0;
entry.max_hp = 0;
return entry;
}
static NpcSpellsEntries GetNpcSpellsEntriesEntry(
const std::vector<NpcSpellsEntries> &npc_spells_entriess,
int npc_spells_entries_id
)
{
for (auto &npc_spells_entries : npc_spells_entriess) {
if (npc_spells_entries.id == npc_spells_entries_id) {
return npc_spells_entries;
}
}
return NewEntity();
}
static NpcSpellsEntries FindOne(
int npc_spells_entries_id
)
{
auto results = content_db.QueryDatabase(
fmt::format(
"{} WHERE id = {} LIMIT 1",
BaseSelect(),
npc_spells_entries_id
)
);
auto row = results.begin();
if (results.RowCount() == 1) {
NpcSpellsEntries entry{};
entry.id = atoi(row[0]);
entry.npc_spells_id = atoi(row[1]);
entry.spellid = atoi(row[2]);
entry.type = atoi(row[3]);
entry.minlevel = atoi(row[4]);
entry.maxlevel = atoi(row[5]);
entry.manacost = atoi(row[6]);
entry.recast_delay = atoi(row[7]);
entry.priority = atoi(row[8]);
entry.resist_adjust = atoi(row[9]);
entry.min_hp = atoi(row[10]);
entry.max_hp = atoi(row[11]);
return entry;
}
return NewEntity();
}
static int DeleteOne(
int npc_spells_entries_id
)
{
auto results = content_db.QueryDatabase(
fmt::format(
"DELETE FROM {} WHERE {} = {}",
TableName(),
PrimaryKey(),
npc_spells_entries_id
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static int UpdateOne(
NpcSpellsEntries npc_spells_entries_entry
)
{
std::vector<std::string> update_values;
auto columns = Columns();
update_values.push_back(columns[1] + " = " + std::to_string(npc_spells_entries_entry.npc_spells_id));
update_values.push_back(columns[2] + " = " + std::to_string(npc_spells_entries_entry.spellid));
update_values.push_back(columns[3] + " = " + std::to_string(npc_spells_entries_entry.type));
update_values.push_back(columns[4] + " = " + std::to_string(npc_spells_entries_entry.minlevel));
update_values.push_back(columns[5] + " = " + std::to_string(npc_spells_entries_entry.maxlevel));
update_values.push_back(columns[6] + " = " + std::to_string(npc_spells_entries_entry.manacost));
update_values.push_back(columns[7] + " = " + std::to_string(npc_spells_entries_entry.recast_delay));
update_values.push_back(columns[8] + " = " + std::to_string(npc_spells_entries_entry.priority));
update_values.push_back(columns[9] + " = " + std::to_string(npc_spells_entries_entry.resist_adjust));
update_values.push_back(columns[10] + " = " + std::to_string(npc_spells_entries_entry.min_hp));
update_values.push_back(columns[11] + " = " + std::to_string(npc_spells_entries_entry.max_hp));
auto results = content_db.QueryDatabase(
fmt::format(
"UPDATE {} SET {} WHERE {} = {}",
TableName(),
implode(", ", update_values),
PrimaryKey(),
npc_spells_entries_entry.id
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static NpcSpellsEntries InsertOne(
NpcSpellsEntries npc_spells_entries_entry
)
{
std::vector<std::string> insert_values;
insert_values.push_back(std::to_string(npc_spells_entries_entry.npc_spells_id));
insert_values.push_back(std::to_string(npc_spells_entries_entry.spellid));
insert_values.push_back(std::to_string(npc_spells_entries_entry.type));
insert_values.push_back(std::to_string(npc_spells_entries_entry.minlevel));
insert_values.push_back(std::to_string(npc_spells_entries_entry.maxlevel));
insert_values.push_back(std::to_string(npc_spells_entries_entry.manacost));
insert_values.push_back(std::to_string(npc_spells_entries_entry.recast_delay));
insert_values.push_back(std::to_string(npc_spells_entries_entry.priority));
insert_values.push_back(std::to_string(npc_spells_entries_entry.resist_adjust));
insert_values.push_back(std::to_string(npc_spells_entries_entry.min_hp));
insert_values.push_back(std::to_string(npc_spells_entries_entry.max_hp));
auto results = content_db.QueryDatabase(
fmt::format(
"{} VALUES ({})",
BaseInsert(),
implode(",", insert_values)
)
);
if (results.Success()) {
npc_spells_entries_entry.id = results.LastInsertedID();
return npc_spells_entries_entry;
}
npc_spells_entries_entry = NpcSpellsEntriesRepository::NewEntity();
return npc_spells_entries_entry;
}
static int InsertMany(
std::vector<NpcSpellsEntries> npc_spells_entries_entries
)
{
std::vector<std::string> insert_chunks;
for (auto &npc_spells_entries_entry: npc_spells_entries_entries) {
std::vector<std::string> insert_values;
insert_values.push_back(std::to_string(npc_spells_entries_entry.npc_spells_id));
insert_values.push_back(std::to_string(npc_spells_entries_entry.spellid));
insert_values.push_back(std::to_string(npc_spells_entries_entry.type));
insert_values.push_back(std::to_string(npc_spells_entries_entry.minlevel));
insert_values.push_back(std::to_string(npc_spells_entries_entry.maxlevel));
insert_values.push_back(std::to_string(npc_spells_entries_entry.manacost));
insert_values.push_back(std::to_string(npc_spells_entries_entry.recast_delay));
insert_values.push_back(std::to_string(npc_spells_entries_entry.priority));
insert_values.push_back(std::to_string(npc_spells_entries_entry.resist_adjust));
insert_values.push_back(std::to_string(npc_spells_entries_entry.min_hp));
insert_values.push_back(std::to_string(npc_spells_entries_entry.max_hp));
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<NpcSpellsEntries> All()
{
std::vector<NpcSpellsEntries> all_entries;
auto results = content_db.QueryDatabase(
fmt::format(
"{}",
BaseSelect()
)
);
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row) {
NpcSpellsEntries entry{};
entry.id = atoi(row[0]);
entry.npc_spells_id = atoi(row[1]);
entry.spellid = atoi(row[2]);
entry.type = atoi(row[3]);
entry.minlevel = atoi(row[4]);
entry.maxlevel = atoi(row[5]);
entry.manacost = atoi(row[6]);
entry.recast_delay = atoi(row[7]);
entry.priority = atoi(row[8]);
entry.resist_adjust = atoi(row[9]);
entry.min_hp = atoi(row[10]);
entry.max_hp = atoi(row[11]);
all_entries.push_back(entry);
}
return all_entries;
}
static std::vector<NpcSpellsEntries> GetWhere(std::string where_filter)
{
std::vector<NpcSpellsEntries> 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) {
NpcSpellsEntries entry{};
entry.id = atoi(row[0]);
entry.npc_spells_id = atoi(row[1]);
entry.spellid = atoi(row[2]);
entry.type = atoi(row[3]);
entry.minlevel = atoi(row[4]);
entry.maxlevel = atoi(row[5]);
entry.manacost = atoi(row[6]);
entry.recast_delay = atoi(row[7]);
entry.priority = atoi(row[8]);
entry.resist_adjust = atoi(row[9]);
entry.min_hp = atoi(row[10]);
entry.max_hp = atoi(row[11]);
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
};