The InspIRCd Project
Home | Developers | Wiki | Forums | Bug Tracker | SVN | Download | Blog | Stats
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

SQLConnection Class Reference

Represents a connection to a mysql database. More...

Inheritance diagram for SQLConnection:

Inheritance graph
[legend]
Collaboration diagram for SQLConnection:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 SQLConnection (const SQLhost &hi)
 ~SQLConnection ()
bool Connect ()
void DoLeadingQuery ()
bool ConnectionLost ()
bool CheckConnection ()
std::string GetError ()
const std::stringGetID ()
std::string GetHost ()
void SetEnable (bool Enable)
bool IsEnabled ()
void Close ()
const SQLhostGetConfHost ()

Public Attributes

QueryQueue queue
ResultQueue rq

Protected Attributes

MYSQL connection
MYSQL_RES * res
MYSQL_ROW row
SQLhost host
std::map< std::string, std::stringthisrow
bool Enabled

Detailed Description

Represents a connection to a mysql database.

Definition at line 277 of file m_mysql.cpp.


Constructor & Destructor Documentation

SQLConnection::SQLConnection const SQLhost hi  )  [inline]
 

Definition at line 294 of file m_mysql.cpp.

00294                                          : host(hi), Enabled(false)
00295         {
00296         }

SQLConnection::~SQLConnection  )  [inline]
 

Definition at line 298 of file m_mysql.cpp.

References Close().

00299         {
00300                 Close();
00301         }


Member Function Documentation

bool SQLConnection::CheckConnection  )  [inline]
 

Definition at line 433 of file m_mysql.cpp.

References Connect(), and ConnectionLost().

Referenced by DoLeadingQuery().

00434         {
00435                 if (ConnectionLost()) {
00436                         return Connect();
00437                 }
00438                 else return true;
00439         }

void SQLConnection::Close  )  [inline]
 

Definition at line 466 of file m_mysql.cpp.

Referenced by ~SQLConnection().

00467         {
00468                 mysql_close(&connection);
00469         }

bool SQLConnection::Connect  )  [inline]
 

Definition at line 305 of file m_mysql.cpp.

References SQLhost::host, host, SQLhost::name, SQLhost::pass, SQLhost::port, and SQLhost::user.

Referenced by CheckConnection().

00306         {
00307                 unsigned int timeout = 1;
00308                 mysql_init(&connection);
00309                 mysql_options(&connection,MYSQL_OPT_CONNECT_TIMEOUT,(char*)&timeout);
00310                 return mysql_real_connect(&connection, host.host.c_str(), host.user.c_str(), host.pass.c_str(), host.name.c_str(), host.port, NULL, 0);
00311         }

bool SQLConnection::ConnectionLost  )  [inline]
 

Definition at line 425 of file m_mysql.cpp.

Referenced by CheckConnection().

00426         {
00427                 if (&connection) {
00428                         return (mysql_ping(&connection) != 0);
00429                 }
00430                 else return false;
00431         }

void SQLConnection::DoLeadingQuery  )  [inline]
 

Definition at line 313 of file m_mysql.cpp.

References CheckConnection(), ConvToStr(), SQLresult::dbid, QueryQueue::front(), GetID(), Request::GetSource(), SQLrequest::id, NotifyMainThread(), SQLquery::p, SQLquery::q, QREPLY_FAIL, SQLresult::query, SQLrequest::query, queue, queue_mutex, res, results_mutex, and rq.

Referenced by DispatcherThread().

00314         {
00315                 if (!CheckConnection())
00316                         return;
00317 
00318                 /* Parse the command string and dispatch it to mysql */
00319                 SQLrequest& req = queue.front();
00320 
00321                 /* Pointer to the buffer we screw around with substitution in */
00322                 char* query;
00323 
00324                 /* Pointer to the current end of query, where we append new stuff */
00325                 char* queryend;
00326 
00327                 /* Total length of the unescaped parameters */
00328                 unsigned long paramlen;
00329 
00330                 /* Total length of query, used for binary-safety in mysql_real_query */
00331                 unsigned long querylength = 0;
00332 
00333                 paramlen = 0;
00334 
00335                 for(ParamL::iterator i = req.query.p.begin(); i != req.query.p.end(); i++)
00336                 {
00337                         paramlen += i->size();
00338                 }
00339 
00340                 /* To avoid a lot of allocations, allocate enough memory for the biggest the escaped query could possibly be.
00341                  * sizeofquery + (totalparamlength*2) + 1
00342                  *
00343                  * The +1 is for null-terminating the string for mysql_real_escape_string
00344                  */
00345 
00346                 query = new char[req.query.q.length() + (paramlen*2) + 1];
00347                 queryend = query;
00348 
00349                 /* Okay, now we have a buffer large enough we need to start copying the query into it and escaping and substituting
00350                  * the parameters into it...
00351                  */
00352 
00353                 for(unsigned long i = 0; i < req.query.q.length(); i++)
00354                 {
00355                         if(req.query.q[i] == '?')
00356                         {
00357                                 /* We found a place to substitute..what fun.
00358                                  * use mysql calls to escape and write the
00359                                  * escaped string onto the end of our query buffer,
00360                                  * then we "just" need to make sure queryend is
00361                                  * pointing at the right place.
00362                                  */
00363                                 if(req.query.p.size())
00364                                 {
00365                                         unsigned long len = mysql_real_escape_string(&connection, queryend, req.query.p.front().c_str(), req.query.p.front().length());
00366 
00367                                         queryend += len;
00368                                         req.query.p.pop_front();
00369                                 }
00370                                 else
00371                                         break;
00372                         }
00373                         else
00374                         {
00375                                 *queryend = req.query.q[i];
00376                                 queryend++;
00377                         }
00378                         querylength++;
00379                 }
00380 
00381                 *queryend = 0;
00382 
00383                 pthread_mutex_lock(&queue_mutex);
00384                 req.query.q = query;
00385                 pthread_mutex_unlock(&queue_mutex);
00386 
00387                 if (!mysql_real_query(&connection, req.query.q.data(), req.query.q.length()))
00388                 {
00389                         /* Successfull query */
00390                         res = mysql_use_result(&connection);
00391                         unsigned long rows = mysql_affected_rows(&connection);
00392                         MySQLresult* r = new MySQLresult(SQLModule, req.GetSource(), res, rows, req.id);
00393                         r->dbid = this->GetID();
00394                         r->query = req.query.q;
00395                         /* Put this new result onto the results queue.
00396                          * XXX: Remember to mutex the queue!
00397                          */
00398                         pthread_mutex_lock(&results_mutex);
00399                         rq.push_back(r);
00400                         pthread_mutex_unlock(&results_mutex);
00401                 }
00402                 else
00403                 {
00404                         /* XXX: See /usr/include/mysql/mysqld_error.h for a list of
00405                          * possible error numbers and error messages */
00406                         SQLerror e(QREPLY_FAIL, ConvToStr(mysql_errno(&connection)) + std::string(": ") + mysql_error(&connection));
00407                         MySQLresult* r = new MySQLresult(SQLModule, req.GetSource(), e, req.id);
00408                         r->dbid = this->GetID();
00409                         r->query = req.query.q;
00410 
00411                         pthread_mutex_lock(&results_mutex);
00412                         rq.push_back(r);
00413                         pthread_mutex_unlock(&results_mutex);
00414                 }
00415 
00416                 /* Now signal the main thread that we've got a result to process.
00417                  * Pass them this connection id as what to examine
00418                  */
00419 
00420                 delete[] query;
00421 
00422                 NotifyMainThread(this);
00423         }

const SQLhost& SQLConnection::GetConfHost  )  [inline]
 

Definition at line 471 of file m_mysql.cpp.

References host.

00472         {
00473                 return host;
00474         }

std::string SQLConnection::GetError  )  [inline]
 

Definition at line 441 of file m_mysql.cpp.

00442         {
00443                 return mysql_error(&connection);
00444         }

std::string SQLConnection::GetHost  )  [inline]
 

Definition at line 451 of file m_mysql.cpp.

References SQLhost::host, and host.

00452         {
00453                 return host.host;
00454         }

const std::string& SQLConnection::GetID  )  [inline]
 

Definition at line 446 of file m_mysql.cpp.

References host, and SQLhost::id.

Referenced by DoLeadingQuery(), and NotifyMainThread().

00447         {
00448                 return host.id;
00449         }

bool SQLConnection::IsEnabled  )  [inline]
 

Definition at line 461 of file m_mysql.cpp.

References Enabled.

00462         {
00463                 return Enabled;
00464         }

void SQLConnection::SetEnable bool  Enable  )  [inline]
 

Definition at line 456 of file m_mysql.cpp.

References Enabled.

00457         {
00458                 Enabled = Enable;
00459         }


Member Data Documentation

MYSQL SQLConnection::connection [protected]
 

Definition at line 281 of file m_mysql.cpp.

bool SQLConnection::Enabled [protected]
 

Definition at line 286 of file m_mysql.cpp.

Referenced by IsEnabled(), and SetEnable().

SQLhost SQLConnection::host [protected]
 

Definition at line 284 of file m_mysql.cpp.

Referenced by Connect(), GetConfHost(), GetHost(), and GetID().

QueryQueue SQLConnection::queue
 

Definition at line 290 of file m_mysql.cpp.

Referenced by DispatcherThread(), and DoLeadingQuery().

MYSQL_RES* SQLConnection::res [protected]
 

Definition at line 282 of file m_mysql.cpp.

Referenced by DoLeadingQuery().

MYSQL_ROW SQLConnection::row [protected]
 

Definition at line 283 of file m_mysql.cpp.

ResultQueue SQLConnection::rq
 

Definition at line 291 of file m_mysql.cpp.

Referenced by DoLeadingQuery().

std::map<std::string,std::string> SQLConnection::thisrow [protected]
 

Definition at line 285 of file m_mysql.cpp.


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