Class provides MySQL routine. More...
#include <db/mysql_database.h>
Inherits nitro::Database.

Public Member Functions | |
| MySQLDatabase (void) | |
| MySQLDatabase (const std::string &theConnectionString) | |
| void | Connect (const std::string &theConnectionString) |
| void | Disconnect (void) |
| void | Query (const std::string &theQueryString) |
| virtual std::size_t | RecordCount (void) |
| virtual std::size_t | FieldCount (void) |
| virtual const char * | GetRecordField (std::size_t i, std::size_t j) |
| virtual const char * | GetFieldName (std::size_t j) |
| virtual void | ClearResult (void) |
| virtual | ~MySQLDatabase () |
Private Member Functions | |
| MySQLDatabase (const MySQLDatabase &DBObject) | |
| MySQLDatabase | operator= (const MySQLDatabase &DBObject) |
Private Attributes | |
| void * | Connection |
| void * | Result |
| void * | Row |
| std::size_t | RowId |
Class provides MySQL routine.
Definition at line 50 of file mysql_database.h.
| nitro::MySQLDatabase::MySQLDatabase | ( | void | ) |
Constructor.
| nitro::exception | Throws exception with the description of error. |
Definition at line 19 of file mysql_database.cpp.
References nitro::exception::code(), Connection, Result, Row, RowId, and nitro::exception::what().
{
try
{
Connection = NULL;
Result = NULL;
Row = NULL;
RowId = std::numeric_limits< std::size_t >::max();
}
catch( nitro::exception e )
{
throw( nitro::exception( std::string( "MySQLDatabase::MySQLDatabase( void )::" ) + e.what() , e.code() ) );
}
catch( ... )
{
throw( nitro::exception( std::string( "MySQLDatabase::MySQLDatabase( void )::An error occured" ) , 0 ) );
}
}

| nitro::MySQLDatabase::MySQLDatabase | ( | const std::string & | theConnectionString | ) |
Constructor.
| theConnectionString | - Connection settings. |
| nitro::exception | Throws exception with the description of error. |
Definition at line 38 of file mysql_database.cpp.
References nitro::exception::code(), Connect(), Connection, Result, Row, RowId, and nitro::exception::what().
{
try
{
Connection = NULL;
Result = NULL;
Row = NULL;
RowId = std::numeric_limits< std::size_t >::max();
Connect( theConnectionString );
}
catch( nitro::exception e )
{
throw( nitro::exception( std::string( "MySQLDatabase::MySQLDatabase( const std::string & theConnectionString )::" ) + e.what() , e.code() ) );
}
catch( ... )
{
throw( nitro::exception( std::string( "MySQLDatabase::MySQLDatabase( const std::string & theConnectionString )::An error occured" ) , 0 ) );
}
}

| nitro::MySQLDatabase::~MySQLDatabase | ( | ) | [virtual] |
Destructor.
Definition at line 147 of file mysql_database.cpp.
References Disconnect().
{
try
{
Disconnect();
}
catch( ... )
{
throw( nitro::exception( std::string( "MySQLDatabase::~MySQLDatabase()::An error occured" ) , 0 ) );
}
}

| nitro::MySQLDatabase::MySQLDatabase | ( | const MySQLDatabase & | DBObject | ) | [inline, private] |
Private copy-constructor.
| DBObject | - database object. |
Definition at line 282 of file mysql_database.h.
{}
| void nitro::MySQLDatabase::ClearResult | ( | void | ) | [virtual] |
Function clears results.
| nitro::exception | Throws exception with the description of error. |
Implements nitro::Database.
Definition at line 243 of file mysql_database.cpp.
References nitro::exception::code(), Result, and nitro::exception::what().
Referenced by Disconnect(), and Query().
{
try
{
mysql_free_result( ( MYSQL_RES * ) Result );
Result = NULL;
}
catch( nitro::exception e )
{
throw( nitro::exception( std::string( "MySQLDatabase::ClearResult( void )::" ) + e.what() , e.code() ) );
}
catch( ... )
{
throw( nitro::exception( std::string( "MySQLDatabase::ClearResult( void )::An error occured" ) , 0 ) );
}
}

| void nitro::MySQLDatabase::Connect | ( | const std::string & | theConnectionString | ) | [virtual] |
Function provide database connection routine.
| theConnectionString | - Connection settings. |
| nitro::exception | Throws exception with the description of error. |
Reimplemented from nitro::Database.
Definition at line 59 of file mysql_database.cpp.
References nitro::Converters::atoi(), nitro::exception::code(), Connection, Disconnect(), nitro::Parsers::GetCommandLineParameter(), Query(), and nitro::exception::what().
Referenced by MySQLDatabase().
{
try
{
if( Connection != NULL )
{
Disconnect();
}
Connection = ( void * )mysql_init( NULL );
std::string Host( nitro::Parsers::GetCommandLineParameter( theConnectionString , std::string( "host" ) , std::string( "localhost" ) ) );
std::string Port( nitro::Parsers::GetCommandLineParameter( theConnectionString , std::string( "port" ) , std::string( "3306" ) ) );
std::string User( nitro::Parsers::GetCommandLineParameter( theConnectionString , std::string( "user" ) , std::string( "root" ) ) );
std::string Password( nitro::Parsers::GetCommandLineParameter( theConnectionString , std::string( "password" ) , std::string( "" ) ) );
std::string DatabaseName( nitro::Parsers::GetCommandLineParameter( theConnectionString , std::string( "dbname" ) , std::string( "mysql" ) ) );
std::string CharacterSet( nitro::Parsers::GetCommandLineParameter( theConnectionString , std::string( "character_set" ) , std::string( "" ) ) );
if( !mysql_real_connect( ( MYSQL * ) Connection , Host.c_str() , User.c_str() , Password.c_str() , DatabaseName.c_str() , nitro::Converters::atoi( Port ) , NULL , 0 ) )
{
throw( nitro::exception( std::string( "An error occured while connecting to the server. Connection string : " ) + theConnectionString , 1 ) );
}
if( !CharacterSet.empty() )
{
Query( std::string( "SET NAMES " ) + CharacterSet );
}
}
catch( nitro::exception e )
{
throw( nitro::exception( std::string( "MySQLDatabase::Connect( const std::string & theConnectionString )::" ) + e.what() , e.code() ) );
}
catch( ... )
{
throw( nitro::exception( std::string( "MySQLDatabase::Connect( const std::string & theConnectionString )::An error occured" ) , 0 ) );
}
}

| void nitro::MySQLDatabase::Disconnect | ( | void | ) | [virtual] |
Function closes connection.
| nitro::exception | Throws exception with the description of error. |
Implements nitro::Database.
Definition at line 127 of file mysql_database.cpp.
References ClearResult(), nitro::exception::code(), Connection, Row, and nitro::exception::what().
Referenced by Connect(), and ~MySQLDatabase().
{
try
{
delete ( MYSQL_ROW * )Row;
ClearResult();
mysql_close( ( MYSQL * ) Connection );
Connection = NULL;
mysql_library_end();
}
catch( nitro::exception e )
{
throw( nitro::exception( std::string( "MySQLDatabase::Disconnect( void )::" ) + e.what() , e.code() ) );
}
catch( ... )
{
throw( nitro::exception( std::string( "MySQLDatabase::Disconnect( void )::An error occured" ) , 0 ) );
}
}

| std::size_t nitro::MySQLDatabase::FieldCount | ( | void | ) | [virtual] |
Function returns count of fields.
| nitro::exception | Throws exception with the description of error. |
Implements nitro::Database.
Definition at line 180 of file mysql_database.cpp.
References nitro::exception::code(), Connection, Result, and nitro::exception::what().
{
try
{
if( Result == NULL )
{
return( 0 );
}
return( mysql_field_count( ( MYSQL * ) Connection ) );
}
catch( nitro::exception e )
{
throw( nitro::exception( std::string( "MySQLDatabase::FieldCount( void )::" ) + e.what() , e.code() ) );
}
catch( ... )
{
throw( nitro::exception( std::string( "MySQLDatabase::FieldCount( void )::An error occured" ) , 0 ) );
}
}

| const char * nitro::MySQLDatabase::GetFieldName | ( | std::size_t | j | ) | [virtual] |
Function returns name of the field.
| j | - number of the field. |
| nitro::exception | Throws exception with the description of error. |
Implements nitro::Database.
Definition at line 223 of file mysql_database.cpp.
References nitro::exception::code(), Result, and nitro::exception::what().
{
try
{
mysql_field_seek( ( MYSQL_RES * ) Result , ( MYSQL_FIELD_OFFSET )j );
MYSQL_FIELD * Field;
Field = mysql_fetch_field( ( MYSQL_RES * ) Result );
return( Field->name );
}
catch( nitro::exception e )
{
throw( nitro::exception( std::string( "MySQLDatabase::GetFieldName( std::size_t j )::" ) + e.what() , e.code() ) );
}
catch( ... )
{
throw( nitro::exception( std::string( "MySQLDatabase::GetFieldName( std::size_t j )::An error occured" ) , 0 ) );
}
}

| const char * nitro::MySQLDatabase::GetRecordField | ( | std::size_t | i, | |
| std::size_t | j | |||
| ) | [virtual] |
Function returns value of the field.
| i | - number of the record. | |
| j | - number of the field. |
| nitro::exception | Throws exception with the description of error. |
Implements nitro::Database.
Definition at line 201 of file mysql_database.cpp.
References nitro::exception::code(), Result, Row, RowId, and nitro::exception::what().
{
try
{
if( RowId == std::numeric_limits< std::size_t >::max() || RowId != i )
{
mysql_data_seek( ( MYSQL_RES * ) Result , i );
*( ( MYSQL_ROW * )this->Row ) = mysql_fetch_row( ( MYSQL_RES * ) Result );
RowId = i;
}
return( ( * ( ( MYSQL_ROW * )this->Row ) )[ j ] ? ( * ( ( MYSQL_ROW * )this->Row ) )[ j ] : "" );
}
catch( nitro::exception e )
{
throw( nitro::exception( std::string( "MySQLDatabase::GetRecordField( std::size_t i , std::size_t j )::" ) + e.what() , e.code() ) );
}
catch( ... )
{
throw( nitro::exception( std::string( "MySQLDatabase::GetRecordField( std::size_t i , std::size_t j )::An error occured" ) , 0 ) );
}
}

| MySQLDatabase nitro::MySQLDatabase::operator= | ( | const MySQLDatabase & | DBObject | ) | [inline, private] |
Private assign operator.
| DBObject | - database object. |
Definition at line 298 of file mysql_database.h.
{return( *this );}
| void nitro::MySQLDatabase::Query | ( | const std::string & | theQueryString | ) | [virtual] |
Executing query.
| theQueryString | - Query. |
| nitro::exception | Throws exception with the description of error. |
Reimplemented from nitro::Database.
Definition at line 97 of file mysql_database.cpp.
References ClearResult(), nitro::exception::code(), Connection, Result, Row, RowId, and nitro::exception::what().
Referenced by Connect().
{
try
{
ClearResult();
if( Row == NULL )
{
Row = ( void * ) new MYSQL_ROW;
}
RowId = std::numeric_limits< std::size_t >::max();
if( mysql_query( ( MYSQL * ) Connection , theQueryString.c_str() ) )
{
throw( nitro::exception( std::string( "An error occured while query execution. Query string : " ) + theQueryString + " Error message : " + mysql_error( ( MYSQL * )Connection ) , 1 ) );
}
Result = ( void * )mysql_store_result( ( MYSQL * ) Connection );
}
catch( nitro::exception e )
{
throw( nitro::exception( std::string( "MySQLDatabase::Query( const std::string & theQueryString )::" ) + e.what() , e.code() ) );
}
catch( ... )
{
throw( nitro::exception( std::string( "MySQLDatabase::Query( const std::string & theQueryString )::An error occured" ) , 0 ) );
}
}

| std::size_t nitro::MySQLDatabase::RecordCount | ( | void | ) | [virtual] |
Function returns count of selected records.
| nitro::exception | Throws exception with the description of error. |
Implements nitro::Database.
Definition at line 159 of file mysql_database.cpp.
References nitro::exception::code(), Result, and nitro::exception::what().
{
try
{
if( Result == NULL )
{
return( 0 );
}
return( ( std::size_t )mysql_num_rows( ( MYSQL_RES * ) Result ) );
}
catch( nitro::exception e )
{
throw( nitro::exception( std::string( "MySQLDatabase::RecordCount( void )::" ) + e.what() , e.code() ) );
}
catch( ... )
{
throw( nitro::exception( std::string( "MySQLDatabase::RecordCount( void )::An error occured" ) , 0 ) );
}
}

void* nitro::MySQLDatabase::Connection [private] |
Connection.
Definition at line 310 of file mysql_database.h.
Referenced by Connect(), Disconnect(), FieldCount(), MySQLDatabase(), and Query().
void* nitro::MySQLDatabase::Result [private] |
Query result.
Definition at line 322 of file mysql_database.h.
Referenced by ClearResult(), FieldCount(), GetFieldName(), GetRecordField(), MySQLDatabase(), Query(), and RecordCount().
void* nitro::MySQLDatabase::Row [private] |
Fetched row.
Definition at line 334 of file mysql_database.h.
Referenced by Disconnect(), GetRecordField(), MySQLDatabase(), and Query().
std::size_t nitro::MySQLDatabase::RowId [private] |
Cursor of the row.
Definition at line 346 of file mysql_database.h.
Referenced by GetRecordField(), MySQLDatabase(), and Query().
1.6.1