AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
DBCDatabaseLoader Struct Reference

#include "DBCDatabaseLoader.h"

Public Member Functions

 DBCDatabaseLoader (char const *dbTable, char const *dbcFormatString, std::vector< char * > &stringPool)
 
char * Load (uint32 &records, char **&indexTable)
 

Private Member Functions

char * CloneStringToPool (std::string const &str)
 
 DBCDatabaseLoader (DBCDatabaseLoader const &right)=delete
 
DBCDatabaseLoaderoperator= (DBCDatabaseLoader const &right)=delete
 

Private Attributes

char const * _sqlTableName
 
char const * _dbcFormat
 
int32 _sqlIndexPos
 
uint32 _recordSize
 
std::vector< char * > & _stringPool
 

Detailed Description

Constructor & Destructor Documentation

◆ DBCDatabaseLoader() [1/2]

DBCDatabaseLoader::DBCDatabaseLoader ( char const *  dbTable,
char const *  dbcFormatString,
std::vector< char * > &  stringPool 
)
25 : _sqlTableName(tableName),
26 _dbcFormat(dbcFormatString),
27 _sqlIndexPos(0),
28 _recordSize(0),
29 _stringPool(stringPool)
30{
31 // Get sql index position
32 int32 indexPos = -1;
34
36}
#define ASSERT
Definition: Errors.h:68
std::int32_t int32
Definition: Define.h:103
static uint32 GetFormatRecordSize(const char *format, int32 *index_pos=nullptr)
Definition: DBCFileLoader.cpp:128
std::vector< char * > & _stringPool
Definition: DBCDatabaseLoader.h:35
uint32 _recordSize
Definition: DBCDatabaseLoader.h:34
char const * _dbcFormat
Definition: DBCDatabaseLoader.h:32
char const * _sqlTableName
Definition: DBCDatabaseLoader.h:31
int32 _sqlIndexPos
Definition: DBCDatabaseLoader.h:33

References _dbcFormat, _recordSize, ASSERT, and DBCFileLoader::GetFormatRecordSize().

◆ DBCDatabaseLoader() [2/2]

DBCDatabaseLoader::DBCDatabaseLoader ( DBCDatabaseLoader const &  right)
privatedelete

Member Function Documentation

◆ CloneStringToPool()

char * DBCDatabaseLoader::CloneStringToPool ( std::string const &  str)
private
137{
138 char* buf = new char[str.size() + 1];
139 memcpy(buf, str.c_str(), str.size() + 1);
140 _stringPool.push_back(buf);
141 return buf;
142}

References _stringPool.

Referenced by Load().

◆ Load()

char * DBCDatabaseLoader::Load ( uint32 records,
char **&  indexTable 
)
39{
40 std::string query = Acore::StringFormat("SELECT * FROM `{}` ORDER BY `ID` DESC", _sqlTableName);
41
42 // no error if empty set
43 QueryResult result = WorldDatabase.Query(query);
44 if (!result)
45 return nullptr;
46
47 // Check if sql index pos is valid
48 if (int32(result->GetFieldCount() - 1) < _sqlIndexPos)
49 {
50 ASSERT(false, "Invalid index pos for dbc: '{}'", _sqlTableName);
51 return nullptr;
52 }
53
54 // Resize index table
55 // database query *MUST* contain ORDER BY `index_field` DESC clause
56 uint32 indexTableSize = std::max(records, (*result)[_sqlIndexPos].Get<uint32>() + 1);
57 if (indexTableSize > records)
58 {
59 char** tmpIdxTable = new char* [indexTableSize];
60 memset(tmpIdxTable, 0, indexTableSize * sizeof(char*));
61 memcpy(tmpIdxTable, indexTable, records * sizeof(char*));
62 delete[] indexTable;
63 indexTable = tmpIdxTable;
64 }
65
66 std::unique_ptr<char[]> dataTable = std::make_unique<char[]>(result->GetRowCount() * _recordSize);
67 std::unique_ptr<uint32[]> newIndexes = std::make_unique<uint32[]>(result->GetRowCount());
68 uint32 newRecords = 0;
69
70 // Insert sql data into the data array
71 do
72 {
73 Field* fields = result->Fetch();
74 uint32 indexValue = fields[_sqlIndexPos].Get<uint32>();
75 char* dataValue = indexTable[indexValue];
76
77 // If exist in DBC file override from DB
78 newIndexes[newRecords] = indexValue;
79 dataValue = &dataTable[newRecords++ * _recordSize];
80
81 uint32 dataOffset = 0;
82 uint32 sqlColumnNumber = 0;
83 char const* dbcFormat = _dbcFormat;
84
85 for (; (*dbcFormat); ++dbcFormat)
86 {
87 switch (*dbcFormat)
88 {
89 case FT_FLOAT:
90 *reinterpret_cast<float*>(&dataValue[dataOffset]) = fields[sqlColumnNumber].Get<float>();
91 dataOffset += sizeof(float);
92 break;
93 case FT_IND:
94 case FT_INT:
95 *reinterpret_cast<uint32*>(&dataValue[dataOffset]) = fields[sqlColumnNumber].Get<uint32>();
96 dataOffset += sizeof(uint32);
97 break;
98 case FT_BYTE:
99 *reinterpret_cast<uint8*>(&dataValue[dataOffset]) = fields[sqlColumnNumber].Get<uint8>();
100 dataOffset += sizeof(uint8);
101 break;
102 case FT_STRING:
103 *reinterpret_cast<char**>(&dataValue[dataOffset]) = CloneStringToPool(fields[sqlColumnNumber].Get<std::string>());
104 dataOffset += sizeof(char*);
105 break;
106 case FT_SORT:
107 case FT_NA:
108 case FT_NA_BYTE:
109 break;
110 default:
111 ASSERT(false, "Unsupported data type '{}' in table '{}'", *dbcFormat, _sqlTableName);
112 return nullptr;
113 }
114
115 ++sqlColumnNumber;
116 }
117
118 ASSERT(sqlColumnNumber == result->GetFieldCount(), "SQL format string does not match database for table: '{}'", _sqlTableName);
119 ASSERT(dataOffset == _recordSize);
120 } while (result->NextRow());
121
122 ASSERT(newRecords == result->GetRowCount());
123
124 // insert new records to index table
125 for (uint32 i = 0; i < newRecords; ++i)
126 {
127 // cppcheck-suppress autoVariables
128 indexTable[newIndexes[i]] = &dataTable[i * _recordSize];
129 }
130
131 records = indexTableSize;
132
133 return dataTable.release();
134}
@ FT_IND
Definition: DBCFileLoader.h:34
@ FT_NA
Definition: DBCFileLoader.h:27
@ FT_FLOAT
Definition: DBCFileLoader.h:30
@ FT_STRING
Definition: DBCFileLoader.h:29
@ FT_SORT
Definition: DBCFileLoader.h:33
@ FT_NA_BYTE
Definition: DBCFileLoader.h:28
@ FT_INT
Definition: DBCFileLoader.h:31
@ FT_BYTE
Definition: DBCFileLoader.h:32
std::uint8_t uint8
Definition: Define.h:109
std::uint32_t uint32
Definition: Define.h:107
std::shared_ptr< ResultSet > QueryResult
Definition: DatabaseEnvFwd.h:27
DatabaseWorkerPool< WorldDatabaseConnection > WorldDatabase
Accessor to the world database.
Definition: DatabaseEnv.cpp:20
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default AC string format function.
Definition: StringFormat.h:34
Class used to access individual fields of database query result.
Definition: Field.h:98
std::enable_if_t< std::is_arithmetic_v< T >, T > Get() const
Definition: Field.h:112
char * CloneStringToPool(std::string const &str)
Definition: DBCDatabaseLoader.cpp:136

References _dbcFormat, _recordSize, _sqlIndexPos, _sqlTableName, ASSERT, CloneStringToPool(), FT_BYTE, FT_FLOAT, FT_IND, FT_INT, FT_NA, FT_NA_BYTE, FT_SORT, FT_STRING, Field::Get(), Acore::StringFormat(), and WorldDatabase.

◆ operator=()

DBCDatabaseLoader & DBCDatabaseLoader::operator= ( DBCDatabaseLoader const &  right)
privatedelete

Member Data Documentation

◆ _dbcFormat

char const* DBCDatabaseLoader::_dbcFormat
private

Referenced by DBCDatabaseLoader(), and Load().

◆ _recordSize

uint32 DBCDatabaseLoader::_recordSize
private

Referenced by DBCDatabaseLoader(), and Load().

◆ _sqlIndexPos

int32 DBCDatabaseLoader::_sqlIndexPos
private

Referenced by Load().

◆ _sqlTableName

char const* DBCDatabaseLoader::_sqlTableName
private

Referenced by Load().

◆ _stringPool

std::vector<char*>& DBCDatabaseLoader::_stringPool
private

Referenced by CloneStringToPool().