mirror of
https://github.com/EQEmu/Server.git
synced 2026-06-01 18:30:39 +00:00
[Repositories] Add GetMaxId, Count (#2371)
* [Repositories] Add GetMaxId, Count * Update cmake with repositories, regenerate extended repos * Remove license from files * Simplify receivers * Receiver simplify remaining * Simplify receivers final * Pass params by const reference * Modernize grid tables * Remove guild members since it doesn't conform as a generatable table * PR comment
This commit is contained in:
@@ -94,20 +94,20 @@ public:
|
||||
|
||||
static Horses NewEntity()
|
||||
{
|
||||
Horses entry{};
|
||||
Horses e{};
|
||||
|
||||
entry.id = 0;
|
||||
entry.filename = "";
|
||||
entry.race = 216;
|
||||
entry.gender = 0;
|
||||
entry.texture = 0;
|
||||
entry.mountspeed = 0.75;
|
||||
entry.notes = "Notes";
|
||||
e.id = 0;
|
||||
e.filename = "";
|
||||
e.race = 216;
|
||||
e.gender = 0;
|
||||
e.texture = 0;
|
||||
e.mountspeed = 0.75;
|
||||
e.notes = "Notes";
|
||||
|
||||
return entry;
|
||||
return e;
|
||||
}
|
||||
|
||||
static Horses GetHorsesEntry(
|
||||
static Horses GetHorses(
|
||||
const std::vector<Horses> &horsess,
|
||||
int horses_id
|
||||
)
|
||||
@@ -136,17 +136,17 @@ public:
|
||||
|
||||
auto row = results.begin();
|
||||
if (results.RowCount() == 1) {
|
||||
Horses entry{};
|
||||
Horses e{};
|
||||
|
||||
entry.id = atoi(row[0]);
|
||||
entry.filename = row[1] ? row[1] : "";
|
||||
entry.race = atoi(row[2]);
|
||||
entry.gender = atoi(row[3]);
|
||||
entry.texture = atoi(row[4]);
|
||||
entry.mountspeed = static_cast<float>(atof(row[5]));
|
||||
entry.notes = row[6] ? row[6] : "";
|
||||
e.id = atoi(row[0]);
|
||||
e.filename = row[1] ? row[1] : "";
|
||||
e.race = atoi(row[2]);
|
||||
e.gender = atoi(row[3]);
|
||||
e.texture = atoi(row[4]);
|
||||
e.mountspeed = static_cast<float>(atof(row[5]));
|
||||
e.notes = row[6] ? row[6] : "";
|
||||
|
||||
return entry;
|
||||
return e;
|
||||
}
|
||||
|
||||
return NewEntity();
|
||||
@@ -171,27 +171,27 @@ public:
|
||||
|
||||
static int UpdateOne(
|
||||
Database& db,
|
||||
Horses horses_entry
|
||||
const Horses &e
|
||||
)
|
||||
{
|
||||
std::vector<std::string> update_values;
|
||||
std::vector<std::string> v;
|
||||
|
||||
auto columns = Columns();
|
||||
|
||||
update_values.push_back(columns[1] + " = '" + Strings::Escape(horses_entry.filename) + "'");
|
||||
update_values.push_back(columns[2] + " = " + std::to_string(horses_entry.race));
|
||||
update_values.push_back(columns[3] + " = " + std::to_string(horses_entry.gender));
|
||||
update_values.push_back(columns[4] + " = " + std::to_string(horses_entry.texture));
|
||||
update_values.push_back(columns[5] + " = " + std::to_string(horses_entry.mountspeed));
|
||||
update_values.push_back(columns[6] + " = '" + Strings::Escape(horses_entry.notes) + "'");
|
||||
v.push_back(columns[1] + " = '" + Strings::Escape(e.filename) + "'");
|
||||
v.push_back(columns[2] + " = " + std::to_string(e.race));
|
||||
v.push_back(columns[3] + " = " + std::to_string(e.gender));
|
||||
v.push_back(columns[4] + " = " + std::to_string(e.texture));
|
||||
v.push_back(columns[5] + " = " + std::to_string(e.mountspeed));
|
||||
v.push_back(columns[6] + " = '" + Strings::Escape(e.notes) + "'");
|
||||
|
||||
auto results = db.QueryDatabase(
|
||||
fmt::format(
|
||||
"UPDATE {} SET {} WHERE {} = {}",
|
||||
TableName(),
|
||||
Strings::Implode(", ", update_values),
|
||||
Strings::Implode(", ", v),
|
||||
PrimaryKey(),
|
||||
horses_entry.id
|
||||
e.id
|
||||
)
|
||||
);
|
||||
|
||||
@@ -200,59 +200,59 @@ public:
|
||||
|
||||
static Horses InsertOne(
|
||||
Database& db,
|
||||
Horses horses_entry
|
||||
Horses e
|
||||
)
|
||||
{
|
||||
std::vector<std::string> insert_values;
|
||||
std::vector<std::string> v;
|
||||
|
||||
insert_values.push_back(std::to_string(horses_entry.id));
|
||||
insert_values.push_back("'" + Strings::Escape(horses_entry.filename) + "'");
|
||||
insert_values.push_back(std::to_string(horses_entry.race));
|
||||
insert_values.push_back(std::to_string(horses_entry.gender));
|
||||
insert_values.push_back(std::to_string(horses_entry.texture));
|
||||
insert_values.push_back(std::to_string(horses_entry.mountspeed));
|
||||
insert_values.push_back("'" + Strings::Escape(horses_entry.notes) + "'");
|
||||
v.push_back(std::to_string(e.id));
|
||||
v.push_back("'" + Strings::Escape(e.filename) + "'");
|
||||
v.push_back(std::to_string(e.race));
|
||||
v.push_back(std::to_string(e.gender));
|
||||
v.push_back(std::to_string(e.texture));
|
||||
v.push_back(std::to_string(e.mountspeed));
|
||||
v.push_back("'" + Strings::Escape(e.notes) + "'");
|
||||
|
||||
auto results = db.QueryDatabase(
|
||||
fmt::format(
|
||||
"{} VALUES ({})",
|
||||
BaseInsert(),
|
||||
Strings::Implode(",", insert_values)
|
||||
Strings::Implode(",", v)
|
||||
)
|
||||
);
|
||||
|
||||
if (results.Success()) {
|
||||
horses_entry.id = results.LastInsertedID();
|
||||
return horses_entry;
|
||||
e.id = results.LastInsertedID();
|
||||
return e;
|
||||
}
|
||||
|
||||
horses_entry = NewEntity();
|
||||
e = NewEntity();
|
||||
|
||||
return horses_entry;
|
||||
return e;
|
||||
}
|
||||
|
||||
static int InsertMany(
|
||||
Database& db,
|
||||
std::vector<Horses> horses_entries
|
||||
const std::vector<Horses> &entries
|
||||
)
|
||||
{
|
||||
std::vector<std::string> insert_chunks;
|
||||
|
||||
for (auto &horses_entry: horses_entries) {
|
||||
std::vector<std::string> insert_values;
|
||||
for (auto &e: entries) {
|
||||
std::vector<std::string> v;
|
||||
|
||||
insert_values.push_back(std::to_string(horses_entry.id));
|
||||
insert_values.push_back("'" + Strings::Escape(horses_entry.filename) + "'");
|
||||
insert_values.push_back(std::to_string(horses_entry.race));
|
||||
insert_values.push_back(std::to_string(horses_entry.gender));
|
||||
insert_values.push_back(std::to_string(horses_entry.texture));
|
||||
insert_values.push_back(std::to_string(horses_entry.mountspeed));
|
||||
insert_values.push_back("'" + Strings::Escape(horses_entry.notes) + "'");
|
||||
v.push_back(std::to_string(e.id));
|
||||
v.push_back("'" + Strings::Escape(e.filename) + "'");
|
||||
v.push_back(std::to_string(e.race));
|
||||
v.push_back(std::to_string(e.gender));
|
||||
v.push_back(std::to_string(e.texture));
|
||||
v.push_back(std::to_string(e.mountspeed));
|
||||
v.push_back("'" + Strings::Escape(e.notes) + "'");
|
||||
|
||||
insert_chunks.push_back("(" + Strings::Implode(",", insert_values) + ")");
|
||||
insert_chunks.push_back("(" + Strings::Implode(",", v) + ")");
|
||||
}
|
||||
|
||||
std::vector<std::string> insert_values;
|
||||
std::vector<std::string> v;
|
||||
|
||||
auto results = db.QueryDatabase(
|
||||
fmt::format(
|
||||
@@ -279,23 +279,23 @@ public:
|
||||
all_entries.reserve(results.RowCount());
|
||||
|
||||
for (auto row = results.begin(); row != results.end(); ++row) {
|
||||
Horses entry{};
|
||||
Horses e{};
|
||||
|
||||
entry.id = atoi(row[0]);
|
||||
entry.filename = row[1] ? row[1] : "";
|
||||
entry.race = atoi(row[2]);
|
||||
entry.gender = atoi(row[3]);
|
||||
entry.texture = atoi(row[4]);
|
||||
entry.mountspeed = static_cast<float>(atof(row[5]));
|
||||
entry.notes = row[6] ? row[6] : "";
|
||||
e.id = atoi(row[0]);
|
||||
e.filename = row[1] ? row[1] : "";
|
||||
e.race = atoi(row[2]);
|
||||
e.gender = atoi(row[3]);
|
||||
e.texture = atoi(row[4]);
|
||||
e.mountspeed = static_cast<float>(atof(row[5]));
|
||||
e.notes = row[6] ? row[6] : "";
|
||||
|
||||
all_entries.push_back(entry);
|
||||
all_entries.push_back(e);
|
||||
}
|
||||
|
||||
return all_entries;
|
||||
}
|
||||
|
||||
static std::vector<Horses> GetWhere(Database& db, std::string where_filter)
|
||||
static std::vector<Horses> GetWhere(Database& db, const std::string &where_filter)
|
||||
{
|
||||
std::vector<Horses> all_entries;
|
||||
|
||||
@@ -310,23 +310,23 @@ public:
|
||||
all_entries.reserve(results.RowCount());
|
||||
|
||||
for (auto row = results.begin(); row != results.end(); ++row) {
|
||||
Horses entry{};
|
||||
Horses e{};
|
||||
|
||||
entry.id = atoi(row[0]);
|
||||
entry.filename = row[1] ? row[1] : "";
|
||||
entry.race = atoi(row[2]);
|
||||
entry.gender = atoi(row[3]);
|
||||
entry.texture = atoi(row[4]);
|
||||
entry.mountspeed = static_cast<float>(atof(row[5]));
|
||||
entry.notes = row[6] ? row[6] : "";
|
||||
e.id = atoi(row[0]);
|
||||
e.filename = row[1] ? row[1] : "";
|
||||
e.race = atoi(row[2]);
|
||||
e.gender = atoi(row[3]);
|
||||
e.texture = atoi(row[4]);
|
||||
e.mountspeed = static_cast<float>(atof(row[5]));
|
||||
e.notes = row[6] ? row[6] : "";
|
||||
|
||||
all_entries.push_back(entry);
|
||||
all_entries.push_back(e);
|
||||
}
|
||||
|
||||
return all_entries;
|
||||
}
|
||||
|
||||
static int DeleteWhere(Database& db, std::string where_filter)
|
||||
static int DeleteWhere(Database& db, const std::string &where_filter)
|
||||
{
|
||||
auto results = db.QueryDatabase(
|
||||
fmt::format(
|
||||
@@ -351,6 +351,32 @@ public:
|
||||
return (results.Success() ? results.RowsAffected() : 0);
|
||||
}
|
||||
|
||||
static int64 GetMaxId(Database& db)
|
||||
{
|
||||
auto results = db.QueryDatabase(
|
||||
fmt::format(
|
||||
"SELECT COALESCE(MAX({}), 0) FROM {}",
|
||||
PrimaryKey(),
|
||||
TableName()
|
||||
)
|
||||
);
|
||||
|
||||
return (results.Success() && results.begin()[0] ? strtoll(results.begin()[0], nullptr, 10) : 0);
|
||||
}
|
||||
|
||||
static int64 Count(Database& db, const std::string &where_filter = "")
|
||||
{
|
||||
auto results = db.QueryDatabase(
|
||||
fmt::format(
|
||||
"SELECT COUNT(*) FROM {} {}",
|
||||
TableName(),
|
||||
(where_filter.empty() ? "" : "WHERE " + where_filter)
|
||||
)
|
||||
);
|
||||
|
||||
return (results.Success() && results.begin()[0] ? strtoll(results.begin()[0], nullptr, 10) : 0);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif //EQEMU_BASE_HORSES_REPOSITORY_H
|
||||
|
||||
Reference in New Issue
Block a user