mirror of
https://github.com/EQEmu/Server.git
synced 2026-05-31 04:56:20 +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:
@@ -85,17 +85,17 @@ public:
|
||||
|
||||
static DataBuckets NewEntity()
|
||||
{
|
||||
DataBuckets entry{};
|
||||
DataBuckets e{};
|
||||
|
||||
entry.id = 0;
|
||||
entry.key = "";
|
||||
entry.value = "";
|
||||
entry.expires = 0;
|
||||
e.id = 0;
|
||||
e.key = "";
|
||||
e.value = "";
|
||||
e.expires = 0;
|
||||
|
||||
return entry;
|
||||
return e;
|
||||
}
|
||||
|
||||
static DataBuckets GetDataBucketsEntry(
|
||||
static DataBuckets GetDataBuckets(
|
||||
const std::vector<DataBuckets> &data_bucketss,
|
||||
int data_buckets_id
|
||||
)
|
||||
@@ -124,14 +124,14 @@ public:
|
||||
|
||||
auto row = results.begin();
|
||||
if (results.RowCount() == 1) {
|
||||
DataBuckets entry{};
|
||||
DataBuckets e{};
|
||||
|
||||
entry.id = strtoll(row[0], nullptr, 10);
|
||||
entry.key = row[1] ? row[1] : "";
|
||||
entry.value = row[2] ? row[2] : "";
|
||||
entry.expires = atoi(row[3]);
|
||||
e.id = strtoll(row[0], nullptr, 10);
|
||||
e.key = row[1] ? row[1] : "";
|
||||
e.value = row[2] ? row[2] : "";
|
||||
e.expires = atoi(row[3]);
|
||||
|
||||
return entry;
|
||||
return e;
|
||||
}
|
||||
|
||||
return NewEntity();
|
||||
@@ -156,24 +156,24 @@ public:
|
||||
|
||||
static int UpdateOne(
|
||||
Database& db,
|
||||
DataBuckets data_buckets_entry
|
||||
const DataBuckets &e
|
||||
)
|
||||
{
|
||||
std::vector<std::string> update_values;
|
||||
std::vector<std::string> v;
|
||||
|
||||
auto columns = Columns();
|
||||
|
||||
update_values.push_back(columns[1] + " = '" + Strings::Escape(data_buckets_entry.key) + "'");
|
||||
update_values.push_back(columns[2] + " = '" + Strings::Escape(data_buckets_entry.value) + "'");
|
||||
update_values.push_back(columns[3] + " = " + std::to_string(data_buckets_entry.expires));
|
||||
v.push_back(columns[1] + " = '" + Strings::Escape(e.key) + "'");
|
||||
v.push_back(columns[2] + " = '" + Strings::Escape(e.value) + "'");
|
||||
v.push_back(columns[3] + " = " + std::to_string(e.expires));
|
||||
|
||||
auto results = db.QueryDatabase(
|
||||
fmt::format(
|
||||
"UPDATE {} SET {} WHERE {} = {}",
|
||||
TableName(),
|
||||
Strings::Implode(", ", update_values),
|
||||
Strings::Implode(", ", v),
|
||||
PrimaryKey(),
|
||||
data_buckets_entry.id
|
||||
e.id
|
||||
)
|
||||
);
|
||||
|
||||
@@ -182,53 +182,53 @@ public:
|
||||
|
||||
static DataBuckets InsertOne(
|
||||
Database& db,
|
||||
DataBuckets data_buckets_entry
|
||||
DataBuckets e
|
||||
)
|
||||
{
|
||||
std::vector<std::string> insert_values;
|
||||
std::vector<std::string> v;
|
||||
|
||||
insert_values.push_back(std::to_string(data_buckets_entry.id));
|
||||
insert_values.push_back("'" + Strings::Escape(data_buckets_entry.key) + "'");
|
||||
insert_values.push_back("'" + Strings::Escape(data_buckets_entry.value) + "'");
|
||||
insert_values.push_back(std::to_string(data_buckets_entry.expires));
|
||||
v.push_back(std::to_string(e.id));
|
||||
v.push_back("'" + Strings::Escape(e.key) + "'");
|
||||
v.push_back("'" + Strings::Escape(e.value) + "'");
|
||||
v.push_back(std::to_string(e.expires));
|
||||
|
||||
auto results = db.QueryDatabase(
|
||||
fmt::format(
|
||||
"{} VALUES ({})",
|
||||
BaseInsert(),
|
||||
Strings::Implode(",", insert_values)
|
||||
Strings::Implode(",", v)
|
||||
)
|
||||
);
|
||||
|
||||
if (results.Success()) {
|
||||
data_buckets_entry.id = results.LastInsertedID();
|
||||
return data_buckets_entry;
|
||||
e.id = results.LastInsertedID();
|
||||
return e;
|
||||
}
|
||||
|
||||
data_buckets_entry = NewEntity();
|
||||
e = NewEntity();
|
||||
|
||||
return data_buckets_entry;
|
||||
return e;
|
||||
}
|
||||
|
||||
static int InsertMany(
|
||||
Database& db,
|
||||
std::vector<DataBuckets> data_buckets_entries
|
||||
const std::vector<DataBuckets> &entries
|
||||
)
|
||||
{
|
||||
std::vector<std::string> insert_chunks;
|
||||
|
||||
for (auto &data_buckets_entry: data_buckets_entries) {
|
||||
std::vector<std::string> insert_values;
|
||||
for (auto &e: entries) {
|
||||
std::vector<std::string> v;
|
||||
|
||||
insert_values.push_back(std::to_string(data_buckets_entry.id));
|
||||
insert_values.push_back("'" + Strings::Escape(data_buckets_entry.key) + "'");
|
||||
insert_values.push_back("'" + Strings::Escape(data_buckets_entry.value) + "'");
|
||||
insert_values.push_back(std::to_string(data_buckets_entry.expires));
|
||||
v.push_back(std::to_string(e.id));
|
||||
v.push_back("'" + Strings::Escape(e.key) + "'");
|
||||
v.push_back("'" + Strings::Escape(e.value) + "'");
|
||||
v.push_back(std::to_string(e.expires));
|
||||
|
||||
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(
|
||||
@@ -255,20 +255,20 @@ public:
|
||||
all_entries.reserve(results.RowCount());
|
||||
|
||||
for (auto row = results.begin(); row != results.end(); ++row) {
|
||||
DataBuckets entry{};
|
||||
DataBuckets e{};
|
||||
|
||||
entry.id = strtoll(row[0], nullptr, 10);
|
||||
entry.key = row[1] ? row[1] : "";
|
||||
entry.value = row[2] ? row[2] : "";
|
||||
entry.expires = atoi(row[3]);
|
||||
e.id = strtoll(row[0], nullptr, 10);
|
||||
e.key = row[1] ? row[1] : "";
|
||||
e.value = row[2] ? row[2] : "";
|
||||
e.expires = atoi(row[3]);
|
||||
|
||||
all_entries.push_back(entry);
|
||||
all_entries.push_back(e);
|
||||
}
|
||||
|
||||
return all_entries;
|
||||
}
|
||||
|
||||
static std::vector<DataBuckets> GetWhere(Database& db, std::string where_filter)
|
||||
static std::vector<DataBuckets> GetWhere(Database& db, const std::string &where_filter)
|
||||
{
|
||||
std::vector<DataBuckets> all_entries;
|
||||
|
||||
@@ -283,20 +283,20 @@ public:
|
||||
all_entries.reserve(results.RowCount());
|
||||
|
||||
for (auto row = results.begin(); row != results.end(); ++row) {
|
||||
DataBuckets entry{};
|
||||
DataBuckets e{};
|
||||
|
||||
entry.id = strtoll(row[0], nullptr, 10);
|
||||
entry.key = row[1] ? row[1] : "";
|
||||
entry.value = row[2] ? row[2] : "";
|
||||
entry.expires = atoi(row[3]);
|
||||
e.id = strtoll(row[0], nullptr, 10);
|
||||
e.key = row[1] ? row[1] : "";
|
||||
e.value = row[2] ? row[2] : "";
|
||||
e.expires = atoi(row[3]);
|
||||
|
||||
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(
|
||||
@@ -321,6 +321,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_DATA_BUCKETS_REPOSITORY_H
|
||||
|
||||
Reference in New Issue
Block a user