diff options
Diffstat (limited to 'src/server/database/Database/Field.cpp')
-rw-r--r-- | src/server/database/Database/Field.cpp | 218 |
1 files changed, 150 insertions, 68 deletions
diff --git a/src/server/database/Database/Field.cpp b/src/server/database/Database/Field.cpp index 6045e8dee81..0c936800f8a 100644 --- a/src/server/database/Database/Field.cpp +++ b/src/server/database/Database/Field.cpp @@ -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; + if (_value) + return _meta->Converter->GetUInt8(_value, _length, _meta); + return 0; +} - return _meta->Converter->GetUInt8(_value, _length, _meta); +int8 Field::GetInt8() const noexcept +{ + if (_value) + return _meta->Converter->GetInt8(_value, _length, _meta); + return 0; } -int8 Field::GetInt8() const +uint16 Field::GetUInt16() const noexcept { - if (!_value) - return 0; + if (_value) + return _meta->Converter->GetUInt16(_value, _length, _meta); + return 0; +} - return _meta->Converter->GetInt8(_value, _length, _meta); +int16 Field::GetInt16() const noexcept +{ + if (_value) + return _meta->Converter->GetInt16(_value, _length, _meta); + return 0; } -uint16 Field::GetUInt16() const +uint32 Field::GetUInt32() const noexcept { - if (!_value) - return 0; + if (_value) + return _meta->Converter->GetUInt32(_value, _length, _meta); + return 0; +} - return _meta->Converter->GetUInt16(_value, _length, _meta); +int32 Field::GetInt32() const noexcept +{ + if (_value) + return _meta->Converter->GetInt32(_value, _length, _meta); + return 0; } -int16 Field::GetInt16() const +uint64 Field::GetUInt64() const noexcept { - if (!_value) - return 0; + if (_value) + return _meta->Converter->GetUInt64(_value, _length, _meta); + return 0; +} - return _meta->Converter->GetInt16(_value, _length, _meta); +int64 Field::GetInt64() const noexcept +{ + if (_value) + return _meta->Converter->GetInt64(_value, _length, _meta); + return 0; } -uint32 Field::GetUInt32() const +float Field::GetFloat() const noexcept { - if (!_value) - return 0; + if (_value) + return _meta->Converter->GetFloat(_value, _length, _meta); + return 0.0f; +} - return _meta->Converter->GetUInt32(_value, _length, _meta); +double Field::GetDouble() const noexcept +{ + if (_value) + return _meta->Converter->GetDouble(_value, _length, _meta); + return 0.0; } -int32 Field::GetInt32() const +SystemTimePoint Field::GetDate() const noexcept { - if (!_value) - return 0; + if (_value) + return _meta->Converter->GetDate(_value, _length, _meta); + return SystemTimePoint::min(); +} - return _meta->Converter->GetInt32(_value, _length, _meta); +char const* Field::GetCString() const noexcept +{ + if (_value) + return _meta->Converter->GetCString(_value, _length, _meta); + return nullptr; } -uint64 Field::GetUInt64() const +std::string Field::GetString() const noexcept { - if (!_value) - return 0; + return std::string(GetStringView()); +} - return _meta->Converter->GetUInt64(_value, _length, _meta); +std::string_view Field::GetStringView() const noexcept +{ + if (char const* string = GetCString()) + return { string, _length }; + return {}; } -int64 Field::GetInt64() const +std::vector<uint8> Field::GetBinary() const noexcept { - if (!_value) - return 0; + std::span<uint8 const> binary = GetBinaryView(); + return { binary.begin(), binary.end() }; +} - return _meta->Converter->GetInt64(_value, _length, _meta); +std::span<uint8 const> Field::GetBinaryView() const noexcept +{ + return { reinterpret_cast<uint8 const*>(_value), _length }; } -float Field::GetFloat() const +Optional<uint8> Field::GetUInt8OrNull() const noexcept { - if (!_value) - return 0.0f; + if (_value) + return _meta->Converter->GetUInt8(_value, _length, _meta); + return {}; +} - return _meta->Converter->GetFloat(_value, _length, _meta); +Optional<int8> Field::GetInt8OrNull() const noexcept +{ + if (_value) + return _meta->Converter->GetInt8(_value, _length, _meta); + return {}; } -double Field::GetDouble() const +Optional<uint16> Field::GetUInt16OrNull() const noexcept { - if (!_value) - return 0.0; + if (_value) + return _meta->Converter->GetUInt16(_value, _length, _meta); + return {}; +} - return _meta->Converter->GetDouble(_value, _length, _meta); +Optional<int16> Field::GetInt16OrNull() const noexcept +{ + if (_value) + return _meta->Converter->GetInt16(_value, _length, _meta); + return {}; } -SystemTimePoint Field::GetDate() const +Optional<uint32> Field::GetUInt32OrNull() const noexcept { - if (!_value) - return SystemTimePoint::min(); + if (_value) + return _meta->Converter->GetUInt32(_value, _length, _meta); + return {}; +} - return _meta->Converter->GetDate(_value, _length, _meta); +Optional<int32> Field::GetInt32OrNull() const noexcept +{ + if (_value) + return _meta->Converter->GetInt32(_value, _length, _meta); + return {}; } -char const* Field::GetCString() const +Optional<uint64> Field::GetUInt64OrNull() const noexcept { - if (!_value) - return nullptr; + if (_value) + return _meta->Converter->GetUInt64(_value, _length, _meta); + return {}; +} - return _meta->Converter->GetCString(_value, _length, _meta); +Optional<int64> Field::GetInt64OrNull() const noexcept +{ + if (_value) + return _meta->Converter->GetInt64(_value, _length, _meta); + return {}; } -std::string Field::GetString() const +Optional<float> Field::GetFloatOrNull() const noexcept { - std::string result; - if (char const* string = GetCString()) - result.assign(string, _length); + if (_value) + return _meta->Converter->GetFloat(_value, _length, _meta); + return {}; +} - return result; +Optional<double> Field::GetDoubleOrNull() const noexcept +{ + if (_value) + return _meta->Converter->GetDouble(_value, _length, _meta); + return {}; } -std::string_view Field::GetStringView() const +Optional<SystemTimePoint> Field::GetDateOrNull() const noexcept { - std::string_view result; - if (char const* const string = GetCString()) - result = { string, _length }; + if (_value) + return _meta->Converter->GetDate(_value, _length, _meta); + return {}; +} - return result; +Optional<std::string> Field::GetStringOrNull() const noexcept +{ + if (Optional<std::string_view> string = GetStringViewOrNull()) + return Optional<std::string>(std::in_place, *string); + return {}; } -std::vector<uint8> Field::GetBinary() const +Optional<std::string_view> Field::GetStringViewOrNull() const noexcept { - std::vector<uint8> result; - if (!_value || !_length) - return result; + if (char const* string = GetCString()) + return Optional<std::string_view>(std::in_place, string, _length); + return {}; +} - result.resize(_length); - memcpy(result.data(), _value, _length); - return result; +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 {}; } -std::span<uint8 const> Field::GetBinaryView() const +Optional<std::span<uint8 const>> Field::GetBinaryViewOrNull() const noexcept { - return { reinterpret_cast<uint8 const*>(_value), _length }; + 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 +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); |