DB::MySQL_Connection Class Reference

#include <mysql_db.h>

Inheritance diagram for DB::MySQL_Connection:

DB::Connection List of all members.

Public Member Functions

 MySQL_Connection ()
 ~MySQL_Connection ()
bool open (const char *database, const char *host, const int port, const char *user, const char *pass)
bool close (void)
bool isConnected (void)
bool execute (const char *sql)
ResultSetexecuteQuery (const char *sql)
char * escape (const char *)
const char * unixtimeToSql (const time_t)
const unsigned long insertId (void)
bool beginTrans (void)
bool commitTrans (void)
bool rollbackTrans (void)
bool setTransactionMode (const enum TRANS_MODE mode)
unsigned int errorno (void) const
const char * errormsg (void) const
const char * version (void) const
void * operator new (size_t bytes)
void operator delete (void *ptr)

Private Member Functions

 MySQL_Connection (const MySQL_Connection &old)
const MySQL_Connectionoperator= (const MySQL_Connection &old)

Private Attributes

MYSQL * mysql_

Detailed Description

Definition at line 76 of file mysql_db.h.


Constructor & Destructor Documentation

DB::MySQL_Connection::MySQL_Connection ( const MySQL_Connection old  )  [private]

DB::MySQL_Connection::MySQL_Connection (  )  [inline]

Definition at line 83 of file mysql_db.h.

00083 : mysql_(NULL) {};

DB::MySQL_Connection::~MySQL_Connection (  )  [inline]

Definition at line 84 of file mysql_db.h.

References close().

00084 { close(); }


Member Function Documentation

bool DB::MySQL_Connection::beginTrans ( void   )  [virtual]

Begins a new transaction. If for some reason it fails, false will be returned (for instance, if the underlying driver does not support transactions.)

Returns:
bool

Implements DB::Connection.

Definition at line 313 of file mysql_db.cpp.

References mysql_.

00314 {
00315     if (!mysql_) return (false);
00316     if (mysql_query(mysql_, "SET AUTOCOMMIT = 0") != 0) {
00317         return (false);
00318     }
00319     if (mysql_query(mysql_, "BEGIN") == 0) {
00320         return (true);
00321     }
00322     return (false);
00323 }

bool DB::MySQL_Connection::close ( void   )  [virtual]

Close the database connection

Returns:
bool True if successful.

Implements DB::Connection.

Definition at line 239 of file mysql_db.cpp.

References mysql_.

Referenced by ~MySQL_Connection().

00240 {
00241     if (!mysql_) return (false);
00242     mysql_close(mysql_);
00243     mysql_ = NULL;
00244     return (true);
00245 }

bool DB::MySQL_Connection::commitTrans ( void   )  [virtual]

Attempts to commit the current transaction

Returns:
bool

Implements DB::Connection.

Definition at line 326 of file mysql_db.cpp.

References mysql_.

00327 {
00328     if (!mysql_) return (false);
00329     if (mysql_query(mysql_, "COMMIT") == 0) {
00330         return (true);
00331     }
00332     return (false);
00333 }

const char * DB::MySQL_Connection::errormsg ( void   )  const [virtual]

Returns a textual representation of the last error to occur. The value returned should NOT be freed.

Returns:
const char*

Implements DB::Connection.

Definition at line 376 of file mysql_db.cpp.

References mysql_.

00377 {
00378     return (mysql_error(mysql_));
00379 }

unsigned int DB::MySQL_Connection::errorno ( void   )  const [virtual]

Returns the last error code to occur.

Returns:
unsigned int

Implements DB::Connection.

Definition at line 370 of file mysql_db.cpp.

References mysql_.

00371 {
00372     return (mysql_errno(mysql_));
00373 }

char * DB::MySQL_Connection::escape ( const char *   )  [virtual]

Returns a database specific escaped string from the input. The string returned must be freed by the caller. This is used by the Query class.

Returns:
char*

Implements DB::Connection.

Definition at line 282 of file mysql_db.cpp.

00283 {
00284     unsigned long len = strlen(str);
00285     char *buf = new char[len*2+4];
00286     buf[0] = '\'';
00287     len = mysql_escape_string(buf+1, str, len);
00288     buf[++len] = '\'';
00289     buf[++len] = 0;
00290     return (buf);
00291 }

bool DB::MySQL_Connection::execute ( const char *  sql  )  [virtual]

Executes a query to the database, discarding any result data. This function is typically used for non-SELECT statements.

Parameters:
sql 
Returns:
bool

Implements DB::Connection.

Definition at line 255 of file mysql_db.cpp.

References mysql_.

Referenced by setTransactionMode().

00256 {
00257     if (!mysql_) return (false);
00258     if (mysql_query(mysql_, sql) == 0) {
00259         return (true);
00260     }
00261     return (false);
00262 }

DB::ResultSet * DB::MySQL_Connection::executeQuery ( const char *  sql  )  [virtual]

Executes a query to the database which returns row data. The row data is manipulated using the ResultSet object returned by this method. If the database wasn't able to return row data or an error occurs, then zero is returned. Therefore be careful to check the returned value, before using it - or a seg. fault is possible!

Parameters:
sql 
Returns:
ResultSet*

Implements DB::Connection.

Definition at line 265 of file mysql_db.cpp.

References mysql_.

00266 {
00267     if (!mysql_) return (false);
00268 
00269     if (mysql_query(mysql_, sql)) {
00270         return (0);
00271     }
00272     MYSQL_RES *res = mysql_use_result(mysql_);
00273     if (!res) {
00274         return (0);
00275     }
00276     DB::ResultSet *c = 0;
00277     ACE_NEW_RETURN (c, DB::MySQL_ResultSet(res), 0);
00278     return (c);
00279 }

const unsigned long DB::MySQL_Connection::insertId ( void   )  [virtual]

Returns the last SQL INSERT unique identifier if the underlying database supports this feature.

Returns:
const unsigned long

Implements DB::Connection.

Definition at line 307 of file mysql_db.cpp.

References mysql_.

00308 {
00309     return ((const unsigned long) mysql_insert_id(mysql_));
00310 }

bool DB::MySQL_Connection::isConnected ( void   )  [virtual]

Attempts to tell if we are still connected to the database.

Returns:
bool

Implements DB::Connection.

Definition at line 248 of file mysql_db.cpp.

References mysql_.

00249 {
00250     if (!mysql_) return (false);
00251     return ((mysql_ping(mysql_) == 0 ? true : false));
00252 }

bool DB::MySQL_Connection::open ( const char *  database,
const char *  host,
const int  port,
const char *  user,
const char *  pass 
) [virtual]

Open a connection to a database.

Parameters:
database Database name. Sqlite3 uses this for the filename.
host Hostname
port Port
user Username
pass Password
Returns:
bool True if successful.

Implements DB::Connection.

Definition at line 215 of file mysql_db.cpp.

References mysql_.

00216 {
00217     if ((mysql_ = mysql_init(NULL)) == NULL) {
00218         return (false);
00219     }
00220     if (mysql_real_connect(
00221             mysql_, 
00222             host, 
00223             user, 
00224             pass, 
00225             NULL, 
00226             (unsigned)port, 
00227             NULL, 
00228             0
00229             ) == NULL) {
00230         return (false);
00231     }
00232     if (mysql_select_db(mysql_, database) != 0) {
00233         return (false);
00234     }
00235     return (true);
00236 }

void DB::MySQL_Connection::operator delete ( void *  ptr  ) 

Definition at line 409 of file mysql_db.cpp.

00410 {
00411   delete [] static_cast <char *> (ptr);
00412 }

void * DB::MySQL_Connection::operator new ( size_t  bytes  ) 

Definition at line 390 of file mysql_db.cpp.

00391 {
00392   return (::new char[bytes]);
00393 }

const MySQL_Connection& DB::MySQL_Connection::operator= ( const MySQL_Connection old  )  [private]

bool DB::MySQL_Connection::rollbackTrans ( void   )  [virtual]

Attempts to rollback the current transaction

Returns:
bool

Implements DB::Connection.

Definition at line 336 of file mysql_db.cpp.

References mysql_.

00337 {
00338     if (!mysql_) return (false);
00339     if (mysql_query(mysql_, "ROLLBACK") == 0) {
00340         return (true);
00341     }
00342     return (false);
00343 }

bool DB::MySQL_Connection::setTransactionMode ( const enum TRANS_MODE  mode  ) 

Definition at line 346 of file mysql_db.cpp.

References execute(), DB::Connection::READ_COMMITTED, DB::Connection::READ_UNCOMMITTED, DB::Connection::REPEATABLE_READ, and DB::Connection::SERIALIZABLE.

00347 {
00348     const char *sql = "";
00349 
00350     switch (mode) {
00351     case READ_UNCOMMITTED:
00352         sql = "SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED";
00353         break;
00354     case READ_COMMITTED:
00355         sql = "SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED";
00356         break;
00357     case REPEATABLE_READ:
00358         sql = "SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ";
00359         break;
00360     case SERIALIZABLE:
00361         sql = "SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE";
00362         break;
00363     default:
00364         return false;
00365     }
00366     return (execute(sql));
00367 }

const char * DB::MySQL_Connection::unixtimeToSql ( const   time_t  )  [virtual]

Returns a database specific representation of the time_t value. This is used by the Query class. The string returned must be freed by the caller.

Parameters:
time_t 
Returns:
const char*

Implements DB::Connection.

Definition at line 294 of file mysql_db.cpp.

00295 {
00296     char *buf = new char[22];
00297     struct tm *tmp = new struct tm;
00298     buf[0] = '\'';
00299     ACE_OS::strftime(buf+1, 20, "%Y-%m-%d %H:%M:%S", ACE_OS::localtime_r(&val, tmp));
00300     buf[20] = '\'';
00301     buf[21] = 0;
00302     delete tmp;
00303     return (buf);
00304 }

const char * DB::MySQL_Connection::version ( void   )  const [virtual]

Returns the DB abstraction module's version number and possibly the version of the underlying database client library used. The returned value should NOT be freed.

Returns:
const char*

Implements DB::Connection.

Definition at line 382 of file mysql_db.cpp.

00383 {
00384     static char ret[256];
00385     ACE_OS::snprintf(ret, 256, "MySQL Driver v0.1 using MySQL client library v%s", mysql_get_client_info());
00386     return ((const char *) ret);
00387 }


Member Data Documentation

MYSQL* DB::MySQL_Connection::mysql_ [private]

Definition at line 119 of file mysql_db.h.

Referenced by beginTrans(), close(), commitTrans(), errormsg(), errorno(), execute(), executeQuery(), insertId(), isConnected(), open(), and rollbackTrans().


The documentation for this class was generated from the following files:
Generated on Tue Apr 24 18:59:42 2007 for DbAbstract by  doxygen 1.4.7