TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
QueryResult.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef QUERYRESULT_H
20 #define QUERYRESULT_H
21 
22 #include <memory>
23 #include "Field.h"
24 
25 #ifdef _WIN32
26  #include <winsock2.h>
27 #endif
28 #include <mysql.h>
29 
31 {
32  public:
33  ResultSet(MYSQL_RES* result, MYSQL_FIELD* fields, uint64 rowCount, uint32 fieldCount);
34  ~ResultSet();
35 
36  bool NextRow();
37  uint64 GetRowCount() const { return _rowCount; }
38  uint32 GetFieldCount() const { return _fieldCount; }
39 
40  Field* Fetch() const { return _currentRow; }
41  const Field & operator [] (uint32 index) const
42  {
43  ASSERT(index < _fieldCount);
44  return _currentRow[index];
45  }
46 
47  protected:
51 
52  private:
53  void CleanUp();
54  MYSQL_RES* _result;
55  MYSQL_FIELD* _fields;
56 
57  ResultSet(ResultSet const& right) = delete;
58  ResultSet& operator=(ResultSet const& right) = delete;
59 };
60 
61 typedef std::shared_ptr<ResultSet> QueryResult;
62 
64 {
65  public:
66  PreparedResultSet(MYSQL_STMT* stmt, MYSQL_RES* result, uint64 rowCount, uint32 fieldCount);
68 
69  bool NextRow();
70  uint64 GetRowCount() const { return m_rowCount; }
71  uint32 GetFieldCount() const { return m_fieldCount; }
72 
73  Field* Fetch() const
74  {
75  ASSERT(m_rowPosition < m_rowCount);
76  return const_cast<Field*>(&m_rows[uint32(m_rowPosition) * m_fieldCount]);
77  }
78 
79  Field const& operator[](uint32 index) const
80  {
81  ASSERT(m_rowPosition < m_rowCount);
82  ASSERT(index < m_fieldCount);
83  return m_rows[uint32(m_rowPosition) * m_fieldCount + index];
84  }
85 
86  protected:
87  std::vector<Field> m_rows;
91 
92  private:
93  MYSQL_BIND* m_rBind;
94  MYSQL_STMT* m_stmt;
95  MYSQL_RES* m_metadataResult;
96 
97  my_bool* m_isNull;
98  unsigned long* m_length;
99 
100  void CleanUp();
101  bool _NextRow();
102 
103  PreparedResultSet(PreparedResultSet const& right) = delete;
104  PreparedResultSet& operator=(PreparedResultSet const& right) = delete;
105 };
106 
107 typedef std::shared_ptr<PreparedResultSet> PreparedQueryResult;
108 
109 #endif
110 
MYSQL_RES * _result
Definition: QueryResult.h:54
#define TC_DATABASE_API
Definition: Define.h:122
uint32 GetFieldCount() const
Definition: QueryResult.h:71
uint32 m_fieldCount
Definition: QueryResult.h:90
std::shared_ptr< PreparedResultSet > PreparedQueryResult
Definition: QueryResult.h:107
MYSQL_FIELD * _fields
Definition: QueryResult.h:55
unsigned long * m_length
Definition: QueryResult.h:98
Class used to access individual fields of database query result.
Definition: Field.h:56
uint32 _fieldCount
Definition: QueryResult.h:50
MYSQL_RES * m_metadataResult
Field metadata, returned by mysql_stmt_result_metadata.
Definition: QueryResult.h:95
std::vector< Field > m_rows
Definition: QueryResult.h:87
uint64 GetRowCount() const
Definition: QueryResult.h:70
Definition: QueryResult.h:30
Field * Fetch() const
Definition: QueryResult.h:73
MYSQL_BIND * m_rBind
Definition: QueryResult.h:93
my_bool * m_isNull
Definition: QueryResult.h:97
Field const & operator[](uint32 index) const
Definition: QueryResult.h:79
uint64 GetRowCount() const
Definition: QueryResult.h:37
Vector2int16 & operator=(const Any &a)
uint64 m_rowPosition
Definition: QueryResult.h:89
uint32_t uint32
Definition: Define.h:150
uint64_t uint64
Definition: Define.h:149
std::shared_ptr< ResultSet > QueryResult
Definition: QueryResult.h:61
Field * Fetch() const
Definition: QueryResult.h:40
G3D::int16 & operator[](int i)
Definition: Vector2int16.h:51
Field * _currentRow
Definition: QueryResult.h:49
MYSQL_STMT * m_stmt
Definition: QueryResult.h:94
uint64 m_rowCount
Definition: QueryResult.h:88
#define ASSERT
Definition: Errors.h:55
uint64 _rowCount
Definition: QueryResult.h:48
uint32_t uint32
Definition: g3dmath.h:168
Definition: QueryResult.h:63
uint32 GetFieldCount() const
Definition: QueryResult.h:38