Go to the documentation of this file.00001 #ifndef __PG_DATABASE_CPP__
00002 #define __PG_DATABASE_CPP__
00003
00004 #include <vector>
00005
00006 #include <db/pg_database.h>
00007 #include <utilities/string_utilities.h>
00008
00009 #include <libpq-fe.h>
00010
00011 namespace nitro{
00012
00013 PGDatabase::PGDatabase( void )
00014 {
00015 try
00016 {
00017 }
00018 catch( nitro::exception e )
00019 {
00020 throw( nitro::exception( std::string( "PGDatabase::PGDatabase( void )::" ) + e.what() , e.code() ) );
00021 }
00022 catch( ... )
00023 {
00024 throw( nitro::exception( std::string( "PGDatabase::PGDatabase( void )::An error occured" ) , 0 ) );
00025 }
00026 }
00027
00028 PGDatabase::PGDatabase( const std::string & theConnectionString )
00029 {
00030 try
00031 {
00032 Connect( theConnectionString );
00033 }
00034 catch( nitro::exception e )
00035 {
00036 throw( nitro::exception( std::string( "PGDatabase::PGDatabase( const std::string & theConnectionString )::" ) + e.what() , e.code() ) );
00037 }
00038 catch( ... )
00039 {
00040 throw( nitro::exception( std::string( "PGDatabase::PGDatabase( const std::string & theConnectionString )::An error occured" ) , 0 ) );
00041 }
00042 }
00043
00044 void PGDatabase::Connect( const std::string & theConnectionString )
00045 {
00046 try
00047 {
00048 Connection = ( void * )PQconnectdb( theConnectionString.c_str() );
00049
00050 if( PQstatus( ( PGconn * )Connection ) != CONNECTION_OK )
00051 {
00052 throw( nitro::exception( std::string( "An error occured while connecting to the server. Connection string : " ) + theConnectionString , 1 ) );
00053 }
00054
00055 std::string CharacterSet( nitro::Parsers::GetCommandLineParameter( theConnectionString , std::string( "character_set" ) , std::string( "" ) ) );
00056
00057 if( !CharacterSet.empty() )
00058 {
00059 Query( std::string( "SET NAMES " ) + CharacterSet );
00060 }
00061 }
00062 catch( nitro::exception e )
00063 {
00064 throw( nitro::exception( std::string( "PGDatabase::Connect( const std::string & theConnectionString )::" ) + e.what() , e.code() ) );
00065 }
00066 catch( ... )
00067 {
00068 throw( nitro::exception( std::string( "PGDatabase::Connect( const std::string & theConnectionString )::An error occured" ) , 0 ) );
00069 }
00070 }
00071
00072 void PGDatabase::Query( const std::string & theQueryString )
00073 {
00074 try
00075 {
00076 Result = ( void * )PQexec( ( PGconn * )Connection , theQueryString.c_str() );
00077
00078 ExecStatusType Status( PQresultStatus( ( PGresult * )Result ) );
00079
00080 if( Status == PGRES_BAD_RESPONSE || Status == PGRES_NONFATAL_ERROR || Status == PGRES_FATAL_ERROR )
00081 {
00082 throw( nitro::exception( std::string( "An error occured while executing query " ) + theQueryString , 1 ) );
00083 }
00084 }
00085 catch( nitro::exception e )
00086 {
00087 throw( nitro::exception( std::string( "PGDatabase::Query( const std::string & theQueryString )::" ) + e.what() , e.code() ) );
00088 }
00089 catch( ... )
00090 {
00091 throw( nitro::exception( std::string( "PGDatabase::Query( const std::string & theQueryString )::An error occured" ) , 0 ) );
00092 }
00093 }
00094
00095 void PGDatabase::Disconnect( void )
00096 {
00097 try
00098 {
00099 PQfinish( ( PGconn * )Connection );
00100 }
00101 catch( nitro::exception e )
00102 {
00103 throw( nitro::exception( std::string( "PGDatabase::Disconnect( void )::" ) + e.what() , e.code() ) );
00104 }
00105 catch( ... )
00106 {
00107 throw( nitro::exception( std::string( "PGDatabase::Disconnect( void )::An error occured" ) , 0 ) );
00108 }
00109 }
00110
00111 PGDatabase::~PGDatabase()
00112 {
00113 try
00114 {
00115 Disconnect();
00116 }
00117 catch( ... )
00118 {
00119 throw( nitro::exception( std::string( "PGDatabase::~PGDatabase()::An error occured" ) , 0 ) );
00120 }
00121 }
00122
00123 std::size_t PGDatabase::RecordCount( void )
00124 {
00125 try
00126 {
00127 return( PQntuples( ( const PGresult * ) Result ) );
00128 }
00129 catch( nitro::exception e )
00130 {
00131 throw( nitro::exception( std::string( "PGDatabase::RecordCount( void )::" ) + e.what() , e.code() ) );
00132 }
00133 catch( ... )
00134 {
00135 throw( nitro::exception( std::string( "PGDatabase::RecordCount( void )::An error occured" ) , 0 ) );
00136 }
00137 }
00138
00139 std::size_t PGDatabase::FieldCount( void )
00140 {
00141 try
00142 {
00143 return( PQnfields( ( const PGresult * ) Result ) );
00144 }
00145 catch( nitro::exception e )
00146 {
00147 throw( nitro::exception( std::string( "PGDatabase::FieldCount( void )::" ) + e.what() , e.code() ) );
00148 }
00149 catch( ... )
00150 {
00151 throw( nitro::exception( std::string( "PGDatabase::FieldCount( void )::An error occured" ) , 0 ) );
00152 }
00153 }
00154
00155 const char * PGDatabase::GetRecordField( std::size_t i , std::size_t j )
00156 {
00157 try
00158 {
00159 return( ( const char * )PQgetvalue( ( const PGresult * ) Result , ( int )i , ( int )j ) );
00160 }
00161 catch( nitro::exception e )
00162 {
00163 throw( nitro::exception( std::string( "PGDatabase::GetRecordField( std::size_t i , std::size_t j )::" ) + e.what() , e.code() ) );
00164 }
00165 catch( ... )
00166 {
00167 throw( nitro::exception( std::string( "PGDatabase::GetRecordField( std::size_t i , std::size_t j )::An error occured" ) , 0 ) );
00168 }
00169 }
00170
00171 const char * PGDatabase::GetFieldName( std::size_t j )
00172 {
00173 try
00174 {
00175 return( ( const char * )PQfname( ( const PGresult * ) Result , ( int )j ) );
00176 }
00177 catch( nitro::exception e )
00178 {
00179 throw( nitro::exception( std::string( "PGDatabase::GetFieldName( std::size_t j )::" ) + e.what() , e.code() ) );
00180 }
00181 catch( ... )
00182 {
00183 throw( nitro::exception( std::string( "PGDatabase::GetFieldName( std::size_t j )::An error occured" ) , 0 ) );
00184 }
00185 }
00186
00187 void PGDatabase::ClearResult( void )
00188 {
00189 try
00190 {
00191 if( Result != NULL )
00192 {
00193 PQclear( ( PGresult * )Result );
00194 Result = NULL;
00195 }
00196 }
00197 catch( nitro::exception e )
00198 {
00199 throw( nitro::exception( std::string( "PGDatabase::ClearResult( void )::" ) + e.what() , e.code() ) );
00200 }
00201 catch( ... )
00202 {
00203 throw( nitro::exception( std::string( "PGDatabase::ClearResult( void )::An error occured" ) , 0 ) );
00204 }
00205 }
00206
00207 };
00208
00209 #endif