TrinityCore
Loading...
Searching...
No Matches
QueryResult.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore 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 "Define.h"
22#include "DatabaseEnvFwd.h"
23#include "Hash.h"
24#include <string_view>
25#include <unordered_map>
26#include <vector>
27
28namespace Trinity::DB
29{
31{
32 std::size_t HashValue;
33 std::string_view Alias;
34
35 // implicit constructor from string literal for users of the query result
36 consteval FieldLookupByAliasKey(char const* alias) : HashValue(HashFnv1a<>::GetHash(std::span(alias, std::char_traits<char>::length(alias)))), Alias(alias) { }
37
38 // runtime only constructor used internally to fill alias to index mapping
39 struct RuntimeInitTag { } static inline constexpr RuntimeInit = { };
40 FieldLookupByAliasKey(RuntimeInitTag, std::string_view alias) : HashValue(HashFnv1a<>::GetHash(alias)), Alias(std::move(alias)) { }
41
42 friend bool operator==(FieldLookupByAliasKey const& left, FieldLookupByAliasKey const& right) = default;
43
44 struct Hash { constexpr std::size_t operator()(FieldLookupByAliasKey const& k) const { return k.HashValue; } };
45};
46
47using FieldAliasToIndexMap = std::unordered_map<FieldLookupByAliasKey, std::size_t, FieldLookupByAliasKey::Hash>;
48}
49
51{
52 public:
53 ResultSet(MySQLResult* result, MySQLField* fields, uint64 rowCount, uint32 fieldCount);
54 ~ResultSet();
55
56 bool NextRow();
57 uint64 GetRowCount() const { return _rowCount; }
58 uint32 GetFieldCount() const { return _fieldCount; }
59
60 Field* Fetch() const { return _currentRow; }
61 Field const& operator[](std::size_t index) const;
62 Field const& operator[](Trinity::DB::FieldLookupByAliasKey const& alias) const;
63
64 QueryResultFieldMetadata const& GetFieldMetadata(std::size_t index) const;
65 QueryResultFieldMetadata const& GetFieldMetadata(Trinity::DB::FieldLookupByAliasKey const& alias) const;
66
67 protected:
68 std::vector<QueryResultFieldMetadata> _fieldMetadata;
73
74 private:
75 void CleanUp();
78
79 ResultSet(ResultSet const& right) = delete;
80 ResultSet& operator=(ResultSet const& right) = delete;
81};
82
84{
85 public:
86 PreparedResultSet(MySQLStmt* stmt, MySQLResult* result, uint64 rowCount, uint32 fieldCount);
88
89 bool NextRow();
90 uint64 GetRowCount() const { return m_rowCount; }
91 uint32 GetFieldCount() const { return m_fieldCount; }
92
93 Field* Fetch() const;
94 Field const& operator[](std::size_t index) const;
95 Field const& operator[](Trinity::DB::FieldLookupByAliasKey const& alias) const;
96
97 QueryResultFieldMetadata const& GetFieldMetadata(std::size_t index) const;
98 QueryResultFieldMetadata const& GetFieldMetadata(Trinity::DB::FieldLookupByAliasKey const& alias) const;
99
100 protected:
101 std::vector<QueryResultFieldMetadata> m_fieldMetadata;
103 std::vector<Field> m_rows;
107
108 private:
112
113 void CleanUp();
114 bool _NextRow();
115
116 PreparedResultSet(PreparedResultSet const& right) = delete;
118};
119
120#endif
#define TC_DATABASE_API
Definition Define.h:111
uint64_t uint64
Definition Define.h:153
uint32_t uint32
Definition Define.h:154
Class used to access individual fields of database query result.
Definition Field.h:94
std::vector< QueryResultFieldMetadata > m_fieldMetadata
MySQLStmt * m_stmt
PreparedResultSet(PreparedResultSet const &right)=delete
uint32 GetFieldCount() const
Definition QueryResult.h:91
MySQLBind * m_rBind
std::vector< Field > m_rows
Trinity::DB::FieldAliasToIndexMap m_fieldIndexByAlias
uint64 GetRowCount() const
Definition QueryResult.h:90
PreparedResultSet & operator=(PreparedResultSet const &right)=delete
MySQLResult * m_metadataResult
Field metadata, returned by mysql_stmt_result_metadata.
uint32 _fieldCount
Definition QueryResult.h:72
Field * _currentRow
Definition QueryResult.h:71
uint64 _rowCount
Definition QueryResult.h:70
uint64 GetRowCount() const
Definition QueryResult.h:57
Field * Fetch() const
Definition QueryResult.h:60
MySQLField * _fields
Definition QueryResult.h:77
std::vector< QueryResultFieldMetadata > _fieldMetadata
Definition QueryResult.h:68
uint32 GetFieldCount() const
Definition QueryResult.h:58
Trinity::DB::FieldAliasToIndexMap _fieldIndexByAlias
Definition QueryResult.h:69
MySQLResult * _result
Definition QueryResult.h:76
ResultSet(ResultSet const &right)=delete
ResultSet & operator=(ResultSet const &right)=delete
std::unordered_map< FieldLookupByAliasKey, std::size_t, FieldLookupByAliasKey::Hash > FieldAliasToIndexMap
Definition QueryResult.h:47
STL namespace.
constexpr std::size_t operator()(FieldLookupByAliasKey const &k) const
Definition QueryResult.h:44
consteval FieldLookupByAliasKey(char const *alias)
Definition QueryResult.h:36
FieldLookupByAliasKey(RuntimeInitTag, std::string_view alias)
Definition QueryResult.h:40
friend bool operator==(FieldLookupByAliasKey const &left, FieldLookupByAliasKey const &right)=default
struct Trinity::DB::FieldLookupByAliasKey::RuntimeInitTag RuntimeInit
std::size_t HashValue
Cached hash value (first field to make opeartor== return early, minimizing number of string compariso...
Definition QueryResult.h:32