aboutsummaryrefslogtreecommitdiff
path: root/src/server/database/Database/Field.cpp
blob: 6957cec00ad8a5ae9e2d4f60ce534d7eb2201403 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * 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/>.
 */

#include "Field.h"
#include "Errors.h"
#include "FieldValueConverter.h"
#include "Log.h"
#include <cstring>

Field::Field() : _value(nullptr), _length(0), _meta(nullptr)
{
}

Field::~Field() = default;

uint8 Field::GetUInt8() const
{
    if (!_value)
        return 0;

    return _meta->Converter->GetUInt8(_value, _length, _meta);
}

int8 Field::GetInt8() const
{
    if (!_value)
        return 0;

    return _meta->Converter->GetInt8(_value, _length, _meta);
}

uint16 Field::GetUInt16() const
{
    if (!_value)
        return 0;

    return _meta->Converter->GetUInt16(_value, _length, _meta);
}

int16 Field::GetInt16() const
{
    if (!_value)
        return 0;

    return _meta->Converter->GetInt16(_value, _length, _meta);
}

uint32 Field::GetUInt32() const
{
    if (!_value)
        return 0;

    return _meta->Converter->GetUInt32(_value, _length, _meta);
}

int32 Field::GetInt32() const
{
    if (!_value)
        return 0;

    return _meta->Converter->GetInt32(_value, _length, _meta);
}

uint64 Field::GetUInt64() const
{
    if (!_value)
        return 0;

    return _meta->Converter->GetUInt64(_value, _length, _meta);
}

int64 Field::GetInt64() const
{
    if (!_value)
        return 0;

    return _meta->Converter->GetInt64(_value, _length, _meta);
}

float Field::GetFloat() const
{
    if (!_value)
        return 0.0f;

    return _meta->Converter->GetFloat(_value, _length, _meta);
}

double Field::GetDouble() const
{
    if (!_value)
        return 0.0;

    return _meta->Converter->GetDouble(_value, _length, _meta);
}

char const* Field::GetCString() const
{
    if (!_value)
        return nullptr;

    return _meta->Converter->GetCString(_value, _length, _meta);
}

std::string Field::GetString() const
{
    if (!_value)
        return "";

    char const* string = GetCString();
    if (!string)
        return "";

    return std::string(string, _length);
}

std::string_view Field::GetStringView() const
{
    if (!_value)
        return {};

    char const* const string = GetCString();
    if (!string)
        return {};

    return { string, _length };
}

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;
}

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)
{
    // This value stores raw bytes that have to be explicitly cast later
    _value = newValue;
    _length = length;
}

void Field::SetMetadata(QueryResultFieldMetadata const* meta)
{
    _meta = meta;
}