Files
TCPlayerbotCore/src/server/database/Database/Field.cpp
T

170 lines
3.5 KiB
C++
Raw Normal View History

/*
2020-01-02 06:44:10 +01:00
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* 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"
#include "Errors.h"
#include "FieldValueConverter.h"
2017-05-12 18:49:51 +02:00
#include "Log.h"
#include <cstring>
2009-10-17 15:51:44 -07: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
Field::~Field() = default;
2009-10-17 15:51:44 -07:00
2017-05-12 18:49:51 +02:00
uint8 Field::GetUInt8() const
{
if (!_value)
2017-05-12 18:49:51 +02:00
return 0;
return _meta->Converter->GetUInt8(_value, _length, _meta);
2017-05-12 18:49:51 +02:00
}
int8 Field::GetInt8() const
{
if (!_value)
2017-05-12 18:49:51 +02:00
return 0;
return _meta->Converter->GetInt8(_value, _length, _meta);
2017-05-12 18:49:51 +02:00
}
uint16 Field::GetUInt16() const
{
if (!_value)
2017-05-12 18:49:51 +02:00
return 0;
return _meta->Converter->GetUInt16(_value, _length, _meta);
2017-05-12 18:49:51 +02:00
}
int16 Field::GetInt16() const
{
if (!_value)
2017-05-12 18:49:51 +02:00
return 0;
return _meta->Converter->GetInt16(_value, _length, _meta);
2017-05-12 18:49:51 +02:00
}
uint32 Field::GetUInt32() const
{
if (!_value)
2017-05-12 18:49:51 +02:00
return 0;
return _meta->Converter->GetUInt32(_value, _length, _meta);
2017-05-12 18:49:51 +02:00
}
int32 Field::GetInt32() const
{
if (!_value)
2017-05-12 18:49:51 +02:00
return 0;
return _meta->Converter->GetInt32(_value, _length, _meta);
2017-05-12 18:49:51 +02:00
}
uint64 Field::GetUInt64() const
{
if (!_value)
2017-05-12 18:49:51 +02:00
return 0;
return _meta->Converter->GetUInt64(_value, _length, _meta);
2017-05-12 18:49:51 +02:00
}
int64 Field::GetInt64() const
{
if (!_value)
2017-05-12 18:49:51 +02:00
return 0;
return _meta->Converter->GetInt64(_value, _length, _meta);
2017-05-12 18:49:51 +02:00
}
float Field::GetFloat() const
{
if (!_value)
2017-05-12 18:49:51 +02:00
return 0.0f;
return _meta->Converter->GetFloat(_value, _length, _meta);
2017-05-12 18:49:51 +02:00
}
double Field::GetDouble() const
{
if (!_value)
return 0.0;
2017-05-12 18:49:51 +02:00
return _meta->Converter->GetDouble(_value, _length, _meta);
2017-05-12 18:49:51 +02:00
}
char const* Field::GetCString() const
{
if (!_value)
return nullptr;
2017-05-12 18:49:51 +02:00
return _meta->Converter->GetCString(_value, _length, _meta);
2017-05-12 18:49:51 +02:00
}
std::string Field::GetString() const
{
if (!_value)
2017-05-12 18:49:51 +02:00
return "";
char const* string = GetCString();
if (!string)
return "";
return std::string(string, _length);
2017-05-12 18:49:51 +02:00
}
std::string_view Field::GetStringView() const
{
if (!_value)
return {};
char const* const string = GetCString();
if (!string)
return {};
return { string, _length };
}
2017-05-12 18:49:51 +02:00
std::vector<uint8> Field::GetBinary() const
{
std::vector<uint8> result;
if (!_value || !_length)
2017-05-12 18:49:51 +02:00
return result;
result.resize(_length);
memcpy(result.data(), _value, _length);
2017-05-12 18:49:51 +02:00
return result;
}
void Field::GetBinarySizeChecked(uint8* buf, size_t length) const
{
ASSERT(_value && (_length == length), "Expected %zu-byte binary blob, got %sdata (%u bytes) instead", length, _value ? "" : "no ", _length);
memcpy(buf, _value, length);
}
void Field::SetValue(char const* newValue, uint32 length)
2008-10-02 16:23:55 -05:00
{
// This value stores raw bytes that have to be explicitly cast later
_value = newValue;
_length = length;
2017-05-12 18:49:51 +02:00
}
void Field::SetMetadata(QueryResultFieldMetadata const* meta)
2017-05-12 18:49:51 +02:00
{
_meta = meta;
2017-05-12 18:49:51 +02:00
}