00001 #ifndef __MYSQL_DATABASE_CPP__
00002 #define __MYSQL_DATABASE_CPP__
00003
00004 #include <limits>
00005 #include <vector>
00006
00007 #include <db/mysql_database.h>
00008 #include <utilities/string_utilities.h>
00009
00010 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM )
00011 #include <winsock2.h>
00012 #undef max
00013 #endif
00014
00015 #include <mysql.h>
00016
00017 namespace nitro{
00018
00019 MySQLDatabase::MySQLDatabase( void )
00020 {
00021 try
00022 {
00023 Connection = NULL;
00024 Result = NULL;
00025 Row = NULL;
00026 RowId = std::numeric_limits< std::size_t >::max();
00027 }
00028 catch( nitro::exception e )
00029 {
00030 throw( nitro::exception( std::string( "MySQLDatabase::MySQLDatabase( void )::" ) + e.what() , e.code() ) );
00031 }
00032 catch( ... )
00033 {
00034 throw( nitro::exception( std::string( "MySQLDatabase::MySQLDatabase( void )::An error occured" ) , 0 ) );
00035 }
00036 }
00037
00038 MySQLDatabase::MySQLDatabase( const std::string & theConnectionString )
00039 {
00040 try
00041 {
00042 Connection = NULL;
00043 Result = NULL;
00044 Row = NULL;
00045 RowId = std::numeric_limits< std::size_t >::max();
00046
00047 Connect( theConnectionString );
00048 }
00049 catch( nitro::exception e )
00050 {
00051 throw( nitro::exception( std::string( "MySQLDatabase::MySQLDatabase( const std::string & theConnectionString )::" ) + e.what() , e.code() ) );
00052 }
00053 catch( ... )
00054 {
00055 throw( nitro::exception( std::string( "MySQLDatabase::MySQLDatabase( const std::string & theConnectionString )::An error occured" ) , 0 ) );
00056 }
00057 }
00058
00059 void MySQLDatabase::Connect( const std::string & theConnectionString )
00060 {
00061 try
00062 {
00063 if( Connection != NULL )
00064 {
00065 Disconnect();
00066 }
00067
00068 Connection = ( void * )mysql_init( NULL );
00069
00070 std::string Host( nitro::Parsers::GetCommandLineParameter( theConnectionString , std::string( "host" ) , std::string( "localhost" ) ) );
00071 std::string Port( nitro::Parsers::GetCommandLineParameter( theConnectionString , std::string( "port" ) , std::string( "3306" ) ) );
00072 std::string User( nitro::Parsers::GetCommandLineParameter( theConnectionString , std::string( "user" ) , std::string( "root" ) ) );
00073 std::string Password( nitro::Parsers::GetCommandLineParameter( theConnectionString , std::string( "password" ) , std::string( "" ) ) );
00074 std::string DatabaseName( nitro::Parsers::GetCommandLineParameter( theConnectionString , std::string( "dbname" ) , std::string( "mysql" ) ) );
00075 std::string CharacterSet( nitro::Parsers::GetCommandLineParameter( theConnectionString , std::string( "character_set" ) , std::string( "" ) ) );
00076
00077 if( !mysql_real_connect( ( MYSQL * ) Connection , Host.c_str() , User.c_str() , Password.c_str() , DatabaseName.c_str() , nitro::Converters::atoi( Port ) , NULL , 0 ) )
00078 {
00079 throw( nitro::exception( std::string( "An error occured while connecting to the server. Connection string : " ) + theConnectionString , 1 ) );
00080 }
00081
00082 if( !CharacterSet.empty() )
00083 {
00084 Query( std::string( "SET NAMES " ) + CharacterSet );
00085 }
00086 }
00087 catch( nitro::exception e )
00088 {
00089 throw( nitro::exception( std::string( "MySQLDatabase::Connect( const std::string & theConnectionString )::" ) + e.what() , e.code() ) );
00090 }
00091 catch( ... )
00092 {
00093 throw( nitro::exception( std::string( "MySQLDatabase::Connect( const std::string & theConnectionString )::An error occured" ) , 0 ) );
00094 }
00095 }
00096
00097 void MySQLDatabase::Query( const std::string & theQueryString )
00098 {
00099 try
00100 {
00101 ClearResult();
00102
00103 if( Row == NULL )
00104 {
00105 Row = ( void * ) new MYSQL_ROW;
00106 }
00107
00108 RowId = std::numeric_limits< std::size_t >::max();
00109
00110 if( mysql_query( ( MYSQL * ) Connection , theQueryString.c_str() ) )
00111 {
00112 throw( nitro::exception( std::string( "An error occured while query execution. Query string : " ) + theQueryString + " Error message : " + mysql_error( ( MYSQL * )Connection ) , 1 ) );
00113 }
00114
00115 Result = ( void * )mysql_store_result( ( MYSQL * ) Connection );
00116 }
00117 catch( nitro::exception e )
00118 {
00119 throw( nitro::exception( std::string( "MySQLDatabase::Query( const std::string & theQueryString )::" ) + e.what() , e.code() ) );
00120 }
00121 catch( ... )
00122 {
00123 throw( nitro::exception( std::string( "MySQLDatabase::Query( const std::string & theQueryString )::An error occured" ) , 0 ) );
00124 }
00125 }
00126
00127 void MySQLDatabase::Disconnect( void )
00128 {
00129 try
00130 {
00131 delete ( MYSQL_ROW * )Row;
00132 ClearResult();
00133 mysql_close( ( MYSQL * ) Connection );
00134 Connection = NULL;
00135 mysql_library_end();
00136 }
00137 catch( nitro::exception e )
00138 {
00139 throw( nitro::exception( std::string( "MySQLDatabase::Disconnect( void )::" ) + e.what() , e.code() ) );
00140 }
00141 catch( ... )
00142 {
00143 throw( nitro::exception( std::string( "MySQLDatabase::Disconnect( void )::An error occured" ) , 0 ) );
00144 }
00145 }
00146
00147 MySQLDatabase::~MySQLDatabase()
00148 {
00149 try
00150 {
00151 Disconnect();
00152 }
00153 catch( ... )
00154 {
00155 throw( nitro::exception( std::string( "MySQLDatabase::~MySQLDatabase()::An error occured" ) , 0 ) );
00156 }
00157 }
00158
00159 std::size_t MySQLDatabase::RecordCount( void )
00160 {
00161 try
00162 {
00163 if( Result == NULL )
00164 {
00165 return( 0 );
00166 }
00167
00168 return( ( std::size_t )mysql_num_rows( ( MYSQL_RES * ) Result ) );
00169 }
00170 catch( nitro::exception e )
00171 {
00172 throw( nitro::exception( std::string( "MySQLDatabase::RecordCount( void )::" ) + e.what() , e.code() ) );
00173 }
00174 catch( ... )
00175 {
00176 throw( nitro::exception( std::string( "MySQLDatabase::RecordCount( void )::An error occured" ) , 0 ) );
00177 }
00178 }
00179
00180 std::size_t MySQLDatabase::FieldCount( void )
00181 {
00182 try
00183 {
00184 if( Result == NULL )
00185 {
00186 return( 0 );
00187 }
00188
00189 return( mysql_field_count( ( MYSQL * ) Connection ) );
00190 }
00191 catch( nitro::exception e )
00192 {
00193 throw( nitro::exception( std::string( "MySQLDatabase::FieldCount( void )::" ) + e.what() , e.code() ) );
00194 }
00195 catch( ... )
00196 {
00197 throw( nitro::exception( std::string( "MySQLDatabase::FieldCount( void )::An error occured" ) , 0 ) );
00198 }
00199 }
00200
00201 const char * MySQLDatabase::GetRecordField( std::size_t i , std::size_t j )
00202 {
00203 try
00204 {
00205 if( RowId == std::numeric_limits< std::size_t >::max() || RowId != i )
00206 {
00207 mysql_data_seek( ( MYSQL_RES * ) Result , i );
00208 *( ( MYSQL_ROW * )this->Row ) = mysql_fetch_row( ( MYSQL_RES * ) Result );
00209 RowId = i;
00210 }
00211 return( ( * ( ( MYSQL_ROW * )this->Row ) )[ j ] ? ( * ( ( MYSQL_ROW * )this->Row ) )[ j ] : "" );
00212 }
00213 catch( nitro::exception e )
00214 {
00215 throw( nitro::exception( std::string( "MySQLDatabase::GetRecordField( std::size_t i , std::size_t j )::" ) + e.what() , e.code() ) );
00216 }
00217 catch( ... )
00218 {
00219 throw( nitro::exception( std::string( "MySQLDatabase::GetRecordField( std::size_t i , std::size_t j )::An error occured" ) , 0 ) );
00220 }
00221 }
00222
00223 const char * MySQLDatabase::GetFieldName( std::size_t j )
00224 {
00225 try
00226 {
00227 mysql_field_seek( ( MYSQL_RES * ) Result , ( MYSQL_FIELD_OFFSET )j );
00228 MYSQL_FIELD * Field;
00229
00230 Field = mysql_fetch_field( ( MYSQL_RES * ) Result );
00231 return( Field->name );
00232 }
00233 catch( nitro::exception e )
00234 {
00235 throw( nitro::exception( std::string( "MySQLDatabase::GetFieldName( std::size_t j )::" ) + e.what() , e.code() ) );
00236 }
00237 catch( ... )
00238 {
00239 throw( nitro::exception( std::string( "MySQLDatabase::GetFieldName( std::size_t j )::An error occured" ) , 0 ) );
00240 }
00241 }
00242
00243 void MySQLDatabase::ClearResult( void )
00244 {
00245 try
00246 {
00247 mysql_free_result( ( MYSQL_RES * ) Result );
00248 Result = NULL;
00249 }
00250 catch( nitro::exception e )
00251 {
00252 throw( nitro::exception( std::string( "MySQLDatabase::ClearResult( void )::" ) + e.what() , e.code() ) );
00253 }
00254 catch( ... )
00255 {
00256 throw( nitro::exception( std::string( "MySQLDatabase::ClearResult( void )::An error occured" ) , 0 ) );
00257 }
00258 }
00259
00260 };
00261
00262 #endif