00001 // 00002 // Copyright (C) 2004-2008 Maciej Sobczak, Stephen Hutton 00003 // Distributed under the Boost Software License, Version 1.0. 00004 // (See accompanying file LICENSE_1_0.txt or copy at 00005 // http://www.boost.org/LICENSE_1_0.txt) 00006 // 00007 00008 #define SOCI_POSTGRESQL_SOURCE 00009 #include "soci-postgresql.h" 00010 #include <libpq/libpq-fs.h> // libpq 00011 #include <cctype> 00012 #include <cstdio> 00013 #include <cstring> 00014 #include <ctime> 00015 #include <sstream> 00016 00017 #ifdef SOCI_PGSQL_NOPARAMS 00018 #define SOCI_PGSQL_NOBINDBYNAME 00019 #endif // SOCI_PGSQL_NOPARAMS 00020 00021 #ifdef _MSC_VER 00022 #pragma warning(disable:4355 4996) 00023 #endif 00024 00025 using namespace soci; 00026 using namespace soci::details; 00027 00028 00029 postgresql_session_backend::postgresql_session_backend( 00030 std::string const & connectString) 00031 : statementCount_(0) 00032 { 00033 PGconn * conn = PQconnectdb(connectString.c_str()); 00034 if (conn == NULL || PQstatus(conn) != CONNECTION_OK) 00035 { 00036 std::string msg = "Cannot establish connection to the database."; 00037 if (conn != NULL) 00038 { 00039 msg += '\n'; 00040 msg += PQerrorMessage(conn); 00041 PQfinish(conn); 00042 } 00043 00044 throw soci_error(msg); 00045 } 00046 00047 conn_ = conn; 00048 } 00049 00050 postgresql_session_backend::~postgresql_session_backend() 00051 { 00052 clean_up(); 00053 } 00054 00055 namespace // unnamed 00056 { 00057 00058 // helper function for hardoded queries 00059 void hard_exec(PGconn * conn, char const * query, char const * errMsg) 00060 { 00061 PGresult * result = PQexec(conn, query); 00062 00063 if (result == NULL) 00064 { 00065 throw soci_error(errMsg); 00066 } 00067 00068 ExecStatusType const status = PQresultStatus(result); 00069 if (status != PGRES_COMMAND_OK) 00070 { 00071 throw soci_error(PQresultErrorMessage(result)); 00072 } 00073 00074 PQclear(result); 00075 } 00076 00077 } // namespace unnamed 00078 00079 void postgresql_session_backend::begin() 00080 { 00081 hard_exec(conn_, "BEGIN", "Cannot begin transaction."); 00082 } 00083 00084 void postgresql_session_backend::commit() 00085 { 00086 hard_exec(conn_, "COMMIT", "Cannot commit transaction."); 00087 } 00088 00089 void postgresql_session_backend::rollback() 00090 { 00091 hard_exec(conn_, "ROLLBACK", "Cannot rollback transaction."); 00092 } 00093 00094 void postgresql_session_backend::clean_up() 00095 { 00096 if (conn_ != NULL) 00097 { 00098 PQfinish(conn_); 00099 conn_ = NULL; 00100 } 00101 } 00102 00103 std::string postgresql_session_backend::get_next_statement_name() 00104 { 00105 char nameBuf[20]; // arbitrary length 00106 sprintf(nameBuf, "st_%d", ++statementCount_); 00107 return nameBuf; 00108 } 00109 00110 postgresql_statement_backend * postgresql_session_backend::make_statement_backend() 00111 { 00112 return new postgresql_statement_backend(*this); 00113 } 00114 00115 postgresql_rowid_backend * postgresql_session_backend::make_rowid_backend() 00116 { 00117 return new postgresql_rowid_backend(*this); 00118 } 00119 00120 postgresql_blob_backend * postgresql_session_backend::make_blob_backend() 00121 { 00122 return new postgresql_blob_backend(*this); 00123 }
Generated on Sun Oct 3 2010 17:42:16 for EXTRAS-SOCI by Doxygen 1.7.1