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

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
136{
137 char* buf = new char[str.size() + 1];
138 memcpy(buf, str.c_str(), str.size() + 1);
139 _stringPool.push_back(buf);
140 return buf;
141}

References _stringPool.

Referenced by Load().

◆ Load()

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

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().