mirror of
https://github.com/EQEmu/Server.git
synced 2026-05-31 00:46:46 +00:00
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:
@@ -23,383 +23,45 @@
|
||||
|
||||
#include "../database.h"
|
||||
#include "../string_util.h"
|
||||
#include "base/base_ground_spawns_repository.h"
|
||||
|
||||
class GroundSpawnsRepository {
|
||||
class GroundSpawnsRepository: public BaseGroundSpawnsRepository {
|
||||
public:
|
||||
struct GroundSpawns {
|
||||
int id;
|
||||
int zoneid;
|
||||
int16 version;
|
||||
float max_x;
|
||||
float max_y;
|
||||
float max_z;
|
||||
float min_x;
|
||||
float min_y;
|
||||
float heading;
|
||||
std::string name;
|
||||
int item;
|
||||
int max_allowed;
|
||||
std::string comment;
|
||||
int respawn_timer;
|
||||
};
|
||||
|
||||
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
|
||||
*
|
||||
* GroundSpawnsRepository::GetByZoneAndVersion(int zone_id, int zone_version)
|
||||
* GroundSpawnsRepository::GetWhereNeverExpires()
|
||||
* GroundSpawnsRepository::GetWhereXAndY()
|
||||
* GroundSpawnsRepository::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",
|
||||
"zoneid",
|
||||
"version",
|
||||
"max_x",
|
||||
"max_y",
|
||||
"max_z",
|
||||
"min_x",
|
||||
"min_y",
|
||||
"heading",
|
||||
"name",
|
||||
"item",
|
||||
"max_allowed",
|
||||
"comment",
|
||||
"respawn_timer",
|
||||
};
|
||||
}
|
||||
|
||||
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("ground_spawns");
|
||||
}
|
||||
|
||||
static std::string BaseSelect()
|
||||
{
|
||||
return fmt::format(
|
||||
"SELECT {} FROM {}",
|
||||
ColumnsRaw(),
|
||||
TableName()
|
||||
);
|
||||
}
|
||||
|
||||
static std::string BaseInsert()
|
||||
{
|
||||
return fmt::format(
|
||||
"INSERT INTO {} ({}) ",
|
||||
TableName(),
|
||||
InsertColumnsRaw()
|
||||
);
|
||||
}
|
||||
|
||||
static GroundSpawns NewEntity()
|
||||
{
|
||||
GroundSpawns entry{};
|
||||
|
||||
entry.id = 0;
|
||||
entry.zoneid = 0;
|
||||
entry.version = 0;
|
||||
entry.max_x = 2000;
|
||||
entry.max_y = 2000;
|
||||
entry.max_z = 10000;
|
||||
entry.min_x = -2000;
|
||||
entry.min_y = -2000;
|
||||
entry.heading = 0;
|
||||
entry.name = "";
|
||||
entry.item = 0;
|
||||
entry.max_allowed = 1;
|
||||
entry.comment = "";
|
||||
entry.respawn_timer = 300;
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
static GroundSpawns GetGroundSpawnsEntry(
|
||||
const std::vector<GroundSpawns> &ground_spawnss,
|
||||
int ground_spawns_id
|
||||
)
|
||||
{
|
||||
for (auto &ground_spawns : ground_spawnss) {
|
||||
if (ground_spawns.id == ground_spawns_id) {
|
||||
return ground_spawns;
|
||||
}
|
||||
}
|
||||
|
||||
return NewEntity();
|
||||
}
|
||||
|
||||
static GroundSpawns FindOne(
|
||||
int ground_spawns_id
|
||||
)
|
||||
{
|
||||
auto results = content_db.QueryDatabase(
|
||||
fmt::format(
|
||||
"{} WHERE id = {} LIMIT 1",
|
||||
BaseSelect(),
|
||||
ground_spawns_id
|
||||
)
|
||||
);
|
||||
|
||||
auto row = results.begin();
|
||||
if (results.RowCount() == 1) {
|
||||
GroundSpawns entry{};
|
||||
|
||||
entry.id = atoi(row[0]);
|
||||
entry.zoneid = atoi(row[1]);
|
||||
entry.version = atoi(row[2]);
|
||||
entry.max_x = atof(row[3]);
|
||||
entry.max_y = atof(row[4]);
|
||||
entry.max_z = atof(row[5]);
|
||||
entry.min_x = atof(row[6]);
|
||||
entry.min_y = atof(row[7]);
|
||||
entry.heading = atof(row[8]);
|
||||
entry.name = row[9] ? row[9] : "";
|
||||
entry.item = atoi(row[10]);
|
||||
entry.max_allowed = atoi(row[11]);
|
||||
entry.comment = row[12] ? row[12] : "";
|
||||
entry.respawn_timer = atoi(row[13]);
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
return NewEntity();
|
||||
}
|
||||
|
||||
static int DeleteOne(
|
||||
int ground_spawns_id
|
||||
)
|
||||
{
|
||||
auto results = content_db.QueryDatabase(
|
||||
fmt::format(
|
||||
"DELETE FROM {} WHERE {} = {}",
|
||||
TableName(),
|
||||
PrimaryKey(),
|
||||
ground_spawns_id
|
||||
)
|
||||
);
|
||||
|
||||
return (results.Success() ? results.RowsAffected() : 0);
|
||||
}
|
||||
|
||||
static int UpdateOne(
|
||||
GroundSpawns ground_spawns_entry
|
||||
)
|
||||
{
|
||||
std::vector<std::string> update_values;
|
||||
|
||||
auto columns = Columns();
|
||||
|
||||
update_values.push_back(columns[1] + " = " + std::to_string(ground_spawns_entry.zoneid));
|
||||
update_values.push_back(columns[2] + " = " + std::to_string(ground_spawns_entry.version));
|
||||
update_values.push_back(columns[3] + " = " + std::to_string(ground_spawns_entry.max_x));
|
||||
update_values.push_back(columns[4] + " = " + std::to_string(ground_spawns_entry.max_y));
|
||||
update_values.push_back(columns[5] + " = " + std::to_string(ground_spawns_entry.max_z));
|
||||
update_values.push_back(columns[6] + " = " + std::to_string(ground_spawns_entry.min_x));
|
||||
update_values.push_back(columns[7] + " = " + std::to_string(ground_spawns_entry.min_y));
|
||||
update_values.push_back(columns[8] + " = " + std::to_string(ground_spawns_entry.heading));
|
||||
update_values.push_back(columns[9] + " = '" + EscapeString(ground_spawns_entry.name) + "'");
|
||||
update_values.push_back(columns[10] + " = " + std::to_string(ground_spawns_entry.item));
|
||||
update_values.push_back(columns[11] + " = " + std::to_string(ground_spawns_entry.max_allowed));
|
||||
update_values.push_back(columns[12] + " = '" + EscapeString(ground_spawns_entry.comment) + "'");
|
||||
update_values.push_back(columns[13] + " = " + std::to_string(ground_spawns_entry.respawn_timer));
|
||||
|
||||
auto results = content_db.QueryDatabase(
|
||||
fmt::format(
|
||||
"UPDATE {} SET {} WHERE {} = {}",
|
||||
TableName(),
|
||||
implode(", ", update_values),
|
||||
PrimaryKey(),
|
||||
ground_spawns_entry.id
|
||||
)
|
||||
);
|
||||
|
||||
return (results.Success() ? results.RowsAffected() : 0);
|
||||
}
|
||||
|
||||
static GroundSpawns InsertOne(
|
||||
GroundSpawns ground_spawns_entry
|
||||
)
|
||||
{
|
||||
std::vector<std::string> insert_values;
|
||||
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.zoneid));
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.version));
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.max_x));
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.max_y));
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.max_z));
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.min_x));
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.min_y));
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.heading));
|
||||
insert_values.push_back("'" + EscapeString(ground_spawns_entry.name) + "'");
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.item));
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.max_allowed));
|
||||
insert_values.push_back("'" + EscapeString(ground_spawns_entry.comment) + "'");
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.respawn_timer));
|
||||
|
||||
auto results = content_db.QueryDatabase(
|
||||
fmt::format(
|
||||
"{} VALUES ({})",
|
||||
BaseInsert(),
|
||||
implode(",", insert_values)
|
||||
)
|
||||
);
|
||||
|
||||
if (results.Success()) {
|
||||
ground_spawns_entry.id = results.LastInsertedID();
|
||||
return ground_spawns_entry;
|
||||
}
|
||||
|
||||
ground_spawns_entry = GroundSpawnsRepository::NewEntity();
|
||||
|
||||
return ground_spawns_entry;
|
||||
}
|
||||
|
||||
static int InsertMany(
|
||||
std::vector<GroundSpawns> ground_spawns_entries
|
||||
)
|
||||
{
|
||||
std::vector<std::string> insert_chunks;
|
||||
|
||||
for (auto &ground_spawns_entry: ground_spawns_entries) {
|
||||
std::vector<std::string> insert_values;
|
||||
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.zoneid));
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.version));
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.max_x));
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.max_y));
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.max_z));
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.min_x));
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.min_y));
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.heading));
|
||||
insert_values.push_back("'" + EscapeString(ground_spawns_entry.name) + "'");
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.item));
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.max_allowed));
|
||||
insert_values.push_back("'" + EscapeString(ground_spawns_entry.comment) + "'");
|
||||
insert_values.push_back(std::to_string(ground_spawns_entry.respawn_timer));
|
||||
|
||||
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<GroundSpawns> All()
|
||||
{
|
||||
std::vector<GroundSpawns> all_entries;
|
||||
|
||||
auto results = content_db.QueryDatabase(
|
||||
fmt::format(
|
||||
"{}",
|
||||
BaseSelect()
|
||||
)
|
||||
);
|
||||
|
||||
all_entries.reserve(results.RowCount());
|
||||
|
||||
for (auto row = results.begin(); row != results.end(); ++row) {
|
||||
GroundSpawns entry{};
|
||||
|
||||
entry.id = atoi(row[0]);
|
||||
entry.zoneid = atoi(row[1]);
|
||||
entry.version = atoi(row[2]);
|
||||
entry.max_x = atof(row[3]);
|
||||
entry.max_y = atof(row[4]);
|
||||
entry.max_z = atof(row[5]);
|
||||
entry.min_x = atof(row[6]);
|
||||
entry.min_y = atof(row[7]);
|
||||
entry.heading = atof(row[8]);
|
||||
entry.name = row[9] ? row[9] : "";
|
||||
entry.item = atoi(row[10]);
|
||||
entry.max_allowed = atoi(row[11]);
|
||||
entry.comment = row[12] ? row[12] : "";
|
||||
entry.respawn_timer = atoi(row[13]);
|
||||
|
||||
all_entries.push_back(entry);
|
||||
}
|
||||
|
||||
return all_entries;
|
||||
}
|
||||
|
||||
static std::vector<GroundSpawns> GetWhere(std::string where_filter)
|
||||
{
|
||||
std::vector<GroundSpawns> 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) {
|
||||
GroundSpawns entry{};
|
||||
|
||||
entry.id = atoi(row[0]);
|
||||
entry.zoneid = atoi(row[1]);
|
||||
entry.version = atoi(row[2]);
|
||||
entry.max_x = atof(row[3]);
|
||||
entry.max_y = atof(row[4]);
|
||||
entry.max_z = atof(row[5]);
|
||||
entry.min_x = atof(row[6]);
|
||||
entry.min_y = atof(row[7]);
|
||||
entry.heading = atof(row[8]);
|
||||
entry.name = row[9] ? row[9] : "";
|
||||
entry.item = atoi(row[10]);
|
||||
entry.max_allowed = atoi(row[11]);
|
||||
entry.comment = row[12] ? row[12] : "";
|
||||
entry.respawn_timer = atoi(row[13]);
|
||||
|
||||
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
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user