OregonCore  revision 3611e8a-git
Your Favourite TBC server
QueryResult.h
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef QUERYRESULT_H
19 #define QUERYRESULT_H
20 
21 #include <ace/Refcounted_Auto_Ptr.h>
22 #include <ace/Null_Mutex.h>
23 #include <stdexcept>
24 
25 #include "Field.h"
26 #include "Utilities/UnorderedMap.h"
27 
28 #ifdef WIN32
29 #include <winsock2.h>
30 #endif
31 #include <mysql.h>
32 
34 {
35  public:
36  QueryResult(MYSQL_RES* result, MYSQL_FIELD* fields, uint64 rowCount, uint32 fieldCount);
37  ~QueryResult();
38 
39  bool NextRow();
40 
41  Field* Fetch() const { return mCurrentRow; }
42  Field const& operator [] (int index) const { return mCurrentRow[index]; }
43  Field const& operator [] (const char* name) const { return mCurrentRow[GetField_idx(name)]; }
44 
45  uint32 GetFieldCount() const { return mFieldCount; }
46  uint64 GetRowCount() const { return mRowCount; }
47 
48  size_t GetField_idx(const char* name) const
49  {
50  for (size_t i = 0; i < mFieldCount; ++i)
51  if (!strcmp(name, mFields[i].name))
52  return i;
53 
54  std::string err = "No column named ";
55  err += name;
56  err += "in field list!";
57  throw std::invalid_argument(err);
58  }
59 
60  protected:
64  MYSQL_FIELD* mFields;
65 
66  private:
67  void EndQuery();
68 
69  MYSQL_RES* mResult;
70 };
71 
73 {
74  public:
75  PreparedQueryResult(MYSQL_STMT* stmt);
77 
78  bool NextRow();
79 
80  Field* Fetch() const { return mCurrentRow; }
81  Field const& operator [] (int index) const { return mCurrentRow[index]; }
82  Field const& operator [] (const char* name) const { return mCurrentRow[GetField_idx(name)]; }
83 
84  uint32 GetFieldCount() const { return mFieldCount; }
85  uint64 GetRowCount() const { return mRowCount; }
86 
87  size_t GetField_idx(const char* name) const
88  {
89  for (size_t i = 0; i < mFieldCount; ++i)
90  if (!strcmp(name, mFields[i].name))
91  return i;
92 
93  std::string err = "No column named ";
94  err += name;
95  err += "in field list!";
96  throw std::invalid_argument(err);
97  }
98 
99  protected:
103  MYSQL_FIELD* mFields;
104 
105  private:
106  void EndQuery();
107 
108  std::vector<Field*> mRows;
109  size_t mCursor;
110  MYSQL_RES* mMetaData;
111 };
112 
113 typedef ACE_Refcounted_Auto_Ptr<QueryResult, ACE_Null_Mutex> QueryResult_AutoPtr;
114 typedef ACE_Refcounted_Auto_Ptr<PreparedQueryResult, ACE_Null_Mutex> PreparedQueryResult_AutoPtr;
115 
116 #endif
Field const & operator[](int index) const
Definition: QueryResult.h:42
ACE_Refcounted_Auto_Ptr< PreparedQueryResult, ACE_Null_Mutex > PreparedQueryResult_AutoPtr
Definition: QueryResult.h:114
void EndQuery()
Definition: Field.h:24
uint64 mRowCount
Definition: QueryResult.h:63
Field * Fetch() const
Definition: QueryResult.h:41
uint32 mFieldCount
Definition: QueryResult.h:62
size_t GetField_idx(const char *name) const
Definition: QueryResult.h:48
MYSQL_RES * mResult
Definition: QueryResult.h:69
Field * Fetch() const
Definition: QueryResult.h:80
std::vector< Field * > mRows
Definition: QueryResult.h:108
MYSQL_FIELD * mFields
Definition: QueryResult.h:64
MYSQL_FIELD * mFields
Definition: QueryResult.h:103
QueryResult(MYSQL_RES *result, MYSQL_FIELD *fields, uint64 rowCount, uint32 fieldCount)
Definition: QueryResult.cpp:21
size_t GetField_idx(const char *name) const
Definition: QueryResult.h:87
uint32 GetFieldCount() const
Definition: QueryResult.h:45
MYSQL_RES * mMetaData
Definition: QueryResult.h:110
uint64 GetRowCount() const
Definition: QueryResult.h:46
uint64 GetRowCount() const
Definition: QueryResult.h:85
ACE_UINT64 uint64
Definition: Define.h:70
ACE_Refcounted_Auto_Ptr< QueryResult, ACE_Null_Mutex > QueryResult_AutoPtr
Definition: QueryResult.h:113
Field * mCurrentRow
Definition: QueryResult.h:61
ACE_UINT32 uint32
Definition: Define.h:71
bool NextRow()
uint32 GetFieldCount() const
Definition: QueryResult.h:84