mirror of
https://github.com/EQEmu/Server.git
synced 2026-09-05 15:36:38 +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:
@@ -1,29 +1,12 @@
|
||||
/**
|
||||
* EQEmulator: Everquest Server Emulator
|
||||
* Copyright (C) 2001-2020 EQEmulator Development Team (https://github.com/EQEmu/Server)
|
||||
* DO NOT MODIFY THIS FILE
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; version 2 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY except by those people which sell it, which
|
||||
* are required to give you total support for your newly bought product;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
||||
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* This repository was automatically generated and is NOT to be modified directly.
|
||||
* Any repository modifications are meant to be made to
|
||||
* the repository extending the base. Any modifications to base repositories are to
|
||||
* be made by the generator only
|
||||
* Any repository modifications are meant to be made to the repository extending the base.
|
||||
* 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
|
||||
*/
|
||||
|
||||
#ifndef EQEMU_BASE_GRID_REPOSITORY_H
|
||||
@@ -31,6 +14,7 @@
|
||||
|
||||
#include "../../database.h"
|
||||
#include "../../strings.h"
|
||||
#include <ctime>
|
||||
|
||||
class BaseGridRepository {
|
||||
public:
|
||||
@@ -56,24 +40,24 @@ public:
|
||||
};
|
||||
}
|
||||
|
||||
static std::vector<std::string> SelectColumns()
|
||||
{
|
||||
return {
|
||||
"id",
|
||||
"zoneid",
|
||||
"type",
|
||||
"type2",
|
||||
};
|
||||
}
|
||||
|
||||
static std::string ColumnsRaw()
|
||||
{
|
||||
return std::string(Strings::Implode(", ", Columns()));
|
||||
}
|
||||
|
||||
static std::string InsertColumnsRaw()
|
||||
static std::string SelectColumnsRaw()
|
||||
{
|
||||
std::vector<std::string> insert_columns;
|
||||
|
||||
for (auto &column : Columns()) {
|
||||
if (column == PrimaryKey()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
insert_columns.push_back(column);
|
||||
}
|
||||
|
||||
return std::string(Strings::Implode(", ", insert_columns));
|
||||
return std::string(Strings::Implode(", ", SelectColumns()));
|
||||
}
|
||||
|
||||
static std::string TableName()
|
||||
@@ -85,7 +69,7 @@ public:
|
||||
{
|
||||
return fmt::format(
|
||||
"SELECT {} FROM {}",
|
||||
ColumnsRaw(),
|
||||
SelectColumnsRaw(),
|
||||
TableName()
|
||||
);
|
||||
}
|
||||
@@ -95,23 +79,23 @@ public:
|
||||
return fmt::format(
|
||||
"INSERT INTO {} ({}) ",
|
||||
TableName(),
|
||||
InsertColumnsRaw()
|
||||
ColumnsRaw()
|
||||
);
|
||||
}
|
||||
|
||||
static Grid NewEntity()
|
||||
{
|
||||
Grid entry{};
|
||||
Grid e{};
|
||||
|
||||
entry.id = 0;
|
||||
entry.zoneid = 0;
|
||||
entry.type = 0;
|
||||
entry.type2 = 0;
|
||||
e.id = 0;
|
||||
e.zoneid = 0;
|
||||
e.type = 0;
|
||||
e.type2 = 0;
|
||||
|
||||
return entry;
|
||||
return e;
|
||||
}
|
||||
|
||||
static Grid GetGridEntry(
|
||||
static Grid GetGrid(
|
||||
const std::vector<Grid> &grids,
|
||||
int grid_id
|
||||
)
|
||||
@@ -126,10 +110,11 @@ public:
|
||||
}
|
||||
|
||||
static Grid FindOne(
|
||||
Database& db,
|
||||
int grid_id
|
||||
)
|
||||
{
|
||||
auto results = content_db.QueryDatabase(
|
||||
auto results = db.QueryDatabase(
|
||||
fmt::format(
|
||||
"{} WHERE id = {} LIMIT 1",
|
||||
BaseSelect(),
|
||||
@@ -139,24 +124,25 @@ public:
|
||||
|
||||
auto row = results.begin();
|
||||
if (results.RowCount() == 1) {
|
||||
Grid entry{};
|
||||
Grid e{};
|
||||
|
||||
entry.id = atoi(row[0]);
|
||||
entry.zoneid = atoi(row[1]);
|
||||
entry.type = atoi(row[2]);
|
||||
entry.type2 = atoi(row[3]);
|
||||
e.id = atoi(row[0]);
|
||||
e.zoneid = atoi(row[1]);
|
||||
e.type = atoi(row[2]);
|
||||
e.type2 = atoi(row[3]);
|
||||
|
||||
return entry;
|
||||
return e;
|
||||
}
|
||||
|
||||
return NewEntity();
|
||||
}
|
||||
|
||||
static int DeleteOne(
|
||||
Database& db,
|
||||
int grid_id
|
||||
)
|
||||
{
|
||||
auto results = content_db.QueryDatabase(
|
||||
auto results = db.QueryDatabase(
|
||||
fmt::format(
|
||||
"DELETE FROM {} WHERE {} = {}",
|
||||
TableName(),
|
||||
@@ -169,25 +155,26 @@ public:
|
||||
}
|
||||
|
||||
static int UpdateOne(
|
||||
Grid grid_entry
|
||||
Database& db,
|
||||
const Grid &e
|
||||
)
|
||||
{
|
||||
std::vector<std::string> update_values;
|
||||
std::vector<std::string> v;
|
||||
|
||||
auto columns = Columns();
|
||||
|
||||
update_values.push_back(columns[0] + " = " + std::to_string(grid_entry.id));
|
||||
update_values.push_back(columns[1] + " = " + std::to_string(grid_entry.zoneid));
|
||||
update_values.push_back(columns[2] + " = " + std::to_string(grid_entry.type));
|
||||
update_values.push_back(columns[3] + " = " + std::to_string(grid_entry.type2));
|
||||
v.push_back(columns[0] + " = " + std::to_string(e.id));
|
||||
v.push_back(columns[1] + " = " + std::to_string(e.zoneid));
|
||||
v.push_back(columns[2] + " = " + std::to_string(e.type));
|
||||
v.push_back(columns[3] + " = " + std::to_string(e.type2));
|
||||
|
||||
auto results = content_db.QueryDatabase(
|
||||
auto results = db.QueryDatabase(
|
||||
fmt::format(
|
||||
"UPDATE {} SET {} WHERE {} = {}",
|
||||
TableName(),
|
||||
Strings::Implode(", ", update_values),
|
||||
Strings::Implode(", ", v),
|
||||
PrimaryKey(),
|
||||
grid_entry.id
|
||||
e.id
|
||||
)
|
||||
);
|
||||
|
||||
@@ -195,54 +182,56 @@ public:
|
||||
}
|
||||
|
||||
static Grid InsertOne(
|
||||
Grid grid_entry
|
||||
Database& db,
|
||||
Grid e
|
||||
)
|
||||
{
|
||||
std::vector<std::string> insert_values;
|
||||
std::vector<std::string> v;
|
||||
|
||||
insert_values.push_back(std::to_string(grid_entry.id));
|
||||
insert_values.push_back(std::to_string(grid_entry.zoneid));
|
||||
insert_values.push_back(std::to_string(grid_entry.type));
|
||||
insert_values.push_back(std::to_string(grid_entry.type2));
|
||||
v.push_back(std::to_string(e.id));
|
||||
v.push_back(std::to_string(e.zoneid));
|
||||
v.push_back(std::to_string(e.type));
|
||||
v.push_back(std::to_string(e.type2));
|
||||
|
||||
auto results = content_db.QueryDatabase(
|
||||
auto results = db.QueryDatabase(
|
||||
fmt::format(
|
||||
"{} VALUES ({})",
|
||||
BaseInsert(),
|
||||
Strings::Implode(",", insert_values)
|
||||
Strings::Implode(",", v)
|
||||
)
|
||||
);
|
||||
|
||||
if (results.Success()) {
|
||||
grid_entry.id = results.LastInsertedID();
|
||||
return grid_entry;
|
||||
e.id = results.LastInsertedID();
|
||||
return e;
|
||||
}
|
||||
|
||||
grid_entry = NewEntity();
|
||||
e = NewEntity();
|
||||
|
||||
return grid_entry;
|
||||
return e;
|
||||
}
|
||||
|
||||
static int InsertMany(
|
||||
std::vector<Grid> grid_entries
|
||||
Database& db,
|
||||
const std::vector<Grid> &entries
|
||||
)
|
||||
{
|
||||
std::vector<std::string> insert_chunks;
|
||||
|
||||
for (auto &grid_entry: grid_entries) {
|
||||
std::vector<std::string> insert_values;
|
||||
for (auto &e: entries) {
|
||||
std::vector<std::string> v;
|
||||
|
||||
insert_values.push_back(std::to_string(grid_entry.id));
|
||||
insert_values.push_back(std::to_string(grid_entry.zoneid));
|
||||
insert_values.push_back(std::to_string(grid_entry.type));
|
||||
insert_values.push_back(std::to_string(grid_entry.type2));
|
||||
v.push_back(std::to_string(e.id));
|
||||
v.push_back(std::to_string(e.zoneid));
|
||||
v.push_back(std::to_string(e.type));
|
||||
v.push_back(std::to_string(e.type2));
|
||||
|
||||
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 = content_db.QueryDatabase(
|
||||
auto results = db.QueryDatabase(
|
||||
fmt::format(
|
||||
"{} VALUES {}",
|
||||
BaseInsert(),
|
||||
@@ -253,11 +242,11 @@ public:
|
||||
return (results.Success() ? results.RowsAffected() : 0);
|
||||
}
|
||||
|
||||
static std::vector<Grid> All()
|
||||
static std::vector<Grid> All(Database& db)
|
||||
{
|
||||
std::vector<Grid> all_entries;
|
||||
|
||||
auto results = content_db.QueryDatabase(
|
||||
auto results = db.QueryDatabase(
|
||||
fmt::format(
|
||||
"{}",
|
||||
BaseSelect()
|
||||
@@ -267,24 +256,24 @@ public:
|
||||
all_entries.reserve(results.RowCount());
|
||||
|
||||
for (auto row = results.begin(); row != results.end(); ++row) {
|
||||
Grid entry{};
|
||||
Grid e{};
|
||||
|
||||
entry.id = atoi(row[0]);
|
||||
entry.zoneid = atoi(row[1]);
|
||||
entry.type = atoi(row[2]);
|
||||
entry.type2 = atoi(row[3]);
|
||||
e.id = atoi(row[0]);
|
||||
e.zoneid = atoi(row[1]);
|
||||
e.type = atoi(row[2]);
|
||||
e.type2 = atoi(row[3]);
|
||||
|
||||
all_entries.push_back(entry);
|
||||
all_entries.push_back(e);
|
||||
}
|
||||
|
||||
return all_entries;
|
||||
}
|
||||
|
||||
static std::vector<Grid> GetWhere(std::string where_filter)
|
||||
static std::vector<Grid> GetWhere(Database& db, const std::string &where_filter)
|
||||
{
|
||||
std::vector<Grid> all_entries;
|
||||
|
||||
auto results = content_db.QueryDatabase(
|
||||
auto results = db.QueryDatabase(
|
||||
fmt::format(
|
||||
"{} WHERE {}",
|
||||
BaseSelect(),
|
||||
@@ -295,22 +284,22 @@ public:
|
||||
all_entries.reserve(results.RowCount());
|
||||
|
||||
for (auto row = results.begin(); row != results.end(); ++row) {
|
||||
Grid entry{};
|
||||
Grid e{};
|
||||
|
||||
entry.id = atoi(row[0]);
|
||||
entry.zoneid = atoi(row[1]);
|
||||
entry.type = atoi(row[2]);
|
||||
entry.type2 = atoi(row[3]);
|
||||
e.id = atoi(row[0]);
|
||||
e.zoneid = atoi(row[1]);
|
||||
e.type = atoi(row[2]);
|
||||
e.type2 = atoi(row[3]);
|
||||
|
||||
all_entries.push_back(entry);
|
||||
all_entries.push_back(e);
|
||||
}
|
||||
|
||||
return all_entries;
|
||||
}
|
||||
|
||||
static int DeleteWhere(std::string where_filter)
|
||||
static int DeleteWhere(Database& db, const std::string &where_filter)
|
||||
{
|
||||
auto results = content_db.QueryDatabase(
|
||||
auto results = db.QueryDatabase(
|
||||
fmt::format(
|
||||
"DELETE FROM {} WHERE {}",
|
||||
TableName(),
|
||||
@@ -321,6 +310,44 @@ public:
|
||||
return (results.Success() ? results.RowsAffected() : 0);
|
||||
}
|
||||
|
||||
static int Truncate(Database& db)
|
||||
{
|
||||
auto results = db.QueryDatabase(
|
||||
fmt::format(
|
||||
"TRUNCATE TABLE {}",
|
||||
TableName()
|
||||
)
|
||||
);
|
||||
|
||||
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_GRID_REPOSITORY_H
|
||||
|
||||
Reference in New Issue
Block a user