Core/DBLayer: Added compile time validation for prepared statement indexes passed to DatabaseWorkerPool::GetPreparedStatement turning mistakes like the one fixed in 0327927fa6 into compiler errors.

This commit is contained in:
Shauren
2015-08-27 00:54:49 +02:00
parent 0327927fa6
commit 7c75160f59
9 changed files with 73 additions and 61 deletions

View File

@@ -428,10 +428,12 @@ class DatabaseWorkerPool
Other
*/
typedef typename T::Statements PreparedStatementIndex;
//! Automanaged (internally) pointer to a prepared statement object for usage in upper level code.
//! Pointer is deleted in this->DirectExecute(PreparedStatement*), this->Query(PreparedStatement*) or PreparedStatementTask::~PreparedStatementTask.
//! This object is not tied to the prepared statement on the MySQL context yet until execution.
PreparedStatement* GetPreparedStatement(uint32 index)
PreparedStatement* GetPreparedStatement(PreparedStatementIndex index)
{
return new PreparedStatement(index);
}

View File

@@ -21,19 +21,6 @@
#include "DatabaseWorkerPool.h"
#include "MySQLConnection.h"
class CharacterDatabaseConnection : public MySQLConnection
{
public:
//- Constructors for sync and async connections
CharacterDatabaseConnection(MySQLConnectionInfo& connInfo) : MySQLConnection(connInfo) { }
CharacterDatabaseConnection(ProducerConsumerQueue<SQLOperation*>* q, MySQLConnectionInfo& connInfo) : MySQLConnection(q, connInfo) { }
//- Loads database type specific prepared statements
void DoPrepareStatements() override;
};
typedef DatabaseWorkerPool<CharacterDatabaseConnection> CharacterDatabaseWorkerPool;
enum CharacterDatabaseStatements
{
/* Naming standard for defines:
@@ -608,4 +595,19 @@ enum CharacterDatabaseStatements
MAX_CHARACTERDATABASE_STATEMENTS
};
class CharacterDatabaseConnection : public MySQLConnection
{
public:
typedef CharacterDatabaseStatements Statements;
//- Constructors for sync and async connections
CharacterDatabaseConnection(MySQLConnectionInfo& connInfo) : MySQLConnection(connInfo) { }
CharacterDatabaseConnection(ProducerConsumerQueue<SQLOperation*>* q, MySQLConnectionInfo& connInfo) : MySQLConnection(q, connInfo) { }
//- Loads database type specific prepared statements
void DoPrepareStatements() override;
};
typedef DatabaseWorkerPool<CharacterDatabaseConnection> CharacterDatabaseWorkerPool;
#endif

View File

@@ -21,19 +21,6 @@
#include "DatabaseWorkerPool.h"
#include "MySQLConnection.h"
class HotfixDatabaseConnection : public MySQLConnection
{
public:
//- Constructors for sync and async connections
HotfixDatabaseConnection(MySQLConnectionInfo& connInfo) : MySQLConnection(connInfo) { }
HotfixDatabaseConnection(ProducerConsumerQueue<SQLOperation*>* q, MySQLConnectionInfo& connInfo) : MySQLConnection(q, connInfo) { }
//- Loads database type specific prepared statements
void DoPrepareStatements() override;
};
typedef DatabaseWorkerPool<HotfixDatabaseConnection> HotfixDatabaseWorkerPool;
enum HotfixDatabaseStatements
{
/* Naming standard for defines:
@@ -263,4 +250,19 @@ enum HotfixDatabaseStatements
MAX_HOTFIXDATABASE_STATEMENTS
};
class HotfixDatabaseConnection : public MySQLConnection
{
public:
typedef HotfixDatabaseStatements Statements;
//- Constructors for sync and async connections
HotfixDatabaseConnection(MySQLConnectionInfo& connInfo) : MySQLConnection(connInfo) { }
HotfixDatabaseConnection(ProducerConsumerQueue<SQLOperation*>* q, MySQLConnectionInfo& connInfo) : MySQLConnection(q, connInfo) { }
//- Loads database type specific prepared statements
void DoPrepareStatements() override;
};
typedef DatabaseWorkerPool<HotfixDatabaseConnection> HotfixDatabaseWorkerPool;
#endif

View File

@@ -21,19 +21,6 @@
#include "DatabaseWorkerPool.h"
#include "MySQLConnection.h"
class LoginDatabaseConnection : public MySQLConnection
{
public:
//- Constructors for sync and async connections
LoginDatabaseConnection(MySQLConnectionInfo& connInfo) : MySQLConnection(connInfo) { }
LoginDatabaseConnection(ProducerConsumerQueue<SQLOperation*>* q, MySQLConnectionInfo& connInfo) : MySQLConnection(q, connInfo) { }
//- Loads database type specific prepared statements
void DoPrepareStatements() override;
};
typedef DatabaseWorkerPool<LoginDatabaseConnection> LoginDatabaseWorkerPool;
enum LoginDatabaseStatements
{
/* Naming standard for defines:
@@ -149,4 +136,19 @@ enum LoginDatabaseStatements
MAX_LOGINDATABASE_STATEMENTS
};
class LoginDatabaseConnection : public MySQLConnection
{
public:
typedef LoginDatabaseStatements Statements;
//- Constructors for sync and async connections
LoginDatabaseConnection(MySQLConnectionInfo& connInfo) : MySQLConnection(connInfo) { }
LoginDatabaseConnection(ProducerConsumerQueue<SQLOperation*>* q, MySQLConnectionInfo& connInfo) : MySQLConnection(q, connInfo) { }
//- Loads database type specific prepared statements
void DoPrepareStatements() override;
};
typedef DatabaseWorkerPool<LoginDatabaseConnection> LoginDatabaseWorkerPool;
#endif

View File

@@ -21,19 +21,6 @@
#include "DatabaseWorkerPool.h"
#include "MySQLConnection.h"
class WorldDatabaseConnection : public MySQLConnection
{
public:
//- Constructors for sync and async connections
WorldDatabaseConnection(MySQLConnectionInfo& connInfo) : MySQLConnection(connInfo) { }
WorldDatabaseConnection(ProducerConsumerQueue<SQLOperation*>* q, MySQLConnectionInfo& connInfo) : MySQLConnection(q, connInfo) { }
//- Loads database type specific prepared statements
void DoPrepareStatements() override;
};
typedef DatabaseWorkerPool<WorldDatabaseConnection> WorldDatabaseWorkerPool;
enum WorldDatabaseStatements
{
/* Naming standard for defines:
@@ -116,4 +103,19 @@ enum WorldDatabaseStatements
MAX_WORLDDATABASE_STATEMENTS
};
class WorldDatabaseConnection : public MySQLConnection
{
public:
typedef WorldDatabaseStatements Statements;
//- Constructors for sync and async connections
WorldDatabaseConnection(MySQLConnectionInfo& connInfo) : MySQLConnection(connInfo) { }
WorldDatabaseConnection(ProducerConsumerQueue<SQLOperation*>* q, MySQLConnectionInfo& connInfo) : MySQLConnection(q, connInfo) { }
//- Loads database type specific prepared statements
void DoPrepareStatements() override;
};
typedef DatabaseWorkerPool<WorldDatabaseConnection> WorldDatabaseWorkerPool;
#endif

View File

@@ -123,6 +123,8 @@ m_length(NULL)
if (fetched_length < buffer_length)
*((char*)buffer + fetched_length) = '\0';
break;
default:
break;
}
m_rows[uint32(m_rowPosition) * m_fieldCount + fIndex].SetByteValue(

View File

@@ -420,7 +420,7 @@ char* DB2FileLoader::AutoProduceStrings(const char* format, char* dataTable, uin
return stringPool;
}
char* DB2DatabaseLoader::Load(const char* format, uint32 preparedStatement, uint32& records, char**& indexTable, char*& stringHolders, std::list<char*>& stringPool)
char* DB2DatabaseLoader::Load(const char* format, HotfixDatabaseStatements preparedStatement, uint32& records, char**& indexTable, char*& stringHolders, std::list<char*>& stringPool)
{
// Even though this query is executed only once, prepared statement is used to send data from mysql server in binary format
PreparedQueryResult result = HotfixDatabase.Query(HotfixDatabase.GetPreparedStatement(preparedStatement));
@@ -561,7 +561,7 @@ char* DB2DatabaseLoader::Load(const char* format, uint32 preparedStatement, uint
return dataTable;
}
void DB2DatabaseLoader::LoadStrings(const char* format, uint32 preparedStatement, uint32 locale, char**& indexTable, std::list<char*>& stringPool)
void DB2DatabaseLoader::LoadStrings(const char* format, HotfixDatabaseStatements preparedStatement, uint32 locale, char**& indexTable, std::list<char*>& stringPool)
{
PreparedStatement* stmt = HotfixDatabase.GetPreparedStatement(preparedStatement);
stmt->setString(0, localeNames[locale]);

View File

@@ -20,7 +20,7 @@
#include "Define.h"
#include "Utilities/ByteConverter.h"
#include "Implementation/HotfixDatabase.h"
#include <cassert>
#include <list>
@@ -111,8 +111,8 @@ class DB2DatabaseLoader
public:
explicit DB2DatabaseLoader(std::string const& storageName) : _storageName(storageName) { }
char* Load(const char* format, uint32 preparedStatement, uint32& records, char**& indexTable, char*& stringHolders, std::list<char*>& stringPool);
void LoadStrings(const char* format, uint32 preparedStatement, uint32 locale, char**& indexTable, std::list<char*>& stringPool);
char* Load(const char* format, HotfixDatabaseStatements preparedStatement, uint32& records, char**& indexTable, char*& stringHolders, std::list<char*>& stringPool);
void LoadStrings(const char* format, HotfixDatabaseStatements preparedStatement, uint32 locale, char**& indexTable, std::list<char*>& stringPool);
static char* AddLocaleString(LocalizedString* holder, uint32 locale, std::string const& value);
private:

View File

@@ -48,7 +48,7 @@ class DB2Storage : public DB2StorageBase
public:
typedef DBStorageIterator<T> iterator;
DB2Storage(char const* fileName, char const* format, uint32 preparedStmtIndex)
DB2Storage(char const* fileName, char const* format, HotfixDatabaseStatements preparedStmtIndex)
: _fileName(fileName), _indexTableSize(0), _fieldCount(0), _format(format), _dataTable(nullptr), _dataTableEx(nullptr), _hotfixStatement(preparedStmtIndex)
{
_indexTable.AsT = NULL;
@@ -178,7 +178,7 @@ public:
if (!DB2FileLoader::GetFormatStringFieldCount(_format))
return;
DB2DatabaseLoader(_fileName).LoadStrings(_format, _hotfixStatement + 1, locale, _indexTable.AsChar, _stringPoolList);
DB2DatabaseLoader(_fileName).LoadStrings(_format, HotfixDatabaseStatements(_hotfixStatement + 1), locale, _indexTable.AsChar, _stringPoolList);
}
typedef bool(*SortFunc)(T const* left, T const* right);
@@ -205,7 +205,7 @@ private:
T* _dataTable;
T* _dataTableEx;
StringPoolList _stringPoolList;
uint32 _hotfixStatement;
HotfixDatabaseStatements _hotfixStatement;
};
#endif