TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DBCStorage< T > Class Template Reference

#include <DBCStore.h>

Public Types

typedef DBStorageIterator< T > iterator
 

Public Member Functions

 DBCStorage (char const *f)
 
 ~DBCStorage ()
 
T constLookupEntry (uint32 id) const
 
T constAssertEntry (uint32 id) const
 
uint32 GetNumRows () const
 
char constGetFormat () const
 
uint32 GetFieldCount () const
 
bool Load (char const *fn, SqlDbc *sql)
 
bool LoadStringsFrom (char const *fn)
 
void Clear ()
 
iterator begin ()
 
iterator end ()
 

Private Types

typedef std::list< char * > StringPoolList
 

Private Member Functions

 DBCStorage (DBCStorage const &right)=delete
 
DBCStorageoperator= (DBCStorage const &right)=delete
 

Private Attributes

char constfmt
 
uint32 nCount
 
uint32 fieldCount
 
union {
   T **   asT
 
   char **   asChar
 
indexTable
 
T * dataTable
 
StringPoolList stringPoolList
 

Member Typedef Documentation

template<class T>
typedef DBStorageIterator<T> DBCStorage< T >::iterator
template<class T>
typedef std::list<char*> DBCStorage< T >::StringPoolList
private

Constructor & Destructor Documentation

template<class T>
DBCStorage< T >::DBCStorage ( char const f)
inlineexplicit
82  : fmt(f), nCount(0), fieldCount(0), dataTable(NULL)
83  {
84  indexTable.asT = NULL;
85  }
uint32 fieldCount
Definition: DBCStore.h:309
arena_t NULL
Definition: jemalloc_internal.h:624
char const * fmt
Definition: DBCStore.h:307
T * dataTable
Definition: DBCStore.h:318
union DBCStorage::@339 indexTable
uint32 nCount
Definition: DBCStore.h:308
template<class T>
DBCStorage< T >::~DBCStorage ( )
inline
87 { Clear(); }
void Clear()
Definition: DBCStore.h:284

+ Here is the call graph for this function:

template<class T>
DBCStorage< T >::DBCStorage ( DBCStorage< T > const right)
privatedelete

Member Function Documentation

template<class T>
T const* DBCStorage< T >::AssertEntry ( uint32  id) const
inline
95  {
96  T const* entry = LookupEntry(id);
97  ASSERT(entry);
98  return entry;
99  }
#define ASSERT
Definition: Errors.h:55
T const * LookupEntry(uint32 id) const
Definition: DBCStore.h:89

+ Here is the call graph for this function:

template<class T>
iterator DBCStorage< T >::begin ( )
inline
303 { return iterator(indexTable.asT, nCount); }
DBStorageIterator< T > iterator
Definition: DBCStore.h:79
union DBCStorage::@339 indexTable
uint32 nCount
Definition: DBCStore.h:308
template<class T>
void DBCStorage< T >::Clear ( )
inline
285  {
286  if (!indexTable.asT)
287  return;
288 
289  delete[] reinterpret_cast<char*>(indexTable.asT);
290  indexTable.asT = NULL;
291  delete[] reinterpret_cast<char*>(dataTable);
292  dataTable = NULL;
293 
294  while (!stringPoolList.empty())
295  {
296  delete[] stringPoolList.front();
297  stringPoolList.pop_front();
298  }
299 
300  nCount = 0;
301  }
StringPoolList stringPoolList
Definition: DBCStore.h:319
arena_t NULL
Definition: jemalloc_internal.h:624
T * dataTable
Definition: DBCStore.h:318
union DBCStorage::@339 indexTable
uint32 nCount
Definition: DBCStore.h:308

+ Here is the caller graph for this function:

template<class T>
iterator DBCStorage< T >::end ( )
inline
304 { return iterator(indexTable.asT, nCount, nCount); }
DBStorageIterator< T > iterator
Definition: DBCStore.h:79
union DBCStorage::@339 indexTable
uint32 nCount
Definition: DBCStore.h:308
template<class T>
uint32 DBCStorage< T >::GetFieldCount ( ) const
inline
103 { return fieldCount; }
uint32 fieldCount
Definition: DBCStore.h:309

+ Here is the caller graph for this function:

template<class T>
char const* DBCStorage< T >::GetFormat ( ) const
inline
102 { return fmt; }
char const * fmt
Definition: DBCStore.h:307

+ Here is the caller graph for this function:

template<class T>
uint32 DBCStorage< T >::GetNumRows ( ) const
inline
101 { return nCount; }
uint32 nCount
Definition: DBCStore.h:308
template<class T>
bool DBCStorage< T >::Load ( char const fn,
SqlDbc sql 
)
inline
106  {
107  DBCFileLoader dbc;
108  // Check if load was successful, only then continue
109  if (!dbc.Load(fn, fmt))
110  return false;
111 
112  uint32 sqlRecordCount = 0;
113  uint32 sqlHighestIndex = 0;
114  Field* fields = NULL;
115  QueryResult result = QueryResult(NULL);
116  // Load data from sql
117  if (sql)
118  {
119  std::string query = "SELECT * FROM " + sql->sqlTableName;
120  if (sql->indexPos >= 0)
121  query +=" ORDER BY " + *sql->indexName + " DESC";
122  query += ';';
123 
124 
125  result = WorldDatabase.Query(query.c_str());
126  if (result)
127  {
128  sqlRecordCount = uint32(result->GetRowCount());
129  if (sql->indexPos >= 0)
130  {
131  fields = result->Fetch();
132  sqlHighestIndex = fields[sql->sqlIndexPos].GetUInt32();
133  }
134 
135  // Check if sql index pos is valid
136  if (int32(result->GetFieldCount() - 1) < sql->sqlIndexPos)
137  {
138  TC_LOG_ERROR("server.loading", "Invalid index pos for dbc:'%s'", sql->sqlTableName.c_str());
139  return false;
140  }
141  }
142  }
143 
144  char* sqlDataTable = NULL;
145  fieldCount = dbc.GetCols();
146 
147  dataTable = reinterpret_cast<T*>(dbc.AutoProduceData(fmt, nCount, indexTable.asChar,
148  sqlRecordCount, sqlHighestIndex, sqlDataTable));
149 
150  stringPoolList.push_back(dbc.AutoProduceStrings(fmt, reinterpret_cast<char*>(dataTable)));
151 
152  // Insert sql data into arrays
153  if (result)
154  {
155  if (indexTable.asT)
156  {
157  uint32 offset = 0;
158  uint32 rowIndex = dbc.GetNumRows();
159  do
160  {
161  if (!fields)
162  fields = result->Fetch();
163 
164  if (sql->indexPos >= 0)
165  {
166  uint32 id = fields[sql->sqlIndexPos].GetUInt32();
167  if (indexTable.asT[id])
168  {
169  TC_LOG_ERROR("server.loading", "Index %d already exists in dbc:'%s'", id, sql->sqlTableName.c_str());
170  return false;
171  }
172 
173  indexTable.asT[id] = reinterpret_cast<T*>(&sqlDataTable[offset]);
174  }
175  else
176  indexTable.asT[rowIndex]= reinterpret_cast<T*>(&sqlDataTable[offset]);
177 
178  uint32 columnNumber = 0;
179  uint32 sqlColumnNumber = 0;
180 
181  for (; columnNumber < sql->formatString->size(); ++columnNumber)
182  {
183  if ((*sql->formatString)[columnNumber] == FT_SQL_ABSENT)
184  {
185  switch (fmt[columnNumber])
186  {
187  case FT_FLOAT:
188  *reinterpret_cast<float*>(&sqlDataTable[offset]) = 0.0f;
189  offset += 4;
190  break;
191  case FT_IND:
192  case FT_INT:
193  *reinterpret_cast<uint32*>(&sqlDataTable[offset]) = uint32(0);
194  offset += 4;
195  break;
196  case FT_BYTE:
197  *reinterpret_cast<uint8*>(&sqlDataTable[offset]) = uint8(0);
198  offset += 1;
199  break;
200  case FT_LONG:
201  *reinterpret_cast<uint64*>(&sqlDataTable[offset]) = uint64(0);
202  offset += 8;
203  break;
204  case FT_STRING:
205  // Beginning of the pool - empty string
206  *reinterpret_cast<char**>(&sqlDataTable[offset]) = stringPoolList.back();
207  offset += sizeof(char*);
208  break;
209  }
210  }
211  else if ((*sql->formatString)[columnNumber] == FT_SQL_PRESENT)
212  {
213  bool validSqlColumn = true;
214  switch (fmt[columnNumber])
215  {
216  case FT_FLOAT:
217  *reinterpret_cast<float*>(&sqlDataTable[offset]) = fields[sqlColumnNumber].GetFloat();
218  offset += 4;
219  break;
220  case FT_IND:
221  case FT_INT:
222  *reinterpret_cast<uint32*>(&sqlDataTable[offset]) = fields[sqlColumnNumber].GetUInt32();
223  offset += 4;
224  break;
225  case FT_BYTE:
226  *reinterpret_cast<uint8*>(&sqlDataTable[offset]) = fields[sqlColumnNumber].GetUInt8();
227  offset += 1;
228  break;
229  case FT_LONG:
230  *reinterpret_cast<uint64*>(&sqlDataTable[offset]) = fields[sqlColumnNumber].GetUInt64();
231  offset += 8;
232  break;
233  case FT_STRING:
234  TC_LOG_ERROR("server.loading", "Unsupported data type in table '%s' at char %d", sql->sqlTableName.c_str(), columnNumber);
235  return false;
236  case FT_SORT:
237  break;
238  default:
239  validSqlColumn = false;
240  break;
241  }
242  if (validSqlColumn && (columnNumber != (sql->formatString->size()-1)))
243  sqlColumnNumber++;
244  }
245  else
246  {
247  TC_LOG_ERROR("server.loading", "Incorrect sql format string '%s' at char %d", sql->sqlTableName.c_str(), columnNumber);
248  return false;
249  }
250  }
251 
252  if (sqlColumnNumber != (result->GetFieldCount() - 1))
253  {
254  TC_LOG_ERROR("server.loading", "SQL and DBC format strings are not matching for table: '%s'", sql->sqlTableName.c_str());
255  return false;
256  }
257 
258  fields = NULL;
259  ++rowIndex;
260  } while (result->NextRow());
261  }
262  }
263 
264  // error in dbc file at loading if NULL
265  return indexTable.asT != NULL;
266  }
Definition: Define.h:163
std::string const * indexName
Definition: DBCStore.h:33
int32 sqlIndexPos
Definition: DBCStore.h:36
uint32 fieldCount
Definition: DBCStore.h:309
StringPoolList stringPoolList
Definition: DBCStore.h:319
Definition: Define.h:161
Class used to access individual fields of database query result.
Definition: Field.h:56
Definition: Define.h:160
std::string const * formatString
Definition: DBCStore.h:32
arena_t NULL
Definition: jemalloc_internal.h:624
WorldDatabaseWorkerPool WorldDatabase
Accessor to the world database.
Definition: DatabaseEnv.cpp:20
uint64_t uint64
Definition: g3dmath.h:170
char * query(struct soap *soap)
Definition: httpget.cpp:244
bool Load(const char *filename, const char *fmt)
Definition: DBCFileLoader.cpp:28
Definition: DBCFileLoader.h:26
Definition: Define.h:167
int32 indexPos
Definition: DBCStore.h:35
std::string sqlTableName
Definition: DBCStore.h:34
char * AutoProduceStrings(const char *fmt, char *dataTable)
Definition: DBCFileLoader.cpp:272
uint32 GetNumRows() const
Get begin iterator over records.
Definition: DBCFileLoader.h:84
Definition: Define.h:165
uint32_t uint32
Definition: Define.h:150
uint64_t uint64
Definition: Define.h:149
std::shared_ptr< ResultSet > QueryResult
Definition: QueryResult.h:61
char * AutoProduceData(const char *fmt, uint32 &count, char **&indexTable, uint32 sqlRecordCount, uint32 sqlHighestIndex, char *&sqlDataTable)
Definition: DBCFileLoader.cpp:173
Definition: Define.h:162
T * dataTable
Definition: DBCStore.h:318
uint8_t uint8
Definition: g3dmath.h:164
uint32 GetCols() const
Definition: DBCFileLoader.h:86
QueryResult Query(const char *sql, T *connection=nullptr)
Definition: DatabaseWorkerPool.cpp:113
uint32 GetUInt32() const
Definition: Field.h:146
int32_t int32
Definition: g3dmath.h:167
Definition: Define.h:158
uint8_t uint8
Definition: Define.h:152
Definition: format.h:285
union DBCStorage::@339 indexTable
Definition: Define.h:164
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:207
uint32_t uint32
Definition: g3dmath.h:168
Definition: Define.h:166
uint32 nCount
Definition: DBCStore.h:308

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T>
bool DBCStorage< T >::LoadStringsFrom ( char const fn)
inline
269  {
270  // DBC must be already loaded using Load
271  if (!indexTable.asT)
272  return false;
273 
274  DBCFileLoader dbc;
275  // Check if load was successful, only then continue
276  if (!dbc.Load(fn, fmt))
277  return false;
278 
279  stringPoolList.push_back(dbc.AutoProduceStrings(fmt, reinterpret_cast<char*>(dataTable)));
280 
281  return true;
282  }
StringPoolList stringPoolList
Definition: DBCStore.h:319
bool Load(const char *filename, const char *fmt)
Definition: DBCFileLoader.cpp:28
Definition: DBCFileLoader.h:26
char * AutoProduceStrings(const char *fmt, char *dataTable)
Definition: DBCFileLoader.cpp:272
T * dataTable
Definition: DBCStore.h:318
Definition: format.h:285
union DBCStorage::@339 indexTable

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T>
T const* DBCStorage< T >::LookupEntry ( uint32  id) const
inline
90  {
91  return (id >= nCount) ? NULL : indexTable.asT[id];
92  }
arena_t NULL
Definition: jemalloc_internal.h:624
union DBCStorage::@339 indexTable
uint32 nCount
Definition: DBCStore.h:308

+ Here is the caller graph for this function:

template<class T>
DBCStorage& DBCStorage< T >::operator= ( DBCStorage< T > const right)
privatedelete

Member Data Documentation

template<class T>
char** DBCStorage< T >::asChar
template<class T>
T** DBCStorage< T >::asT
template<class T>
T* DBCStorage< T >::dataTable
private
template<class T>
uint32 DBCStorage< T >::fieldCount
private
template<class T>
char const* DBCStorage< T >::fmt
private
union { ... } DBCStorage< T >::indexTable
template<class T>
uint32 DBCStorage< T >::nCount
private
template<class T>
StringPoolList DBCStorage< T >::stringPoolList
private

The documentation for this class was generated from the following file: