Main | License | Installation | FAQ | Screenshots | Contact | Links | Sf.net | Freshmeat.net | KDE-Apps.org
Requirements | Design | Modules | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

MySQLSocket Class Reference
[MySQL C++ Wrappers]

#include <mysqlSocket.hpp>

Inheritance diagram for MySQLSocket:

Inheritance graph
[legend]

Public Member Functions

 MySQLSocket (const std::string &_userName, const std::string &_passWord, const std::string &_socketName, MySQLDaemonPtr _daemon=MySQLDaemonPtr()) throw (Error)
virtual ~MySQLSocket (void)
 Destructor.
const std::string & getSocketName (void) const
virtual void connect (const std::string &database, MYSQL *m) throw (Error)
MySQLDaemonPtr getDaemon (void) const

Protected Attributes

std::string socketName
MySQLDaemonPtr daemon

Detailed Description

Store information for connecting to MySQL using a file-socket.
Date:
Sun Feb 19 2006
Author:
Jan Wedekind (wedesoft@users.sourceforge.net)

Definition at line 28 of file mysqlSocket.hpp.


Constructor & Destructor Documentation

MySQLSocket::MySQLSocket ( const std::string &  _userName,
const std::string &  _passWord,
const std::string &  _socketName,
MySQLDaemonPtr  _daemon = MySQLDaemonPtr() 
) throw (Error)

Constructor. Store information about the file-socket and the MySQL server.

Parameters:
_userName Name of database user.
_passWord Password of database-user.
_socketName Name of the socket for connecting to the server (for example /var/lib/mysql/mysql.sock).

Definition at line 23 of file mysqlSocket.cpp.

00026                                                                 :
00027   MySQLServer( _userName, _passWord ), socketName(_socketName),
00028   daemon(_daemon)
00029 {
00030   connect( "", &connection );
00031 }

MySQLSocket::~MySQLSocket ( void   )  [virtual]

Destructor.

Definition at line 84 of file mysqlSocket.cpp.

References MySQLServer::connection.

00085 {
00086   mysql_close( &connection );
00087 }


Member Function Documentation

const std::string& MySQLSocket::getSocketName ( void   )  const [inline]

Definition at line 44 of file mysqlSocket.hpp.

References socketName.

00044 { return socketName; }

void MySQLSocket::connect ( const std::string &  database,
MYSQL *  m 
) throw (Error) [virtual]

Implements MySQLServer.

Definition at line 33 of file mysqlSocket.cpp.

References timer::elapsed(), and ERRORMACRO.

00035 {
00036   mysql_init( m );
00037 
00038   const int timeout = 10;// seconds
00039   mysql_options( m, MYSQL_OPT_CONNECT_TIMEOUT,
00040                  (const char *)&timeout );
00041 
00042   try {
00043 
00044     MYSQL *result = NULL;
00045     timer t;
00046 
00047     // Loop if the mysql-daemon was started by this program (server may not
00048     // be up yet in this case).
00049     do {
00050       // Specifying an alternative port will lead to a crash in the library :-(
00051       result = mysql_real_connect( m, NULL,
00052                                    userName.c_str(),
00053                                    passWord.empty() ? (const char *)NULL :
00054                                    passWord.c_str(),
00055                                    database.empty() ? (const char *)NULL :
00056                                    database.c_str(),
00057                                    0, socketName.c_str(), 0 );
00058       if ( result == NULL ) {
00059         if ( t.elapsed() > timeout ||
00060              mysql_errno( m ) != CR_CONNECTION_ERROR ||
00061              !daemon ) {
00062           ERRORMACRO( result != NULL, Error, , "Error connecting to socket '"
00063                       << socketName << "' as user '" << userName << "' "
00064                       << ( passWord.empty() ? "without" : "with" )
00065                       << " password: " << mysql_error( m ) );
00066         } else
00067           sleep( 1 );
00068       };
00069     } while ( result == NULL );
00070 
00071     // Set character-set of connection to UTF-8.
00072     // If the server is older than MySQL 4.1, the resulting error-message is
00073     // being ignored.
00074     mysql_query( m, "SET NAMES utf8;" );
00075 
00076   } catch ( Error &e ) {
00077 
00078     mysql_close( m );
00079     throw e;
00080 
00081   };
00082 };

MySQLDaemonPtr MySQLSocket::getDaemon ( void   )  const [inline]

Definition at line 49 of file mysqlSocket.hpp.

References daemon.

00049 { return daemon; }


Field Documentation

std::string MySQLSocket::socketName [protected]

Definition at line 52 of file mysqlSocket.hpp.

Referenced by getSocketName().

MySQLDaemonPtr MySQLSocket::daemon [protected]

Definition at line 54 of file mysqlSocket.hpp.

Referenced by getDaemon().


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

anymeal 0.30 - recipe management software - Make the most of your food! - © Jan Wedekind Sun Sep 14 16:01:29 2008 - GNU Free Documentation License