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

@ -31,7 +31,7 @@ size_t DatabasePostgre::db_count = 0;
DatabasePostgre::DatabasePostgre()
{
// before first connection
if( db_count++ == 0 )
if (db_count++ == 0)
{
if (!PQisthreadsafe())
{
@ -46,7 +46,7 @@ DatabasePostgre::~DatabasePostgre()
}
SqlConnection * DatabasePostgre::CreateConnection()
SqlConnection* DatabasePostgre::CreateConnection()
{
return new PostgreSQLConnection();
}
@ -56,7 +56,7 @@ PostgreSQLConnection::~PostgreSQLConnection()
PQfinish(mPGconn);
}
bool PostgreSQLConnection::Initialize(const char *infoString)
bool PostgreSQLConnection::Initialize(const char* infoString)
{
Tokens tokens = StrSplit(infoString, ";");
@ -66,15 +66,15 @@ bool PostgreSQLConnection::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_dir = *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++;
if (host == ".")
@ -85,8 +85,8 @@ bool PostgreSQLConnection::Initialize(const char *infoString)
/* check to see that the backend connection was successfully made */
if (PQstatus(mPGconn) != CONNECTION_OK)
{
sLog.outError( "Could not connect to Postgre database at %s: %s",
host.c_str(), PQerrorMessage(mPGconn));
sLog.outError("Could not connect to Postgre database at %s: %s",
host.c_str(), PQerrorMessage(mPGconn));
PQfinish(mPGconn);
mPGconn = NULL;
return false;
@ -97,7 +97,7 @@ bool PostgreSQLConnection::Initialize(const char *infoString)
return true;
}
bool PostgreSQLConnection::_Query(const char *sql, PGresult** pResult, uint64* pRowCount, uint32* pFieldCount)
bool PostgreSQLConnection::_Query(const char* sql, PGresult** pResult, uint64* pRowCount, uint32* pFieldCount)
{
if (!mPGconn)
return false;
@ -105,19 +105,19 @@ bool PostgreSQLConnection::_Query(const char *sql, PGresult** pResult, uint64* p
uint32 _s = WorldTimer::getMSTime();
// Send the query
*pResult = PQexec(mPGconn, sql);
if(!*pResult )
if (!*pResult)
return false;
if (PQresultStatus(*pResult) != PGRES_TUPLES_OK)
{
sLog.outErrorDb( "SQL : %s", sql );
sLog.outErrorDb( "SQL %s", PQerrorMessage(mPGconn));
sLog.outErrorDb("SQL : %s", sql);
sLog.outErrorDb("SQL %s", PQerrorMessage(mPGconn));
PQclear(*pResult);
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);
}
*pRowCount = PQntuples(*pResult);
@ -133,7 +133,7 @@ bool PostgreSQLConnection::_Query(const char *sql, PGresult** pResult, uint64* p
return true;
}
QueryResult* PostgreSQLConnection::Query(const char *sql)
QueryResult* PostgreSQLConnection::Query(const char* sql)
{
if (!mPGconn)
return NULL;
@ -142,16 +142,16 @@ QueryResult* PostgreSQLConnection::Query(const char *sql)
uint64 rowCount = 0;
uint32 fieldCount = 0;
if(!_Query(sql,&result,&rowCount,&fieldCount))
if (!_Query(sql,&result,&rowCount,&fieldCount))
return NULL;
QueryResultPostgre * queryResult = new QueryResultPostgre(result, rowCount, fieldCount);
QueryResultPostgre* queryResult = new QueryResultPostgre(result, rowCount, fieldCount);
queryResult->NextRow();
return queryResult;
}
QueryNamedResult* PostgreSQLConnection::QueryNamed(const char *sql)
QueryNamedResult* PostgreSQLConnection::QueryNamed(const char* sql)
{
if (!mPGconn)
return NULL;
@ -160,48 +160,48 @@ QueryNamedResult* PostgreSQLConnection::QueryNamed(const char *sql)
uint64 rowCount = 0;
uint32 fieldCount = 0;
if(!_Query(sql,&result,&rowCount,&fieldCount))
if (!_Query(sql,&result,&rowCount,&fieldCount))
return NULL;
QueryFieldNames names(fieldCount);
for (uint32 i = 0; i < fieldCount; i++)
names[i] = PQfname(result, i);
QueryResultPostgre * queryResult = new QueryResultPostgre(result, rowCount, fieldCount);
QueryResultPostgre* queryResult = new QueryResultPostgre(result, rowCount, fieldCount);
queryResult->NextRow();
return new QueryNamedResult(queryResult,names);
}
bool PostgreSQLConnection::Execute(const char *sql)
bool PostgreSQLConnection::Execute(const char* sql)
{
if (!mPGconn)
return false;
uint32 _s = WorldTimer::getMSTime();
PGresult *res = PQexec(mPGconn, sql);
PGresult* res = PQexec(mPGconn, sql);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
sLog.outErrorDb( "SQL: %s", sql );
sLog.outErrorDb( "SQL %s", PQerrorMessage(mPGconn) );
sLog.outErrorDb("SQL: %s", sql);
sLog.outErrorDb("SQL %s", PQerrorMessage(mPGconn));
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);
}
PQclear(res);
return true;
}
bool PostgreSQLConnection::_TransactionCmd(const char *sql)
bool PostgreSQLConnection::_TransactionCmd(const char* sql)
{
if (!mPGconn)
return false;
PGresult *res = PQexec(mPGconn, sql);
PGresult* res = PQexec(mPGconn, sql);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
sLog.outError("SQL: %s", sql);
@ -230,7 +230,7 @@ bool PostgreSQLConnection::RollbackTransaction()
return _TransactionCmd("ROLLBACK");
}
unsigned long PostgreSQLConnection::escape_string(char *to, const char *from, unsigned long length)
unsigned long PostgreSQLConnection::escape_string(char* to, const char* from, unsigned long length)
{
if (!mPGconn || !to || !from || !length)
return 0;