#include <db/database.h>
Inherited by nitro::MySQLDatabase, and nitro::PGDatabase.
Public Member Functions | |
virtual void | Connect (const std::string &theConnectionString) |
virtual void | Disconnect (void)=0 |
virtual void | Query (const std::string &theQueryString) |
void | Select (const std::string &What, const std::string &Tables, const std::string &Condition="1 = 1") |
template<class iter > | |
void | FetchCommonArrays (iter &InsertIterator) |
template<class iter > | |
void | FetchAssociativeArrays (iter &InsertIterator) |
virtual void | Insert (const std::string &Table, const std::string &Fields, const std::string &Values) |
template<class cont1 , class cont2 > | |
void | Update (const std::string &Table, const cont1 &Fields, const cont2 &Values, const std::string &Condition="1 = 1") |
virtual void | Delete (const std::string &Table, const std::string &Condition="1 = 1") |
virtual std::size_t | RecordCount (void)=0 |
virtual std::size_t | FieldCount (void)=0 |
virtual const char * | GetRecordField (std::size_t i, std::size_t j)=0 |
virtual const char * | GetFieldName (std::size_t j)=0 |
virtual void | ClearResult (void)=0 |
virtual | ~Database () |
Definition at line 47 of file database.h.
nitro::Database::~Database | ( | ) | [virtual] |
virtual void nitro::Database::ClearResult | ( | void | ) | [pure virtual] |
Function clears results.
nitro::exception | Throws exception with the description of error. |
Implemented in nitro::MySQLDatabase, and nitro::PGDatabase.
Referenced by FetchAssociativeArrays(), FetchCommonArrays(), and Select().
void nitro::Database::Connect | ( | const std::string & | theConnectionString | ) | [virtual] |
Function provide database connection routine.
theConnectionString | - Connection settings. |
nitro::exception | Throws exception with the description of error. |
Reimplemented in nitro::MySQLDatabase, and nitro::PGDatabase.
Definition at line 8 of file database.cpp.
References nitro::exception::code(), and nitro::exception::what().
{ try { Connect( theConnectionString.c_str() ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Database::Connect( const std::string & theConnectionString )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Database::Connect( const std::string & theConnectionString )::An error occured" ) , 0 ) ); } }
void nitro::Database::Delete | ( | const std::string & | Table, | |
const std::string & | Condition = "1 = 1" | |||
) | [virtual] |
Function deletes record.
Table | - Table to delete from. | |
Condition | - Records selection condition. |
nitro::exception | Throws exception with the description of error. |
Definition at line 74 of file database.cpp.
References nitro::exception::code(), Query(), and nitro::exception::what().
{ try { Query( ( std::string( "DELETE FROM " ) + Table + " WHERE " + Condition ).c_str() ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Database::Delete( const std::string & Table , const std::string & Condition )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Database::Delete( const std::string & Table , const std::string & Condition )::An error occured" ) , 0 ) ); } }
virtual void nitro::Database::Disconnect | ( | void | ) | [pure virtual] |
Function closes connection.
nitro::exception | Throws exception with the description of error. |
Implemented in nitro::MySQLDatabase, and nitro::PGDatabase.
void nitro::Database::FetchAssociativeArrays | ( | iter & | InsertIterator | ) |
Function returns data as associative arrays.
InsertIterator | - Insert iterator. |
nitro::exception | Throws exception with the description of error. |
Definition at line 414 of file database.h.
References ClearResult(), nitro::exception::code(), FieldCount(), GetFieldName(), GetRecordField(), RecordCount(), and nitro::exception::what().
{ try { std::size_t RecCount( RecordCount() ); std::size_t FldCount( FieldCount() ); typedef typename iter::container_type::value_type RecordType; for( std::size_t i( 0 ) ; i < RecCount ; i++ ) { RecordType Record; for( std::size_t j( 0 ) ; j < FldCount ; j++ ) { Record.insert( std::pair< std::string , std::string >( std::string( GetFieldName( j ) ) , std::string( GetRecordField( i , j ) ) ) ); } ( * InsertIterator ) = Record; } ClearResult(); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Database::FetchAssociativeArrays( iter & InsertIterator )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Database::FetchAssociativeArrays( iter & InsertIterator )::An error occured" ) , 0 ) ); } }
void nitro::Database::FetchCommonArrays | ( | iter & | InsertIterator | ) |
Function returns data as common arrays.
InsertIterator | - Insert iterator. |
nitro::exception | Throws exception with the description of error. |
Definition at line 381 of file database.h.
References ClearResult(), nitro::exception::code(), FieldCount(), GetRecordField(), RecordCount(), and nitro::exception::what().
{ try { std::size_t RecCount( RecordCount() ); std::size_t FldCount( FieldCount() ); typedef typename iter::container_type::value_type RecordType; for( std::size_t i( 0 ) ; i < RecCount ; i++ ) { RecordType Record; for( std::size_t j( 0 ) ; j < FldCount ; j++ ) { Record.push_back( std::string( GetRecordField( i , j ) ) ); } ( * InsertIterator ) = Record; } ClearResult(); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Database::FetchCommonArrays( iter & InsertIterator )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Database::FetchCommonArrays( iter & InsertIterator )::An error occured" ) , 0 ) ); } }
virtual std::size_t nitro::Database::FieldCount | ( | void | ) | [pure virtual] |
Function returns count of fields.
nitro::exception | Throws exception with the description of error. |
Implemented in nitro::MySQLDatabase, and nitro::PGDatabase.
Referenced by FetchAssociativeArrays(), and FetchCommonArrays().
virtual const char* nitro::Database::GetFieldName | ( | std::size_t | j | ) | [pure virtual] |
Function returns name of the field.
j | - number of the field. |
nitro::exception | Throws exception with the description of error. |
Implemented in nitro::MySQLDatabase, and nitro::PGDatabase.
Referenced by FetchAssociativeArrays().
virtual const char* nitro::Database::GetRecordField | ( | std::size_t | i, | |
std::size_t | j | |||
) | [pure 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. |
Implemented in nitro::MySQLDatabase, and nitro::PGDatabase.
Referenced by FetchAssociativeArrays(), and FetchCommonArrays().
void nitro::Database::Insert | ( | const std::string & | Table, | |
const std::string & | Fields, | |||
const std::string & | Values | |||
) | [virtual] |
Function inserts record.
Table | - Table to insert in. | |
Fields | - List of fields. | |
Values | - Inserting values. |
nitro::exception | Throws exception with the description of error. |
Definition at line 58 of file database.cpp.
References nitro::exception::code(), Query(), and nitro::exception::what().
{ try { Query( ( std::string( "INSERT INTO " ) + Table + " ( " + Fields + " ) VALUES ( " + Values + " )" ).c_str() ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Database::Insert( const std::string & Table , const std::string & Fields , const std::string & Values )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Database::Insert( const std::string & Table , const std::string & Fields , const std::string & Values )::An error occured" ) , 0 ) ); } }
void nitro::Database::Query | ( | const std::string & | theQueryString | ) | [virtual] |
Executing query.
theQueryString | - Query. |
nitro::exception | Throws exception with the description of error. |
Reimplemented in nitro::MySQLDatabase, and nitro::PGDatabase.
Definition at line 24 of file database.cpp.
References nitro::exception::code(), and nitro::exception::what().
Referenced by Delete(), Insert(), Select(), and Update().
{ try { Query( theQueryString.c_str() ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Database::Query( const std::string & theQueryString )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Database::Query( const std::string & theQueryString )::An error occured" ) , 0 ) ); } }
virtual std::size_t nitro::Database::RecordCount | ( | void | ) | [pure virtual] |
Function returns count of selected records.
nitro::exception | Throws exception with the description of error. |
Implemented in nitro::MySQLDatabase, and nitro::PGDatabase.
Referenced by FetchAssociativeArrays(), and FetchCommonArrays().
void nitro::Database::Select | ( | const std::string & | What, | |
const std::string & | Tables, | |||
const std::string & | Condition = "1 = 1" | |||
) |
Function executes "select" query.
What | - List of fields. | |
Tables | - List of tables. | |
Condition | - Query selecting condition. |
nitro::exception | Throws exception with the description of error. |
Definition at line 40 of file database.cpp.
References ClearResult(), nitro::exception::code(), Query(), and nitro::exception::what().
{ try { ClearResult(); Query( ( std::string( "SELECT " ) + What + " FROM " + Tables + " WHERE " + Condition ).c_str() ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Database::Select( const std::string & What , const std::string & Tables , const std::string & Condition /* = 1 = 1 */ )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Database::Select( const std::string & What , const std::string & Tables , const std::string & Condition /* = 1 = 1 */ )::An error occured" ) , 0 ) ); } }
void nitro::Database::Update | ( | const std::string & | Table, | |
const cont1 & | Fields, | |||
const cont2 & | Values, | |||
const std::string & | Condition = "1 = 1" | |||
) |
Function updates record.
Table | - Table to update. | |
Fields | - List of fields. | |
Values | - Changing values. | |
Condition | - Query selecting condition. |
nitro::exception | Throws exception with the description of error. |
Definition at line 447 of file database.h.
References nitro::exception::code(), Query(), and nitro::exception::what().
{ try { std::string UpdateString( "" ); typename cont1::const_iterator i( Fields.begin() ); typename cont2::const_iterator j( Values.begin() ); for( ; i != Fields.end() && j != Values.end() ; i++ , j++ ) { if( i + 1 != Fields.end() && j + 1 != Values.end() ) { UpdateString += *i + " = '" + *j + "' , "; } else { UpdateString += *i + " = '" + *j + "'"; } } Query( std::string( "UPDATE " ) + Table + " SET " + UpdateString + " WHERE " + Condition ); } catch( nitro::exception e ) { 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() ) ); } catch( ... ) { 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 ) ); } }