mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-15 23:20:36 +01:00
Core/Database: Add new member functions to Field class that return empty optionals if database result was NULL
This commit is contained in:
@@ -26,137 +26,219 @@ Field::Field() : _value(nullptr), _length(0), _meta(nullptr)
|
||||
|
||||
Field::~Field() = default;
|
||||
|
||||
uint8 Field::GetUInt8() const
|
||||
uint8 Field::GetUInt8() const noexcept
|
||||
{
|
||||
if (!_value)
|
||||
return 0;
|
||||
|
||||
return _meta->Converter->GetUInt8(_value, _length, _meta);
|
||||
if (_value)
|
||||
return _meta->Converter->GetUInt8(_value, _length, _meta);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int8 Field::GetInt8() const
|
||||
int8 Field::GetInt8() const noexcept
|
||||
{
|
||||
if (!_value)
|
||||
return 0;
|
||||
|
||||
return _meta->Converter->GetInt8(_value, _length, _meta);
|
||||
if (_value)
|
||||
return _meta->Converter->GetInt8(_value, _length, _meta);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint16 Field::GetUInt16() const
|
||||
uint16 Field::GetUInt16() const noexcept
|
||||
{
|
||||
if (!_value)
|
||||
return 0;
|
||||
|
||||
return _meta->Converter->GetUInt16(_value, _length, _meta);
|
||||
if (_value)
|
||||
return _meta->Converter->GetUInt16(_value, _length, _meta);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int16 Field::GetInt16() const
|
||||
int16 Field::GetInt16() const noexcept
|
||||
{
|
||||
if (!_value)
|
||||
return 0;
|
||||
|
||||
return _meta->Converter->GetInt16(_value, _length, _meta);
|
||||
if (_value)
|
||||
return _meta->Converter->GetInt16(_value, _length, _meta);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32 Field::GetUInt32() const
|
||||
uint32 Field::GetUInt32() const noexcept
|
||||
{
|
||||
if (!_value)
|
||||
return 0;
|
||||
|
||||
return _meta->Converter->GetUInt32(_value, _length, _meta);
|
||||
if (_value)
|
||||
return _meta->Converter->GetUInt32(_value, _length, _meta);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32 Field::GetInt32() const
|
||||
int32 Field::GetInt32() const noexcept
|
||||
{
|
||||
if (!_value)
|
||||
return 0;
|
||||
|
||||
return _meta->Converter->GetInt32(_value, _length, _meta);
|
||||
if (_value)
|
||||
return _meta->Converter->GetInt32(_value, _length, _meta);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64 Field::GetUInt64() const
|
||||
uint64 Field::GetUInt64() const noexcept
|
||||
{
|
||||
if (!_value)
|
||||
return 0;
|
||||
|
||||
return _meta->Converter->GetUInt64(_value, _length, _meta);
|
||||
if (_value)
|
||||
return _meta->Converter->GetUInt64(_value, _length, _meta);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64 Field::GetInt64() const
|
||||
int64 Field::GetInt64() const noexcept
|
||||
{
|
||||
if (!_value)
|
||||
return 0;
|
||||
|
||||
return _meta->Converter->GetInt64(_value, _length, _meta);
|
||||
if (_value)
|
||||
return _meta->Converter->GetInt64(_value, _length, _meta);
|
||||
return 0;
|
||||
}
|
||||
|
||||
float Field::GetFloat() const
|
||||
float Field::GetFloat() const noexcept
|
||||
{
|
||||
if (!_value)
|
||||
return 0.0f;
|
||||
|
||||
return _meta->Converter->GetFloat(_value, _length, _meta);
|
||||
if (_value)
|
||||
return _meta->Converter->GetFloat(_value, _length, _meta);
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
double Field::GetDouble() const
|
||||
double Field::GetDouble() const noexcept
|
||||
{
|
||||
if (!_value)
|
||||
return 0.0;
|
||||
|
||||
return _meta->Converter->GetDouble(_value, _length, _meta);
|
||||
if (_value)
|
||||
return _meta->Converter->GetDouble(_value, _length, _meta);
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
SystemTimePoint Field::GetDate() const
|
||||
SystemTimePoint Field::GetDate() const noexcept
|
||||
{
|
||||
if (!_value)
|
||||
return SystemTimePoint::min();
|
||||
|
||||
return _meta->Converter->GetDate(_value, _length, _meta);
|
||||
if (_value)
|
||||
return _meta->Converter->GetDate(_value, _length, _meta);
|
||||
return SystemTimePoint::min();
|
||||
}
|
||||
|
||||
char const* Field::GetCString() const
|
||||
char const* Field::GetCString() const noexcept
|
||||
{
|
||||
if (!_value)
|
||||
return nullptr;
|
||||
|
||||
return _meta->Converter->GetCString(_value, _length, _meta);
|
||||
if (_value)
|
||||
return _meta->Converter->GetCString(_value, _length, _meta);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::string Field::GetString() const
|
||||
std::string Field::GetString() const noexcept
|
||||
{
|
||||
return std::string(GetStringView());
|
||||
}
|
||||
|
||||
std::string_view Field::GetStringView() const noexcept
|
||||
{
|
||||
std::string result;
|
||||
if (char const* string = GetCString())
|
||||
result.assign(string, _length);
|
||||
|
||||
return result;
|
||||
return { string, _length };
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string_view Field::GetStringView() const
|
||||
std::vector<uint8> Field::GetBinary() const noexcept
|
||||
{
|
||||
std::string_view result;
|
||||
if (char const* const string = GetCString())
|
||||
result = { string, _length };
|
||||
|
||||
return result;
|
||||
std::span<uint8 const> binary = GetBinaryView();
|
||||
return { binary.begin(), binary.end() };
|
||||
}
|
||||
|
||||
std::vector<uint8> Field::GetBinary() const
|
||||
{
|
||||
std::vector<uint8> result;
|
||||
if (!_value || !_length)
|
||||
return result;
|
||||
|
||||
result.resize(_length);
|
||||
memcpy(result.data(), _value, _length);
|
||||
return result;
|
||||
}
|
||||
|
||||
std::span<uint8 const> Field::GetBinaryView() const
|
||||
std::span<uint8 const> Field::GetBinaryView() const noexcept
|
||||
{
|
||||
return { reinterpret_cast<uint8 const*>(_value), _length };
|
||||
}
|
||||
|
||||
void Field::GetBinarySizeChecked(uint8* buf, size_t length) const
|
||||
Optional<uint8> Field::GetUInt8OrNull() const noexcept
|
||||
{
|
||||
if (_value)
|
||||
return _meta->Converter->GetUInt8(_value, _length, _meta);
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<int8> Field::GetInt8OrNull() const noexcept
|
||||
{
|
||||
if (_value)
|
||||
return _meta->Converter->GetInt8(_value, _length, _meta);
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<uint16> Field::GetUInt16OrNull() const noexcept
|
||||
{
|
||||
if (_value)
|
||||
return _meta->Converter->GetUInt16(_value, _length, _meta);
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<int16> Field::GetInt16OrNull() const noexcept
|
||||
{
|
||||
if (_value)
|
||||
return _meta->Converter->GetInt16(_value, _length, _meta);
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<uint32> Field::GetUInt32OrNull() const noexcept
|
||||
{
|
||||
if (_value)
|
||||
return _meta->Converter->GetUInt32(_value, _length, _meta);
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<int32> Field::GetInt32OrNull() const noexcept
|
||||
{
|
||||
if (_value)
|
||||
return _meta->Converter->GetInt32(_value, _length, _meta);
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<uint64> Field::GetUInt64OrNull() const noexcept
|
||||
{
|
||||
if (_value)
|
||||
return _meta->Converter->GetUInt64(_value, _length, _meta);
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<int64> Field::GetInt64OrNull() const noexcept
|
||||
{
|
||||
if (_value)
|
||||
return _meta->Converter->GetInt64(_value, _length, _meta);
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<float> Field::GetFloatOrNull() const noexcept
|
||||
{
|
||||
if (_value)
|
||||
return _meta->Converter->GetFloat(_value, _length, _meta);
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<double> Field::GetDoubleOrNull() const noexcept
|
||||
{
|
||||
if (_value)
|
||||
return _meta->Converter->GetDouble(_value, _length, _meta);
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<SystemTimePoint> Field::GetDateOrNull() const noexcept
|
||||
{
|
||||
if (_value)
|
||||
return _meta->Converter->GetDate(_value, _length, _meta);
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<std::string> Field::GetStringOrNull() const noexcept
|
||||
{
|
||||
if (Optional<std::string_view> string = GetStringViewOrNull())
|
||||
return Optional<std::string>(std::in_place, *string);
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<std::string_view> Field::GetStringViewOrNull() const noexcept
|
||||
{
|
||||
if (char const* string = GetCString())
|
||||
return Optional<std::string_view>(std::in_place, string, _length);
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<std::vector<uint8>> Field::GetBinaryOrNull() const noexcept
|
||||
{
|
||||
if (Optional<std::span<uint8 const>> binary = GetBinaryViewOrNull())
|
||||
return Optional<std::vector<uint8>>(std::in_place, binary->begin(), binary->end());
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<std::span<uint8 const>> Field::GetBinaryViewOrNull() const noexcept
|
||||
{
|
||||
if (_value)
|
||||
return Optional<std::span<uint8 const>>(std::in_place, reinterpret_cast<uint8 const*>(_value), _length);
|
||||
return {};
|
||||
}
|
||||
|
||||
void Field::GetBinarySizeChecked(uint8* buf, size_t length) const noexcept
|
||||
{
|
||||
ASSERT(_value && (_length == length), "Expected %zu-byte binary blob, got %sdata (%u bytes) instead", length, _value ? "" : "no ", _length);
|
||||
memcpy(buf, _value, length);
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
#include "Define.h"
|
||||
#include "Duration.h"
|
||||
#include "Optional.h"
|
||||
#include <array>
|
||||
#include <span>
|
||||
#include <string>
|
||||
@@ -98,40 +99,64 @@ class TC_DATABASE_API Field
|
||||
Field();
|
||||
~Field();
|
||||
|
||||
bool GetBool() const // Wrapper, actually gets integer
|
||||
bool GetBool() const noexcept // Wrapper, actually gets integer
|
||||
{
|
||||
return GetUInt8() == 1 ? true : false;
|
||||
return GetUInt8() != 0;
|
||||
}
|
||||
|
||||
uint8 GetUInt8() const;
|
||||
int8 GetInt8() const;
|
||||
uint16 GetUInt16() const;
|
||||
int16 GetInt16() const;
|
||||
uint32 GetUInt32() const;
|
||||
int32 GetInt32() const;
|
||||
uint64 GetUInt64() const;
|
||||
int64 GetInt64() const;
|
||||
float GetFloat() const;
|
||||
double GetDouble() const;
|
||||
SystemTimePoint GetDate() const;
|
||||
char const* GetCString() const;
|
||||
std::string GetString() const;
|
||||
std::string_view GetStringView() const;
|
||||
std::vector<uint8> GetBinary() const;
|
||||
std::span<uint8 const> GetBinaryView() const;
|
||||
uint8 GetUInt8() const noexcept;
|
||||
int8 GetInt8() const noexcept;
|
||||
uint16 GetUInt16() const noexcept;
|
||||
int16 GetInt16() const noexcept;
|
||||
uint32 GetUInt32() const noexcept;
|
||||
int32 GetInt32() const noexcept;
|
||||
uint64 GetUInt64() const noexcept;
|
||||
int64 GetInt64() const noexcept;
|
||||
float GetFloat() const noexcept;
|
||||
double GetDouble() const noexcept;
|
||||
SystemTimePoint GetDate() const noexcept;
|
||||
char const* GetCString() const noexcept;
|
||||
std::string GetString() const noexcept;
|
||||
std::string_view GetStringView() const noexcept;
|
||||
std::vector<uint8> GetBinary() const noexcept;
|
||||
std::span<uint8 const> GetBinaryView() const noexcept;
|
||||
template <size_t S>
|
||||
std::array<uint8, S> GetBinary() const
|
||||
std::array<uint8, S> GetBinary() const noexcept
|
||||
{
|
||||
std::array<uint8, S> buf;
|
||||
GetBinarySizeChecked(buf.data(), S);
|
||||
return buf;
|
||||
}
|
||||
|
||||
bool IsNull() const
|
||||
bool IsNull() const noexcept
|
||||
{
|
||||
return _value == nullptr;
|
||||
}
|
||||
|
||||
Optional<uint8> GetUInt8OrNull() const noexcept;
|
||||
Optional<int8> GetInt8OrNull() const noexcept;
|
||||
Optional<uint16> GetUInt16OrNull() const noexcept;
|
||||
Optional<int16> GetInt16OrNull() const noexcept;
|
||||
Optional<uint32> GetUInt32OrNull() const noexcept;
|
||||
Optional<int32> GetInt32OrNull() const noexcept;
|
||||
Optional<uint64> GetUInt64OrNull() const noexcept;
|
||||
Optional<int64> GetInt64OrNull() const noexcept;
|
||||
Optional<float> GetFloatOrNull() const noexcept;
|
||||
Optional<double> GetDoubleOrNull() const noexcept;
|
||||
Optional<SystemTimePoint> GetDateOrNull() const noexcept;
|
||||
Optional<std::string> GetStringOrNull() const noexcept;
|
||||
Optional<std::string_view> GetStringViewOrNull() const noexcept;
|
||||
Optional<std::vector<uint8>> GetBinaryOrNull() const noexcept;
|
||||
Optional<std::span<uint8 const>> GetBinaryViewOrNull() const noexcept;
|
||||
template <size_t S>
|
||||
Optional<std::array<uint8, S>> GetBinaryOrNull() const noexcept
|
||||
{
|
||||
Optional<std::array<uint8, S>> buf;
|
||||
if (!IsNull())
|
||||
GetBinarySizeChecked(buf.emplace().data(), S);
|
||||
return buf;
|
||||
}
|
||||
|
||||
private:
|
||||
char const* _value; // Actual data in memory
|
||||
uint32 _length; // Length
|
||||
@@ -141,7 +166,7 @@ class TC_DATABASE_API Field
|
||||
QueryResultFieldMetadata const* _meta;
|
||||
void SetMetadata(QueryResultFieldMetadata const* meta);
|
||||
|
||||
void GetBinarySizeChecked(uint8* buf, size_t size) const;
|
||||
void GetBinarySizeChecked(uint8* buf, size_t size) const noexcept;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -46,7 +46,7 @@ public:
|
||||
virtual SystemTimePoint GetDate(char const* data, uint32 size, QueryResultFieldMetadata const* meta) const = 0;
|
||||
virtual char const* GetCString(char const* data, uint32 size, QueryResultFieldMetadata const* meta) const = 0;
|
||||
|
||||
static void LogTruncation(char const* getter, QueryResultFieldMetadata const* meta);
|
||||
[[noreturn]] static void LogTruncation(char const* getter, QueryResultFieldMetadata const* meta);
|
||||
};
|
||||
|
||||
#endif // TRINITY_FIELD_VALUE_CONVERTER_H
|
||||
|
||||
Reference in New Issue
Block a user