OregonCore  revision be9e804-git
Your Favourite TBC server
SqlQueryHolder Class Reference

#include <SqlOperations.h>

+ Inheritance diagram for SqlQueryHolder:

Public Member Functions

 SqlQueryHolder ()
 
 ~SqlQueryHolder ()
 
bool SetQuery (size_t index, const char *sql)
 
bool SetPQuery (size_t index, const char *format,...) ATTR_PRINTF(3
 
bool void SetSize (size_t size)
 
QueryResult_AutoPtr GetResult (size_t index)
 
void SetResult (size_t index, QueryResult_AutoPtr result)
 
bool Execute (Oregon::IQueryCallback *callback, SqlDelayThread *thread, SqlResultQueue *queue)
 

Private Types

typedef std::pair< const char *, QueryResult_AutoPtrSqlResultPair
 

Private Attributes

std::vector< SqlResultPairm_queries
 

Friends

class SqlQueryHolderEx
 

Detailed Description

Definition at line 173 of file SqlOperations.h.

Member Typedef Documentation

typedef std::pair<const char*, QueryResult_AutoPtr> SqlQueryHolder::SqlResultPair
private

Definition at line 177 of file SqlOperations.h.

Constructor & Destructor Documentation

SqlQueryHolder::SqlQueryHolder ( )
inline

Definition at line 180 of file SqlOperations.h.

References ATTR_PRINTF, and SqlOperation::Execute().

180 {}
SqlQueryHolder::~SqlQueryHolder ( )

Definition at line 134 of file SqlOperations.cpp.

135 {
136  for (size_t i = 0; i < m_queries.size(); i++)
137  {
138  // if the result was never used, free the resources
139  // results used already (getresult called) are expected to be deleted
140  if (m_queries[i].first != NULL)
141  free((void*)(const_cast<char*>(m_queries[i].first)));
142  }
143 }
std::vector< SqlResultPair > m_queries

Member Function Documentation

bool SqlQueryHolder::Execute ( Oregon::IQueryCallback callback,
SqlDelayThread thread,
SqlResultQueue queue 
)

Definition at line 55 of file SqlOperations.cpp.

References SqlDelayThread::Delay().

Referenced by Database::DelayQueryHolder().

56 {
57  if (!callback || !thread || !queue)
58  return false;
59 
60  // delay the execution of the queries, sync them with the delay thread
61  // which will in turn resync on execution (via the queue) and call back
62  SqlQueryHolderEx* holderEx = new SqlQueryHolderEx(this, callback, queue);
63  thread->Delay(holderEx);
64  return true;
65 }
friend class SqlQueryHolderEx
bool Delay(SqlOperation *sql)
QueryResult_AutoPtr SqlQueryHolder::GetResult ( size_t  index)

Definition at line 110 of file SqlOperations.cpp.

Referenced by WorldSession::HandlePlayerLogin(), and Player::LoadFromDB().

111 {
112  if (index < m_queries.size())
113  {
114  // the query strings are freed on the first GetResult or in the destructor
115  if (m_queries[index].first != NULL)
116  {
117  free((void*)(const_cast<char*>(m_queries[index].first)));
118  m_queries[index].first = NULL;
119  }
120  // when you get a result aways remember to delete it!
121  return m_queries[index].second;
122  }
123  else
124  return QueryResult_AutoPtr(NULL);
125 }
ACE_Refcounted_Auto_Ptr< QueryResult, ACE_Null_Mutex > QueryResult_AutoPtr
Definition: QueryResult.h:113
std::vector< SqlResultPair > m_queries
bool SqlQueryHolder::SetPQuery ( size_t  index,
const char *  format,
  ... 
)

Definition at line 87 of file SqlOperations.cpp.

References MAX_QUERY_LEN, sLog, and vsnprintf.

Referenced by LoginQueryHolder::Initialize().

88 {
89  if (!format)
90  {
91  sLog.outError("Query (index: %lu) is empty.", index);
92  return false;
93  }
94 
95  va_list ap;
96  char szQuery [MAX_QUERY_LEN];
97  va_start(ap, format);
98  int res = vsnprintf(szQuery, MAX_QUERY_LEN, format, ap);
99  va_end(ap);
100 
101  if (res == -1)
102  {
103  sLog.outError("SQL Query truncated (and not execute) for format: %s", format);
104  return false;
105  }
106 
107  return SetQuery(index, szQuery);
108 }
#define sLog
Log class singleton.
Definition: Log.h:187
bool SetQuery(size_t index, const char *sql)
#define vsnprintf
Definition: Common.h:131
#define MAX_QUERY_LEN
Definition: Database.h:44
bool SqlQueryHolder::SetQuery ( size_t  index,
const char *  sql 
)

Definition at line 67 of file SqlOperations.cpp.

References sLog.

68 {
69  if (m_queries.size() <= index)
70  {
71  sLog.outError("Query index (%lu) out of range (size: %u) for query: %s", index, (uint32)m_queries.size(), sql);
72  return false;
73  }
74 
75  if (m_queries[index].first != NULL)
76  {
77  sLog.outError("Attempt assign query to holder index (%lu) where other query stored (Old: [%s] New: [%s])",
78  index, m_queries[index].first, sql);
79  return false;
80  }
81 
82  // not executed yet, just stored (it's not called a holder for nothing)
83  m_queries[index] = SqlResultPair(strdup(sql), QueryResult_AutoPtr(NULL));
84  return true;
85 }
#define sLog
Log class singleton.
Definition: Log.h:187
ACE_Refcounted_Auto_Ptr< QueryResult, ACE_Null_Mutex > QueryResult_AutoPtr
Definition: QueryResult.h:113
std::vector< SqlResultPair > m_queries
ACE_UINT32 uint32
Definition: Define.h:71
std::pair< const char *, QueryResult_AutoPtr > SqlResultPair
void SqlQueryHolder::SetResult ( size_t  index,
QueryResult_AutoPtr  result 
)

Definition at line 127 of file SqlOperations.cpp.

128 {
129  // store the result in the holder
130  if (index < m_queries.size())
131  m_queries[index].second = result;
132 }
std::vector< SqlResultPair > m_queries
void SqlQueryHolder::SetSize ( size_t  size)

Definition at line 145 of file SqlOperations.cpp.

Referenced by LoginQueryHolder::Initialize().

146 {
147  // to optimize push_back, reserve the number of queries about to be executed
148  m_queries.resize(size);
149 }
std::vector< SqlResultPair > m_queries

Friends And Related Function Documentation

friend class SqlQueryHolderEx
friend

Definition at line 175 of file SqlOperations.h.

Member Data Documentation

std::vector<SqlResultPair> SqlQueryHolder::m_queries
private

Definition at line 178 of file SqlOperations.h.


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