[Logs] Bulk insert new log settings (#3810)

This commit is contained in:
Chris Miles 2023-12-27 20:04:01 -06:00 committed by GitHub
parent 8bedcd8751
commit d31cb09214
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 9 deletions

View File

@ -651,6 +651,9 @@ EQEmuLogSys *EQEmuLogSys::LoadLogDatabaseSettings()
}
// Auto inject categories that don't exist in the database...
std::vector<LogsysCategoriesRepository::LogsysCategories> db_categories_to_add{};
for (int i = Logs::AA; i != Logs::MaxCategoryID; i++) {
bool is_missing_in_database = std::find(db_categories.begin(), db_categories.end(), i) == db_categories.end();
@ -665,11 +668,7 @@ EQEmuLogSys *EQEmuLogSys::LoadLogDatabaseSettings()
}
if (is_missing_in_database && !is_deprecated_category) {
LogInfo(
"Automatically adding new log category [{}] ({})",
Logs::LogCategoryName[i],
i
);
LogInfo("Automatically adding new log category [{}] ({})", Logs::LogCategoryName[i], i);
auto new_category = LogsysCategoriesRepository::NewEntity();
new_category.log_category_id = i;
@ -678,11 +677,16 @@ EQEmuLogSys *EQEmuLogSys::LoadLogDatabaseSettings()
new_category.log_to_gmsay = log_settings[i].log_to_gmsay;
new_category.log_to_file = log_settings[i].log_to_file;
new_category.log_to_discord = log_settings[i].log_to_discord;
LogsysCategoriesRepository::InsertOne(*m_database, new_category);
db_categories_to_add.emplace_back(new_category);
}
}
if (!db_categories_to_add.empty()) {
LogsysCategoriesRepository::ReplaceMany(*m_database, db_categories_to_add);
LoadLogDatabaseSettings();
return this;
}
LogInfo("Loaded [{}] log categories", categories.size());
auto webhooks = DiscordWebhooksRepository::GetWhere(*m_database, fmt::format("id < {}", MAX_DISCORD_WEBHOOK_ID));

View File

@ -6,7 +6,7 @@
* Any modifications to base repositories are to be made by the generator only
*
* @generator ./utils/scripts/generators/repository-generator.pl
* @docs https://eqemu.gitbook.io/server/in-development/developer-area/repositories
* @docs https://docs.eqemu.io/developer/repositories
*/
#ifndef EQEMU_BASE_LOGSYS_CATEGORIES_REPOSITORY_H
@ -16,6 +16,7 @@
#include "../../strings.h"
#include <ctime>
class BaseLogsysCategoriesRepository {
public:
struct LogsysCategories {
@ -128,8 +129,9 @@ public:
{
auto results = db.QueryDatabase(
fmt::format(
"{} WHERE id = {} LIMIT 1",
"{} WHERE {} = {} LIMIT 1",
BaseSelect(),
PrimaryKey(),
logsys_categories_id
)
);
@ -378,6 +380,74 @@ public:
return (results.Success() && results.begin()[0] ? strtoll(results.begin()[0], nullptr, 10) : 0);
}
static std::string BaseReplace()
{
return fmt::format(
"REPLACE INTO {} ({}) ",
TableName(),
ColumnsRaw()
);
}
static int ReplaceOne(
Database& db,
const LogsysCategories &e
)
{
std::vector<std::string> v;
v.push_back(std::to_string(e.log_category_id));
v.push_back("'" + Strings::Escape(e.log_category_description) + "'");
v.push_back(std::to_string(e.log_to_console));
v.push_back(std::to_string(e.log_to_file));
v.push_back(std::to_string(e.log_to_gmsay));
v.push_back(std::to_string(e.log_to_discord));
v.push_back(std::to_string(e.discord_webhook_id));
auto results = db.QueryDatabase(
fmt::format(
"{} VALUES ({})",
BaseReplace(),
Strings::Implode(",", v)
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
static int ReplaceMany(
Database& db,
const std::vector<LogsysCategories> &entries
)
{
std::vector<std::string> insert_chunks;
for (auto &e: entries) {
std::vector<std::string> v;
v.push_back(std::to_string(e.log_category_id));
v.push_back("'" + Strings::Escape(e.log_category_description) + "'");
v.push_back(std::to_string(e.log_to_console));
v.push_back(std::to_string(e.log_to_file));
v.push_back(std::to_string(e.log_to_gmsay));
v.push_back(std::to_string(e.log_to_discord));
v.push_back(std::to_string(e.discord_webhook_id));
insert_chunks.push_back("(" + Strings::Implode(",", v) + ")");
}
std::vector<std::string> v;
auto results = db.QueryDatabase(
fmt::format(
"{} VALUES {}",
BaseReplace(),
Strings::Implode(",", insert_chunks)
)
);
return (results.Success() ? results.RowsAffected() : 0);
}
};
#endif //EQEMU_BASE_LOGSYS_CATEGORIES_REPOSITORY_H