Click here to Skip to main content
15,881,803 members
Articles / Database Development / SQL Server

PostgreSQL/libpqxx Class Generator

Rate me:
Please Sign up or sign in to vote.
4.87/5 (15 votes)
18 Aug 200627 min read 82.5K   1.9K   37  
Automated generation of PostgreSQL data transfer classes.
/***************************************************************************
 DataManager.h - Singleton class that manages connection to the database.
                 This file also include the declaration of the dmwork class,
				 which is a custom transaction object, designed to work with
				 the data manager's connection pooling.

 begin     : August 2006
 copyright : (C) 2006 by Phil Cairns
 email     : oti169@hotmail.com
 $Id: DataManager.h 2080 2006-08-18 01:53:19Z phil $

 This code may be used in compiled form in any way you desire (including
 commercial use). The code may be redistributed unmodified by any means
 providing it is not sold for profit without the authors written consent,
 and providing that this notice and the authors name and all copyright
 notices remains intact.

 This software is provided "as is" without express or implied warranty. Use
 it at your own risk!
 ***************************************************************************/

#pragma once

#include "Singleton.h"
#include "SyncColl.h"

class dmwork;

class DataManager
{
	friend class Pagaros::Singleton<DataManager>;
	friend class dmwork;

private:
	DataManager(void);
	~DataManager(void);

private:
	Pagaros::SyncList<pqxx::connection*> _dbConnPool;
	static Pagaros::SyncObject _trxSync;
	std::string _connStr;

private:
	pqxx::connection* checkOutDbConn();
	void checkInDbConn(pqxx::connection* pConn);
	bool processSQLFile(dmwork& trx, const std::string& fName);
	void strToVersion(const std::string& s, __int64& i64v);
	void versionToStr(__int64 i64v, std::string& s);

public:
	pqxx::connection* dbConn() { return checkOutDbConn(); }
	std::string connStr() { return _connStr; }
	void cleanUp();
};
typedef Pagaros::Singleton<DataManager> DataManagerS;

// Transaction that ensures serialised access to the connection
class dmwork : public pqxx::basic_transaction
{
	friend class DataManager;
private:
	bool _monitoring;
	pqxx::connection* _myConn;
public:
	typedef pqxx::isolation_traits<pqxx::read_committed> isolation_tag;
	explicit dmwork(pqxx::connection* C, const std::string &TName, bool monitoring=true)	//[t1]
		: basic_transaction(*_myConn, isolation_tag::name(), TName)
		, _myConn(C)
		, _monitoring(monitoring)
	{
		Begin();
	}
	explicit dmwork(pqxx::connection* C, const char* TName, bool monitoring=true)	//[t1]
		: basic_transaction(*C, isolation_tag::name(), std::string(TName))
		, _myConn(C)
		, _monitoring(monitoring)
	{
		Begin();
	}
	explicit dmwork(pqxx::connection* C, bool monitoring=true)				//[t1]
		: basic_transaction(*C, isolation_tag::name(), std::string())
		, _myConn(C)
		, _monitoring(monitoring)
	{
		Begin();
	}
	virtual ~dmwork() throw ()
	{
#ifdef PQXX_QUIET_DESTRUCTORS
		pqxx::internal::disable_noticer Quiet(conn());
#endif
		End();
		if (_monitoring)
		{
			DataManagerS::instance().checkInDbConn(_myConn);
		}
	}
protected:
	virtual const char *classname() const throw () { return "dm_transaction"; }
};

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions