WoW Model Viewer
Your premiere tool for viewing, equipping and animating World of Warcraft models.
Loading...
Searching...
No Matches
DB2Table.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <cstring>
5#include <memory>
6#include <string>
7#include <vector>
8#include <unordered_map>
9
10class DB2Reader;
11
14{
15 std::string name;
16 std::string type;
17 int pos = -1;
18 unsigned int arraySize = 1;
19 bool isKey = false;
20 bool isRelationshipData = false;
21};
22
23class DB2Table;
24
26class DB2Row
27{
28public:
29 DB2Row() = default;
30 DB2Row(const DB2Table* table, size_t recordIndex)
31 : m_table(table), m_recordIndex(recordIndex) {}
32
33 bool isValid() const { return m_table != nullptr; }
34 explicit operator bool() const { return isValid(); }
35
36 uint32_t recordID() const;
37
38 uint32_t getUInt(const std::string& field, unsigned int arrayIndex = 0) const;
39 int32_t getInt(const std::string& field, unsigned int arrayIndex = 0) const;
40 float getFloat(const std::string& field, unsigned int arrayIndex = 0) const;
41 std::string getString(const std::string& field, unsigned int arrayIndex = 0) const;
42
43private:
44 const DB2Table* m_table = nullptr;
45 size_t m_recordIndex = SIZE_MAX;
46};
47
50{
51public:
52 DB2Table(std::unique_ptr<DB2Reader> reader, std::vector<DB2FieldInfo> fields);
54
55 DB2Table(const DB2Table&) = delete;
56 DB2Table& operator=(const DB2Table&) = delete;
57
58 // Lookup by primary key ID
59 DB2Row getRow(uint32_t id) const;
60
61 // Linear access by index (0-based into the record locations array)
62 DB2Row getRowByIndex(size_t index) const;
63 size_t getRowCount() const;
64
65 // Range-based for support
67 {
68 public:
69 Iterator(const DB2Table* table, size_t index) : m_table(table), m_index(index) {}
70 DB2Row operator*() const { return DB2Row(m_table, m_index); }
71 Iterator& operator++() { ++m_index; return *this; }
72 bool operator!=(const Iterator& other) const { return m_index != other.m_index; }
73 private:
75 size_t m_index;
76 };
77
78 Iterator begin() const { return Iterator(this, 0); }
79 Iterator end() const { return Iterator(this, getRowCount()); }
80
81 // Field info lookup by base name
82 const DB2FieldInfo* findField(const std::string& name) const;
83
84private:
85 friend class DB2Row;
86
87 // Resolve a field name, handling SQL-style expanded array names (e.g. "Field1" -> "Field", arrayIndex=0).
88 // arrayIndex is both input (caller-specified) and output (overwritten for expanded names).
89 const DB2FieldInfo* resolveField(const std::string& name, unsigned int& arrayIndex) const;
90
91 // Read a raw uint32 value for a given field from a record.
92 uint32_t readUInt(size_t recordIndex, const DB2FieldInfo& field, unsigned int arrayIndex) const;
93
94 // Read a string value for a given field from a record.
95 std::string readString(size_t recordIndex, const DB2FieldInfo& field, unsigned int arrayIndex) const;
96
97 // Get the record ID for a given record index.
98 uint32_t getRecordID(size_t recordIndex) const;
99
100 std::unique_ptr<DB2Reader> m_reader;
101 std::vector<DB2FieldInfo> m_fields;
102 std::unordered_map<std::string, size_t> m_fieldNameToIndex; // base name -> index in m_fields
103};
Unified reader for WDC2, WDC3, WDC4 and WDC5 DB2 files.
Definition DB2Reader.h:16
Lightweight handle to a single row in a DB2Table.
Definition DB2Table.h:27
DB2Row()=default
std::string getString(const std::string &field, unsigned int arrayIndex=0) const
Definition DB2Table.cpp:67
float getFloat(const std::string &field, unsigned int arrayIndex=0) const
Definition DB2Table.cpp:54
size_t m_recordIndex
Definition DB2Table.h:45
int32_t getInt(const std::string &field, unsigned int arrayIndex=0) const
Definition DB2Table.cpp:26
uint32_t recordID() const
Definition DB2Table.cpp:11
const DB2Table * m_table
Definition DB2Table.h:44
uint32_t getUInt(const std::string &field, unsigned int arrayIndex=0) const
Definition DB2Table.cpp:17
DB2Row(const DB2Table *table, size_t recordIndex)
Definition DB2Table.h:30
bool isValid() const
Definition DB2Table.h:33
const DB2Table * m_table
Definition DB2Table.h:74
Iterator & operator++()
Definition DB2Table.h:71
bool operator!=(const Iterator &other) const
Definition DB2Table.h:72
DB2Row operator*() const
Definition DB2Table.h:70
Iterator(const DB2Table *table, size_t index)
Definition DB2Table.h:69
Provides typed, field-name-based access to records in a WDC DB2 file.
Definition DB2Table.h:50
Iterator begin() const
Definition DB2Table.h:78
DB2Row getRow(uint32_t id) const
Definition DB2Table.cpp:97
const DB2FieldInfo * findField(const std::string &name) const
Definition DB2Table.cpp:117
uint32_t readUInt(size_t recordIndex, const DB2FieldInfo &field, unsigned int arrayIndex) const
Definition DB2Table.cpp:163
std::vector< DB2FieldInfo > m_fields
Definition DB2Table.h:101
size_t getRowCount() const
Definition DB2Table.cpp:112
DB2Table(const DB2Table &)=delete
Iterator end() const
Definition DB2Table.h:79
DB2Row getRowByIndex(size_t index) const
Definition DB2Table.cpp:105
std::unordered_map< std::string, size_t > m_fieldNameToIndex
Definition DB2Table.h:102
friend class DB2Row
Definition DB2Table.h:85
std::string readString(size_t recordIndex, const DB2FieldInfo &field, unsigned int arrayIndex) const
Definition DB2Table.cpp:184
DB2Table & operator=(const DB2Table &)=delete
std::unique_ptr< DB2Reader > m_reader
Definition DB2Table.h:100
const DB2FieldInfo * resolveField(const std::string &name, unsigned int &arrayIndex) const
Definition DB2Table.cpp:125
uint32_t getRecordID(size_t recordIndex) const
Definition DB2Table.cpp:158
Describes a single field (column) in a DB2 table.
Definition DB2Table.h:14
bool isRelationshipData
Whether this field is relationship data.
Definition DB2Table.h:20
int pos
DB2 field position index (-1 for non-inline/key).
Definition DB2Table.h:17
std::string name
Column name.
Definition DB2Table.h:15
std::string type
Type string ("text", "float", "int8", "uint8", etc.).
Definition DB2Table.h:16
unsigned int arraySize
Array size (> 1 for array fields).
Definition DB2Table.h:18
bool isKey
Whether this field is the primary key.
Definition DB2Table.h:19