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,404 +23,45 @@
#include "../database.h"
#include "../string_util.h"
#include "base/base_inventory_snapshots_repository.h"
class InventorySnapshotsRepository {
class InventorySnapshotsRepository: public BaseInventorySnapshotsRepository {
public:
struct InventorySnapshots {
int time_index;
int charid;
int slotid;
int itemid;
int16 charges;
int color;
int augslot1;
int augslot2;
int augslot3;
int augslot4;
int augslot5;
int augslot6;
int8 instnodrop;
std::string custom_data;
int ornamenticon;
int ornamentidfile;
int ornament_hero_model;
};
static std::string PrimaryKey()
{
return std::string("slotid");
}
/**
* 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
*
* InventorySnapshotsRepository::GetByZoneAndVersion(int zone_id, int zone_version)
* InventorySnapshotsRepository::GetWhereNeverExpires()
* InventorySnapshotsRepository::GetWhereXAndY()
* InventorySnapshotsRepository::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 {
"time_index",
"charid",
"slotid",
"itemid",
"charges",
"color",
"augslot1",
"augslot2",
"augslot3",
"augslot4",
"augslot5",
"augslot6",
"instnodrop",
"custom_data",
"ornamenticon",
"ornamentidfile",
"ornament_hero_model",
};
}
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("inventory_snapshots");
}
static std::string BaseSelect()
{
return fmt::format(
"SELECT {} FROM {}",
ColumnsRaw(),
TableName()
);
}
static std::string BaseInsert()
{
return fmt::format(
"INSERT INTO {} ({}) ",
TableName(),
InsertColumnsRaw()
);
}
static InventorySnapshots NewEntity()
{
InventorySnapshots entry{};
entry.time_index = 0;
entry.charid = 0;
entry.slotid = 0;
entry.itemid = 0;
entry.charges = 0;
entry.color = 0;
entry.augslot1 = 0;
entry.augslot2 = 0;
entry.augslot3 = 0;
entry.augslot4 = 0;
entry.augslot5 = 0;
entry.augslot6 = 0;
entry.instnodrop = 0;
entry.custom_data = "";
entry.ornamenticon = 0;
entry.ornamentidfile = 0;
entry.ornament_hero_model = 0;
return entry;
}
static InventorySnapshots GetInventorySnapshotsEntry(
const std::vector<InventorySnapshots> &inventory_snapshotss,
int inventory_snapshots_id
)
{
for (auto &inventory_snapshots : inventory_snapshotss) {
if (inventory_snapshots.slotid == inventory_snapshots_id) {
return inventory_snapshots;
}
}
return NewEntity();
}
static InventorySnapshots FindOne(
int inventory_snapshots_id
)
{
auto results = database.QueryDatabase(
fmt::format(
"{} WHERE id = {} LIMIT 1",
BaseSelect(),
inventory_snapshots_id
)
);
auto row = results.begin();
if (results.RowCount() == 1) {
InventorySnapshots entry{};
entry.time_index = atoi(row[0]);
entry.charid = atoi(row[1]);
entry.slotid = atoi(row[2]);
entry.itemid = atoi(row[3]);
entry.charges = atoi(row[4]);
entry.color = atoi(row[5]);
entry.augslot1 = atoi(row[6]);
entry.augslot2 = atoi(row[7]);
entry.augslot3 = atoi(row[8]);
entry.augslot4 = atoi(row[9]);
entry.augslot5 = atoi(row[10]);
entry.augslot6 = atoi(row[11]);
entry.instnodrop = atoi(row[12]);
entry.custom_data = row[13] ? row[13] : "";
entry.ornamenticon = atoi(row[14]);
entry.ornamentidfile = atoi(row[15]);
entry.ornament_hero_model = atoi(row[16]);
return entry;
}
return NewEntity();
}
static int DeleteOne(
int inventory_snapshots_id
)
{
auto results = database.QueryDatabase(
fmt::format(
"DELETE FROM {} WHERE {} = {}",
TableName(),
PrimaryKey(),
inventory_snapshots_id
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static int UpdateOne(
InventorySnapshots inventory_snapshots_entry
)
{
std::vector<std::string> update_values;
auto columns = Columns();
update_values.push_back(columns[3] + " = " + std::to_string(inventory_snapshots_entry.itemid));
update_values.push_back(columns[4] + " = " + std::to_string(inventory_snapshots_entry.charges));
update_values.push_back(columns[5] + " = " + std::to_string(inventory_snapshots_entry.color));
update_values.push_back(columns[6] + " = " + std::to_string(inventory_snapshots_entry.augslot1));
update_values.push_back(columns[7] + " = " + std::to_string(inventory_snapshots_entry.augslot2));
update_values.push_back(columns[8] + " = " + std::to_string(inventory_snapshots_entry.augslot3));
update_values.push_back(columns[9] + " = " + std::to_string(inventory_snapshots_entry.augslot4));
update_values.push_back(columns[10] + " = " + std::to_string(inventory_snapshots_entry.augslot5));
update_values.push_back(columns[11] + " = " + std::to_string(inventory_snapshots_entry.augslot6));
update_values.push_back(columns[12] + " = " + std::to_string(inventory_snapshots_entry.instnodrop));
update_values.push_back(columns[13] + " = '" + EscapeString(inventory_snapshots_entry.custom_data) + "'");
update_values.push_back(columns[14] + " = " + std::to_string(inventory_snapshots_entry.ornamenticon));
update_values.push_back(columns[15] + " = " + std::to_string(inventory_snapshots_entry.ornamentidfile));
update_values.push_back(columns[16] + " = " + std::to_string(inventory_snapshots_entry.ornament_hero_model));
auto results = database.QueryDatabase(
fmt::format(
"UPDATE {} SET {} WHERE {} = {}",
TableName(),
implode(", ", update_values),
PrimaryKey(),
inventory_snapshots_entry.slotid
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static InventorySnapshots InsertOne(
InventorySnapshots inventory_snapshots_entry
)
{
std::vector<std::string> insert_values;
insert_values.push_back(std::to_string(inventory_snapshots_entry.itemid));
insert_values.push_back(std::to_string(inventory_snapshots_entry.charges));
insert_values.push_back(std::to_string(inventory_snapshots_entry.color));
insert_values.push_back(std::to_string(inventory_snapshots_entry.augslot1));
insert_values.push_back(std::to_string(inventory_snapshots_entry.augslot2));
insert_values.push_back(std::to_string(inventory_snapshots_entry.augslot3));
insert_values.push_back(std::to_string(inventory_snapshots_entry.augslot4));
insert_values.push_back(std::to_string(inventory_snapshots_entry.augslot5));
insert_values.push_back(std::to_string(inventory_snapshots_entry.augslot6));
insert_values.push_back(std::to_string(inventory_snapshots_entry.instnodrop));
insert_values.push_back("'" + EscapeString(inventory_snapshots_entry.custom_data) + "'");
insert_values.push_back(std::to_string(inventory_snapshots_entry.ornamenticon));
insert_values.push_back(std::to_string(inventory_snapshots_entry.ornamentidfile));
insert_values.push_back(std::to_string(inventory_snapshots_entry.ornament_hero_model));
auto results = database.QueryDatabase(
fmt::format(
"{} VALUES ({})",
BaseInsert(),
implode(",", insert_values)
)
);
if (results.Success()) {
inventory_snapshots_entry.id = results.LastInsertedID();
return inventory_snapshots_entry;
}
inventory_snapshots_entry = InventorySnapshotsRepository::NewEntity();
return inventory_snapshots_entry;
}
static int InsertMany(
std::vector<InventorySnapshots> inventory_snapshots_entries
)
{
std::vector<std::string> insert_chunks;
for (auto &inventory_snapshots_entry: inventory_snapshots_entries) {
std::vector<std::string> insert_values;
insert_values.push_back(std::to_string(inventory_snapshots_entry.itemid));
insert_values.push_back(std::to_string(inventory_snapshots_entry.charges));
insert_values.push_back(std::to_string(inventory_snapshots_entry.color));
insert_values.push_back(std::to_string(inventory_snapshots_entry.augslot1));
insert_values.push_back(std::to_string(inventory_snapshots_entry.augslot2));
insert_values.push_back(std::to_string(inventory_snapshots_entry.augslot3));
insert_values.push_back(std::to_string(inventory_snapshots_entry.augslot4));
insert_values.push_back(std::to_string(inventory_snapshots_entry.augslot5));
insert_values.push_back(std::to_string(inventory_snapshots_entry.augslot6));
insert_values.push_back(std::to_string(inventory_snapshots_entry.instnodrop));
insert_values.push_back("'" + EscapeString(inventory_snapshots_entry.custom_data) + "'");
insert_values.push_back(std::to_string(inventory_snapshots_entry.ornamenticon));
insert_values.push_back(std::to_string(inventory_snapshots_entry.ornamentidfile));
insert_values.push_back(std::to_string(inventory_snapshots_entry.ornament_hero_model));
insert_chunks.push_back("(" + implode(",", insert_values) + ")");
}
std::vector<std::string> insert_values;
auto results = database.QueryDatabase(
fmt::format(
"{} VALUES {}",
BaseInsert(),
implode(",", insert_chunks)
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static std::vector<InventorySnapshots> All()
{
std::vector<InventorySnapshots> all_entries;
auto results = database.QueryDatabase(
fmt::format(
"{}",
BaseSelect()
)
);
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row) {
InventorySnapshots entry{};
entry.time_index = atoi(row[0]);
entry.charid = atoi(row[1]);
entry.slotid = atoi(row[2]);
entry.itemid = atoi(row[3]);
entry.charges = atoi(row[4]);
entry.color = atoi(row[5]);
entry.augslot1 = atoi(row[6]);
entry.augslot2 = atoi(row[7]);
entry.augslot3 = atoi(row[8]);
entry.augslot4 = atoi(row[9]);
entry.augslot5 = atoi(row[10]);
entry.augslot6 = atoi(row[11]);
entry.instnodrop = atoi(row[12]);
entry.custom_data = row[13] ? row[13] : "";
entry.ornamenticon = atoi(row[14]);
entry.ornamentidfile = atoi(row[15]);
entry.ornament_hero_model = atoi(row[16]);
all_entries.push_back(entry);
}
return all_entries;
}
static std::vector<InventorySnapshots> GetWhere(std::string where_filter)
{
std::vector<InventorySnapshots> all_entries;
auto results = database.QueryDatabase(
fmt::format(
"{} WHERE {}",
BaseSelect(),
where_filter
)
);
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row) {
InventorySnapshots entry{};
entry.time_index = atoi(row[0]);
entry.charid = atoi(row[1]);
entry.slotid = atoi(row[2]);
entry.itemid = atoi(row[3]);
entry.charges = atoi(row[4]);
entry.color = atoi(row[5]);
entry.augslot1 = atoi(row[6]);
entry.augslot2 = atoi(row[7]);
entry.augslot3 = atoi(row[8]);
entry.augslot4 = atoi(row[9]);
entry.augslot5 = atoi(row[10]);
entry.augslot6 = atoi(row[11]);
entry.instnodrop = atoi(row[12]);
entry.custom_data = row[13] ? row[13] : "";
entry.ornamenticon = atoi(row[14]);
entry.ornamentidfile = atoi(row[15]);
entry.ornament_hero_model = atoi(row[16]);
all_entries.push_back(entry);
}
return all_entries;
}
static int DeleteWhere(std::string where_filter)
{
auto results = database.QueryDatabase(
fmt::format(
"DELETE FROM {} WHERE {}",
TableName(),
PrimaryKey(),
where_filter
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
// Custom extended repository methods here
};