[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
@@ -91,19 +91,19 @@ public:
static Lootdrop NewEntity()
{
Lootdrop entry{};
Lootdrop e{};
entry.id = 0;
entry.name = "";
entry.min_expansion = -1;
entry.max_expansion = -1;
entry.content_flags = "";
entry.content_flags_disabled = "";
e.id = 0;
e.name = "";
e.min_expansion = -1;
e.max_expansion = -1;
e.content_flags = "";
e.content_flags_disabled = "";
return entry;
return e;
}
static Lootdrop GetLootdropEntry(
static Lootdrop GetLootdrop(
const std::vector<Lootdrop> &lootdrops,
int lootdrop_id
)
@@ -132,16 +132,16 @@ public:
auto row = results.begin();
if (results.RowCount() == 1) {
Lootdrop entry{};
Lootdrop e{};
entry.id = atoi(row[0]);
entry.name = row[1] ? row[1] : "";
entry.min_expansion = atoi(row[2]);
entry.max_expansion = atoi(row[3]);
entry.content_flags = row[4] ? row[4] : "";
entry.content_flags_disabled = row[5] ? row[5] : "";
e.id = atoi(row[0]);
e.name = row[1] ? row[1] : "";
e.min_expansion = atoi(row[2]);
e.max_expansion = atoi(row[3]);
e.content_flags = row[4] ? row[4] : "";
e.content_flags_disabled = row[5] ? row[5] : "";
return entry;
return e;
}
return NewEntity();
@@ -166,26 +166,26 @@ public:
static int UpdateOne(
Database& db,
Lootdrop lootdrop_entry
const Lootdrop &e
)
{
std::vector<std::string> update_values;
std::vector<std::string> v;
auto columns = Columns();
update_values.push_back(columns[1] + " = '" + Strings::Escape(lootdrop_entry.name) + "'");
update_values.push_back(columns[2] + " = " + std::to_string(lootdrop_entry.min_expansion));
update_values.push_back(columns[3] + " = " + std::to_string(lootdrop_entry.max_expansion));
update_values.push_back(columns[4] + " = '" + Strings::Escape(lootdrop_entry.content_flags) + "'");
update_values.push_back(columns[5] + " = '" + Strings::Escape(lootdrop_entry.content_flags_disabled) + "'");
v.push_back(columns[1] + " = '" + Strings::Escape(e.name) + "'");
v.push_back(columns[2] + " = " + std::to_string(e.min_expansion));
v.push_back(columns[3] + " = " + std::to_string(e.max_expansion));
v.push_back(columns[4] + " = '" + Strings::Escape(e.content_flags) + "'");
v.push_back(columns[5] + " = '" + Strings::Escape(e.content_flags_disabled) + "'");
auto results = db.QueryDatabase(
fmt::format(
"UPDATE {} SET {} WHERE {} = {}",
TableName(),
Strings::Implode(", ", update_values),
Strings::Implode(", ", v),
PrimaryKey(),
lootdrop_entry.id
e.id
)
);
@@ -194,57 +194,57 @@ public:
static Lootdrop InsertOne(
Database& db,
Lootdrop lootdrop_entry
Lootdrop e
)
{
std::vector<std::string> insert_values;
std::vector<std::string> v;
insert_values.push_back(std::to_string(lootdrop_entry.id));
insert_values.push_back("'" + Strings::Escape(lootdrop_entry.name) + "'");
insert_values.push_back(std::to_string(lootdrop_entry.min_expansion));
insert_values.push_back(std::to_string(lootdrop_entry.max_expansion));
insert_values.push_back("'" + Strings::Escape(lootdrop_entry.content_flags) + "'");
insert_values.push_back("'" + Strings::Escape(lootdrop_entry.content_flags_disabled) + "'");
v.push_back(std::to_string(e.id));
v.push_back("'" + Strings::Escape(e.name) + "'");
v.push_back(std::to_string(e.min_expansion));
v.push_back(std::to_string(e.max_expansion));
v.push_back("'" + Strings::Escape(e.content_flags) + "'");
v.push_back("'" + Strings::Escape(e.content_flags_disabled) + "'");
auto results = db.QueryDatabase(
fmt::format(
"{} VALUES ({})",
BaseInsert(),
Strings::Implode(",", insert_values)
Strings::Implode(",", v)
)
);
if (results.Success()) {
lootdrop_entry.id = results.LastInsertedID();
return lootdrop_entry;
e.id = results.LastInsertedID();
return e;
}
lootdrop_entry = NewEntity();
e = NewEntity();
return lootdrop_entry;
return e;
}
static int InsertMany(
Database& db,
std::vector<Lootdrop> lootdrop_entries
const std::vector<Lootdrop> &entries
)
{
std::vector<std::string> insert_chunks;
for (auto &lootdrop_entry: lootdrop_entries) {
std::vector<std::string> insert_values;
for (auto &e: entries) {
std::vector<std::string> v;
insert_values.push_back(std::to_string(lootdrop_entry.id));
insert_values.push_back("'" + Strings::Escape(lootdrop_entry.name) + "'");
insert_values.push_back(std::to_string(lootdrop_entry.min_expansion));
insert_values.push_back(std::to_string(lootdrop_entry.max_expansion));
insert_values.push_back("'" + Strings::Escape(lootdrop_entry.content_flags) + "'");
insert_values.push_back("'" + Strings::Escape(lootdrop_entry.content_flags_disabled) + "'");
v.push_back(std::to_string(e.id));
v.push_back("'" + Strings::Escape(e.name) + "'");
v.push_back(std::to_string(e.min_expansion));
v.push_back(std::to_string(e.max_expansion));
v.push_back("'" + Strings::Escape(e.content_flags) + "'");
v.push_back("'" + Strings::Escape(e.content_flags_disabled) + "'");
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(
@@ -271,22 +271,22 @@ public:
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row) {
Lootdrop entry{};
Lootdrop e{};
entry.id = atoi(row[0]);
entry.name = row[1] ? row[1] : "";
entry.min_expansion = atoi(row[2]);
entry.max_expansion = atoi(row[3]);
entry.content_flags = row[4] ? row[4] : "";
entry.content_flags_disabled = row[5] ? row[5] : "";
e.id = atoi(row[0]);
e.name = row[1] ? row[1] : "";
e.min_expansion = atoi(row[2]);
e.max_expansion = atoi(row[3]);
e.content_flags = row[4] ? row[4] : "";
e.content_flags_disabled = row[5] ? row[5] : "";
all_entries.push_back(entry);
all_entries.push_back(e);
}
return all_entries;
}
static std::vector<Lootdrop> GetWhere(Database& db, std::string where_filter)
static std::vector<Lootdrop> GetWhere(Database& db, const std::string &where_filter)
{
std::vector<Lootdrop> all_entries;
@@ -301,22 +301,22 @@ public:
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row) {
Lootdrop entry{};
Lootdrop e{};
entry.id = atoi(row[0]);
entry.name = row[1] ? row[1] : "";
entry.min_expansion = atoi(row[2]);
entry.max_expansion = atoi(row[3]);
entry.content_flags = row[4] ? row[4] : "";
entry.content_flags_disabled = row[5] ? row[5] : "";
e.id = atoi(row[0]);
e.name = row[1] ? row[1] : "";
e.min_expansion = atoi(row[2]);
e.max_expansion = atoi(row[3]);
e.content_flags = row[4] ? row[4] : "";
e.content_flags_disabled = row[5] ? row[5] : "";
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(
@@ -341,6 +341,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_LOOTDROP_REPOSITORY_H