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.cpp

Go to the documentation of this file.
00001 /* Copyright (C) 2006 Jan Wedekind.
00002    This file is part of the recipe database application AnyMeal.
00003 
00004    AnyMeal is free software; you can redistribute it and/or modify it under
00005    the terms of the GNU GENERAL PUBLIC LICENSE as published by the Free
00006    Software Foundation; either version 3 of the License, or (at your option)
00007    any later version.
00008 
00009    AnyMeal is distributed in the hope that it will be useful, but WITHOUT ANY
00010    WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
00011    FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
00012    details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
00016 #include <mysql/errmsg.h>
00017 #include "mysqlDatabase.hpp"
00018 #include "mysqlSocket.hpp"
00019 #include "timer.hpp"
00020 
00021 using namespace std;
00022 
00023 MySQLSocket::MySQLSocket( const std::string &_userName,
00024                           const std::string &_passWord,
00025                           const std::string &_socketName,
00026                           MySQLDaemonPtr _daemon ) throw (Error):
00027   MySQLServer( _userName, _passWord ), socketName(_socketName),
00028   daemon(_daemon)
00029 {
00030   connect( "", &connection );
00031 }
00032 
00033 void MySQLSocket::connect( const std::string &database, MYSQL *m )
00034   throw (Error)
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 };
00083 
00084 MySQLSocket::~MySQLSocket(void)
00085 {
00086   mysql_close( &connection );
00087 }


anymeal 0.31 - recipe management software - Make the most of your food! - © Jan Wedekind Sun Dec 19 19:36:02 2010 - GNU Free Documentation License