aboutsummaryrefslogtreecommitdiff
path: root/src/common/DataStores/DB2Meta.cpp
blob: 6ab1ddd8522075efe7c88b4222721783ca037fef (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
/*
 * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/>
 *
 * 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 "DB2Meta.h"
#include "Errors.h"

DB2FieldDefault::DB2FieldDefault(uint8 u8)
{
    AsUInt8 = u8;
}

DB2FieldDefault::DB2FieldDefault(uint16 u16)
{
    AsUInt16 = u16;
}

DB2FieldDefault::DB2FieldDefault(uint32 u32)
{
    AsUInt32 = u32;
}

DB2FieldDefault::DB2FieldDefault(float f)
{
    AsFloat = f;
}

DB2FieldDefault::DB2FieldDefault(char const* str)
{
    AsString = str;
}

DB2Meta::DB2Meta(int32 indexField, uint32 fieldCount, uint32 layoutHash, char const* types, uint8 const* arraySizes, DB2FieldDefault const* fieldDefaults)
    : IndexField(indexField), FieldCount(fieldCount), LayoutHash(layoutHash), Types(types), ArraySizes(arraySizes), FieldDefaults(fieldDefaults)
{
}

bool DB2Meta::HasIndexFieldInData() const
{
    return IndexField != -1;
}

uint32 DB2Meta::GetIndexField() const
{
    return IndexField == -1 ? 0 : uint32(IndexField);
}

uint32 DB2Meta::GetRecordSize() const
{
    uint32 size = 0;
    for (uint32 i = 0; i < FieldCount; ++i)
    {
        for (uint8 j = 0; j < ArraySizes[i]; ++j)
        {
            switch (Types[i])
            {
                case FT_BYTE:
                    size += 1;
                    break;
                case FT_SHORT:
                    size += 2;
                    break;
                case FT_FLOAT:
                case FT_INT:
                    size += 4;
                    break;
                case FT_STRING:
                    size += sizeof(char*);
                    break;
                default:
                    ASSERT(false, "Unsupported column type specified %c", Types[i]);
                    break;
            }
        }
    }

    if (!HasIndexFieldInData())
        size += 4;

    return size;
}

uint32 DB2Meta::GetDbIndexField() const
{
    if (IndexField == -1)
        return 0;

    uint32 index = 0;
    for (uint32 i = 0; i < FieldCount && i < uint32(IndexField); ++i)
        index += ArraySizes[i];

    return index;
}

uint32 DB2Meta::GetDbFieldCount() const
{
    uint32 fields = 0;
    for (uint32 i = 0; i < FieldCount; ++i)
        fields += ArraySizes[i];

    if (!HasIndexFieldInData())
        ++fields;

    return fields;
}

DB2FieldMeta::DB2FieldMeta(bool isSigned, DBCFormer type, char const* name)
    : IsSigned(isSigned), Type(type), Name(name)
{
}