[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
@@ -103,23 +103,23 @@ public:
static Eventlog NewEntity()
{
Eventlog entry{};
Eventlog e{};
entry.id = 0;
entry.accountname = "";
entry.accountid = 0;
entry.status = 0;
entry.charname = "";
entry.target = "None";
entry.time = std::time(nullptr);
entry.descriptiontype = "";
entry.description = "";
entry.event_nid = 0;
e.id = 0;
e.accountname = "";
e.accountid = 0;
e.status = 0;
e.charname = "";
e.target = "None";
e.time = std::time(nullptr);
e.descriptiontype = "";
e.description = "";
e.event_nid = 0;
return entry;
return e;
}
static Eventlog GetEventlogEntry(
static Eventlog GetEventlog(
const std::vector<Eventlog> &eventlogs,
int eventlog_id
)
@@ -148,20 +148,20 @@ public:
auto row = results.begin();
if (results.RowCount() == 1) {
Eventlog entry{};
Eventlog e{};
entry.id = atoi(row[0]);
entry.accountname = row[1] ? row[1] : "";
entry.accountid = atoi(row[2]);
entry.status = atoi(row[3]);
entry.charname = row[4] ? row[4] : "";
entry.target = row[5] ? row[5] : "";
entry.time = row[6] ? row[6] : "";
entry.descriptiontype = row[7] ? row[7] : "";
entry.description = row[8] ? row[8] : "";
entry.event_nid = atoi(row[9]);
e.id = atoi(row[0]);
e.accountname = row[1] ? row[1] : "";
e.accountid = atoi(row[2]);
e.status = atoi(row[3]);
e.charname = row[4] ? row[4] : "";
e.target = row[5] ? row[5] : "";
e.time = row[6] ? row[6] : "";
e.descriptiontype = row[7] ? row[7] : "";
e.description = row[8] ? row[8] : "";
e.event_nid = atoi(row[9]);
return entry;
return e;
}
return NewEntity();
@@ -186,30 +186,30 @@ public:
static int UpdateOne(
Database& db,
Eventlog eventlog_entry
const Eventlog &e
)
{
std::vector<std::string> update_values;
std::vector<std::string> v;
auto columns = Columns();
update_values.push_back(columns[1] + " = '" + Strings::Escape(eventlog_entry.accountname) + "'");
update_values.push_back(columns[2] + " = " + std::to_string(eventlog_entry.accountid));
update_values.push_back(columns[3] + " = " + std::to_string(eventlog_entry.status));
update_values.push_back(columns[4] + " = '" + Strings::Escape(eventlog_entry.charname) + "'");
update_values.push_back(columns[5] + " = '" + Strings::Escape(eventlog_entry.target) + "'");
update_values.push_back(columns[6] + " = '" + Strings::Escape(eventlog_entry.time) + "'");
update_values.push_back(columns[7] + " = '" + Strings::Escape(eventlog_entry.descriptiontype) + "'");
update_values.push_back(columns[8] + " = '" + Strings::Escape(eventlog_entry.description) + "'");
update_values.push_back(columns[9] + " = " + std::to_string(eventlog_entry.event_nid));
v.push_back(columns[1] + " = '" + Strings::Escape(e.accountname) + "'");
v.push_back(columns[2] + " = " + std::to_string(e.accountid));
v.push_back(columns[3] + " = " + std::to_string(e.status));
v.push_back(columns[4] + " = '" + Strings::Escape(e.charname) + "'");
v.push_back(columns[5] + " = '" + Strings::Escape(e.target) + "'");
v.push_back(columns[6] + " = '" + Strings::Escape(e.time) + "'");
v.push_back(columns[7] + " = '" + Strings::Escape(e.descriptiontype) + "'");
v.push_back(columns[8] + " = '" + Strings::Escape(e.description) + "'");
v.push_back(columns[9] + " = " + std::to_string(e.event_nid));
auto results = db.QueryDatabase(
fmt::format(
"UPDATE {} SET {} WHERE {} = {}",
TableName(),
Strings::Implode(", ", update_values),
Strings::Implode(", ", v),
PrimaryKey(),
eventlog_entry.id
e.id
)
);
@@ -218,65 +218,65 @@ public:
static Eventlog InsertOne(
Database& db,
Eventlog eventlog_entry
Eventlog e
)
{
std::vector<std::string> insert_values;
std::vector<std::string> v;
insert_values.push_back(std::to_string(eventlog_entry.id));
insert_values.push_back("'" + Strings::Escape(eventlog_entry.accountname) + "'");
insert_values.push_back(std::to_string(eventlog_entry.accountid));
insert_values.push_back(std::to_string(eventlog_entry.status));
insert_values.push_back("'" + Strings::Escape(eventlog_entry.charname) + "'");
insert_values.push_back("'" + Strings::Escape(eventlog_entry.target) + "'");
insert_values.push_back("'" + Strings::Escape(eventlog_entry.time) + "'");
insert_values.push_back("'" + Strings::Escape(eventlog_entry.descriptiontype) + "'");
insert_values.push_back("'" + Strings::Escape(eventlog_entry.description) + "'");
insert_values.push_back(std::to_string(eventlog_entry.event_nid));
v.push_back(std::to_string(e.id));
v.push_back("'" + Strings::Escape(e.accountname) + "'");
v.push_back(std::to_string(e.accountid));
v.push_back(std::to_string(e.status));
v.push_back("'" + Strings::Escape(e.charname) + "'");
v.push_back("'" + Strings::Escape(e.target) + "'");
v.push_back("'" + Strings::Escape(e.time) + "'");
v.push_back("'" + Strings::Escape(e.descriptiontype) + "'");
v.push_back("'" + Strings::Escape(e.description) + "'");
v.push_back(std::to_string(e.event_nid));
auto results = db.QueryDatabase(
fmt::format(
"{} VALUES ({})",
BaseInsert(),
Strings::Implode(",", insert_values)
Strings::Implode(",", v)
)
);
if (results.Success()) {
eventlog_entry.id = results.LastInsertedID();
return eventlog_entry;
e.id = results.LastInsertedID();
return e;
}
eventlog_entry = NewEntity();
e = NewEntity();
return eventlog_entry;
return e;
}
static int InsertMany(
Database& db,
std::vector<Eventlog> eventlog_entries
const std::vector<Eventlog> &entries
)
{
std::vector<std::string> insert_chunks;
for (auto &eventlog_entry: eventlog_entries) {
std::vector<std::string> insert_values;
for (auto &e: entries) {
std::vector<std::string> v;
insert_values.push_back(std::to_string(eventlog_entry.id));
insert_values.push_back("'" + Strings::Escape(eventlog_entry.accountname) + "'");
insert_values.push_back(std::to_string(eventlog_entry.accountid));
insert_values.push_back(std::to_string(eventlog_entry.status));
insert_values.push_back("'" + Strings::Escape(eventlog_entry.charname) + "'");
insert_values.push_back("'" + Strings::Escape(eventlog_entry.target) + "'");
insert_values.push_back("'" + Strings::Escape(eventlog_entry.time) + "'");
insert_values.push_back("'" + Strings::Escape(eventlog_entry.descriptiontype) + "'");
insert_values.push_back("'" + Strings::Escape(eventlog_entry.description) + "'");
insert_values.push_back(std::to_string(eventlog_entry.event_nid));
v.push_back(std::to_string(e.id));
v.push_back("'" + Strings::Escape(e.accountname) + "'");
v.push_back(std::to_string(e.accountid));
v.push_back(std::to_string(e.status));
v.push_back("'" + Strings::Escape(e.charname) + "'");
v.push_back("'" + Strings::Escape(e.target) + "'");
v.push_back("'" + Strings::Escape(e.time) + "'");
v.push_back("'" + Strings::Escape(e.descriptiontype) + "'");
v.push_back("'" + Strings::Escape(e.description) + "'");
v.push_back(std::to_string(e.event_nid));
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(
@@ -303,26 +303,26 @@ public:
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row) {
Eventlog entry{};
Eventlog e{};
entry.id = atoi(row[0]);
entry.accountname = row[1] ? row[1] : "";
entry.accountid = atoi(row[2]);
entry.status = atoi(row[3]);
entry.charname = row[4] ? row[4] : "";
entry.target = row[5] ? row[5] : "";
entry.time = row[6] ? row[6] : "";
entry.descriptiontype = row[7] ? row[7] : "";
entry.description = row[8] ? row[8] : "";
entry.event_nid = atoi(row[9]);
e.id = atoi(row[0]);
e.accountname = row[1] ? row[1] : "";
e.accountid = atoi(row[2]);
e.status = atoi(row[3]);
e.charname = row[4] ? row[4] : "";
e.target = row[5] ? row[5] : "";
e.time = row[6] ? row[6] : "";
e.descriptiontype = row[7] ? row[7] : "";
e.description = row[8] ? row[8] : "";
e.event_nid = atoi(row[9]);
all_entries.push_back(entry);
all_entries.push_back(e);
}
return all_entries;
}
static std::vector<Eventlog> GetWhere(Database& db, std::string where_filter)
static std::vector<Eventlog> GetWhere(Database& db, const std::string &where_filter)
{
std::vector<Eventlog> all_entries;
@@ -337,26 +337,26 @@ public:
all_entries.reserve(results.RowCount());
for (auto row = results.begin(); row != results.end(); ++row) {
Eventlog entry{};
Eventlog e{};
entry.id = atoi(row[0]);
entry.accountname = row[1] ? row[1] : "";
entry.accountid = atoi(row[2]);
entry.status = atoi(row[3]);
entry.charname = row[4] ? row[4] : "";
entry.target = row[5] ? row[5] : "";
entry.time = row[6] ? row[6] : "";
entry.descriptiontype = row[7] ? row[7] : "";
entry.description = row[8] ? row[8] : "";
entry.event_nid = atoi(row[9]);
e.id = atoi(row[0]);
e.accountname = row[1] ? row[1] : "";
e.accountid = atoi(row[2]);
e.status = atoi(row[3]);
e.charname = row[4] ? row[4] : "";
e.target = row[5] ? row[5] : "";
e.time = row[6] ? row[6] : "";
e.descriptiontype = row[7] ? row[7] : "";
e.description = row[8] ? row[8] : "";
e.event_nid = atoi(row[9]);
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(
@@ -381,6 +381,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_EVENTLOG_REPOSITORY_H