mirror of
https://github.com/EQEmu/Server.git
synced 2026-05-31 04:56:20 +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:
@@ -79,15 +79,15 @@ public:
|
||||
|
||||
static RuleSets NewEntity()
|
||||
{
|
||||
RuleSets entry{};
|
||||
RuleSets e{};
|
||||
|
||||
entry.ruleset_id = 0;
|
||||
entry.name = "";
|
||||
e.ruleset_id = 0;
|
||||
e.name = "";
|
||||
|
||||
return entry;
|
||||
return e;
|
||||
}
|
||||
|
||||
static RuleSets GetRuleSetsEntry(
|
||||
static RuleSets GetRuleSets(
|
||||
const std::vector<RuleSets> &rule_setss,
|
||||
int rule_sets_id
|
||||
)
|
||||
@@ -116,12 +116,12 @@ public:
|
||||
|
||||
auto row = results.begin();
|
||||
if (results.RowCount() == 1) {
|
||||
RuleSets entry{};
|
||||
RuleSets e{};
|
||||
|
||||
entry.ruleset_id = atoi(row[0]);
|
||||
entry.name = row[1] ? row[1] : "";
|
||||
e.ruleset_id = atoi(row[0]);
|
||||
e.name = row[1] ? row[1] : "";
|
||||
|
||||
return entry;
|
||||
return e;
|
||||
}
|
||||
|
||||
return NewEntity();
|
||||
@@ -146,22 +146,22 @@ public:
|
||||
|
||||
static int UpdateOne(
|
||||
Database& db,
|
||||
RuleSets rule_sets_entry
|
||||
const RuleSets &e
|
||||
)
|
||||
{
|
||||
std::vector<std::string> update_values;
|
||||
std::vector<std::string> v;
|
||||
|
||||
auto columns = Columns();
|
||||
|
||||
update_values.push_back(columns[1] + " = '" + Strings::Escape(rule_sets_entry.name) + "'");
|
||||
v.push_back(columns[1] + " = '" + Strings::Escape(e.name) + "'");
|
||||
|
||||
auto results = db.QueryDatabase(
|
||||
fmt::format(
|
||||
"UPDATE {} SET {} WHERE {} = {}",
|
||||
TableName(),
|
||||
Strings::Implode(", ", update_values),
|
||||
Strings::Implode(", ", v),
|
||||
PrimaryKey(),
|
||||
rule_sets_entry.ruleset_id
|
||||
e.ruleset_id
|
||||
)
|
||||
);
|
||||
|
||||
@@ -170,49 +170,49 @@ public:
|
||||
|
||||
static RuleSets InsertOne(
|
||||
Database& db,
|
||||
RuleSets rule_sets_entry
|
||||
RuleSets e
|
||||
)
|
||||
{
|
||||
std::vector<std::string> insert_values;
|
||||
std::vector<std::string> v;
|
||||
|
||||
insert_values.push_back(std::to_string(rule_sets_entry.ruleset_id));
|
||||
insert_values.push_back("'" + Strings::Escape(rule_sets_entry.name) + "'");
|
||||
v.push_back(std::to_string(e.ruleset_id));
|
||||
v.push_back("'" + Strings::Escape(e.name) + "'");
|
||||
|
||||
auto results = db.QueryDatabase(
|
||||
fmt::format(
|
||||
"{} VALUES ({})",
|
||||
BaseInsert(),
|
||||
Strings::Implode(",", insert_values)
|
||||
Strings::Implode(",", v)
|
||||
)
|
||||
);
|
||||
|
||||
if (results.Success()) {
|
||||
rule_sets_entry.ruleset_id = results.LastInsertedID();
|
||||
return rule_sets_entry;
|
||||
e.ruleset_id = results.LastInsertedID();
|
||||
return e;
|
||||
}
|
||||
|
||||
rule_sets_entry = NewEntity();
|
||||
e = NewEntity();
|
||||
|
||||
return rule_sets_entry;
|
||||
return e;
|
||||
}
|
||||
|
||||
static int InsertMany(
|
||||
Database& db,
|
||||
std::vector<RuleSets> rule_sets_entries
|
||||
const std::vector<RuleSets> &entries
|
||||
)
|
||||
{
|
||||
std::vector<std::string> insert_chunks;
|
||||
|
||||
for (auto &rule_sets_entry: rule_sets_entries) {
|
||||
std::vector<std::string> insert_values;
|
||||
for (auto &e: entries) {
|
||||
std::vector<std::string> v;
|
||||
|
||||
insert_values.push_back(std::to_string(rule_sets_entry.ruleset_id));
|
||||
insert_values.push_back("'" + Strings::Escape(rule_sets_entry.name) + "'");
|
||||
v.push_back(std::to_string(e.ruleset_id));
|
||||
v.push_back("'" + Strings::Escape(e.name) + "'");
|
||||
|
||||
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(
|
||||
@@ -239,18 +239,18 @@ public:
|
||||
all_entries.reserve(results.RowCount());
|
||||
|
||||
for (auto row = results.begin(); row != results.end(); ++row) {
|
||||
RuleSets entry{};
|
||||
RuleSets e{};
|
||||
|
||||
entry.ruleset_id = atoi(row[0]);
|
||||
entry.name = row[1] ? row[1] : "";
|
||||
e.ruleset_id = atoi(row[0]);
|
||||
e.name = row[1] ? row[1] : "";
|
||||
|
||||
all_entries.push_back(entry);
|
||||
all_entries.push_back(e);
|
||||
}
|
||||
|
||||
return all_entries;
|
||||
}
|
||||
|
||||
static std::vector<RuleSets> GetWhere(Database& db, std::string where_filter)
|
||||
static std::vector<RuleSets> GetWhere(Database& db, const std::string &where_filter)
|
||||
{
|
||||
std::vector<RuleSets> all_entries;
|
||||
|
||||
@@ -265,18 +265,18 @@ public:
|
||||
all_entries.reserve(results.RowCount());
|
||||
|
||||
for (auto row = results.begin(); row != results.end(); ++row) {
|
||||
RuleSets entry{};
|
||||
RuleSets e{};
|
||||
|
||||
entry.ruleset_id = atoi(row[0]);
|
||||
entry.name = row[1] ? row[1] : "";
|
||||
e.ruleset_id = atoi(row[0]);
|
||||
e.name = row[1] ? row[1] : "";
|
||||
|
||||
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(
|
||||
@@ -301,6 +301,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_RULE_SETS_REPOSITORY_H
|
||||
|
||||
Reference in New Issue
Block a user