Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » General » Downloads
 
Add your own
alternative version
Go to top

PostgreSQL/libpqxx Class Generator

, 18 Aug 2006
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

Share

About the Author

_oti

Australia Australia
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 18 Aug 2006
Article Copyright 2005 by _oti
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid