mysqlxx.cpp

Go to the documentation of this file.
00001 /*
00002 _______________________________________________________________________________
00003 
00004                                  SqlWrapperXX
00005 
00006 -------------------------------------------------------------------------------
00007 Copyright (c) 2007 Kai Braaten
00008 
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015 
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018 
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 -------------------------------------------------------------------------------
00027 */
00028 
00029 #include "mysqlxx.hpp"
00030 
00031 // Public member functions
00032 
00033 MySqlContext::MySqlContext( const string &dbName,
00034                             const string &user,
00035                             const string &passwd,
00036                             const string &host,
00037                             const int port,
00038                             const string &unixSocket,
00039                             const unsigned long clientFlags )
00040   : handle( 0 ),
00041     _dbName( dbName ),
00042     _user( user ),
00043     _passwd( passwd ),
00044     _host( host ),
00045     _port( port ),
00046     _unixSocket( unixSocket ),
00047     _clientFlags( clientFlags )
00048 {
00049   if( !_dbName.empty() )
00050     {
00051       try
00052         {
00053           open();
00054         }
00055       catch( SqlDatabaseOpenException &e )
00056         {
00057           throw e;
00058         }
00059     }
00060 }
00061 
00062 MySqlContext::~MySqlContext()
00063 {
00064   close();
00065 }
00066 
00067 void MySqlContext::close()
00068 {
00069   if( handle )
00070     {
00071       mysql_close( handle );
00072       handle = 0;
00073     }
00074 }
00075 
00076 string MySqlContext::escapeString( const string &param ) const
00077 {
00078   char escapedSql[ param.length() * 2 + 1 ];
00079   mysql_real_escape_string( handle, escapedSql, param.c_str(), param.length() );
00080   return escapedSql;
00081 }
00082 
00083 string MySqlContext::version() const
00084 {
00085   string v = "MySQL client library ";
00086   v += mysql_get_client_info();
00087   v += "\nMySQL server ";
00088   v += mysql_get_server_info( handle );
00089   return v;
00090 }
00091 
00092 // Private member functions
00093 
00094 void MySqlContext::open()
00095 {
00096   if( handle == 0 )
00097     {
00098       if( !( handle = mysql_init( 0 ) ) )
00099         {
00100           throw SqlDatabaseOpenException( "insufficient memory to initialize context" );
00101         }
00102 
00103       bool success = mysql_real_connect( handle, _host.c_str(), _user.c_str(),
00104                                          _passwd.c_str(), _dbName.c_str(),
00105                                          _port, 0, _clientFlags );
00106 
00107       if( !success )
00108         {
00109           string ex = "Failed to connect to database: ";
00110           ex += mysql_error( handle );
00111           close();
00112           throw SqlDatabaseOpenException( ex );
00113         }
00114     }
00115 }
00116 
00117 SqlResult MySqlContext::query( const string &sql ) const
00118 {
00119   MYSQL_RES *rawResult = 0;
00120   SqlResult result;
00121 
00122   if( mysql_real_query( handle, sql.c_str(), sql.length() ) != 0 )
00123     {
00124       string ex = "Bad SQL statement: ";
00125       ex += sql;
00126       ex += "\nMySQL message: ";
00127       ex += mysql_error( handle );
00128       throw SqlDatabaseBadStatementException( ex );
00129     }
00130 
00131   do
00132     {
00133       if( ( rawResult = mysql_store_result( handle ) ) )
00134         {
00135           MYSQL_ROW rawRow;
00136           unsigned int numFields = mysql_num_fields( rawResult );
00137 
00138           while( ( rawRow = mysql_fetch_row( rawResult ) ) )
00139             {
00140               SqlRow *row = new SqlRow( numFields );
00141               result.add( row );
00142 
00143               for( int i = 0; i < row->columns(); ++i )
00144                 {
00145                   row->add( rawRow[ i ] );
00146                 }
00147             }
00148         }
00149 
00150       mysql_free_result( rawResult );
00151     } while( mysql_next_result( handle ) > 0 );
00152 
00153   return result;
00154 }

Generated on Sat Aug 18 16:21:44 2007 for SqlWrapperXX by  doxygen 1.5.0