2010-10-07 15:35:36 +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:35:36 +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-09-03 00:04:14 +02:00
|
|
|
#include "Field.h"
|
2020-08-02 22:52:21 +02:00
|
|
|
#include "Errors.h"
|
2023-05-14 16:30:05 +02:00
|
|
|
#include "FieldValueConverter.h"
|
2022-02-11 14:33:35 +01:00
|
|
|
#include <cstring>
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2023-05-14 16:30:05 +02:00
|
|
|
Field::Field() : _value(nullptr), _length(0), _meta(nullptr)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
|
|
|
|
}
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2020-07-05 12:17:47 +02:00
|
|
|
Field::~Field() = default;
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
uint8 Field::GetUInt8() const noexcept
|
2017-05-12 18:49:51 +02:00
|
|
|
{
|
2025-06-24 15:41:25 +02:00
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetUInt8(_value, _length, _meta);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2017-05-12 18:49:51 +02:00
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
int8 Field::GetInt8() const noexcept
|
|
|
|
|
{
|
|
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetInt8(_value, _length, _meta);
|
|
|
|
|
return 0;
|
2017-05-12 18:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
uint16 Field::GetUInt16() const noexcept
|
2017-05-12 18:49:51 +02:00
|
|
|
{
|
2025-06-24 15:41:25 +02:00
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetUInt16(_value, _length, _meta);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2017-05-12 18:49:51 +02:00
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
int16 Field::GetInt16() const noexcept
|
|
|
|
|
{
|
|
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetInt16(_value, _length, _meta);
|
|
|
|
|
return 0;
|
2017-05-12 18:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
uint32 Field::GetUInt32() const noexcept
|
2017-05-12 18:49:51 +02:00
|
|
|
{
|
2025-06-24 15:41:25 +02:00
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetUInt32(_value, _length, _meta);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2017-05-12 18:49:51 +02:00
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
int32 Field::GetInt32() const noexcept
|
|
|
|
|
{
|
|
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetInt32(_value, _length, _meta);
|
|
|
|
|
return 0;
|
2017-05-12 18:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
uint64 Field::GetUInt64() const noexcept
|
2017-05-12 18:49:51 +02:00
|
|
|
{
|
2025-06-24 15:41:25 +02:00
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetUInt64(_value, _length, _meta);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2017-05-12 18:49:51 +02:00
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
int64 Field::GetInt64() const noexcept
|
|
|
|
|
{
|
|
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetInt64(_value, _length, _meta);
|
|
|
|
|
return 0;
|
2017-05-12 18:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
float Field::GetFloat() const noexcept
|
2017-05-12 18:49:51 +02:00
|
|
|
{
|
2025-06-24 15:41:25 +02:00
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetFloat(_value, _length, _meta);
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
2017-05-12 18:49:51 +02:00
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
double Field::GetDouble() const noexcept
|
|
|
|
|
{
|
|
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetDouble(_value, _length, _meta);
|
|
|
|
|
return 0.0;
|
2017-05-12 18:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
SystemTimePoint Field::GetDate() const noexcept
|
2017-05-12 18:49:51 +02:00
|
|
|
{
|
2025-06-24 15:41:25 +02:00
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetDate(_value, _length, _meta);
|
|
|
|
|
return SystemTimePoint::min();
|
|
|
|
|
}
|
2017-05-12 18:49:51 +02:00
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
char const* Field::GetCString() const noexcept
|
|
|
|
|
{
|
|
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetCString(_value, _length, _meta);
|
|
|
|
|
return nullptr;
|
2017-05-12 18:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
std::string Field::GetString() const noexcept
|
2017-05-12 18:49:51 +02:00
|
|
|
{
|
2025-06-24 15:41:25 +02:00
|
|
|
return std::string(GetStringView());
|
|
|
|
|
}
|
2017-05-12 18:49:51 +02:00
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
std::string_view Field::GetStringView() const noexcept
|
|
|
|
|
{
|
|
|
|
|
if (char const* string = GetCString())
|
|
|
|
|
return { string, _length };
|
|
|
|
|
return {};
|
2017-05-12 18:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
std::vector<uint8> Field::GetBinary() const noexcept
|
2017-05-12 18:49:51 +02:00
|
|
|
{
|
2025-06-24 15:41:25 +02:00
|
|
|
std::span<uint8 const> binary = GetBinaryView();
|
|
|
|
|
return { binary.begin(), binary.end() };
|
|
|
|
|
}
|
2017-05-12 18:49:51 +02:00
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
std::span<uint8 const> Field::GetBinaryView() const noexcept
|
|
|
|
|
{
|
|
|
|
|
return { reinterpret_cast<uint8 const*>(_value), _length };
|
2017-05-12 18:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
Optional<uint8> Field::GetUInt8OrNull() const noexcept
|
2017-05-12 18:49:51 +02:00
|
|
|
{
|
2025-06-24 15:41:25 +02:00
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetUInt8(_value, _length, _meta);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2017-05-12 18:49:51 +02:00
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
Optional<int8> Field::GetInt8OrNull() const noexcept
|
|
|
|
|
{
|
|
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetInt8(_value, _length, _meta);
|
|
|
|
|
return {};
|
2017-05-12 18:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
Optional<uint16> Field::GetUInt16OrNull() const noexcept
|
2017-05-12 18:49:51 +02:00
|
|
|
{
|
2025-06-24 15:41:25 +02:00
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetUInt16(_value, _length, _meta);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2017-05-12 18:49:51 +02:00
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
Optional<int16> Field::GetInt16OrNull() const noexcept
|
|
|
|
|
{
|
|
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetInt16(_value, _length, _meta);
|
|
|
|
|
return {};
|
2017-05-12 18:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
Optional<uint32> Field::GetUInt32OrNull() const noexcept
|
2024-04-10 13:59:19 +02:00
|
|
|
{
|
2025-06-24 15:41:25 +02:00
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetUInt32(_value, _length, _meta);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2024-04-10 13:59:19 +02:00
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
Optional<int32> Field::GetInt32OrNull() const noexcept
|
|
|
|
|
{
|
|
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetInt32(_value, _length, _meta);
|
|
|
|
|
return {};
|
2024-04-10 13:59:19 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
Optional<uint64> Field::GetUInt64OrNull() const noexcept
|
2017-05-12 18:49:51 +02:00
|
|
|
{
|
2025-06-24 15:41:25 +02:00
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetUInt64(_value, _length, _meta);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2017-05-12 18:49:51 +02:00
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
Optional<int64> Field::GetInt64OrNull() const noexcept
|
|
|
|
|
{
|
|
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetInt64(_value, _length, _meta);
|
|
|
|
|
return {};
|
2017-05-12 18:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
Optional<float> Field::GetFloatOrNull() const noexcept
|
2017-05-12 18:49:51 +02:00
|
|
|
{
|
2025-06-24 15:41:25 +02:00
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetFloat(_value, _length, _meta);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2017-05-12 18:49:51 +02:00
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
Optional<double> Field::GetDoubleOrNull() const noexcept
|
|
|
|
|
{
|
|
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetDouble(_value, _length, _meta);
|
|
|
|
|
return {};
|
2017-05-12 18:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
Optional<SystemTimePoint> Field::GetDateOrNull() const noexcept
|
2020-08-28 00:11:16 +02:00
|
|
|
{
|
2025-06-24 15:41:25 +02:00
|
|
|
if (_value)
|
|
|
|
|
return _meta->Converter->GetDate(_value, _length, _meta);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2020-08-28 00:11:16 +02:00
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
Optional<std::string> Field::GetStringOrNull() const noexcept
|
|
|
|
|
{
|
|
|
|
|
if (Optional<std::string_view> string = GetStringViewOrNull())
|
|
|
|
|
return Optional<std::string>(std::in_place, *string);
|
|
|
|
|
return {};
|
2020-08-28 00:11:16 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
Optional<std::string_view> Field::GetStringViewOrNull() const noexcept
|
2017-05-12 18:49:51 +02:00
|
|
|
{
|
2025-06-24 15:41:25 +02:00
|
|
|
if (char const* string = GetCString())
|
|
|
|
|
return Optional<std::string_view>(std::in_place, string, _length);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2017-05-12 18:49:51 +02:00
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
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 {};
|
2017-05-12 18:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
Optional<std::span<uint8 const>> Field::GetBinaryViewOrNull() const noexcept
|
2025-01-03 13:04:19 +01:00
|
|
|
{
|
2025-06-24 15:41:25 +02:00
|
|
|
if (_value)
|
|
|
|
|
return Optional<std::span<uint8 const>>(std::in_place, reinterpret_cast<uint8 const*>(_value), _length);
|
|
|
|
|
return {};
|
2025-01-03 13:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 15:41:25 +02:00
|
|
|
void Field::GetBinarySizeChecked(uint8* buf, size_t length) const noexcept
|
2020-08-02 22:52:21 +02:00
|
|
|
{
|
2023-05-14 16:30:05 +02:00
|
|
|
ASSERT(_value && (_length == length), "Expected %zu-byte binary blob, got %sdata (%u bytes) instead", length, _value ? "" : "no ", _length);
|
|
|
|
|
memcpy(buf, _value, length);
|
2020-08-02 22:52:21 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-14 16:30:05 +02:00
|
|
|
void Field::SetValue(char const* newValue, uint32 length)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
2013-12-25 14:16:55 -03:30
|
|
|
// This value stores raw bytes that have to be explicitly cast later
|
2023-05-14 16:30:05 +02:00
|
|
|
_value = newValue;
|
|
|
|
|
_length = length;
|
2017-05-12 18:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-14 16:30:05 +02:00
|
|
|
void Field::SetMetadata(QueryResultFieldMetadata const* meta)
|
2017-05-12 18:49:51 +02:00
|
|
|
{
|
2023-05-14 16:30:05 +02:00
|
|
|
_meta = meta;
|
2017-05-12 18:49:51 +02:00
|
|
|
}
|