[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:
Chris Miles
2022-08-13 18:40:56 -05:00
committed by GitHub
parent b79e1947f1
commit 79285b1002
374 changed files with 26837 additions and 26020 deletions
@@ -82,16 +82,16 @@ public:
static CharacterSpells NewEntity()
{
CharacterSpells entry{};
CharacterSpells e{};
entry.id = 0;
entry.slot_id = 0;
entry.spell_id = 0;
e.id = 0;
e.slot_id = 0;
e.spell_id = 0;
return entry;
return e;
}
static CharacterSpells GetCharacterSpellsEntry(
static CharacterSpells GetCharacterSpells(
const std::vector<CharacterSpells> &character_spellss,
int character_spells_id
)
@@ -120,13 +120,13 @@ public:
auto row = results.begin();
if (results.RowCount() == 1) {
CharacterSpells entry{};
CharacterSpells e{};
entry.id = atoi(row[0]);
entry.slot_id = atoi(row[1]);
entry.spell_id = atoi(row[2]);
e.id = atoi(row[0]);
e.slot_id = atoi(row[1]);
e.spell_id = atoi(row[2]);
return entry;
return e;
}
return NewEntity();
@@ -151,23 +151,23 @@ public:
static int UpdateOne(
Database& db,
CharacterSpells character_spells_entry
const CharacterSpells &e
)
{
std::vector<std::string> update_values;
std::vector<std::string> v;
auto columns = Columns();
update_values.push_back(columns[1] + " = " + std::to_string(character_spells_entry.slot_id));
update_values.push_back(columns[2] + " = " + std::to_string(character_spells_entry.spell_id));
v.push_back(columns[1] + " = " + std::to_string(e.slot_id));
v.push_back(columns[2] + " = " + std::to_string(e.spell_id));
auto results = db.QueryDatabase(
fmt::format(
"UPDATE {} SET {} WHERE {} = {}",
TableName(),
Strings::Implode(", ", update_values),
Strings::Implode(", ", v),
PrimaryKey(),
character_spells_entry.id
e.id
)
);
@@ -176,51 +176,51 @@ public:
static CharacterSpells InsertOne(
Database& db,
CharacterSpells character_spells_entry
CharacterSpells e
)
{
std::vector<std::string> insert_values;
std::vector<std::string> v;
insert_values.push_back(std::to_string(character_spells_entry.id));
insert_values.push_back(std::to_string(character_spells_entry.slot_id));
insert_values.push_back(std::to_string(character_spells_entry.spell_id));
v.push_back(std::to_string(e.id));
v.push_back(std::to_string(e.slot_id));
v.push_back(std::to_string(e.spell_id));
auto results = db.QueryDatabase(
fmt::format(
"{} VALUES ({})",
BaseInsert(),
Strings::Implode(",", insert_values)
Strings::Implode(",", v)
)
);
if (results.Success()) {
character_spells_entry.id = results.LastInsertedID();
return character_spells_entry;
e.id = results.LastInsertedID();
return e;
}
character_spells_entry = NewEntity();
e = NewEntity();
return character_spells_entry;
return e;
}
static int InsertMany(
Database& db,
std::vector<CharacterSpells> character_spells_entries
const std::vector<CharacterSpells> &entries
)
{
std::vector<std::string> insert_chunks;
for (auto &character_spells_entry: character_spells_entries) {
std::vector<std::string> insert_values;
for (auto &e: entries) {
std::vector<std::string> v;
insert_values.push_back(std::to_string(character_spells_entry.id));
insert_values.push_back(std::to_string(character_spells_entry.slot_id));
insert_values.push_back(std::to_string(character_spells_entry.spell_id));
v.push_back(std::to_string(e.id));
v.push_back(std::to_string(e.slot_id));
v.push_back(std::to_string(e.spell_id));
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(
@@ -247,19 +247,19 @@ public:
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row) {
CharacterSpells entry{};
CharacterSpells e{};
entry.id = atoi(row[0]);
entry.slot_id = atoi(row[1]);
entry.spell_id = atoi(row[2]);
e.id = atoi(row[0]);
e.slot_id = atoi(row[1]);
e.spell_id = atoi(row[2]);
all_entries.push_back(entry);
all_entries.push_back(e);
}
return all_entries;
}
static std::vector<CharacterSpells> GetWhere(Database& db, std::string where_filter)
static std::vector<CharacterSpells> GetWhere(Database& db, const std::string &where_filter)
{
std::vector<CharacterSpells> all_entries;
@@ -274,19 +274,19 @@ public:
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row) {
CharacterSpells entry{};
CharacterSpells e{};
entry.id = atoi(row[0]);
entry.slot_id = atoi(row[1]);
entry.spell_id = atoi(row[2]);
e.id = atoi(row[0]);
e.slot_id = atoi(row[1]);
e.spell_id = atoi(row[2]);
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(
@@ -311,6 +311,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_CHARACTER_SPELLS_REPOSITORY_H