mirror of
https://github.com/EQEmu/Server.git
synced 2026-05-31 09:06: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,410 +23,45 @@
|
||||
|
||||
#include "../database.h"
|
||||
#include "../string_util.h"
|
||||
#include "base/base_zone_points_repository.h"
|
||||
|
||||
class ZonePointsRepository {
|
||||
class ZonePointsRepository: public BaseZonePointsRepository {
|
||||
public:
|
||||
struct ZonePoints {
|
||||
int id;
|
||||
std::string zone;
|
||||
int version;
|
||||
int16 number;
|
||||
float y;
|
||||
float x;
|
||||
float z;
|
||||
float heading;
|
||||
float target_y;
|
||||
float target_x;
|
||||
float target_z;
|
||||
float target_heading;
|
||||
int16 zoneinst;
|
||||
int target_zone_id;
|
||||
int target_instance;
|
||||
float buffer;
|
||||
int client_version_mask;
|
||||
};
|
||||
|
||||
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
|
||||
*
|
||||
* ZonePointsRepository::GetByZoneAndVersion(int zone_id, int zone_version)
|
||||
* ZonePointsRepository::GetWhereNeverExpires()
|
||||
* ZonePointsRepository::GetWhereXAndY()
|
||||
* ZonePointsRepository::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",
|
||||
"zone",
|
||||
"version",
|
||||
"number",
|
||||
"y",
|
||||
"x",
|
||||
"z",
|
||||
"heading",
|
||||
"target_y",
|
||||
"target_x",
|
||||
"target_z",
|
||||
"target_heading",
|
||||
"zoneinst",
|
||||
"target_zone_id",
|
||||
"target_instance",
|
||||
"buffer",
|
||||
"client_version_mask",
|
||||
};
|
||||
}
|
||||
|
||||
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("zone_points");
|
||||
}
|
||||
|
||||
static std::string BaseSelect()
|
||||
{
|
||||
return fmt::format(
|
||||
"SELECT {} FROM {}",
|
||||
ColumnsRaw(),
|
||||
TableName()
|
||||
);
|
||||
}
|
||||
|
||||
static std::string BaseInsert()
|
||||
{
|
||||
return fmt::format(
|
||||
"INSERT INTO {} ({}) ",
|
||||
TableName(),
|
||||
InsertColumnsRaw()
|
||||
);
|
||||
}
|
||||
|
||||
static ZonePoints NewEntity()
|
||||
{
|
||||
ZonePoints entry{};
|
||||
|
||||
entry.id = 0;
|
||||
entry.zone = "";
|
||||
entry.version = 0;
|
||||
entry.number = 1;
|
||||
entry.y = 0;
|
||||
entry.x = 0;
|
||||
entry.z = 0;
|
||||
entry.heading = 0;
|
||||
entry.target_y = 0;
|
||||
entry.target_x = 0;
|
||||
entry.target_z = 0;
|
||||
entry.target_heading = 0;
|
||||
entry.zoneinst = 0;
|
||||
entry.target_zone_id = 0;
|
||||
entry.target_instance = 0;
|
||||
entry.buffer = 0;
|
||||
entry.client_version_mask = 4294967295;
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
static ZonePoints GetZonePointsEntry(
|
||||
const std::vector<ZonePoints> &zone_pointss,
|
||||
int zone_points_id
|
||||
)
|
||||
{
|
||||
for (auto &zone_points : zone_pointss) {
|
||||
if (zone_points.id == zone_points_id) {
|
||||
return zone_points;
|
||||
}
|
||||
}
|
||||
|
||||
return NewEntity();
|
||||
}
|
||||
|
||||
static ZonePoints FindOne(
|
||||
int zone_points_id
|
||||
)
|
||||
{
|
||||
auto results = content_db.QueryDatabase(
|
||||
fmt::format(
|
||||
"{} WHERE id = {} LIMIT 1",
|
||||
BaseSelect(),
|
||||
zone_points_id
|
||||
)
|
||||
);
|
||||
|
||||
auto row = results.begin();
|
||||
if (results.RowCount() == 1) {
|
||||
ZonePoints entry{};
|
||||
|
||||
entry.id = atoi(row[0]);
|
||||
entry.zone = row[1] ? row[1] : "";
|
||||
entry.version = atoi(row[2]);
|
||||
entry.number = atoi(row[3]);
|
||||
entry.y = atof(row[4]);
|
||||
entry.x = atof(row[5]);
|
||||
entry.z = atof(row[6]);
|
||||
entry.heading = atof(row[7]);
|
||||
entry.target_y = atof(row[8]);
|
||||
entry.target_x = atof(row[9]);
|
||||
entry.target_z = atof(row[10]);
|
||||
entry.target_heading = atof(row[11]);
|
||||
entry.zoneinst = atoi(row[12]);
|
||||
entry.target_zone_id = atoi(row[13]);
|
||||
entry.target_instance = atoi(row[14]);
|
||||
entry.buffer = atof(row[15]);
|
||||
entry.client_version_mask = atoi(row[16]);
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
return NewEntity();
|
||||
}
|
||||
|
||||
static int DeleteOne(
|
||||
int zone_points_id
|
||||
)
|
||||
{
|
||||
auto results = content_db.QueryDatabase(
|
||||
fmt::format(
|
||||
"DELETE FROM {} WHERE {} = {}",
|
||||
TableName(),
|
||||
PrimaryKey(),
|
||||
zone_points_id
|
||||
)
|
||||
);
|
||||
|
||||
return (results.Success() ? results.RowsAffected() : 0);
|
||||
}
|
||||
|
||||
static int UpdateOne(
|
||||
ZonePoints zone_points_entry
|
||||
)
|
||||
{
|
||||
std::vector<std::string> update_values;
|
||||
|
||||
auto columns = Columns();
|
||||
|
||||
update_values.push_back(columns[1] + " = '" + EscapeString(zone_points_entry.zone) + "'");
|
||||
update_values.push_back(columns[2] + " = " + std::to_string(zone_points_entry.version));
|
||||
update_values.push_back(columns[3] + " = " + std::to_string(zone_points_entry.number));
|
||||
update_values.push_back(columns[4] + " = " + std::to_string(zone_points_entry.y));
|
||||
update_values.push_back(columns[5] + " = " + std::to_string(zone_points_entry.x));
|
||||
update_values.push_back(columns[6] + " = " + std::to_string(zone_points_entry.z));
|
||||
update_values.push_back(columns[7] + " = " + std::to_string(zone_points_entry.heading));
|
||||
update_values.push_back(columns[8] + " = " + std::to_string(zone_points_entry.target_y));
|
||||
update_values.push_back(columns[9] + " = " + std::to_string(zone_points_entry.target_x));
|
||||
update_values.push_back(columns[10] + " = " + std::to_string(zone_points_entry.target_z));
|
||||
update_values.push_back(columns[11] + " = " + std::to_string(zone_points_entry.target_heading));
|
||||
update_values.push_back(columns[12] + " = " + std::to_string(zone_points_entry.zoneinst));
|
||||
update_values.push_back(columns[13] + " = " + std::to_string(zone_points_entry.target_zone_id));
|
||||
update_values.push_back(columns[14] + " = " + std::to_string(zone_points_entry.target_instance));
|
||||
update_values.push_back(columns[15] + " = " + std::to_string(zone_points_entry.buffer));
|
||||
update_values.push_back(columns[16] + " = " + std::to_string(zone_points_entry.client_version_mask));
|
||||
|
||||
auto results = content_db.QueryDatabase(
|
||||
fmt::format(
|
||||
"UPDATE {} SET {} WHERE {} = {}",
|
||||
TableName(),
|
||||
implode(", ", update_values),
|
||||
PrimaryKey(),
|
||||
zone_points_entry.id
|
||||
)
|
||||
);
|
||||
|
||||
return (results.Success() ? results.RowsAffected() : 0);
|
||||
}
|
||||
|
||||
static ZonePoints InsertOne(
|
||||
ZonePoints zone_points_entry
|
||||
)
|
||||
{
|
||||
std::vector<std::string> insert_values;
|
||||
|
||||
insert_values.push_back("'" + EscapeString(zone_points_entry.zone) + "'");
|
||||
insert_values.push_back(std::to_string(zone_points_entry.version));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.number));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.y));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.x));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.z));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.heading));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.target_y));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.target_x));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.target_z));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.target_heading));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.zoneinst));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.target_zone_id));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.target_instance));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.buffer));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.client_version_mask));
|
||||
|
||||
auto results = content_db.QueryDatabase(
|
||||
fmt::format(
|
||||
"{} VALUES ({})",
|
||||
BaseInsert(),
|
||||
implode(",", insert_values)
|
||||
)
|
||||
);
|
||||
|
||||
if (results.Success()) {
|
||||
zone_points_entry.id = results.LastInsertedID();
|
||||
return zone_points_entry;
|
||||
}
|
||||
|
||||
zone_points_entry = ZonePointsRepository::NewEntity();
|
||||
|
||||
return zone_points_entry;
|
||||
}
|
||||
|
||||
static int InsertMany(
|
||||
std::vector<ZonePoints> zone_points_entries
|
||||
)
|
||||
{
|
||||
std::vector<std::string> insert_chunks;
|
||||
|
||||
for (auto &zone_points_entry: zone_points_entries) {
|
||||
std::vector<std::string> insert_values;
|
||||
|
||||
insert_values.push_back("'" + EscapeString(zone_points_entry.zone) + "'");
|
||||
insert_values.push_back(std::to_string(zone_points_entry.version));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.number));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.y));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.x));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.z));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.heading));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.target_y));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.target_x));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.target_z));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.target_heading));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.zoneinst));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.target_zone_id));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.target_instance));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.buffer));
|
||||
insert_values.push_back(std::to_string(zone_points_entry.client_version_mask));
|
||||
|
||||
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<ZonePoints> All()
|
||||
{
|
||||
std::vector<ZonePoints> all_entries;
|
||||
|
||||
auto results = content_db.QueryDatabase(
|
||||
fmt::format(
|
||||
"{}",
|
||||
BaseSelect()
|
||||
)
|
||||
);
|
||||
|
||||
all_entries.reserve(results.RowCount());
|
||||
|
||||
for (auto row = results.begin(); row != results.end(); ++row) {
|
||||
ZonePoints entry{};
|
||||
|
||||
entry.id = atoi(row[0]);
|
||||
entry.zone = row[1] ? row[1] : "";
|
||||
entry.version = atoi(row[2]);
|
||||
entry.number = atoi(row[3]);
|
||||
entry.y = atof(row[4]);
|
||||
entry.x = atof(row[5]);
|
||||
entry.z = atof(row[6]);
|
||||
entry.heading = atof(row[7]);
|
||||
entry.target_y = atof(row[8]);
|
||||
entry.target_x = atof(row[9]);
|
||||
entry.target_z = atof(row[10]);
|
||||
entry.target_heading = atof(row[11]);
|
||||
entry.zoneinst = atoi(row[12]);
|
||||
entry.target_zone_id = atoi(row[13]);
|
||||
entry.target_instance = atoi(row[14]);
|
||||
entry.buffer = atof(row[15]);
|
||||
entry.client_version_mask = atoi(row[16]);
|
||||
|
||||
all_entries.push_back(entry);
|
||||
}
|
||||
|
||||
return all_entries;
|
||||
}
|
||||
|
||||
static std::vector<ZonePoints> GetWhere(std::string where_filter)
|
||||
{
|
||||
std::vector<ZonePoints> 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) {
|
||||
ZonePoints entry{};
|
||||
|
||||
entry.id = atoi(row[0]);
|
||||
entry.zone = row[1] ? row[1] : "";
|
||||
entry.version = atoi(row[2]);
|
||||
entry.number = atoi(row[3]);
|
||||
entry.y = atof(row[4]);
|
||||
entry.x = atof(row[5]);
|
||||
entry.z = atof(row[6]);
|
||||
entry.heading = atof(row[7]);
|
||||
entry.target_y = atof(row[8]);
|
||||
entry.target_x = atof(row[9]);
|
||||
entry.target_z = atof(row[10]);
|
||||
entry.target_heading = atof(row[11]);
|
||||
entry.zoneinst = atoi(row[12]);
|
||||
entry.target_zone_id = atoi(row[13]);
|
||||
entry.target_instance = atoi(row[14]);
|
||||
entry.buffer = atof(row[15]);
|
||||
entry.client_version_mask = atoi(row[16]);
|
||||
|
||||
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