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 -325
View File
@@ -23,335 +23,45 @@
#include "../database.h"
#include "../string_util.h"
#include "base/base_lootdrop_entries_repository.h"
class LootdropEntriesRepository {
class LootdropEntriesRepository: public BaseLootdropEntriesRepository {
public:
struct LootdropEntries {
int lootdrop_id;
int item_id;
int16 item_charges;
int8 equip_item;
float chance;
float disabled_chance;
int8 minlevel;
int8 maxlevel;
int8 multiplier;
};
static std::string PrimaryKey()
{
return std::string("item_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
*
* LootdropEntriesRepository::GetByZoneAndVersion(int zone_id, int zone_version)
* LootdropEntriesRepository::GetWhereNeverExpires()
* LootdropEntriesRepository::GetWhereXAndY()
* LootdropEntriesRepository::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 {
"lootdrop_id",
"item_id",
"item_charges",
"equip_item",
"chance",
"disabled_chance",
"minlevel",
"maxlevel",
"multiplier",
};
}
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("lootdrop_entries");
}
static std::string BaseSelect()
{
return fmt::format(
"SELECT {} FROM {}",
ColumnsRaw(),
TableName()
);
}
static std::string BaseInsert()
{
return fmt::format(
"INSERT INTO {} ({}) ",
TableName(),
InsertColumnsRaw()
);
}
static LootdropEntries NewEntity()
{
LootdropEntries entry{};
entry.lootdrop_id = 0;
entry.item_id = 0;
entry.item_charges = 1;
entry.equip_item = 0;
entry.chance = 1;
entry.disabled_chance = 0;
entry.minlevel = 0;
entry.maxlevel = 127;
entry.multiplier = 1;
return entry;
}
static LootdropEntries GetLootdropEntriesEntry(
const std::vector<LootdropEntries> &lootdrop_entriess,
int lootdrop_entries_id
)
{
for (auto &lootdrop_entries : lootdrop_entriess) {
if (lootdrop_entries.item_id == lootdrop_entries_id) {
return lootdrop_entries;
}
}
return NewEntity();
}
static LootdropEntries FindOne(
int lootdrop_entries_id
)
{
auto results = content_db.QueryDatabase(
fmt::format(
"{} WHERE id = {} LIMIT 1",
BaseSelect(),
lootdrop_entries_id
)
);
auto row = results.begin();
if (results.RowCount() == 1) {
LootdropEntries entry{};
entry.lootdrop_id = atoi(row[0]);
entry.item_id = atoi(row[1]);
entry.item_charges = atoi(row[2]);
entry.equip_item = atoi(row[3]);
entry.chance = atof(row[4]);
entry.disabled_chance = atof(row[5]);
entry.minlevel = atoi(row[6]);
entry.maxlevel = atoi(row[7]);
entry.multiplier = atoi(row[8]);
return entry;
}
return NewEntity();
}
static int DeleteOne(
int lootdrop_entries_id
)
{
auto results = content_db.QueryDatabase(
fmt::format(
"DELETE FROM {} WHERE {} = {}",
TableName(),
PrimaryKey(),
lootdrop_entries_id
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static int UpdateOne(
LootdropEntries lootdrop_entries_entry
)
{
std::vector<std::string> update_values;
auto columns = Columns();
update_values.push_back(columns[2] + " = " + std::to_string(lootdrop_entries_entry.item_charges));
update_values.push_back(columns[3] + " = " + std::to_string(lootdrop_entries_entry.equip_item));
update_values.push_back(columns[4] + " = " + std::to_string(lootdrop_entries_entry.chance));
update_values.push_back(columns[5] + " = " + std::to_string(lootdrop_entries_entry.disabled_chance));
update_values.push_back(columns[6] + " = " + std::to_string(lootdrop_entries_entry.minlevel));
update_values.push_back(columns[7] + " = " + std::to_string(lootdrop_entries_entry.maxlevel));
update_values.push_back(columns[8] + " = " + std::to_string(lootdrop_entries_entry.multiplier));
auto results = content_db.QueryDatabase(
fmt::format(
"UPDATE {} SET {} WHERE {} = {}",
TableName(),
implode(", ", update_values),
PrimaryKey(),
lootdrop_entries_entry.item_id
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static LootdropEntries InsertOne(
LootdropEntries lootdrop_entries_entry
)
{
std::vector<std::string> insert_values;
insert_values.push_back(std::to_string(lootdrop_entries_entry.item_charges));
insert_values.push_back(std::to_string(lootdrop_entries_entry.equip_item));
insert_values.push_back(std::to_string(lootdrop_entries_entry.chance));
insert_values.push_back(std::to_string(lootdrop_entries_entry.disabled_chance));
insert_values.push_back(std::to_string(lootdrop_entries_entry.minlevel));
insert_values.push_back(std::to_string(lootdrop_entries_entry.maxlevel));
insert_values.push_back(std::to_string(lootdrop_entries_entry.multiplier));
auto results = content_db.QueryDatabase(
fmt::format(
"{} VALUES ({})",
BaseInsert(),
implode(",", insert_values)
)
);
if (results.Success()) {
lootdrop_entries_entry.id = results.LastInsertedID();
return lootdrop_entries_entry;
}
lootdrop_entries_entry = LootdropEntriesRepository::NewEntity();
return lootdrop_entries_entry;
}
static int InsertMany(
std::vector<LootdropEntries> lootdrop_entries_entries
)
{
std::vector<std::string> insert_chunks;
for (auto &lootdrop_entries_entry: lootdrop_entries_entries) {
std::vector<std::string> insert_values;
insert_values.push_back(std::to_string(lootdrop_entries_entry.item_charges));
insert_values.push_back(std::to_string(lootdrop_entries_entry.equip_item));
insert_values.push_back(std::to_string(lootdrop_entries_entry.chance));
insert_values.push_back(std::to_string(lootdrop_entries_entry.disabled_chance));
insert_values.push_back(std::to_string(lootdrop_entries_entry.minlevel));
insert_values.push_back(std::to_string(lootdrop_entries_entry.maxlevel));
insert_values.push_back(std::to_string(lootdrop_entries_entry.multiplier));
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<LootdropEntries> All()
{
std::vector<LootdropEntries> all_entries;
auto results = content_db.QueryDatabase(
fmt::format(
"{}",
BaseSelect()
)
);
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row) {
LootdropEntries entry{};
entry.lootdrop_id = atoi(row[0]);
entry.item_id = atoi(row[1]);
entry.item_charges = atoi(row[2]);
entry.equip_item = atoi(row[3]);
entry.chance = atof(row[4]);
entry.disabled_chance = atof(row[5]);
entry.minlevel = atoi(row[6]);
entry.maxlevel = atoi(row[7]);
entry.multiplier = atoi(row[8]);
all_entries.push_back(entry);
}
return all_entries;
}
static std::vector<LootdropEntries> GetWhere(std::string where_filter)
{
std::vector<LootdropEntries> 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) {
LootdropEntries entry{};
entry.lootdrop_id = atoi(row[0]);
entry.item_id = atoi(row[1]);
entry.item_charges = atoi(row[2]);
entry.equip_item = atoi(row[3]);
entry.chance = atof(row[4]);
entry.disabled_chance = atof(row[5]);
entry.minlevel = atoi(row[6]);
entry.maxlevel = atoi(row[7]);
entry.multiplier = atoi(row[8]);
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
};