From 7344a5ef6f776f355cda34da77d2aed01f228b47 Mon Sep 17 00:00:00 2001 From: megamage Date: Mon, 1 Jun 2009 21:52:17 -0500 Subject: [7935] Move seldom used access to query data by field names to independent object. Author: VladimirMangos This let not do preparation code for unused later functionlity. --HG-- branch : trunk --- src/shared/Database/QueryResult.h | 56 +++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 20 deletions(-) (limited to 'src/shared/Database/QueryResult.h') diff --git a/src/shared/Database/QueryResult.h b/src/shared/Database/QueryResult.h index 39228dd4ba9..f9f1a009833 100644 --- a/src/shared/Database/QueryResult.h +++ b/src/shared/Database/QueryResult.h @@ -31,37 +31,53 @@ class TRINITY_DLL_SPEC QueryResult virtual bool NextRow() = 0; - typedef std::map FieldNames; - - uint32 GetField_idx(const std::string &name) const - { - for(FieldNames::const_iterator iter = GetFieldNames().begin(); iter != GetFieldNames().end(); ++iter) - { - if(iter->second == name) - return iter->first; - } - ASSERT(false && "unknown field name"); - return uint32(-1); - } - Field *Fetch() const { return mCurrentRow; } const Field & operator [] (int index) const { return mCurrentRow[index]; } - const Field & operator [] (const std::string &name) const - { - return mCurrentRow[GetField_idx(name)]; - } - uint32 GetFieldCount() const { return mFieldCount; } uint64 GetRowCount() const { return mRowCount; } - FieldNames const& GetFieldNames() const {return mFieldNames; } protected: Field *mCurrentRow; uint32 mFieldCount; uint64 mRowCount; - FieldNames mFieldNames; }; + +typedef std::vector QueryFieldNames; + +class MANGOS_DLL_SPEC QueryNamedResult +{ + public: + explicit QueryNamedResult(QueryResult* query, QueryFieldNames const& names) : mQuery(query), mFieldNames(names) {} + ~QueryNamedResult() { delete mQuery; } + + // compatible interface with QueryResult + bool NextRow() { return mQuery->NextRow(); } + Field *Fetch() const { return mQuery->Fetch(); } + uint32 GetFieldCount() const { return mQuery->GetFieldCount(); } + uint64 GetRowCount() const { return mQuery->GetRowCount(); } + Field const& operator[] (int index) const { return (*mQuery)[index]; } + + // named access + Field const& operator[] (const std::string &name) const { return mQuery->Fetch()[GetField_idx(name)]; } + QueryFieldNames const& GetFieldNames() const { return mFieldNames; } + + uint32 GetField_idx(const std::string &name) const + { + for(size_t idx = 0; idx < mFieldNames.size(); ++idx) + { + if(mFieldNames[idx] == name) + return idx; + } + ASSERT(false && "unknown field name"); + return uint32(-1); + } + + protected: + QueryResult *mQuery; + QueryFieldNames mFieldNames; +}; + #endif -- cgit v1.2.3