feat(Core/DB): add support range loop for mysql result (#13355)

This commit is contained in:
Kargatum
2022-10-09 22:46:48 +03:00
committed by GitHub
parent 092710ff19
commit 8a334e8210
3 changed files with 37 additions and 11 deletions
@@ -195,13 +195,14 @@ void AuthSession::CheckIpCallback(PreparedQueryResult result)
{ {
bool banned = false; bool banned = false;
do for (auto const& fields : *result)
{ {
Field* fields = result->Fetch();
if (fields[0].Get<uint64>() != 0) if (fields[0].Get<uint64>() != 0)
{
banned = true; banned = true;
break;
} while (result->NextRow()); }
}
if (banned) if (banned)
{ {
@@ -24,6 +24,27 @@
#include <tuple> #include <tuple>
#include <vector> #include <vector>
template<typename T>
struct ResultIterator
{
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = T*;
using reference = T&;
explicit ResultIterator(pointer ptr) : _ptr(ptr) { }
reference operator*() const { return *_ptr; }
pointer operator->() { return _ptr; }
ResultIterator& operator++() { if (!_ptr->NextRow()) _ptr = nullptr; return *this; }
bool operator!=(const ResultIterator& right) { return _ptr != right._ptr; }
private:
pointer _ptr;
};
class AC_DATABASE_API ResultSet class AC_DATABASE_API ResultSet
{ {
public: public:
@@ -54,6 +75,9 @@ public:
return theTuple; return theTuple;
} }
auto begin() { return ResultIterator<ResultSet>(this); }
static auto end() { return ResultIterator<ResultSet>(nullptr); }
protected: protected:
std::vector<QueryResultFieldMetadata> _fieldMetadata; std::vector<QueryResultFieldMetadata> _fieldMetadata;
uint64 _rowCount; uint64 _rowCount;
@@ -100,6 +124,9 @@ public:
return theTuple; return theTuple;
} }
auto begin() { return ResultIterator<PreparedResultSet>(this); }
static auto end() { return ResultIterator<PreparedResultSet>(nullptr); }
protected: protected:
std::vector<QueryResultFieldMetadata> m_fieldMetadata; std::vector<QueryResultFieldMetadata> m_fieldMetadata;
std::vector<Field> m_rows; std::vector<Field> m_rows;
+5 -7
View File
@@ -53,11 +53,10 @@ void RealmList::Close()
void RealmList::LoadBuildInfo() void RealmList::LoadBuildInfo()
{ {
// 0 1 2 3 4 5 6 // 0 1 2 3 4 5 6
if (QueryResult result = LoginDatabase.Query("SELECT majorVersion, minorVersion, bugfixVersion, hotfixVersion, build, winChecksumSeed, macChecksumSeed FROM build_info ORDER BY build ASC")) if (auto result = LoginDatabase.Query("SELECT majorVersion, minorVersion, bugfixVersion, hotfixVersion, build, winChecksumSeed, macChecksumSeed FROM build_info ORDER BY build ASC"))
{ {
do for (auto const& fields : *result)
{ {
Field* fields = result->Fetch();
RealmBuildInfo& build = _builds.emplace_back(); RealmBuildInfo& build = _builds.emplace_back();
build.MajorVersion = fields[0].Get<uint32>(); build.MajorVersion = fields[0].Get<uint32>();
build.MinorVersion = fields[1].Get<uint32>(); build.MinorVersion = fields[1].Get<uint32>();
@@ -87,7 +86,7 @@ void RealmList::LoadBuildInfo()
{ {
HexStrToByteArray(macHash, build.MacHash); HexStrToByteArray(macHash, build.MacHash);
} }
} while (result->NextRow()); }
} }
} }
@@ -149,11 +148,10 @@ void RealmList::UpdateRealms(boost::system::error_code const& error)
// Circle through results and add them to the realm map // Circle through results and add them to the realm map
if (result) if (result)
{ {
do for (auto const& fields : *result)
{ {
try try
{ {
Field* fields = result->Fetch();
uint32 realmId = fields[0].Get<uint32>(); uint32 realmId = fields[0].Get<uint32>();
std::string name = fields[1].Get<std::string>(); std::string name = fields[1].Get<std::string>();
std::string externalAddressString = fields[2].Get<std::string>(); std::string externalAddressString = fields[2].Get<std::string>();
@@ -221,7 +219,7 @@ void RealmList::UpdateRealms(boost::system::error_code const& error)
LOG_ERROR("server.authserver", "Realmlist::UpdateRealms has thrown an exception: {}", ex.what()); LOG_ERROR("server.authserver", "Realmlist::UpdateRealms has thrown an exception: {}", ex.what());
ABORT(); ABORT();
} }
} while (result->NextRow()); }
} }
for (auto itr = existingRealms.begin(); itr != existingRealms.end(); ++itr) for (auto itr = existingRealms.begin(); itr != existingRealms.end(); ++itr)