Go to the documentation of this file.00001 #ifndef __DATABASE_H__
00002 #define __DATABASE_H__
00003
00004 #include <utility>
00005
00006 #include <utilities/cpp_utilities.h>
00007 #include <utilities/exception.h>
00008
00009 #if !defined( WIN32_PLATFORM ) && !defined( NIX_PLATFORM ) && !defined( MINGW_PLATFORM ) && !defined( CYGWIN_PLATFORM )
00010 #define WIN32_PLATFORM
00011 #endif
00012
00013 #ifdef DATABASE
00014 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM ) || defined( CYGWIN_PLATFORM )
00015 #define DATABASE_DLL_ENTITY __declspec( dllexport )
00016 #endif
00017
00018 #if defined( NIX_PLATFORM )
00019 #define DATABASE_DLL_ENTITY
00020 #endif
00021 #else
00022 #if defined( WIN32_PLATFORM )
00023 #define DATABASE_DLL_ENTITY __declspec( dllimport )
00024 #pragma comment( lib , "database.lib" )
00025 #endif
00026
00027 #if defined( CYGWIN_PLATFORM ) || defined( MINGW_PLATFORM )
00028 #define DATABASE_DLL_ENTITY __declspec( dllimport )
00029 #endif
00030
00031 #if defined( NIX_PLATFORM )
00032 #define DATABASE_DLL_ENTITY
00033 #endif
00034 #endif
00035
00036 #include <string>
00037 #include <vector>
00038
00039 namespace nitro{
00040
00041 #define COMMON_RECORD std::vector< std::string >
00042 #define COMMON_RECORD_SET std::vector< COMMON_RECORD >
00043
00044 #define ASSOC_RECORD std::map< std::string , std::string >
00045 #define ASSOC_RECORD_SET std::vector< ASSOC_RECORD >
00046
00047 class DATABASE_DLL_ENTITY Database{
00048
00049 public:
00050
00069 virtual void Connect( const std::string & theConnectionString );
00070
00085 virtual void Disconnect( void ) = 0;
00086
00105 virtual void Query( const std::string & theQueryString );
00106
00133 void Select( const std::string & What , const std::string & Tables , const std::string & Condition = "1 = 1" );
00134
00153 template< class iter >void FetchCommonArrays( iter & InsertIterator );
00154
00173 template< class iter >void FetchAssociativeArrays( iter & InsertIterator );
00174
00201 virtual void Insert( const std::string & Table , const std::string & Fields , const std::string & Values );
00202
00233 template< class cont1 , class cont2 >void Update( const std::string & Table , const cont1 & Fields , const cont2 & Values , const std::string & Condition = "1 = 1" );
00234
00257 virtual void Delete( const std::string & Table , const std::string & Condition = "1 = 1" );
00258
00277 virtual std::size_t RecordCount( void ) = 0;
00278
00297 virtual std::size_t FieldCount( void ) = 0;
00298
00325 virtual const char * GetRecordField( std::size_t i , std::size_t j ) = 0;
00326
00349 virtual const char * GetFieldName( std::size_t j ) = 0;
00350
00365 virtual void ClearResult( void ) = 0;
00366
00377 virtual ~Database();
00378
00379 };
00380
00381 template< class iter >void Database::FetchCommonArrays( iter & InsertIterator )
00382 {
00383 try
00384 {
00385 std::size_t RecCount( RecordCount() );
00386 std::size_t FldCount( FieldCount() );
00387
00388 typedef typename iter::container_type::value_type RecordType;
00389
00390 for( std::size_t i( 0 ) ; i < RecCount ; i++ )
00391 {
00392 RecordType Record;
00393
00394 for( std::size_t j( 0 ) ; j < FldCount ; j++ )
00395 {
00396 Record.push_back( std::string( GetRecordField( i , j ) ) );
00397 }
00398
00399 ( * InsertIterator ) = Record;
00400 }
00401
00402 ClearResult();
00403 }
00404 catch( nitro::exception e )
00405 {
00406 throw( nitro::exception( std::string( "Database::FetchCommonArrays( iter & InsertIterator )::" ) + e.what() , e.code() ) );
00407 }
00408 catch( ... )
00409 {
00410 throw( nitro::exception( std::string( "Database::FetchCommonArrays( iter & InsertIterator )::An error occured" ) , 0 ) );
00411 }
00412 }
00413
00414 template< class iter >void Database::FetchAssociativeArrays( iter & InsertIterator )
00415 {
00416 try
00417 {
00418 std::size_t RecCount( RecordCount() );
00419 std::size_t FldCount( FieldCount() );
00420
00421 typedef typename iter::container_type::value_type RecordType;
00422
00423 for( std::size_t i( 0 ) ; i < RecCount ; i++ )
00424 {
00425 RecordType Record;
00426
00427 for( std::size_t j( 0 ) ; j < FldCount ; j++ )
00428 {
00429 Record.insert( std::pair< std::string , std::string >( std::string( GetFieldName( j ) ) , std::string( GetRecordField( i , j ) ) ) );
00430 }
00431
00432 ( * InsertIterator ) = Record;
00433 }
00434
00435 ClearResult();
00436 }
00437 catch( nitro::exception e )
00438 {
00439 throw( nitro::exception( std::string( "Database::FetchAssociativeArrays( iter & InsertIterator )::" ) + e.what() , e.code() ) );
00440 }
00441 catch( ... )
00442 {
00443 throw( nitro::exception( std::string( "Database::FetchAssociativeArrays( iter & InsertIterator )::An error occured" ) , 0 ) );
00444 }
00445 }
00446
00447 template< class cont1 , class cont2 >void Database::Update( const std::string & Table , const cont1 & Fields , const cont2 & Values , const std::string & Condition )
00448 {
00449 try
00450 {
00451 std::string UpdateString( "" );
00452
00453 typename cont1::const_iterator i( Fields.begin() );
00454 typename cont2::const_iterator j( Values.begin() );
00455
00456 for( ; i != Fields.end() && j != Values.end() ; i++ , j++ )
00457 {
00458 if( i + 1 != Fields.end() && j + 1 != Values.end() )
00459 {
00460 UpdateString += *i + " = '" + *j + "' , ";
00461 }
00462 else
00463 {
00464 UpdateString += *i + " = '" + *j + "'";
00465 }
00466 }
00467
00468 Query( std::string( "UPDATE " ) + Table + " SET " + UpdateString + " WHERE " + Condition );
00469 }
00470 catch( nitro::exception e )
00471 {
00472 throw( nitro::exception( std::string( "Database::Update( const std::string & Table , const cont1 & Fields , const cont2 & Values , const std::string & Condition /* = 1 = 1 */ )::" ) + e.what() , e.code() ) );
00473 }
00474 catch( ... )
00475 {
00476 throw( nitro::exception( std::string( "Database::Update( const std::string & Table , const cont1 & Fields , const cont2 & Values , const std::string & Condition /* = 1 = 1 */ )::An error occured" ) , 0 ) );
00477 }
00478 }
00479
00480 };
00481
00482 #endif