Public Member Functions | Private Member Functions | Private Attributes

nitro::MySQLDatabase Class Reference

Class provides MySQL routine. More...

#include <db/mysql_database.h>

Inherits nitro::Database.

Collaboration diagram for nitro::MySQLDatabase:
Collaboration graph
[legend]

List of all members.

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

Detailed Description

Class provides MySQL routine.

Author:
Dodonov A.A.

Definition at line 50 of file mysql_database.h.


Constructor & Destructor Documentation

nitro::MySQLDatabase::MySQLDatabase ( void   ) 

Constructor.

Exceptions:
nitro::exception Throws exception with the description of error.
Author:
Dodonov A.A.

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 ) );
                }
        }

Here is the call graph for this function:

nitro::MySQLDatabase::MySQLDatabase ( const std::string &  theConnectionString  ) 

Constructor.

Parameters:
theConnectionString - Connection settings.
Exceptions:
nitro::exception Throws exception with the description of error.
Author:
Dodonov A.A.

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 ) );
                }
        }

Here is the call graph for this function:

nitro::MySQLDatabase::~MySQLDatabase (  )  [virtual]

Destructor.

Author:
Dodonov A.A.

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 ) );
                }
        }

Here is the call graph for this function:

nitro::MySQLDatabase::MySQLDatabase ( const MySQLDatabase DBObject  )  [inline, private]

Private copy-constructor.

Parameters:
DBObject - database object.
Author:
Dodonov A.A.

Definition at line 282 of file mysql_database.h.

{}


Member Function Documentation

void nitro::MySQLDatabase::ClearResult ( void   )  [virtual]

Function clears results.

Exceptions:
nitro::exception Throws exception with the description of error.
Author:
Dodonov A.A.

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 ) );
                }
        }

Here is the call graph for this function:

void nitro::MySQLDatabase::Connect ( const std::string &  theConnectionString  )  [virtual]

Function provide database connection routine.

Parameters:
theConnectionString - Connection settings.
Exceptions:
nitro::exception Throws exception with the description of error.
Author:
Dodonov A.A.

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 ) );
                }
        }

Here is the call graph for this function:

void nitro::MySQLDatabase::Disconnect ( void   )  [virtual]

Function closes connection.

Exceptions:
nitro::exception Throws exception with the description of error.
Author:
Dodonov A.A.

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 ) );
                }
        }

Here is the call graph for this function:

std::size_t nitro::MySQLDatabase::FieldCount ( void   )  [virtual]

Function returns count of fields.

Returns:
Count of fields.
Exceptions:
nitro::exception Throws exception with the description of error.
Author:
Dodonov A.A.

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 ) );
                }
        }

Here is the call graph for this function:

const char * nitro::MySQLDatabase::GetFieldName ( std::size_t  j  )  [virtual]

Function returns name of the field.

Parameters:
j - number of the field.
Returns:
Name of the field.
Exceptions:
nitro::exception Throws exception with the description of error.
Author:
Dodonov A.A.

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 ) );
                }
        }

Here is the call graph for this function:

const char * nitro::MySQLDatabase::GetRecordField ( std::size_t  i,
std::size_t  j 
) [virtual]

Function returns value of the field.

Parameters:
i - number of the record.
j - number of the field.
Returns:
Count of fields.
Exceptions:
nitro::exception Throws exception with the description of error.
Author:
Dodonov A.A.

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 ) );
                }
        }

Here is the call graph for this function:

MySQLDatabase nitro::MySQLDatabase::operator= ( const MySQLDatabase DBObject  )  [inline, private]

Private assign operator.

Parameters:
DBObject - database object.
Author:
Dodonov A.A.

Definition at line 298 of file mysql_database.h.

{return( *this );}

void nitro::MySQLDatabase::Query ( const std::string &  theQueryString  )  [virtual]

Executing query.

Parameters:
theQueryString - Query.
Exceptions:
nitro::exception Throws exception with the description of error.
Author:
Dodonov A.A.

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 ) );
                }
        }

Here is the call graph for this function:

std::size_t nitro::MySQLDatabase::RecordCount ( void   )  [virtual]

Function returns count of selected records.

Returns:
Count of records.
Exceptions:
nitro::exception Throws exception with the description of error.
Author:
Dodonov A.A.

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 ) );
                }
        }

Here is the call graph for this function:


Member Data Documentation

Connection.

Author:
Dodonov A.A.

Definition at line 310 of file mysql_database.h.

Referenced by Connect(), Disconnect(), FieldCount(), MySQLDatabase(), and Query().

Query result.

Author:
Dodonov A.A.

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.

Author:
Dodonov A.A.

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.

Author:
Dodonov A.A.

Definition at line 346 of file mysql_database.h.

Referenced by GetRecordField(), MySQLDatabase(), and Query().


The documentation for this class was generated from the following files:

Generated by  doxygen 1.6.1