2010-10-07 15:54:07 +02:00
|
|
|
/*
|
2020-01-02 06:44:10 +01:00
|
|
|
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
|
2010-10-07 15:54:07 +02:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
|
* option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
|
* more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-10-02 16:23:55 -05:00
|
|
|
*/
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2010-08-21 16:37:30 +02:00
|
|
|
#ifndef QUERYRESULT_H
|
2008-10-02 16:23:55 -05:00
|
|
|
#define QUERYRESULT_H
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2017-05-12 18:49:51 +02:00
|
|
|
#include "Define.h"
|
|
|
|
|
#include "DatabaseEnvFwd.h"
|
2025-01-23 12:45:56 +01:00
|
|
|
#include "Hash.h"
|
2026-03-13 12:14:00 +01:00
|
|
|
#include <string_view>
|
2025-01-23 12:21:13 +01:00
|
|
|
#include <unordered_map>
|
2017-05-12 18:49:51 +02:00
|
|
|
#include <vector>
|
2010-05-08 05:03:32 +02:00
|
|
|
|
2025-01-23 12:45:56 +01:00
|
|
|
namespace Trinity::DB
|
|
|
|
|
{
|
|
|
|
|
struct FieldLookupByAliasKey
|
|
|
|
|
{
|
|
|
|
|
std::size_t HashValue; ///< Cached hash value (first field to make opeartor== return early, minimizing number of string comparisons)
|
|
|
|
|
std::string_view Alias;
|
|
|
|
|
|
|
|
|
|
// implicit constructor from string literal for users of the query result
|
2026-01-08 00:41:16 +01:00
|
|
|
consteval FieldLookupByAliasKey(char const* alias) : HashValue(HashFnv1a<>::GetHash(std::span(alias, std::char_traits<char>::length(alias)))), Alias(alias) { }
|
2025-01-23 12:45:56 +01:00
|
|
|
|
|
|
|
|
// runtime only constructor used internally to fill alias to index mapping
|
|
|
|
|
struct RuntimeInitTag { } static inline constexpr RuntimeInit = { };
|
2026-01-08 00:41:16 +01:00
|
|
|
FieldLookupByAliasKey(RuntimeInitTag, std::string_view alias) : HashValue(HashFnv1a<>::GetHash(alias)), Alias(std::move(alias)) { }
|
2025-01-23 12:45:56 +01:00
|
|
|
|
|
|
|
|
friend bool operator==(FieldLookupByAliasKey const& left, FieldLookupByAliasKey const& right) = default;
|
|
|
|
|
|
|
|
|
|
struct Hash { constexpr std::size_t operator()(FieldLookupByAliasKey const& k) const { return k.HashValue; } };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using FieldAliasToIndexMap = std::unordered_map<FieldLookupByAliasKey, std::size_t, FieldLookupByAliasKey::Hash>;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-11 15:57:26 +01:00
|
|
|
class TC_DATABASE_API ResultSet
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
|
|
|
|
public:
|
2019-11-01 16:21:14 +01:00
|
|
|
ResultSet(MySQLResult* result, MySQLField* fields, uint64 rowCount, uint32 fieldCount);
|
2010-09-11 21:10:54 +02:00
|
|
|
~ResultSet();
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2010-05-08 05:03:32 +02:00
|
|
|
bool NextRow();
|
2012-02-14 12:46:26 -05:00
|
|
|
uint64 GetRowCount() const { return _rowCount; }
|
|
|
|
|
uint32 GetFieldCount() const { return _fieldCount; }
|
2010-09-24 22:16:21 +02:00
|
|
|
|
2012-02-14 12:46:26 -05:00
|
|
|
Field* Fetch() const { return _currentRow; }
|
2017-05-12 18:49:51 +02:00
|
|
|
Field const& operator[](std::size_t index) const;
|
2025-01-23 12:45:56 +01:00
|
|
|
Field const& operator[](Trinity::DB::FieldLookupByAliasKey const& alias) const;
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2024-07-14 14:54:51 +02:00
|
|
|
QueryResultFieldMetadata const& GetFieldMetadata(std::size_t index) const;
|
2025-04-25 19:54:38 +02:00
|
|
|
QueryResultFieldMetadata const& GetFieldMetadata(Trinity::DB::FieldLookupByAliasKey const& alias) const;
|
2024-07-14 14:54:51 +02:00
|
|
|
|
2008-10-02 16:23:55 -05:00
|
|
|
protected:
|
2020-07-05 12:17:47 +02:00
|
|
|
std::vector<QueryResultFieldMetadata> _fieldMetadata;
|
2025-01-23 12:45:56 +01:00
|
|
|
Trinity::DB::FieldAliasToIndexMap _fieldIndexByAlias;
|
2012-02-14 12:46:26 -05:00
|
|
|
uint64 _rowCount;
|
|
|
|
|
Field* _currentRow;
|
|
|
|
|
uint32 _fieldCount;
|
2010-05-08 05:03:32 +02:00
|
|
|
|
|
|
|
|
private:
|
2010-09-24 22:16:21 +02:00
|
|
|
void CleanUp();
|
2019-11-01 16:21:14 +01:00
|
|
|
MySQLResult* _result;
|
|
|
|
|
MySQLField* _fields;
|
2014-04-20 16:48:35 +02:00
|
|
|
|
2014-04-29 16:35:11 +02:00
|
|
|
ResultSet(ResultSet const& right) = delete;
|
|
|
|
|
ResultSet& operator=(ResultSet const& right) = delete;
|
2008-10-02 16:23:55 -05:00
|
|
|
};
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2016-03-11 15:57:26 +01:00
|
|
|
class TC_DATABASE_API PreparedResultSet
|
2009-06-01 21:52:17 -05:00
|
|
|
{
|
|
|
|
|
public:
|
2019-11-01 16:21:14 +01:00
|
|
|
PreparedResultSet(MySQLStmt* stmt, MySQLResult* result, uint64 rowCount, uint32 fieldCount);
|
2010-09-24 22:16:21 +02:00
|
|
|
~PreparedResultSet();
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2010-09-24 22:16:21 +02:00
|
|
|
bool NextRow();
|
|
|
|
|
uint64 GetRowCount() const { return m_rowCount; }
|
|
|
|
|
uint32 GetFieldCount() const { return m_fieldCount; }
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2017-05-12 18:49:51 +02:00
|
|
|
Field* Fetch() const;
|
|
|
|
|
Field const& operator[](std::size_t index) const;
|
2025-01-23 12:45:56 +01:00
|
|
|
Field const& operator[](Trinity::DB::FieldLookupByAliasKey const& alias) const;
|
2010-09-11 09:17:14 +02:00
|
|
|
|
2024-07-14 14:54:51 +02:00
|
|
|
QueryResultFieldMetadata const& GetFieldMetadata(std::size_t index) const;
|
2025-04-25 19:54:38 +02:00
|
|
|
QueryResultFieldMetadata const& GetFieldMetadata(Trinity::DB::FieldLookupByAliasKey const& alias) const;
|
2024-07-14 14:54:51 +02:00
|
|
|
|
2010-09-11 09:17:14 +02:00
|
|
|
protected:
|
2020-07-05 12:17:47 +02:00
|
|
|
std::vector<QueryResultFieldMetadata> m_fieldMetadata;
|
2025-01-23 12:45:56 +01:00
|
|
|
Trinity::DB::FieldAliasToIndexMap m_fieldIndexByAlias;
|
2015-08-26 17:00:26 +02:00
|
|
|
std::vector<Field> m_rows;
|
2010-09-24 22:16:21 +02:00
|
|
|
uint64 m_rowCount;
|
|
|
|
|
uint64 m_rowPosition;
|
|
|
|
|
uint32 m_fieldCount;
|
2010-09-25 22:03:57 +02:00
|
|
|
|
2010-09-24 22:16:21 +02:00
|
|
|
private:
|
2019-11-01 16:21:14 +01:00
|
|
|
MySQLBind* m_rBind;
|
|
|
|
|
MySQLStmt* m_stmt;
|
|
|
|
|
MySQLResult* m_metadataResult; ///< Field metadata, returned by mysql_stmt_result_metadata
|
2010-09-11 09:17:14 +02:00
|
|
|
|
2010-09-24 22:16:21 +02:00
|
|
|
void CleanUp();
|
|
|
|
|
bool _NextRow();
|
2010-09-11 09:17:14 +02:00
|
|
|
|
2014-04-29 16:35:11 +02:00
|
|
|
PreparedResultSet(PreparedResultSet const& right) = delete;
|
|
|
|
|
PreparedResultSet& operator=(PreparedResultSet const& right) = delete;
|
2010-09-11 09:17:14 +02:00
|
|
|
};
|
|
|
|
|
|
2008-10-02 16:23:55 -05:00
|
|
|
#endif
|