[7935] Move seldom used access to query data by field names to independent object.

This let not do preparation code for unused later functionlity.
This commit is contained in:
VladimirMangos 2009-06-01 10:20:28 +04:00
parent 626553c9ee
commit c3c7187841
12 changed files with 165 additions and 71 deletions

View file

@ -29,36 +29,52 @@ class MANGOS_DLL_SPEC QueryResult
virtual bool NextRow() = 0;
typedef std::map<uint32, std::string> 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<std::string> 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