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

mysqlNetwork.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/mysqld_error.h>
00017 #include "mysqlDatabase.hpp"
00018 #include "mysqlNetwork.hpp"
00019 
00020 using namespace std;
00021 
00022 MySQLNetwork::MySQLNetwork( const std::string &_userName,
00023                             const std::string &_passWord,
00024                             const std::string &_serverName,
00025                             int _portNumber ) throw (Error):
00026   MySQLServer( _userName, _passWord ), serverName(_serverName),
00027   portNumber( _portNumber )
00028 {
00029   connect( "", &connection );
00030 }
00031 
00032 
00033 void MySQLNetwork::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     // Specifying an alternative port will lead to a crash in the library :-(
00045     ERRORMACRO( mysql_real_connect( m,
00046                                     serverName.c_str(), userName.c_str(),
00047                                     passWord.empty() ? (const char *)NULL :
00048                                                        passWord.c_str(),
00049                                     database.empty() ? (const char *)NULL :
00050                                                        database.c_str(),
00051                                     portNumber != MYSQL_PORT ? portNumber : 0,
00052                                     NULL, 0 ),
00053                 Error, , "Error connecting to server '"
00054                 << serverName << "' as user '" << userName << "' "
00055                 << ( passWord.empty() ? "without" : "with" ) << " password: "
00056                 << mysql_error( m ) );
00057 
00058     // Set character-set of connection to UTF-8.
00059     // If the server is older than MySQL 4.1, the resulting error-message is
00060     // being ignored.
00061     mysql_query( m, "SET NAMES utf8;" );
00062 
00063   } catch ( Error &e ) {
00064 
00065     mysql_close( m );
00066     throw e;
00067 
00068   };
00069 };
00070 
00071 
00072 MySQLNetwork::~MySQLNetwork(void)
00073 {
00074   mysql_close( &connection );
00075 }


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