Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Accessing: PostgreSql data base using libpq - C Library.

, 18 May 2009 CPOL
A set of class to access and manipule PostgreSql data base using libpq - C Library.
//
// PostgreSql.h
//
// PostgreSql imp.
//
// Copyright (c) 2007, Renato Tegon Forti
//
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//


#include "Exception.h"
#include "ResultSet.h"
#include "DataBase.h"
#include <libpq-fe.h> // Client programs that use libpq must include the header file libpq-fe.h and must link with the libpq library. 

class PostgreSql
	/// PostgreSql data base class
	/// http://www.postgresql.org/docs/8.3/interactive/libpq.html
{
	friend class DataBase<PostgreSql>;

public:

	PostgreSql()
		: _hasResult(false)
	{
	}

	virtual ~PostgreSql()
	{
	}
	
	void connect(const std::string& server, const std::string& user, const std::string& password, const std::string& database)
	{		
		_connectionHandlerPtr = PQsetdbLogin(
					server.c_str(),
                    NULL,
                    NULL,
                    NULL,
                    database.c_str(),
                    user.c_str(),
                    password.c_str()
					);

		// Check to see that the backend connection was successfully made
		if (PQstatus(_connectionHandlerPtr) != CONNECTION_OK)
		{
			std::string msg("Failed to connect to database: Error: " + std::string(PQerrorMessage(_connectionHandlerPtr)));

			PQfinish(_connectionHandlerPtr);

			throw DataBaseError(msg);
		}

	}

	void execute(const std::string& sql)
	{
		std::cout << sql << std::endl;

		if(_hasResult) 
			PQclear(_resultPtr);

		_resultPtr = PQexec(_connectionHandlerPtr, sql.c_str());

		if(_resultPtr == NULL)
		{
			throw DataBaseError("Failed to execute sql: Error: " + std::string(PQerrorMessage(_connectionHandlerPtr)));
		}

		_status = PQresultStatus(_resultPtr);

		if ((_status == PGRES_EMPTY_QUERY) || (_status == PGRES_BAD_RESPONSE) || (_status == PGRES_FATAL_ERROR))
		{
			PQclear(_resultPtr);

			_hasResult = false;

			throw DataBaseError("Failed to execute sql: Error: " 
				+ std::string(PQresStatus(_status)) 
				+ " : " + std::string(PQerrorMessage(_connectionHandlerPtr)));
		}

		(_status != PGRES_TUPLES_OK) ? PQclear(_resultPtr) : _hasResult = true;
	}

	void populate(ResultSet& rs)
	{
		if(_status != PGRES_TUPLES_OK) 
			throw DataBaseError("This command don't support results");

		if(_hasResult == false)
			throw DataBaseError("Any results available");

		unsigned int num_tuples = PQntuples(_resultPtr);
		unsigned int num_fields = PQnfields(_resultPtr);

		for(int i = 0; i < num_tuples; ++i)
		{
			std::vector<std::string> myRow;

			for(int j = 0; j < num_fields; ++j)
			{
				myRow.push_back(PQgetvalue(_resultPtr, i, j));
			}

			rs.addRow(myRow);
		}
	
		PQclear(_resultPtr);

		_hasResult = false;
	}

protected:

	void close(void)
		/// close the connection to the database and cleanup
	{
		PQfinish(_connectionHandlerPtr);
	}

	
private:
	ExecStatusType	_status;
	bool			_hasResult;
	PGresult*		_resultPtr;
	PGconn*			_connectionHandlerPtr;

}; // PostgreSql

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Renato Tegon Forti
Systems Engineer
Brazil Brazil
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 18 May 2009
Article Copyright 2009 by Renato Tegon Forti
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid