Various Cleanups (shared/Database/)

This commit is contained in:
Schmoozerd 2012-07-19 21:57:32 +02:00
parent 9753625fd1
commit c334cd5ea4
25 changed files with 508 additions and 508 deletions

View file

@ -40,7 +40,7 @@ void DatabaseMysql::ThreadEnd()
DatabaseMysql::DatabaseMysql()
{
// before first connection
if( db_count++ == 0 )
if (db_count++ == 0)
{
// Mysql Library Init
mysql_library_init(-1, NULL, NULL);
@ -59,11 +59,11 @@ DatabaseMysql::~DatabaseMysql()
StopServer();
//Free Mysql library pointers for last ~DB
if(--db_count == 0)
if (--db_count == 0)
mysql_library_end();
}
SqlConnection * DatabaseMysql::CreateConnection()
SqlConnection* DatabaseMysql::CreateConnection()
{
return new MySQLConnection(*this);
}
@ -74,12 +74,12 @@ MySQLConnection::~MySQLConnection()
mysql_close(mMysql);
}
bool MySQLConnection::Initialize(const char *infoString)
bool MySQLConnection::Initialize(const char* infoString)
{
MYSQL * mysqlInit = mysql_init(NULL);
MYSQL* mysqlInit = mysql_init(NULL);
if (!mysqlInit)
{
sLog.outError( "Could not initialize Mysql connection" );
sLog.outError("Could not initialize Mysql connection");
return false;
}
@ -93,20 +93,20 @@ bool MySQLConnection::Initialize(const char *infoString)
iter = tokens.begin();
if(iter != tokens.end())
if (iter != tokens.end())
host = *iter++;
if(iter != tokens.end())
if (iter != tokens.end())
port_or_socket = *iter++;
if(iter != tokens.end())
if (iter != tokens.end())
user = *iter++;
if(iter != tokens.end())
if (iter != tokens.end())
password = *iter++;
if(iter != tokens.end())
if (iter != tokens.end())
database = *iter++;
mysql_options(mysqlInit,MYSQL_SET_CHARSET_NAME,"utf8");
#ifdef WIN32
if(host==".") // named pipe use option (Windows)
if (host==".") // named pipe use option (Windows)
{
unsigned int opt = MYSQL_PROTOCOL_PIPE;
mysql_options(mysqlInit,MYSQL_OPT_PROTOCOL,(char const*)&opt);
@ -119,7 +119,7 @@ bool MySQLConnection::Initialize(const char *infoString)
unix_socket = 0;
}
#else
if(host==".") // socket use option (Unix/Linux)
if (host==".") // socket use option (Unix/Linux)
{
unsigned int opt = MYSQL_PROTOCOL_SOCKET;
mysql_options(mysqlInit,MYSQL_OPT_PROTOCOL,(char const*)&opt);
@ -135,19 +135,19 @@ bool MySQLConnection::Initialize(const char *infoString)
#endif
mMysql = mysql_real_connect(mysqlInit, host.c_str(), user.c_str(),
password.c_str(), database.c_str(), port, unix_socket, 0);
password.c_str(), database.c_str(), port, unix_socket, 0);
if (!mMysql)
{
sLog.outError( "Could not connect to MySQL database at %s: %s\n",
host.c_str(),mysql_error(mysqlInit));
sLog.outError("Could not connect to MySQL database at %s: %s\n",
host.c_str(),mysql_error(mysqlInit));
mysql_close(mysqlInit);
return false;
}
DETAIL_LOG("Connected to MySQL database %s@%s:%s/%s", user.c_str(), host.c_str(), port_or_socket.c_str(), database.c_str());
sLog.outString("MySQL client library: %s", mysql_get_client_info());
sLog.outString("MySQL server ver: %s ", mysql_get_server_info( mMysql));
sLog.outString("MySQL server ver: %s ", mysql_get_server_info(mMysql));
/*----------SET AUTOCOMMIT ON---------*/
// It seems mysql 5.0.x have enabled this feature
@ -174,29 +174,29 @@ bool MySQLConnection::Initialize(const char *infoString)
return true;
}
bool MySQLConnection::_Query(const char *sql, MYSQL_RES **pResult, MYSQL_FIELD **pFields, uint64* pRowCount, uint32* pFieldCount)
bool MySQLConnection::_Query(const char* sql, MYSQL_RES** pResult, MYSQL_FIELD** pFields, uint64* pRowCount, uint32* pFieldCount)
{
if (!mMysql)
return 0;
uint32 _s = WorldTimer::getMSTime();
if(mysql_query(mMysql, sql))
if (mysql_query(mMysql, sql))
{
sLog.outErrorDb( "SQL: %s", sql );
sLog.outErrorDb("SQL: %s", sql);
sLog.outErrorDb("query ERROR: %s", mysql_error(mMysql));
return false;
}
else
{
DEBUG_FILTER_LOG(LOG_FILTER_SQL_TEXT, "[%u ms] SQL: %s", WorldTimer::getMSTimeDiff(_s,WorldTimer::getMSTime()), sql );
DEBUG_FILTER_LOG(LOG_FILTER_SQL_TEXT, "[%u ms] SQL: %s", WorldTimer::getMSTimeDiff(_s,WorldTimer::getMSTime()), sql);
}
*pResult = mysql_store_result(mMysql);
*pRowCount = mysql_affected_rows(mMysql);
*pFieldCount = mysql_field_count(mMysql);
if (!*pResult )
if (!*pResult)
return false;
if (!*pRowCount)
@ -209,37 +209,37 @@ bool MySQLConnection::_Query(const char *sql, MYSQL_RES **pResult, MYSQL_FIELD *
return true;
}
QueryResult* MySQLConnection::Query(const char *sql)
QueryResult* MySQLConnection::Query(const char* sql)
{
MYSQL_RES *result = NULL;
MYSQL_FIELD *fields = NULL;
MYSQL_RES* result = NULL;
MYSQL_FIELD* fields = NULL;
uint64 rowCount = 0;
uint32 fieldCount = 0;
if(!_Query(sql,&result,&fields,&rowCount,&fieldCount))
if (!_Query(sql,&result,&fields,&rowCount,&fieldCount))
return NULL;
QueryResultMysql *queryResult = new QueryResultMysql(result, fields, rowCount, fieldCount);
QueryResultMysql* queryResult = new QueryResultMysql(result, fields, rowCount, fieldCount);
queryResult->NextRow();
return queryResult;
}
QueryNamedResult* MySQLConnection::QueryNamed(const char *sql)
QueryNamedResult* MySQLConnection::QueryNamed(const char* sql)
{
MYSQL_RES *result = NULL;
MYSQL_FIELD *fields = NULL;
MYSQL_RES* result = NULL;
MYSQL_FIELD* fields = NULL;
uint64 rowCount = 0;
uint32 fieldCount = 0;
if(!_Query(sql,&result,&fields,&rowCount,&fieldCount))
if (!_Query(sql,&result,&fields,&rowCount,&fieldCount))
return NULL;
QueryFieldNames names(fieldCount);
for (uint32 i = 0; i < fieldCount; i++)
names[i] = fields[i].name;
QueryResultMysql *queryResult = new QueryResultMysql(result, fields, rowCount, fieldCount);
QueryResultMysql* queryResult = new QueryResultMysql(result, fields, rowCount, fieldCount);
queryResult->NextRow();
return new QueryNamedResult(queryResult,names);
@ -253,7 +253,7 @@ bool MySQLConnection::Execute(const char* sql)
{
uint32 _s = WorldTimer::getMSTime();
if(mysql_query(mMysql, sql))
if (mysql_query(mMysql, sql))
{
sLog.outErrorDb("SQL: %s", sql);
sLog.outErrorDb("SQL ERROR: %s", mysql_error(mMysql));
@ -261,7 +261,7 @@ bool MySQLConnection::Execute(const char* sql)
}
else
{
DEBUG_FILTER_LOG(LOG_FILTER_SQL_TEXT, "[%u ms] SQL: %s", WorldTimer::getMSTimeDiff(_s,WorldTimer::getMSTime()), sql );
DEBUG_FILTER_LOG(LOG_FILTER_SQL_TEXT, "[%u ms] SQL: %s", WorldTimer::getMSTimeDiff(_s,WorldTimer::getMSTime()), sql);
}
// end guarded block
}
@ -269,7 +269,7 @@ bool MySQLConnection::Execute(const char* sql)
return true;
}
bool MySQLConnection::_TransactionCmd(const char *sql)
bool MySQLConnection::_TransactionCmd(const char* sql)
{
if (mysql_query(mMysql, sql))
{
@ -299,7 +299,7 @@ bool MySQLConnection::RollbackTransaction()
return _TransactionCmd("ROLLBACK");
}
unsigned long MySQLConnection::escape_string(char *to, const char *from, unsigned long length)
unsigned long MySQLConnection::escape_string(char* to, const char* from, unsigned long length)
{
if (!mMysql || !to || !from || !length)
return 0;
@ -308,14 +308,14 @@ unsigned long MySQLConnection::escape_string(char *to, const char *from, unsigne
}
//////////////////////////////////////////////////////////////////////////
SqlPreparedStatement * MySQLConnection::CreateStatement( const std::string& fmt )
SqlPreparedStatement* MySQLConnection::CreateStatement(const std::string& fmt)
{
return new MySqlPreparedStatement(fmt, *this, mMysql);
}
//////////////////////////////////////////////////////////////////////////
MySqlPreparedStatement::MySqlPreparedStatement( const std::string& fmt, SqlConnection& conn, MYSQL * mysql ) : SqlPreparedStatement(fmt, conn),
MySqlPreparedStatement::MySqlPreparedStatement(const std::string& fmt, SqlConnection& conn, MYSQL* mysql) : SqlPreparedStatement(fmt, conn),
m_pMySQLConn(mysql), m_stmt(NULL), m_pInputArgs(NULL), m_pResult(NULL), m_pResultMetadata(NULL)
{
}
@ -327,7 +327,7 @@ MySqlPreparedStatement::~MySqlPreparedStatement()
bool MySqlPreparedStatement::prepare()
{
if(isPrepared())
if (isPrepared())
return true;
//remove old binds
@ -363,14 +363,14 @@ bool MySqlPreparedStatement::prepare()
}
//bind input buffers
if(m_nParams)
if (m_nParams)
{
m_pInputArgs = new MYSQL_BIND[m_nParams];
memset(m_pInputArgs, 0, sizeof(MYSQL_BIND) * m_nParams);
}
//check if we have a statement which returns result sets
if(m_pResultMetadata)
if (m_pResultMetadata)
{
//our statement is query
m_bIsQuery = true;
@ -384,20 +384,20 @@ bool MySqlPreparedStatement::prepare()
return true;
}
void MySqlPreparedStatement::bind( const SqlStmtParameters& holder )
void MySqlPreparedStatement::bind(const SqlStmtParameters& holder)
{
if(!isPrepared())
if (!isPrepared())
{
MANGOS_ASSERT(false);
return;
}
//finalize adding params
if(!m_pInputArgs)
if (!m_pInputArgs)
return;
//verify if we bound all needed input parameters
if(m_nParams != holder.boundParams())
if (m_nParams != holder.boundParams())
{
MANGOS_ASSERT(false);
return;
@ -414,14 +414,14 @@ void MySqlPreparedStatement::bind( const SqlStmtParameters& holder )
}
//bind input arguments
if(mysql_stmt_bind_param(m_stmt, m_pInputArgs))
if (mysql_stmt_bind_param(m_stmt, m_pInputArgs))
{
sLog.outError("SQL ERROR: mysql_stmt_bind_param() failed\n");
sLog.outError("SQL ERROR: %s", mysql_stmt_error(m_stmt));
}
}
void MySqlPreparedStatement::addParam( int nIndex, const SqlStmtFieldData& data )
void MySqlPreparedStatement::addParam(int nIndex, const SqlStmtFieldData& data)
{
MANGOS_ASSERT(m_pInputArgs);
MANGOS_ASSERT(nIndex < m_nParams);
@ -441,7 +441,7 @@ void MySqlPreparedStatement::addParam( int nIndex, const SqlStmtFieldData& data
void MySqlPreparedStatement::RemoveBinds()
{
if(!m_stmt)
if (!m_stmt)
return;
delete [] m_pInputArgs;
@ -460,10 +460,10 @@ void MySqlPreparedStatement::RemoveBinds()
bool MySqlPreparedStatement::execute()
{
if(!isPrepared())
if (!isPrepared())
return false;
if(mysql_stmt_execute(m_stmt))
if (mysql_stmt_execute(m_stmt))
{
sLog.outError("SQL: cannot execute '%s'", m_szFmt.c_str());
sLog.outError("SQL ERROR: %s", mysql_stmt_error(m_stmt));
@ -473,27 +473,27 @@ bool MySqlPreparedStatement::execute()
return true;
}
enum_field_types MySqlPreparedStatement::ToMySQLType( const SqlStmtFieldData &data, my_bool &bUnsigned )
enum_field_types MySqlPreparedStatement::ToMySQLType(const SqlStmtFieldData& data, my_bool& bUnsigned)
{
bUnsigned = 0;
enum_field_types dataType = MYSQL_TYPE_NULL;
switch (data.type())
{
case FIELD_NONE: dataType = MYSQL_TYPE_NULL; break;
// MySQL does not support MYSQL_TYPE_BIT as input type
case FIELD_BOOL: //dataType = MYSQL_TYPE_BIT; bUnsigned = 1; break;
case FIELD_UI8: dataType = MYSQL_TYPE_TINY; bUnsigned = 1; break;
case FIELD_I8: dataType = MYSQL_TYPE_TINY; break;
case FIELD_I16: dataType = MYSQL_TYPE_SHORT; break;
case FIELD_UI16: dataType = MYSQL_TYPE_SHORT; bUnsigned = 1; break;
case FIELD_I32: dataType = MYSQL_TYPE_LONG; break;
case FIELD_UI32: dataType = MYSQL_TYPE_LONG; bUnsigned = 1; break;
case FIELD_I64: dataType = MYSQL_TYPE_LONGLONG; break;
case FIELD_UI64: dataType = MYSQL_TYPE_LONGLONG; bUnsigned = 1; break;
case FIELD_FLOAT: dataType = MYSQL_TYPE_FLOAT; break;
case FIELD_DOUBLE: dataType = MYSQL_TYPE_DOUBLE; break;
case FIELD_STRING: dataType = MYSQL_TYPE_STRING; break;
case FIELD_NONE: dataType = MYSQL_TYPE_NULL; break;
// MySQL does not support MYSQL_TYPE_BIT as input type
case FIELD_BOOL: //dataType = MYSQL_TYPE_BIT; bUnsigned = 1; break;
case FIELD_UI8: dataType = MYSQL_TYPE_TINY; bUnsigned = 1; break;
case FIELD_I8: dataType = MYSQL_TYPE_TINY; break;
case FIELD_I16: dataType = MYSQL_TYPE_SHORT; break;
case FIELD_UI16: dataType = MYSQL_TYPE_SHORT; bUnsigned = 1; break;
case FIELD_I32: dataType = MYSQL_TYPE_LONG; break;
case FIELD_UI32: dataType = MYSQL_TYPE_LONG; bUnsigned = 1; break;
case FIELD_I64: dataType = MYSQL_TYPE_LONGLONG; break;
case FIELD_UI64: dataType = MYSQL_TYPE_LONGLONG; bUnsigned = 1; break;
case FIELD_FLOAT: dataType = MYSQL_TYPE_FLOAT; break;
case FIELD_DOUBLE: dataType = MYSQL_TYPE_DOUBLE; break;
case FIELD_STRING: dataType = MYSQL_TYPE_STRING; break;
}
return dataType;