mirror of
https://github.com/EQEmu/Server.git
synced 2026-05-31 00:46:46 +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:
@@ -76,14 +76,14 @@ public:
|
||||
|
||||
static {{TABLE_NAME_STRUCT}} NewEntity()
|
||||
{
|
||||
{{TABLE_NAME_STRUCT}} entry{};
|
||||
{{TABLE_NAME_STRUCT}} e{};
|
||||
|
||||
{{DEFAULT_ENTRIES}}
|
||||
|
||||
return entry;
|
||||
return e;
|
||||
}
|
||||
|
||||
static {{TABLE_NAME_STRUCT}} Get{{TABLE_NAME_STRUCT}}Entry(
|
||||
static {{TABLE_NAME_STRUCT}} Get{{TABLE_NAME_STRUCT}}(
|
||||
const std::vector<{{TABLE_NAME_STRUCT}}> &{{TABLE_NAME_VAR}}s,
|
||||
int {{TABLE_NAME_VAR}}_id
|
||||
)
|
||||
@@ -112,10 +112,10 @@ public:
|
||||
|
||||
auto row = results.begin();
|
||||
if (results.RowCount() == 1) {
|
||||
{{TABLE_NAME_STRUCT}} entry{};
|
||||
{{TABLE_NAME_STRUCT}} e{};
|
||||
|
||||
{{FIND_ONE_ENTRIES}}
|
||||
return entry;
|
||||
return e;
|
||||
}
|
||||
|
||||
return NewEntity();
|
||||
@@ -140,10 +140,10 @@ public:
|
||||
|
||||
static int UpdateOne(
|
||||
Database& db,
|
||||
{{TABLE_NAME_STRUCT}} {{TABLE_NAME_VAR}}_entry
|
||||
const {{TABLE_NAME_STRUCT}} &e
|
||||
)
|
||||
{
|
||||
std::vector<std::string> update_values;
|
||||
std::vector<std::string> v;
|
||||
|
||||
auto columns = Columns();
|
||||
|
||||
@@ -153,9 +153,9 @@ public:
|
||||
fmt::format(
|
||||
"UPDATE {} SET {} WHERE {} = {}",
|
||||
TableName(),
|
||||
Strings::Implode(", ", update_values),
|
||||
Strings::Implode(", ", v),
|
||||
PrimaryKey(),
|
||||
{{TABLE_NAME_VAR}}_entry.{{PRIMARY_KEY_STRING}}
|
||||
e.{{PRIMARY_KEY_STRING}}
|
||||
)
|
||||
);
|
||||
|
||||
@@ -164,10 +164,10 @@ public:
|
||||
|
||||
static {{TABLE_NAME_STRUCT}} InsertOne(
|
||||
Database& db,
|
||||
{{TABLE_NAME_STRUCT}} {{TABLE_NAME_VAR}}_entry
|
||||
{{TABLE_NAME_STRUCT}} e
|
||||
)
|
||||
{
|
||||
std::vector<std::string> insert_values;
|
||||
std::vector<std::string> v;
|
||||
|
||||
{{INSERT_ONE_ENTRIES}}
|
||||
|
||||
@@ -175,36 +175,36 @@ public:
|
||||
fmt::format(
|
||||
"{} VALUES ({})",
|
||||
BaseInsert(),
|
||||
Strings::Implode(",", insert_values)
|
||||
Strings::Implode(",", v)
|
||||
)
|
||||
);
|
||||
|
||||
if (results.Success()) {
|
||||
{{TABLE_NAME_VAR}}_entry.{{PRIMARY_KEY_STRING}} = results.LastInsertedID();
|
||||
return {{TABLE_NAME_VAR}}_entry;
|
||||
e.{{PRIMARY_KEY_STRING}} = results.LastInsertedID();
|
||||
return e;
|
||||
}
|
||||
|
||||
{{TABLE_NAME_VAR}}_entry = NewEntity();
|
||||
e = NewEntity();
|
||||
|
||||
return {{TABLE_NAME_VAR}}_entry;
|
||||
return e;
|
||||
}
|
||||
|
||||
static int InsertMany(
|
||||
Database& db,
|
||||
std::vector<{{TABLE_NAME_STRUCT}}> {{TABLE_NAME_VAR}}_entries
|
||||
const std::vector<{{TABLE_NAME_STRUCT}}> &entries
|
||||
)
|
||||
{
|
||||
std::vector<std::string> insert_chunks;
|
||||
|
||||
for (auto &{{TABLE_NAME_VAR}}_entry: {{TABLE_NAME_VAR}}_entries) {
|
||||
std::vector<std::string> insert_values;
|
||||
for (auto &e: entries) {
|
||||
std::vector<std::string> v;
|
||||
|
||||
{{INSERT_MANY_ENTRIES}}
|
||||
|
||||
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(
|
||||
@@ -231,17 +231,17 @@ public:
|
||||
all_entries.reserve(results.RowCount());
|
||||
|
||||
for (auto row = results.begin(); row != results.end(); ++row) {
|
||||
{{TABLE_NAME_STRUCT}} entry{};
|
||||
{{TABLE_NAME_STRUCT}} e{};
|
||||
|
||||
{{ALL_ENTRIES}}
|
||||
|
||||
all_entries.push_back(entry);
|
||||
all_entries.push_back(e);
|
||||
}
|
||||
|
||||
return all_entries;
|
||||
}
|
||||
|
||||
static std::vector<{{TABLE_NAME_STRUCT}}> GetWhere(Database& db, std::string where_filter)
|
||||
static std::vector<{{TABLE_NAME_STRUCT}}> GetWhere(Database& db, const std::string &where_filter)
|
||||
{
|
||||
std::vector<{{TABLE_NAME_STRUCT}}> all_entries;
|
||||
|
||||
@@ -256,17 +256,17 @@ public:
|
||||
all_entries.reserve(results.RowCount());
|
||||
|
||||
for (auto row = results.begin(); row != results.end(); ++row) {
|
||||
{{TABLE_NAME_STRUCT}} entry{};
|
||||
{{TABLE_NAME_STRUCT}} e{};
|
||||
|
||||
{{ALL_ENTRIES}}
|
||||
|
||||
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(
|
||||
@@ -291,6 +291,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_{{TABLE_NAME_UPPER}}_REPOSITORY_H
|
||||
|
||||
Reference in New Issue
Block a user