mirror of
https://github.com/mangosfour/server.git
synced 2025-12-14 16:37:01 +00:00
[7935] Move seldom used access to query data by field names to independent object.
This let not do preparation code for unused later functionlity.
This commit is contained in:
parent
626553c9ee
commit
c3c7187841
12 changed files with 165 additions and 71 deletions
|
|
@ -335,16 +335,16 @@ std::string PlayerDumpWriter::GetDump(uint32 guid)
|
|||
dump += "IMPORTANT NOTE: NOT APPLY ITS DIRECTLY to character DB or you will DAMAGE and CORRUPT character DB\n\n";
|
||||
|
||||
// revision check guard
|
||||
QueryResult* result = CharacterDatabase.Query("SELECT * FROM character_db_version LIMIT 1");
|
||||
QueryNamedResult* result = CharacterDatabase.QueryNamed("SELECT * FROM character_db_version LIMIT 1");
|
||||
if(result)
|
||||
{
|
||||
QueryResult::FieldNames const& namesMap = result->GetFieldNames();
|
||||
QueryFieldNames const& namesMap = result->GetFieldNames();
|
||||
std::string reqName;
|
||||
for(QueryResult::FieldNames::const_iterator itr = namesMap.begin(); itr != namesMap.end(); ++itr)
|
||||
for(QueryFieldNames::const_iterator itr = namesMap.begin(); itr != namesMap.end(); ++itr)
|
||||
{
|
||||
if(itr->second.substr(0,9)=="required_")
|
||||
if(itr->substr(0,9)=="required_")
|
||||
{
|
||||
reqName = itr->second;
|
||||
reqName = *itr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -131,6 +131,25 @@ QueryResult* Database::PQuery(const char *format,...)
|
|||
return Query(szQuery);
|
||||
}
|
||||
|
||||
QueryNamedResult* Database::PQueryNamed(const char *format,...)
|
||||
{
|
||||
if(!format) return NULL;
|
||||
|
||||
va_list ap;
|
||||
char szQuery [MAX_QUERY_LEN];
|
||||
va_start(ap, format);
|
||||
int res = vsnprintf( szQuery, MAX_QUERY_LEN, format, ap );
|
||||
va_end(ap);
|
||||
|
||||
if(res==-1)
|
||||
{
|
||||
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
|
||||
return false;
|
||||
}
|
||||
|
||||
return QueryNamed(szQuery);
|
||||
}
|
||||
|
||||
bool Database::PExecute(const char * format,...)
|
||||
{
|
||||
if (!format)
|
||||
|
|
|
|||
|
|
@ -52,6 +52,8 @@ class MANGOS_DLL_SPEC Database
|
|||
|
||||
virtual QueryResult* Query(const char *sql) = 0;
|
||||
QueryResult* PQuery(const char *format,...) ATTR_PRINTF(2,3);
|
||||
virtual QueryNamedResult* QueryNamed(const char *sql) = 0;
|
||||
QueryNamedResult* PQueryNamed(const char *format,...) ATTR_PRINTF(2,3);
|
||||
|
||||
/// Async queries and query holders, implemented in DatabaseImpl.h
|
||||
|
||||
|
|
|
|||
|
|
@ -176,15 +176,11 @@ bool DatabaseMysql::Initialize(const char *infoString)
|
|||
}
|
||||
}
|
||||
|
||||
QueryResult* DatabaseMysql::Query(const char *sql)
|
||||
bool DatabaseMysql::_Query(const char *sql, MYSQL_RES **pResult, MYSQL_FIELD **pFields, uint64* pRowCount, uint32* pFieldCount)
|
||||
{
|
||||
if (!mMysql)
|
||||
return 0;
|
||||
|
||||
MYSQL_RES *result = 0;
|
||||
uint64 rowCount = 0;
|
||||
uint32 fieldCount = 0;
|
||||
|
||||
{
|
||||
// guarded block for thread-safe mySQL request
|
||||
ACE_Guard<ACE_Thread_Mutex> query_connection_guard(mMutex);
|
||||
|
|
@ -195,7 +191,7 @@ QueryResult* DatabaseMysql::Query(const char *sql)
|
|||
{
|
||||
sLog.outErrorDb( "SQL: %s", sql );
|
||||
sLog.outErrorDb("query ERROR: %s", mysql_error(mMysql));
|
||||
return NULL;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -204,29 +200,63 @@ QueryResult* DatabaseMysql::Query(const char *sql)
|
|||
#endif
|
||||
}
|
||||
|
||||
result = mysql_store_result(mMysql);
|
||||
|
||||
rowCount = mysql_affected_rows(mMysql);
|
||||
fieldCount = mysql_field_count(mMysql);
|
||||
*pResult = mysql_store_result(mMysql);
|
||||
*pRowCount = mysql_affected_rows(mMysql);
|
||||
*pFieldCount = mysql_field_count(mMysql);
|
||||
// end guarded block
|
||||
}
|
||||
|
||||
if (!result )
|
||||
return NULL;
|
||||
if (!*pResult )
|
||||
return false;
|
||||
|
||||
if (!rowCount)
|
||||
if (!*pRowCount)
|
||||
{
|
||||
mysql_free_result(result);
|
||||
return NULL;
|
||||
mysql_free_result(*pResult);
|
||||
return false;
|
||||
}
|
||||
|
||||
QueryResultMysql *queryResult = new QueryResultMysql(result, rowCount, fieldCount);
|
||||
*pFields = mysql_fetch_fields(*pResult);
|
||||
return true;
|
||||
}
|
||||
|
||||
QueryResult* DatabaseMysql::Query(const char *sql)
|
||||
{
|
||||
MYSQL_RES *result = NULL;
|
||||
MYSQL_FIELD *fields = NULL;
|
||||
uint64 rowCount = 0;
|
||||
uint32 fieldCount = 0;
|
||||
|
||||
if(!_Query(sql,&result,&fields,&rowCount,&fieldCount))
|
||||
return NULL;
|
||||
|
||||
QueryResultMysql *queryResult = new QueryResultMysql(result, fields, rowCount, fieldCount);
|
||||
|
||||
queryResult->NextRow();
|
||||
|
||||
return queryResult;
|
||||
}
|
||||
|
||||
QueryNamedResult* DatabaseMysql::QueryNamed(const char *sql)
|
||||
{
|
||||
MYSQL_RES *result = NULL;
|
||||
MYSQL_FIELD *fields = NULL;
|
||||
uint64 rowCount = 0;
|
||||
uint32 fieldCount = 0;
|
||||
|
||||
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);
|
||||
|
||||
queryResult->NextRow();
|
||||
|
||||
return new QueryNamedResult(queryResult,names);
|
||||
}
|
||||
|
||||
bool DatabaseMysql::Execute(const char *sql)
|
||||
{
|
||||
if (!mMysql)
|
||||
|
|
|
|||
|
|
@ -48,6 +48,7 @@ class MANGOS_DLL_SPEC DatabaseMysql : public Database
|
|||
void InitDelayThread();
|
||||
void HaltDelayThread();
|
||||
QueryResult* Query(const char *sql);
|
||||
QueryNamedResult* QueryNamed(const char *sql);
|
||||
bool Execute(const char *sql);
|
||||
bool DirectExecute(const char* sql);
|
||||
bool BeginTransaction();
|
||||
|
|
@ -73,6 +74,7 @@ class MANGOS_DLL_SPEC DatabaseMysql : public Database
|
|||
static size_t db_count;
|
||||
|
||||
bool _TransactionCmd(const char *sql);
|
||||
bool _Query(const char *sql, MYSQL_RES **pResult, MYSQL_FIELD **pFields, uint64* pRowCount, uint32* pFieldCount);
|
||||
};
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -115,32 +115,27 @@ bool DatabasePostgre::Initialize(const char *infoString)
|
|||
|
||||
}
|
||||
|
||||
QueryResult* DatabasePostgre::Query(const char *sql)
|
||||
bool DatabasePostgre::_Query(const char *sql, PGresult** pResult, uint64* pRowCount, uint32* pFieldCount)
|
||||
{
|
||||
if (!mPGconn)
|
||||
return 0;
|
||||
|
||||
uint64 rowCount = 0;
|
||||
uint32 fieldCount = 0;
|
||||
|
||||
// guarded block for thread-safe request
|
||||
ACE_Guard<ACE_Thread_Mutex> query_connection_guard(mMutex);
|
||||
#ifdef MANGOS_DEBUG
|
||||
uint32 _s = getMSTime();
|
||||
#endif
|
||||
// Send the query
|
||||
PGresult * result = PQexec(mPGconn, sql);
|
||||
if (!result )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
*pResult = PQexec(mPGconn, sql);
|
||||
if(!*pResult )
|
||||
return false;
|
||||
|
||||
if (PQresultStatus(result) != PGRES_TUPLES_OK)
|
||||
if (PQresultStatus(*pResult) != PGRES_TUPLES_OK)
|
||||
{
|
||||
sLog.outErrorDb( "SQL : %s", sql );
|
||||
sLog.outErrorDb( "SQL %s", PQerrorMessage(mPGconn));
|
||||
PQclear(result);
|
||||
return NULL;
|
||||
PQclear(*pResult);
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -149,15 +144,29 @@ QueryResult* DatabasePostgre::Query(const char *sql)
|
|||
#endif
|
||||
}
|
||||
|
||||
rowCount = PQntuples(result);
|
||||
fieldCount = PQnfields(result);
|
||||
*pRowCount = PQntuples(*pResult);
|
||||
*pFieldCount = PQnfields(*pResult);
|
||||
// end guarded block
|
||||
|
||||
if (!rowCount)
|
||||
if (!*pRowCount)
|
||||
{
|
||||
PQclear(result);
|
||||
return NULL;
|
||||
PQclear(*pResult);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
QueryResult* DatabasePostgre::Query(const char *sql)
|
||||
{
|
||||
if (!mPGconn)
|
||||
return 0;
|
||||
|
||||
PGresult* result = NULL;
|
||||
uint64 rowCount = 0;
|
||||
uint32 fieldCount = 0;
|
||||
|
||||
if(!_Query(sql,&result,&rowCount,&fieldCount))
|
||||
return NULL;
|
||||
|
||||
QueryResultPostgre * queryResult = new QueryResultPostgre(result, rowCount, fieldCount);
|
||||
queryResult->NextRow();
|
||||
|
|
@ -165,6 +174,28 @@ QueryResult* DatabasePostgre::Query(const char *sql)
|
|||
return queryResult;
|
||||
}
|
||||
|
||||
QueryNamedResult* DatabasePostgre::QueryNamed(const char *sql)
|
||||
{
|
||||
if (!mPGconn)
|
||||
return 0;
|
||||
|
||||
PGresult* result = NULL;
|
||||
uint64 rowCount = 0;
|
||||
uint32 fieldCount = 0;
|
||||
|
||||
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);
|
||||
queryResult->NextRow();
|
||||
|
||||
return new QueryNamedResult(queryResult,names);
|
||||
}
|
||||
|
||||
bool DatabasePostgre::Execute(const char *sql)
|
||||
{
|
||||
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ class DatabasePostgre : public Database
|
|||
void InitDelayThread();
|
||||
void HaltDelayThread();
|
||||
QueryResult* Query(const char *sql);
|
||||
QueryNamedResult* QueryNamed(const char *sql);
|
||||
bool Execute(const char *sql);
|
||||
bool DirectExecute(const char* sql);
|
||||
bool BeginTransaction();
|
||||
|
|
@ -68,5 +69,6 @@ class DatabasePostgre : public Database
|
|||
static size_t db_count;
|
||||
|
||||
bool _TransactionCmd(const char *sql);
|
||||
bool _Query(const char *sql, PGresult **pResult, uint64* pRowCount, uint32* pFieldCount);
|
||||
};
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -29,36 +29,52 @@ class MANGOS_DLL_SPEC QueryResult
|
|||
|
||||
virtual bool NextRow() = 0;
|
||||
|
||||
typedef std::map<uint32, std::string> FieldNames;
|
||||
|
||||
uint32 GetField_idx(const std::string &name) const
|
||||
{
|
||||
for(FieldNames::const_iterator iter = GetFieldNames().begin(); iter != GetFieldNames().end(); ++iter)
|
||||
{
|
||||
if(iter->second == name)
|
||||
return iter->first;
|
||||
}
|
||||
ASSERT(false && "unknown field name");
|
||||
return uint32(-1);
|
||||
}
|
||||
|
||||
Field *Fetch() const { return mCurrentRow; }
|
||||
|
||||
const Field & operator [] (int index) const { return mCurrentRow[index]; }
|
||||
|
||||
const Field & operator [] (const std::string &name) const
|
||||
{
|
||||
return mCurrentRow[GetField_idx(name)];
|
||||
}
|
||||
|
||||
uint32 GetFieldCount() const { return mFieldCount; }
|
||||
uint64 GetRowCount() const { return mRowCount; }
|
||||
FieldNames const& GetFieldNames() const {return mFieldNames; }
|
||||
|
||||
protected:
|
||||
Field *mCurrentRow;
|
||||
uint32 mFieldCount;
|
||||
uint64 mRowCount;
|
||||
FieldNames mFieldNames;
|
||||
};
|
||||
|
||||
typedef std::vector<std::string> QueryFieldNames;
|
||||
|
||||
class MANGOS_DLL_SPEC QueryNamedResult
|
||||
{
|
||||
public:
|
||||
explicit QueryNamedResult(QueryResult* query, QueryFieldNames const& names) : mQuery(query), mFieldNames(names) {}
|
||||
~QueryNamedResult() { delete mQuery; }
|
||||
|
||||
// compatible interface with QueryResult
|
||||
bool NextRow() { return mQuery->NextRow(); }
|
||||
Field *Fetch() const { return mQuery->Fetch(); }
|
||||
uint32 GetFieldCount() const { return mQuery->GetFieldCount(); }
|
||||
uint64 GetRowCount() const { return mQuery->GetRowCount(); }
|
||||
Field const& operator[] (int index) const { return (*mQuery)[index]; }
|
||||
|
||||
// named access
|
||||
Field const& operator[] (const std::string &name) const { return mQuery->Fetch()[GetField_idx(name)]; }
|
||||
QueryFieldNames const& GetFieldNames() const { return mFieldNames; }
|
||||
|
||||
uint32 GetField_idx(const std::string &name) const
|
||||
{
|
||||
for(size_t idx = 0; idx < mFieldNames.size(); ++idx)
|
||||
{
|
||||
if(mFieldNames[idx] == name)
|
||||
return idx;
|
||||
}
|
||||
ASSERT(false && "unknown field name");
|
||||
return uint32(-1);
|
||||
}
|
||||
|
||||
protected:
|
||||
QueryResult *mQuery;
|
||||
QueryFieldNames mFieldNames;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -20,20 +20,15 @@
|
|||
|
||||
#include "DatabaseEnv.h"
|
||||
|
||||
QueryResultMysql::QueryResultMysql(MYSQL_RES *result, uint64 rowCount, uint32 fieldCount) :
|
||||
QueryResult(rowCount, fieldCount), mResult(result)
|
||||
QueryResultMysql::QueryResultMysql(MYSQL_RES *result, MYSQL_FIELD *fields, uint64 rowCount, uint32 fieldCount) :
|
||||
QueryResult(rowCount, fieldCount), mResult(result)
|
||||
{
|
||||
|
||||
mCurrentRow = new Field[mFieldCount];
|
||||
ASSERT(mCurrentRow);
|
||||
|
||||
MYSQL_FIELD *fields = mysql_fetch_fields(mResult);
|
||||
|
||||
for (uint32 i = 0; i < mFieldCount; i++)
|
||||
{
|
||||
mFieldNames[i] = fields[i].name;
|
||||
mCurrentRow[i].SetType(ConvertNativeType(fields[i].type));
|
||||
}
|
||||
}
|
||||
|
||||
QueryResultMysql::~QueryResultMysql()
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@
|
|||
class QueryResultMysql : public QueryResult
|
||||
{
|
||||
public:
|
||||
QueryResultMysql(MYSQL_RES *result, uint64 rowCount, uint32 fieldCount);
|
||||
QueryResultMysql(MYSQL_RES *result, MYSQL_FIELD *fields, uint64 rowCount, uint32 fieldCount);
|
||||
|
||||
~QueryResultMysql();
|
||||
|
||||
|
|
|
|||
|
|
@ -21,17 +21,14 @@
|
|||
#include "DatabaseEnv.h"
|
||||
|
||||
QueryResultPostgre::QueryResultPostgre(PGresult *result, uint64 rowCount, uint32 fieldCount) :
|
||||
QueryResult(rowCount, fieldCount), mResult(result), mTableIndex(0)
|
||||
QueryResult(rowCount, fieldCount), mResult(result), mTableIndex(0)
|
||||
{
|
||||
|
||||
mCurrentRow = new Field[mFieldCount];
|
||||
ASSERT(mCurrentRow);
|
||||
|
||||
for (uint32 i = 0; i < mFieldCount; i++)
|
||||
{
|
||||
mFieldNames[i] = PQfname(result, i);
|
||||
mCurrentRow[i].SetType(ConvertNativeType(PQftype( result, i )));
|
||||
}
|
||||
}
|
||||
|
||||
QueryResultPostgre::~QueryResultPostgre()
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#ifndef __REVISION_NR_H__
|
||||
#define __REVISION_NR_H__
|
||||
#define REVISION_NR "7934"
|
||||
#define REVISION_NR "7935"
|
||||
#endif // __REVISION_NR_H__
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue